-
Notifications
You must be signed in to change notification settings - Fork 166
Open
Description
I'm having trouble changing the webapp example to allow reading the request body. My idea was to detach after receiving the fields, receive the body, attach again to send the status, detach one more time to send the response body. Everything works fine until I try to detach the second time. The program simply blocks and never returns. I'm not sure if this is a bug or a mistake in my understanding of how the library should be used.
coroutine void html_worker(int s) {
s = http_attach(s);
assert(s >= 0);
char command[256];
char resource[256];
int rc = http_recvrequest(s, command, sizeof(command),
resource, sizeof(resource), -1);
assert(rc == 0);
printf("%s %s HTTP/1.1\n", command, resource);
/* This will hold the content length */
int sz = 0;
while(1) {
char name[256];
char value[256];
rc = http_recvfield(s, name, sizeof(name), value, sizeof(value), -1);
if(rc < 0 && errno == EPIPE) break;
printf("%s: %s\n", name, value);
if(!strcmp(name, "Content-Length")) sz = atoi(value);
assert(rc == 0);
}
/* Detach so we can read the request body. */
s = http_detach(s, -1);
assert(s >= 0);
/* Read the request body */
char buf[sz + 1];
rc = brecv(s, buf, sz, -1);
assert(rc == 0);
buf[sz] = '\0';
printf("\n%s\n", buf);
/* Attach again so we can send the reply. */
s = http_attach(s);
assert(s >= 0);
/* Send an HTTP reply. */
rc = http_sendstatus(s, 200, "OK", -1);
assert(rc == 0);
/* Perform HTTP terminal handshake. */
s = http_detach(s, -1); /* <--------- This is where the program blocks forever.
assert(s >= 0);
/* Send the HTML to the browser. */
rc = bsend(s, html, strlen(html), -1);
assert(rc == 0);
/* Close the underlying TCP connection. */
rc = tcp_close(s, -1);
assert(rc == 0);
}Metadata
Metadata
Assignees
Labels
No labels