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