adamc@741
|
1 #define _XOPEN_SOURCE 500
|
adamc@436
|
2
|
adamc@117
|
3 #include <stdlib.h>
|
adamc@102
|
4 #include <stdio.h>
|
adamc@117
|
5 #include <string.h>
|
adamc@457
|
6 #include <strings.h>
|
adamc@106
|
7 #include <ctype.h>
|
adamc@167
|
8 #include <setjmp.h>
|
adamc@167
|
9 #include <stdarg.h>
|
adamc@682
|
10 #include <assert.h>
|
adamc@741
|
11 #include <ctype.h>
|
adamc@102
|
12
|
adamc@667
|
13 #include <pthread.h>
|
adamc@667
|
14
|
adamc@102
|
15 #include "types.h"
|
adamc@102
|
16
|
adamc@311
|
17 uw_unit uw_unit_v = {};
|
adamc@102
|
18
|
adamc@667
|
19
|
adamc@667
|
20 // Socket extras
|
adamc@667
|
21
|
adamc@667
|
22 int uw_really_send(int sock, const void *buf, size_t len) {
|
adamc@667
|
23 while (len > 0) {
|
adamc@667
|
24 size_t n = send(sock, buf, len, 0);
|
adamc@667
|
25
|
adamc@667
|
26 if (n < 0)
|
adamc@667
|
27 return n;
|
adamc@667
|
28
|
adamc@667
|
29 buf += n;
|
adamc@667
|
30 len -= n;
|
adamc@667
|
31 }
|
adamc@667
|
32
|
adamc@667
|
33 return 0;
|
adamc@667
|
34 }
|
adamc@667
|
35
|
adamc@667
|
36
|
adamc@667
|
37 // Buffers
|
adamc@667
|
38
|
adamc@667
|
39 typedef struct {
|
adamc@667
|
40 char *start, *front, *back;
|
adamc@667
|
41 } buf;
|
adamc@667
|
42
|
adamc@667
|
43 static void buf_init(buf *b, size_t s) {
|
adamc@667
|
44 b->front = b->start = malloc(s);
|
adamc@667
|
45 b->back = b->front + s;
|
adamc@667
|
46 }
|
adamc@667
|
47
|
adamc@667
|
48 static void buf_free(buf *b) {
|
adamc@667
|
49 free(b->start);
|
adamc@667
|
50 }
|
adamc@667
|
51
|
adamc@667
|
52 static void buf_reset(buf *b) {
|
adamc@667
|
53 b->front = b->start;
|
adamc@667
|
54 }
|
adamc@667
|
55
|
adamc@667
|
56 static void buf_check(buf *b, size_t extra) {
|
adamc@667
|
57 if (b->back - b->front < extra) {
|
adamc@667
|
58 size_t desired = b->front - b->start + extra, next;
|
adamc@667
|
59 char *new_heap;
|
adamc@667
|
60
|
adamc@667
|
61 next = b->back - b->start;
|
adamc@667
|
62 if (next == 0)
|
adamc@667
|
63 next = 1;
|
adamc@667
|
64 for (; next < desired; next *= 2);
|
adamc@667
|
65
|
adamc@667
|
66 new_heap = realloc(b->start, next);
|
adamc@667
|
67 b->front = new_heap + (b->front - b->start);
|
adamc@667
|
68 b->back = new_heap + next;
|
adamc@667
|
69 b->start = new_heap;
|
adamc@667
|
70 }
|
adamc@667
|
71 }
|
adamc@667
|
72
|
adamc@667
|
73 static size_t buf_used(buf *b) {
|
adamc@667
|
74 return b->front - b->start;
|
adamc@667
|
75 }
|
adamc@667
|
76
|
adamc@667
|
77 static size_t buf_avail(buf *b) {
|
adamc@667
|
78 return b->back - b->start;
|
adamc@667
|
79 }
|
adamc@667
|
80
|
adamc@668
|
81 static void buf_append(buf *b, const char *s, size_t len) {
|
adamc@672
|
82 buf_check(b, len+1);
|
adamc@668
|
83 memcpy(b->front, s, len);
|
adamc@668
|
84 b->front += len;
|
adamc@672
|
85 *b->front = 0;
|
adamc@668
|
86 }
|
adamc@667
|
87
|
adamc@668
|
88
|
adamc@672
|
89 // Persistent state types
|
adamc@667
|
90
|
adamc@667
|
91 typedef enum { UNUSED, USED } usage;
|
adamc@667
|
92
|
adamc@667
|
93 typedef struct client {
|
adamc@682
|
94 unsigned id;
|
adamc@667
|
95 usage mode;
|
adamc@682
|
96 int pass;
|
adamc@682
|
97 struct client *next;
|
adamc@689
|
98 pthread_mutex_t lock, pull_lock;
|
adamc@682
|
99 buf msgs;
|
adamc@682
|
100 int sock;
|
adamc@682
|
101 time_t last_contact;
|
adamc@682
|
102 unsigned n_channels;
|
adamc@685
|
103 unsigned refcount;
|
adamc@667
|
104 } client;
|
adamc@667
|
105
|
adamc@672
|
106
|
adamc@672
|
107 // Persistent client state
|
adamc@672
|
108
|
adamc@682
|
109 static client **clients, *clients_free, *clients_used;
|
adamc@682
|
110 static unsigned n_clients;
|
adamc@667
|
111
|
adamc@667
|
112 static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
|
adamc@667
|
113
|
adamc@682
|
114 static client *new_client() {
|
adamc@667
|
115 client *c;
|
adamc@667
|
116
|
adamc@667
|
117 pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
118
|
adamc@667
|
119 if (clients_free) {
|
adamc@667
|
120 c = clients_free;
|
adamc@682
|
121 clients_free = clients_free->next;
|
adamc@667
|
122 }
|
adamc@667
|
123 else {
|
adamc@667
|
124 ++n_clients;
|
adamc@667
|
125 clients = realloc(clients, sizeof(client) * n_clients);
|
adamc@667
|
126 c = malloc(sizeof(client));
|
adamc@667
|
127 c->id = n_clients-1;
|
adamc@682
|
128 pthread_mutex_init(&c->lock, NULL);
|
adamc@689
|
129 pthread_mutex_init(&c->pull_lock, NULL);
|
adamc@682
|
130 buf_init(&c->msgs, 0);
|
adamc@667
|
131 clients[n_clients-1] = c;
|
adamc@667
|
132 }
|
adamc@667
|
133
|
adamc@682
|
134 pthread_mutex_lock(&c->lock);
|
adamc@667
|
135 c->mode = USED;
|
adamc@682
|
136 c->pass = rand();
|
adamc@682
|
137 c->sock = -1;
|
adamc@682
|
138 c->last_contact = time(NULL);
|
adamc@682
|
139 buf_reset(&c->msgs);
|
adamc@682
|
140 c->n_channels = 0;
|
adamc@685
|
141 c->refcount = 0;
|
adamc@682
|
142 pthread_mutex_unlock(&c->lock);
|
adamc@682
|
143
|
adamc@682
|
144 c->next = clients_used;
|
adamc@682
|
145 clients_used = c;
|
adamc@667
|
146
|
adamc@667
|
147 pthread_mutex_unlock(&clients_mutex);
|
adamc@667
|
148
|
adamc@667
|
149 return c;
|
adamc@667
|
150 }
|
adamc@667
|
151
|
adamc@685
|
152 static void use_client(client *c) {
|
adamc@685
|
153 pthread_mutex_lock(&c->lock);
|
adamc@685
|
154 ++c->refcount;
|
adamc@685
|
155 pthread_mutex_unlock(&c->lock);
|
adamc@689
|
156 pthread_mutex_lock(&c->pull_lock);
|
adamc@685
|
157 }
|
adamc@685
|
158
|
adamc@685
|
159 static void release_client(client *c) {
|
adamc@689
|
160 pthread_mutex_unlock(&c->pull_lock);
|
adamc@685
|
161 pthread_mutex_lock(&c->lock);
|
adamc@685
|
162 --c->refcount;
|
adamc@685
|
163 pthread_mutex_unlock(&c->lock);
|
adamc@689
|
164
|
adamc@685
|
165 }
|
adamc@685
|
166
|
adamc@667
|
167 static const char begin_msgs[] = "HTTP/1.1 200 OK\r\nContent-type: text/plain\r\n\r\n";
|
adamc@667
|
168
|
adamc@682
|
169 static client *find_client(unsigned id) {
|
adamc@667
|
170 client *c;
|
adamc@667
|
171
|
adamc@667
|
172 pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
173
|
adamc@667
|
174 if (id >= n_clients) {
|
adamc@667
|
175 pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
176 return NULL;
|
adamc@667
|
177 }
|
adamc@667
|
178
|
adamc@667
|
179 c = clients[id];
|
adamc@667
|
180
|
adamc@668
|
181 pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
182 return c;
|
adamc@668
|
183 }
|
adamc@668
|
184
|
adamc@682
|
185 void uw_client_connect(unsigned id, int pass, int sock) {
|
adamc@682
|
186 client *c = find_client(id);
|
adamc@668
|
187
|
adamc@668
|
188 if (c == NULL) {
|
adamc@667
|
189 close(sock);
|
adamc@682
|
190 fprintf(stderr, "Out-of-bounds client request (%u)\n", id);
|
adamc@667
|
191 return;
|
adamc@667
|
192 }
|
adamc@667
|
193
|
adamc@682
|
194 pthread_mutex_lock(&c->lock);
|
adamc@671
|
195
|
adamc@682
|
196 if (c->mode != USED) {
|
adamc@682
|
197 pthread_mutex_unlock(&c->lock);
|
adamc@667
|
198 close(sock);
|
adamc@682
|
199 fprintf(stderr, "Client request for unused slot (%u)\n", id);
|
adamc@667
|
200 return;
|
adamc@667
|
201 }
|
adamc@667
|
202
|
adamc@682
|
203 if (pass != c->pass) {
|
adamc@682
|
204 pthread_mutex_unlock(&c->lock);
|
adamc@682
|
205 close(sock);
|
adamc@682
|
206 fprintf(stderr, "Wrong client password (%u, %d)\n", id, pass);
|
adamc@682
|
207 return;
|
adamc@667
|
208 }
|
adamc@667
|
209
|
adamc@682
|
210 if (c->sock != -1) {
|
adamc@682
|
211 close(c->sock);
|
adamc@682
|
212 c->sock = -1;
|
adamc@682
|
213 }
|
adamc@667
|
214
|
adamc@682
|
215 c->last_contact = time(NULL);
|
adamc@682
|
216
|
adamc@682
|
217 if (buf_used(&c->msgs) > 0) {
|
adamc@667
|
218 uw_really_send(sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adamc@682
|
219 uw_really_send(sock, c->msgs.start, buf_used(&c->msgs));
|
adamc@682
|
220 buf_reset(&c->msgs);
|
adamc@667
|
221 close(sock);
|
adamc@667
|
222 }
|
adamc@668
|
223 else
|
adamc@682
|
224 c->sock = sock;
|
adamc@667
|
225
|
adamc@682
|
226 pthread_mutex_unlock(&c->lock);
|
adamc@667
|
227 }
|
adamc@667
|
228
|
adamc@682
|
229 static void free_client(client *c) {
|
adamc@683
|
230 printf("Freeing client %u\n", c->id);
|
adamc@667
|
231
|
adamc@682
|
232 c->mode = UNUSED;
|
adamc@682
|
233 c->pass = -1;
|
adamc@667
|
234
|
adamc@682
|
235 c->next = clients_free;
|
adamc@682
|
236 clients_free = c;
|
adamc@667
|
237 }
|
adamc@667
|
238
|
adamc@682
|
239 static uw_Basis_channel new_channel(client *c) {
|
adamc@682
|
240 uw_Basis_channel ch = {c->id, c->n_channels++};
|
adamc@668
|
241 return ch;
|
adamc@668
|
242 }
|
adamc@668
|
243
|
adamc@685
|
244 static void client_send(client *c, buf *msg) {
|
adamc@685
|
245 pthread_mutex_lock(&c->lock);
|
adamc@671
|
246
|
adamc@682
|
247 if (c->sock != -1) {
|
adamc@682
|
248 uw_really_send(c->sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adamc@682
|
249 uw_really_send(c->sock, msg->start, buf_used(msg));
|
adamc@682
|
250 close(c->sock);
|
adamc@682
|
251 c->sock = -1;
|
adamc@682
|
252 } else
|
adamc@682
|
253 buf_append(&c->msgs, msg->start, buf_used(msg));
|
adamc@671
|
254
|
adamc@685
|
255 pthread_mutex_unlock(&c->lock);
|
adamc@668
|
256 }
|
adamc@668
|
257
|
adamc@668
|
258
|
adamc@668
|
259 // Global entry points
|
adamc@668
|
260
|
adamc@668
|
261 void uw_global_init() {
|
adamc@668
|
262 srand(time(NULL) ^ getpid());
|
adamc@668
|
263
|
adamc@668
|
264 clients = malloc(0);
|
adamc@668
|
265 }
|
adamc@668
|
266
|
adamc@668
|
267
|
adamc@667
|
268 // Single-request state
|
adamc@667
|
269
|
adamc@167
|
270 #define ERROR_BUF_LEN 1024
|
adamc@167
|
271
|
adamc@323
|
272 typedef struct regions {
|
adamc@323
|
273 struct regions *next;
|
adamc@323
|
274 } regions;
|
adamc@323
|
275
|
adamc@425
|
276 typedef struct {
|
adamc@425
|
277 void (*func)(void*);
|
adamc@425
|
278 void *arg;
|
adamc@425
|
279 } cleanup;
|
adamc@425
|
280
|
adamc@671
|
281 typedef struct {
|
adamc@682
|
282 unsigned client;
|
adamc@671
|
283 buf msgs;
|
adamc@682
|
284 } delta;
|
adamc@671
|
285
|
adamc@737
|
286 typedef enum {
|
adamc@759
|
287 UNSET, NORMAL, FIL, SUBFORM, SUBFORMS, ENTRY
|
adamc@737
|
288 } input_kind;
|
adamc@737
|
289
|
adamc@756
|
290 typedef struct input {
|
adamc@737
|
291 input_kind kind;
|
adamc@737
|
292 union {
|
adamc@737
|
293 char *normal;
|
adamc@739
|
294 uw_Basis_file file;
|
adamc@756
|
295 struct {
|
adamc@759
|
296 struct input *fields, *parent;
|
adamc@756
|
297 } subform;
|
adamc@759
|
298 struct {
|
adamc@759
|
299 struct input *entries, *parent;
|
adamc@759
|
300 } subforms;
|
adamc@759
|
301 struct {
|
adamc@759
|
302 struct input *fields, *next, *parent;
|
adamc@759
|
303 } entry;
|
adamc@737
|
304 } data;
|
adamc@737
|
305 } input;
|
adamc@737
|
306
|
adamc@771
|
307 typedef struct {
|
adamc@771
|
308 void *data;
|
adamc@771
|
309 uw_callback commit, rollback, free;
|
adamc@771
|
310 } transactional;
|
adamc@771
|
311
|
adamc@311
|
312 struct uw_context {
|
adamc@458
|
313 char *headers, *headers_end;
|
adamc@457
|
314
|
adamc@666
|
315 buf outHeaders, page, heap, script;
|
adamc@759
|
316 input *inputs, *subinputs, *cur_container;
|
adamc@756
|
317 size_t n_subinputs, used_subinputs;
|
adamc@167
|
318
|
adamc@565
|
319 int source_count;
|
adamc@562
|
320
|
adamc@272
|
321 void *db;
|
adamc@272
|
322
|
adamc@167
|
323 jmp_buf jmp_buf;
|
adamc@167
|
324
|
adamc@323
|
325 regions *regions;
|
adamc@323
|
326
|
adamc@425
|
327 cleanup *cleanup, *cleanup_front, *cleanup_back;
|
adamc@425
|
328
|
adamc@667
|
329 const char *script_header, *url_prefix;
|
adamc@643
|
330
|
adamc@736
|
331 int needs_push, needs_sig;
|
adamc@693
|
332
|
adamc@682
|
333 size_t n_deltas, used_deltas;
|
adamc@682
|
334 delta *deltas;
|
adamc@671
|
335
|
adamc@673
|
336 int timeout;
|
adamc@673
|
337
|
adamc@682
|
338 client *client;
|
adamc@682
|
339
|
adamc@771
|
340 transactional *transactionals;
|
adamc@771
|
341 size_t n_transactionals, used_transactionals;
|
adamc@771
|
342
|
adamc@167
|
343 char error_message[ERROR_BUF_LEN];
|
adamc@117
|
344 };
|
adamc@117
|
345
|
adamc@683
|
346 extern int uw_inputs_len, uw_timeout;
|
adamc@144
|
347
|
adamc@687
|
348 uw_context uw_init() {
|
adamc@311
|
349 uw_context ctx = malloc(sizeof(struct uw_context));
|
adamc@136
|
350
|
adamc@458
|
351 ctx->headers = ctx->headers_end = NULL;
|
adamc@457
|
352
|
adamc@687
|
353 buf_init(&ctx->outHeaders, 0);
|
adamc@687
|
354 buf_init(&ctx->page, 0);
|
adamc@687
|
355 buf_init(&ctx->heap, 0);
|
adamc@698
|
356 buf_init(&ctx->script, 1);
|
adamc@667
|
357 ctx->script.start[0] = 0;
|
adamc@136
|
358
|
adamc@737
|
359 ctx->inputs = calloc(uw_inputs_len, sizeof(input));
|
adamc@759
|
360 ctx->cur_container = NULL;
|
adamc@756
|
361 ctx->subinputs = malloc(0);
|
adamc@756
|
362 ctx->n_subinputs = ctx->used_subinputs = 0;
|
adamc@144
|
363
|
adamc@272
|
364 ctx->db = NULL;
|
adamc@272
|
365
|
adamc@323
|
366 ctx->regions = NULL;
|
adamc@323
|
367
|
adamc@425
|
368 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
|
adamc@425
|
369
|
adamc@643
|
370 ctx->script_header = "";
|
adamc@667
|
371 ctx->url_prefix = "/";
|
adamc@693
|
372 ctx->needs_push = 0;
|
adamc@736
|
373 ctx->needs_sig = 0;
|
adamc@643
|
374
|
adamc@167
|
375 ctx->error_message[0] = 0;
|
adamc@167
|
376
|
adamc@565
|
377 ctx->source_count = 0;
|
adamc@562
|
378
|
adamc@682
|
379 ctx->n_deltas = ctx->used_deltas = 0;
|
adamc@671
|
380 ctx->deltas = malloc(0);
|
adamc@671
|
381
|
adamc@673
|
382 ctx->timeout = uw_timeout;
|
adamc@673
|
383
|
adamc@700
|
384 ctx->client = NULL;
|
adamc@700
|
385
|
adamc@700
|
386 ctx->error_message[0] = 0;
|
adamc@700
|
387
|
adamc@771
|
388 ctx->transactionals = malloc(0);
|
adamc@771
|
389 ctx->n_transactionals = ctx->used_transactionals = 0;
|
adamc@771
|
390
|
adamc@117
|
391 return ctx;
|
adamc@106
|
392 }
|
adamc@106
|
393
|
adamc@311
|
394 void uw_set_db(uw_context ctx, void *db) {
|
adamc@272
|
395 ctx->db = db;
|
adamc@272
|
396 }
|
adamc@272
|
397
|
adamc@311
|
398 void *uw_get_db(uw_context ctx) {
|
adamc@272
|
399 return ctx->db;
|
adamc@272
|
400 }
|
adamc@272
|
401
|
adamc@311
|
402 void uw_free(uw_context ctx) {
|
adamc@671
|
403 size_t i;
|
adamc@671
|
404
|
adamc@666
|
405 buf_free(&ctx->outHeaders);
|
adamc@666
|
406 buf_free(&ctx->script);
|
adamc@666
|
407 buf_free(&ctx->page);
|
adamc@666
|
408 buf_free(&ctx->heap);
|
adamc@144
|
409 free(ctx->inputs);
|
adamc@756
|
410 free(ctx->subinputs);
|
adamc@425
|
411 free(ctx->cleanup);
|
adamc@771
|
412 free(ctx->transactionals);
|
adamc@671
|
413
|
adamc@682
|
414 for (i = 0; i < ctx->n_deltas; ++i)
|
adamc@671
|
415 buf_free(&ctx->deltas[i].msgs);
|
adamc@671
|
416
|
adamc@136
|
417 free(ctx);
|
adamc@136
|
418 }
|
adamc@136
|
419
|
adamc@562
|
420 void uw_reset_keep_error_message(uw_context ctx) {
|
adamc@756
|
421 size_t i;
|
adamc@756
|
422
|
adamc@666
|
423 buf_reset(&ctx->outHeaders);
|
adamc@666
|
424 buf_reset(&ctx->script);
|
adamc@667
|
425 ctx->script.start[0] = 0;
|
adamc@666
|
426 buf_reset(&ctx->page);
|
adamc@666
|
427 buf_reset(&ctx->heap);
|
adamc@323
|
428 ctx->regions = NULL;
|
adamc@425
|
429 ctx->cleanup_front = ctx->cleanup;
|
adamc@565
|
430 ctx->source_count = 0;
|
adamc@682
|
431 ctx->used_deltas = 0;
|
adamc@682
|
432 ctx->client = NULL;
|
adamc@759
|
433 ctx->cur_container = NULL;
|
adamc@771
|
434 ctx->used_transactionals = 0;
|
adamc@167
|
435 }
|
adamc@167
|
436
|
adamc@562
|
437 void uw_reset_keep_request(uw_context ctx) {
|
adamc@562
|
438 uw_reset_keep_error_message(ctx);
|
adamc@562
|
439 ctx->error_message[0] = 0;
|
adamc@167
|
440 }
|
adamc@167
|
441
|
adamc@311
|
442 void uw_reset(uw_context ctx) {
|
adamc@311
|
443 uw_reset_keep_request(ctx);
|
adamc@737
|
444 memset(ctx->inputs, 0, uw_inputs_len * sizeof(input));
|
adamc@756
|
445 memset(ctx->subinputs, 0, ctx->n_subinputs * sizeof(input));
|
adamc@756
|
446 ctx->used_subinputs = 0;
|
adamc@144
|
447 }
|
adamc@144
|
448
|
adamc@311
|
449 void uw_db_init(uw_context);
|
adamc@311
|
450 void uw_handle(uw_context, char *);
|
adamc@167
|
451
|
adamc@311
|
452 failure_kind uw_begin_init(uw_context ctx) {
|
adamc@272
|
453 int r = setjmp(ctx->jmp_buf);
|
adamc@272
|
454
|
adamc@272
|
455 if (r == 0)
|
adamc@311
|
456 uw_db_init(ctx);
|
adamc@272
|
457
|
adamc@272
|
458 return r;
|
adamc@272
|
459 }
|
adamc@272
|
460
|
adamc@458
|
461 void uw_set_headers(uw_context ctx, char *headers) {
|
adamc@458
|
462 char *s = headers, *s2;
|
adamc@458
|
463 ctx->headers = headers;
|
adamc@458
|
464
|
adamc@458
|
465 while (s2 = strchr(s, '\r')) {
|
adamc@458
|
466 s = s2;
|
adamc@458
|
467
|
adamc@458
|
468 if (s[1] == 0)
|
adamc@458
|
469 break;
|
adamc@458
|
470
|
adamc@458
|
471 *s = 0;
|
adamc@458
|
472 s += 2;
|
adamc@458
|
473 }
|
adamc@458
|
474
|
adamc@458
|
475 ctx->headers_end = s;
|
adamc@458
|
476 }
|
adamc@458
|
477
|
adamc@742
|
478 void uw_headers_moved(uw_context ctx, char *headers) {
|
adamc@742
|
479 ctx->headers_end = headers + (ctx->headers_end - ctx->headers);
|
adamc@742
|
480 ctx->headers = headers;
|
adamc@742
|
481 }
|
adamc@742
|
482
|
adamc@671
|
483 int uw_db_begin(uw_context);
|
adamc@167
|
484
|
adamc@801
|
485 static void uw_set_error(uw_context ctx, const char *fmt, ...) {
|
adamc@801
|
486 va_list ap;
|
adamc@801
|
487 va_start(ap, fmt);
|
adamc@801
|
488
|
adamc@801
|
489 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
|
adamc@801
|
490 }
|
adamc@801
|
491
|
adamc@311
|
492 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
|
adamc@425
|
493 cleanup *cl;
|
adamc@425
|
494
|
adamc@167
|
495 va_list ap;
|
adamc@167
|
496 va_start(ap, fmt);
|
adamc@167
|
497
|
adamc@167
|
498 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
|
adamc@167
|
499
|
adamc@425
|
500 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@425
|
501 cl->func(cl->arg);
|
adamc@425
|
502
|
adamc@425
|
503 ctx->cleanup_front = ctx->cleanup;
|
adamc@425
|
504
|
adamc@190
|
505 longjmp(ctx->jmp_buf, fk);
|
adamc@167
|
506 }
|
adamc@167
|
507
|
adamc@425
|
508 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
|
adamc@425
|
509 if (ctx->cleanup_front >= ctx->cleanup_back) {
|
adamc@425
|
510 int len = ctx->cleanup_back - ctx->cleanup, newLen;
|
adamc@425
|
511 if (len == 0)
|
adamc@425
|
512 newLen = 1;
|
adamc@425
|
513 else
|
adamc@428
|
514 newLen = len * 2;
|
adamc@470
|
515 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
|
adamc@425
|
516 ctx->cleanup_front = ctx->cleanup + len;
|
adamc@425
|
517 ctx->cleanup_back = ctx->cleanup + newLen;
|
adamc@425
|
518 }
|
adamc@425
|
519
|
adamc@425
|
520 ctx->cleanup_front->func = func;
|
adamc@425
|
521 ctx->cleanup_front->arg = arg;
|
adamc@425
|
522 ++ctx->cleanup_front;
|
adamc@425
|
523 }
|
adamc@425
|
524
|
adamc@682
|
525 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
|
adamc@682
|
526 int len = strlen(h);
|
adamc@682
|
527 char *s = ctx->headers, *p;
|
adamc@682
|
528
|
adamc@682
|
529 while (p = strchr(s, ':')) {
|
adamc@682
|
530 if (p - s == len && !strncasecmp(s, h, len)) {
|
adamc@682
|
531 return p + 2;
|
adamc@682
|
532 } else {
|
adamc@682
|
533 if ((s = strchr(p, 0)) && s < ctx->headers_end)
|
adamc@682
|
534 s += 2;
|
adamc@682
|
535 else
|
adamc@682
|
536 return NULL;
|
adamc@682
|
537 }
|
adamc@682
|
538 }
|
adamc@682
|
539
|
adamc@682
|
540 return NULL;
|
adamc@682
|
541 }
|
adamc@682
|
542
|
adamc@682
|
543 void uw_login(uw_context ctx) {
|
adamc@693
|
544 if (ctx->needs_push) {
|
adamc@682
|
545 char *id_s, *pass_s;
|
adamc@682
|
546
|
adamc@682
|
547 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client"))
|
adamc@682
|
548 && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) {
|
adamc@682
|
549 unsigned id = atoi(id_s);
|
adamc@682
|
550 int pass = atoi(pass_s);
|
adamc@682
|
551 client *c = find_client(id);
|
adamc@682
|
552
|
adamc@682
|
553 if (c == NULL)
|
adamc@682
|
554 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", id_s, pass_s);
|
adamc@682
|
555 else {
|
adamc@685
|
556 use_client(c);
|
adamc@682
|
557 ctx->client = c;
|
adamc@682
|
558
|
adamc@682
|
559 if (c->mode != USED)
|
adamc@682
|
560 uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id);
|
adamc@682
|
561 if (c->pass != pass)
|
adamc@682
|
562 uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass);
|
adamc@682
|
563 }
|
adamc@682
|
564 } else {
|
adamc@682
|
565 client *c = new_client();
|
adamc@685
|
566 use_client(c);
|
adamc@682
|
567 ctx->client = c;
|
adamc@682
|
568 }
|
adamc@682
|
569 }
|
adamc@682
|
570 }
|
adamc@682
|
571
|
adamc@671
|
572 failure_kind uw_begin(uw_context ctx, char *path) {
|
adamc@671
|
573 int r = setjmp(ctx->jmp_buf);
|
adamc@671
|
574
|
adamc@671
|
575 if (r == 0) {
|
adamc@671
|
576 if (uw_db_begin(ctx))
|
adamc@671
|
577 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
|
adamc@682
|
578
|
adamc@671
|
579 uw_handle(ctx, path);
|
adamc@671
|
580 }
|
adamc@671
|
581
|
adamc@671
|
582 return r;
|
adamc@671
|
583 }
|
adamc@671
|
584
|
adamc@772
|
585 uw_Basis_client uw_Basis_self(uw_context ctx) {
|
adamc@682
|
586 if (ctx->client == NULL)
|
adamc@682
|
587 uw_error(ctx, FATAL, "Call to Basis.self() from page that has only server-side code");
|
adamc@682
|
588
|
adamc@682
|
589 return ctx->client->id;
|
adamc@682
|
590 }
|
adamc@682
|
591
|
adamc@425
|
592 void uw_pop_cleanup(uw_context ctx) {
|
adamc@425
|
593 if (ctx->cleanup_front == ctx->cleanup)
|
adamc@425
|
594 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
|
adamc@425
|
595
|
adamc@425
|
596 --ctx->cleanup_front;
|
adamc@425
|
597 ctx->cleanup_front->func(ctx->cleanup_front->arg);
|
adamc@425
|
598 }
|
adamc@425
|
599
|
adamc@311
|
600 char *uw_error_message(uw_context ctx) {
|
adamc@167
|
601 return ctx->error_message;
|
adamc@167
|
602 }
|
adamc@167
|
603
|
adamc@737
|
604 extern int uw_input_num(const char*);
|
adamc@144
|
605
|
adamc@759
|
606 static input *INP(uw_context ctx) {
|
adamc@759
|
607 if (ctx->cur_container == NULL)
|
adamc@759
|
608 return ctx->inputs;
|
adamc@759
|
609 else if (ctx->cur_container->kind == SUBFORM)
|
adamc@759
|
610 return ctx->cur_container->data.subform.fields;
|
adamc@759
|
611 else if (ctx->cur_container->kind == ENTRY)
|
adamc@759
|
612 return ctx->cur_container->data.entry.fields;
|
adamc@759
|
613 else
|
adamc@760
|
614 uw_error(ctx, FATAL, "INP: Wrong kind (%d, %p)", ctx->cur_container->kind, ctx->cur_container);
|
adamc@759
|
615 }
|
adamc@759
|
616
|
adamc@760
|
617 static void adjust_pointer(input **ptr, input *old_start, input *new_start, size_t len) {
|
adamc@760
|
618 if (*ptr != NULL && *ptr >= old_start && *ptr < old_start + len)
|
adamc@760
|
619 *ptr += new_start - old_start;
|
adamc@760
|
620 }
|
adamc@760
|
621
|
adamc@760
|
622 static void adjust_input(input *x, input *old_start, input *new_start, size_t len) {
|
adamc@759
|
623 switch (x->kind) {
|
adamc@759
|
624 case SUBFORM:
|
adamc@760
|
625 adjust_pointer(&x->data.subform.fields, old_start, new_start, len);
|
adamc@760
|
626 adjust_pointer(&x->data.subform.parent, old_start, new_start, len);
|
adamc@759
|
627 break;
|
adamc@759
|
628 case SUBFORMS:
|
adamc@760
|
629 adjust_pointer(&x->data.subforms.entries, old_start, new_start, len);
|
adamc@760
|
630 adjust_pointer(&x->data.subforms.parent, old_start, new_start, len);
|
adamc@759
|
631 break;
|
adamc@759
|
632 case ENTRY:
|
adamc@760
|
633 adjust_pointer(&x->data.entry.fields, old_start, new_start, len);
|
adamc@760
|
634 adjust_pointer(&x->data.entry.next, old_start, new_start, len);
|
adamc@760
|
635 adjust_pointer(&x->data.entry.parent, old_start, new_start, len);
|
adamc@759
|
636 }
|
adamc@759
|
637 }
|
adamc@759
|
638
|
adamc@759
|
639 static input *check_input_space(uw_context ctx, size_t len) {
|
adamc@759
|
640 size_t i;
|
adamc@759
|
641 input *r;
|
adamc@759
|
642
|
adamc@759
|
643 if (ctx->used_subinputs + len >= ctx->n_subinputs) {
|
adamc@759
|
644 input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len));
|
adamc@759
|
645 size_t offset = new_subinputs - ctx->subinputs;
|
adamc@759
|
646
|
adamc@760
|
647 if (ctx->subinputs != new_subinputs) {
|
adamc@760
|
648 for (i = 0; i < ctx->used_subinputs; ++i)
|
adamc@760
|
649 adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
|
adamc@760
|
650 for (i = 0; i < uw_inputs_len; ++i)
|
adamc@760
|
651 adjust_input(&ctx->inputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
|
adamc@760
|
652
|
adamc@760
|
653 adjust_pointer(&ctx->cur_container, ctx->subinputs, new_subinputs, ctx->used_subinputs);
|
adamc@760
|
654
|
adamc@760
|
655 ctx->n_subinputs = ctx->used_subinputs + len;
|
adamc@760
|
656 ctx->subinputs = new_subinputs;
|
adamc@760
|
657 }
|
adamc@759
|
658 }
|
adamc@759
|
659
|
adamc@759
|
660 r = &ctx->subinputs[ctx->used_subinputs];
|
adamc@759
|
661
|
adamc@759
|
662 for (i = 0; i < len; ++i)
|
adamc@759
|
663 ctx->subinputs[ctx->used_subinputs++].kind = UNUSED;
|
adamc@759
|
664
|
adamc@759
|
665 return r;
|
adamc@759
|
666 }
|
adamc@756
|
667
|
adamc@801
|
668 int uw_set_input(uw_context ctx, const char *name, char *value) {
|
adamc@756
|
669 if (!strcasecmp(name, ".b")) {
|
adamc@756
|
670 int n = uw_input_num(value);
|
adamc@756
|
671 input *inps;
|
adamc@756
|
672
|
adamc@801
|
673 if (n < 0) {
|
adamc@801
|
674 uw_set_error(ctx, "Bad subform name %s", value);
|
adamc@801
|
675 return -1;
|
adamc@801
|
676 }
|
adamc@801
|
677
|
adamc@801
|
678 if (n >= uw_inputs_len) {
|
adamc@801
|
679 uw_set_error(ctx, "For subform name %s, index %d is out of range", value, n);
|
adamc@801
|
680 return -1;
|
adamc@801
|
681 }
|
adamc@756
|
682
|
adamc@759
|
683 inps = check_input_space(ctx, uw_inputs_len);
|
adamc@759
|
684
|
adamc@759
|
685 INP(ctx)[n].kind = SUBFORM;
|
adamc@759
|
686 INP(ctx)[n].data.subform.parent = ctx->cur_container;
|
adamc@759
|
687 INP(ctx)[n].data.subform.fields = inps;
|
adamc@759
|
688 ctx->cur_container = &INP(ctx)[n];
|
adamc@756
|
689 } else if (!strcasecmp(name, ".e")) {
|
adamc@756
|
690 input *tmp;
|
adamc@756
|
691
|
adamc@801
|
692 if (ctx->cur_container == NULL) {
|
adamc@801
|
693 uw_set_error(ctx, "Unmatched subform closer");
|
adamc@801
|
694 return -1;
|
adamc@801
|
695 }
|
adamc@756
|
696
|
adamc@759
|
697 tmp = ctx->cur_container;
|
adamc@759
|
698 switch (tmp->kind) {
|
adamc@759
|
699 case SUBFORM:
|
adamc@759
|
700 ctx->cur_container = tmp->data.subform.parent;
|
adamc@759
|
701 tmp->data.subform.parent = NULL;
|
adamc@759
|
702 break;
|
adamc@759
|
703 case SUBFORMS:
|
adamc@759
|
704 ctx->cur_container = tmp->data.subforms.parent;
|
adamc@759
|
705 tmp->data.subforms.parent = NULL;
|
adamc@759
|
706 break;
|
adamc@759
|
707 case ENTRY:
|
adamc@759
|
708 ctx->cur_container = tmp->data.entry.parent;
|
adamc@759
|
709 break;
|
adamc@759
|
710 default:
|
adamc@801
|
711 uw_set_error(ctx, "uw_set_input: Wrong kind");
|
adamc@801
|
712 return -1;
|
adamc@759
|
713 }
|
adamc@759
|
714 } else if (!strcasecmp(name, ".s")) {
|
adamc@759
|
715 int n = uw_input_num(value);
|
adamc@759
|
716
|
adamc@801
|
717 if (n < 0) {
|
adamc@801
|
718 uw_set_error(ctx, "Bad subforms name %s", value);
|
adamc@801
|
719 return -1;
|
adamc@801
|
720 }
|
adamc@801
|
721
|
adamc@801
|
722 if (n >= uw_inputs_len) {
|
adamc@801
|
723 uw_set_error(ctx, "For subforms name %s, index %d is out of range", value, n);
|
adamc@801
|
724 return -1;
|
adamc@801
|
725 }
|
adamc@759
|
726
|
adamc@759
|
727 INP(ctx)[n].kind = SUBFORMS;
|
adamc@759
|
728 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
|
adamc@759
|
729 INP(ctx)[n].data.subforms.entries = NULL;
|
adamc@759
|
730 ctx->cur_container = &INP(ctx)[n];
|
adamc@759
|
731 } else if (!strcasecmp(name, ".i")) {
|
adamc@759
|
732 input *inps;
|
adamc@759
|
733
|
adamc@801
|
734 if (!ctx->cur_container) {
|
adamc@801
|
735 uw_set_error(ctx, "New entry without container");
|
adamc@801
|
736 return -1;
|
adamc@801
|
737 }
|
adamc@801
|
738
|
adamc@801
|
739 if (ctx->cur_container->kind != SUBFORMS) {
|
adamc@801
|
740 uw_set_error(ctx, "Bad kind for entry parent");
|
adamc@801
|
741 return -1;
|
adamc@801
|
742 }
|
adamc@759
|
743
|
adamc@759
|
744 inps = check_input_space(ctx, uw_inputs_len + 1);
|
adamc@759
|
745
|
adamc@759
|
746 inps->kind = ENTRY;
|
adamc@759
|
747 inps->data.entry.parent = ctx->cur_container;
|
adamc@759
|
748 inps->data.entry.next = ctx->cur_container->data.subforms.entries;
|
adamc@759
|
749 ctx->cur_container->data.subforms.entries = inps;
|
adamc@759
|
750
|
adamc@759
|
751 inps->data.entry.fields = inps+1;
|
adamc@759
|
752 ctx->cur_container = inps;
|
adamc@756
|
753 } else {
|
adamc@756
|
754 int n = uw_input_num(name);
|
adamc@756
|
755
|
adamc@801
|
756 if (n < 0) {
|
adamc@801
|
757 uw_set_error(ctx, "Bad input name %s", name);
|
adamc@801
|
758 return -1;
|
adamc@801
|
759 }
|
adamc@801
|
760
|
adamc@801
|
761 if (n >= uw_inputs_len) {
|
adamc@801
|
762 uw_set_error(ctx, "For input name %s, index %d is out of range", name, n);
|
adamc@801
|
763 return -1;
|
adamc@801
|
764 }
|
adamc@756
|
765
|
adamc@756
|
766 INP(ctx)[n].kind = NORMAL;
|
adamc@756
|
767 INP(ctx)[n].data.normal = value;
|
adamc@756
|
768 }
|
adamc@801
|
769
|
adamc@801
|
770 return 0;
|
adamc@144
|
771 }
|
adamc@144
|
772
|
adamc@311
|
773 char *uw_get_input(uw_context ctx, int n) {
|
adamc@169
|
774 if (n < 0)
|
adamc@311
|
775 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@311
|
776 if (n >= uw_inputs_len)
|
adamc@311
|
777 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@737
|
778
|
adamc@756
|
779 switch (INP(ctx)[n].kind) {
|
adamc@737
|
780 case UNSET:
|
adamc@737
|
781 return NULL;
|
adamc@739
|
782 case FIL:
|
adamc@739
|
783 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
|
adamc@756
|
784 case SUBFORM:
|
adamc@756
|
785 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
|
adamc@759
|
786 case SUBFORMS:
|
adamc@759
|
787 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
|
adamc@759
|
788 case ENTRY:
|
adamc@759
|
789 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
|
adamc@737
|
790 case NORMAL:
|
adamc@756
|
791 return INP(ctx)[n].data.normal;
|
adamc@737
|
792 default:
|
adamc@737
|
793 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
794 }
|
adamc@138
|
795 }
|
adamc@138
|
796
|
adamc@311
|
797 char *uw_get_optional_input(uw_context ctx, int n) {
|
adamc@190
|
798 if (n < 0)
|
adamc@311
|
799 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@311
|
800 if (n >= uw_inputs_len)
|
adamc@311
|
801 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@737
|
802
|
adamc@756
|
803 switch (INP(ctx)[n].kind) {
|
adamc@737
|
804 case UNSET:
|
adamc@737
|
805 return "";
|
adamc@739
|
806 case FIL:
|
adamc@739
|
807 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
|
adamc@756
|
808 case SUBFORM:
|
adamc@756
|
809 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
|
adamc@759
|
810 case SUBFORMS:
|
adamc@759
|
811 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
|
adamc@759
|
812 case ENTRY:
|
adamc@759
|
813 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
|
adamc@737
|
814 case NORMAL:
|
adamc@756
|
815 return INP(ctx)[n].data.normal;
|
adamc@737
|
816 default:
|
adamc@737
|
817 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
818 }
|
adamc@737
|
819 }
|
adamc@737
|
820
|
adamc@801
|
821 int uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) {
|
adamc@737
|
822 int n = uw_input_num(name);
|
adamc@737
|
823
|
adamc@801
|
824 if (n < 0) {
|
adamc@801
|
825 uw_set_error(ctx, "Bad file input name %s", name);
|
adamc@801
|
826 return -1;
|
adamc@801
|
827 }
|
adamc@801
|
828
|
adamc@801
|
829 if (n >= uw_inputs_len) {
|
adamc@801
|
830 uw_set_error(ctx, "For file input name %s, index %d is out of range", name, n);
|
adamc@801
|
831 return -1;
|
adamc@801
|
832 }
|
adamc@737
|
833
|
adamc@739
|
834 ctx->inputs[n].kind = FIL;
|
adamc@739
|
835 ctx->inputs[n].data.file = f;
|
adamc@801
|
836
|
adamc@801
|
837 return 0;
|
adamc@737
|
838 }
|
adamc@737
|
839
|
adamc@739
|
840 void *uw_malloc(uw_context ctx, size_t len);
|
adamc@739
|
841
|
adamc@760
|
842
|
adamc@760
|
843 static void parents(input *inp) {
|
adamc@760
|
844 printf("Stack: %p\n", inp);
|
adamc@760
|
845 while (inp) {
|
adamc@760
|
846 switch (inp->kind) {
|
adamc@760
|
847 case NORMAL:
|
adamc@760
|
848 printf("Normal(%p)\n", inp);
|
adamc@760
|
849 break;
|
adamc@760
|
850 case FIL:
|
adamc@760
|
851 printf("File(%p)\n", inp);
|
adamc@760
|
852 break;
|
adamc@760
|
853 case SUBFORM:
|
adamc@760
|
854 printf("Subform; fields = %p\n", inp->data.subform.fields);
|
adamc@760
|
855 inp = inp->data.subform.parent;
|
adamc@760
|
856 break;
|
adamc@760
|
857 case SUBFORMS:
|
adamc@760
|
858 printf("Subforms; entries = %p\n", inp->data.subforms.entries);
|
adamc@760
|
859 inp = inp->data.subforms.parent;
|
adamc@760
|
860 break;
|
adamc@760
|
861 case ENTRY:
|
adamc@760
|
862 printf("Entry; fields = %p; next = %p\n", inp->data.entry.fields, inp->data.entry.next);
|
adamc@760
|
863 inp = inp->data.entry.parent;
|
adamc@760
|
864 break;
|
adamc@760
|
865 default:
|
adamc@760
|
866 inp = NULL;
|
adamc@760
|
867 }
|
adamc@760
|
868 }
|
adamc@760
|
869 }
|
adamc@760
|
870
|
adamc@739
|
871 uw_Basis_file uw_get_file_input(uw_context ctx, int n) {
|
adamc@737
|
872 if (n < 0)
|
adamc@737
|
873 uw_error(ctx, FATAL, "Negative file input index %d", n);
|
adamc@737
|
874 if (n >= uw_inputs_len)
|
adamc@737
|
875 uw_error(ctx, FATAL, "Out-of-bounds file input index %d", n);
|
adamc@737
|
876
|
adamc@756
|
877 switch (INP(ctx)[n].kind) {
|
adamc@737
|
878 case UNSET:
|
adamc@737
|
879 {
|
adamc@739
|
880 char *data = uw_malloc(ctx, 0);
|
adamc@740
|
881 uw_Basis_file f = {NULL, "", {0, data}};
|
adamc@739
|
882 return f;
|
adamc@737
|
883 }
|
adamc@739
|
884 case FIL:
|
adamc@756
|
885 return INP(ctx)[n].data.file;
|
adamc@737
|
886 case NORMAL:
|
adamc@737
|
887 uw_error(ctx, FATAL, "Tried to read a normal form input as files");
|
adamc@756
|
888 case SUBFORM:
|
adamc@756
|
889 uw_error(ctx, FATAL, "Tried to read a subform form input as files");
|
adamc@759
|
890 case SUBFORMS:
|
adamc@759
|
891 uw_error(ctx, FATAL, "Tried to read a subforms form input as files");
|
adamc@759
|
892 case ENTRY:
|
adamc@759
|
893 uw_error(ctx, FATAL, "Tried to read an entry form input as files");
|
adamc@737
|
894 default:
|
adamc@737
|
895 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
896 }
|
adamc@190
|
897 }
|
adamc@190
|
898
|
adamc@756
|
899 void uw_enter_subform(uw_context ctx, int n) {
|
adamc@756
|
900 if (n < 0)
|
adamc@756
|
901 uw_error(ctx, FATAL, "Negative subform index %d", n);
|
adamc@756
|
902 if (n >= uw_inputs_len)
|
adamc@756
|
903 uw_error(ctx, FATAL, "Out-of-bounds subform index %d", n);
|
adamc@756
|
904
|
adamc@756
|
905 switch (INP(ctx)[n].kind) {
|
adamc@756
|
906 case UNSET:
|
adamc@756
|
907 uw_error(ctx, FATAL, "Missing subform");
|
adamc@756
|
908 case FIL:
|
adamc@756
|
909 uw_error(ctx, FATAL, "Tried to read a file form input as subform");
|
adamc@756
|
910 case NORMAL:
|
adamc@756
|
911 uw_error(ctx, FATAL, "Tried to read a normal form input as subform");
|
adamc@759
|
912 case SUBFORMS:
|
adamc@759
|
913 uw_error(ctx, FATAL, "Tried to read a subforms form input as subform");
|
adamc@759
|
914 case ENTRY:
|
adamc@759
|
915 uw_error(ctx, FATAL, "Tried to read an entry form input as subform");
|
adamc@756
|
916 case SUBFORM:
|
adamc@759
|
917 INP(ctx)[n].data.subform.parent = ctx->cur_container;
|
adamc@760
|
918 ctx->cur_container = &INP(ctx)[n];
|
adamc@756
|
919 return;
|
adamc@756
|
920 default:
|
adamc@756
|
921 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@756
|
922 }
|
adamc@756
|
923 }
|
adamc@756
|
924
|
adamc@756
|
925 void uw_leave_subform(uw_context ctx) {
|
adamc@756
|
926 input *tmp;
|
adamc@756
|
927
|
adamc@759
|
928 if (ctx->cur_container == NULL)
|
adamc@756
|
929 uw_error(ctx, FATAL, "Unmatched uw_leave_subform");
|
adamc@756
|
930
|
adamc@759
|
931 tmp = ctx->cur_container;
|
adamc@759
|
932 ctx->cur_container = tmp->data.subform.parent;
|
adamc@759
|
933 tmp->data.subform.parent = NULL;
|
adamc@759
|
934 }
|
adamc@759
|
935
|
adamc@759
|
936 int uw_enter_subforms(uw_context ctx, int n) {
|
adamc@759
|
937 input *inps;
|
adamc@759
|
938
|
adamc@759
|
939 if (n < 0)
|
adamc@759
|
940 uw_error(ctx, FATAL, "Negative subforms index %d", n);
|
adamc@759
|
941 if (n >= uw_inputs_len)
|
adamc@759
|
942 uw_error(ctx, FATAL, "Out-of-bounds subforms index %d", n);
|
adamc@759
|
943
|
adamc@759
|
944 switch (INP(ctx)[n].kind) {
|
adamc@759
|
945 case UNSET:
|
adamc@759
|
946 uw_error(ctx, FATAL, "Missing subforms");
|
adamc@759
|
947 case FIL:
|
adamc@759
|
948 uw_error(ctx, FATAL, "Tried to read a file form input as subforms");
|
adamc@759
|
949 case NORMAL:
|
adamc@759
|
950 uw_error(ctx, FATAL, "Tried to read a normal form input %p as subforms", &INP(ctx)[n]);
|
adamc@759
|
951 case SUBFORM:
|
adamc@759
|
952 uw_error(ctx, FATAL, "Tried to read a subform form input as subforms");
|
adamc@759
|
953 case ENTRY:
|
adamc@759
|
954 uw_error(ctx, FATAL, "Tried to read an entry form input as subforms");
|
adamc@759
|
955 case SUBFORMS:
|
adamc@759
|
956 inps = INP(ctx)[n].data.subforms.entries;
|
adamc@759
|
957 if (inps) {
|
adamc@759
|
958 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
|
adamc@759
|
959 ctx->cur_container = INP(ctx)[n].data.subforms.entries;
|
adamc@759
|
960 return 1;
|
adamc@759
|
961 } else
|
adamc@759
|
962 return 0;
|
adamc@759
|
963 default:
|
adamc@759
|
964 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@759
|
965 }
|
adamc@759
|
966 }
|
adamc@759
|
967
|
adamc@759
|
968 int uw_next_entry(uw_context ctx) {
|
adamc@759
|
969 if (ctx->cur_container == NULL)
|
adamc@759
|
970 uw_error(ctx, FATAL, "uw_next_entry(NULL)");
|
adamc@759
|
971
|
adamc@759
|
972 switch (ctx->cur_container->kind) {
|
adamc@759
|
973 case UNSET:
|
adamc@759
|
974 uw_error(ctx, FATAL, "Missing entry");
|
adamc@759
|
975 case FIL:
|
adamc@759
|
976 uw_error(ctx, FATAL, "Tried to read a file form input as entry");
|
adamc@759
|
977 case NORMAL:
|
adamc@759
|
978 uw_error(ctx, FATAL, "Tried to read a normal form input as entry");
|
adamc@759
|
979 case SUBFORM:
|
adamc@759
|
980 uw_error(ctx, FATAL, "Tried to read a subform form input as entry");
|
adamc@759
|
981 case SUBFORMS:
|
adamc@759
|
982 uw_error(ctx, FATAL, "Tried to read a subforms form input as entry");
|
adamc@759
|
983 case ENTRY:
|
adamc@759
|
984 if (ctx->cur_container->data.entry.next) {
|
adamc@759
|
985 ctx->cur_container = ctx->cur_container->data.entry.next;
|
adamc@759
|
986 return 1;
|
adamc@759
|
987 } else {
|
adamc@759
|
988 ctx->cur_container = ctx->cur_container->data.entry.parent->data.subforms.parent;
|
adamc@759
|
989 return 0;
|
adamc@759
|
990 }
|
adamc@759
|
991 default:
|
adamc@759
|
992 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@759
|
993 }
|
adamc@756
|
994 }
|
adamc@756
|
995
|
adamc@643
|
996 void uw_set_script_header(uw_context ctx, const char *s) {
|
adamc@643
|
997 ctx->script_header = s;
|
adamc@643
|
998 }
|
adamc@643
|
999
|
adamc@667
|
1000 void uw_set_url_prefix(uw_context ctx, const char *s) {
|
adamc@667
|
1001 ctx->url_prefix = s;
|
adamc@667
|
1002 }
|
adamc@667
|
1003
|
adamc@693
|
1004 void uw_set_needs_push(uw_context ctx, int n) {
|
adamc@693
|
1005 ctx->needs_push = n;
|
adamc@693
|
1006 }
|
adamc@693
|
1007
|
adamc@736
|
1008 void uw_set_needs_sig(uw_context ctx, int n) {
|
adamc@736
|
1009 ctx->needs_sig = n;
|
adamc@736
|
1010 }
|
adamc@736
|
1011
|
adamc@667
|
1012
|
adamc@667
|
1013 static void buf_check_ctx(uw_context ctx, buf *b, size_t extra, const char *desc) {
|
adamc@666
|
1014 if (b->back - b->front < extra) {
|
adamc@666
|
1015 size_t desired = b->front - b->start + extra, next;
|
adamc@136
|
1016 char *new_heap;
|
adamc@136
|
1017
|
adamc@666
|
1018 next = b->back - b->start;
|
adamc@317
|
1019 if (next == 0)
|
adamc@317
|
1020 next = 1;
|
adamc@317
|
1021 for (; next < desired; next *= 2);
|
adamc@136
|
1022
|
adamc@666
|
1023 new_heap = realloc(b->start, next);
|
adamc@666
|
1024 b->front = new_heap + (b->front - b->start);
|
adamc@666
|
1025 b->back = new_heap + next;
|
adamc@136
|
1026
|
adamc@667
|
1027 if (new_heap != b->start) {
|
adamc@666
|
1028 b->start = new_heap;
|
adamc@666
|
1029 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously", desc);
|
adamc@136
|
1030 }
|
adamc@136
|
1031
|
adamc@666
|
1032 b->start = new_heap;
|
adamc@136
|
1033 }
|
adamc@136
|
1034 }
|
adamc@136
|
1035
|
adamc@666
|
1036 static void uw_check_heap(uw_context ctx, size_t extra) {
|
adamc@667
|
1037 buf_check_ctx(ctx, &ctx->heap, extra, "heap chunk");
|
adamc@666
|
1038 }
|
adamc@666
|
1039
|
adamc@311
|
1040 void *uw_malloc(uw_context ctx, size_t len) {
|
adamc@136
|
1041 void *result;
|
adamc@136
|
1042
|
adamc@311
|
1043 uw_check_heap(ctx, len);
|
adamc@136
|
1044
|
adamc@666
|
1045 result = ctx->heap.front;
|
adamc@666
|
1046 ctx->heap.front += len;
|
adamc@136
|
1047 return result;
|
adamc@117
|
1048 }
|
adamc@117
|
1049
|
adamc@323
|
1050 void uw_begin_region(uw_context ctx) {
|
adamc@666
|
1051 regions *r = (regions *) ctx->heap.front;
|
adamc@323
|
1052
|
adamc@323
|
1053 uw_check_heap(ctx, sizeof(regions));
|
adamc@323
|
1054
|
adamc@666
|
1055 ctx->heap.front += sizeof(regions);
|
adamc@323
|
1056
|
adamc@323
|
1057 r->next = ctx->regions;
|
adamc@323
|
1058 ctx->regions = r;
|
adamc@323
|
1059 }
|
adamc@323
|
1060
|
adamc@323
|
1061 void uw_end_region(uw_context ctx) {
|
adamc@323
|
1062 regions *r = ctx->regions;
|
adamc@323
|
1063
|
adamc@323
|
1064 if (r == NULL)
|
adamc@323
|
1065 uw_error(ctx, FATAL, "Region stack underflow");
|
adamc@323
|
1066
|
adamc@666
|
1067 ctx->heap.front = (char *) r;
|
adamc@323
|
1068 ctx->regions = r->next;
|
adamc@323
|
1069 }
|
adamc@323
|
1070
|
adamc@324
|
1071 void uw_memstats(uw_context ctx) {
|
adamc@666
|
1072 printf("Headers: %d/%d\n", buf_used(&ctx->outHeaders), buf_avail(&ctx->outHeaders));
|
adamc@666
|
1073 printf("Script: %d/%d\n", buf_used(&ctx->script), buf_avail(&ctx->script));
|
adamc@666
|
1074 printf("Page: %d/%d\n", buf_used(&ctx->page), buf_avail(&ctx->page));
|
adamc@666
|
1075 printf("Heap: %d/%d\n", buf_used(&ctx->heap), buf_avail(&ctx->heap));
|
adamc@324
|
1076 }
|
adamc@324
|
1077
|
adamc@311
|
1078 int uw_send(uw_context ctx, int sock) {
|
adamc@666
|
1079 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
|
adamc@462
|
1080
|
adamc@462
|
1081 if (n < 0)
|
adamc@462
|
1082 return n;
|
adamc@462
|
1083
|
adamc@462
|
1084 n = uw_really_send(sock, "\r\n", 2);
|
adamc@462
|
1085
|
adamc@462
|
1086 if (n < 0)
|
adamc@462
|
1087 return n;
|
adamc@462
|
1088
|
adamc@666
|
1089 return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start);
|
adamc@462
|
1090 }
|
adamc@462
|
1091
|
adamc@462
|
1092 static void uw_check_headers(uw_context ctx, size_t extra) {
|
adamc@667
|
1093 buf_check(&ctx->outHeaders, extra);
|
adamc@462
|
1094 }
|
adamc@462
|
1095
|
adamc@462
|
1096 void uw_write_header(uw_context ctx, uw_Basis_string s) {
|
adamc@462
|
1097 int len = strlen(s);
|
adamc@462
|
1098
|
adamc@462
|
1099 uw_check_headers(ctx, len + 1);
|
adamc@666
|
1100 strcpy(ctx->outHeaders.front, s);
|
adamc@666
|
1101 ctx->outHeaders.front += len;
|
adamc@562
|
1102 }
|
adamc@562
|
1103
|
adamc@562
|
1104 static void uw_check_script(uw_context ctx, size_t extra) {
|
adamc@667
|
1105 buf_check(&ctx->script, extra);
|
adamc@562
|
1106 }
|
adamc@562
|
1107
|
adamc@562
|
1108 void uw_write_script(uw_context ctx, uw_Basis_string s) {
|
adamc@562
|
1109 int len = strlen(s);
|
adamc@562
|
1110
|
adamc@562
|
1111 uw_check_script(ctx, len + 1);
|
adamc@666
|
1112 strcpy(ctx->script.front, s);
|
adamc@666
|
1113 ctx->script.front += len;
|
adamc@562
|
1114 }
|
adamc@562
|
1115
|
adamc@645
|
1116 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
|
adamc@667
|
1117 if (ctx->script_header[0] == 0)
|
adamc@667
|
1118 return "";
|
adamc@696
|
1119 else if (buf_used(&ctx->script) == 0)
|
adamc@696
|
1120 return ctx->script_header;
|
adamc@667
|
1121 else {
|
adamc@695
|
1122 char *r = uw_malloc(ctx, strlen(ctx->script_header) + 42 + buf_used(&ctx->script));
|
adamc@695
|
1123 sprintf(r, "%s<script type=\"text/javascript\">%s</script>",
|
adamc@673
|
1124 ctx->script_header,
|
adamc@673
|
1125 ctx->script.start);
|
adamc@565
|
1126 return r;
|
adamc@565
|
1127 }
|
adamc@565
|
1128 }
|
adamc@565
|
1129
|
adamc@695
|
1130 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
|
adamc@695
|
1131 if (s[0] == 0)
|
adamc@695
|
1132 return "";
|
adamc@695
|
1133 else {
|
adamc@695
|
1134 char *r = uw_malloc(ctx, 11 + strlen(s));
|
adamc@695
|
1135 sprintf(r, " onload='%s'", s);
|
adamc@695
|
1136 return r;
|
adamc@695
|
1137 }
|
adamc@695
|
1138 }
|
adamc@695
|
1139
|
adamc@736
|
1140 extern uw_Basis_string uw_cookie_sig(uw_context);
|
adamc@736
|
1141
|
adamc@694
|
1142 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
|
adamc@736
|
1143 if (ctx->client == NULL) {
|
adamc@736
|
1144 if (ctx->needs_sig) {
|
adamc@736
|
1145 char *sig = uw_cookie_sig(ctx);
|
adamc@736
|
1146 char *r = uw_malloc(ctx, strlen(sig) + 8);
|
adamc@736
|
1147 sprintf(r, "sig=\"%s\";", sig);
|
adamc@736
|
1148 return r;
|
adamc@736
|
1149 }
|
adamc@736
|
1150 else
|
adamc@736
|
1151 return "";
|
adamc@736
|
1152 } else {
|
adamc@736
|
1153 char *sig = ctx->needs_sig ? uw_cookie_sig(ctx) : "";
|
adamc@736
|
1154 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->url_prefix)
|
adamc@736
|
1155 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
|
adamc@736
|
1156 sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
|
adamc@682
|
1157 ctx->client->id,
|
adamc@682
|
1158 ctx->client->pass,
|
adamc@679
|
1159 ctx->url_prefix,
|
adamc@736
|
1160 ctx->timeout,
|
adamc@736
|
1161 ctx->needs_sig ? "sig=\"" : "",
|
adamc@736
|
1162 sig,
|
adamc@736
|
1163 ctx->needs_sig ? "\";" : "");
|
adamc@679
|
1164 return r;
|
adamc@668
|
1165 }
|
adamc@667
|
1166 }
|
adamc@667
|
1167
|
adamc@574
|
1168 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
1169 char *r, *s2;
|
adamc@574
|
1170
|
adamc@574
|
1171 uw_check_heap(ctx, strlen(s) * 4 + 2);
|
adamc@574
|
1172
|
adamc@666
|
1173 r = s2 = ctx->heap.front;
|
adamc@574
|
1174 *s2++ = '"';
|
adamc@574
|
1175
|
adamc@574
|
1176 for (; *s; s++) {
|
adamc@574
|
1177 char c = *s;
|
adamc@574
|
1178
|
adamc@574
|
1179 switch (c) {
|
adamc@574
|
1180 case '"':
|
adamc@574
|
1181 strcpy(s2, "\\\"");
|
adamc@574
|
1182 s2 += 2;
|
adamc@574
|
1183 break;
|
adamc@574
|
1184 case '\\':
|
adamc@574
|
1185 strcpy(s2, "\\\\");
|
adamc@574
|
1186 s2 += 2;
|
adamc@574
|
1187 break;
|
adamc@574
|
1188 default:
|
adamc@574
|
1189 if (isprint(c))
|
adamc@574
|
1190 *s2++ = c;
|
adamc@574
|
1191 else {
|
adamc@574
|
1192 sprintf(s2, "\\%3o", c);
|
adamc@574
|
1193 s2 += 4;
|
adamc@574
|
1194 }
|
adamc@574
|
1195 }
|
adamc@574
|
1196 }
|
adamc@574
|
1197
|
adamc@574
|
1198 strcpy(s2, "\"");
|
adamc@666
|
1199 ctx->heap.front = s2 + 1;
|
adamc@574
|
1200 return r;
|
adamc@574
|
1201 }
|
adamc@574
|
1202
|
adamc@574
|
1203 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
1204 char *r, *s2;
|
adamc@574
|
1205
|
adamc@574
|
1206 uw_check_script(ctx, strlen(s) * 4 + 2);
|
adamc@574
|
1207
|
adamc@666
|
1208 r = s2 = ctx->script.front;
|
adamc@574
|
1209 *s2++ = '"';
|
adamc@574
|
1210
|
adamc@574
|
1211 for (; *s; s++) {
|
adamc@574
|
1212 char c = *s;
|
adamc@574
|
1213
|
adamc@574
|
1214 switch (c) {
|
adamc@577
|
1215 case '\'':
|
adamc@574
|
1216 strcpy(s2, "\\\"");
|
adamc@574
|
1217 s2 += 2;
|
adamc@574
|
1218 break;
|
adamc@574
|
1219 case '\\':
|
adamc@574
|
1220 strcpy(s2, "\\\\");
|
adamc@574
|
1221 s2 += 2;
|
adamc@574
|
1222 break;
|
adamc@574
|
1223 default:
|
adamc@574
|
1224 if (isprint(c))
|
adamc@574
|
1225 *s2++ = c;
|
adamc@574
|
1226 else {
|
adamc@574
|
1227 sprintf(s2, "\\%3o", c);
|
adamc@574
|
1228 s2 += 4;
|
adamc@574
|
1229 }
|
adamc@574
|
1230 }
|
adamc@574
|
1231 }
|
adamc@574
|
1232
|
adamc@574
|
1233 strcpy(s2, "\"");
|
adamc@666
|
1234 ctx->script.front = s2 + 1;
|
adamc@574
|
1235 return r;
|
adamc@574
|
1236 }
|
adamc@574
|
1237
|
adamc@682
|
1238 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1239 if (ctx->client == NULL || chn.cli != ctx->client->id)
|
adamc@682
|
1240 return "null";
|
adamc@682
|
1241 else {
|
adamc@682
|
1242 int len;
|
adamc@682
|
1243 char *r;
|
adamc@682
|
1244
|
adamc@682
|
1245 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
1246 r = ctx->heap.front;
|
adamc@682
|
1247 sprintf(r, "%u%n", chn.chn, &len);
|
adamc@682
|
1248 ctx->heap.front += len+1;
|
adamc@682
|
1249 return r;
|
adamc@682
|
1250 }
|
adamc@682
|
1251 }
|
adamc@682
|
1252
|
adamc@577
|
1253 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
|
adamc@577
|
1254 int len;
|
adamc@577
|
1255 size_t s_len = strlen(s);
|
adamc@562
|
1256
|
adamc@577
|
1257 uw_check_script(ctx, 12 + INTS_MAX + s_len);
|
adamc@666
|
1258 sprintf(ctx->script.front, "var s%d=sc(%n", ctx->source_count, &len);
|
adamc@666
|
1259 ctx->script.front += len;
|
adamc@666
|
1260 strcpy(ctx->script.front, s);
|
adamc@666
|
1261 ctx->script.front += s_len;
|
adamc@666
|
1262 strcpy(ctx->script.front, ");");
|
adamc@666
|
1263 ctx->script.front += 2;
|
adamc@562
|
1264
|
adamc@565
|
1265 return ctx->source_count++;
|
adamc@117
|
1266 }
|
adamc@117
|
1267
|
adamc@577
|
1268 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
|
adamc@577
|
1269 int len;
|
adamc@577
|
1270 size_t s_len = strlen(s);
|
adamc@577
|
1271
|
adamc@577
|
1272 uw_check_script(ctx, 6 + INTS_MAX + s_len);
|
adamc@666
|
1273 sprintf(ctx->script.front, "s%d.v=%n", (int)n, &len);
|
adamc@666
|
1274 ctx->script.front += len;
|
adamc@666
|
1275 strcpy(ctx->script.front, s);
|
adamc@666
|
1276 ctx->script.front += s_len;
|
adamc@666
|
1277 strcpy(ctx->script.front, ";");
|
adamc@666
|
1278 ctx->script.front++;
|
adamc@577
|
1279
|
adamc@577
|
1280 return uw_unit_v;
|
adamc@577
|
1281 }
|
adamc@577
|
1282
|
adamc@311
|
1283 static void uw_check(uw_context ctx, size_t extra) {
|
adamc@667
|
1284 buf_check(&ctx->page, extra);
|
adamc@117
|
1285 }
|
adamc@117
|
1286
|
adamc@311
|
1287 static void uw_writec_unsafe(uw_context ctx, char c) {
|
adamc@666
|
1288 *(ctx->page.front)++ = c;
|
adamc@117
|
1289 }
|
adamc@117
|
1290
|
adamc@311
|
1291 void uw_writec(uw_context ctx, char c) {
|
adamc@311
|
1292 uw_check(ctx, 1);
|
adamc@311
|
1293 uw_writec_unsafe(ctx, c);
|
adamc@117
|
1294 }
|
adamc@117
|
1295
|
adamc@311
|
1296 static void uw_write_unsafe(uw_context ctx, const char* s) {
|
adamc@117
|
1297 int len = strlen(s);
|
adamc@666
|
1298 memcpy(ctx->page.front, s, len);
|
adamc@666
|
1299 ctx->page.front += len;
|
adamc@117
|
1300 }
|
adamc@117
|
1301
|
adamc@311
|
1302 void uw_write(uw_context ctx, const char* s) {
|
adamc@311
|
1303 uw_check(ctx, strlen(s) + 1);
|
adamc@311
|
1304 uw_write_unsafe(ctx, s);
|
adamc@666
|
1305 *ctx->page.front = 0;
|
adamc@102
|
1306 }
|
adamc@106
|
1307
|
adamc@311
|
1308 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@136
|
1309 char *result;
|
adamc@136
|
1310 int len;
|
adamc@311
|
1311 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1312 result = ctx->heap.front;
|
adamc@276
|
1313 sprintf(result, "%lld%n", n, &len);
|
adamc@666
|
1314 ctx->heap.front += len+1;
|
adamc@136
|
1315 return result;
|
adamc@106
|
1316 }
|
adamc@106
|
1317
|
adamc@311
|
1318 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@136
|
1319 char *result;
|
adamc@136
|
1320 int len;
|
adamc@311
|
1321 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1322 result = ctx->heap.front;
|
adamc@136
|
1323 sprintf(result, "%g%n", n, &len);
|
adamc@666
|
1324 ctx->heap.front += len+1;
|
adamc@136
|
1325 return result;
|
adamc@106
|
1326 }
|
adamc@106
|
1327
|
adamc@311
|
1328 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@136
|
1329 int len = strlen(s);
|
adamc@136
|
1330 char *result, *p;
|
adamc@311
|
1331 uw_check_heap(ctx, len * 6 + 1);
|
adamc@136
|
1332
|
adamc@666
|
1333 result = p = ctx->heap.front;
|
adamc@136
|
1334
|
adamc@136
|
1335 for (; *s; s++) {
|
adamc@136
|
1336 char c = *s;
|
adamc@136
|
1337
|
adamc@136
|
1338 if (c == '"') {
|
adamc@136
|
1339 strcpy(p, """);
|
adamc@136
|
1340 p += 6;
|
adamc@136
|
1341 } else if (c == '&') {
|
adamc@136
|
1342 strcpy(p, "&");
|
adamc@136
|
1343 p += 5;
|
adamc@136
|
1344 }
|
adamc@136
|
1345 else if (isprint(c))
|
adamc@136
|
1346 *p++ = c;
|
adamc@136
|
1347 else {
|
adamc@136
|
1348 int len2;
|
adamc@136
|
1349 sprintf(p, "&#%d;%n", c, &len2);
|
adamc@136
|
1350 p += len2;
|
adamc@136
|
1351 }
|
adamc@136
|
1352 }
|
adamc@136
|
1353
|
adamc@137
|
1354 *p++ = 0;
|
adamc@666
|
1355 ctx->heap.front = p;
|
adamc@136
|
1356 return result;
|
adamc@106
|
1357 }
|
adamc@106
|
1358
|
adamc@721
|
1359 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
|
adamc@721
|
1360 return s;
|
adamc@721
|
1361 }
|
adamc@721
|
1362
|
adamc@311
|
1363 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@117
|
1364 int len;
|
adamc@117
|
1365
|
adamc@666
|
1366 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1367 ctx->page.front += len;
|
adamc@106
|
1368 }
|
adamc@106
|
1369
|
adamc@428
|
1370 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
1371 uw_check(ctx, INTS_MAX);
|
adamc@311
|
1372 uw_Basis_attrifyInt_w_unsafe(ctx, n);
|
adamc@428
|
1373
|
adamc@428
|
1374 return uw_unit_v;
|
adamc@106
|
1375 }
|
adamc@106
|
1376
|
adamc@428
|
1377 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@117
|
1378 int len;
|
adamc@117
|
1379
|
adamc@311
|
1380 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1381 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1382 ctx->page.front += len;
|
adamc@428
|
1383
|
adamc@428
|
1384 return uw_unit_v;
|
adamc@117
|
1385 }
|
adamc@117
|
1386
|
adamc@428
|
1387 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1388 uw_check(ctx, strlen(s) * 6);
|
adamc@117
|
1389
|
adamc@106
|
1390 for (; *s; s++) {
|
adamc@106
|
1391 char c = *s;
|
adamc@106
|
1392
|
adamc@106
|
1393 if (c == '"')
|
adamc@311
|
1394 uw_write_unsafe(ctx, """);
|
adamc@136
|
1395 else if (c == '&')
|
adamc@311
|
1396 uw_write_unsafe(ctx, "&");
|
adamc@106
|
1397 else if (isprint(c))
|
adamc@311
|
1398 uw_writec_unsafe(ctx, c);
|
adamc@106
|
1399 else {
|
adamc@311
|
1400 uw_write_unsafe(ctx, "&#");
|
adamc@311
|
1401 uw_Basis_attrifyInt_w_unsafe(ctx, c);
|
adamc@311
|
1402 uw_writec_unsafe(ctx, ';');
|
adamc@106
|
1403 }
|
adamc@106
|
1404 }
|
adamc@428
|
1405
|
adamc@428
|
1406 return uw_unit_v;
|
adamc@106
|
1407 }
|
adamc@120
|
1408
|
adamc@120
|
1409
|
adamc@311
|
1410 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@137
|
1411 int len;
|
adamc@137
|
1412 char *r;
|
adamc@137
|
1413
|
adamc@311
|
1414 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1415 r = ctx->heap.front;
|
adamc@276
|
1416 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1417 ctx->heap.front += len+1;
|
adamc@137
|
1418 return r;
|
adamc@120
|
1419 }
|
adamc@120
|
1420
|
adamc@682
|
1421 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1422 if (ctx->client == NULL || chn.cli != ctx->client->id)
|
adamc@682
|
1423 return "";
|
adamc@682
|
1424 else {
|
adamc@682
|
1425 int len;
|
adamc@682
|
1426 char *r;
|
adamc@679
|
1427
|
adamc@682
|
1428 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
1429 r = ctx->heap.front;
|
adamc@682
|
1430 sprintf(r, "%u%n", chn.chn, &len);
|
adamc@682
|
1431 ctx->heap.front += len+1;
|
adamc@682
|
1432 return r;
|
adamc@682
|
1433 }
|
adamc@679
|
1434 }
|
adamc@679
|
1435
|
adamc@311
|
1436 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@137
|
1437 int len;
|
adamc@137
|
1438 char *r;
|
adamc@137
|
1439
|
adamc@311
|
1440 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1441 r = ctx->heap.front;
|
adamc@137
|
1442 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1443 ctx->heap.front += len+1;
|
adamc@137
|
1444 return r;
|
adamc@120
|
1445 }
|
adamc@120
|
1446
|
adamc@311
|
1447 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
1448 char *r, *p;
|
adamc@137
|
1449
|
adamc@311
|
1450 uw_check_heap(ctx, strlen(s) * 3 + 1);
|
adamc@137
|
1451
|
adamc@666
|
1452 for (r = p = ctx->heap.front; *s; s++) {
|
adamc@137
|
1453 char c = *s;
|
adamc@137
|
1454
|
adamc@137
|
1455 if (c == ' ')
|
adamc@137
|
1456 *p++ = '+';
|
adamc@137
|
1457 else if (isalnum(c))
|
adamc@137
|
1458 *p++ = c;
|
adamc@137
|
1459 else {
|
adamc@137
|
1460 sprintf(p, "%%%02X", c);
|
adamc@137
|
1461 p += 3;
|
adamc@137
|
1462 }
|
adamc@137
|
1463 }
|
adamc@137
|
1464
|
adamc@137
|
1465 *p++ = 0;
|
adamc@666
|
1466 ctx->heap.front = p;
|
adamc@137
|
1467 return r;
|
adamc@120
|
1468 }
|
adamc@120
|
1469
|
adamc@311
|
1470 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1471 if (b == uw_Basis_False)
|
adamc@186
|
1472 return "0";
|
adamc@186
|
1473 else
|
adamc@186
|
1474 return "1";
|
adamc@186
|
1475 }
|
adamc@186
|
1476
|
adamc@311
|
1477 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@120
|
1478 int len;
|
adamc@120
|
1479
|
adamc@666
|
1480 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1481 ctx->page.front += len;
|
adamc@120
|
1482 }
|
adamc@120
|
1483
|
adamc@428
|
1484 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
1485 uw_check(ctx, INTS_MAX);
|
adamc@311
|
1486 uw_Basis_urlifyInt_w_unsafe(ctx, n);
|
adamc@428
|
1487
|
adamc@428
|
1488 return uw_unit_v;
|
adamc@120
|
1489 }
|
adamc@120
|
1490
|
adamc@682
|
1491 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1492 if (ctx->client != NULL && chn.cli == ctx->client->id) {
|
adamc@682
|
1493 int len;
|
adamc@682
|
1494
|
adamc@682
|
1495 uw_check(ctx, INTS_MAX + 1);
|
adamc@682
|
1496 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
|
adamc@682
|
1497 ctx->page.front += len;
|
adamc@682
|
1498 }
|
adamc@682
|
1499
|
adamc@679
|
1500 return uw_unit_v;
|
adamc@679
|
1501 }
|
adamc@679
|
1502
|
adamc@428
|
1503 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@120
|
1504 int len;
|
adamc@120
|
1505
|
adamc@311
|
1506 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1507 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1508 ctx->page.front += len;
|
adamc@428
|
1509
|
adamc@428
|
1510 return uw_unit_v;
|
adamc@120
|
1511 }
|
adamc@120
|
1512
|
adamc@488
|
1513 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@488
|
1514 return uw_Basis_urlifyInt(ctx, t);
|
adamc@488
|
1515 }
|
adamc@488
|
1516
|
adamc@428
|
1517 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1518 uw_check(ctx, strlen(s) * 3);
|
adamc@120
|
1519
|
adamc@120
|
1520 for (; *s; s++) {
|
adamc@120
|
1521 char c = *s;
|
adamc@120
|
1522
|
adamc@120
|
1523 if (c == ' ')
|
adamc@311
|
1524 uw_writec_unsafe(ctx, '+');
|
adamc@120
|
1525 else if (isalnum(c))
|
adamc@311
|
1526 uw_writec_unsafe(ctx, c);
|
adamc@120
|
1527 else {
|
adamc@666
|
1528 sprintf(ctx->page.front, "%%%02X", c);
|
adamc@666
|
1529 ctx->page.front += 3;
|
adamc@120
|
1530 }
|
adamc@120
|
1531 }
|
adamc@428
|
1532
|
adamc@428
|
1533 return uw_unit_v;
|
adamc@120
|
1534 }
|
adamc@120
|
1535
|
adamc@428
|
1536 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1537 if (b == uw_Basis_False)
|
adamc@311
|
1538 uw_writec(ctx, '0');
|
adamc@186
|
1539 else
|
adamc@311
|
1540 uw_writec(ctx, '1');
|
adamc@428
|
1541
|
adamc@428
|
1542 return uw_unit_v;
|
adamc@186
|
1543 }
|
adamc@186
|
1544
|
adamc@120
|
1545
|
adamc@311
|
1546 static char *uw_unurlify_advance(char *s) {
|
adamc@144
|
1547 char *new_s = strchr(s, '/');
|
adamc@120
|
1548
|
adamc@120
|
1549 if (new_s)
|
adamc@120
|
1550 *new_s++ = 0;
|
adamc@120
|
1551 else
|
adamc@144
|
1552 new_s = strchr(s, 0);
|
adamc@144
|
1553
|
adamc@144
|
1554 return new_s;
|
adamc@144
|
1555 }
|
adamc@144
|
1556
|
adamc@311
|
1557 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
|
adamc@311
|
1558 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1559 uw_Basis_int r;
|
adamc@120
|
1560
|
adamc@276
|
1561 r = atoll(*s);
|
adamc@120
|
1562 *s = new_s;
|
adamc@120
|
1563 return r;
|
adamc@120
|
1564 }
|
adamc@120
|
1565
|
adamc@311
|
1566 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
|
adamc@311
|
1567 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1568 uw_Basis_float r;
|
adamc@120
|
1569
|
adamc@120
|
1570 r = atof(*s);
|
adamc@120
|
1571 *s = new_s;
|
adamc@120
|
1572 return r;
|
adamc@120
|
1573 }
|
adamc@120
|
1574
|
adamc@488
|
1575 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
|
adamc@488
|
1576 return uw_Basis_unurlifyInt(ctx, s);
|
adamc@488
|
1577 }
|
adamc@488
|
1578
|
adamc@311
|
1579 static uw_Basis_string uw_unurlifyString_to(uw_context ctx, char *r, char *s) {
|
adamc@144
|
1580 char *s1, *s2;
|
adamc@144
|
1581 int n;
|
adamc@136
|
1582
|
adamc@144
|
1583 for (s1 = r, s2 = s; *s2; ++s1, ++s2) {
|
adamc@136
|
1584 char c = *s2;
|
adamc@136
|
1585
|
adamc@136
|
1586 switch (c) {
|
adamc@136
|
1587 case '+':
|
adamc@136
|
1588 *s1 = ' ';
|
adamc@136
|
1589 break;
|
adamc@136
|
1590 case '%':
|
adamc@169
|
1591 if (s2[1] == 0)
|
adamc@311
|
1592 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
|
adamc@169
|
1593 if (s2[2] == 0)
|
adamc@311
|
1594 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
|
adamc@169
|
1595 if (sscanf(s2+1, "%02X", &n) != 1)
|
adamc@311
|
1596 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
|
adamc@136
|
1597 *s1 = n;
|
adamc@136
|
1598 s2 += 2;
|
adamc@136
|
1599 break;
|
adamc@136
|
1600 default:
|
adamc@136
|
1601 *s1 = c;
|
adamc@136
|
1602 }
|
adamc@136
|
1603 }
|
adamc@136
|
1604 *s1++ = 0;
|
adamc@144
|
1605 return s1;
|
adamc@144
|
1606 }
|
adamc@144
|
1607
|
adamc@311
|
1608 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
|
adamc@311
|
1609 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1610 uw_Basis_bool r;
|
adamc@186
|
1611
|
adamc@186
|
1612 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
|
adamc@311
|
1613 r = uw_Basis_False;
|
adamc@186
|
1614 else
|
adamc@311
|
1615 r = uw_Basis_True;
|
adamc@186
|
1616
|
adamc@186
|
1617 *s = new_s;
|
adamc@186
|
1618 return r;
|
adamc@186
|
1619 }
|
adamc@186
|
1620
|
adamc@311
|
1621 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
|
adamc@311
|
1622 char *new_s = uw_unurlify_advance(*s);
|
adamc@144
|
1623 char *r, *s1, *s2;
|
adamc@144
|
1624 int len, n;
|
adamc@144
|
1625
|
adamc@200
|
1626 len = strlen(*s);
|
adamc@311
|
1627 uw_check_heap(ctx, len + 1);
|
adamc@144
|
1628
|
adamc@666
|
1629 r = ctx->heap.front;
|
adamc@666
|
1630 ctx->heap.front = uw_unurlifyString_to(ctx, ctx->heap.front, *s);
|
adamc@136
|
1631 *s = new_s;
|
adamc@136
|
1632 return r;
|
adamc@120
|
1633 }
|
adamc@135
|
1634
|
adamc@135
|
1635
|
adamc@311
|
1636 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
1637 int len;
|
adamc@286
|
1638 char *r;
|
adamc@286
|
1639
|
adamc@311
|
1640 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1641 r = ctx->heap.front;
|
adamc@286
|
1642 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1643 ctx->heap.front += len+1;
|
adamc@286
|
1644 return r;
|
adamc@286
|
1645 }
|
adamc@286
|
1646
|
adamc@428
|
1647 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
1648 int len;
|
adamc@286
|
1649
|
adamc@311
|
1650 uw_check(ctx, INTS_MAX);
|
adamc@666
|
1651 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1652 ctx->page.front += len;
|
adamc@428
|
1653
|
adamc@428
|
1654 return uw_unit_v;
|
adamc@286
|
1655 }
|
adamc@286
|
1656
|
adamc@311
|
1657 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
1658 int len;
|
adamc@286
|
1659 char *r;
|
adamc@286
|
1660
|
adamc@311
|
1661 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1662 r = ctx->heap.front;
|
adamc@286
|
1663 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1664 ctx->heap.front += len+1;
|
adamc@286
|
1665 return r;
|
adamc@286
|
1666 }
|
adamc@286
|
1667
|
adamc@428
|
1668 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
1669 int len;
|
adamc@286
|
1670
|
adamc@311
|
1671 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1672 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1673 ctx->page.front += len;
|
adamc@428
|
1674
|
adamc@428
|
1675 return uw_unit_v;
|
adamc@286
|
1676 }
|
adamc@286
|
1677
|
adamc@311
|
1678 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
1679 char *r, *s2;
|
adamc@137
|
1680
|
adamc@311
|
1681 uw_check_heap(ctx, strlen(s) * 5 + 1);
|
adamc@137
|
1682
|
adamc@666
|
1683 for (r = s2 = ctx->heap.front; *s; s++) {
|
adamc@137
|
1684 char c = *s;
|
adamc@137
|
1685
|
adamc@137
|
1686 switch (c) {
|
adamc@137
|
1687 case '<':
|
adamc@137
|
1688 strcpy(s2, "<");
|
adamc@137
|
1689 s2 += 4;
|
adamc@137
|
1690 break;
|
adamc@137
|
1691 case '&':
|
adamc@137
|
1692 strcpy(s2, "&");
|
adamc@137
|
1693 s2 += 5;
|
adamc@137
|
1694 break;
|
adamc@137
|
1695 default:
|
adamc@137
|
1696 if (isprint(c))
|
adamc@137
|
1697 *s2++ = c;
|
adamc@137
|
1698 else {
|
adamc@137
|
1699 int len2;
|
adamc@137
|
1700 sprintf(s2, "&#%d;%n", c, &len2);
|
adamc@137
|
1701 s2 += len2;
|
adamc@137
|
1702 }
|
adamc@137
|
1703 }
|
adamc@137
|
1704 }
|
adamc@137
|
1705
|
adamc@137
|
1706 *s2++ = 0;
|
adamc@666
|
1707 ctx->heap.front = s2;
|
adamc@137
|
1708 return r;
|
adamc@135
|
1709 }
|
adamc@135
|
1710
|
adamc@428
|
1711 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@321
|
1712 uw_check(ctx, strlen(s) * 6);
|
adamc@135
|
1713
|
adamc@135
|
1714 for (; *s; s++) {
|
adamc@135
|
1715 char c = *s;
|
adamc@135
|
1716
|
adamc@135
|
1717 switch (c) {
|
adamc@135
|
1718 case '<':
|
adamc@311
|
1719 uw_write_unsafe(ctx, "<");
|
adamc@135
|
1720 break;
|
adamc@135
|
1721 case '&':
|
adamc@311
|
1722 uw_write_unsafe(ctx, "&");
|
adamc@135
|
1723 break;
|
adamc@135
|
1724 default:
|
adamc@135
|
1725 if (isprint(c))
|
adamc@311
|
1726 uw_writec_unsafe(ctx, c);
|
adamc@135
|
1727 else {
|
adamc@311
|
1728 uw_write_unsafe(ctx, "&#");
|
adamc@311
|
1729 uw_Basis_attrifyInt_w_unsafe(ctx, c);
|
adamc@311
|
1730 uw_writec_unsafe(ctx, ';');
|
adamc@135
|
1731 }
|
adamc@135
|
1732 }
|
adamc@135
|
1733 }
|
adamc@428
|
1734
|
adamc@428
|
1735 return uw_unit_v;
|
adamc@135
|
1736 }
|
adamc@180
|
1737
|
adamc@311
|
1738 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1739 if (b == uw_Basis_False)
|
adamc@286
|
1740 return "False";
|
adamc@286
|
1741 else
|
adamc@286
|
1742 return "True";
|
adamc@286
|
1743 }
|
adamc@286
|
1744
|
adamc@428
|
1745 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1746 if (b == uw_Basis_False) {
|
adamc@311
|
1747 uw_check(ctx, 6);
|
adamc@666
|
1748 strcpy(ctx->page.front, "False");
|
adamc@666
|
1749 ctx->page.front += 5;
|
adamc@286
|
1750 } else {
|
adamc@311
|
1751 uw_check(ctx, 5);
|
adamc@666
|
1752 strcpy(ctx->page.front, "True");
|
adamc@666
|
1753 ctx->page.front += 4;
|
adamc@286
|
1754 }
|
adamc@428
|
1755
|
adamc@428
|
1756 return uw_unit_v;
|
adamc@286
|
1757 }
|
adamc@286
|
1758
|
adamc@436
|
1759 #define TIME_FMT "%x %X"
|
adamc@438
|
1760 #define TIME_FMT_PG "%Y-%m-%d %T"
|
adamc@436
|
1761
|
adamc@436
|
1762 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
1763 size_t len;
|
adamc@436
|
1764 char *r;
|
adamc@436
|
1765 struct tm stm;
|
adamc@436
|
1766
|
adamc@436
|
1767 if (localtime_r(&t, &stm)) {
|
adamc@436
|
1768 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
1769 r = ctx->heap.front;
|
adamc@436
|
1770 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1771 ctx->heap.front += len+1;
|
adamc@436
|
1772 return r;
|
adamc@436
|
1773 } else
|
adamc@436
|
1774 return "<i>Invalid time</i>";
|
adamc@436
|
1775 }
|
adamc@436
|
1776
|
adamc@436
|
1777 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
1778 size_t len;
|
adamc@436
|
1779 char *r;
|
adamc@436
|
1780 struct tm stm;
|
adamc@436
|
1781
|
adamc@436
|
1782 if (localtime_r(&t, &stm)) {
|
adamc@436
|
1783 uw_check(ctx, TIMES_MAX);
|
adamc@666
|
1784 r = ctx->page.front;
|
adamc@436
|
1785 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1786 ctx->page.front += len;
|
adamc@436
|
1787 } else {
|
adamc@436
|
1788 uw_check(ctx, 20);
|
adamc@666
|
1789 strcpy(ctx->page.front, "<i>Invalid time</i>");
|
adamc@666
|
1790 ctx->page.front += 19;
|
adamc@436
|
1791 }
|
adamc@436
|
1792
|
adamc@436
|
1793 return uw_unit_v;
|
adamc@436
|
1794 }
|
adamc@436
|
1795
|
adamc@821
|
1796 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
|
adamc@821
|
1797 if (n >= 0 && n < strlen(s))
|
adamc@821
|
1798 return s[n];
|
adamc@821
|
1799 else
|
adamc@821
|
1800 uw_error(ctx, FATAL, "Out-of-bounds strsub");
|
adamc@821
|
1801 }
|
adamc@821
|
1802
|
adamc@821
|
1803 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
|
adamc@821
|
1804 if (n >= 0 && n < strlen(s))
|
adamc@821
|
1805 return &s[n];
|
adamc@821
|
1806 else
|
adamc@821
|
1807 uw_error(ctx, FATAL, "Out-of-bounds strsuffix");
|
adamc@821
|
1808 }
|
adamc@821
|
1809
|
adamc@828
|
1810 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
|
adamc@828
|
1811 return strlen(s);
|
adamc@828
|
1812 }
|
adamc@828
|
1813
|
adamc@829
|
1814 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
|
adamc@829
|
1815 return strchr(s, ch);
|
adamc@829
|
1816 }
|
adamc@829
|
1817
|
adamc@831
|
1818 uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) {
|
adamc@831
|
1819 return strcspn(s, chs);
|
adamc@831
|
1820 }
|
adamc@831
|
1821
|
adamc@829
|
1822 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
|
adamc@829
|
1823 uw_Basis_string r = strchr(s, ch);
|
adamc@829
|
1824 if (r == NULL)
|
adamc@829
|
1825 return NULL;
|
adamc@829
|
1826 else {
|
adamc@829
|
1827 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adamc@829
|
1828 *nr = r - s;
|
adamc@829
|
1829 return nr;
|
adamc@829
|
1830 }
|
adamc@829
|
1831 }
|
adamc@829
|
1832
|
adamc@311
|
1833 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
|
adamc@828
|
1834 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
|
adamc@180
|
1835 char *s;
|
adamc@180
|
1836
|
adamc@311
|
1837 uw_check_heap(ctx, len);
|
adamc@180
|
1838
|
adamc@666
|
1839 s = ctx->heap.front;
|
adamc@180
|
1840
|
adamc@180
|
1841 strcpy(s, s1);
|
adamc@180
|
1842 strcat(s, s2);
|
adamc@666
|
1843 ctx->heap.front += len;
|
adamc@180
|
1844
|
adamc@180
|
1845 return s;
|
adamc@180
|
1846 }
|
adamc@278
|
1847
|
adamc@829
|
1848 uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) {
|
adamc@829
|
1849 size_t full_len = uw_Basis_strlen(ctx, s);
|
adamc@829
|
1850
|
adamc@829
|
1851 if (start < 0)
|
adamc@829
|
1852 uw_error(ctx, FATAL, "substring: Negative start index");
|
adamc@829
|
1853 if (len < 0)
|
adamc@829
|
1854 uw_error(ctx, FATAL, "substring: Negative length");
|
adamc@829
|
1855 if (start + len > full_len)
|
adamc@829
|
1856 uw_error(ctx, FATAL, "substring: Start index plus length is too large");
|
adamc@829
|
1857
|
adamc@829
|
1858 if (start + len == full_len)
|
adamc@829
|
1859 return &s[start];
|
adamc@829
|
1860 else {
|
adamc@829
|
1861 uw_Basis_string r = uw_malloc(ctx, len+1);
|
adamc@829
|
1862 memcpy(r, s, len);
|
adamc@829
|
1863 r[len] = 0;
|
adamc@829
|
1864 return r;
|
adamc@829
|
1865 }
|
adamc@829
|
1866
|
adamc@829
|
1867 }
|
adamc@829
|
1868
|
adamc@737
|
1869 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@828
|
1870 int len = uw_Basis_strlen(ctx, s1) + 1;
|
adamc@278
|
1871 char *s;
|
adamc@278
|
1872
|
adamc@311
|
1873 uw_check_heap(ctx, len);
|
adamc@278
|
1874
|
adamc@666
|
1875 s = ctx->heap.front;
|
adamc@278
|
1876
|
adamc@278
|
1877 strcpy(s, s1);
|
adamc@666
|
1878 ctx->heap.front += len;
|
adamc@278
|
1879
|
adamc@278
|
1880 return s;
|
adamc@278
|
1881 }
|
adamc@280
|
1882
|
adamc@737
|
1883 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@493
|
1884 if (s1)
|
adamc@737
|
1885 return uw_strdup(ctx, s1);
|
adamc@493
|
1886 else
|
adamc@493
|
1887 return NULL;
|
adamc@493
|
1888 }
|
adamc@493
|
1889
|
adamc@742
|
1890 char *uw_memdup(uw_context ctx, const char *p, size_t len) {
|
adamc@742
|
1891 char *r = uw_malloc(ctx, len);
|
adamc@742
|
1892 memcpy(r, p, len);
|
adamc@742
|
1893 return r;
|
adamc@742
|
1894 }
|
adamc@280
|
1895
|
adamc@311
|
1896 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@281
|
1897 int len;
|
adamc@281
|
1898 char *r;
|
adamc@281
|
1899
|
adamc@311
|
1900 uw_check_heap(ctx, INTS_MAX + 6);
|
adamc@666
|
1901 r = ctx->heap.front;
|
adamc@281
|
1902 sprintf(r, "%lld::int8%n", n, &len);
|
adamc@666
|
1903 ctx->heap.front += len+1;
|
adamc@281
|
1904 return r;
|
adamc@281
|
1905 }
|
adamc@281
|
1906
|
adamc@467
|
1907 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
|
adamc@467
|
1908 if (n == NULL)
|
adamc@467
|
1909 return "NULL";
|
adamc@467
|
1910 else
|
adamc@467
|
1911 return uw_Basis_sqlifyInt(ctx, *n);
|
adamc@467
|
1912 }
|
adamc@467
|
1913
|
adamc@311
|
1914 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@281
|
1915 int len;
|
adamc@281
|
1916 char *r;
|
adamc@281
|
1917
|
adamc@311
|
1918 uw_check_heap(ctx, FLOATS_MAX + 8);
|
adamc@666
|
1919 r = ctx->heap.front;
|
adamc@281
|
1920 sprintf(r, "%g::float8%n", n, &len);
|
adamc@666
|
1921 ctx->heap.front += len+1;
|
adamc@281
|
1922 return r;
|
adamc@281
|
1923 }
|
adamc@281
|
1924
|
adamc@467
|
1925 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
|
adamc@467
|
1926 if (n == NULL)
|
adamc@467
|
1927 return "NULL";
|
adamc@467
|
1928 else
|
adamc@467
|
1929 return uw_Basis_sqlifyFloat(ctx, *n);
|
adamc@467
|
1930 }
|
adamc@467
|
1931
|
adamc@281
|
1932
|
adamc@311
|
1933 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@280
|
1934 char *r, *s2;
|
adamc@280
|
1935
|
adamc@311
|
1936 uw_check_heap(ctx, strlen(s) * 2 + 10);
|
adamc@280
|
1937
|
adamc@666
|
1938 r = s2 = ctx->heap.front;
|
adamc@280
|
1939 *s2++ = 'E';
|
adamc@280
|
1940 *s2++ = '\'';
|
adamc@280
|
1941
|
adamc@280
|
1942 for (; *s; s++) {
|
adamc@280
|
1943 char c = *s;
|
adamc@280
|
1944
|
adamc@280
|
1945 switch (c) {
|
adamc@280
|
1946 case '\'':
|
adamc@280
|
1947 strcpy(s2, "\\'");
|
adamc@280
|
1948 s2 += 2;
|
adamc@280
|
1949 break;
|
adamc@280
|
1950 case '\\':
|
adamc@280
|
1951 strcpy(s2, "\\\\");
|
adamc@280
|
1952 s2 += 2;
|
adamc@280
|
1953 break;
|
adamc@280
|
1954 default:
|
adamc@280
|
1955 if (isprint(c))
|
adamc@280
|
1956 *s2++ = c;
|
adamc@280
|
1957 else {
|
adamc@737
|
1958 sprintf(s2, "\\%03o", c);
|
adamc@280
|
1959 s2 += 4;
|
adamc@280
|
1960 }
|
adamc@280
|
1961 }
|
adamc@280
|
1962 }
|
adamc@280
|
1963
|
adamc@281
|
1964 strcpy(s2, "'::text");
|
adamc@666
|
1965 ctx->heap.front = s2 + 8;
|
adamc@280
|
1966 return r;
|
adamc@280
|
1967 }
|
adamc@281
|
1968
|
adamc@737
|
1969 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
|
adamc@737
|
1970 char *r, *s2;
|
adamc@737
|
1971 size_t i;
|
adamc@737
|
1972
|
adamc@737
|
1973 uw_check_heap(ctx, b.size * 5 + 11);
|
adamc@737
|
1974
|
adamc@737
|
1975 r = s2 = ctx->heap.front;
|
adamc@737
|
1976 *s2++ = 'E';
|
adamc@737
|
1977 *s2++ = '\'';
|
adamc@737
|
1978
|
adamc@737
|
1979 for (i = 0; i < b.size; ++i) {
|
adamc@737
|
1980 char c = b.data[i];
|
adamc@737
|
1981
|
adamc@737
|
1982 switch (c) {
|
adamc@737
|
1983 case '\'':
|
adamc@737
|
1984 strcpy(s2, "\\'");
|
adamc@737
|
1985 s2 += 2;
|
adamc@737
|
1986 break;
|
adamc@737
|
1987 case '\\':
|
adamc@737
|
1988 strcpy(s2, "\\\\\\\\");
|
adamc@737
|
1989 s2 += 4;
|
adamc@737
|
1990 break;
|
adamc@737
|
1991 default:
|
adamc@737
|
1992 if (isprint(c))
|
adamc@737
|
1993 *s2++ = c;
|
adamc@737
|
1994 else {
|
adamc@737
|
1995 sprintf(s2, "\\\\%03o", c);
|
adamc@737
|
1996 s2 += 5;
|
adamc@737
|
1997 }
|
adamc@737
|
1998 }
|
adamc@737
|
1999 }
|
adamc@737
|
2000
|
adamc@737
|
2001 strcpy(s2, "'::bytea");
|
adamc@737
|
2002 ctx->heap.front = s2 + 9;
|
adamc@737
|
2003 return r;
|
adamc@737
|
2004 }
|
adamc@737
|
2005
|
adamc@682
|
2006 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
2007 int len;
|
adamc@682
|
2008 char *r;
|
adamc@682
|
2009 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
|
adamc@682
|
2010
|
adamc@682
|
2011 uw_check_heap(ctx, INTS_MAX + 7);
|
adamc@682
|
2012 r = ctx->heap.front;
|
adamc@682
|
2013 sprintf(r, "%lld::int8%n", combo, &len);
|
adamc@682
|
2014 ctx->heap.front += len+1;
|
adamc@682
|
2015 return r;
|
adamc@682
|
2016 }
|
adamc@682
|
2017
|
adamc@682
|
2018 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
2019 int len;
|
adamc@682
|
2020 char *r;
|
adamc@682
|
2021 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
|
adamc@682
|
2022
|
adamc@682
|
2023 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
2024 r = ctx->heap.front;
|
adamc@682
|
2025 sprintf(r, "%lld%n", combo, &len);
|
adamc@682
|
2026 ctx->heap.front += len+1;
|
adamc@682
|
2027 return r;
|
adamc@682
|
2028 }
|
adamc@682
|
2029
|
adamc@682
|
2030 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
|
adamc@682
|
2031 int len;
|
adamc@682
|
2032 char *r;
|
adamc@682
|
2033
|
adamc@682
|
2034 uw_check_heap(ctx, INTS_MAX + 7);
|
adamc@682
|
2035 r = ctx->heap.front;
|
adamc@682
|
2036 sprintf(r, "%u::int4%n", cli, &len);
|
adamc@682
|
2037 ctx->heap.front += len+1;
|
adamc@682
|
2038 return r;
|
adamc@682
|
2039 }
|
adamc@682
|
2040
|
adamc@682
|
2041 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
|
adamc@682
|
2042 int len;
|
adamc@682
|
2043 char *r;
|
adamc@682
|
2044
|
adamc@682
|
2045 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
2046 r = ctx->heap.front;
|
adamc@682
|
2047 sprintf(r, "%u%n", cli, &len);
|
adamc@682
|
2048 ctx->heap.front += len+1;
|
adamc@682
|
2049 return r;
|
adamc@682
|
2050 }
|
adamc@682
|
2051
|
adamc@467
|
2052 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
|
adamc@467
|
2053 if (s == NULL)
|
adamc@467
|
2054 return "NULL";
|
adamc@467
|
2055 else
|
adamc@467
|
2056 return uw_Basis_sqlifyString(ctx, s);
|
adamc@467
|
2057 }
|
adamc@467
|
2058
|
adamc@311
|
2059 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2060 if (b == uw_Basis_False)
|
adamc@281
|
2061 return "FALSE";
|
adamc@281
|
2062 else
|
adamc@281
|
2063 return "TRUE";
|
adamc@281
|
2064 }
|
adamc@282
|
2065
|
adamc@467
|
2066 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
|
adamc@467
|
2067 if (b == NULL)
|
adamc@467
|
2068 return "NULL";
|
adamc@467
|
2069 else
|
adamc@467
|
2070 return uw_Basis_sqlifyBool(ctx, *b);
|
adamc@467
|
2071 }
|
adamc@467
|
2072
|
adamc@439
|
2073 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@439
|
2074 size_t len;
|
adamc@682
|
2075 char *r, *s;
|
adamc@682
|
2076 struct tm stm;
|
adamc@682
|
2077
|
adamc@682
|
2078 if (localtime_r(&t, &stm)) {
|
adamc@682
|
2079 s = uw_malloc(ctx, TIMES_MAX);
|
adamc@682
|
2080 len = strftime(s, TIMES_MAX, TIME_FMT, &stm);
|
adamc@682
|
2081 r = uw_malloc(ctx, len + 14);
|
adamc@682
|
2082 sprintf(r, "'%s'::timestamp", s);
|
adamc@682
|
2083 return r;
|
adamc@682
|
2084 } else
|
adamc@682
|
2085 return "<Invalid time>";
|
adamc@682
|
2086 }
|
adamc@682
|
2087
|
adamc@682
|
2088 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@682
|
2089 size_t len;
|
adamc@439
|
2090 char *r;
|
adamc@439
|
2091 struct tm stm;
|
adamc@439
|
2092
|
adamc@439
|
2093 if (localtime_r(&t, &stm)) {
|
adamc@439
|
2094 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
2095 r = ctx->heap.front;
|
adamc@439
|
2096 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
2097 ctx->heap.front += len+1;
|
adamc@439
|
2098 return r;
|
adamc@439
|
2099 } else
|
adamc@439
|
2100 return "<Invalid time>";
|
adamc@439
|
2101 }
|
adamc@439
|
2102
|
adamc@467
|
2103 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
|
adamc@467
|
2104 if (t == NULL)
|
adamc@467
|
2105 return "NULL";
|
adamc@467
|
2106 else
|
adamc@467
|
2107 return uw_Basis_sqlifyTime(ctx, *t);
|
adamc@467
|
2108 }
|
adamc@467
|
2109
|
adamc@311
|
2110 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
|
adamc@311
|
2111 static uw_Basis_int true = 1;
|
adamc@311
|
2112 static uw_Basis_int false = 0;
|
adamc@282
|
2113
|
adamc@311
|
2114 if (b == uw_Basis_False)
|
adamc@282
|
2115 return (char *)&false;
|
adamc@282
|
2116 else
|
adamc@282
|
2117 return (char *)&true;
|
adamc@282
|
2118 }
|
adamc@284
|
2119
|
adamc@311
|
2120 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
|
adamc@284
|
2121 int len;
|
adamc@284
|
2122 char *r;
|
adamc@284
|
2123
|
adamc@311
|
2124 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
2125 r = ctx->heap.front;
|
adamc@284
|
2126 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
2127 ctx->heap.front += len+1;
|
adamc@284
|
2128 return r;
|
adamc@284
|
2129 }
|
adamc@285
|
2130
|
adamc@311
|
2131 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
|
adamc@285
|
2132 int len;
|
adamc@285
|
2133 char *r;
|
adamc@285
|
2134
|
adamc@311
|
2135 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
2136 r = ctx->heap.front;
|
adamc@285
|
2137 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
2138 ctx->heap.front += len+1;
|
adamc@285
|
2139 return r;
|
adamc@285
|
2140 }
|
adamc@285
|
2141
|
adamc@821
|
2142 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
|
adamc@821
|
2143 char *r = uw_malloc(ctx, 2);
|
adamc@821
|
2144 r[0] = ch;
|
adamc@821
|
2145 r[1] = 0;
|
adamc@821
|
2146 return r;
|
adamc@821
|
2147 }
|
adamc@821
|
2148
|
adamc@311
|
2149 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2150 if (b == uw_Basis_False)
|
adamc@285
|
2151 return "False";
|
adamc@285
|
2152 else
|
adamc@285
|
2153 return "True";
|
adamc@285
|
2154 }
|
adamc@288
|
2155
|
adamc@436
|
2156 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
2157 size_t len;
|
adamc@436
|
2158 char *r;
|
adamc@436
|
2159 struct tm stm;
|
adamc@436
|
2160
|
adamc@436
|
2161 if (localtime_r(&t, &stm)) {
|
adamc@436
|
2162 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
2163 r = ctx->heap.front;
|
adamc@436
|
2164 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
2165 ctx->heap.front += len+1;
|
adamc@436
|
2166 return r;
|
adamc@436
|
2167 } else
|
adamc@436
|
2168 return "<Invalid time>";
|
adamc@436
|
2169 }
|
adamc@288
|
2170
|
adamc@311
|
2171 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
|
adamc@288
|
2172 char *endptr;
|
adamc@311
|
2173 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@288
|
2174
|
adamc@288
|
2175 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
2176 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adamc@288
|
2177 *r = n;
|
adamc@288
|
2178 return r;
|
adamc@288
|
2179 } else
|
adamc@288
|
2180 return NULL;
|
adamc@288
|
2181 }
|
adamc@289
|
2182
|
adamc@311
|
2183 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
|
adamc@289
|
2184 char *endptr;
|
adamc@311
|
2185 uw_Basis_float n = strtod(s, &endptr);
|
adamc@289
|
2186
|
adamc@289
|
2187 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
2188 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
|
adamc@289
|
2189 *r = n;
|
adamc@289
|
2190 return r;
|
adamc@289
|
2191 } else
|
adamc@289
|
2192 return NULL;
|
adamc@289
|
2193 }
|
adamc@289
|
2194
|
adamc@821
|
2195 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
|
adamc@821
|
2196 if (s[0] == 0) {
|
adamc@821
|
2197 uw_Basis_char *r = uw_malloc(ctx, 1);
|
adamc@821
|
2198 r[0] = 0;
|
adamc@821
|
2199 return r;
|
adamc@821
|
2200 } else if (s[1] != 0)
|
adamc@821
|
2201 return NULL;
|
adamc@821
|
2202 else {
|
adamc@821
|
2203 uw_Basis_char *r = uw_malloc(ctx, 1);
|
adamc@821
|
2204 r[0] = s[0];
|
adamc@821
|
2205 return r;
|
adamc@821
|
2206 }
|
adamc@821
|
2207 }
|
adamc@821
|
2208
|
adamc@311
|
2209 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
2210 static uw_Basis_bool true = uw_Basis_True;
|
adamc@311
|
2211 static uw_Basis_bool false = uw_Basis_False;
|
adamc@289
|
2212
|
adamc@289
|
2213 if (!strcasecmp (s, "True"))
|
adamc@289
|
2214 return &true;
|
adamc@289
|
2215 else if (!strcasecmp (s, "False"))
|
adamc@289
|
2216 return &false;
|
adamc@289
|
2217 else
|
adamc@289
|
2218 return NULL;
|
adamc@289
|
2219 }
|
adamc@292
|
2220
|
adamc@436
|
2221 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
2222 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
2223 struct tm stm;
|
adamc@436
|
2224
|
adamc@439
|
2225 if (dot) {
|
adamc@439
|
2226 *dot = 0;
|
adamc@439
|
2227 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
2228 *dot = '.';
|
adamc@439
|
2229 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
2230 *r = mktime(&stm);
|
adamc@439
|
2231 return r;
|
adamc@439
|
2232 }
|
adamc@439
|
2233 else {
|
adamc@439
|
2234 *dot = '.';
|
adamc@439
|
2235 return NULL;
|
adamc@439
|
2236 }
|
adamc@436
|
2237 }
|
adamc@439
|
2238 else {
|
adamc@439
|
2239 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
2240 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
2241 *r = mktime(&stm);
|
adamc@439
|
2242 return r;
|
adamc@439
|
2243 }
|
adamc@439
|
2244 else if (strptime(s, TIME_FMT, &stm) == end) {
|
adamc@439
|
2245 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
2246 *r = mktime(&stm);
|
adamc@439
|
2247 return r;
|
adamc@439
|
2248 }
|
adamc@439
|
2249 else
|
adamc@439
|
2250 return NULL;
|
adamc@439
|
2251 }
|
adamc@436
|
2252 }
|
adamc@436
|
2253
|
adamc@311
|
2254 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
|
adamc@292
|
2255 char *endptr;
|
adamc@311
|
2256 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@292
|
2257
|
adamc@292
|
2258 if (*s != '\0' && *endptr == '\0')
|
adamc@292
|
2259 return n;
|
adamc@292
|
2260 else
|
adamc@311
|
2261 uw_error(ctx, FATAL, "Can't parse int: %s", s);
|
adamc@292
|
2262 }
|
adamc@293
|
2263
|
adamc@682
|
2264 #include <errno.h>
|
adamc@682
|
2265
|
adamc@678
|
2266 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
|
adamc@682
|
2267 unsigned long long n;
|
adamc@682
|
2268
|
adamc@682
|
2269 if (sscanf(s, "%llu", &n) < 1)
|
adamc@682
|
2270 uw_error(ctx, FATAL, "Can't parse channel: %s", s);
|
adamc@682
|
2271 else {
|
adamc@682
|
2272 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
|
adamc@682
|
2273 return ch;
|
adamc@682
|
2274 }
|
adamc@682
|
2275 }
|
adamc@682
|
2276
|
adamc@682
|
2277 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
|
adamc@678
|
2278 char *endptr;
|
adamc@682
|
2279 unsigned long n = strtoul(s, &endptr, 10);
|
adamc@678
|
2280
|
adamc@678
|
2281 if (*s != '\0' && *endptr == '\0')
|
adamc@678
|
2282 return n;
|
adamc@678
|
2283 else
|
adamc@682
|
2284 uw_error(ctx, FATAL, "Can't parse client: %s", s);
|
adamc@678
|
2285 }
|
adamc@678
|
2286
|
adamc@311
|
2287 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
|
adamc@293
|
2288 char *endptr;
|
adamc@311
|
2289 uw_Basis_float n = strtod(s, &endptr);
|
adamc@293
|
2290
|
adamc@293
|
2291 if (*s != '\0' && *endptr == '\0')
|
adamc@293
|
2292 return n;
|
adamc@293
|
2293 else
|
adamc@311
|
2294 uw_error(ctx, FATAL, "Can't parse float: %s", s);
|
adamc@293
|
2295 }
|
adamc@293
|
2296
|
adamc@821
|
2297 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
|
adamc@821
|
2298 if (s[0] == 0)
|
adamc@821
|
2299 return 0;
|
adamc@821
|
2300 else if (s[1] != 0)
|
adamc@821
|
2301 uw_error(ctx, FATAL, "Can't parse char: %s", s);
|
adamc@821
|
2302 else
|
adamc@821
|
2303 return s[0];
|
adamc@821
|
2304 }
|
adamc@821
|
2305
|
adamc@311
|
2306 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
|
adamc@296
|
2307 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
|
adamc@311
|
2308 return uw_Basis_True;
|
adamc@296
|
2309 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
|
adamc@311
|
2310 return uw_Basis_False;
|
adamc@293
|
2311 else
|
adamc@311
|
2312 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
|
adamc@293
|
2313 }
|
adamc@436
|
2314
|
adamc@436
|
2315 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
2316 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
2317 struct tm stm = {};
|
adamc@436
|
2318
|
adamc@438
|
2319 if (dot) {
|
adamc@438
|
2320 *dot = 0;
|
adamc@438
|
2321 if (strptime(s, TIME_FMT_PG, &stm)) {
|
adamc@438
|
2322 *dot = '.';
|
adamc@438
|
2323 return mktime(&stm);
|
adamc@438
|
2324 }
|
adamc@438
|
2325 else {
|
adamc@438
|
2326 *dot = '.';
|
adamc@438
|
2327 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
2328 }
|
adamc@438
|
2329 }
|
adamc@438
|
2330 else {
|
adamc@439
|
2331 if (strptime(s, TIME_FMT_PG, &stm) == end)
|
adamc@439
|
2332 return mktime(&stm);
|
adamc@439
|
2333 else if (strptime(s, TIME_FMT, &stm) == end)
|
adamc@438
|
2334 return mktime(&stm);
|
adamc@438
|
2335 else
|
adamc@438
|
2336 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
2337 }
|
adamc@436
|
2338 }
|
adamc@457
|
2339
|
adamc@742
|
2340 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
|
adamc@742
|
2341 char *r = ctx->heap.front;
|
adamc@742
|
2342 uw_Basis_blob b = {len, r};
|
adamc@742
|
2343
|
adamc@742
|
2344 uw_check_heap(ctx, len);
|
adamc@742
|
2345
|
adamc@742
|
2346 while (*s) {
|
adamc@742
|
2347 if (s[0] == '\\') {
|
adamc@742
|
2348 if (s[1] == '\\') {
|
adamc@742
|
2349 *r++ = '\\';
|
adamc@742
|
2350 s += 2;
|
adamc@742
|
2351 } else if (isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) {
|
adamc@742
|
2352 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
|
adamc@742
|
2353 s += 4;
|
adamc@742
|
2354 }
|
adamc@742
|
2355 else {
|
adamc@742
|
2356 *r++ = '\\';
|
adamc@742
|
2357 ++s;
|
adamc@742
|
2358 }
|
adamc@742
|
2359 } else {
|
adamc@742
|
2360 *r++ = s[0];
|
adamc@742
|
2361 ++s;
|
adamc@742
|
2362 }
|
adamc@742
|
2363 }
|
adamc@742
|
2364
|
adamc@742
|
2365 b.size = r - ctx->heap.front;
|
adamc@742
|
2366 ctx->heap.front = r;
|
adamc@742
|
2367 return b;
|
adamc@742
|
2368 }
|
adamc@742
|
2369
|
adamc@463
|
2370 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
|
adamc@463
|
2371 int len = strlen(c);
|
adamc@666
|
2372 char *s = ctx->headers, *p = ctx->outHeaders.start;
|
adamc@474
|
2373
|
adamc@474
|
2374 while (p = strstr(p, "\nSet-Cookie: ")) {
|
adamc@474
|
2375 char *p2;
|
adamc@474
|
2376 p += 13;
|
adamc@474
|
2377 p2 = strchr(p, '=');
|
adamc@474
|
2378
|
adamc@474
|
2379 if (p2) {
|
adamc@474
|
2380 size_t sz = strcspn(p2+1, ";\r\n");
|
adamc@474
|
2381
|
adamc@474
|
2382 if (!strncasecmp(p, c, p2 - p)) {
|
adamc@474
|
2383 char *ret = uw_malloc(ctx, sz + 1);
|
adamc@474
|
2384 memcpy(ret, p2+1, sz);
|
adamc@474
|
2385 ret[sz] = 0;
|
adamc@474
|
2386 return ret;
|
adamc@474
|
2387 }
|
adamc@474
|
2388 }
|
adamc@474
|
2389 }
|
adamc@463
|
2390
|
adamc@463
|
2391 while (p = strchr(s, ':')) {
|
adamc@476
|
2392 if (!strncasecmp(s, "Cookie: ", 8)) {
|
adamc@476
|
2393 p += 2;
|
adamc@476
|
2394 while (1) {
|
adamc@476
|
2395 if (!strncmp(p, c, len)
|
adamc@476
|
2396 && p + len < ctx->headers_end && p[len] == '=')
|
adamc@476
|
2397 return p + 1 + len;
|
adamc@476
|
2398 else if (p = strchr(p, ';'))
|
adamc@476
|
2399 p += 2;
|
adamc@476
|
2400 else if ((s = strchr(s, 0)) && s < ctx->headers_end) {
|
adamc@476
|
2401 s += 2;
|
adamc@476
|
2402 break;
|
adamc@476
|
2403 }
|
adamc@476
|
2404 else
|
adamc@476
|
2405 return NULL;
|
adamc@476
|
2406 }
|
adamc@463
|
2407 } else {
|
adamc@463
|
2408 if ((s = strchr(p, 0)) && s < ctx->headers_end)
|
adamc@463
|
2409 s += 2;
|
adamc@463
|
2410 else
|
adamc@463
|
2411 return NULL;
|
adamc@463
|
2412 }
|
adamc@463
|
2413 }
|
adamc@792
|
2414
|
adamc@792
|
2415 return NULL;
|
adamc@457
|
2416 }
|
adamc@462
|
2417
|
adamc@466
|
2418 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v) {
|
adamc@462
|
2419 uw_write_header(ctx, "Set-Cookie: ");
|
adamc@462
|
2420 uw_write_header(ctx, c);
|
adamc@462
|
2421 uw_write_header(ctx, "=");
|
adamc@462
|
2422 uw_write_header(ctx, v);
|
adamc@466
|
2423 uw_write_header(ctx, "; path=");
|
adamc@466
|
2424 uw_write_header(ctx, prefix);
|
adamc@462
|
2425 uw_write_header(ctx, "\r\n");
|
adamc@462
|
2426
|
adamc@462
|
2427 return uw_unit_v;
|
adamc@462
|
2428 }
|
adamc@667
|
2429
|
adamc@682
|
2430 static delta *allocate_delta(uw_context ctx, unsigned client) {
|
adamc@682
|
2431 unsigned i;
|
adamc@682
|
2432 delta *d;
|
adamc@671
|
2433
|
adamc@682
|
2434 for (i = 0; i < ctx->used_deltas; ++i)
|
adamc@682
|
2435 if (ctx->deltas[i].client == client)
|
adamc@682
|
2436 return &ctx->deltas[i];
|
adamc@671
|
2437
|
adamc@682
|
2438 if (ctx->used_deltas >= ctx->n_deltas) {
|
adamc@682
|
2439 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
|
adamc@682
|
2440 buf_init(&ctx->deltas[ctx->n_deltas-1].msgs, 0);
|
adamc@682
|
2441 }
|
adamc@671
|
2442
|
adamc@682
|
2443 d = &ctx->deltas[ctx->used_deltas++];
|
adamc@682
|
2444 d->client = client;
|
adamc@682
|
2445 buf_reset(&d->msgs);
|
adamc@682
|
2446 return d;
|
adamc@671
|
2447 }
|
adamc@671
|
2448
|
adamc@668
|
2449 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
|
adamc@682
|
2450 if (ctx->client == NULL)
|
adamc@682
|
2451 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
|
adamc@671
|
2452
|
adamc@682
|
2453 return new_channel(ctx->client);
|
adamc@668
|
2454 }
|
adamc@668
|
2455
|
adamc@668
|
2456 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
|
adamc@682
|
2457 delta *d = allocate_delta(ctx, chn.cli);
|
adamc@682
|
2458 size_t len;
|
adamc@682
|
2459 int preLen;
|
adamc@682
|
2460 char pre[INTS_MAX + 2];
|
adamc@668
|
2461
|
adamc@682
|
2462 len = strlen(msg);
|
adamc@682
|
2463
|
adamc@682
|
2464 sprintf(pre, "%u\n%n", chn.chn, &preLen);
|
adamc@682
|
2465
|
adamc@682
|
2466 buf_append(&d->msgs, pre, preLen);
|
adamc@682
|
2467 buf_append(&d->msgs, msg, len);
|
adamc@682
|
2468 buf_append(&d->msgs, "\n", 1);
|
adamc@671
|
2469
|
adamc@671
|
2470 return uw_unit_v;
|
adamc@668
|
2471 }
|
adamc@671
|
2472
|
adamc@671
|
2473 int uw_db_commit(uw_context);
|
adamc@671
|
2474 int uw_db_rollback(uw_context);
|
adamc@671
|
2475
|
adamc@671
|
2476 void uw_commit(uw_context ctx) {
|
adamc@682
|
2477 unsigned i;
|
adamc@671
|
2478
|
adamc@771
|
2479 for (i = 0; i < ctx->used_transactionals; ++i)
|
adamc@771
|
2480 if (ctx->transactionals[i].rollback != NULL)
|
adamc@771
|
2481 ctx->transactionals[i].commit(ctx->transactionals[i].data);
|
adamc@771
|
2482
|
adamc@771
|
2483 for (i = 0; i < ctx->used_transactionals; ++i)
|
adamc@771
|
2484 if (ctx->transactionals[i].rollback == NULL)
|
adamc@771
|
2485 ctx->transactionals[i].commit(ctx->transactionals[i].data);
|
adamc@771
|
2486
|
adamc@771
|
2487 if (uw_db_commit(ctx))
|
adamc@671
|
2488 uw_error(ctx, FATAL, "Error running SQL COMMIT");
|
adamc@682
|
2489
|
adamc@682
|
2490 for (i = 0; i < ctx->used_deltas; ++i) {
|
adamc@682
|
2491 delta *d = &ctx->deltas[i];
|
adamc@682
|
2492 client *c = find_client(d->client);
|
adamc@682
|
2493
|
adamc@682
|
2494 assert (c != NULL && c->mode == USED);
|
adamc@682
|
2495
|
adamc@685
|
2496 client_send(c, &d->msgs);
|
adamc@682
|
2497 }
|
adamc@682
|
2498
|
adamc@682
|
2499 if (ctx->client)
|
adamc@685
|
2500 release_client(ctx->client);
|
adamc@771
|
2501
|
adamc@771
|
2502 for (i = 0; i < ctx->used_transactionals; ++i)
|
adamc@771
|
2503 ctx->transactionals[i].free(ctx->transactionals[i].data);
|
adamc@671
|
2504 }
|
adamc@671
|
2505
|
adamc@671
|
2506 int uw_rollback(uw_context ctx) {
|
adamc@771
|
2507 size_t i;
|
adamc@771
|
2508
|
adamc@682
|
2509 if (ctx->client)
|
adamc@685
|
2510 release_client(ctx->client);
|
adamc@671
|
2511
|
adamc@771
|
2512 for (i = 0; i < ctx->used_transactionals; ++i)
|
adamc@771
|
2513 if (ctx->transactionals[i].rollback != NULL)
|
adamc@771
|
2514 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
|
adamc@771
|
2515
|
adamc@771
|
2516 for (i = 0; i < ctx->used_transactionals; ++i)
|
adamc@771
|
2517 ctx->transactionals[i].free(ctx->transactionals[i].data);
|
adamc@771
|
2518
|
adamc@671
|
2519 return uw_db_rollback(ctx);
|
adamc@671
|
2520 }
|
adamc@683
|
2521
|
adamc@771
|
2522 void uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback,
|
adamc@771
|
2523 uw_callback free) {
|
adamc@771
|
2524 if (commit == NULL)
|
adamc@771
|
2525 uw_error(ctx, FATAL, "uw_register_transactional: NULL commit callback");
|
adamc@771
|
2526
|
adamc@771
|
2527 if (ctx->used_transactionals >= ctx->n_transactionals) {
|
adamc@771
|
2528 ctx->transactionals = realloc(ctx->transactionals, ctx->used_transactionals+1);
|
adamc@771
|
2529 ++ctx->n_transactionals;
|
adamc@771
|
2530 }
|
adamc@771
|
2531
|
adamc@771
|
2532 ctx->transactionals[ctx->used_transactionals].data = data;
|
adamc@771
|
2533 ctx->transactionals[ctx->used_transactionals].commit = commit;
|
adamc@771
|
2534 ctx->transactionals[ctx->used_transactionals].rollback = rollback;
|
adamc@771
|
2535 ctx->transactionals[ctx->used_transactionals++].free = free;
|
adamc@771
|
2536 }
|
adamc@771
|
2537
|
adamc@683
|
2538
|
adamc@683
|
2539 // "Garbage collection"
|
adamc@683
|
2540
|
adamc@683
|
2541 void uw_expunger(uw_context ctx, uw_Basis_client cli);
|
adamc@683
|
2542
|
adamc@683
|
2543 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli) {
|
adamc@683
|
2544 int r = setjmp(ctx->jmp_buf);
|
adamc@683
|
2545
|
adamc@684
|
2546 if (r == 0) {
|
adamc@684
|
2547 if (uw_db_begin(ctx))
|
adamc@684
|
2548 uw_error(ctx, FATAL, "Error running SQL BEGIN");
|
adamc@683
|
2549 uw_expunger(ctx, cli);
|
adamc@684
|
2550 if (uw_db_commit(ctx))
|
adamc@684
|
2551 uw_error(ctx, FATAL, "Error running SQL COMMIT");
|
adamc@684
|
2552 }
|
adamc@683
|
2553
|
adamc@683
|
2554 return r;
|
adamc@683
|
2555 }
|
adamc@683
|
2556
|
adamc@683
|
2557 void uw_prune_clients(uw_context ctx) {
|
adamc@683
|
2558 client *c, *next, *prev = NULL;
|
adamc@683
|
2559 time_t cutoff;
|
adamc@683
|
2560
|
adamc@683
|
2561 cutoff = time(NULL) - uw_timeout;
|
adamc@683
|
2562
|
adamc@683
|
2563 pthread_mutex_lock(&clients_mutex);
|
adamc@683
|
2564
|
adamc@683
|
2565 for (c = clients_used; c; c = next) {
|
adamc@683
|
2566 next = c->next;
|
adamc@683
|
2567 pthread_mutex_lock(&c->lock);
|
adamc@685
|
2568 if (c->last_contact < cutoff && c->refcount == 0) {
|
adamc@683
|
2569 failure_kind fk = UNLIMITED_RETRY;
|
adamc@683
|
2570 if (prev)
|
adamc@683
|
2571 prev->next = next;
|
adamc@683
|
2572 else
|
adamc@683
|
2573 clients_used = next;
|
adamc@684
|
2574 uw_reset(ctx);
|
adamc@683
|
2575 while (fk == UNLIMITED_RETRY) {
|
adamc@683
|
2576 fk = uw_expunge(ctx, c->id);
|
adamc@684
|
2577 if (fk == UNLIMITED_RETRY) {
|
adamc@684
|
2578 uw_db_rollback(ctx);
|
adamc@684
|
2579 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
|
adamc@683
|
2580 }
|
adamc@683
|
2581 }
|
adamc@684
|
2582 if (fk == SUCCESS)
|
adamc@684
|
2583 free_client(c);
|
adamc@684
|
2584 else {
|
adamc@684
|
2585 uw_db_rollback(ctx);
|
adamc@683
|
2586 printf("Expunge blocked by error: %s\n", uw_error_message(ctx));
|
adamc@684
|
2587 }
|
adamc@683
|
2588 }
|
adamc@683
|
2589 else
|
adamc@683
|
2590 prev = c;
|
adamc@683
|
2591 pthread_mutex_unlock(&c->lock);
|
adamc@683
|
2592 }
|
adamc@683
|
2593
|
adamc@683
|
2594 pthread_mutex_unlock(&clients_mutex);
|
adamc@683
|
2595 }
|
adamc@687
|
2596
|
adamc@687
|
2597 void uw_initializer(uw_context ctx);
|
adamc@687
|
2598
|
adamc@687
|
2599 failure_kind uw_initialize(uw_context ctx) {
|
adamc@687
|
2600 int r = setjmp(ctx->jmp_buf);
|
adamc@687
|
2601
|
adamc@687
|
2602 if (r == 0) {
|
adamc@687
|
2603 if (uw_db_begin(ctx))
|
adamc@687
|
2604 uw_error(ctx, FATAL, "Error running SQL BEGIN");
|
adamc@687
|
2605 uw_initializer(ctx);
|
adamc@687
|
2606 if (uw_db_commit(ctx))
|
adamc@687
|
2607 uw_error(ctx, FATAL, "Error running SQL COMMIT");
|
adamc@687
|
2608 }
|
adamc@687
|
2609
|
adamc@687
|
2610 return r;
|
adamc@687
|
2611 }
|
adamc@717
|
2612
|
adamc@770
|
2613 extern int uw_check_url(const char *);
|
adamc@770
|
2614 extern int uw_check_mime(const char *);
|
adamc@770
|
2615
|
adamc@717
|
2616 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
|
adamc@770
|
2617 if (uw_check_url(s))
|
adamc@770
|
2618 return s;
|
adamc@770
|
2619 else
|
adamc@770
|
2620 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@717
|
2621 }
|
adamc@734
|
2622
|
adamc@770
|
2623 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
|
adamc@770
|
2624 if (uw_check_url(s))
|
adamc@770
|
2625 return s;
|
adamc@770
|
2626 else
|
adamc@770
|
2627 return NULL;
|
adamc@770
|
2628 }
|
adamc@770
|
2629
|
adamc@770
|
2630 int mime_format(const char *s) {
|
adamc@770
|
2631 for (; *s; ++s)
|
adamc@770
|
2632 if (!isalnum(*s) && *s != '/' && *s != '-' && *s != '.')
|
adamc@770
|
2633 return 0;
|
adamc@770
|
2634
|
adamc@770
|
2635 return 1;
|
adamc@770
|
2636 }
|
adamc@770
|
2637
|
adamc@741
|
2638 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
|
adamc@770
|
2639 if (!mime_format(s))
|
adamc@770
|
2640 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
|
adamc@770
|
2641
|
adamc@770
|
2642 if (uw_check_mime(s))
|
adamc@770
|
2643 return s;
|
adamc@770
|
2644 else
|
adamc@770
|
2645 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@770
|
2646 }
|
adamc@770
|
2647
|
adamc@770
|
2648 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
|
adamc@770
|
2649 if (!mime_format(s))
|
adamc@770
|
2650 return NULL;
|
adamc@770
|
2651
|
adamc@770
|
2652 if (uw_check_mime(s))
|
adamc@770
|
2653 return s;
|
adamc@770
|
2654 else
|
adamc@770
|
2655 return NULL;
|
adamc@741
|
2656 }
|
adamc@741
|
2657
|
adamc@734
|
2658 uw_Basis_string uw_unnull(uw_Basis_string s) {
|
adamc@734
|
2659 return s ? s : "";
|
adamc@734
|
2660 }
|
adamc@734
|
2661
|
adamc@734
|
2662 extern int uw_hash_blocksize;
|
adamc@734
|
2663
|
adamc@734
|
2664 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
|
adamc@734
|
2665 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
|
adamc@734
|
2666 int i;
|
adamc@734
|
2667
|
adamc@734
|
2668 for (i = 0; i < uw_hash_blocksize; ++i)
|
adamc@734
|
2669 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
|
adamc@734
|
2670
|
adamc@734
|
2671 return r;
|
adamc@734
|
2672 }
|
adamc@734
|
2673
|
adamc@734
|
2674 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
|
adamc@734
|
2675 return uw_cookie_sig(ctx);
|
adamc@734
|
2676 }
|
adamc@737
|
2677
|
adamc@737
|
2678 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
|
adamc@737
|
2679 return f.name;
|
adamc@737
|
2680 }
|
adamc@737
|
2681
|
adamc@740
|
2682 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
|
adamc@740
|
2683 return f.type;
|
adamc@740
|
2684 }
|
adamc@740
|
2685
|
adamc@745
|
2686 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
|
adamc@745
|
2687 return b.size;
|
adamc@745
|
2688 }
|
adamc@745
|
2689
|
adamc@737
|
2690 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
|
adamc@737
|
2691 return f.data;
|
adamc@737
|
2692 }
|
adamc@741
|
2693
|
adamc@741
|
2694 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
|
adamc@741
|
2695 cleanup *cl;
|
adamc@741
|
2696 int len;
|
adamc@741
|
2697
|
adamc@741
|
2698 buf_reset(&ctx->outHeaders);
|
adamc@741
|
2699 buf_reset(&ctx->page);
|
adamc@741
|
2700
|
adamc@741
|
2701 uw_write_header(ctx, "HTTP/1.1 200 OK\r\nContent-Type: ");
|
adamc@741
|
2702 uw_write_header(ctx, mimeType);
|
adamc@741
|
2703 uw_write_header(ctx, "\r\nContent-Length: ");
|
adamc@741
|
2704 buf_check(&ctx->outHeaders, INTS_MAX);
|
adamc@741
|
2705 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len);
|
adamc@741
|
2706 ctx->outHeaders.front += len;
|
adamc@741
|
2707 uw_write_header(ctx, "\r\n");
|
adamc@741
|
2708
|
adamc@741
|
2709 buf_append(&ctx->page, b.data, b.size);
|
adamc@741
|
2710
|
adamc@741
|
2711 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@741
|
2712 cl->func(cl->arg);
|
adamc@741
|
2713
|
adamc@741
|
2714 ctx->cleanup_front = ctx->cleanup;
|
adamc@741
|
2715
|
adamc@741
|
2716 longjmp(ctx->jmp_buf, RETURN_BLOB);
|
adamc@741
|
2717 }
|