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