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