Mercurial > urweb
comparison src/c/driver.c @ 730:1b1047992ecf
POST support
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 16 Apr 2009 14:35:01 -0400 |
parents | a5d8b470d7ca |
children | f2a2be93331c |
comparison
equal
deleted
inserted
replaced
729:7c6b6c3c7b79 | 730:1b1047992ecf |
---|---|
105 static void *worker(void *data) { | 105 static void *worker(void *data) { |
106 int me = *(int *)data, retries_left = MAX_RETRIES; | 106 int me = *(int *)data, retries_left = MAX_RETRIES; |
107 uw_context ctx = new_context(); | 107 uw_context ctx = new_context(); |
108 | 108 |
109 while (1) { | 109 while (1) { |
110 char buf[uw_bufsize+1], *back = buf, *s; | 110 char buf[uw_bufsize+1], *back = buf, *s, *post; |
111 int sock, dont_close = 0; | 111 int sock, dont_close = 0; |
112 | 112 |
113 pthread_mutex_lock(&queue_mutex); | 113 pthread_mutex_lock(&queue_mutex); |
114 while (empty()) | 114 while (empty()) |
115 pthread_cond_wait(&queue_cond, &queue_mutex); | 115 pthread_cond_wait(&queue_cond, &queue_mutex); |
130 if (r == 0) { | 130 if (r == 0) { |
131 printf("Connection closed.\n"); | 131 printf("Connection closed.\n"); |
132 break; | 132 break; |
133 } | 133 } |
134 | 134 |
135 printf("Received %d bytes.\n", r); | 135 //printf("Received %d bytes.\n", r); |
136 | 136 |
137 back += r; | 137 back += r; |
138 *back = 0; | 138 *back = 0; |
139 | 139 |
140 if (s = strstr(buf, "\r\n\r\n")) { | 140 if (s = strstr(buf, "\r\n\r\n")) { |
141 failure_kind fk; | 141 failure_kind fk; |
142 char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs; | 142 int is_post = 0; |
143 char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs, *after_headers; | |
143 | 144 |
144 s[2] = 0; | 145 s[2] = 0; |
146 after_headers = s + 4; | |
145 | 147 |
146 if (!(s = strstr(buf, "\r\n"))) { | 148 if (!(s = strstr(buf, "\r\n"))) { |
147 fprintf(stderr, "No newline in buf\n"); | 149 fprintf(stderr, "No newline in buf\n"); |
148 break; | 150 break; |
149 } | 151 } |
157 if (!strsep(&s, " ")) { | 159 if (!strsep(&s, " ")) { |
158 fprintf(stderr, "No first space in HTTP command\n"); | 160 fprintf(stderr, "No first space in HTTP command\n"); |
159 break; | 161 break; |
160 } | 162 } |
161 | 163 |
162 if (strcmp(cmd, "GET")) { | 164 uw_set_headers(ctx, headers); |
163 fprintf(stderr, "Not ready for non-get command: %s\n", cmd); | 165 |
166 if (!strcmp(cmd, "POST")) { | |
167 char *clen_s = uw_Basis_requestHeader(ctx, "Content-length"); | |
168 if (!clen_s) { | |
169 printf("No Content-length with POST\n"); | |
170 goto done; | |
171 } | |
172 int clen = atoi(clen_s); | |
173 if (clen < 0) { | |
174 printf("Negative Content-length with POST\n"); | |
175 goto done; | |
176 } | |
177 | |
178 while (back - after_headers < clen) { | |
179 r = recv(sock, back, uw_bufsize - (back - buf), 0); | |
180 | |
181 if (r < 0) { | |
182 fprintf(stderr, "Recv failed\n"); | |
183 goto done; | |
184 } | |
185 | |
186 if (r == 0) { | |
187 printf("Connection closed.\n"); | |
188 goto done; | |
189 } | |
190 | |
191 back += r; | |
192 *back = 0; | |
193 } | |
194 | |
195 is_post = 1; | |
196 } else if (strcmp(cmd, "GET")) { | |
197 fprintf(stderr, "Not ready for non-GET/POST command: %s\n", cmd); | |
164 break; | 198 break; |
165 } | 199 } |
166 | 200 |
167 path = s; | 201 path = s; |
168 if (!strsep(&s, " ")) { | 202 if (!strsep(&s, " ")) { |
169 fprintf(stderr, "No second space in HTTP command\n"); | 203 fprintf(stderr, "No second space in HTTP command\n"); |
170 break; | 204 break; |
171 } | 205 } |
172 | |
173 uw_set_headers(ctx, headers); | |
174 | 206 |
175 if (!strcmp(path, "/.msgs")) { | 207 if (!strcmp(path, "/.msgs")) { |
176 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client"); | 208 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client"); |
177 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass"); | 209 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass"); |
178 | 210 |
186 fprintf(stderr, "Missing fields in .msgs request: %s, %s\n\n", id, pass); | 218 fprintf(stderr, "Missing fields in .msgs request: %s, %s\n\n", id, pass); |
187 } | 219 } |
188 break; | 220 break; |
189 } | 221 } |
190 | 222 |
191 if (inputs = strchr(path, '?')) { | 223 if (is_post) |
224 inputs = after_headers; | |
225 else if (inputs = strchr(path, '?')) | |
226 *inputs++ = 0; | |
227 if (inputs) { | |
192 char *name, *value; | 228 char *name, *value; |
193 *inputs++ = 0; | |
194 | 229 |
195 while (*inputs) { | 230 while (*inputs) { |
196 name = inputs; | 231 name = inputs; |
197 if (inputs = strchr(inputs, '&')) | 232 if (inputs = strchr(inputs, '&')) |
198 *inputs++ = 0; | 233 *inputs++ = 0; |
278 uw_memstats(ctx); | 313 uw_memstats(ctx); |
279 break; | 314 break; |
280 } | 315 } |
281 } | 316 } |
282 | 317 |
318 done: | |
283 if (!dont_close) | 319 if (!dont_close) |
284 close(sock); | 320 close(sock); |
285 uw_reset(ctx); | 321 uw_reset(ctx); |
286 } | 322 } |
287 } | 323 } |