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