comparison src/c/request.c @ 1094:db52c32dbe42

All three current protocols work with move to using uw_app
author Adam Chlipala <adamc@hcoop.net>
date Sun, 27 Dec 2009 10:37:24 -0500
parents 740b85ef4352
children 72670131dace
comparison
equal deleted inserted replaced
1093:8d3aa6c7cee0 1094:db52c32dbe42
29 } 29 }
30 30
31 return r; 31 return r;
32 } 32 }
33 33
34 uw_context uw_request_new_context(void *logger_data, uw_logger log_error, uw_logger log_debug) { 34 uw_context uw_request_new_context(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
35 uw_context ctx = uw_init(); 35 uw_context ctx = uw_init();
36 int retries_left = MAX_RETRIES; 36 int retries_left = MAX_RETRIES;
37 uw_set_app(ctx, app);
37 38
38 while (1) { 39 while (1) {
39 failure_kind fk = uw_begin_init(ctx); 40 failure_kind fk = uw_begin_init(ctx);
40 41
41 if (fk == SUCCESS) { 42 if (fk == SUCCESS) {
93 log_error(logger_data, "Key generation failed\n"); 94 log_error(logger_data, "Key generation failed\n");
94 exit(1); 95 exit(1);
95 } 96 }
96 } 97 }
97 98
98 void uw_request_init(void *logger_data, uw_logger log_error, uw_logger log_debug) { 99 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
99 uw_context ctx; 100 uw_context ctx;
100 failure_kind fk; 101 failure_kind fk;
101 102
102 uw_global_init(); 103 uw_global_init();
103 104 uw_app_init(app);
104 ctx = uw_request_new_context(logger_data, log_error, log_debug); 105
106 ctx = uw_request_new_context(app, logger_data, log_error, log_debug);
105 107
106 if (!ctx) 108 if (!ctx)
107 exit(1); 109 exit(1);
108 110
109 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) { 111 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
110 log_debug(logger_data, "Unlimited retry during init: %s\n", uw_error_message(ctx)); 112 log_debug(logger_data, "Unlimited retry during init: %s\n", uw_error_message(ctx));
111 uw_db_rollback(ctx); 113 uw_rollback(ctx);
112 uw_reset(ctx); 114 uw_reset(ctx);
113 } 115 }
114 116
115 if (fk != SUCCESS) { 117 if (fk != SUCCESS) {
116 log_error(logger_data, "Failed to initialize database! %s\n", uw_error_message(ctx)); 118 log_error(logger_data, "Failed to initialize database! %s\n", uw_error_message(ctx));
117 uw_db_rollback(ctx); 119 uw_rollback(ctx);
118 exit(1); 120 exit(1);
119 } 121 }
120 122
121 uw_free(ctx); 123 uw_free(ctx);
122 124
148 150
149 void uw_free_request_context(uw_request_context r) { 151 void uw_free_request_context(uw_request_context r) {
150 free(r->path_copy); 152 free(r->path_copy);
151 free(r); 153 free(r);
152 } 154 }
153
154 extern char *uw_url_prefix;
155 155
156 request_result uw_request(uw_request_context rc, uw_context ctx, 156 request_result uw_request(uw_request_context rc, uw_context ctx,
157 char *method, char *path, char *query_string, 157 char *method, char *path, char *query_string,
158 char *body, size_t body_len, 158 char *body, size_t body_len,
159 void (*on_success)(uw_context), void (*on_failure)(uw_context), 159 void (*on_success)(uw_context), void (*on_failure)(uw_context),
166 failure_kind fk; 166 failure_kind fk;
167 int is_post = 0, do_normal_send = 1; 167 int is_post = 0, do_normal_send = 1;
168 char *boundary = NULL; 168 char *boundary = NULL;
169 size_t boundary_len; 169 size_t boundary_len;
170 char *inputs; 170 char *inputs;
171 const char *prefix = uw_get_url_prefix(ctx);
171 172
172 uw_set_currentUrl(ctx, path); 173 uw_set_currentUrl(ctx, path);
173 174
174 if (!strcmp(method, "POST")) { 175 if (!strcmp(method, "POST")) {
175 char *clen_s = uw_Basis_requestHeader(ctx, "Content-length"); 176 char *clen_s = uw_Basis_requestHeader(ctx, "Content-length");
206 } else if (strcmp(method, "GET")) { 207 } else if (strcmp(method, "GET")) {
207 log_error(logger_data, "Not ready for non-GET/POST command: %s\n", method); 208 log_error(logger_data, "Not ready for non-GET/POST command: %s\n", method);
208 return FAILED; 209 return FAILED;
209 } 210 }
210 211
211 if (!strncmp(path, uw_url_prefix, strlen(uw_url_prefix)) 212 if (!strncmp(path, prefix, strlen(prefix))
212 && !strcmp(path + strlen(uw_url_prefix), ".msgs")) { 213 && !strcmp(path + strlen(prefix), ".msgs")) {
213 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client"); 214 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
214 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass"); 215 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
215 216
216 if (sock < 0) { 217 if (sock < 0) {
217 log_error(logger_data, ".msgs requested, but not socket supplied\n"); 218 log_error(logger_data, ".msgs requested, but not socket supplied\n");
433 uw_reset_keep_request(ctx); 434 uw_reset_keep_request(ctx);
434 } 435 }
435 } 436 }
436 437
437 typedef struct { 438 typedef struct {
439 uw_app *app;
438 void *logger_data; 440 void *logger_data;
439 uw_logger log_error, log_debug; 441 uw_logger log_error, log_debug;
440 } loggers; 442 } loggers;
441 443
442 void *client_pruner(void *data) { 444 void *client_pruner(void *data) {
443 loggers *ls = (loggers *)data; 445 loggers *ls = (loggers *)data;
444 uw_context ctx = uw_request_new_context(ls->logger_data, ls->log_error, ls->log_debug); 446 uw_context ctx = uw_request_new_context(ls->app, ls->logger_data, ls->log_error, ls->log_debug);
445 447
446 if (!ctx) 448 if (!ctx)
447 exit(1); 449 exit(1);
448 450
449 while (1) { 451 while (1) {