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