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