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