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 {
|
adamc@853
|
167 size_t path_copy_size;
|
adamc@853
|
168 char *path_copy;
|
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));
|
adamc@853
|
173 r->path_copy_size = 0;
|
adamc@853
|
174 r->path_copy = malloc(0);
|
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);
|
adamc@853
|
180 free(r);
|
adamc@853
|
181 }
|
adamc@853
|
182
|
adamc@854
|
183 request_result uw_request(uw_request_context rc, uw_context ctx,
|
adamc@854
|
184 char *method, char *path, char *query_string,
|
adamc@854
|
185 char *body, size_t body_len,
|
adamc@856
|
186 void (*on_success)(uw_context), void (*on_failure)(uw_context),
|
adamc@856
|
187 void *logger_data, uw_logger log_error, uw_logger log_debug,
|
adamc@863
|
188 int sock,
|
adamc@863
|
189 int (*send)(int sockfd, const void *buf, size_t len),
|
adamc@863
|
190 int (*close)(int fd)) {
|
adamc@853
|
191 int retries_left = MAX_RETRIES;
|
adamc@853
|
192 failure_kind fk;
|
adamc@1134
|
193 int is_post = 0;
|
adamc@853
|
194 char *boundary = NULL;
|
adamc@1134
|
195 size_t boundary_len = 0;
|
adamc@854
|
196 char *inputs;
|
adamc@1094
|
197 const char *prefix = uw_get_url_prefix(ctx);
|
adamc@1169
|
198 char *s;
|
adam@1294
|
199 int had_error = 0;
|
adam@1294
|
200 char errmsg[ERROR_BUF_LEN];
|
adamc@1169
|
201
|
adamc@1169
|
202 for (s = path; *s; ++s) {
|
adamc@1169
|
203 if (s[0] == '%' && s[1] == '2' && s[2] == '7') {
|
adamc@1169
|
204 s[0] = '\'';
|
adamc@1169
|
205 memmove(s+1, s+3, strlen(s+3)+1);
|
adamc@1169
|
206 }
|
adamc@1169
|
207 }
|
adamc@853
|
208
|
adamc@1066
|
209 uw_set_currentUrl(ctx, path);
|
adamc@1066
|
210
|
adamc@854
|
211 if (!strcmp(method, "POST")) {
|
adamc@853
|
212 char *clen_s = uw_Basis_requestHeader(ctx, "Content-length");
|
adamc@853
|
213 if (!clen_s) {
|
adamc@1037
|
214 clen_s = "0";
|
adamc@1037
|
215 /*log_error(logger_data, "No Content-length with POST\n");
|
adamc@1037
|
216 return FAILED;*/
|
adamc@853
|
217 }
|
adamc@853
|
218 int clen = atoi(clen_s);
|
adamc@853
|
219 if (clen < 0) {
|
adamc@856
|
220 log_error(logger_data, "Negative Content-length with POST\n");
|
adamc@853
|
221 return FAILED;
|
adamc@853
|
222 }
|
adamc@853
|
223
|
adamc@854
|
224 if (body_len < clen) {
|
adamc@856
|
225 log_error(logger_data, "Request doesn't contain all POST data (according to Content-Length)\n");
|
adamc@853
|
226 return FAILED;
|
adamc@853
|
227 }
|
adamc@853
|
228
|
adamc@853
|
229 is_post = 1;
|
adamc@853
|
230
|
adamc@853
|
231 clen_s = uw_Basis_requestHeader(ctx, "Content-type");
|
adamc@853
|
232 if (clen_s && !strncasecmp(clen_s, "multipart/form-data", 19)) {
|
adamc@853
|
233 if (strncasecmp(clen_s + 19, "; boundary=", 11)) {
|
adamc@856
|
234 log_error(logger_data, "Bad multipart boundary spec");
|
adamc@853
|
235 return FAILED;
|
adamc@853
|
236 }
|
adamc@853
|
237
|
adamc@853
|
238 boundary = clen_s + 28;
|
adamc@853
|
239 boundary[0] = '-';
|
adamc@853
|
240 boundary[1] = '-';
|
adamc@853
|
241 boundary_len = strlen(boundary);
|
adam@1347
|
242 } else if (clen_s && strcasecmp(clen_s, "application/x-www-form-urlencoded")) {
|
adam@1347
|
243 uw_Basis_postBody pb = {clen_s, body};
|
adam@1347
|
244 uw_postBody(ctx, pb);
|
adamc@853
|
245 }
|
adamc@854
|
246 } else if (strcmp(method, "GET")) {
|
adamc@856
|
247 log_error(logger_data, "Not ready for non-GET/POST command: %s\n", method);
|
adamc@853
|
248 return FAILED;
|
adamc@853
|
249 }
|
adamc@853
|
250
|
adamc@1094
|
251 if (!strncmp(path, prefix, strlen(prefix))
|
adamc@1094
|
252 && !strcmp(path + strlen(prefix), ".msgs")) {
|
adamc@853
|
253 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
|
adamc@853
|
254 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
|
adamc@853
|
255
|
adamc@853
|
256 if (sock < 0) {
|
adamc@856
|
257 log_error(logger_data, ".msgs requested, but not socket supplied\n");
|
adamc@853
|
258 return FAILED;
|
adamc@853
|
259 }
|
adamc@853
|
260
|
adamc@853
|
261 if (id && pass) {
|
adamc@853
|
262 unsigned idn = atoi(id);
|
adamc@864
|
263 uw_client_connect(idn, atoi(pass), sock, send, close, logger_data, log_error);
|
adam@1320
|
264 log_debug(logger_data, "Processed request for messages by client %u\n\n", idn);
|
adamc@853
|
265 return KEEP_OPEN;
|
adamc@853
|
266 }
|
adamc@853
|
267 else {
|
adamc@856
|
268 log_error(logger_data, "Missing fields in .msgs request: %s, %s\n\n", id, pass);
|
adamc@853
|
269 return FAILED;
|
adamc@853
|
270 }
|
adamc@853
|
271 }
|
adamc@853
|
272
|
adamc@853
|
273 if (boundary) {
|
adamc@854
|
274 char *part = body, *after_sub_headers, *header, *after_header;
|
adamc@853
|
275 size_t part_len;
|
adamc@853
|
276
|
adamc@853
|
277 part = strstr(part, boundary);
|
adamc@853
|
278 if (!part) {
|
adamc@856
|
279 log_error(logger_data, "Missing first multipart boundary\n");
|
adamc@853
|
280 return FAILED;
|
adamc@853
|
281 }
|
adamc@853
|
282 part += boundary_len;
|
adamc@853
|
283
|
adamc@853
|
284 while (1) {
|
adamc@853
|
285 char *name = NULL, *filename = NULL, *type = NULL;
|
adamc@853
|
286
|
adamc@853
|
287 if (part[0] == '-' && part[1] == '-')
|
adamc@853
|
288 break;
|
adamc@853
|
289
|
adamc@853
|
290 if (*part != '\r') {
|
adamc@856
|
291 log_error(logger_data, "No \\r after multipart boundary\n");
|
adamc@853
|
292 return FAILED;
|
adamc@853
|
293 }
|
adamc@853
|
294 ++part;
|
adamc@853
|
295 if (*part != '\n') {
|
adamc@856
|
296 log_error(logger_data, "No \\n after multipart boundary\n");
|
adamc@853
|
297 return FAILED;
|
adamc@853
|
298 }
|
adamc@853
|
299 ++part;
|
adamc@853
|
300
|
adamc@853
|
301 if (!(after_sub_headers = strstr(part, "\r\n\r\n"))) {
|
adamc@856
|
302 log_error(logger_data, "Missing end of headers after multipart boundary\n");
|
adamc@853
|
303 return FAILED;
|
adamc@853
|
304 }
|
adamc@853
|
305 after_sub_headers[2] = 0;
|
adamc@853
|
306 after_sub_headers += 4;
|
adamc@853
|
307
|
adamc@1134
|
308 for (header = part; (after_header = strstr(header, "\r\n")); header = after_header + 2) {
|
adamc@853
|
309 char *colon, *after_colon;
|
adamc@853
|
310
|
adamc@853
|
311 *after_header = 0;
|
adamc@853
|
312 if (!(colon = strchr(header, ':'))) {
|
adamc@856
|
313 log_error(logger_data, "Missing colon in multipart sub-header\n");
|
adamc@853
|
314 return FAILED;
|
adamc@853
|
315 }
|
adamc@853
|
316 *colon++ = 0;
|
adamc@853
|
317 if (*colon++ != ' ') {
|
adamc@856
|
318 log_error(logger_data, "No space after colon in multipart sub-header\n");
|
adamc@853
|
319 return FAILED;
|
adamc@853
|
320 }
|
adamc@853
|
321
|
adamc@853
|
322 if (!strcasecmp(header, "Content-Disposition")) {
|
adamc@853
|
323 if (strncmp(colon, "form-data; ", 11)) {
|
adamc@856
|
324 log_error(logger_data, "Multipart data is not \"form-data\"\n");
|
adamc@853
|
325 return FAILED;
|
adamc@853
|
326 }
|
adamc@853
|
327
|
adamc@1134
|
328 for (colon += 11; (after_colon = strchr(colon, '=')); colon = after_colon) {
|
adamc@853
|
329 char *data;
|
adamc@853
|
330 after_colon[0] = 0;
|
adamc@853
|
331 if (after_colon[1] != '"') {
|
adamc@856
|
332 log_error(logger_data, "Disposition setting is missing initial quote\n");
|
adamc@853
|
333 return FAILED;
|
adamc@853
|
334 }
|
adamc@853
|
335 data = after_colon+2;
|
adamc@853
|
336 if (!(after_colon = strchr(data, '"'))) {
|
adamc@856
|
337 log_error(logger_data, "Disposition setting is missing final quote\n");
|
adamc@853
|
338 return FAILED;
|
adamc@853
|
339 }
|
adamc@853
|
340 after_colon[0] = 0;
|
adamc@853
|
341 ++after_colon;
|
adamc@853
|
342 if (after_colon[0] == ';' && after_colon[1] == ' ')
|
adamc@853
|
343 after_colon += 2;
|
adamc@853
|
344
|
adamc@853
|
345 if (!strcasecmp(colon, "name"))
|
adamc@853
|
346 name = data;
|
adamc@853
|
347 else if (!strcasecmp(colon, "filename"))
|
adamc@853
|
348 filename = data;
|
adamc@853
|
349 }
|
adamc@853
|
350 } else if (!strcasecmp(header, "Content-Type")) {
|
adamc@853
|
351 type = colon;
|
adamc@853
|
352 }
|
adamc@853
|
353 }
|
adamc@853
|
354
|
adamc@854
|
355 part = memmem(after_sub_headers, body + body_len - after_sub_headers, boundary, boundary_len);
|
adamc@853
|
356 if (!part) {
|
adamc@856
|
357 log_error(logger_data, "Missing boundary after multipart payload\n");
|
adamc@853
|
358 return FAILED;
|
adamc@853
|
359 }
|
adamc@853
|
360 part[-2] = 0;
|
adamc@853
|
361 part_len = part - after_sub_headers - 2;
|
adamc@853
|
362 part[0] = 0;
|
adamc@853
|
363 part += boundary_len;
|
adamc@853
|
364
|
adamc@853
|
365 if (filename) {
|
adamc@853
|
366 uw_Basis_file f = {filename, type, {part_len, after_sub_headers}};
|
adamc@853
|
367
|
adamc@853
|
368 if (uw_set_file_input(ctx, name, f)) {
|
adamc@856
|
369 log_error(logger_data, "%s\n", uw_error_message(ctx));
|
adamc@853
|
370 return FAILED;
|
adamc@853
|
371 }
|
adamc@853
|
372 } else if (uw_set_input(ctx, name, after_sub_headers)) {
|
adamc@856
|
373 log_error(logger_data, "%s\n", uw_error_message(ctx));
|
adamc@853
|
374 return FAILED;
|
adamc@853
|
375 }
|
adamc@853
|
376 }
|
adamc@853
|
377 }
|
adam@1347
|
378 else if (!uw_hasPostBody(ctx)) {
|
adamc@854
|
379 inputs = is_post ? body : query_string;
|
adamc@853
|
380
|
adamc@853
|
381 if (inputs) {
|
adamc@853
|
382 char *name, *value;
|
adamc@853
|
383
|
adamc@853
|
384 while (*inputs) {
|
adamc@853
|
385 name = inputs;
|
adamc@1134
|
386 if ((inputs = strchr(inputs, '&')))
|
adamc@853
|
387 *inputs++ = 0;
|
adamc@853
|
388 else
|
adamc@853
|
389 inputs = strchr(name, 0);
|
adamc@853
|
390
|
adamc@1134
|
391 if ((value = strchr(name, '='))) {
|
adamc@853
|
392 *value++ = 0;
|
adamc@853
|
393 if (uw_set_input(ctx, name, value)) {
|
adamc@856
|
394 log_error(logger_data, "%s\n", uw_error_message(ctx));
|
adamc@853
|
395 return FAILED;
|
adamc@853
|
396 }
|
adamc@853
|
397 }
|
adamc@853
|
398 else if (uw_set_input(ctx, name, "")) {
|
adamc@856
|
399 log_error(logger_data, "%s\n", uw_error_message(ctx));
|
adamc@853
|
400 return FAILED;
|
adamc@853
|
401 }
|
adamc@853
|
402 }
|
adamc@853
|
403 }
|
adamc@853
|
404 }
|
adamc@853
|
405
|
adamc@856
|
406 log_debug(logger_data, "Serving URI %s....\n", path);
|
adamc@853
|
407
|
adamc@853
|
408 while (1) {
|
adam@1294
|
409 if (!had_error) {
|
adam@1294
|
410 size_t path_len = strlen(path);
|
adamc@853
|
411
|
adam@1294
|
412 on_success(ctx);
|
adamc@853
|
413
|
adam@1294
|
414 if (path_len + 1 > rc->path_copy_size) {
|
adam@1294
|
415 rc->path_copy_size = path_len + 1;
|
adam@1294
|
416 rc->path_copy = realloc(rc->path_copy, rc->path_copy_size);
|
adam@1294
|
417 }
|
adam@1294
|
418 strcpy(rc->path_copy, path);
|
adam@1308
|
419
|
adam@1308
|
420 uw_set_deadline(ctx, uw_time + uw_time_max);
|
adam@1294
|
421 fk = uw_begin(ctx, rc->path_copy);
|
adam@1308
|
422 } else {
|
adam@1308
|
423 uw_set_deadline(ctx, uw_time + uw_time_max);
|
adam@1294
|
424 fk = uw_begin_onError(ctx, errmsg);
|
adam@1308
|
425 }
|
adam@1294
|
426
|
adamc@1065
|
427 if (fk == SUCCESS || fk == RETURN_INDIRECTLY) {
|
adamc@853
|
428 uw_commit(ctx);
|
adam@1294
|
429 if (uw_has_error(ctx) && !had_error) {
|
adamc@1131
|
430 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
|
adamc@1131
|
431 uw_reset_keep_error_message(ctx);
|
adamc@1131
|
432 on_failure(ctx);
|
adam@1294
|
433
|
adam@1294
|
434 if (uw_get_app(ctx)->on_error) {
|
adam@1294
|
435 had_error = 1;
|
adam@1294
|
436 strcpy(errmsg, uw_error_message(ctx));
|
adam@1294
|
437 } else {
|
adam@1294
|
438 uw_write_header(ctx, "Content-type: text/html\r\n");
|
adam@1294
|
439 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
|
adam@1294
|
440 uw_write(ctx, "Fatal error: ");
|
adam@1294
|
441 uw_write(ctx, uw_error_message(ctx));
|
adam@1294
|
442 uw_write(ctx, "\n</body></html>");
|
adamc@1131
|
443
|
adam@1294
|
444 return FAILED;
|
adam@1294
|
445 }
|
adamc@1131
|
446 } else
|
adam@1294
|
447 return had_error ? FAILED : SERVED;
|
adamc@853
|
448 } else if (fk == BOUNDED_RETRY) {
|
adamc@853
|
449 if (retries_left) {
|
adamc@856
|
450 log_debug(logger_data, "Error triggers bounded retry: %s\n", uw_error_message(ctx));
|
adamc@853
|
451 --retries_left;
|
adamc@853
|
452 }
|
adamc@853
|
453 else {
|
adamc@856
|
454 log_error(logger_data, "Fatal error (out of retries): %s\n", uw_error_message(ctx));
|
adamc@853
|
455
|
adam@1327
|
456 try_rollback(ctx, 0, logger_data, log_error);
|
adamc@853
|
457
|
adam@1294
|
458 if (!had_error && uw_get_app(ctx)->on_error) {
|
adam@1294
|
459 had_error = 1;
|
adam@1294
|
460 strcpy(errmsg, uw_error_message(ctx));
|
adam@1294
|
461 } else {
|
adam@1294
|
462 uw_reset_keep_error_message(ctx);
|
adam@1294
|
463 on_failure(ctx);
|
adam@1294
|
464 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adam@1294
|
465 uw_write(ctx, "Fatal error (out of retries): ");
|
adam@1294
|
466 uw_write(ctx, uw_error_message(ctx));
|
adam@1294
|
467 uw_write(ctx, "\n");
|
adam@1294
|
468
|
adam@1294
|
469 return FAILED;
|
adam@1294
|
470 }
|
adamc@853
|
471 }
|
adamc@853
|
472 } else if (fk == UNLIMITED_RETRY)
|
adamc@856
|
473 log_debug(logger_data, "Error triggers unlimited retry: %s\n", uw_error_message(ctx));
|
adamc@853
|
474 else if (fk == FATAL) {
|
adamc@856
|
475 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
|
adamc@853
|
476
|
adam@1327
|
477 try_rollback(ctx, 0, logger_data, log_error);
|
adamc@853
|
478
|
adam@1294
|
479 if (uw_get_app(ctx)->on_error && !had_error) {
|
adam@1294
|
480 had_error = 1;
|
adam@1294
|
481 strcpy(errmsg, uw_error_message(ctx));
|
adam@1294
|
482 } else {
|
adam@1294
|
483 uw_reset_keep_error_message(ctx);
|
adam@1294
|
484 on_failure(ctx);
|
adam@1294
|
485 uw_write_header(ctx, "Content-type: text/html\r\n");
|
adam@1294
|
486 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
|
adam@1294
|
487 uw_write(ctx, "Fatal error: ");
|
adam@1294
|
488 uw_write(ctx, uw_error_message(ctx));
|
adam@1294
|
489 uw_write(ctx, "\n</body></html>");
|
adamc@853
|
490
|
adam@1294
|
491 return FAILED;
|
adam@1294
|
492 }
|
adamc@853
|
493 } else {
|
adamc@856
|
494 log_error(logger_data, "Unknown uw_handle return code!\n");
|
adamc@853
|
495
|
adam@1327
|
496 try_rollback(ctx, 0, logger_data, log_error);
|
adamc@853
|
497
|
adam@1294
|
498 if (uw_get_app(ctx)->on_error && !had_error) {
|
adam@1294
|
499 had_error = 1;
|
adam@1294
|
500 strcpy(errmsg, "Unknown uw_handle return code");
|
adam@1294
|
501 } else {
|
adam@1294
|
502 uw_reset_keep_request(ctx);
|
adam@1294
|
503 on_failure(ctx);
|
adam@1294
|
504 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adam@1294
|
505 uw_write(ctx, "Unknown uw_handle return code!\n");
|
adamc@853
|
506
|
adam@1294
|
507 return FAILED;
|
adam@1294
|
508 }
|
adamc@853
|
509 }
|
adamc@853
|
510
|
adam@1327
|
511 if (try_rollback(ctx, 1, logger_data, log_error))
|
adamc@853
|
512 return FAILED;
|
adamc@853
|
513
|
adamc@853
|
514 uw_reset_keep_request(ctx);
|
adamc@853
|
515 }
|
adamc@853
|
516 }
|
adamc@853
|
517
|
adamc@853
|
518 void *client_pruner(void *data) {
|
adamc@856
|
519 loggers *ls = (loggers *)data;
|
adamc@1094
|
520 uw_context ctx = uw_request_new_context(ls->app, ls->logger_data, ls->log_error, ls->log_debug);
|
adamc@853
|
521
|
adamc@853
|
522 if (!ctx)
|
adamc@853
|
523 exit(1);
|
adamc@853
|
524
|
adamc@853
|
525 while (1) {
|
adamc@853
|
526 uw_prune_clients(ctx);
|
adamc@853
|
527 sleep(5);
|
adamc@853
|
528 }
|
adamc@853
|
529 }
|