adamc@1268
|
1 #include "config.h"
|
adamc@1268
|
2
|
adamc@853
|
3 #include <stdio.h>
|
adamc@853
|
4 #include <string.h>
|
adamc@853
|
5 #include <stdlib.h>
|
adamc@853
|
6 #include <sys/types.h>
|
adamc@853
|
7 #include <sys/socket.h>
|
adamc@853
|
8 #include <netinet/in.h>
|
adamc@853
|
9 #include <unistd.h>
|
adamc@853
|
10 #include <signal.h>
|
adamc@853
|
11
|
adamc@853
|
12 #include <pthread.h>
|
adamc@853
|
13
|
adamc@853
|
14 #include "urweb.h"
|
grrwlf@1997
|
15 #include "request.h"
|
adamc@853
|
16
|
adamc@853
|
17 #define MAX_RETRIES 5
|
adamc@853
|
18
|
adamc@1152
|
19 void *memmem(const void *b1, size_t len1, const void *b2, size_t len2);
|
adamc@1152
|
20
|
adam@1327
|
21 static int try_rollback(uw_context ctx, int will_retry, void *logger_data, uw_logger log_error) {
|
adam@1327
|
22 int r = uw_rollback(ctx, will_retry);
|
adamc@853
|
23
|
adamc@853
|
24 if (r) {
|
adamc@856
|
25 log_error(logger_data, "Error running SQL ROLLBACK\n");
|
adamc@853
|
26 uw_reset(ctx);
|
adamc@1115
|
27 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
|
adamc@853
|
28 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
|
adamc@853
|
29 uw_write(ctx, "Error running SQL ROLLBACK\n");
|
adamc@1115
|
30 uw_set_error_message(ctx, "Database error; you are probably out of storage space.");
|
adamc@853
|
31 }
|
adamc@853
|
32
|
adamc@853
|
33 return r;
|
adamc@853
|
34 }
|
adamc@853
|
35
|
grrwlf@1997
|
36 uw_context uw_request_new_context(int id, uw_app *app, uw_loggers *ls) {
|
grrwlf@1997
|
37 void *logger_data = ls->logger_data;
|
grrwlf@1997
|
38 uw_logger log_debug = ls->log_debug;
|
grrwlf@1997
|
39 uw_logger log_error = ls->log_error;
|
grrwlf@1997
|
40 uw_context ctx = uw_init(id, ls);
|
adamc@853
|
41 int retries_left = MAX_RETRIES;
|
adamc@1094
|
42 uw_set_app(ctx, app);
|
adamc@853
|
43
|
adamc@853
|
44 while (1) {
|
adamc@853
|
45 failure_kind fk = uw_begin_init(ctx);
|
adamc@853
|
46
|
adamc@853
|
47 if (fk == SUCCESS) {
|
adamc@856
|
48 log_debug(logger_data, "Database connection initialized.\n");
|
adamc@853
|
49 break;
|
adamc@853
|
50 } else if (fk == BOUNDED_RETRY) {
|
adamc@853
|
51 if (retries_left) {
|
adamc@856
|
52 log_debug(logger_data, "Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
|
adamc@853
|
53 --retries_left;
|
adamc@853
|
54 } else {
|
adamc@856
|
55 log_error(logger_data, "Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
|
adamc@853
|
56 uw_free(ctx);
|
adamc@853
|
57 return NULL;
|
adamc@853
|
58 }
|
adamc@853
|
59 } else if (fk == UNLIMITED_RETRY)
|
adamc@856
|
60 log_debug(logger_data, "Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
|
adamc@853
|
61 else if (fk == FATAL) {
|
adamc@856
|
62 log_error(logger_data, "Fatal initialization error: %s\n", uw_error_message(ctx));
|
adamc@853
|
63 uw_free(ctx);
|
adamc@853
|
64 return NULL;
|
adamc@853
|
65 } else {
|
adamc@856
|
66 log_error(logger_data, "Unknown uw_begin_init return code!\n");
|
adamc@853
|
67 uw_free(ctx);
|
adamc@853
|
68 return NULL;
|
adamc@853
|
69 }
|
adamc@853
|
70 }
|
adamc@853
|
71
|
adamc@853
|
72 return ctx;
|
adamc@853
|
73 }
|
adamc@853
|
74
|
adam@1308
|
75 static void *ticker(void *data) {
|
adam@1308
|
76 while (1) {
|
adam@1308
|
77 usleep(100000);
|
adam@1308
|
78 ++uw_time;
|
adam@1308
|
79 }
|
adam@1308
|
80
|
adam@1308
|
81 return NULL;
|
adam@1308
|
82 }
|
adam@1308
|
83
|
adam@1349
|
84 typedef struct {
|
grrwlf@1997
|
85 int id;
|
grrwlf@1997
|
86 uw_loggers *ls;
|
grrwlf@1997
|
87 uw_periodic pdic;
|
adam@1349
|
88 uw_app *app;
|
adam@1349
|
89 } periodic;
|
adam@1349
|
90
|
adam@1349
|
91 static void *periodic_loop(void *data) {
|
adam@1349
|
92 periodic *p = (periodic *)data;
|
grrwlf@1997
|
93 uw_context ctx = uw_request_new_context(p->id, p->app, p->ls);
|
adam@1349
|
94
|
adam@1349
|
95 if (!ctx)
|
adam@1349
|
96 exit(1);
|
adam@1349
|
97
|
adam@1349
|
98 while (1) {
|
adam@1418
|
99 int retries_left = MAX_RETRIES;
|
adam@1418
|
100
|
adam@1349
|
101 failure_kind r;
|
adam@1349
|
102 do {
|
adam@1418
|
103 uw_reset(ctx);
|
adam@1349
|
104 r = uw_runCallback(ctx, p->pdic.callback);
|
adam@1418
|
105 if (r == BOUNDED_RETRY)
|
adam@1418
|
106 --retries_left;
|
adam@1418
|
107 else if (r == UNLIMITED_RETRY)
|
adam@1418
|
108 p->ls->log_debug(p->ls->logger_data, "Error triggers unlimited retry in periodic: %s\n", uw_error_message(ctx));
|
adam@1418
|
109 else if (r == BOUNDED_RETRY)
|
adam@1418
|
110 p->ls->log_debug(p->ls->logger_data, "Error triggers bounded retry in periodic: %s\n", uw_error_message(ctx));
|
adam@1418
|
111 else if (r == FATAL)
|
adam@1418
|
112 p->ls->log_error(p->ls->logger_data, "Fatal error: %s\n", uw_error_message(ctx));
|
adam@1418
|
113 if (r == FATAL || r == BOUNDED_RETRY || r == UNLIMITED_RETRY)
|
adam@1437
|
114 if (try_rollback(ctx, 0, p->ls->logger_data, p->ls->log_error))
|
adam@1437
|
115 return NULL;
|
adam@1418
|
116 } while (r == UNLIMITED_RETRY || (r == BOUNDED_RETRY && retries_left > 0));
|
adam@1418
|
117
|
adam@1938
|
118 if (r != FATAL && r != BOUNDED_RETRY) {
|
adam@1938
|
119 if (uw_commit(ctx))
|
adam@1938
|
120 r = UNLIMITED_RETRY;
|
adam@1938
|
121 }
|
adam@1349
|
122
|
adam@1349
|
123 sleep(p->pdic.period);
|
adam@1349
|
124 };
|
adam@1349
|
125 }
|
adam@1349
|
126
|
adam@1522
|
127 static unsigned long long stackSize;
|
adam@1522
|
128
|
adam@1522
|
129 int pthread_create_big(pthread_t *outThread, void *foo, void *threadFunc, void *arg)
|
adam@1522
|
130 {
|
adam@1882
|
131 if (stackSize > 0) {
|
adam@1882
|
132 int err;
|
adam@1882
|
133 pthread_attr_t stackSizeAttribute;
|
adam@1522
|
134
|
adam@1882
|
135 err = pthread_attr_init(&stackSizeAttribute);
|
adam@1882
|
136 if (err) return err;
|
adam@1522
|
137
|
adam@1522
|
138 err = pthread_attr_setstacksize(&stackSizeAttribute, stackSize);
|
adam@1522
|
139 if (err) return err;
|
adam@1882
|
140
|
adam@1882
|
141 return pthread_create(outThread, &stackSizeAttribute, threadFunc, arg);
|
adam@1882
|
142 } else {
|
adam@1882
|
143 return pthread_create(outThread, NULL, threadFunc, arg);
|
adam@1522
|
144 }
|
adam@1522
|
145 }
|
adam@1522
|
146
|
grrwlf@1997
|
147 void uw_request_init(uw_app *app, uw_loggers* ls) {
|
adamc@853
|
148 uw_context ctx;
|
adamc@853
|
149 failure_kind fk;
|
adam@1349
|
150 uw_periodic *ps;
|
adam@1446
|
151 int id;
|
adam@1522
|
152 char *stackSize_s;
|
adam@1522
|
153
|
grrwlf@1997
|
154 uw_logger log_debug = ls->log_debug;
|
grrwlf@1997
|
155 uw_logger log_error = ls->log_error;
|
grrwlf@1997
|
156 void* logger_data = ls->logger_data;
|
grrwlf@1997
|
157
|
adam@1522
|
158 if ((stackSize_s = getenv("URWEB_STACK_SIZE")) != NULL && stackSize_s[0] != 0) {
|
adam@1522
|
159 stackSize = atoll(stackSize_s);
|
adam@1522
|
160
|
adam@1522
|
161 if (stackSize <= 0) {
|
adam@1522
|
162 fprintf(stderr, "Invalid stack size \"%s\"\n", stackSize_s);
|
adam@1522
|
163 exit(1);
|
adam@1522
|
164 }
|
adam@1522
|
165 }
|
adam@1349
|
166
|
adamc@853
|
167 uw_global_init();
|
adamc@1094
|
168 uw_app_init(app);
|
adamc@853
|
169
|
adam@1308
|
170 {
|
adam@1308
|
171 pthread_t thread;
|
adam@1308
|
172
|
adam@1522
|
173 if (uw_time_max && pthread_create_big(&thread, NULL, ticker, NULL)) {
|
adam@1308
|
174 fprintf(stderr, "Error creating ticker thread\n");
|
adam@1308
|
175 exit(1);
|
adam@1308
|
176 }
|
adam@1308
|
177 }
|
adam@1308
|
178
|
grrwlf@1997
|
179 ctx = uw_request_new_context(0, app, ls);
|
adamc@853
|
180
|
adamc@853
|
181 if (!ctx)
|
adamc@853
|
182 exit(1);
|
adamc@853
|
183
|
adamc@853
|
184 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
|
adamc@856
|
185 log_debug(logger_data, "Unlimited retry during init: %s\n", uw_error_message(ctx));
|
adam@1327
|
186 uw_rollback(ctx, 1);
|
adamc@853
|
187 uw_reset(ctx);
|
adamc@853
|
188 }
|
adamc@853
|
189
|
adamc@853
|
190 if (fk != SUCCESS) {
|
adamc@856
|
191 log_error(logger_data, "Failed to initialize database! %s\n", uw_error_message(ctx));
|
adam@1327
|
192 uw_rollback(ctx, 0);
|
adamc@853
|
193 exit(1);
|
adamc@853
|
194 }
|
adamc@853
|
195
|
adamc@853
|
196 uw_free(ctx);
|
adam@1349
|
197
|
adam@1446
|
198 id = 1;
|
adam@1349
|
199 for (ps = app->periodics; ps->callback; ++ps) {
|
adam@1349
|
200 pthread_t thread;
|
adam@1349
|
201 periodic *arg = malloc(sizeof(periodic));
|
adam@1446
|
202 arg->id = id++;
|
adam@1349
|
203 arg->ls = ls;
|
adam@1349
|
204 arg->pdic = *ps;
|
grrwlf@1997
|
205 arg->app = app;
|
adam@1349
|
206
|
adam@1522
|
207 if (pthread_create_big(&thread, NULL, periodic_loop, arg)) {
|
adam@1349
|
208 fprintf(stderr, "Error creating periodic thread\n");
|
adam@1349
|
209 exit(1);
|
adam@1349
|
210 }
|
adam@1349
|
211 }
|
adamc@853
|
212 }
|
adamc@853
|
213
|
adamc@853
|
214
|
adamc@853
|
215 typedef struct uw_rc {
|
adam@1370
|
216 size_t path_copy_size, queryString_size;
|
adam@1370
|
217 char *path_copy, *queryString;
|
adamc@853
|
218 } *uw_request_context;
|
adamc@853
|
219
|
adamc@853
|
220 uw_request_context uw_new_request_context(void) {
|
adamc@853
|
221 uw_request_context r = malloc(sizeof(struct uw_rc));
|
adam@1371
|
222 r->path_copy_size = 0;
|
adam@1371
|
223 r->queryString_size = 1;
|
adamc@853
|
224 r->path_copy = malloc(0);
|
adam@1371
|
225 r->queryString = malloc(1);
|
adamc@853
|
226 return r;
|
adamc@853
|
227 }
|
adamc@853
|
228
|
adamc@853
|
229 void uw_free_request_context(uw_request_context r) {
|
adamc@853
|
230 free(r->path_copy);
|
adam@1370
|
231 free(r->queryString);
|
adamc@853
|
232 free(r);
|
adamc@853
|
233 }
|
adamc@853
|
234
|
adamc@854
|
235 request_result uw_request(uw_request_context rc, uw_context ctx,
|
adamc@854
|
236 char *method, char *path, char *query_string,
|
adamc@854
|
237 char *body, size_t body_len,
|
adamc@856
|
238 void (*on_success)(uw_context), void (*on_failure)(uw_context),
|
adamc@856
|
239 void *logger_data, uw_logger log_error, uw_logger log_debug,
|
adamc@863
|
240 int sock,
|
grrwlf@1997
|
241 int (*send)(int sockfd, const void *buf, ssize_t len),
|
adamc@863
|
242 int (*close)(int fd)) {
|
adamc@853
|
243 int retries_left = MAX_RETRIES;
|
adamc@853
|
244 failure_kind fk;
|
adamc@1134
|
245 int is_post = 0;
|
adamc@853
|
246 char *boundary = NULL;
|
adamc@1134
|
247 size_t boundary_len = 0;
|
adamc@854
|
248 char *inputs;
|
adamc@1094
|
249 const char *prefix = uw_get_url_prefix(ctx);
|
adamc@1169
|
250 char *s;
|
adam@1788
|
251 int had_error = 0, is_fancy = 0;
|
adam@1294
|
252 char errmsg[ERROR_BUF_LEN];
|
adamc@1169
|
253
|
adam@1417
|
254 uw_reset(ctx);
|
adam@1446
|
255
|
adam@1371
|
256 rc->queryString[0] = 0;
|
adam@1371
|
257
|
adamc@1169
|
258 for (s = path; *s; ++s) {
|
adamc@1169
|
259 if (s[0] == '%' && s[1] == '2' && s[2] == '7') {
|
adamc@1169
|
260 s[0] = '\'';
|
adamc@1169
|
261 memmove(s+1, s+3, strlen(s+3)+1);
|
adamc@1169
|
262 }
|
adamc@1169
|
263 }
|
adamc@853
|
264
|
adamc@1066
|
265 uw_set_currentUrl(ctx, path);
|
adamc@1066
|
266
|
adamc@854
|
267 if (!strcmp(method, "POST")) {
|
adamc@853
|
268 char *clen_s = uw_Basis_requestHeader(ctx, "Content-length");
|
adamc@853
|
269 if (!clen_s) {
|
adamc@1037
|
270 clen_s = "0";
|
adamc@1037
|
271 /*log_error(logger_data, "No Content-length with POST\n");
|
adamc@1037
|
272 return FAILED;*/
|
adamc@853
|
273 }
|
adamc@853
|
274 int clen = atoi(clen_s);
|
adamc@853
|
275 if (clen < 0) {
|
adamc@856
|
276 log_error(logger_data, "Negative Content-length with POST\n");
|
adamc@853
|
277 return FAILED;
|
adamc@853
|
278 }
|
adamc@853
|
279
|
adamc@854
|
280 if (body_len < clen) {
|
adamc@856
|
281 log_error(logger_data, "Request doesn't contain all POST data (according to Content-Length)\n");
|
adamc@853
|
282 return FAILED;
|
adamc@853
|
283 }
|
adamc@853
|
284
|
adamc@853
|
285 is_post = 1;
|
adam@1386
|
286 uw_isPost(ctx);
|
adamc@853
|
287
|
adamc@853
|
288 clen_s = uw_Basis_requestHeader(ctx, "Content-type");
|
adam@1788
|
289
|
adam@1788
|
290 if (!clen_s || strcasecmp(clen_s, "application/x-www-form-urlencoded"))
|
adam@1788
|
291 is_fancy = 1;
|
adam@1788
|
292
|
adamc@853
|
293 if (clen_s && !strncasecmp(clen_s, "multipart/form-data", 19)) {
|
adamc@853
|
294 if (strncasecmp(clen_s + 19, "; boundary=", 11)) {
|
adamc@856
|
295 log_error(logger_data, "Bad multipart boundary spec");
|
adamc@853
|
296 return FAILED;
|
adamc@853
|
297 }
|
adamc@853
|
298
|
adamc@853
|
299 boundary = clen_s + 28;
|
adamc@853
|
300 boundary[0] = '-';
|
adamc@853
|
301 boundary[1] = '-';
|
adamc@853
|
302 boundary_len = strlen(boundary);
|
adam@1787
|
303 } else if (clen_s) {
|
adam@1788
|
304 uw_Basis_postBody pb = {clen_s, body, body_len};
|
adam@1347
|
305 uw_postBody(ctx, pb);
|
adamc@853
|
306 }
|
adamc@854
|
307 } else if (strcmp(method, "GET")) {
|
adamc@856
|
308 log_error(logger_data, "Not ready for non-GET/POST command: %s\n", method);
|
adamc@853
|
309 return FAILED;
|
adamc@853
|
310 }
|
adamc@853
|
311
|
adamc@1094
|
312 if (!strncmp(path, prefix, strlen(prefix))
|
adamc@1094
|
313 && !strcmp(path + strlen(prefix), ".msgs")) {
|
adamc@853
|
314 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
|
adamc@853
|
315 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
|
adamc@853
|
316
|
adamc@853
|
317 if (sock < 0) {
|
adamc@856
|
318 log_error(logger_data, ".msgs requested, but not socket supplied\n");
|
adamc@853
|
319 return FAILED;
|
adamc@853
|
320 }
|
adamc@853
|
321
|
adamc@853
|
322 if (id && pass) {
|
adamc@853
|
323 unsigned idn = atoi(id);
|
adamc@864
|
324 uw_client_connect(idn, atoi(pass), sock, send, close, logger_data, log_error);
|
adam@1320
|
325 log_debug(logger_data, "Processed request for messages by client %u\n\n", idn);
|
adamc@853
|
326 return KEEP_OPEN;
|
adamc@853
|
327 }
|
adamc@853
|
328 else {
|
adamc@856
|
329 log_error(logger_data, "Missing fields in .msgs request: %s, %s\n\n", id, pass);
|
adamc@853
|
330 return FAILED;
|
adamc@853
|
331 }
|
adamc@853
|
332 }
|
adamc@853
|
333
|
adamc@853
|
334 if (boundary) {
|
adamc@854
|
335 char *part = body, *after_sub_headers, *header, *after_header;
|
adamc@853
|
336 size_t part_len;
|
adamc@853
|
337
|
adamc@853
|
338 part = strstr(part, boundary);
|
adamc@853
|
339 if (!part) {
|
adamc@856
|
340 log_error(logger_data, "Missing first multipart boundary\n");
|
adamc@853
|
341 return FAILED;
|
adamc@853
|
342 }
|
adamc@853
|
343 part += boundary_len;
|
adamc@853
|
344
|
adamc@853
|
345 while (1) {
|
adamc@853
|
346 char *name = NULL, *filename = NULL, *type = NULL;
|
adamc@853
|
347
|
adamc@853
|
348 if (part[0] == '-' && part[1] == '-')
|
adamc@853
|
349 break;
|
adamc@853
|
350
|
adamc@853
|
351 if (*part != '\r') {
|
adamc@856
|
352 log_error(logger_data, "No \\r after multipart boundary\n");
|
adamc@853
|
353 return FAILED;
|
adamc@853
|
354 }
|
adamc@853
|
355 ++part;
|
adamc@853
|
356 if (*part != '\n') {
|
adamc@856
|
357 log_error(logger_data, "No \\n after multipart boundary\n");
|
adamc@853
|
358 return FAILED;
|
adamc@853
|
359 }
|
adamc@853
|
360 ++part;
|
adamc@853
|
361
|
adamc@853
|
362 if (!(after_sub_headers = strstr(part, "\r\n\r\n"))) {
|
adamc@856
|
363 log_error(logger_data, "Missing end of headers after multipart boundary\n");
|
adamc@853
|
364 return FAILED;
|
adamc@853
|
365 }
|
adamc@853
|
366 after_sub_headers[2] = 0;
|
adamc@853
|
367 after_sub_headers += 4;
|
adamc@853
|
368
|
adamc@1134
|
369 for (header = part; (after_header = strstr(header, "\r\n")); header = after_header + 2) {
|
adamc@853
|
370 char *colon, *after_colon;
|
adamc@853
|
371
|
adamc@853
|
372 *after_header = 0;
|
adamc@853
|
373 if (!(colon = strchr(header, ':'))) {
|
adamc@856
|
374 log_error(logger_data, "Missing colon in multipart sub-header\n");
|
adamc@853
|
375 return FAILED;
|
adamc@853
|
376 }
|
adamc@853
|
377 *colon++ = 0;
|
adamc@853
|
378 if (*colon++ != ' ') {
|
adamc@856
|
379 log_error(logger_data, "No space after colon in multipart sub-header\n");
|
adamc@853
|
380 return FAILED;
|
adamc@853
|
381 }
|
adamc@853
|
382
|
adamc@853
|
383 if (!strcasecmp(header, "Content-Disposition")) {
|
adamc@853
|
384 if (strncmp(colon, "form-data; ", 11)) {
|
adamc@856
|
385 log_error(logger_data, "Multipart data is not \"form-data\"\n");
|
adamc@853
|
386 return FAILED;
|
adamc@853
|
387 }
|
adamc@853
|
388
|
adamc@1134
|
389 for (colon += 11; (after_colon = strchr(colon, '=')); colon = after_colon) {
|
adamc@853
|
390 char *data;
|
adamc@853
|
391 after_colon[0] = 0;
|
adamc@853
|
392 if (after_colon[1] != '"') {
|
adamc@856
|
393 log_error(logger_data, "Disposition setting is missing initial quote\n");
|
adamc@853
|
394 return FAILED;
|
adamc@853
|
395 }
|
adamc@853
|
396 data = after_colon+2;
|
adamc@853
|
397 if (!(after_colon = strchr(data, '"'))) {
|
adamc@856
|
398 log_error(logger_data, "Disposition setting is missing final quote\n");
|
adamc@853
|
399 return FAILED;
|
adamc@853
|
400 }
|
adamc@853
|
401 after_colon[0] = 0;
|
adamc@853
|
402 ++after_colon;
|
adamc@853
|
403 if (after_colon[0] == ';' && after_colon[1] == ' ')
|
adamc@853
|
404 after_colon += 2;
|
adamc@853
|
405
|
adamc@853
|
406 if (!strcasecmp(colon, "name"))
|
adamc@853
|
407 name = data;
|
adamc@853
|
408 else if (!strcasecmp(colon, "filename"))
|
adamc@853
|
409 filename = data;
|
adamc@853
|
410 }
|
adamc@853
|
411 } else if (!strcasecmp(header, "Content-Type")) {
|
adamc@853
|
412 type = colon;
|
adamc@853
|
413 }
|
adamc@853
|
414 }
|
adamc@853
|
415
|
adamc@854
|
416 part = memmem(after_sub_headers, body + body_len - after_sub_headers, boundary, boundary_len);
|
adamc@853
|
417 if (!part) {
|
adamc@856
|
418 log_error(logger_data, "Missing boundary after multipart payload\n");
|
adamc@853
|
419 return FAILED;
|
adamc@853
|
420 }
|
adamc@853
|
421 part[-2] = 0;
|
adamc@853
|
422 part_len = part - after_sub_headers - 2;
|
adamc@853
|
423 part[0] = 0;
|
adamc@853
|
424 part += boundary_len;
|
adamc@853
|
425
|
adamc@853
|
426 if (filename) {
|
adamc@853
|
427 uw_Basis_file f = {filename, type, {part_len, after_sub_headers}};
|
adamc@853
|
428
|
adamc@853
|
429 if (uw_set_file_input(ctx, name, f)) {
|
adamc@856
|
430 log_error(logger_data, "%s\n", uw_error_message(ctx));
|
adamc@853
|
431 return FAILED;
|
adamc@853
|
432 }
|
adamc@853
|
433 } else if (uw_set_input(ctx, name, after_sub_headers)) {
|
adamc@856
|
434 log_error(logger_data, "%s\n", uw_error_message(ctx));
|
adamc@853
|
435 return FAILED;
|
adamc@853
|
436 }
|
adamc@853
|
437 }
|
adamc@853
|
438 }
|
adam@1788
|
439 else if (!is_fancy) {
|
adamc@854
|
440 inputs = is_post ? body : query_string;
|
adamc@853
|
441
|
adamc@853
|
442 if (inputs) {
|
adamc@853
|
443 char *name, *value;
|
adam@1370
|
444 int len = strlen(inputs);
|
adam@1370
|
445
|
adam@1370
|
446 if (len+1 > rc->queryString_size) {
|
adam@1370
|
447 rc->queryString_size = len+1;
|
adam@1370
|
448 rc->queryString = realloc(rc->queryString, len+1);
|
adam@1370
|
449 }
|
adam@1370
|
450 strcpy(rc->queryString, inputs);
|
adamc@853
|
451
|
adamc@853
|
452 while (*inputs) {
|
adamc@853
|
453 name = inputs;
|
adamc@1134
|
454 if ((inputs = strchr(inputs, '&')))
|
adamc@853
|
455 *inputs++ = 0;
|
adamc@853
|
456 else
|
adamc@853
|
457 inputs = strchr(name, 0);
|
adamc@853
|
458
|
adamc@1134
|
459 if ((value = strchr(name, '='))) {
|
adamc@853
|
460 *value++ = 0;
|
adamc@853
|
461 if (uw_set_input(ctx, name, value)) {
|
adamc@856
|
462 log_error(logger_data, "%s\n", uw_error_message(ctx));
|
adamc@853
|
463 return FAILED;
|
adamc@853
|
464 }
|
adamc@853
|
465 }
|
adamc@853
|
466 else if (uw_set_input(ctx, name, "")) {
|
adamc@856
|
467 log_error(logger_data, "%s\n", uw_error_message(ctx));
|
adamc@853
|
468 return FAILED;
|
adamc@853
|
469 }
|
adamc@853
|
470 }
|
adamc@853
|
471 }
|
adamc@853
|
472 }
|
adamc@853
|
473
|
adamc@853
|
474 while (1) {
|
adam@1371
|
475 uw_setQueryString(ctx, rc->queryString);
|
adam@1371
|
476
|
adam@1294
|
477 if (!had_error) {
|
adam@1294
|
478 size_t path_len = strlen(path);
|
adamc@853
|
479
|
adam@1294
|
480 on_success(ctx);
|
adamc@853
|
481
|
adam@1294
|
482 if (path_len + 1 > rc->path_copy_size) {
|
adam@1294
|
483 rc->path_copy_size = path_len + 1;
|
adam@1294
|
484 rc->path_copy = realloc(rc->path_copy, rc->path_copy_size);
|
adam@1294
|
485 }
|
adam@1294
|
486 strcpy(rc->path_copy, path);
|
adam@1308
|
487
|
adam@1308
|
488 uw_set_deadline(ctx, uw_time + uw_time_max);
|
adam@1294
|
489 fk = uw_begin(ctx, rc->path_copy);
|
adam@1308
|
490 } else {
|
adam@1308
|
491 uw_set_deadline(ctx, uw_time + uw_time_max);
|
adam@1294
|
492 fk = uw_begin_onError(ctx, errmsg);
|
adam@1308
|
493 }
|
adam@1294
|
494
|
adamc@1065
|
495 if (fk == SUCCESS || fk == RETURN_INDIRECTLY) {
|
adamc@853
|
496 uw_commit(ctx);
|
adam@1294
|
497 if (uw_has_error(ctx) && !had_error) {
|
adamc@1131
|
498 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
|
adamc@1131
|
499 uw_reset_keep_error_message(ctx);
|
adamc@1131
|
500 on_failure(ctx);
|
adam@1294
|
501
|
adam@1294
|
502 if (uw_get_app(ctx)->on_error) {
|
adam@1294
|
503 had_error = 1;
|
adam@1294
|
504 strcpy(errmsg, uw_error_message(ctx));
|
adam@1294
|
505 } else {
|
adam@1294
|
506 uw_write_header(ctx, "Content-type: text/html\r\n");
|
adam@1294
|
507 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
|
adam@1294
|
508 uw_write(ctx, "Fatal error: ");
|
adam@1294
|
509 uw_write(ctx, uw_error_message(ctx));
|
adam@1294
|
510 uw_write(ctx, "\n</body></html>");
|
adamc@1131
|
511
|
adam@1774
|
512 try_rollback(ctx, 0, logger_data, log_error);
|
adam@1774
|
513
|
adam@1294
|
514 return FAILED;
|
adam@1294
|
515 }
|
adamc@1131
|
516 } else
|
adam@1294
|
517 return had_error ? FAILED : SERVED;
|
adamc@853
|
518 } else if (fk == BOUNDED_RETRY) {
|
adamc@853
|
519 if (retries_left) {
|
adamc@856
|
520 log_debug(logger_data, "Error triggers bounded retry: %s\n", uw_error_message(ctx));
|
adamc@853
|
521 --retries_left;
|
adamc@853
|
522 }
|
adamc@853
|
523 else {
|
adamc@856
|
524 log_error(logger_data, "Fatal error (out of retries): %s\n", uw_error_message(ctx));
|
adamc@853
|
525
|
adam@1294
|
526 if (!had_error && uw_get_app(ctx)->on_error) {
|
adam@1294
|
527 had_error = 1;
|
adam@1294
|
528 strcpy(errmsg, uw_error_message(ctx));
|
adam@1294
|
529 } else {
|
adam@1294
|
530 uw_reset_keep_error_message(ctx);
|
adam@1294
|
531 on_failure(ctx);
|
adam@1294
|
532 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adam@1294
|
533 uw_write(ctx, "Fatal error (out of retries): ");
|
adam@1294
|
534 uw_write(ctx, uw_error_message(ctx));
|
adam@1294
|
535 uw_write(ctx, "\n");
|
adam@1294
|
536
|
adam@1774
|
537 try_rollback(ctx, 0, logger_data, log_error);
|
adam@1774
|
538
|
adam@1294
|
539 return FAILED;
|
adam@1294
|
540 }
|
adamc@853
|
541 }
|
adamc@853
|
542 } else if (fk == UNLIMITED_RETRY)
|
adamc@856
|
543 log_debug(logger_data, "Error triggers unlimited retry: %s\n", uw_error_message(ctx));
|
adamc@853
|
544 else if (fk == FATAL) {
|
adamc@856
|
545 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
|
adamc@853
|
546
|
adam@1294
|
547 if (uw_get_app(ctx)->on_error && !had_error) {
|
adam@1294
|
548 had_error = 1;
|
adam@1294
|
549 strcpy(errmsg, uw_error_message(ctx));
|
adam@1294
|
550 } else {
|
adam@1294
|
551 uw_reset_keep_error_message(ctx);
|
adam@1294
|
552 on_failure(ctx);
|
adam@1294
|
553 uw_write_header(ctx, "Content-type: text/html\r\n");
|
adam@1294
|
554 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
|
adam@1294
|
555 uw_write(ctx, "Fatal error: ");
|
adam@1294
|
556 uw_write(ctx, uw_error_message(ctx));
|
adam@1294
|
557 uw_write(ctx, "\n</body></html>");
|
adamc@853
|
558
|
adam@1774
|
559 try_rollback(ctx, 0, logger_data, log_error);
|
adam@1774
|
560
|
adam@1294
|
561 return FAILED;
|
adam@1294
|
562 }
|
adamc@853
|
563 } else {
|
adamc@856
|
564 log_error(logger_data, "Unknown uw_handle return code!\n");
|
adamc@853
|
565
|
adam@1294
|
566 if (uw_get_app(ctx)->on_error && !had_error) {
|
adam@1294
|
567 had_error = 1;
|
adam@1294
|
568 strcpy(errmsg, "Unknown uw_handle return code");
|
adam@1294
|
569 } else {
|
adam@1294
|
570 uw_reset_keep_request(ctx);
|
adam@1294
|
571 on_failure(ctx);
|
adam@1294
|
572 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adam@1294
|
573 uw_write(ctx, "Unknown uw_handle return code!\n");
|
adamc@853
|
574
|
adam@1774
|
575 try_rollback(ctx, 0, logger_data, log_error);
|
adam@1774
|
576
|
adam@1294
|
577 return FAILED;
|
adam@1294
|
578 }
|
adamc@853
|
579 }
|
adamc@853
|
580
|
adam@1327
|
581 if (try_rollback(ctx, 1, logger_data, log_error))
|
adamc@853
|
582 return FAILED;
|
adamc@853
|
583
|
adamc@853
|
584 uw_reset_keep_request(ctx);
|
adamc@853
|
585 }
|
adamc@853
|
586 }
|
adamc@853
|
587
|
adamc@853
|
588 void *client_pruner(void *data) {
|
grrwlf@1997
|
589 pruner_data *pd = (pruner_data *)data;
|
grrwlf@1997
|
590 uw_context ctx = uw_request_new_context(0, pd->app, pd->loggers);
|
adamc@853
|
591
|
adamc@853
|
592 if (!ctx)
|
adamc@853
|
593 exit(1);
|
adamc@853
|
594
|
adamc@853
|
595 while (1) {
|
adamc@853
|
596 uw_prune_clients(ctx);
|
adamc@853
|
597 sleep(5);
|
adamc@853
|
598 }
|
adamc@853
|
599 }
|