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