comparison src/c/http.c @ 1937:94f9570671f0

Fix compilation of apps that don't use database; fix HTTP pipelining
author Adam Chlipala <adam@chlipala.net>
date Tue, 17 Dec 2013 20:12:33 -0500
parents 6745eafff617
children d02c1a0d8082
comparison
equal deleted inserted replaced
1936:6745eafff617 1937:94f9570671f0
71 } 71 }
72 72
73 static void *worker(void *data) { 73 static void *worker(void *data) {
74 int me = *(int *)data; 74 int me = *(int *)data;
75 uw_context ctx = uw_request_new_context(me, &uw_application, NULL, log_error, log_debug); 75 uw_context ctx = uw_request_new_context(me, &uw_application, NULL, log_error, log_debug);
76 size_t buf_size = 2; 76 size_t buf_size = 1024;
77 char *buf = malloc(buf_size), *back = buf; 77 char *buf = malloc(buf_size), *back = buf;
78 uw_request_context rc = uw_new_request_context(); 78 uw_request_context rc = uw_new_request_context();
79 int sock = 0; 79 int sock = 0;
80 80
81 while (1) { 81 while (1) {
97 new_buf = realloc(buf, buf_size); 97 new_buf = realloc(buf, buf_size);
98 back = new_buf + (back - buf); 98 back = new_buf + (back - buf);
99 buf = new_buf; 99 buf = new_buf;
100 } 100 }
101 101
102 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
103
104 if (r < 0) {
105 if (!quiet)
106 fprintf(stderr, "Recv failed\n");
107 close(sock);
108 sock = 0;
109 break;
110 }
111
112 if (r == 0) {
113 if (!quiet)
114 printf("Connection closed.\n");
115 close(sock);
116 sock = 0;
117 break;
118 }
119
120 back += r;
121 *back = 0; 102 *back = 0;
122 103 body = strstr(buf, "\r\n\r\n");
123 if ((body = strstr(buf, "\r\n\r\n"))) { 104 if (body == NULL) {
105 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
106
107 if (r < 0) {
108 if (!quiet)
109 fprintf(stderr, "Recv failed\n");
110 close(sock);
111 sock = 0;
112 break;
113 }
114
115 if (r == 0) {
116 if (!quiet)
117 printf("Connection closed.\n");
118 close(sock);
119 sock = 0;
120 break;
121 }
122
123 back += r;
124 *back = 0;
125 }
126
127 if (body != NULL || (body = strstr(buf, "\r\n\r\n"))) {
124 request_result rr; 128 request_result rr;
125 int should_keepalive = 0; 129 int should_keepalive = 0;
126 130
127 body[0] = body[1] = 0; 131 body[0] = body[1] = 0;
128 body += 4; 132 body += 4;
211 else 215 else
212 query_string = NULL; 216 query_string = NULL;
213 217
214 s = headers; 218 s = headers;
215 while ((s2 = strchr(s, '\r'))) { 219 while ((s2 = strchr(s, '\r'))) {
220 if (s2 == s) {
221 *s = 0;
222 break;
223 }
224
216 s = s2; 225 s = s2;
217 226
218 if (s[1] == 0) 227 if (s[1] == 0)
219 break; 228 break;
220 229