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