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