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