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>
|
adam@1571
|
19 #include <math.h>
|
adamc@102
|
20
|
adamc@667
|
21 #include <pthread.h>
|
adamc@667
|
22
|
adamc@102
|
23 #include "types.h"
|
adamc@102
|
24
|
ziv@2234
|
25 #include "uthash.h"
|
ziv@2234
|
26
|
adam@1431
|
27 uw_unit uw_unit_v = 0;
|
adamc@102
|
28
|
adamc@667
|
29
|
adamc@667
|
30 // Socket extras
|
adamc@667
|
31
|
adamc@863
|
32 int uw_really_send(int sock, const void *buf, ssize_t len) {
|
adamc@667
|
33 while (len > 0) {
|
adamc@856
|
34 ssize_t n = send(sock, buf, len, 0);
|
adamc@856
|
35
|
adamc@856
|
36 if (n < 0)
|
adamc@856
|
37 return n;
|
adamc@856
|
38
|
adamc@856
|
39 buf += n;
|
adamc@856
|
40 len -= n;
|
adamc@856
|
41 }
|
adamc@856
|
42
|
adamc@856
|
43 return 0;
|
adamc@856
|
44 }
|
adamc@856
|
45
|
adamc@856
|
46 int uw_really_write(int fd, const void *buf, size_t len) {
|
adamc@856
|
47 while (len > 0) {
|
adamc@856
|
48 ssize_t n = write(fd, buf, len);
|
adamc@667
|
49
|
adamc@667
|
50 if (n < 0)
|
adamc@667
|
51 return n;
|
adamc@667
|
52
|
adamc@667
|
53 buf += n;
|
adamc@667
|
54 len -= n;
|
adamc@667
|
55 }
|
adamc@667
|
56
|
adamc@667
|
57 return 0;
|
adamc@667
|
58 }
|
adamc@667
|
59
|
adamc@667
|
60
|
adamc@667
|
61 // Buffers
|
adamc@667
|
62
|
adam@1369
|
63 void uw_buffer_init(size_t max, uw_buffer *b, size_t s) {
|
adamc@1113
|
64 b->max = max;
|
adamc@667
|
65 b->front = b->start = malloc(s);
|
adamc@667
|
66 b->back = b->front + s;
|
adamc@667
|
67 }
|
adamc@667
|
68
|
adam@1369
|
69 void uw_buffer_free(uw_buffer *b) {
|
adamc@667
|
70 free(b->start);
|
adamc@667
|
71 }
|
adamc@667
|
72
|
adam@1369
|
73 void uw_buffer_reset(uw_buffer *b) {
|
adamc@667
|
74 b->front = b->start;
|
ziv@2262
|
75 if (b->front != b->back) {
|
ziv@2262
|
76 *b->front = 0;
|
ziv@2262
|
77 }
|
adamc@667
|
78 }
|
adamc@667
|
79
|
adam@1369
|
80 int uw_buffer_check(uw_buffer *b, size_t extra) {
|
adamc@667
|
81 if (b->back - b->front < extra) {
|
adamc@667
|
82 size_t desired = b->front - b->start + extra, next;
|
adamc@667
|
83 char *new_heap;
|
adamc@667
|
84
|
adamc@667
|
85 next = b->back - b->start;
|
adamc@667
|
86 if (next == 0)
|
adamc@667
|
87 next = 1;
|
adamc@667
|
88 for (; next < desired; next *= 2);
|
adamc@667
|
89
|
adamc@1134
|
90 if (next > b->max) {
|
adamc@1113
|
91 if (desired <= b->max)
|
adamc@1113
|
92 next = desired;
|
adamc@1113
|
93 else
|
adamc@1113
|
94 return 1;
|
adamc@1134
|
95 }
|
adamc@1113
|
96
|
adamc@667
|
97 new_heap = realloc(b->start, next);
|
adamc@667
|
98 b->front = new_heap + (b->front - b->start);
|
adamc@667
|
99 b->back = new_heap + next;
|
adamc@667
|
100 b->start = new_heap;
|
adamc@667
|
101 }
|
adamc@1113
|
102
|
adamc@1113
|
103 return 0;
|
adamc@1113
|
104 }
|
adamc@1113
|
105
|
adamc@1113
|
106 __attribute__((noreturn)) void uw_error(uw_context, failure_kind, const char *, ...);
|
adamc@1113
|
107
|
adam@1369
|
108 static void ctx_uw_buffer_check(uw_context ctx, const char *kind, uw_buffer *b, size_t extra) {
|
adam@1369
|
109 if (uw_buffer_check(b, extra))
|
adamc@1113
|
110 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind);
|
adamc@667
|
111 }
|
adamc@667
|
112
|
adam@1369
|
113 size_t uw_buffer_used(uw_buffer *b) {
|
adamc@667
|
114 return b->front - b->start;
|
adamc@667
|
115 }
|
adamc@667
|
116
|
adam@1369
|
117 size_t uw_buffer_avail(uw_buffer *b) {
|
adamc@667
|
118 return b->back - b->start;
|
adamc@667
|
119 }
|
adamc@667
|
120
|
adam@1369
|
121 int uw_buffer_append(uw_buffer *b, const char *s, size_t len) {
|
adam@1369
|
122 if (uw_buffer_check(b, len+1))
|
adamc@1113
|
123 return 1;
|
adamc@1113
|
124
|
adamc@1113
|
125 memcpy(b->front, s, len);
|
adamc@1113
|
126 b->front += len;
|
adamc@1113
|
127 *b->front = 0;
|
adamc@1113
|
128
|
adamc@1113
|
129 return 0;
|
adamc@1113
|
130 }
|
adamc@1113
|
131
|
adam@1369
|
132 static void ctx_uw_buffer_append(uw_context ctx, const char *kind, uw_buffer *b, const char *s, size_t len) {
|
adam@1369
|
133 ctx_uw_buffer_check(ctx, kind, b, len+1);
|
adamc@1113
|
134
|
adamc@668
|
135 memcpy(b->front, s, len);
|
adamc@668
|
136 b->front += len;
|
adamc@672
|
137 *b->front = 0;
|
adamc@668
|
138 }
|
adamc@667
|
139
|
adamc@668
|
140
|
adamc@672
|
141 // Persistent state types
|
adamc@667
|
142
|
adamc@667
|
143 typedef enum { UNUSED, USED } usage;
|
adamc@667
|
144
|
adamc@667
|
145 typedef struct client {
|
adamc@682
|
146 unsigned id;
|
adamc@667
|
147 usage mode;
|
adamc@682
|
148 int pass;
|
adamc@682
|
149 struct client *next;
|
adamc@689
|
150 pthread_mutex_t lock, pull_lock;
|
adam@1369
|
151 uw_buffer msgs;
|
adamc@682
|
152 int sock;
|
adamc@863
|
153 int (*send)(int sockfd, const void *buf, ssize_t len);
|
adamc@863
|
154 int (*close)(int fd);
|
adamc@682
|
155 time_t last_contact;
|
adamc@682
|
156 unsigned n_channels;
|
adamc@685
|
157 unsigned refcount;
|
adamc@1121
|
158 void *data;
|
adamc@667
|
159 } client;
|
adamc@667
|
160
|
adamc@672
|
161
|
adamc@672
|
162 // Persistent client state
|
adamc@672
|
163
|
adamc@682
|
164 static client **clients, *clients_free, *clients_used;
|
adamc@682
|
165 static unsigned n_clients;
|
adamc@667
|
166
|
adam@1707
|
167 static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
|
adamc@1113
|
168 size_t uw_messages_max = SIZE_MAX;
|
adamc@1113
|
169 size_t uw_clients_max = SIZE_MAX;
|
adamc@1113
|
170
|
adamc@1121
|
171 void *uw_init_client_data();
|
adamc@1121
|
172 void uw_free_client_data(void *);
|
adamc@1121
|
173 void uw_copy_client_data(void *dst, void *src);
|
adamc@1121
|
174
|
adam@2104
|
175 static uw_Basis_int my_rand() {
|
adam@2105
|
176 int ret, r = RAND_bytes((unsigned char *)&ret, sizeof ret);
|
adam@2104
|
177 if (r)
|
adam@2105
|
178 return abs(ret);
|
adam@2104
|
179 else
|
adam@2104
|
180 return -1;
|
adam@2104
|
181 }
|
adam@2104
|
182
|
adamc@682
|
183 static client *new_client() {
|
adamc@667
|
184 client *c;
|
adamc@667
|
185
|
adamc@667
|
186 pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
187
|
adamc@667
|
188 if (clients_free) {
|
adamc@667
|
189 c = clients_free;
|
adamc@682
|
190 clients_free = clients_free->next;
|
adamc@667
|
191 }
|
adam@1672
|
192 else if (n_clients >= uw_clients_max) {
|
adam@1672
|
193 pthread_mutex_unlock(&clients_mutex);
|
adamc@1113
|
194 return NULL;
|
adam@1672
|
195 } else {
|
adamc@667
|
196 ++n_clients;
|
adamc@667
|
197 clients = realloc(clients, sizeof(client) * n_clients);
|
adamc@667
|
198 c = malloc(sizeof(client));
|
adamc@667
|
199 c->id = n_clients-1;
|
adamc@682
|
200 pthread_mutex_init(&c->lock, NULL);
|
adamc@689
|
201 pthread_mutex_init(&c->pull_lock, NULL);
|
adam@1369
|
202 uw_buffer_init(uw_messages_max, &c->msgs, 0);
|
adamc@667
|
203 clients[n_clients-1] = c;
|
adamc@667
|
204 }
|
adamc@667
|
205
|
adamc@682
|
206 pthread_mutex_lock(&c->lock);
|
adamc@667
|
207 c->mode = USED;
|
adam@2104
|
208 c->pass = my_rand();
|
adamc@682
|
209 c->sock = -1;
|
adamc@682
|
210 c->last_contact = time(NULL);
|
adam@1369
|
211 uw_buffer_reset(&c->msgs);
|
adamc@682
|
212 c->n_channels = 0;
|
adamc@685
|
213 c->refcount = 0;
|
adamc@1121
|
214 c->data = uw_init_client_data();
|
adamc@682
|
215 pthread_mutex_unlock(&c->lock);
|
adamc@682
|
216
|
adamc@682
|
217 c->next = clients_used;
|
adamc@682
|
218 clients_used = c;
|
adamc@667
|
219
|
adamc@667
|
220 pthread_mutex_unlock(&clients_mutex);
|
adamc@667
|
221
|
adamc@667
|
222 return c;
|
adamc@667
|
223 }
|
adamc@667
|
224
|
adamc@685
|
225 static void use_client(client *c) {
|
adamc@685
|
226 pthread_mutex_lock(&c->lock);
|
adamc@685
|
227 ++c->refcount;
|
adamc@685
|
228 pthread_mutex_unlock(&c->lock);
|
adamc@689
|
229 pthread_mutex_lock(&c->pull_lock);
|
adamc@685
|
230 }
|
adamc@685
|
231
|
adamc@685
|
232 static void release_client(client *c) {
|
adamc@689
|
233 pthread_mutex_unlock(&c->pull_lock);
|
adamc@685
|
234 pthread_mutex_lock(&c->lock);
|
adamc@685
|
235 --c->refcount;
|
adamc@685
|
236 pthread_mutex_unlock(&c->lock);
|
adamc@685
|
237 }
|
adamc@685
|
238
|
adamc@856
|
239 static const char begin_msgs[] = "Content-type: text/plain\r\n\r\n";
|
adam@1707
|
240 static pthread_t pruning_thread;
|
adam@1707
|
241 static int pruning_thread_initialized = 0;
|
adamc@667
|
242
|
adamc@682
|
243 static client *find_client(unsigned id) {
|
adamc@667
|
244 client *c;
|
adam@1708
|
245 int i_am_pruner = pruning_thread_initialized && pthread_equal(pruning_thread, pthread_self());
|
adam@1708
|
246
|
adam@1708
|
247 if (!i_am_pruner) pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
248
|
adamc@667
|
249 if (id >= n_clients) {
|
adam@1708
|
250 if (!i_am_pruner) pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
251 return NULL;
|
adamc@667
|
252 }
|
adamc@667
|
253
|
adamc@667
|
254 c = clients[id];
|
adamc@667
|
255
|
adam@1708
|
256 if (!i_am_pruner) pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
257 return c;
|
adamc@668
|
258 }
|
adamc@668
|
259
|
adamc@856
|
260 static char *on_success = "HTTP/1.1 200 OK\r\n";
|
adam@1409
|
261 static char *on_redirect = "HTTP/1.1 303 See Other\r\n";
|
adamc@856
|
262
|
adamc@856
|
263 void uw_set_on_success(char *s) {
|
adamc@856
|
264 on_success = s;
|
adamc@856
|
265 }
|
adamc@856
|
266
|
adam@1552
|
267 static void chastise(int (*send)(int sockfd, const void *buf, ssize_t len), int sock) {
|
adam@1552
|
268 send(sock, on_success, strlen(on_success));
|
adam@1552
|
269 send(sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adam@1552
|
270 send(sock, "R", 1);
|
adam@1552
|
271 close(sock);
|
adam@1552
|
272 }
|
adam@1552
|
273
|
adamc@863
|
274 void uw_client_connect(unsigned id, int pass, int sock,
|
adamc@863
|
275 int (*send)(int sockfd, const void *buf, ssize_t len),
|
adamc@864
|
276 int (*close)(int fd),
|
adamc@864
|
277 void *logger_data, uw_logger log_error) {
|
adamc@682
|
278 client *c = find_client(id);
|
adamc@668
|
279
|
adamc@668
|
280 if (c == NULL) {
|
adam@1552
|
281 chastise(send, sock);
|
adamc@864
|
282 log_error(logger_data, "Out-of-bounds client request (%u)\n", id);
|
adamc@667
|
283 return;
|
adamc@667
|
284 }
|
adamc@667
|
285
|
adamc@682
|
286 pthread_mutex_lock(&c->lock);
|
adamc@671
|
287
|
adamc@682
|
288 if (c->mode != USED) {
|
adamc@682
|
289 pthread_mutex_unlock(&c->lock);
|
adam@1552
|
290 chastise(send, sock);
|
adamc@864
|
291 log_error(logger_data, "Client request for unused slot (%u)\n", id);
|
adamc@667
|
292 return;
|
adamc@667
|
293 }
|
adamc@667
|
294
|
adamc@682
|
295 if (pass != c->pass) {
|
adamc@682
|
296 pthread_mutex_unlock(&c->lock);
|
adam@1552
|
297 chastise(send, sock);
|
adamc@864
|
298 log_error(logger_data, "Wrong client password (%u, %d)\n", id, pass);
|
adamc@682
|
299 return;
|
adamc@667
|
300 }
|
adamc@667
|
301
|
adamc@682
|
302 if (c->sock != -1) {
|
adamc@863
|
303 c->close(c->sock);
|
adamc@682
|
304 c->sock = -1;
|
adamc@682
|
305 }
|
adamc@667
|
306
|
adamc@682
|
307 c->last_contact = time(NULL);
|
adamc@682
|
308
|
adam@1369
|
309 if (uw_buffer_used(&c->msgs) > 0) {
|
adamc@863
|
310 send(sock, on_success, strlen(on_success));
|
adamc@863
|
311 send(sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adam@1369
|
312 send(sock, c->msgs.start, uw_buffer_used(&c->msgs));
|
adam@1369
|
313 uw_buffer_reset(&c->msgs);
|
adamc@667
|
314 close(sock);
|
adamc@667
|
315 }
|
adamc@863
|
316 else {
|
adamc@682
|
317 c->sock = sock;
|
adamc@863
|
318 c->send = send;
|
adamc@863
|
319 c->close = close;
|
adamc@863
|
320 }
|
adamc@667
|
321
|
adamc@682
|
322 pthread_mutex_unlock(&c->lock);
|
adamc@667
|
323 }
|
adamc@667
|
324
|
adamc@682
|
325 static void free_client(client *c) {
|
adamc@682
|
326 c->mode = UNUSED;
|
adamc@682
|
327 c->pass = -1;
|
adamc@667
|
328
|
adamc@682
|
329 c->next = clients_free;
|
adamc@682
|
330 clients_free = c;
|
adamc@667
|
331 }
|
adamc@667
|
332
|
adamc@682
|
333 static uw_Basis_channel new_channel(client *c) {
|
adamc@682
|
334 uw_Basis_channel ch = {c->id, c->n_channels++};
|
adamc@668
|
335 return ch;
|
adamc@668
|
336 }
|
adamc@668
|
337
|
adam@1447
|
338 static void client_send(client *c, uw_buffer *msg, const char *script, int script_len) {
|
adamc@685
|
339 pthread_mutex_lock(&c->lock);
|
adamc@671
|
340
|
adamc@682
|
341 if (c->sock != -1) {
|
adamc@863
|
342 c->send(c->sock, on_success, strlen(on_success));
|
adamc@863
|
343 c->send(c->sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adam@1447
|
344 if (script_len > 0) {
|
adam@1447
|
345 c->send(c->sock, "E\n", 2);
|
adam@1447
|
346 c->send(c->sock, script, script_len);
|
adam@1447
|
347 c->send(c->sock, "\n", 1);
|
adam@1447
|
348 }
|
adam@1369
|
349 c->send(c->sock, msg->start, uw_buffer_used(msg));
|
adamc@863
|
350 c->close(c->sock);
|
adamc@682
|
351 c->sock = -1;
|
adam@1665
|
352 } else if (uw_buffer_append(&c->msgs, msg->start, uw_buffer_used(msg)))
|
adamc@1113
|
353 fprintf(stderr, "Client message buffer size exceeded");
|
adamc@671
|
354
|
adamc@685
|
355 pthread_mutex_unlock(&c->lock);
|
adamc@668
|
356 }
|
adamc@668
|
357
|
adamc@668
|
358
|
adamc@668
|
359 // Global entry points
|
adamc@668
|
360
|
adamc@1164
|
361 extern void uw_global_custom();
|
adamc@1104
|
362 extern void uw_init_crypto();
|
adamc@1104
|
363
|
adamc@668
|
364 void uw_global_init() {
|
adamc@668
|
365 clients = malloc(0);
|
adamc@1104
|
366
|
adamc@1164
|
367 uw_global_custom();
|
adamc@1104
|
368 uw_init_crypto();
|
adamc@1094
|
369 }
|
adamc@1094
|
370
|
adamc@1094
|
371 void uw_app_init(uw_app *app) {
|
adamc@1094
|
372 app->client_init();
|
adamc@668
|
373 }
|
adamc@668
|
374
|
adam@1332
|
375 int uw_time = 0, uw_time_max = 0, uw_min_heap = 0;
|
adamc@1114
|
376
|
adamc@668
|
377
|
adamc@667
|
378 // Single-request state
|
adamc@667
|
379
|
adamc@323
|
380 typedef struct regions {
|
adamc@323
|
381 struct regions *next;
|
adamc@323
|
382 } regions;
|
adamc@323
|
383
|
adamc@425
|
384 typedef struct {
|
adamc@425
|
385 void (*func)(void*);
|
adamc@425
|
386 void *arg;
|
adamc@425
|
387 } cleanup;
|
adamc@425
|
388
|
adamc@671
|
389 typedef struct {
|
adamc@682
|
390 unsigned client;
|
adam@1369
|
391 uw_buffer msgs;
|
adamc@682
|
392 } delta;
|
adamc@671
|
393
|
adamc@737
|
394 typedef enum {
|
adamc@759
|
395 UNSET, NORMAL, FIL, SUBFORM, SUBFORMS, ENTRY
|
adamc@737
|
396 } input_kind;
|
adamc@737
|
397
|
adamc@756
|
398 typedef struct input {
|
adamc@737
|
399 input_kind kind;
|
adamc@737
|
400 union {
|
adamc@737
|
401 char *normal;
|
adamc@739
|
402 uw_Basis_file file;
|
adamc@756
|
403 struct {
|
adamc@759
|
404 struct input *fields, *parent;
|
adamc@756
|
405 } subform;
|
adamc@759
|
406 struct {
|
adamc@759
|
407 struct input *entries, *parent;
|
adamc@759
|
408 } subforms;
|
adamc@759
|
409 struct {
|
adamc@759
|
410 struct input *fields, *next, *parent;
|
adamc@759
|
411 } entry;
|
adamc@737
|
412 } data;
|
adamc@737
|
413 } input;
|
adamc@737
|
414
|
adamc@771
|
415 typedef struct {
|
adamc@771
|
416 void *data;
|
adam@1327
|
417 uw_callback commit, rollback;
|
adam@1327
|
418 uw_callback_with_retry free;
|
adamc@771
|
419 } transactional;
|
adamc@771
|
420
|
adamc@1061
|
421 typedef struct {
|
adamc@1061
|
422 char *name;
|
adamc@1061
|
423 void *data;
|
adamc@1061
|
424 void (*free)(void*);
|
adamc@1061
|
425 } global;
|
adamc@1061
|
426
|
ziv@2285
|
427 typedef struct uw_Sqlcache_Update {
|
ziv@2282
|
428 uw_Sqlcache_Cache *cache;
|
ziv@2282
|
429 char **keys;
|
ziv@2285
|
430 uw_Sqlcache_Value *value;
|
ziv@2285
|
431 struct uw_Sqlcache_Update *next;
|
ziv@2285
|
432 } uw_Sqlcache_Update;
|
ziv@2282
|
433
|
adamc@311
|
434 struct uw_context {
|
adamc@1094
|
435 uw_app *app;
|
adam@1446
|
436 int id;
|
adamc@1094
|
437
|
adamc@854
|
438 char *(*get_header)(void *, const char *);
|
adamc@854
|
439 void *get_header_data;
|
adamc@457
|
440
|
adam@1799
|
441 char *(*get_env)(void *, const char *);
|
adam@1799
|
442 void *get_env_data;
|
adam@1799
|
443
|
adam@1369
|
444 uw_buffer outHeaders, page, heap, script;
|
adam@1451
|
445 int allowed_to_return_indirectly, returning_indirectly;
|
adamc@759
|
446 input *inputs, *subinputs, *cur_container;
|
adamc@1094
|
447 size_t sz_inputs, n_subinputs, used_subinputs;
|
adamc@167
|
448
|
adam@1446
|
449 unsigned long long source_count;
|
adamc@562
|
450
|
adamc@272
|
451 void *db;
|
adam@1931
|
452 int transaction_started;
|
adamc@272
|
453
|
adamc@167
|
454 jmp_buf jmp_buf;
|
adamc@167
|
455
|
adamc@323
|
456 regions *regions;
|
adamc@323
|
457
|
adamc@425
|
458 cleanup *cleanup, *cleanup_front, *cleanup_back;
|
adamc@425
|
459
|
adamc@1094
|
460 const char *script_header;
|
adamc@643
|
461
|
adam@2056
|
462 int needs_push, needs_sig, could_write_db, at_most_one_query;
|
adamc@693
|
463
|
adamc@682
|
464 size_t n_deltas, used_deltas;
|
adamc@682
|
465 delta *deltas;
|
adamc@671
|
466
|
adamc@682
|
467 client *client;
|
adamc@682
|
468
|
adamc@771
|
469 transactional *transactionals;
|
adamc@771
|
470 size_t n_transactionals, used_transactionals;
|
adamc@771
|
471
|
adamc@1061
|
472 global *globals;
|
adamc@1061
|
473 size_t n_globals;
|
adamc@1061
|
474
|
adamc@1066
|
475 char *current_url;
|
adamc@1066
|
476
|
adamc@1114
|
477 int deadline;
|
adamc@1114
|
478
|
adamc@1121
|
479 void *client_data;
|
adamc@1121
|
480
|
grrwlf@1997
|
481 uw_loggers *loggers;
|
adam@1334
|
482
|
adam@1386
|
483 int isPost, hasPostBody;
|
adam@1347
|
484 uw_Basis_postBody postBody;
|
adam@1370
|
485 uw_Basis_string queryString;
|
adam@1347
|
486
|
adam@1556
|
487 unsigned nextId;
|
adam@1556
|
488
|
adam@1782
|
489 int amInitializing;
|
adam@1782
|
490
|
adamc@167
|
491 char error_message[ERROR_BUF_LEN];
|
adam@1877
|
492
|
adam@1877
|
493 int usedSig, needsResig;
|
adam@1949
|
494
|
adam@1949
|
495 char *output_buffer;
|
adam@1949
|
496 size_t output_buffer_size;
|
adam@2073
|
497
|
ziv@2281
|
498 // Sqlcache.
|
ziv@2262
|
499 int numRecording;
|
ziv@2262
|
500 int recordingOffset;
|
ziv@2285
|
501 uw_Sqlcache_Update *cacheUpdate;
|
ziv@2285
|
502 uw_Sqlcache_Update *cacheUpdateTail;
|
ziv@2220
|
503
|
adam@2073
|
504 int remoteSock;
|
adamc@117
|
505 };
|
adamc@117
|
506
|
adamc@1113
|
507 size_t uw_headers_max = SIZE_MAX;
|
adamc@1113
|
508 size_t uw_page_max = SIZE_MAX;
|
adamc@1113
|
509 size_t uw_heap_max = SIZE_MAX;
|
adamc@1113
|
510 size_t uw_script_max = SIZE_MAX;
|
adamc@1113
|
511
|
grrwlf@1997
|
512 uw_context uw_init(int id, uw_loggers *lg) {
|
ziv@2285
|
513 puts("Initializing");
|
adamc@311
|
514 uw_context ctx = malloc(sizeof(struct uw_context));
|
adamc@136
|
515
|
adamc@1094
|
516 ctx->app = NULL;
|
adam@1446
|
517 ctx->id = id;
|
adamc@1094
|
518
|
adamc@854
|
519 ctx->get_header = NULL;
|
adamc@854
|
520 ctx->get_header_data = NULL;
|
adamc@457
|
521
|
adam@1799
|
522 ctx->get_env = NULL;
|
adam@1799
|
523 ctx->get_env_data = NULL;
|
adam@1799
|
524
|
adam@1413
|
525 uw_buffer_init(uw_headers_max, &ctx->outHeaders, 1);
|
adam@1413
|
526 ctx->outHeaders.start[0] = 0;
|
adam@1413
|
527 uw_buffer_init(uw_page_max, &ctx->page, 1);
|
adam@1413
|
528 ctx->page.start[0] = 0;
|
adam@1451
|
529 ctx->allowed_to_return_indirectly = ctx->returning_indirectly = 0;
|
adam@1369
|
530 uw_buffer_init(uw_heap_max, &ctx->heap, uw_min_heap);
|
adam@1369
|
531 uw_buffer_init(uw_script_max, &ctx->script, 1);
|
adamc@667
|
532 ctx->script.start[0] = 0;
|
adamc@136
|
533
|
adamc@1094
|
534 ctx->inputs = malloc(0);
|
adamc@759
|
535 ctx->cur_container = NULL;
|
adamc@756
|
536 ctx->subinputs = malloc(0);
|
adamc@1094
|
537 ctx->sz_inputs = ctx->n_subinputs = ctx->used_subinputs = 0;
|
adamc@144
|
538
|
adamc@272
|
539 ctx->db = NULL;
|
adam@1931
|
540 ctx->transaction_started = 0;
|
adamc@272
|
541
|
adamc@323
|
542 ctx->regions = NULL;
|
adamc@323
|
543
|
adamc@425
|
544 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
|
adamc@425
|
545
|
adamc@643
|
546 ctx->script_header = "";
|
adamc@693
|
547 ctx->needs_push = 0;
|
adamc@736
|
548 ctx->needs_sig = 0;
|
adam@1936
|
549 ctx->could_write_db = 1;
|
adam@2056
|
550 ctx->at_most_one_query = 0;
|
adamc@1104
|
551
|
adamc@565
|
552 ctx->source_count = 0;
|
adamc@562
|
553
|
adamc@682
|
554 ctx->n_deltas = ctx->used_deltas = 0;
|
adamc@671
|
555 ctx->deltas = malloc(0);
|
adamc@671
|
556
|
adamc@700
|
557 ctx->client = NULL;
|
adamc@700
|
558
|
adamc@700
|
559 ctx->error_message[0] = 0;
|
adamc@700
|
560
|
adamc@771
|
561 ctx->transactionals = malloc(0);
|
adamc@771
|
562 ctx->n_transactionals = ctx->used_transactionals = 0;
|
adamc@771
|
563
|
adamc@1061
|
564 ctx->globals = malloc(0);
|
adamc@1061
|
565 ctx->n_globals = 0;
|
adamc@1061
|
566
|
adamc@1066
|
567 ctx->current_url = "";
|
adamc@1066
|
568
|
adamc@1114
|
569 ctx->deadline = INT_MAX;
|
adamc@1114
|
570
|
adamc@1121
|
571 ctx->client_data = uw_init_client_data();
|
adamc@1121
|
572
|
grrwlf@1997
|
573 ctx->loggers = lg;
|
adam@1334
|
574
|
adam@1386
|
575 ctx->isPost = ctx->hasPostBody = 0;
|
adam@1347
|
576
|
adam@1370
|
577 ctx->queryString = NULL;
|
adam@1370
|
578
|
adam@1556
|
579 ctx->nextId = 0;
|
adam@1556
|
580
|
adam@1782
|
581 ctx->amInitializing = 0;
|
adam@1782
|
582
|
adam@1877
|
583 ctx->usedSig = 0;
|
adam@1877
|
584 ctx->needsResig = 0;
|
adam@1877
|
585
|
adam@1949
|
586 ctx->output_buffer = malloc(1);
|
adam@1949
|
587 ctx->output_buffer_size = 1;
|
adam@1949
|
588
|
ziv@2262
|
589 ctx->numRecording = 0;
|
ziv@2262
|
590 ctx->recordingOffset = 0;
|
ziv@2285
|
591 ctx->cacheUpdate = NULL;
|
ziv@2285
|
592 ctx->cacheUpdateTail = NULL;
|
ziv@2204
|
593
|
adam@2073
|
594 ctx->remoteSock = -1;
|
adam@2073
|
595
|
adamc@117
|
596 return ctx;
|
adamc@106
|
597 }
|
adamc@106
|
598
|
adamc@1113
|
599 size_t uw_inputs_max = SIZE_MAX;
|
adamc@1113
|
600
|
adamc@1121
|
601 uw_app *uw_get_app(uw_context ctx) {
|
adamc@1121
|
602 return ctx->app;
|
adamc@1121
|
603 }
|
adamc@1121
|
604
|
adamc@1113
|
605 int uw_set_app(uw_context ctx, uw_app *app) {
|
adamc@1094
|
606 ctx->app = app;
|
adamc@1094
|
607
|
adamc@1105
|
608 if (app && app->inputs_len > ctx->sz_inputs) {
|
adamc@1113
|
609 if (app->inputs_len > uw_inputs_max)
|
adamc@1113
|
610 return 1;
|
adamc@1113
|
611
|
adamc@1094
|
612 ctx->sz_inputs = app->inputs_len;
|
adamc@1094
|
613 ctx->inputs = realloc(ctx->inputs, ctx->sz_inputs * sizeof(input));
|
adamc@1094
|
614 memset(ctx->inputs, 0, ctx->sz_inputs * sizeof(input));
|
adamc@1094
|
615 }
|
adamc@1134
|
616
|
adamc@1134
|
617 return 0;
|
adamc@1094
|
618 }
|
adamc@1094
|
619
|
adamc@1121
|
620 void uw_set_client_data(uw_context ctx, void *data) {
|
adamc@1121
|
621 uw_copy_client_data(ctx->client_data, data);
|
adamc@1121
|
622 }
|
adamc@1121
|
623
|
adamc@311
|
624 void uw_set_db(uw_context ctx, void *db) {
|
adamc@272
|
625 ctx->db = db;
|
adamc@272
|
626 }
|
adamc@272
|
627
|
adamc@311
|
628 void *uw_get_db(uw_context ctx) {
|
adamc@272
|
629 return ctx->db;
|
adamc@272
|
630 }
|
adamc@272
|
631
|
grrwlf@1997
|
632
|
grrwlf@1997
|
633 uw_loggers* uw_get_loggers(struct uw_context *ctx) {
|
grrwlf@1997
|
634 return ctx->loggers;
|
grrwlf@1997
|
635 }
|
grrwlf@1997
|
636
|
adamc@311
|
637 void uw_free(uw_context ctx) {
|
adamc@671
|
638 size_t i;
|
adamc@671
|
639
|
adam@1369
|
640 uw_buffer_free(&ctx->outHeaders);
|
adam@1369
|
641 uw_buffer_free(&ctx->script);
|
adam@1369
|
642 uw_buffer_free(&ctx->page);
|
adam@1369
|
643 uw_buffer_free(&ctx->heap);
|
adamc@144
|
644 free(ctx->inputs);
|
adamc@756
|
645 free(ctx->subinputs);
|
adamc@425
|
646 free(ctx->cleanup);
|
adamc@771
|
647 free(ctx->transactionals);
|
adamc@1121
|
648 uw_free_client_data(ctx->client_data);
|
adamc@671
|
649
|
adamc@682
|
650 for (i = 0; i < ctx->n_deltas; ++i)
|
adam@1369
|
651 uw_buffer_free(&ctx->deltas[i].msgs);
|
marco-oweber@1330
|
652 free(ctx->deltas);
|
adamc@671
|
653
|
adamc@1061
|
654 for (i = 0; i < ctx->n_globals; ++i)
|
adamc@1089
|
655 if (ctx->globals[i].free)
|
adamc@1089
|
656 ctx->globals[i].free(ctx->globals[i].data);
|
marco-oweber@1330
|
657 free(ctx->globals);
|
adamc@1061
|
658
|
adam@1949
|
659 free(ctx->output_buffer);
|
adam@1949
|
660
|
adamc@136
|
661 free(ctx);
|
adamc@136
|
662 }
|
adamc@136
|
663
|
adamc@562
|
664 void uw_reset_keep_error_message(uw_context ctx) {
|
adam@1369
|
665 uw_buffer_reset(&ctx->outHeaders);
|
adam@1369
|
666 uw_buffer_reset(&ctx->script);
|
adamc@667
|
667 ctx->script.start[0] = 0;
|
adam@1369
|
668 uw_buffer_reset(&ctx->page);
|
adam@1451
|
669 ctx->allowed_to_return_indirectly = ctx->returning_indirectly = 0;
|
adam@1369
|
670 uw_buffer_reset(&ctx->heap);
|
adamc@323
|
671 ctx->regions = NULL;
|
adamc@425
|
672 ctx->cleanup_front = ctx->cleanup;
|
adamc@682
|
673 ctx->used_deltas = 0;
|
adamc@682
|
674 ctx->client = NULL;
|
adamc@759
|
675 ctx->cur_container = NULL;
|
adamc@771
|
676 ctx->used_transactionals = 0;
|
adamc@1117
|
677 ctx->script_header = "";
|
adam@1370
|
678 ctx->queryString = NULL;
|
adam@1556
|
679 ctx->nextId = 0;
|
adam@1782
|
680 ctx->amInitializing = 0;
|
adam@1877
|
681 ctx->usedSig = 0;
|
adam@1877
|
682 ctx->needsResig = 0;
|
adam@2073
|
683 ctx->remoteSock = -1;
|
adamc@167
|
684 }
|
adamc@167
|
685
|
adamc@562
|
686 void uw_reset_keep_request(uw_context ctx) {
|
adamc@562
|
687 uw_reset_keep_error_message(ctx);
|
adamc@562
|
688 ctx->error_message[0] = 0;
|
adamc@167
|
689 }
|
adamc@167
|
690
|
adamc@311
|
691 void uw_reset(uw_context ctx) {
|
adamc@311
|
692 uw_reset_keep_request(ctx);
|
adamc@1105
|
693 if (ctx->app)
|
adamc@1105
|
694 memset(ctx->inputs, 0, ctx->app->inputs_len * sizeof(input));
|
adamc@756
|
695 memset(ctx->subinputs, 0, ctx->n_subinputs * sizeof(input));
|
adam@1461
|
696 ctx->used_subinputs = ctx->hasPostBody = ctx->isPost = 0;
|
adam@1943
|
697 ctx->transaction_started = 0;
|
adamc@144
|
698 }
|
adamc@144
|
699
|
adamc@311
|
700 failure_kind uw_begin_init(uw_context ctx) {
|
adamc@272
|
701 int r = setjmp(ctx->jmp_buf);
|
adamc@272
|
702
|
adamc@1105
|
703 if (r == 0 && ctx->app)
|
adamc@1094
|
704 ctx->app->db_init(ctx);
|
adamc@272
|
705
|
adamc@272
|
706 return r;
|
adamc@272
|
707 }
|
adamc@272
|
708
|
adamc@1118
|
709 void uw_close(uw_context ctx) {
|
adamc@1118
|
710 ctx->app->db_close(ctx);
|
adamc@1118
|
711 }
|
adamc@1118
|
712
|
adam@1446
|
713 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
|
adam@1446
|
714 return ctx->get_header(ctx->get_header_data, h);
|
adam@1446
|
715 }
|
adam@1446
|
716
|
adamc@854
|
717 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) {
|
adamc@854
|
718 ctx->get_header = get_header;
|
adamc@854
|
719 ctx->get_header_data = get_header_data;
|
adamc@742
|
720 }
|
adamc@742
|
721
|
adam@1799
|
722 void uw_set_env(uw_context ctx, char *(*get_env)(void *, const char *), void *get_env_data) {
|
adam@1799
|
723 ctx->get_env = get_env;
|
adam@1799
|
724 ctx->get_env_data = get_env_data;
|
adam@1799
|
725 }
|
adam@1799
|
726
|
adamc@801
|
727 static void uw_set_error(uw_context ctx, const char *fmt, ...) {
|
adamc@801
|
728 va_list ap;
|
adamc@801
|
729 va_start(ap, fmt);
|
adamc@801
|
730
|
adamc@801
|
731 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
|
adamc@801
|
732 }
|
adamc@801
|
733
|
adamc@1131
|
734 int uw_has_error(uw_context ctx) {
|
adamc@1131
|
735 return ctx->error_message[0] != 0;
|
adamc@1131
|
736 }
|
adamc@1131
|
737
|
adamc@311
|
738 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
|
adamc@425
|
739 cleanup *cl;
|
adamc@425
|
740
|
adamc@167
|
741 va_list ap;
|
adamc@167
|
742 va_start(ap, fmt);
|
adamc@167
|
743
|
adamc@167
|
744 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
|
adamc@167
|
745
|
adamc@425
|
746 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@425
|
747 cl->func(cl->arg);
|
adamc@425
|
748
|
adamc@425
|
749 ctx->cleanup_front = ctx->cleanup;
|
adamc@425
|
750
|
adamc@190
|
751 longjmp(ctx->jmp_buf, fk);
|
adamc@167
|
752 }
|
adamc@167
|
753
|
adamc@1113
|
754 size_t uw_cleanup_max = SIZE_MAX;
|
adamc@1113
|
755
|
adamc@425
|
756 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
|
adamc@425
|
757 if (ctx->cleanup_front >= ctx->cleanup_back) {
|
adamc@425
|
758 int len = ctx->cleanup_back - ctx->cleanup, newLen;
|
adamc@425
|
759 if (len == 0)
|
adamc@425
|
760 newLen = 1;
|
adamc@425
|
761 else
|
adamc@428
|
762 newLen = len * 2;
|
adamc@1113
|
763
|
adamc@1134
|
764 if (newLen > uw_cleanup_max) {
|
adamc@1113
|
765 if (len+1 <= uw_cleanup_max)
|
adamc@1113
|
766 newLen = uw_cleanup_max;
|
adamc@1113
|
767 else
|
adamc@1113
|
768 uw_error(ctx, FATAL, "Exceeded limit on number of cleanup handlers");
|
adamc@1134
|
769 }
|
adamc@1113
|
770
|
adamc@470
|
771 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
|
adamc@425
|
772 ctx->cleanup_front = ctx->cleanup + len;
|
adamc@425
|
773 ctx->cleanup_back = ctx->cleanup + newLen;
|
adamc@425
|
774 }
|
adamc@425
|
775
|
adamc@425
|
776 ctx->cleanup_front->func = func;
|
adamc@425
|
777 ctx->cleanup_front->arg = arg;
|
adamc@425
|
778 ++ctx->cleanup_front;
|
adamc@425
|
779 }
|
adamc@425
|
780
|
adam@1444
|
781 char *uw_Basis_htmlifyString(uw_context, const char *);
|
adam@1444
|
782
|
adamc@682
|
783 void uw_login(uw_context ctx) {
|
adam@1942
|
784 char *id_s, *pass_s;
|
adam@1942
|
785
|
adam@1942
|
786 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client"))
|
adam@1942
|
787 && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) {
|
adam@1942
|
788 unsigned id = atoi(id_s);
|
adam@1942
|
789 int pass = atoi(pass_s);
|
adam@1942
|
790 client *c = find_client(id);
|
adam@1942
|
791
|
adam@1942
|
792 if (c == NULL)
|
adam@1942
|
793 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", uw_Basis_htmlifyString(ctx, id_s), uw_Basis_htmlifyString(ctx, pass_s));
|
adam@1942
|
794 else {
|
adamc@685
|
795 use_client(c);
|
adamc@682
|
796 ctx->client = c;
|
adam@1942
|
797
|
adam@1942
|
798 if (c->mode != USED)
|
adam@1942
|
799 uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id);
|
adam@1942
|
800 if (c->pass != pass)
|
adam@1942
|
801 uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass);
|
adamc@864
|
802 }
|
adam@1942
|
803 } else if (ctx->needs_push) {
|
adam@1942
|
804 client *c = new_client();
|
adam@1942
|
805
|
adam@1942
|
806 if (c == NULL)
|
adam@1942
|
807 uw_error(ctx, FATAL, "Limit exceeded on number of message-passing clients");
|
adam@1942
|
808
|
adam@1942
|
809 use_client(c);
|
adam@1942
|
810 uw_copy_client_data(c->data, ctx->client_data);
|
adam@1942
|
811 ctx->client = c;
|
adamc@682
|
812 }
|
adamc@682
|
813 }
|
adamc@682
|
814
|
adamc@671
|
815 failure_kind uw_begin(uw_context ctx, char *path) {
|
adamc@671
|
816 int r = setjmp(ctx->jmp_buf);
|
adamc@671
|
817
|
adam@1931
|
818 if (r == 0)
|
adam@1931
|
819 ctx->app->handle(ctx, path);
|
adam@1931
|
820
|
adam@1931
|
821 return r;
|
adam@1931
|
822 }
|
adam@1931
|
823
|
adam@2176
|
824 static void uw_try_reconnecting(uw_context ctx) {
|
adam@2176
|
825 // Hm, error starting transaction.
|
adam@2176
|
826 // Maybe the database server died but has since come back up.
|
adam@2176
|
827 // Let's try starting from scratch.
|
adam@2176
|
828 if (ctx->db) {
|
adam@2176
|
829 ctx->app->db_close(ctx);
|
adam@2176
|
830 ctx->db = NULL;
|
adam@2176
|
831 }
|
adam@2176
|
832 ctx->app->db_init(ctx);
|
adam@2176
|
833 }
|
adam@2176
|
834
|
adam@2177
|
835 void uw_try_reconnecting_and_restarting(uw_context ctx) {
|
adam@2177
|
836 uw_try_reconnecting(ctx);
|
adam@2178
|
837 uw_error(ctx, BOUNDED_RETRY, "Restarting transaction after fixing database connection");
|
adam@2176
|
838 }
|
adam@2176
|
839
|
adam@1931
|
840 void uw_ensure_transaction(uw_context ctx) {
|
adam@2056
|
841 if (!ctx->transaction_started && !ctx->at_most_one_query) {
|
adam@2176
|
842 if (!ctx->db || ctx->app->db_begin(ctx, ctx->could_write_db)) {
|
adam@2176
|
843 uw_try_reconnecting(ctx);
|
adam@2176
|
844
|
adam@2176
|
845 if (ctx->app->db_begin(ctx, ctx->could_write_db))
|
adam@2176
|
846 uw_error(ctx, FATAL, "Error running SQL BEGIN");
|
adam@2176
|
847 }
|
adam@2176
|
848
|
adam@1931
|
849 ctx->transaction_started = 1;
|
adam@2178
|
850 } else if (ctx->at_most_one_query && !ctx->db)
|
adam@2178
|
851 uw_try_reconnecting(ctx);
|
adamc@671
|
852 }
|
adamc@671
|
853
|
adamc@772
|
854 uw_Basis_client uw_Basis_self(uw_context ctx) {
|
adamc@682
|
855 if (ctx->client == NULL)
|
adamc@682
|
856 uw_error(ctx, FATAL, "Call to Basis.self() from page that has only server-side code");
|
adamc@682
|
857
|
adamc@682
|
858 return ctx->client->id;
|
adamc@682
|
859 }
|
adamc@682
|
860
|
adamc@425
|
861 void uw_pop_cleanup(uw_context ctx) {
|
adamc@425
|
862 if (ctx->cleanup_front == ctx->cleanup)
|
adamc@425
|
863 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
|
adamc@425
|
864
|
adamc@425
|
865 --ctx->cleanup_front;
|
adamc@425
|
866 ctx->cleanup_front->func(ctx->cleanup_front->arg);
|
adamc@425
|
867 }
|
adamc@425
|
868
|
adamc@311
|
869 char *uw_error_message(uw_context ctx) {
|
adamc@167
|
870 return ctx->error_message;
|
adamc@167
|
871 }
|
adamc@167
|
872
|
adamc@1115
|
873 void uw_set_error_message(uw_context ctx, const char *msg) {
|
adamc@1115
|
874 strncpy(ctx->error_message, msg, sizeof(ctx->error_message));
|
adamc@1115
|
875 ctx->error_message[sizeof(ctx->error_message)-1] = 0;
|
adamc@1115
|
876 }
|
adamc@1115
|
877
|
adamc@759
|
878 static input *INP(uw_context ctx) {
|
adamc@759
|
879 if (ctx->cur_container == NULL)
|
adamc@759
|
880 return ctx->inputs;
|
adamc@759
|
881 else if (ctx->cur_container->kind == SUBFORM)
|
adamc@759
|
882 return ctx->cur_container->data.subform.fields;
|
adamc@759
|
883 else if (ctx->cur_container->kind == ENTRY)
|
adamc@759
|
884 return ctx->cur_container->data.entry.fields;
|
adamc@759
|
885 else
|
adamc@760
|
886 uw_error(ctx, FATAL, "INP: Wrong kind (%d, %p)", ctx->cur_container->kind, ctx->cur_container);
|
adamc@759
|
887 }
|
adamc@759
|
888
|
adamc@760
|
889 static void adjust_pointer(input **ptr, input *old_start, input *new_start, size_t len) {
|
adamc@760
|
890 if (*ptr != NULL && *ptr >= old_start && *ptr < old_start + len)
|
adamc@760
|
891 *ptr += new_start - old_start;
|
adamc@760
|
892 }
|
adamc@760
|
893
|
adamc@760
|
894 static void adjust_input(input *x, input *old_start, input *new_start, size_t len) {
|
adamc@759
|
895 switch (x->kind) {
|
adamc@759
|
896 case SUBFORM:
|
adamc@760
|
897 adjust_pointer(&x->data.subform.fields, old_start, new_start, len);
|
adamc@760
|
898 adjust_pointer(&x->data.subform.parent, old_start, new_start, len);
|
adamc@759
|
899 break;
|
adamc@759
|
900 case SUBFORMS:
|
adamc@760
|
901 adjust_pointer(&x->data.subforms.entries, old_start, new_start, len);
|
adamc@760
|
902 adjust_pointer(&x->data.subforms.parent, old_start, new_start, len);
|
adamc@759
|
903 break;
|
adamc@759
|
904 case ENTRY:
|
adamc@760
|
905 adjust_pointer(&x->data.entry.fields, old_start, new_start, len);
|
adamc@760
|
906 adjust_pointer(&x->data.entry.next, old_start, new_start, len);
|
adamc@760
|
907 adjust_pointer(&x->data.entry.parent, old_start, new_start, len);
|
adamc@1134
|
908 break;
|
adamc@1134
|
909 default:
|
adamc@1134
|
910 break;
|
phurst@1981
|
911 }
|
adamc@759
|
912 }
|
adamc@759
|
913
|
adamc@1113
|
914 size_t uw_subinputs_max = SIZE_MAX;
|
adamc@1113
|
915
|
adamc@759
|
916 static input *check_input_space(uw_context ctx, size_t len) {
|
adamc@759
|
917 size_t i;
|
adamc@759
|
918 input *r;
|
adamc@759
|
919
|
adamc@759
|
920 if (ctx->used_subinputs + len >= ctx->n_subinputs) {
|
adamc@1113
|
921 if (ctx->used_subinputs + len > uw_subinputs_max)
|
adamc@1113
|
922 uw_error(ctx, FATAL, "Exceeded limit on number of subinputs");
|
adamc@1113
|
923
|
adamc@759
|
924 input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len));
|
adamc@759
|
925
|
adamc@760
|
926 if (ctx->subinputs != new_subinputs) {
|
adamc@760
|
927 for (i = 0; i < ctx->used_subinputs; ++i)
|
adamc@760
|
928 adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
|
adamc@1094
|
929 for (i = 0; i < ctx->app->inputs_len; ++i)
|
adamc@760
|
930 adjust_input(&ctx->inputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
|
adamc@760
|
931
|
adamc@760
|
932 adjust_pointer(&ctx->cur_container, ctx->subinputs, new_subinputs, ctx->used_subinputs);
|
adamc@760
|
933
|
adamc@760
|
934 ctx->n_subinputs = ctx->used_subinputs + len;
|
adamc@760
|
935 ctx->subinputs = new_subinputs;
|
adamc@760
|
936 }
|
adamc@759
|
937 }
|
adamc@759
|
938
|
adamc@759
|
939 r = &ctx->subinputs[ctx->used_subinputs];
|
adamc@759
|
940
|
adamc@759
|
941 for (i = 0; i < len; ++i)
|
adam@1474
|
942 ctx->subinputs[ctx->used_subinputs++].kind = UNSET;
|
adamc@759
|
943
|
adamc@759
|
944 return r;
|
adamc@759
|
945 }
|
adamc@756
|
946
|
adamc@801
|
947 int uw_set_input(uw_context ctx, const char *name, char *value) {
|
adamc@1050
|
948 //printf("Input name %s\n", name);
|
adamc@1038
|
949
|
adamc@756
|
950 if (!strcasecmp(name, ".b")) {
|
adamc@1094
|
951 int n = ctx->app->input_num(value);
|
adamc@756
|
952 input *inps;
|
adamc@756
|
953
|
adamc@801
|
954 if (n < 0) {
|
adam@1444
|
955 uw_set_error(ctx, "Bad subform name %s", uw_Basis_htmlifyString(ctx, value));
|
adamc@801
|
956 return -1;
|
adamc@801
|
957 }
|
adamc@801
|
958
|
adamc@1094
|
959 if (n >= ctx->app->inputs_len) {
|
adam@1444
|
960 uw_set_error(ctx, "For subform name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, value), n);
|
adamc@801
|
961 return -1;
|
adamc@801
|
962 }
|
adamc@756
|
963
|
adamc@1094
|
964 inps = check_input_space(ctx, ctx->app->inputs_len);
|
adamc@759
|
965
|
adamc@759
|
966 INP(ctx)[n].kind = SUBFORM;
|
adamc@759
|
967 INP(ctx)[n].data.subform.parent = ctx->cur_container;
|
adamc@759
|
968 INP(ctx)[n].data.subform.fields = inps;
|
adamc@759
|
969 ctx->cur_container = &INP(ctx)[n];
|
adamc@756
|
970 } else if (!strcasecmp(name, ".e")) {
|
adamc@756
|
971 input *tmp;
|
adamc@756
|
972
|
adamc@801
|
973 if (ctx->cur_container == NULL) {
|
adamc@801
|
974 uw_set_error(ctx, "Unmatched subform closer");
|
adamc@801
|
975 return -1;
|
adamc@801
|
976 }
|
adamc@756
|
977
|
adamc@759
|
978 tmp = ctx->cur_container;
|
adamc@759
|
979 switch (tmp->kind) {
|
adamc@759
|
980 case SUBFORM:
|
adamc@759
|
981 ctx->cur_container = tmp->data.subform.parent;
|
adamc@759
|
982 tmp->data.subform.parent = NULL;
|
adamc@759
|
983 break;
|
adamc@759
|
984 case SUBFORMS:
|
adamc@759
|
985 ctx->cur_container = tmp->data.subforms.parent;
|
adamc@759
|
986 tmp->data.subforms.parent = NULL;
|
adamc@759
|
987 break;
|
adamc@759
|
988 case ENTRY:
|
adamc@759
|
989 ctx->cur_container = tmp->data.entry.parent;
|
adamc@759
|
990 break;
|
adamc@759
|
991 default:
|
adamc@801
|
992 uw_set_error(ctx, "uw_set_input: Wrong kind");
|
adamc@801
|
993 return -1;
|
adamc@759
|
994 }
|
adamc@759
|
995 } else if (!strcasecmp(name, ".s")) {
|
adamc@1094
|
996 int n = ctx->app->input_num(value);
|
adamc@759
|
997
|
adamc@801
|
998 if (n < 0) {
|
adam@1444
|
999 uw_set_error(ctx, "Bad subforms name %s", uw_Basis_htmlifyString(ctx, value));
|
adamc@801
|
1000 return -1;
|
adamc@801
|
1001 }
|
adamc@801
|
1002
|
adamc@1094
|
1003 if (n >= ctx->app->inputs_len) {
|
adam@1444
|
1004 uw_set_error(ctx, "For subforms name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, value), n);
|
adamc@801
|
1005 return -1;
|
adamc@801
|
1006 }
|
adamc@759
|
1007
|
adamc@759
|
1008 INP(ctx)[n].kind = SUBFORMS;
|
adamc@759
|
1009 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
|
adamc@759
|
1010 INP(ctx)[n].data.subforms.entries = NULL;
|
adamc@759
|
1011 ctx->cur_container = &INP(ctx)[n];
|
adamc@759
|
1012 } else if (!strcasecmp(name, ".i")) {
|
adamc@759
|
1013 input *inps;
|
adamc@759
|
1014
|
adamc@801
|
1015 if (!ctx->cur_container) {
|
adamc@801
|
1016 uw_set_error(ctx, "New entry without container");
|
adamc@801
|
1017 return -1;
|
adamc@801
|
1018 }
|
adamc@801
|
1019
|
adamc@801
|
1020 if (ctx->cur_container->kind != SUBFORMS) {
|
adamc@801
|
1021 uw_set_error(ctx, "Bad kind for entry parent");
|
adamc@801
|
1022 return -1;
|
adamc@801
|
1023 }
|
adamc@759
|
1024
|
adamc@1094
|
1025 inps = check_input_space(ctx, ctx->app->inputs_len + 1);
|
adamc@759
|
1026
|
adamc@759
|
1027 inps->kind = ENTRY;
|
adamc@759
|
1028 inps->data.entry.parent = ctx->cur_container;
|
adamc@759
|
1029 inps->data.entry.next = ctx->cur_container->data.subforms.entries;
|
adamc@759
|
1030 ctx->cur_container->data.subforms.entries = inps;
|
adamc@759
|
1031
|
adamc@759
|
1032 inps->data.entry.fields = inps+1;
|
adamc@759
|
1033 ctx->cur_container = inps;
|
adamc@756
|
1034 } else {
|
adamc@1094
|
1035 int n = ctx->app->input_num(name);
|
adamc@756
|
1036
|
adam@1384
|
1037 if (n < 0)
|
adam@1384
|
1038 return 0;
|
adamc@801
|
1039
|
adamc@1094
|
1040 if (n >= ctx->app->inputs_len) {
|
adam@1444
|
1041 uw_set_error(ctx, "For input name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, name), n);
|
adamc@801
|
1042 return -1;
|
adamc@801
|
1043 }
|
adamc@756
|
1044
|
adamc@756
|
1045 INP(ctx)[n].kind = NORMAL;
|
adamc@756
|
1046 INP(ctx)[n].data.normal = value;
|
adamc@756
|
1047 }
|
adamc@801
|
1048
|
adamc@801
|
1049 return 0;
|
adamc@144
|
1050 }
|
adamc@144
|
1051
|
adamc@311
|
1052 char *uw_get_input(uw_context ctx, int n) {
|
adamc@169
|
1053 if (n < 0)
|
adamc@311
|
1054 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@1094
|
1055 if (n >= ctx->app->inputs_len)
|
adamc@311
|
1056 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@737
|
1057
|
adamc@756
|
1058 switch (INP(ctx)[n].kind) {
|
adamc@737
|
1059 case UNSET:
|
adamc@737
|
1060 return NULL;
|
adamc@739
|
1061 case FIL:
|
adamc@739
|
1062 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
|
adamc@756
|
1063 case SUBFORM:
|
adamc@756
|
1064 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
|
adamc@759
|
1065 case SUBFORMS:
|
adamc@759
|
1066 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
|
adamc@759
|
1067 case ENTRY:
|
adamc@759
|
1068 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
|
adamc@737
|
1069 case NORMAL:
|
adamc@756
|
1070 return INP(ctx)[n].data.normal;
|
adamc@737
|
1071 default:
|
adamc@737
|
1072 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
1073 }
|
adamc@138
|
1074 }
|
adamc@138
|
1075
|
adamc@311
|
1076 char *uw_get_optional_input(uw_context ctx, int n) {
|
adamc@190
|
1077 if (n < 0)
|
adamc@311
|
1078 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@1094
|
1079 if (n >= ctx->app->inputs_len)
|
adamc@311
|
1080 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@737
|
1081
|
adamc@756
|
1082 switch (INP(ctx)[n].kind) {
|
adamc@737
|
1083 case UNSET:
|
adamc@737
|
1084 return "";
|
adamc@739
|
1085 case FIL:
|
adamc@739
|
1086 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
|
adamc@756
|
1087 case SUBFORM:
|
adamc@756
|
1088 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
|
adamc@759
|
1089 case SUBFORMS:
|
adamc@759
|
1090 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
|
adamc@759
|
1091 case ENTRY:
|
adamc@759
|
1092 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
|
adamc@737
|
1093 case NORMAL:
|
adamc@756
|
1094 return INP(ctx)[n].data.normal;
|
adamc@737
|
1095 default:
|
adamc@737
|
1096 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
1097 }
|
adamc@737
|
1098 }
|
adamc@737
|
1099
|
adamc@801
|
1100 int uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) {
|
adamc@1094
|
1101 int n = ctx->app->input_num(name);
|
adamc@737
|
1102
|
adamc@801
|
1103 if (n < 0) {
|
adam@2054
|
1104 uw_set_error(ctx, "Bad file input name");
|
adamc@801
|
1105 return -1;
|
adamc@801
|
1106 }
|
adamc@801
|
1107
|
adamc@1094
|
1108 if (n >= ctx->app->inputs_len) {
|
adam@2054
|
1109 uw_set_error(ctx, "For file input name, index %d is out of range", n);
|
adamc@801
|
1110 return -1;
|
adamc@801
|
1111 }
|
adamc@737
|
1112
|
adamc@739
|
1113 ctx->inputs[n].kind = FIL;
|
adamc@739
|
1114 ctx->inputs[n].data.file = f;
|
adamc@801
|
1115
|
adamc@801
|
1116 return 0;
|
adamc@737
|
1117 }
|
adamc@737
|
1118
|
adamc@739
|
1119 void *uw_malloc(uw_context ctx, size_t len);
|
adamc@739
|
1120
|
adamc@739
|
1121 uw_Basis_file uw_get_file_input(uw_context ctx, int n) {
|
adamc@737
|
1122 if (n < 0)
|
adamc@737
|
1123 uw_error(ctx, FATAL, "Negative file input index %d", n);
|
adamc@1094
|
1124 if (n >= ctx->app->inputs_len)
|
adamc@737
|
1125 uw_error(ctx, FATAL, "Out-of-bounds file input index %d", n);
|
adamc@737
|
1126
|
adamc@756
|
1127 switch (INP(ctx)[n].kind) {
|
adamc@737
|
1128 case UNSET:
|
adamc@737
|
1129 {
|
adamc@739
|
1130 char *data = uw_malloc(ctx, 0);
|
adamc@740
|
1131 uw_Basis_file f = {NULL, "", {0, data}};
|
adamc@739
|
1132 return f;
|
adamc@737
|
1133 }
|
adamc@739
|
1134 case FIL:
|
adamc@756
|
1135 return INP(ctx)[n].data.file;
|
adamc@737
|
1136 case NORMAL:
|
adamc@737
|
1137 uw_error(ctx, FATAL, "Tried to read a normal form input as files");
|
adamc@756
|
1138 case SUBFORM:
|
adamc@756
|
1139 uw_error(ctx, FATAL, "Tried to read a subform form input as files");
|
adamc@759
|
1140 case SUBFORMS:
|
adamc@759
|
1141 uw_error(ctx, FATAL, "Tried to read a subforms form input as files");
|
adamc@759
|
1142 case ENTRY:
|
adamc@759
|
1143 uw_error(ctx, FATAL, "Tried to read an entry form input as files");
|
adamc@737
|
1144 default:
|
adamc@737
|
1145 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
1146 }
|
adamc@190
|
1147 }
|
adamc@190
|
1148
|
adamc@756
|
1149 void uw_enter_subform(uw_context ctx, int n) {
|
adamc@756
|
1150 if (n < 0)
|
adamc@756
|
1151 uw_error(ctx, FATAL, "Negative subform index %d", n);
|
adamc@1094
|
1152 if (n >= ctx->app->inputs_len)
|
adamc@756
|
1153 uw_error(ctx, FATAL, "Out-of-bounds subform index %d", n);
|
adamc@756
|
1154
|
adamc@756
|
1155 switch (INP(ctx)[n].kind) {
|
adamc@756
|
1156 case UNSET:
|
adamc@756
|
1157 uw_error(ctx, FATAL, "Missing subform");
|
adamc@756
|
1158 case FIL:
|
adamc@756
|
1159 uw_error(ctx, FATAL, "Tried to read a file form input as subform");
|
adamc@756
|
1160 case NORMAL:
|
adamc@756
|
1161 uw_error(ctx, FATAL, "Tried to read a normal form input as subform");
|
adamc@759
|
1162 case SUBFORMS:
|
adamc@759
|
1163 uw_error(ctx, FATAL, "Tried to read a subforms form input as subform");
|
adamc@759
|
1164 case ENTRY:
|
adamc@759
|
1165 uw_error(ctx, FATAL, "Tried to read an entry form input as subform");
|
adamc@756
|
1166 case SUBFORM:
|
adamc@759
|
1167 INP(ctx)[n].data.subform.parent = ctx->cur_container;
|
adamc@760
|
1168 ctx->cur_container = &INP(ctx)[n];
|
adamc@756
|
1169 return;
|
adamc@756
|
1170 default:
|
adamc@756
|
1171 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@756
|
1172 }
|
adamc@756
|
1173 }
|
adamc@756
|
1174
|
adamc@756
|
1175 void uw_leave_subform(uw_context ctx) {
|
adamc@756
|
1176 input *tmp;
|
adamc@756
|
1177
|
adamc@759
|
1178 if (ctx->cur_container == NULL)
|
adamc@756
|
1179 uw_error(ctx, FATAL, "Unmatched uw_leave_subform");
|
adamc@756
|
1180
|
adamc@759
|
1181 tmp = ctx->cur_container;
|
adamc@759
|
1182 ctx->cur_container = tmp->data.subform.parent;
|
adamc@759
|
1183 tmp->data.subform.parent = NULL;
|
adamc@759
|
1184 }
|
adamc@759
|
1185
|
adamc@759
|
1186 int uw_enter_subforms(uw_context ctx, int n) {
|
adamc@759
|
1187 input *inps;
|
adamc@759
|
1188
|
adamc@759
|
1189 if (n < 0)
|
adamc@759
|
1190 uw_error(ctx, FATAL, "Negative subforms index %d", n);
|
adamc@1094
|
1191 if (n >= ctx->app->inputs_len)
|
adamc@759
|
1192 uw_error(ctx, FATAL, "Out-of-bounds subforms index %d", n);
|
adamc@759
|
1193
|
adamc@759
|
1194 switch (INP(ctx)[n].kind) {
|
adamc@759
|
1195 case UNSET:
|
adamc@759
|
1196 uw_error(ctx, FATAL, "Missing subforms");
|
adamc@759
|
1197 case FIL:
|
adamc@759
|
1198 uw_error(ctx, FATAL, "Tried to read a file form input as subforms");
|
adamc@759
|
1199 case NORMAL:
|
adamc@759
|
1200 uw_error(ctx, FATAL, "Tried to read a normal form input %p as subforms", &INP(ctx)[n]);
|
adamc@759
|
1201 case SUBFORM:
|
adamc@759
|
1202 uw_error(ctx, FATAL, "Tried to read a subform form input as subforms");
|
adamc@759
|
1203 case ENTRY:
|
adamc@759
|
1204 uw_error(ctx, FATAL, "Tried to read an entry form input as subforms");
|
adamc@759
|
1205 case SUBFORMS:
|
adamc@759
|
1206 inps = INP(ctx)[n].data.subforms.entries;
|
adamc@759
|
1207 if (inps) {
|
adamc@759
|
1208 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
|
adamc@759
|
1209 ctx->cur_container = INP(ctx)[n].data.subforms.entries;
|
adamc@759
|
1210 return 1;
|
adamc@759
|
1211 } else
|
adamc@759
|
1212 return 0;
|
adamc@759
|
1213 default:
|
adamc@759
|
1214 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@759
|
1215 }
|
adamc@759
|
1216 }
|
adamc@759
|
1217
|
adamc@759
|
1218 int uw_next_entry(uw_context ctx) {
|
adamc@759
|
1219 if (ctx->cur_container == NULL)
|
adamc@759
|
1220 uw_error(ctx, FATAL, "uw_next_entry(NULL)");
|
adamc@759
|
1221
|
adamc@759
|
1222 switch (ctx->cur_container->kind) {
|
adamc@759
|
1223 case UNSET:
|
adamc@759
|
1224 uw_error(ctx, FATAL, "Missing entry");
|
adamc@759
|
1225 case FIL:
|
adamc@759
|
1226 uw_error(ctx, FATAL, "Tried to read a file form input as entry");
|
adamc@759
|
1227 case NORMAL:
|
adamc@759
|
1228 uw_error(ctx, FATAL, "Tried to read a normal form input as entry");
|
adamc@759
|
1229 case SUBFORM:
|
adamc@759
|
1230 uw_error(ctx, FATAL, "Tried to read a subform form input as entry");
|
adamc@759
|
1231 case SUBFORMS:
|
adamc@759
|
1232 uw_error(ctx, FATAL, "Tried to read a subforms form input as entry");
|
adamc@759
|
1233 case ENTRY:
|
adamc@759
|
1234 if (ctx->cur_container->data.entry.next) {
|
adamc@759
|
1235 ctx->cur_container = ctx->cur_container->data.entry.next;
|
adamc@759
|
1236 return 1;
|
adamc@759
|
1237 } else {
|
adamc@759
|
1238 ctx->cur_container = ctx->cur_container->data.entry.parent->data.subforms.parent;
|
adamc@759
|
1239 return 0;
|
adamc@759
|
1240 }
|
adamc@759
|
1241 default:
|
adamc@759
|
1242 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@759
|
1243 }
|
adamc@756
|
1244 }
|
adamc@756
|
1245
|
adamc@643
|
1246 void uw_set_script_header(uw_context ctx, const char *s) {
|
adamc@643
|
1247 ctx->script_header = s;
|
adamc@643
|
1248 }
|
adamc@643
|
1249
|
adamc@1094
|
1250 const char *uw_get_url_prefix(uw_context ctx) {
|
adamc@1094
|
1251 return ctx->app->url_prefix;
|
adamc@667
|
1252 }
|
adamc@667
|
1253
|
adamc@693
|
1254 void uw_set_needs_push(uw_context ctx, int n) {
|
adamc@693
|
1255 ctx->needs_push = n;
|
adamc@693
|
1256 }
|
adamc@693
|
1257
|
adamc@736
|
1258 void uw_set_needs_sig(uw_context ctx, int n) {
|
adamc@736
|
1259 ctx->needs_sig = n;
|
adamc@736
|
1260 }
|
adamc@736
|
1261
|
adam@1936
|
1262 void uw_set_could_write_db(uw_context ctx, int n) {
|
adam@1936
|
1263 ctx->could_write_db = n;
|
adam@1936
|
1264 }
|
adam@1936
|
1265
|
adam@2056
|
1266 void uw_set_at_most_one_query(uw_context ctx, int n) {
|
adam@2056
|
1267 ctx->at_most_one_query = n;
|
adam@2056
|
1268 }
|
adam@2056
|
1269
|
adamc@667
|
1270
|
adam@1369
|
1271 static void uw_buffer_check_ctx(uw_context ctx, const char *kind, uw_buffer *b, size_t extra, const char *desc) {
|
adamc@666
|
1272 if (b->back - b->front < extra) {
|
adamc@666
|
1273 size_t desired = b->front - b->start + extra, next;
|
adamc@136
|
1274 char *new_heap;
|
adamc@136
|
1275
|
adamc@666
|
1276 next = b->back - b->start;
|
adamc@317
|
1277 if (next == 0)
|
adamc@317
|
1278 next = 1;
|
adamc@317
|
1279 for (; next < desired; next *= 2);
|
adamc@136
|
1280
|
adamc@1134
|
1281 if (next > b->max) {
|
adamc@1113
|
1282 if (desired <= b->max)
|
adamc@1113
|
1283 next = desired;
|
adamc@1113
|
1284 else
|
adamc@1113
|
1285 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind);
|
adamc@1134
|
1286 }
|
adamc@1113
|
1287
|
adamc@666
|
1288 new_heap = realloc(b->start, next);
|
adamc@666
|
1289 b->front = new_heap + (b->front - b->start);
|
adamc@666
|
1290 b->back = new_heap + next;
|
adamc@136
|
1291
|
adamc@667
|
1292 if (new_heap != b->start) {
|
adamc@666
|
1293 b->start = new_heap;
|
adam@1389
|
1294 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously; increasing size to %llu", desc, (unsigned long long)next);
|
adamc@136
|
1295 }
|
adamc@136
|
1296
|
adamc@666
|
1297 b->start = new_heap;
|
adamc@136
|
1298 }
|
adamc@136
|
1299 }
|
adamc@136
|
1300
|
adamc@832
|
1301 void uw_check_heap(uw_context ctx, size_t extra) {
|
adam@1369
|
1302 uw_buffer_check_ctx(ctx, "heap", &ctx->heap, extra, "heap chunk");
|
adamc@666
|
1303 }
|
adamc@666
|
1304
|
adamc@832
|
1305 char *uw_heap_front(uw_context ctx) {
|
adamc@832
|
1306 return ctx->heap.front;
|
adamc@832
|
1307 }
|
adamc@832
|
1308
|
adamc@832
|
1309 void uw_set_heap_front(uw_context ctx, char *fr) {
|
adamc@832
|
1310 ctx->heap.front = fr;
|
adamc@832
|
1311 }
|
adamc@832
|
1312
|
adam@1782
|
1313 void uw_begin_initializing(uw_context ctx) {
|
adam@1782
|
1314 ctx->amInitializing = 1;
|
adam@1782
|
1315 }
|
adam@1782
|
1316
|
adam@1782
|
1317 void uw_end_initializing(uw_context ctx) {
|
adam@1782
|
1318 ctx->amInitializing = 0;
|
adam@1782
|
1319 }
|
adam@1782
|
1320
|
adam@2013
|
1321 static void align_heap(uw_context ctx) {
|
adam@2013
|
1322 size_t posn = ctx->heap.front - ctx->heap.start;
|
adam@2013
|
1323
|
adam@2017
|
1324 if (posn % sizeof(void *) != 0) {
|
adam@2017
|
1325 size_t bump = sizeof(void *) - posn % sizeof(void *);
|
adam@2013
|
1326 uw_check_heap(ctx, bump);
|
adam@2013
|
1327 ctx->heap.front += bump;
|
adam@2013
|
1328 }
|
adam@2013
|
1329 }
|
adam@2013
|
1330
|
adamc@311
|
1331 void *uw_malloc(uw_context ctx, size_t len) {
|
adam@2013
|
1332 // On some architectures, it's important that all word-sized memory accesses
|
adam@2013
|
1333 // be to word-aligned addresses, so we'll do a little bit of extra work here
|
adam@2013
|
1334 // in anticipation of a possible word-aligned access to the address we'll
|
adam@2013
|
1335 // return.
|
adam@2013
|
1336
|
adamc@136
|
1337 void *result;
|
adamc@136
|
1338
|
adam@1782
|
1339 if (ctx->amInitializing) {
|
adam@2017
|
1340 int error = posix_memalign(&result, sizeof(void *), len);
|
adam@2013
|
1341
|
adam@2013
|
1342 if (!error)
|
adam@1782
|
1343 return result;
|
adam@1782
|
1344 else
|
adam@2013
|
1345 uw_error(ctx, FATAL, "uw_malloc: posix_memalign() returns %d", error);
|
adam@1782
|
1346 } else {
|
adam@2013
|
1347 align_heap(ctx);
|
adam@2013
|
1348
|
adam@1782
|
1349 uw_check_heap(ctx, len);
|
adam@1782
|
1350
|
adam@1782
|
1351 result = ctx->heap.front;
|
adam@1782
|
1352 ctx->heap.front += len;
|
adam@1782
|
1353 return result;
|
adam@1782
|
1354 }
|
adamc@117
|
1355 }
|
adamc@117
|
1356
|
adamc@323
|
1357 void uw_begin_region(uw_context ctx) {
|
adam@2013
|
1358 align_heap(ctx);
|
adam@2013
|
1359
|
adamc@666
|
1360 regions *r = (regions *) ctx->heap.front;
|
adamc@323
|
1361
|
adamc@323
|
1362 uw_check_heap(ctx, sizeof(regions));
|
adamc@323
|
1363
|
adamc@666
|
1364 ctx->heap.front += sizeof(regions);
|
adamc@323
|
1365
|
adamc@323
|
1366 r->next = ctx->regions;
|
adamc@323
|
1367 ctx->regions = r;
|
adamc@323
|
1368 }
|
adamc@323
|
1369
|
adamc@323
|
1370 void uw_end_region(uw_context ctx) {
|
adamc@323
|
1371 regions *r = ctx->regions;
|
adamc@323
|
1372
|
adamc@323
|
1373 if (r == NULL)
|
adamc@323
|
1374 uw_error(ctx, FATAL, "Region stack underflow");
|
adamc@323
|
1375
|
adamc@666
|
1376 ctx->heap.front = (char *) r;
|
adamc@323
|
1377 ctx->regions = r->next;
|
adamc@323
|
1378 }
|
adamc@323
|
1379
|
adamc@324
|
1380 void uw_memstats(uw_context ctx) {
|
adam@1369
|
1381 printf("Headers: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->outHeaders), (unsigned long)uw_buffer_avail(&ctx->outHeaders));
|
adam@1369
|
1382 printf("Script: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->script), (unsigned long)uw_buffer_avail(&ctx->script));
|
adam@1369
|
1383 printf("Page: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->page), (unsigned long)uw_buffer_avail(&ctx->page));
|
adam@1369
|
1384 printf("Heap: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->heap), (unsigned long)uw_buffer_avail(&ctx->heap));
|
adamc@324
|
1385 }
|
adamc@324
|
1386
|
adam@1915
|
1387 int uw_pagelen(uw_context ctx) {
|
adam@1915
|
1388 return ctx->page.front - ctx->page.start;
|
adam@1915
|
1389 }
|
adam@1915
|
1390
|
adamc@311
|
1391 int uw_send(uw_context ctx, int sock) {
|
adam@1949
|
1392 size_t target_length = (ctx->outHeaders.front - ctx->outHeaders.start) + 2 + (ctx->page.front - ctx->page.start);
|
adam@1949
|
1393
|
adam@1949
|
1394 if (ctx->output_buffer_size < target_length) {
|
adam@1949
|
1395 do {
|
adam@1949
|
1396 ctx->output_buffer_size *= 2;
|
adam@1949
|
1397 } while (ctx->output_buffer_size < target_length);
|
adam@1949
|
1398 ctx->output_buffer = realloc(ctx->output_buffer, ctx->output_buffer_size);
|
adam@1949
|
1399 }
|
adam@1949
|
1400
|
adam@1949
|
1401 memcpy(ctx->output_buffer, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
|
adam@1949
|
1402 memcpy(ctx->output_buffer + (ctx->outHeaders.front - ctx->outHeaders.start), "\r\n", 2);
|
adam@1949
|
1403 memcpy(ctx->output_buffer + (ctx->outHeaders.front - ctx->outHeaders.start) + 2, ctx->page.start, ctx->page.front - ctx->page.start);
|
adam@1949
|
1404
|
adam@1949
|
1405 return uw_really_send(sock, ctx->output_buffer, target_length);
|
adamc@462
|
1406 }
|
adamc@462
|
1407
|
adamc@856
|
1408 int uw_print(uw_context ctx, int fd) {
|
adamc@856
|
1409 int n = uw_really_write(fd, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
|
adamc@856
|
1410
|
adamc@856
|
1411 if (n < 0)
|
adamc@856
|
1412 return n;
|
adamc@856
|
1413
|
adamc@856
|
1414 n = uw_really_write(fd, "\r\n", 2);
|
adamc@856
|
1415
|
adamc@856
|
1416 if (n < 0)
|
adamc@856
|
1417 return n;
|
adamc@856
|
1418
|
adamc@856
|
1419 return uw_really_write(fd, ctx->page.start, ctx->page.front - ctx->page.start);
|
adamc@856
|
1420 }
|
adamc@856
|
1421
|
adamc@860
|
1422 int uw_output(uw_context ctx, int (*output)(void *data, char *buf, size_t len), void *data) {
|
adamc@860
|
1423 int n = output(data, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
|
adamc@860
|
1424
|
adamc@860
|
1425 if (n < 0)
|
adamc@860
|
1426 return n;
|
adamc@860
|
1427
|
adamc@860
|
1428 n = output(data, "\r\n", 2);
|
adamc@860
|
1429
|
adamc@860
|
1430 if (n < 0)
|
adamc@860
|
1431 return n;
|
adamc@860
|
1432
|
adamc@860
|
1433 return output(data, ctx->page.start, ctx->page.front - ctx->page.start);
|
adamc@860
|
1434 }
|
adamc@860
|
1435
|
adamc@462
|
1436 static void uw_check_headers(uw_context ctx, size_t extra) {
|
adam@1369
|
1437 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, extra);
|
adamc@462
|
1438 }
|
adamc@462
|
1439
|
adamc@462
|
1440 void uw_write_header(uw_context ctx, uw_Basis_string s) {
|
adamc@462
|
1441 int len = strlen(s);
|
adamc@462
|
1442
|
adamc@462
|
1443 uw_check_headers(ctx, len + 1);
|
adamc@666
|
1444 strcpy(ctx->outHeaders.front, s);
|
adamc@666
|
1445 ctx->outHeaders.front += len;
|
adamc@562
|
1446 }
|
adamc@562
|
1447
|
adam@1934
|
1448 int uw_has_contentLength(uw_context ctx) {
|
adam@1934
|
1449 return strstr(ctx->outHeaders.start, "Content-length: ") != NULL;
|
adam@1934
|
1450 }
|
adam@1934
|
1451
|
adamc@1110
|
1452 void uw_clear_headers(uw_context ctx) {
|
adam@1369
|
1453 uw_buffer_reset(&ctx->outHeaders);
|
adamc@1110
|
1454 }
|
adamc@1110
|
1455
|
adam@1932
|
1456 void uw_Basis_clear_page(uw_context ctx) {
|
adam@1932
|
1457 uw_buffer_reset(&ctx->page);
|
adam@1932
|
1458 }
|
adam@1932
|
1459
|
adamc@562
|
1460 static void uw_check_script(uw_context ctx, size_t extra) {
|
adam@1369
|
1461 ctx_uw_buffer_check(ctx, "script", &ctx->script, extra);
|
adamc@562
|
1462 }
|
adamc@562
|
1463
|
adamc@562
|
1464 void uw_write_script(uw_context ctx, uw_Basis_string s) {
|
adamc@562
|
1465 int len = strlen(s);
|
adamc@562
|
1466
|
adamc@562
|
1467 uw_check_script(ctx, len + 1);
|
adamc@666
|
1468 strcpy(ctx->script.front, s);
|
adamc@666
|
1469 ctx->script.front += len;
|
adamc@562
|
1470 }
|
adamc@562
|
1471
|
adam@1446
|
1472 const char *uw_get_real_script(uw_context ctx) {
|
adam@1671
|
1473 if (strstr(ctx->outHeaders.start, "Set-Cookie: ")) {
|
adam@1671
|
1474 uw_write_script(ctx, "sig=\"");
|
adam@1671
|
1475 uw_write_script(ctx, ctx->app->cookie_sig(ctx));
|
adam@1671
|
1476 uw_write_script(ctx, "\";");
|
adam@1671
|
1477 }
|
adam@1671
|
1478
|
adam@1446
|
1479 return ctx->script.start;
|
adam@1446
|
1480 }
|
adam@1446
|
1481
|
adamc@695
|
1482 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
|
adamc@695
|
1483 if (s[0] == 0)
|
adamc@695
|
1484 return "";
|
adamc@695
|
1485 else {
|
adamc@695
|
1486 char *r = uw_malloc(ctx, 11 + strlen(s));
|
adamc@695
|
1487 sprintf(r, " onload='%s'", s);
|
adamc@695
|
1488 return r;
|
adamc@695
|
1489 }
|
adamc@695
|
1490 }
|
adamc@695
|
1491
|
adamc@1042
|
1492 uw_Basis_string uw_Basis_maybe_onunload(uw_context ctx, uw_Basis_string s) {
|
adamc@1042
|
1493 if (ctx->script_header[0] == 0)
|
adamc@1042
|
1494 return "";
|
adamc@1042
|
1495 else {
|
vshabanoff@1763
|
1496 char *r = uw_malloc(ctx, 37 + strlen(s));
|
vshabanoff@1763
|
1497 sprintf(r, " onunload='unloading=true;%s;unload()'", s);
|
adamc@1042
|
1498 return r;
|
adamc@1042
|
1499 }
|
adamc@1042
|
1500 }
|
adamc@1042
|
1501
|
adamc@694
|
1502 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
|
adamc@736
|
1503 if (ctx->client == NULL) {
|
adamc@736
|
1504 if (ctx->needs_sig) {
|
adamc@1094
|
1505 char *sig = ctx->app->cookie_sig(ctx);
|
adamc@736
|
1506 char *r = uw_malloc(ctx, strlen(sig) + 8);
|
adamc@736
|
1507 sprintf(r, "sig=\"%s\";", sig);
|
adamc@736
|
1508 return r;
|
adamc@736
|
1509 }
|
adamc@736
|
1510 else
|
adamc@736
|
1511 return "";
|
adamc@736
|
1512 } else {
|
adamc@1094
|
1513 char *sig = ctx->needs_sig ? ctx->app->cookie_sig(ctx) : "";
|
adamc@1094
|
1514 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->app->url_prefix)
|
adamc@736
|
1515 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
|
adam@1552
|
1516 sprintf(r, "isPost=%s;client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
|
adam@1552
|
1517 (ctx->isPost ? "true" : "false"),
|
adamc@682
|
1518 ctx->client->id,
|
adamc@682
|
1519 ctx->client->pass,
|
adamc@1094
|
1520 ctx->app->url_prefix,
|
adamc@1094
|
1521 ctx->app->timeout,
|
adamc@736
|
1522 ctx->needs_sig ? "sig=\"" : "",
|
adamc@736
|
1523 sig,
|
adamc@736
|
1524 ctx->needs_sig ? "\";" : "");
|
adamc@679
|
1525 return r;
|
adamc@668
|
1526 }
|
adamc@667
|
1527 }
|
adamc@667
|
1528
|
adamc@574
|
1529 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
1530 char *r, *s2;
|
adamc@574
|
1531
|
adam@1446
|
1532 uw_check_heap(ctx, strlen(s) * 4 + 3);
|
adamc@574
|
1533
|
adamc@666
|
1534 r = s2 = ctx->heap.front;
|
adamc@574
|
1535 *s2++ = '"';
|
adamc@574
|
1536
|
adamc@574
|
1537 for (; *s; s++) {
|
adam@1286
|
1538 unsigned char c = *s;
|
adamc@574
|
1539
|
adamc@574
|
1540 switch (c) {
|
adamc@574
|
1541 case '"':
|
adamc@574
|
1542 strcpy(s2, "\\\"");
|
adamc@574
|
1543 s2 += 2;
|
adamc@574
|
1544 break;
|
adamc@1027
|
1545 case '\'':
|
adamc@1027
|
1546 strcpy(s2, "\\047");
|
adamc@1027
|
1547 s2 += 4;
|
adamc@1027
|
1548 break;
|
adamc@574
|
1549 case '\\':
|
adamc@574
|
1550 strcpy(s2, "\\\\");
|
adamc@574
|
1551 s2 += 2;
|
adamc@574
|
1552 break;
|
adam@1457
|
1553 case '<':
|
adam@1457
|
1554 strcpy(s2, "\\074");
|
adam@1457
|
1555 s2 += 4;
|
adam@1457
|
1556 break;
|
adamc@574
|
1557 default:
|
adam@1286
|
1558 if (isprint((int)c) || c >= 128)
|
adamc@574
|
1559 *s2++ = c;
|
adamc@574
|
1560 else {
|
adam@1429
|
1561 sprintf(s2, "\\%03o", c);
|
adamc@574
|
1562 s2 += 4;
|
adamc@574
|
1563 }
|
adamc@574
|
1564 }
|
adamc@574
|
1565 }
|
adamc@574
|
1566
|
adamc@574
|
1567 strcpy(s2, "\"");
|
adamc@910
|
1568 ctx->heap.front = s2 + 2;
|
adamc@574
|
1569 return r;
|
adamc@574
|
1570 }
|
adamc@574
|
1571
|
adam@1286
|
1572 uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c1) {
|
adam@1286
|
1573 unsigned char c = c1;
|
adamc@1025
|
1574 char *r, *s2;
|
adamc@1025
|
1575
|
adam@1446
|
1576 uw_check_heap(ctx, 7);
|
adamc@1025
|
1577
|
adamc@1025
|
1578 r = s2 = ctx->heap.front;
|
adamc@1025
|
1579 *s2++ = '"';
|
adamc@1025
|
1580
|
adamc@1025
|
1581 switch (c) {
|
adamc@1025
|
1582 case '"':
|
adamc@1025
|
1583 strcpy(s2, "\\\"");
|
adamc@1025
|
1584 s2 += 2;
|
adamc@1025
|
1585 break;
|
adamc@1027
|
1586 case '\'':
|
adamc@1027
|
1587 strcpy(s2, "\\047");
|
adamc@1027
|
1588 s2 += 4;
|
adamc@1027
|
1589 break;
|
adamc@1025
|
1590 case '\\':
|
adamc@1025
|
1591 strcpy(s2, "\\\\");
|
adamc@1025
|
1592 s2 += 2;
|
adamc@1025
|
1593 break;
|
adam@1457
|
1594 case '<':
|
adam@1457
|
1595 strcpy(s2, "\\074");
|
adam@1457
|
1596 s2 += 4;
|
adam@1457
|
1597 break;
|
adamc@1025
|
1598 default:
|
adam@1286
|
1599 if (isprint((int)c) || c >= 128)
|
adamc@1025
|
1600 *s2++ = c;
|
adamc@1025
|
1601 else {
|
adam@1429
|
1602 sprintf(s2, "\\%03o", (unsigned char)c);
|
adamc@1025
|
1603 s2 += 4;
|
adamc@1025
|
1604 }
|
adamc@1025
|
1605 }
|
adamc@1025
|
1606
|
adamc@1025
|
1607 strcpy(s2, "\"");
|
adamc@1025
|
1608 ctx->heap.front = s2 + 2;
|
adamc@1025
|
1609 return r;
|
adamc@1025
|
1610 }
|
adamc@1025
|
1611
|
adamc@574
|
1612 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
1613 char *r, *s2;
|
adamc@574
|
1614
|
adam@1446
|
1615 uw_check_script(ctx, strlen(s) * 4 + 3);
|
adamc@574
|
1616
|
adamc@666
|
1617 r = s2 = ctx->script.front;
|
adamc@574
|
1618 *s2++ = '"';
|
adamc@574
|
1619
|
adamc@574
|
1620 for (; *s; s++) {
|
adam@1286
|
1621 unsigned char c = *s;
|
adamc@574
|
1622
|
adamc@574
|
1623 switch (c) {
|
adamc@577
|
1624 case '\'':
|
adamc@1027
|
1625 strcpy(s2, "\\");
|
adamc@574
|
1626 s2 += 2;
|
adamc@574
|
1627 break;
|
adamc@574
|
1628 case '\\':
|
adamc@574
|
1629 strcpy(s2, "\\\\");
|
adamc@574
|
1630 s2 += 2;
|
adamc@574
|
1631 break;
|
adam@1457
|
1632 case '<':
|
adam@1457
|
1633 strcpy(s2, "\\074");
|
adam@1457
|
1634 s2 += 4;
|
adam@1457
|
1635 break;
|
adamc@574
|
1636 default:
|
adam@1286
|
1637 if (isprint((int)c) || c >= 128)
|
adamc@574
|
1638 *s2++ = c;
|
adamc@574
|
1639 else {
|
adam@1429
|
1640 sprintf(s2, "\\%03o", c);
|
adamc@574
|
1641 s2 += 4;
|
adamc@574
|
1642 }
|
adamc@574
|
1643 }
|
adamc@574
|
1644 }
|
adamc@574
|
1645
|
adamc@574
|
1646 strcpy(s2, "\"");
|
adamc@666
|
1647 ctx->script.front = s2 + 1;
|
adamc@574
|
1648 return r;
|
adamc@574
|
1649 }
|
adamc@574
|
1650
|
adamc@682
|
1651 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1652 if (ctx->client == NULL || chn.cli != ctx->client->id)
|
adamc@682
|
1653 return "null";
|
adamc@682
|
1654 else {
|
adamc@682
|
1655 int len;
|
adamc@682
|
1656 char *r;
|
adamc@682
|
1657
|
adamc@682
|
1658 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
1659 r = ctx->heap.front;
|
adamc@682
|
1660 sprintf(r, "%u%n", chn.chn, &len);
|
adamc@682
|
1661 ctx->heap.front += len+1;
|
adamc@682
|
1662 return r;
|
adamc@682
|
1663 }
|
adamc@682
|
1664 }
|
adamc@682
|
1665
|
adam@1446
|
1666 uw_Basis_source uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
|
adamc@577
|
1667 int len;
|
adamc@577
|
1668 size_t s_len = strlen(s);
|
adamc@562
|
1669
|
grrwlf@1998
|
1670 if(ctx->id < 0)
|
grrwlf@1998
|
1671 uw_error(ctx, FATAL, "Attempt to create client source using inappropriate context");
|
grrwlf@1998
|
1672
|
adam@1446
|
1673 uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len);
|
adam@1446
|
1674 sprintf(ctx->script.front, "s%d_%llu=sc(exec(%n", ctx->id, ctx->source_count, &len);
|
adamc@666
|
1675 ctx->script.front += len;
|
adamc@666
|
1676 strcpy(ctx->script.front, s);
|
adamc@666
|
1677 ctx->script.front += s_len;
|
adamc@970
|
1678 strcpy(ctx->script.front, "));");
|
adamc@972
|
1679 ctx->script.front += 3;
|
adamc@562
|
1680
|
adam@1446
|
1681 uw_Basis_source r = {ctx->id, ctx->source_count++};
|
adam@1446
|
1682 return r;
|
adam@1446
|
1683 }
|
adam@1446
|
1684
|
adam@1446
|
1685 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_source src, uw_Basis_string s) {
|
adamc@577
|
1686 int len;
|
adamc@577
|
1687 size_t s_len = strlen(s);
|
adamc@577
|
1688
|
adam@1446
|
1689 uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len);
|
adam@1446
|
1690 sprintf(ctx->script.front, "sv(s%d_%llu,exec(%n", src.context, src.source, &len);
|
adamc@666
|
1691 ctx->script.front += len;
|
adamc@666
|
1692 strcpy(ctx->script.front, s);
|
adamc@666
|
1693 ctx->script.front += s_len;
|
adamc@970
|
1694 strcpy(ctx->script.front, "));");
|
adamc@972
|
1695 ctx->script.front += 3;
|
adamc@577
|
1696
|
adamc@577
|
1697 return uw_unit_v;
|
adamc@577
|
1698 }
|
adamc@577
|
1699
|
adamc@311
|
1700 static void uw_check(uw_context ctx, size_t extra) {
|
adam@1369
|
1701 ctx_uw_buffer_check(ctx, "page", &ctx->page, extra);
|
adamc@117
|
1702 }
|
adamc@117
|
1703
|
adamc@311
|
1704 static void uw_writec_unsafe(uw_context ctx, char c) {
|
adamc@666
|
1705 *(ctx->page.front)++ = c;
|
adam@1413
|
1706 *ctx->page.front = 0;
|
adamc@117
|
1707 }
|
adamc@117
|
1708
|
adamc@311
|
1709 void uw_writec(uw_context ctx, char c) {
|
adam@1413
|
1710 uw_check(ctx, 2);
|
adamc@311
|
1711 uw_writec_unsafe(ctx, c);
|
adamc@117
|
1712 }
|
adamc@117
|
1713
|
adam@2085
|
1714 void uw_Basis_writec(uw_context ctx, char c) {
|
adam@2085
|
1715 uw_writec(ctx, c);
|
adam@2085
|
1716 }
|
adam@2085
|
1717
|
adamc@311
|
1718 static void uw_write_unsafe(uw_context ctx, const char* s) {
|
adamc@117
|
1719 int len = strlen(s);
|
adamc@666
|
1720 memcpy(ctx->page.front, s, len);
|
adamc@666
|
1721 ctx->page.front += len;
|
adamc@117
|
1722 }
|
adamc@117
|
1723
|
adamc@311
|
1724 void uw_write(uw_context ctx, const char* s) {
|
adamc@311
|
1725 uw_check(ctx, strlen(s) + 1);
|
adamc@311
|
1726 uw_write_unsafe(ctx, s);
|
adamc@666
|
1727 *ctx->page.front = 0;
|
adamc@102
|
1728 }
|
adamc@106
|
1729
|
ziv@2204
|
1730 void uw_recordingStart(uw_context ctx) {
|
ziv@2262
|
1731 if (ctx->numRecording++ == 0) {
|
ziv@2262
|
1732 ctx->recordingOffset = ctx->page.front - ctx->page.start;
|
ziv@2262
|
1733 }
|
ziv@2204
|
1734 }
|
ziv@2204
|
1735
|
ziv@2204
|
1736 char *uw_recordingRead(uw_context ctx) {
|
ziv@2262
|
1737 // Only the outermost recorder can read unless the recording is empty.
|
ziv@2262
|
1738 char *recording = ctx->page.start + ctx->recordingOffset;
|
ziv@2262
|
1739 if (--ctx->numRecording > 0 && recording != ctx->page.front) {
|
ziv@2262
|
1740 return NULL;
|
ziv@2262
|
1741 }
|
ziv@2262
|
1742 return strdup(recording);
|
ziv@2204
|
1743 }
|
ziv@2204
|
1744
|
adamc@311
|
1745 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@136
|
1746 char *result;
|
adamc@136
|
1747 int len;
|
adamc@311
|
1748 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1749 result = ctx->heap.front;
|
adamc@276
|
1750 sprintf(result, "%lld%n", n, &len);
|
adamc@666
|
1751 ctx->heap.front += len+1;
|
adamc@136
|
1752 return result;
|
adamc@106
|
1753 }
|
adamc@106
|
1754
|
adamc@311
|
1755 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@136
|
1756 char *result;
|
adamc@136
|
1757 int len;
|
adamc@311
|
1758 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1759 result = ctx->heap.front;
|
adam@1920
|
1760 sprintf(result, "%.16g%n", n, &len);
|
adamc@666
|
1761 ctx->heap.front += len+1;
|
adamc@136
|
1762 return result;
|
adamc@106
|
1763 }
|
adamc@106
|
1764
|
adamc@311
|
1765 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@136
|
1766 int len = strlen(s);
|
adamc@136
|
1767 char *result, *p;
|
adamc@311
|
1768 uw_check_heap(ctx, len * 6 + 1);
|
adamc@136
|
1769
|
adamc@666
|
1770 result = p = ctx->heap.front;
|
adamc@136
|
1771
|
adamc@136
|
1772 for (; *s; s++) {
|
adamc@1054
|
1773 unsigned char c = *s;
|
adamc@136
|
1774
|
adamc@136
|
1775 if (c == '"') {
|
adamc@136
|
1776 strcpy(p, """);
|
adamc@136
|
1777 p += 6;
|
adamc@136
|
1778 } else if (c == '&') {
|
adamc@136
|
1779 strcpy(p, "&");
|
adamc@136
|
1780 p += 5;
|
adamc@136
|
1781 }
|
adamc@1059
|
1782 else
|
adamc@136
|
1783 *p++ = c;
|
adamc@136
|
1784 }
|
adamc@136
|
1785
|
adamc@137
|
1786 *p++ = 0;
|
adamc@666
|
1787 ctx->heap.front = p;
|
adamc@136
|
1788 return result;
|
adamc@106
|
1789 }
|
adamc@106
|
1790
|
adamc@1011
|
1791 char *uw_Basis_attrifyChar(uw_context ctx, uw_Basis_char c) {
|
adamc@1011
|
1792 char *result, *p;
|
adamc@1011
|
1793 uw_check_heap(ctx, 7);
|
adamc@1011
|
1794
|
adamc@1011
|
1795 result = p = ctx->heap.front;
|
adamc@1011
|
1796
|
adamc@1011
|
1797 if (c == '"') {
|
adamc@1011
|
1798 strcpy(p, """);
|
adamc@1011
|
1799 p += 6;
|
adamc@1011
|
1800 } else if (c == '&') {
|
adamc@1011
|
1801 strcpy(p, "&");
|
adamc@1011
|
1802 p += 5;
|
adamc@1011
|
1803 }
|
adamc@1059
|
1804 else
|
adamc@1011
|
1805 *p++ = c;
|
adamc@1011
|
1806
|
adamc@1011
|
1807 *p++ = 0;
|
adamc@1011
|
1808 ctx->heap.front = p;
|
adamc@1011
|
1809 return result;
|
adamc@1011
|
1810 }
|
adamc@1011
|
1811
|
adamc@721
|
1812 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
|
adamc@721
|
1813 return s;
|
adamc@721
|
1814 }
|
adamc@721
|
1815
|
adamc@311
|
1816 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@117
|
1817 int len;
|
adamc@117
|
1818
|
adamc@666
|
1819 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1820 ctx->page.front += len;
|
adamc@106
|
1821 }
|
adamc@106
|
1822
|
adamc@428
|
1823 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
1824 uw_check(ctx, INTS_MAX);
|
adamc@311
|
1825 uw_Basis_attrifyInt_w_unsafe(ctx, n);
|
adamc@428
|
1826
|
adamc@428
|
1827 return uw_unit_v;
|
adamc@106
|
1828 }
|
adamc@106
|
1829
|
adamc@428
|
1830 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@117
|
1831 int len;
|
adamc@117
|
1832
|
adamc@311
|
1833 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1834 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1835 ctx->page.front += len;
|
adamc@428
|
1836
|
adamc@428
|
1837 return uw_unit_v;
|
adamc@117
|
1838 }
|
adamc@117
|
1839
|
adamc@428
|
1840 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1841 uw_check(ctx, strlen(s) * 6);
|
adamc@117
|
1842
|
adamc@106
|
1843 for (; *s; s++) {
|
adamc@1054
|
1844 unsigned char c = *s;
|
adamc@106
|
1845
|
adamc@106
|
1846 if (c == '"')
|
adamc@311
|
1847 uw_write_unsafe(ctx, """);
|
adamc@136
|
1848 else if (c == '&')
|
adamc@311
|
1849 uw_write_unsafe(ctx, "&");
|
adamc@1059
|
1850 else
|
adamc@311
|
1851 uw_writec_unsafe(ctx, c);
|
adamc@106
|
1852 }
|
adamc@428
|
1853
|
adamc@428
|
1854 return uw_unit_v;
|
adamc@106
|
1855 }
|
adamc@120
|
1856
|
adamc@1011
|
1857 uw_unit uw_Basis_attrifyChar_w(uw_context ctx, uw_Basis_char c) {
|
adamc@1011
|
1858 uw_check(ctx, 6);
|
adamc@1011
|
1859
|
adamc@1011
|
1860 if (c == '"')
|
adamc@1011
|
1861 uw_write_unsafe(ctx, """);
|
adamc@1011
|
1862 else if (c == '&')
|
adamc@1011
|
1863 uw_write_unsafe(ctx, "&");
|
adamc@1059
|
1864 else
|
adamc@1011
|
1865 uw_writec_unsafe(ctx, c);
|
adamc@1011
|
1866
|
adamc@1011
|
1867 return uw_unit_v;
|
adamc@1011
|
1868 }
|
adamc@1011
|
1869
|
adamc@120
|
1870
|
adamc@311
|
1871 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@137
|
1872 int len;
|
adamc@137
|
1873 char *r;
|
adamc@137
|
1874
|
adamc@311
|
1875 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1876 r = ctx->heap.front;
|
adamc@276
|
1877 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1878 ctx->heap.front += len+1;
|
adamc@137
|
1879 return r;
|
adamc@120
|
1880 }
|
adamc@120
|
1881
|
adamc@682
|
1882 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1883 if (ctx->client == NULL || chn.cli != ctx->client->id)
|
adamc@682
|
1884 return "";
|
adamc@682
|
1885 else {
|
adamc@682
|
1886 int len;
|
adamc@682
|
1887 char *r;
|
adamc@679
|
1888
|
adamc@682
|
1889 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
1890 r = ctx->heap.front;
|
adamc@682
|
1891 sprintf(r, "%u%n", chn.chn, &len);
|
adamc@682
|
1892 ctx->heap.front += len+1;
|
adamc@682
|
1893 return r;
|
adamc@682
|
1894 }
|
adamc@679
|
1895 }
|
adamc@679
|
1896
|
adamc@311
|
1897 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@137
|
1898 int len;
|
adamc@137
|
1899 char *r;
|
adamc@137
|
1900
|
adamc@311
|
1901 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1902 r = ctx->heap.front;
|
adamc@137
|
1903 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1904 ctx->heap.front += len+1;
|
adamc@137
|
1905 return r;
|
adamc@120
|
1906 }
|
adamc@120
|
1907
|
adamc@311
|
1908 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
1909 char *r, *p;
|
adamc@137
|
1910
|
adamc@925
|
1911 if (s[0] == '\0')
|
adamc@925
|
1912 return "_";
|
adamc@925
|
1913
|
adamc@925
|
1914 uw_check_heap(ctx, strlen(s) * 3 + 1 + !!(s[0] == '_'));
|
adamc@925
|
1915
|
adamc@925
|
1916 r = p = ctx->heap.front;
|
adamc@925
|
1917 if (s[0] == '_')
|
adamc@925
|
1918 *p++ = '_';
|
adamc@925
|
1919
|
adamc@925
|
1920 for (; *s; s++) {
|
adamc@1087
|
1921 unsigned char c = *s;
|
adamc@137
|
1922
|
adamc@137
|
1923 if (c == ' ')
|
adamc@137
|
1924 *p++ = '+';
|
adamc@137
|
1925 else if (isalnum(c))
|
adamc@137
|
1926 *p++ = c;
|
adamc@137
|
1927 else {
|
adamc@1259
|
1928 sprintf(p, ".%02X", c);
|
adamc@137
|
1929 p += 3;
|
adamc@137
|
1930 }
|
adamc@137
|
1931 }
|
adamc@137
|
1932
|
adamc@137
|
1933 *p++ = 0;
|
adamc@666
|
1934 ctx->heap.front = p;
|
adamc@137
|
1935 return r;
|
adamc@120
|
1936 }
|
adamc@120
|
1937
|
adamc@311
|
1938 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1939 if (b == uw_Basis_False)
|
adamc@186
|
1940 return "0";
|
adamc@186
|
1941 else
|
adamc@186
|
1942 return "1";
|
adamc@186
|
1943 }
|
adamc@186
|
1944
|
adam@1620
|
1945 char *uw_Basis_urlifySource(uw_context ctx, uw_Basis_source src) {
|
adam@1620
|
1946 char *r;
|
adam@1620
|
1947 int len;
|
adam@1620
|
1948 uw_check_heap(ctx, 2 * INTS_MAX + 2);
|
adam@1620
|
1949 r = ctx->heap.front;
|
adam@1620
|
1950 sprintf(r, "%d/%llu%n", src.context, src.source, &len);
|
adam@1620
|
1951 ctx->heap.front += len+1;
|
adam@1620
|
1952 return r;
|
adam@1620
|
1953 }
|
adam@1620
|
1954
|
adamc@311
|
1955 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@120
|
1956 int len;
|
adamc@120
|
1957
|
adamc@666
|
1958 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1959 ctx->page.front += len;
|
adamc@120
|
1960 }
|
adamc@120
|
1961
|
adamc@428
|
1962 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
1963 uw_check(ctx, INTS_MAX);
|
adamc@311
|
1964 uw_Basis_urlifyInt_w_unsafe(ctx, n);
|
adamc@428
|
1965
|
adamc@428
|
1966 return uw_unit_v;
|
adamc@120
|
1967 }
|
adamc@120
|
1968
|
adamc@682
|
1969 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1970 if (ctx->client != NULL && chn.cli == ctx->client->id) {
|
adamc@682
|
1971 int len;
|
phurst@1981
|
1972
|
adamc@682
|
1973 uw_check(ctx, INTS_MAX + 1);
|
adamc@682
|
1974 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
|
adamc@682
|
1975 ctx->page.front += len;
|
adamc@682
|
1976 }
|
phurst@1981
|
1977
|
adamc@679
|
1978 return uw_unit_v;
|
adamc@679
|
1979 }
|
adamc@679
|
1980
|
adamc@428
|
1981 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@120
|
1982 int len;
|
adamc@120
|
1983
|
adamc@311
|
1984 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1985 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1986 ctx->page.front += len;
|
adamc@428
|
1987
|
adamc@428
|
1988 return uw_unit_v;
|
adamc@120
|
1989 }
|
adamc@120
|
1990
|
adamc@488
|
1991 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@1376
|
1992 return uw_Basis_urlifyInt(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds);
|
adamc@488
|
1993 }
|
adamc@488
|
1994
|
adam@1404
|
1995 uw_unit uw_Basis_urlifyTime_w(uw_context ctx, uw_Basis_time t) {
|
adam@1404
|
1996 return uw_Basis_urlifyInt_w(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds);
|
adam@1404
|
1997 }
|
adam@1404
|
1998
|
adamc@428
|
1999 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@925
|
2000 if (s[0] == '\0') {
|
adamc@925
|
2001 uw_check(ctx, 1);
|
adamc@925
|
2002 uw_writec_unsafe(ctx, '_');
|
adamc@925
|
2003 return uw_unit_v;
|
adamc@925
|
2004 }
|
adamc@925
|
2005
|
adamc@925
|
2006 uw_check(ctx, strlen(s) * 3 + !!(s[0] == '_'));
|
adamc@925
|
2007
|
adamc@925
|
2008 if (s[0] == '_')
|
adamc@925
|
2009 uw_writec_unsafe(ctx, '_');
|
adamc@120
|
2010
|
adamc@120
|
2011 for (; *s; s++) {
|
adamc@1087
|
2012 unsigned char c = *s;
|
adamc@120
|
2013
|
adamc@120
|
2014 if (c == ' ')
|
adamc@311
|
2015 uw_writec_unsafe(ctx, '+');
|
adamc@120
|
2016 else if (isalnum(c))
|
adamc@311
|
2017 uw_writec_unsafe(ctx, c);
|
adamc@120
|
2018 else {
|
adamc@1259
|
2019 sprintf(ctx->page.front, ".%02X", c);
|
adamc@666
|
2020 ctx->page.front += 3;
|
adamc@120
|
2021 }
|
adamc@120
|
2022 }
|
adamc@428
|
2023
|
adamc@428
|
2024 return uw_unit_v;
|
adamc@120
|
2025 }
|
adamc@120
|
2026
|
adamc@428
|
2027 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2028 if (b == uw_Basis_False)
|
adamc@311
|
2029 uw_writec(ctx, '0');
|
adamc@186
|
2030 else
|
adamc@311
|
2031 uw_writec(ctx, '1');
|
adamc@428
|
2032
|
adamc@428
|
2033 return uw_unit_v;
|
adamc@186
|
2034 }
|
adamc@186
|
2035
|
adam@1620
|
2036 uw_unit uw_Basis_urlifySource_w(uw_context ctx, uw_Basis_source src) {
|
adam@1620
|
2037 int len;
|
phurst@1981
|
2038
|
adam@1620
|
2039 uw_check(ctx, 2 * INTS_MAX + 2);
|
adam@1620
|
2040 sprintf(ctx->page.front, "%d/%llu%n", src.context, src.source, &len);
|
adam@1620
|
2041 ctx->page.front += len;
|
phurst@1981
|
2042
|
adam@1620
|
2043 return uw_unit_v;
|
adam@1620
|
2044 }
|
adam@1620
|
2045
|
adamc@120
|
2046
|
adamc@311
|
2047 static char *uw_unurlify_advance(char *s) {
|
adamc@144
|
2048 char *new_s = strchr(s, '/');
|
adamc@120
|
2049
|
adamc@120
|
2050 if (new_s)
|
adamc@120
|
2051 *new_s++ = 0;
|
adamc@120
|
2052 else
|
adamc@144
|
2053 new_s = strchr(s, 0);
|
adamc@144
|
2054
|
adamc@144
|
2055 return new_s;
|
adamc@144
|
2056 }
|
adamc@144
|
2057
|
adamc@311
|
2058 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
|
adamc@311
|
2059 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
2060 uw_Basis_int r;
|
adamc@120
|
2061
|
adamc@276
|
2062 r = atoll(*s);
|
adamc@120
|
2063 *s = new_s;
|
adamc@120
|
2064 return r;
|
adamc@120
|
2065 }
|
adamc@120
|
2066
|
adamc@311
|
2067 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
|
adamc@311
|
2068 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
2069 uw_Basis_float r;
|
adamc@120
|
2070
|
adamc@120
|
2071 r = atof(*s);
|
adamc@120
|
2072 *s = new_s;
|
adamc@120
|
2073 return r;
|
adamc@120
|
2074 }
|
adamc@120
|
2075
|
adamc@488
|
2076 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
|
adam@1365
|
2077 uw_Basis_int n = uw_Basis_unurlifyInt(ctx, s);
|
adam@1365
|
2078 uw_Basis_time r = {n / 1000000, n % 1000000};
|
adam@1365
|
2079 return r;
|
adamc@488
|
2080 }
|
adamc@488
|
2081
|
adamc@1023
|
2082 static uw_Basis_string uw_unurlifyString_to(int fromClient, uw_context ctx, char *r, char *s) {
|
adamc@927
|
2083 char *s1, *s2 = s;
|
adamc@144
|
2084 int n;
|
adamc@136
|
2085
|
adamc@1023
|
2086 if (!fromClient) {
|
adamc@1023
|
2087 if (*s2 == '_')
|
adamc@1023
|
2088 ++s2;
|
adamc@1259
|
2089 else if ((s2[0] == '%' || s2[0] == '.') && s2[1] == '5' && (s2[2] == 'f' || s2[2] == 'F'))
|
adamc@1023
|
2090 s2 += 3;
|
adamc@1023
|
2091 }
|
adamc@925
|
2092
|
adamc@927
|
2093 for (s1 = r; *s2; ++s1, ++s2) {
|
adamc@1087
|
2094 unsigned char c = *s2;
|
adamc@136
|
2095
|
adamc@136
|
2096 switch (c) {
|
adamc@136
|
2097 case '+':
|
adamc@136
|
2098 *s1 = ' ';
|
adamc@136
|
2099 break;
|
adamc@136
|
2100 case '%':
|
adamc@169
|
2101 if (s2[1] == 0)
|
adamc@311
|
2102 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
|
adamc@169
|
2103 if (s2[2] == 0)
|
adamc@311
|
2104 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
|
adamc@169
|
2105 if (sscanf(s2+1, "%02X", &n) != 1)
|
adam@1444
|
2106 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", uw_Basis_htmlifyString(ctx, s2));
|
adamc@136
|
2107 *s1 = n;
|
adamc@136
|
2108 s2 += 2;
|
adamc@136
|
2109 break;
|
adamc@1259
|
2110 case '.':
|
adamc@1259
|
2111 if (!fromClient) {
|
adamc@1259
|
2112 if (s2[1] == 0)
|
adamc@1259
|
2113 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
|
adamc@1259
|
2114 if (s2[2] == 0)
|
adamc@1259
|
2115 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
|
adamc@1259
|
2116 if (sscanf(s2+1, "%02X", &n) != 1)
|
adam@1444
|
2117 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", uw_Basis_htmlifyString(ctx, s2));
|
adamc@1259
|
2118 *s1 = n;
|
adamc@1259
|
2119 s2 += 2;
|
adamc@1259
|
2120 break;
|
adamc@1259
|
2121 }
|
adamc@136
|
2122 default:
|
adamc@136
|
2123 *s1 = c;
|
adamc@136
|
2124 }
|
adamc@136
|
2125 }
|
adamc@136
|
2126 *s1++ = 0;
|
adamc@144
|
2127 return s1;
|
adamc@144
|
2128 }
|
adamc@144
|
2129
|
adamc@311
|
2130 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
|
adamc@311
|
2131 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
2132 uw_Basis_bool r;
|
phurst@1981
|
2133
|
adamc@186
|
2134 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
|
adamc@311
|
2135 r = uw_Basis_False;
|
adamc@186
|
2136 else
|
adamc@311
|
2137 r = uw_Basis_True;
|
adamc@186
|
2138
|
adamc@186
|
2139 *s = new_s;
|
adamc@186
|
2140 return r;
|
adamc@186
|
2141 }
|
adamc@186
|
2142
|
adamc@311
|
2143 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
|
adamc@311
|
2144 char *new_s = uw_unurlify_advance(*s);
|
adamc@1134
|
2145 char *r;
|
adamc@1134
|
2146 int len;
|
adamc@144
|
2147
|
adamc@200
|
2148 len = strlen(*s);
|
adamc@311
|
2149 uw_check_heap(ctx, len + 1);
|
adamc@144
|
2150
|
adamc@666
|
2151 r = ctx->heap.front;
|
adamc@1023
|
2152 ctx->heap.front = uw_unurlifyString_to(0, ctx, ctx->heap.front, *s);
|
adamc@1023
|
2153 *s = new_s;
|
adamc@1023
|
2154 return r;
|
adamc@1023
|
2155 }
|
adamc@1023
|
2156
|
adamc@1109
|
2157 uw_Basis_unit uw_Basis_unurlifyUnit(uw_context ctx, char **s) {
|
adamc@1109
|
2158 *s = uw_unurlify_advance(*s);
|
adamc@1109
|
2159 return uw_unit_v;
|
adamc@1109
|
2160 }
|
adamc@1109
|
2161
|
adamc@1023
|
2162 uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) {
|
adamc@1023
|
2163 char *new_s = uw_unurlify_advance(*s);
|
adamc@1134
|
2164 char *r;
|
adamc@1134
|
2165 int len;
|
adamc@1023
|
2166
|
adamc@1023
|
2167 len = strlen(*s);
|
adamc@1023
|
2168 uw_check_heap(ctx, len + 1);
|
adamc@1023
|
2169
|
adamc@1023
|
2170 r = ctx->heap.front;
|
adamc@1023
|
2171 ctx->heap.front = uw_unurlifyString_to(1, ctx, ctx->heap.front, *s);
|
adamc@136
|
2172 *s = new_s;
|
adamc@136
|
2173 return r;
|
adamc@120
|
2174 }
|
adamc@135
|
2175
|
adamc@135
|
2176
|
adamc@311
|
2177 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
2178 int len;
|
adamc@286
|
2179 char *r;
|
adamc@286
|
2180
|
adamc@311
|
2181 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
2182 r = ctx->heap.front;
|
adamc@286
|
2183 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
2184 ctx->heap.front += len+1;
|
adamc@286
|
2185 return r;
|
adamc@286
|
2186 }
|
adamc@286
|
2187
|
adamc@428
|
2188 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
2189 int len;
|
adamc@286
|
2190
|
adamc@311
|
2191 uw_check(ctx, INTS_MAX);
|
adamc@666
|
2192 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
2193 ctx->page.front += len;
|
phurst@1981
|
2194
|
adamc@428
|
2195 return uw_unit_v;
|
adamc@286
|
2196 }
|
adamc@286
|
2197
|
adam@1358
|
2198 char *uw_Basis_htmlifySpecialChar(uw_context ctx, unsigned char ch) {
|
adam@1358
|
2199 unsigned int n = ch;
|
adam@1358
|
2200 int len;
|
adam@1358
|
2201 char *r;
|
adam@1358
|
2202
|
adam@1358
|
2203 uw_check_heap(ctx, INTS_MAX+3);
|
adam@1358
|
2204 r = ctx->heap.front;
|
adam@1358
|
2205 sprintf(r, "&#%u;%n", n, &len);
|
adam@1358
|
2206 ctx->heap.front += len+1;
|
adam@1358
|
2207 return r;
|
adam@1358
|
2208 }
|
adam@1358
|
2209
|
adam@1358
|
2210 uw_unit uw_Basis_htmlifySpecialChar_w(uw_context ctx, unsigned char ch) {
|
adam@1358
|
2211 unsigned int n = ch;
|
adam@1358
|
2212 int len;
|
adam@1358
|
2213
|
adam@1358
|
2214 uw_check(ctx, INTS_MAX+3);
|
adam@1358
|
2215 sprintf(ctx->page.front, "&#%u;%n", n, &len);
|
adam@1358
|
2216 ctx->page.front += len;
|
adam@1358
|
2217 return uw_unit_v;
|
adam@1358
|
2218 }
|
adam@1358
|
2219
|
adamc@311
|
2220 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
2221 int len;
|
adamc@286
|
2222 char *r;
|
adamc@286
|
2223
|
adamc@311
|
2224 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
2225 r = ctx->heap.front;
|
adamc@286
|
2226 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
2227 ctx->heap.front += len+1;
|
adamc@286
|
2228 return r;
|
adamc@286
|
2229 }
|
adamc@286
|
2230
|
adamc@428
|
2231 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
2232 int len;
|
adamc@286
|
2233
|
adamc@311
|
2234 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
2235 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
2236 ctx->page.front += len;
|
adamc@428
|
2237
|
adamc@428
|
2238 return uw_unit_v;
|
adamc@286
|
2239 }
|
adamc@286
|
2240
|
adam@1365
|
2241 char *uw_Basis_jsifyTime(uw_context ctx, uw_Basis_time t) {
|
adam@1360
|
2242 int len;
|
adam@1360
|
2243 char *r;
|
adam@1360
|
2244
|
adam@1360
|
2245 uw_check_heap(ctx, INTS_MAX);
|
adam@1360
|
2246 r = ctx->heap.front;
|
adamc@1377
|
2247 sprintf(r, "%lld%n", (uw_Basis_int)t.seconds * 1000000 + t.microseconds, &len);
|
adam@1360
|
2248 ctx->heap.front += len+1;
|
adam@1360
|
2249 return r;
|
adam@1360
|
2250 }
|
adam@1360
|
2251
|
adam@1365
|
2252 uw_unit uw_Basis_jsifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adam@1360
|
2253 int len;
|
adam@1360
|
2254
|
adam@1360
|
2255 uw_check(ctx, INTS_MAX);
|
adam@1360
|
2256 sprintf(ctx->page.front, "%lld%n", (uw_Basis_int)n, &len);
|
adam@1360
|
2257 ctx->page.front += len;
|
phurst@1981
|
2258
|
adam@1360
|
2259 return uw_unit_v;
|
adam@1360
|
2260 }
|
adam@1360
|
2261
|
adam@1444
|
2262 char *uw_Basis_htmlifyString(uw_context ctx, const char *s) {
|
adamc@137
|
2263 char *r, *s2;
|
adamc@137
|
2264
|
adamc@311
|
2265 uw_check_heap(ctx, strlen(s) * 5 + 1);
|
adamc@137
|
2266
|
adamc@666
|
2267 for (r = s2 = ctx->heap.front; *s; s++) {
|
adamc@1054
|
2268 unsigned char c = *s;
|
adamc@137
|
2269
|
adamc@137
|
2270 switch (c) {
|
adamc@137
|
2271 case '<':
|
adamc@137
|
2272 strcpy(s2, "<");
|
adamc@137
|
2273 s2 += 4;
|
adamc@137
|
2274 break;
|
adamc@137
|
2275 case '&':
|
adamc@137
|
2276 strcpy(s2, "&");
|
adamc@137
|
2277 s2 += 5;
|
adamc@137
|
2278 break;
|
adamc@137
|
2279 default:
|
adamc@1059
|
2280 *s2++ = c;
|
adamc@137
|
2281 }
|
adamc@137
|
2282 }
|
adamc@137
|
2283
|
adamc@137
|
2284 *s2++ = 0;
|
adamc@666
|
2285 ctx->heap.front = s2;
|
adamc@137
|
2286 return r;
|
adamc@135
|
2287 }
|
adamc@135
|
2288
|
adamc@428
|
2289 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@321
|
2290 uw_check(ctx, strlen(s) * 6);
|
adamc@135
|
2291
|
adamc@135
|
2292 for (; *s; s++) {
|
adamc@1054
|
2293 unsigned char c = *s;
|
adamc@135
|
2294
|
adamc@135
|
2295 switch (c) {
|
adamc@135
|
2296 case '<':
|
adamc@311
|
2297 uw_write_unsafe(ctx, "<");
|
adamc@135
|
2298 break;
|
adamc@135
|
2299 case '&':
|
adamc@311
|
2300 uw_write_unsafe(ctx, "&");
|
adamc@135
|
2301 break;
|
adamc@135
|
2302 default:
|
adamc@1059
|
2303 uw_writec_unsafe(ctx, c);
|
adamc@135
|
2304 }
|
adamc@135
|
2305 }
|
adamc@428
|
2306
|
adamc@428
|
2307 return uw_unit_v;
|
adamc@135
|
2308 }
|
adamc@180
|
2309
|
adamc@311
|
2310 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2311 if (b == uw_Basis_False)
|
adamc@286
|
2312 return "False";
|
adamc@286
|
2313 else
|
adamc@286
|
2314 return "True";
|
adamc@286
|
2315 }
|
adamc@286
|
2316
|
adamc@428
|
2317 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2318 if (b == uw_Basis_False) {
|
adamc@311
|
2319 uw_check(ctx, 6);
|
adamc@666
|
2320 strcpy(ctx->page.front, "False");
|
adamc@666
|
2321 ctx->page.front += 5;
|
adamc@286
|
2322 } else {
|
adamc@311
|
2323 uw_check(ctx, 5);
|
adamc@666
|
2324 strcpy(ctx->page.front, "True");
|
adamc@666
|
2325 ctx->page.front += 4;
|
adamc@286
|
2326 }
|
adamc@428
|
2327
|
adamc@428
|
2328 return uw_unit_v;
|
adamc@286
|
2329 }
|
adamc@286
|
2330
|
adamc@436
|
2331 #define TIME_FMT "%x %X"
|
adamc@438
|
2332 #define TIME_FMT_PG "%Y-%m-%d %T"
|
adam@1632
|
2333 #define TIME_FMT_JS "%Y/%m/%d %T"
|
adamc@436
|
2334
|
adam@1629
|
2335 uw_Basis_string uw_Basis_timeToString(uw_context, uw_Basis_time);
|
adam@1629
|
2336
|
adamc@436
|
2337 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
|
adam@1629
|
2338 return uw_Basis_htmlifyString(ctx, uw_Basis_timeToString(ctx, t));
|
adamc@436
|
2339 }
|
adamc@436
|
2340
|
adamc@436
|
2341 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
|
adam@1629
|
2342 return uw_Basis_htmlifyString_w(ctx, uw_Basis_timeToString(ctx, t));
|
adamc@436
|
2343 }
|
adamc@436
|
2344
|
adam@1446
|
2345 char *uw_Basis_htmlifySource(uw_context ctx, uw_Basis_source src) {
|
adam@1446
|
2346 int len;
|
adam@1446
|
2347 char *r;
|
adam@1446
|
2348
|
adam@1446
|
2349 uw_check_heap(ctx, 2 * INTS_MAX + 2);
|
adam@1446
|
2350 r = ctx->heap.front;
|
adam@1446
|
2351 sprintf(r, "s%d_%llu%n", src.context, src.source, &len);
|
adam@1446
|
2352 ctx->heap.front += len+1;
|
adam@1446
|
2353 return r;
|
adam@1446
|
2354 }
|
adam@1446
|
2355
|
adam@1446
|
2356 uw_unit uw_Basis_htmlifySource_w(uw_context ctx, uw_Basis_source src) {
|
adam@1446
|
2357 int len;
|
adam@1446
|
2358
|
adam@1446
|
2359 uw_check(ctx, 2 * INTS_MAX + 1);
|
adam@1446
|
2360 sprintf(ctx->page.front, "s%d_%llu%n", src.context, src.source, &len);
|
adam@1446
|
2361 ctx->page.front += len;
|
phurst@1981
|
2362
|
adam@1446
|
2363 return uw_unit_v;
|
adam@1446
|
2364 }
|
adam@1446
|
2365
|
adamc@821
|
2366 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
|
adam@1388
|
2367 while (n >= 0) {
|
adam@1388
|
2368 if (*s == 0)
|
adam@1388
|
2369 uw_error(ctx, FATAL, "Out-of-bounds strsub");
|
adam@1388
|
2370
|
adam@1388
|
2371 if (n == 0)
|
adam@1388
|
2372 return *s;
|
adam@1388
|
2373
|
adam@1388
|
2374 --n;
|
adam@1388
|
2375 ++s;
|
adam@1388
|
2376 }
|
adam@1388
|
2377
|
adam@1388
|
2378 uw_error(ctx, FATAL, "Negative strsub bound");
|
adamc@821
|
2379 }
|
adamc@821
|
2380
|
adamc@821
|
2381 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
|
adam@1388
|
2382 while (n >= 0) {
|
adam@1388
|
2383 if (*s == 0 || n == 0)
|
adam@1388
|
2384 return s;
|
adam@1388
|
2385
|
adam@1388
|
2386 --n;
|
adam@1388
|
2387 ++s;
|
adam@1388
|
2388 }
|
adam@1388
|
2389
|
adam@1388
|
2390 uw_error(ctx, FATAL, "Negative strsuffix bound");
|
adamc@821
|
2391 }
|
adamc@821
|
2392
|
adamc@828
|
2393 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
|
adamc@828
|
2394 return strlen(s);
|
adamc@828
|
2395 }
|
adamc@828
|
2396
|
adam@1388
|
2397 uw_Basis_bool uw_Basis_strlenGe(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
|
adam@1388
|
2398 while (n > 0) {
|
adam@1388
|
2399 if (*s == 0)
|
adam@1388
|
2400 return uw_Basis_False;
|
adam@1388
|
2401
|
adam@1388
|
2402 --n;
|
adam@1388
|
2403 ++s;
|
adam@1388
|
2404 }
|
adam@1388
|
2405
|
adam@1388
|
2406 return uw_Basis_True;
|
adam@1388
|
2407 }
|
adam@1388
|
2408
|
adamc@829
|
2409 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
|
adamc@829
|
2410 return strchr(s, ch);
|
adamc@829
|
2411 }
|
adamc@829
|
2412
|
adamc@831
|
2413 uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) {
|
adamc@831
|
2414 return strcspn(s, chs);
|
adamc@831
|
2415 }
|
adamc@831
|
2416
|
adamc@829
|
2417 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
|
adamc@829
|
2418 uw_Basis_string r = strchr(s, ch);
|
adamc@829
|
2419 if (r == NULL)
|
adamc@829
|
2420 return NULL;
|
adamc@829
|
2421 else {
|
adamc@829
|
2422 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adamc@829
|
2423 *nr = r - s;
|
adamc@829
|
2424 return nr;
|
adamc@829
|
2425 }
|
adamc@829
|
2426 }
|
adamc@829
|
2427
|
adam@1390
|
2428 uw_Basis_int *uw_Basis_strsindex(uw_context ctx, const char *haystack, const char *needle) {
|
adam@1390
|
2429 uw_Basis_string r = strstr(haystack, needle);
|
adam@1390
|
2430 if (r == NULL)
|
adam@1390
|
2431 return NULL;
|
adam@1390
|
2432 else {
|
adam@1390
|
2433 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adam@1390
|
2434 *nr = r - haystack;
|
adam@1390
|
2435 return nr;
|
adam@1390
|
2436 }
|
adam@1390
|
2437 }
|
adam@1390
|
2438
|
adamc@311
|
2439 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
|
adamc@828
|
2440 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
|
adamc@180
|
2441 char *s;
|
adamc@180
|
2442
|
adamc@311
|
2443 uw_check_heap(ctx, len);
|
adamc@180
|
2444
|
adamc@666
|
2445 s = ctx->heap.front;
|
adamc@180
|
2446
|
adamc@180
|
2447 strcpy(s, s1);
|
adamc@180
|
2448 strcat(s, s2);
|
adamc@666
|
2449 ctx->heap.front += len;
|
adamc@180
|
2450
|
adamc@180
|
2451 return s;
|
adamc@180
|
2452 }
|
adamc@278
|
2453
|
adamc@829
|
2454 uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) {
|
adamc@829
|
2455 size_t full_len = uw_Basis_strlen(ctx, s);
|
adamc@829
|
2456
|
adamc@829
|
2457 if (start < 0)
|
adamc@829
|
2458 uw_error(ctx, FATAL, "substring: Negative start index");
|
adamc@829
|
2459 if (len < 0)
|
adamc@829
|
2460 uw_error(ctx, FATAL, "substring: Negative length");
|
adamc@829
|
2461 if (start + len > full_len)
|
adamc@829
|
2462 uw_error(ctx, FATAL, "substring: Start index plus length is too large");
|
adamc@829
|
2463
|
adamc@829
|
2464 if (start + len == full_len)
|
adamc@829
|
2465 return &s[start];
|
adamc@829
|
2466 else {
|
adamc@829
|
2467 uw_Basis_string r = uw_malloc(ctx, len+1);
|
adamc@959
|
2468 memcpy(r, s+start, len);
|
adamc@829
|
2469 r[len] = 0;
|
adamc@829
|
2470 return r;
|
adamc@829
|
2471 }
|
phurst@1981
|
2472
|
adamc@829
|
2473 }
|
adamc@829
|
2474
|
adamc@1023
|
2475 uw_Basis_string uw_Basis_str1(uw_context ctx, uw_Basis_char ch) {
|
adamc@1023
|
2476 char *r;
|
adamc@1023
|
2477
|
adamc@1023
|
2478 uw_check_heap(ctx, 2);
|
adamc@1023
|
2479 r = ctx->heap.front;
|
adamc@1023
|
2480 r[0] = ch;
|
adamc@1023
|
2481 r[1] = 0;
|
adamc@1023
|
2482
|
adamc@1023
|
2483 ctx->heap.front += 2;
|
adamc@1023
|
2484
|
adamc@1023
|
2485 return r;
|
adamc@1023
|
2486 }
|
adamc@1023
|
2487
|
adamc@737
|
2488 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@828
|
2489 int len = uw_Basis_strlen(ctx, s1) + 1;
|
adamc@278
|
2490 char *s;
|
adamc@278
|
2491
|
adamc@311
|
2492 uw_check_heap(ctx, len);
|
adamc@278
|
2493
|
adamc@666
|
2494 s = ctx->heap.front;
|
adamc@278
|
2495
|
adamc@278
|
2496 strcpy(s, s1);
|
adamc@666
|
2497 ctx->heap.front += len;
|
adamc@278
|
2498
|
adamc@278
|
2499 return s;
|
adamc@278
|
2500 }
|
adamc@280
|
2501
|
adam@1295
|
2502 uw_Basis_string uw_dup_and_clear_error_message(uw_context ctx) {
|
adam@1295
|
2503 if (ctx->error_message[0]) {
|
adam@1295
|
2504 char *s = uw_strdup(ctx, ctx->error_message);
|
adam@1295
|
2505 ctx->error_message[0] = 0;
|
adam@1295
|
2506 return s;
|
adam@1295
|
2507 } else
|
adam@1295
|
2508 return NULL;
|
adam@1295
|
2509 }
|
adam@1295
|
2510
|
adamc@737
|
2511 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@493
|
2512 if (s1)
|
adamc@737
|
2513 return uw_strdup(ctx, s1);
|
adamc@493
|
2514 else
|
adamc@493
|
2515 return NULL;
|
adamc@493
|
2516 }
|
adamc@493
|
2517
|
adamc@742
|
2518 char *uw_memdup(uw_context ctx, const char *p, size_t len) {
|
adamc@742
|
2519 char *r = uw_malloc(ctx, len);
|
adamc@742
|
2520 memcpy(r, p, len);
|
adamc@742
|
2521 return r;
|
adamc@742
|
2522 }
|
adamc@280
|
2523
|
adamc@879
|
2524 char *uw_sqlfmtInt = "%lld::int8%n";
|
adamc@879
|
2525
|
adamc@311
|
2526 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@281
|
2527 int len;
|
adamc@281
|
2528 char *r;
|
adamc@281
|
2529
|
adamc@311
|
2530 uw_check_heap(ctx, INTS_MAX + 6);
|
adamc@666
|
2531 r = ctx->heap.front;
|
adamc@879
|
2532 sprintf(r, uw_sqlfmtInt, n, &len);
|
adamc@666
|
2533 ctx->heap.front += len+1;
|
adamc@281
|
2534 return r;
|
adamc@281
|
2535 }
|
adamc@281
|
2536
|
adamc@467
|
2537 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
|
adamc@467
|
2538 if (n == NULL)
|
adamc@467
|
2539 return "NULL";
|
adamc@467
|
2540 else
|
adamc@467
|
2541 return uw_Basis_sqlifyInt(ctx, *n);
|
adamc@467
|
2542 }
|
adamc@467
|
2543
|
adam@1920
|
2544 char *uw_sqlfmtFloat = "%.16g::float8%n";
|
adamc@879
|
2545
|
adamc@311
|
2546 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@281
|
2547 int len;
|
adamc@281
|
2548 char *r;
|
adamc@281
|
2549
|
adamc@311
|
2550 uw_check_heap(ctx, FLOATS_MAX + 8);
|
adamc@666
|
2551 r = ctx->heap.front;
|
adamc@879
|
2552 sprintf(r, uw_sqlfmtFloat, n, &len);
|
adamc@666
|
2553 ctx->heap.front += len+1;
|
adamc@281
|
2554 return r;
|
adamc@281
|
2555 }
|
adamc@281
|
2556
|
adamc@467
|
2557 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
|
adamc@467
|
2558 if (n == NULL)
|
adamc@467
|
2559 return "NULL";
|
adamc@467
|
2560 else
|
adamc@467
|
2561 return uw_Basis_sqlifyFloat(ctx, *n);
|
adamc@467
|
2562 }
|
adamc@467
|
2563
|
adam@1834
|
2564 int uw_Estrings = 1, uw_sql_type_annotations = 1;
|
adamc@879
|
2565 char *uw_sqlsuffixString = "::text";
|
adamc@1011
|
2566 char *uw_sqlsuffixChar = "::char";
|
adamc@281
|
2567
|
adamc@311
|
2568 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@280
|
2569 char *r, *s2;
|
adamc@280
|
2570
|
adamc@879
|
2571 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString));
|
adamc@280
|
2572
|
adamc@666
|
2573 r = s2 = ctx->heap.front;
|
adamc@879
|
2574 if (uw_Estrings)
|
adamc@879
|
2575 *s2++ = 'E';
|
adamc@280
|
2576 *s2++ = '\'';
|
adamc@280
|
2577
|
adamc@280
|
2578 for (; *s; s++) {
|
adamc@280
|
2579 char c = *s;
|
adamc@280
|
2580
|
adamc@280
|
2581 switch (c) {
|
adamc@280
|
2582 case '\'':
|
adamc@885
|
2583 if (uw_Estrings)
|
adamc@885
|
2584 strcpy(s2, "\\'");
|
adamc@885
|
2585 else
|
adamc@885
|
2586 strcpy(s2, "''");
|
adamc@280
|
2587 s2 += 2;
|
adamc@280
|
2588 break;
|
adamc@280
|
2589 case '\\':
|
adamc@885
|
2590 if (uw_Estrings) {
|
adamc@885
|
2591 strcpy(s2, "\\\\");
|
adamc@885
|
2592 s2 += 2;
|
adamc@885
|
2593 } else
|
adamc@885
|
2594 *s2++ = '\\';
|
adamc@280
|
2595 break;
|
adamc@280
|
2596 default:
|
adamc@1150
|
2597 if (isprint((int)c))
|
adamc@280
|
2598 *s2++ = c;
|
adamc@879
|
2599 else if (uw_Estrings) {
|
adam@1857
|
2600 sprintf(s2, "\\%03o", (unsigned char)c);
|
adamc@280
|
2601 s2 += 4;
|
adamc@280
|
2602 }
|
adamc@879
|
2603 else
|
adam@1689
|
2604 *s2++ = c; // I hope this is safe to do... don't know how to support UTF-8 outside Postgres otherwise!
|
adamc@280
|
2605 }
|
adamc@280
|
2606 }
|
adamc@280
|
2607
|
adamc@879
|
2608 *s2++ = '\'';
|
adamc@879
|
2609 strcpy(s2, uw_sqlsuffixString);
|
adamc@879
|
2610 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString);
|
adamc@280
|
2611 return r;
|
adamc@280
|
2612 }
|
adamc@281
|
2613
|
adamc@1011
|
2614 uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) {
|
adamc@1011
|
2615 char *r, *s2;
|
adamc@1011
|
2616
|
adamc@1011
|
2617 uw_check_heap(ctx, 5 + uw_Estrings + strlen(uw_sqlsuffixChar));
|
adamc@1011
|
2618
|
adamc@1011
|
2619 r = s2 = ctx->heap.front;
|
adamc@1011
|
2620 if (uw_Estrings)
|
adamc@1011
|
2621 *s2++ = 'E';
|
adamc@1011
|
2622 *s2++ = '\'';
|
adamc@1011
|
2623
|
adamc@1011
|
2624 switch (c) {
|
adamc@1011
|
2625 case '\'':
|
adamc@1011
|
2626 if (uw_Estrings)
|
adamc@1011
|
2627 strcpy(s2, "\\'");
|
adamc@1011
|
2628 else
|
adamc@1011
|
2629 strcpy(s2, "''");
|
adamc@1011
|
2630 s2 += 2;
|
adamc@1011
|
2631 break;
|
adamc@1011
|
2632 case '\\':
|
adamc@1011
|
2633 if (uw_Estrings) {
|
adamc@1011
|
2634 strcpy(s2, "\\\\");
|
adamc@1011
|
2635 s2 += 2;
|
adamc@1011
|
2636 } else
|
adamc@1011
|
2637 *s2++ = '\\';
|
adamc@1011
|
2638 break;
|
adamc@1011
|
2639 default:
|
adamc@1150
|
2640 if (isprint((int)c))
|
adamc@1011
|
2641 *s2++ = c;
|
adamc@1011
|
2642 else if (uw_Estrings) {
|
adam@1857
|
2643 sprintf(s2, "\\%03o", (unsigned char)c);
|
adamc@1011
|
2644 s2 += 4;
|
adamc@1011
|
2645 }
|
adamc@1011
|
2646 else
|
adamc@1011
|
2647 uw_error(ctx, FATAL, "Non-printable character %u in char to SQLify", c);
|
adamc@1011
|
2648 }
|
adamc@1011
|
2649
|
adamc@1011
|
2650 *s2++ = '\'';
|
adamc@1011
|
2651 strcpy(s2, uw_sqlsuffixChar);
|
adamc@1011
|
2652 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixChar);
|
adamc@1011
|
2653 return r;
|
adamc@1011
|
2654 }
|
adamc@1011
|
2655
|
adamc@879
|
2656 char *uw_sqlsuffixBlob = "::bytea";
|
adamc@879
|
2657
|
adam@1867
|
2658 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
|
adamc@737
|
2659 char *r, *s2;
|
adamc@737
|
2660 size_t i;
|
adamc@737
|
2661
|
adam@1867
|
2662 uw_check_heap(ctx, b.size * 5 + 4 + strlen(uw_sqlsuffixBlob));
|
adamc@737
|
2663
|
adamc@737
|
2664 r = s2 = ctx->heap.front;
|
adamc@879
|
2665 if (uw_Estrings)
|
adamc@879
|
2666 *s2++ = 'E';
|
adam@1867
|
2667 else
|
adam@1867
|
2668 *s2++ = 'X';
|
adamc@737
|
2669 *s2++ = '\'';
|
adamc@737
|
2670
|
adamc@737
|
2671 for (i = 0; i < b.size; ++i) {
|
adam@1867
|
2672 unsigned char c = b.data[i];
|
adam@1867
|
2673
|
adam@1867
|
2674 if (uw_Estrings) {
|
adam@1867
|
2675 switch (c) {
|
adam@1867
|
2676 case '\'':
|
adamc@885
|
2677 strcpy(s2, "\\'");
|
adam@1867
|
2678 s2 += 2;
|
adam@1867
|
2679 break;
|
adam@1867
|
2680 case '\\':
|
adamc@885
|
2681 strcpy(s2, "\\\\\\\\");
|
adamc@885
|
2682 s2 += 4;
|
adam@1867
|
2683 break;
|
adam@1867
|
2684 default:
|
adam@1867
|
2685 if (isprint((int)c))
|
adam@1867
|
2686 *s2++ = c;
|
adam@1867
|
2687 else {
|
adam@1867
|
2688 sprintf(s2, "\\\\%03o", c);
|
adam@1867
|
2689 s2 += 5;
|
adam@1867
|
2690 }
|
adamc@737
|
2691 }
|
adam@1867
|
2692 } else {
|
adam@1867
|
2693 sprintf(s2, "%02X", c);
|
adam@1867
|
2694 s2 += 2;
|
adamc@737
|
2695 }
|
phurst@1981
|
2696 }
|
adamc@737
|
2697
|
adamc@879
|
2698 *s2++ = '\'';
|
adamc@879
|
2699 strcpy(s2, uw_sqlsuffixBlob);
|
adamc@879
|
2700 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob);
|
adamc@737
|
2701 return r;
|
adamc@737
|
2702 }
|
adamc@737
|
2703
|
adamc@682
|
2704 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
2705 int len;
|
adamc@682
|
2706 char *r;
|
adamc@682
|
2707 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
|
adamc@682
|
2708
|
adamc@682
|
2709 uw_check_heap(ctx, INTS_MAX + 7);
|
adamc@682
|
2710 r = ctx->heap.front;
|
adamc@879
|
2711 sprintf(r, uw_sqlfmtInt, combo, &len);
|
adamc@682
|
2712 ctx->heap.front += len+1;
|
adamc@682
|
2713 return r;
|
adamc@682
|
2714 }
|
adamc@682
|
2715
|
adamc@682
|
2716 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
2717 int len;
|
adamc@682
|
2718 char *r;
|
adamc@682
|
2719 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
|
adamc@682
|
2720
|
adamc@682
|
2721 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
2722 r = ctx->heap.front;
|
adamc@682
|
2723 sprintf(r, "%lld%n", combo, &len);
|
adamc@682
|
2724 ctx->heap.front += len+1;
|
adamc@682
|
2725 return r;
|
adamc@682
|
2726 }
|
adamc@682
|
2727
|
adamc@879
|
2728 char *uw_sqlfmtUint4 = "%u::int4%n";
|
adamc@879
|
2729
|
adamc@682
|
2730 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
|
adamc@682
|
2731 int len;
|
adamc@682
|
2732 char *r;
|
adamc@682
|
2733
|
adamc@682
|
2734 uw_check_heap(ctx, INTS_MAX + 7);
|
adamc@682
|
2735 r = ctx->heap.front;
|
adamc@879
|
2736 sprintf(r, uw_sqlfmtUint4, cli, &len);
|
adamc@682
|
2737 ctx->heap.front += len+1;
|
adamc@682
|
2738 return r;
|
adamc@682
|
2739 }
|
adamc@682
|
2740
|
adamc@682
|
2741 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
|
adamc@682
|
2742 int len;
|
adamc@682
|
2743 char *r;
|
adamc@682
|
2744
|
adamc@682
|
2745 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
2746 r = ctx->heap.front;
|
adamc@682
|
2747 sprintf(r, "%u%n", cli, &len);
|
adamc@682
|
2748 ctx->heap.front += len+1;
|
adamc@682
|
2749 return r;
|
adamc@682
|
2750 }
|
adamc@682
|
2751
|
adamc@467
|
2752 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
|
adamc@467
|
2753 if (s == NULL)
|
adamc@467
|
2754 return "NULL";
|
adamc@467
|
2755 else
|
adamc@467
|
2756 return uw_Basis_sqlifyString(ctx, s);
|
adamc@467
|
2757 }
|
adamc@467
|
2758
|
adamc@311
|
2759 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2760 if (b == uw_Basis_False)
|
adamc@281
|
2761 return "FALSE";
|
adamc@281
|
2762 else
|
adamc@281
|
2763 return "TRUE";
|
adamc@281
|
2764 }
|
adamc@282
|
2765
|
adamc@467
|
2766 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
|
adamc@467
|
2767 if (b == NULL)
|
adamc@467
|
2768 return "NULL";
|
adamc@467
|
2769 else
|
adamc@467
|
2770 return uw_Basis_sqlifyBool(ctx, *b);
|
adamc@467
|
2771 }
|
adamc@467
|
2772
|
adamc@439
|
2773 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@439
|
2774 size_t len;
|
adamc@682
|
2775 char *r, *s;
|
adam@1392
|
2776 struct tm stm = {};
|
adam@1392
|
2777 stm.tm_isdst = -1;
|
adamc@682
|
2778
|
adam@1365
|
2779 if (localtime_r(&t.seconds, &stm)) {
|
adamc@682
|
2780 s = uw_malloc(ctx, TIMES_MAX);
|
adam@1364
|
2781 len = strftime(s, TIMES_MAX, TIME_FMT_PG, &stm);
|
adam@1834
|
2782 if (uw_sql_type_annotations) {
|
adam@1834
|
2783 if (t.microseconds) {
|
adam@1834
|
2784 r = uw_malloc(ctx, len + 21);
|
adam@1834
|
2785 sprintf(r, "'%s.%06u'::timestamp", s, t.microseconds);
|
adam@1834
|
2786 } else {
|
adam@1834
|
2787 r = uw_malloc(ctx, len + 14);
|
adam@1834
|
2788 sprintf(r, "'%s'::timestamp", s);
|
adam@1834
|
2789 }
|
adam@1367
|
2790 } else {
|
adam@1834
|
2791 r = uw_malloc(ctx, len + 3);
|
adam@1834
|
2792 sprintf(r, "'%s'", s);
|
adam@1367
|
2793 }
|
adamc@682
|
2794 return r;
|
adamc@682
|
2795 } else
|
adamc@682
|
2796 return "<Invalid time>";
|
adamc@682
|
2797 }
|
adamc@682
|
2798
|
adamc@682
|
2799 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@682
|
2800 size_t len;
|
adamc@439
|
2801 char *r;
|
adam@1392
|
2802 struct tm stm = {};
|
adam@1392
|
2803 stm.tm_isdst = -1;
|
adamc@439
|
2804
|
adam@1365
|
2805 if (localtime_r(&t.seconds, &stm)) {
|
adamc@439
|
2806 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
2807 r = ctx->heap.front;
|
adamc@439
|
2808 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
2809 ctx->heap.front += len+1;
|
adamc@439
|
2810 return r;
|
adamc@439
|
2811 } else
|
adamc@439
|
2812 return "<Invalid time>";
|
adamc@439
|
2813 }
|
adamc@439
|
2814
|
adam@1364
|
2815 char *uw_Basis_ensqlTime(uw_context ctx, uw_Basis_time t) {
|
adam@1364
|
2816 size_t len;
|
adam@1364
|
2817 char *r;
|
adam@1392
|
2818 struct tm stm = {};
|
adam@1392
|
2819 stm.tm_isdst = -1;
|
adam@1364
|
2820
|
adam@1365
|
2821 if (localtime_r(&t.seconds, &stm)) {
|
adam@1364
|
2822 uw_check_heap(ctx, TIMES_MAX);
|
adam@1364
|
2823 r = ctx->heap.front;
|
adam@1365
|
2824 len = strftime(r, TIMES_MAX-7, TIME_FMT_PG, &stm);
|
adam@1365
|
2825 ctx->heap.front += len;
|
adam@1365
|
2826 sprintf(ctx->heap.front, ".%06u", t.microseconds);
|
adam@1365
|
2827 ctx->heap.front += 8;
|
adam@1364
|
2828 return r;
|
adam@1364
|
2829 } else
|
adam@1364
|
2830 return "<Invalid time>";
|
adam@1364
|
2831 }
|
adam@1364
|
2832
|
adamc@467
|
2833 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
|
adamc@467
|
2834 if (t == NULL)
|
adamc@467
|
2835 return "NULL";
|
adamc@467
|
2836 else
|
adamc@467
|
2837 return uw_Basis_sqlifyTime(ctx, *t);
|
adamc@467
|
2838 }
|
adamc@467
|
2839
|
adamc@311
|
2840 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
|
adamc@311
|
2841 static uw_Basis_int true = 1;
|
adamc@311
|
2842 static uw_Basis_int false = 0;
|
adamc@282
|
2843
|
adamc@311
|
2844 if (b == uw_Basis_False)
|
adamc@282
|
2845 return (char *)&false;
|
adamc@282
|
2846 else
|
adamc@282
|
2847 return (char *)&true;
|
adamc@282
|
2848 }
|
adamc@284
|
2849
|
adamc@311
|
2850 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
|
adamc@284
|
2851 int len;
|
adamc@284
|
2852 char *r;
|
adamc@284
|
2853
|
adamc@311
|
2854 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
2855 r = ctx->heap.front;
|
adamc@284
|
2856 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
2857 ctx->heap.front += len+1;
|
adamc@284
|
2858 return r;
|
adamc@284
|
2859 }
|
adamc@285
|
2860
|
adamc@311
|
2861 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
|
adamc@285
|
2862 int len;
|
adamc@285
|
2863 char *r;
|
adamc@285
|
2864
|
adamc@311
|
2865 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
2866 r = ctx->heap.front;
|
adamc@285
|
2867 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
2868 ctx->heap.front += len+1;
|
adamc@285
|
2869 return r;
|
adamc@285
|
2870 }
|
adamc@285
|
2871
|
adamc@821
|
2872 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
|
adamc@821
|
2873 char *r = uw_malloc(ctx, 2);
|
adamc@821
|
2874 r[0] = ch;
|
adamc@821
|
2875 r[1] = 0;
|
adamc@821
|
2876 return r;
|
adamc@821
|
2877 }
|
adamc@821
|
2878
|
adamc@311
|
2879 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2880 if (b == uw_Basis_False)
|
adamc@285
|
2881 return "False";
|
adamc@285
|
2882 else
|
adamc@285
|
2883 return "True";
|
adamc@285
|
2884 }
|
adamc@288
|
2885
|
adam@1359
|
2886 uw_Basis_string uw_Basis_timef(uw_context ctx, const char *fmt, uw_Basis_time t) {
|
adam@1352
|
2887 size_t len;
|
adam@1352
|
2888 char *r;
|
adam@1392
|
2889 struct tm stm = {};
|
adam@1392
|
2890 stm.tm_isdst = -1;
|
adam@1352
|
2891
|
adam@1365
|
2892 if (localtime_r(&t.seconds, &stm)) {
|
adam@1352
|
2893 uw_check_heap(ctx, TIMES_MAX);
|
adam@1352
|
2894 r = ctx->heap.front;
|
adam@1352
|
2895 len = strftime(r, TIMES_MAX, fmt, &stm);
|
adam@1352
|
2896 ctx->heap.front += len+1;
|
adam@1352
|
2897 return r;
|
adam@1352
|
2898 } else
|
adam@1352
|
2899 return "<Invalid time>";
|
adam@1352
|
2900 }
|
adam@1352
|
2901
|
adam@1629
|
2902 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
|
adam@1629
|
2903 return uw_Basis_timef(ctx, ctx->app->time_format, t);
|
adam@1629
|
2904 }
|
adam@1629
|
2905
|
adamc@311
|
2906 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
|
adamc@288
|
2907 char *endptr;
|
adamc@311
|
2908 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@288
|
2909
|
adamc@288
|
2910 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
2911 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adamc@288
|
2912 *r = n;
|
adamc@288
|
2913 return r;
|
adamc@288
|
2914 } else
|
adamc@288
|
2915 return NULL;
|
adamc@288
|
2916 }
|
adamc@289
|
2917
|
adamc@311
|
2918 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
|
adamc@289
|
2919 char *endptr;
|
adamc@311
|
2920 uw_Basis_float n = strtod(s, &endptr);
|
adamc@289
|
2921
|
adamc@289
|
2922 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
2923 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
|
adamc@289
|
2924 *r = n;
|
adamc@289
|
2925 return r;
|
adamc@289
|
2926 } else
|
adamc@289
|
2927 return NULL;
|
adamc@289
|
2928 }
|
adamc@289
|
2929
|
adamc@821
|
2930 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
|
adamc@821
|
2931 if (s[0] == 0) {
|
adamc@821
|
2932 uw_Basis_char *r = uw_malloc(ctx, 1);
|
adamc@821
|
2933 r[0] = 0;
|
adamc@821
|
2934 return r;
|
adamc@821
|
2935 } else if (s[1] != 0)
|
adamc@821
|
2936 return NULL;
|
adamc@821
|
2937 else {
|
adamc@821
|
2938 uw_Basis_char *r = uw_malloc(ctx, 1);
|
adamc@821
|
2939 r[0] = s[0];
|
adamc@821
|
2940 return r;
|
adamc@821
|
2941 }
|
adamc@821
|
2942 }
|
adamc@821
|
2943
|
adamc@311
|
2944 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
2945 static uw_Basis_bool true = uw_Basis_True;
|
adamc@311
|
2946 static uw_Basis_bool false = uw_Basis_False;
|
adamc@289
|
2947
|
adamc@289
|
2948 if (!strcasecmp (s, "True"))
|
adamc@289
|
2949 return &true;
|
adamc@289
|
2950 else if (!strcasecmp (s, "False"))
|
adamc@289
|
2951 return &false;
|
adamc@289
|
2952 else
|
adamc@289
|
2953 return NULL;
|
adamc@289
|
2954 }
|
adamc@292
|
2955
|
adamc@436
|
2956 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
2957 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adam@1374
|
2958 struct tm stm = {};
|
adam@1392
|
2959 stm.tm_isdst = -1;
|
adamc@436
|
2960
|
adamc@439
|
2961 if (dot) {
|
adamc@439
|
2962 *dot = 0;
|
adamc@439
|
2963 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
2964 *dot = '.';
|
adamc@439
|
2965 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1365
|
2966 r->seconds = mktime(&stm);
|
adam@1365
|
2967 r->microseconds = 0;
|
adamc@439
|
2968 return r;
|
adamc@439
|
2969 }
|
adamc@439
|
2970 else {
|
adamc@439
|
2971 *dot = '.';
|
adamc@439
|
2972 return NULL;
|
adamc@439
|
2973 }
|
adamc@436
|
2974 }
|
adamc@439
|
2975 else {
|
adam@2072
|
2976 if (strptime(s, ctx->app->time_format, &stm) == end) {
|
adamc@439
|
2977 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1365
|
2978 r->seconds = mktime(&stm);
|
adam@1365
|
2979 r->microseconds = 0;
|
adamc@439
|
2980 return r;
|
adam@2072
|
2981 } else if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adam@2072
|
2982 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@2072
|
2983 r->seconds = mktime(&stm);
|
adam@2072
|
2984 r->microseconds = 0;
|
adam@2072
|
2985 return r;
|
adam@2072
|
2986 } else if (strptime(s, TIME_FMT, &stm) == end) {
|
adamc@439
|
2987 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1365
|
2988 r->seconds = mktime(&stm);
|
adam@1365
|
2989 r->microseconds = 0;
|
adamc@439
|
2990 return r;
|
adam@1632
|
2991 } else if (strptime(s, TIME_FMT_JS, &stm) == end) {
|
adam@1632
|
2992 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1632
|
2993 r->seconds = mktime(&stm);
|
adam@1632
|
2994 r->microseconds = 0;
|
adam@1632
|
2995 return r;
|
adamc@439
|
2996 }
|
adamc@439
|
2997 else
|
adamc@439
|
2998 return NULL;
|
adamc@439
|
2999 }
|
adamc@436
|
3000 }
|
adamc@436
|
3001
|
adam@1352
|
3002 uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) {
|
adam@1352
|
3003 char *end = strchr(s, 0);
|
adam@1374
|
3004 struct tm stm = {};
|
adam@1392
|
3005 stm.tm_isdst = -1;
|
adam@1352
|
3006
|
adam@1352
|
3007 if (strptime(s, fmt, &stm) == end) {
|
adam@1352
|
3008 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1365
|
3009 r->seconds = mktime(&stm);
|
adam@1365
|
3010 r->microseconds = 0;
|
adam@1352
|
3011 return r;
|
adam@1352
|
3012 }
|
adam@1352
|
3013 else
|
adam@1352
|
3014 return NULL;
|
adam@1352
|
3015 }
|
adam@1352
|
3016
|
adamc@311
|
3017 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
|
adamc@292
|
3018 char *endptr;
|
adamc@311
|
3019 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@292
|
3020
|
adamc@292
|
3021 if (*s != '\0' && *endptr == '\0')
|
adamc@292
|
3022 return n;
|
adamc@292
|
3023 else
|
adam@1444
|
3024 uw_error(ctx, FATAL, "Can't parse int: %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@292
|
3025 }
|
adamc@293
|
3026
|
adamc@682
|
3027 #include <errno.h>
|
adamc@682
|
3028
|
adamc@678
|
3029 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
|
adamc@682
|
3030 unsigned long long n;
|
adamc@682
|
3031
|
adamc@682
|
3032 if (sscanf(s, "%llu", &n) < 1)
|
adam@1444
|
3033 uw_error(ctx, FATAL, "Can't parse channel: %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@682
|
3034 else {
|
adamc@682
|
3035 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
|
adamc@682
|
3036 return ch;
|
adamc@682
|
3037 }
|
adamc@682
|
3038 }
|
adamc@682
|
3039
|
adamc@682
|
3040 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
|
adamc@678
|
3041 char *endptr;
|
adamc@682
|
3042 unsigned long n = strtoul(s, &endptr, 10);
|
adamc@678
|
3043
|
adamc@678
|
3044 if (*s != '\0' && *endptr == '\0')
|
adamc@678
|
3045 return n;
|
adamc@678
|
3046 else
|
adam@1444
|
3047 uw_error(ctx, FATAL, "Can't parse client: %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@678
|
3048 }
|
adamc@678
|
3049
|
adamc@311
|
3050 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
|
adamc@293
|
3051 char *endptr;
|
adamc@311
|
3052 uw_Basis_float n = strtod(s, &endptr);
|
adamc@293
|
3053
|
adamc@293
|
3054 if (*s != '\0' && *endptr == '\0')
|
adamc@293
|
3055 return n;
|
adamc@293
|
3056 else
|
adam@1444
|
3057 uw_error(ctx, FATAL, "Can't parse float: %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@293
|
3058 }
|
adamc@293
|
3059
|
adamc@821
|
3060 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
|
adamc@821
|
3061 if (s[0] == 0)
|
adamc@821
|
3062 return 0;
|
adamc@821
|
3063 else if (s[1] != 0)
|
adam@1444
|
3064 uw_error(ctx, FATAL, "Can't parse char: %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@821
|
3065 else
|
adamc@821
|
3066 return s[0];
|
adamc@821
|
3067 }
|
adamc@821
|
3068
|
adamc@311
|
3069 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
|
adamc@296
|
3070 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
|
adamc@311
|
3071 return uw_Basis_True;
|
adamc@296
|
3072 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
|
adamc@311
|
3073 return uw_Basis_False;
|
adamc@293
|
3074 else
|
adam@1444
|
3075 uw_error(ctx, FATAL, "Can't parse bool: %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@293
|
3076 }
|
adamc@436
|
3077
|
adam@1352
|
3078 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
|
adam@1352
|
3079 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adam@1352
|
3080 struct tm stm = {};
|
adam@1392
|
3081 stm.tm_isdst = -1;
|
adam@1352
|
3082
|
adam@1352
|
3083 if (dot) {
|
adam@1352
|
3084 *dot = 0;
|
adam@1352
|
3085 if (strptime(s, TIME_FMT_PG, &stm)) {
|
adam@1352
|
3086 *dot = '.';
|
adam@1410
|
3087 char usec[] = "000000";
|
adam@1410
|
3088 int len = strlen(dot+1);
|
adam@1410
|
3089 memcpy(usec, dot+1, len < 6 ? len : 6);
|
adam@1410
|
3090 uw_Basis_time r = { mktime(&stm), atoi(usec) };
|
adam@1365
|
3091 return r;
|
adam@1352
|
3092 }
|
adam@1352
|
3093 else {
|
adam@1352
|
3094 *dot = '.';
|
adam@1444
|
3095 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
|
adam@1352
|
3096 }
|
adam@1352
|
3097 }
|
adam@1352
|
3098 else {
|
adam@1352
|
3099 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adam@1365
|
3100 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
3101 return r;
|
adam@1352
|
3102 } else if (strptime(s, TIME_FMT, &stm) == end) {
|
adam@1365
|
3103 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
3104 return r;
|
adam@1352
|
3105 } else
|
adam@1444
|
3106 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
|
adam@1352
|
3107 }
|
adam@1352
|
3108 }
|
adam@1352
|
3109
|
adamc@436
|
3110 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
3111 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
3112 struct tm stm = {};
|
adam@1392
|
3113 stm.tm_isdst = -1;
|
adamc@436
|
3114
|
adamc@438
|
3115 if (dot) {
|
adamc@438
|
3116 *dot = 0;
|
adamc@438
|
3117 if (strptime(s, TIME_FMT_PG, &stm)) {
|
adamc@438
|
3118 *dot = '.';
|
adam@1365
|
3119 {
|
adam@1365
|
3120 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
3121 return r;
|
adam@1365
|
3122 }
|
adamc@438
|
3123 }
|
adamc@438
|
3124 else {
|
adamc@438
|
3125 *dot = '.';
|
adam@1444
|
3126 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@438
|
3127 }
|
adamc@438
|
3128 }
|
adamc@438
|
3129 else {
|
adam@2072
|
3130 if (strptime(s, ctx->app->time_format, &stm) == end) {
|
adam@2072
|
3131 uw_Basis_time r = { mktime(&stm) };
|
adam@2072
|
3132 return r;
|
adam@2072
|
3133 } else if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adam@1365
|
3134 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
3135 return r;
|
adam@1365
|
3136 } else if (strptime(s, TIME_FMT, &stm) == end) {
|
adam@1365
|
3137 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
3138 return r;
|
adam@1632
|
3139 } else if (strptime(s, TIME_FMT_JS, &stm) == end) {
|
adam@1632
|
3140 uw_Basis_time r = { mktime(&stm) };
|
adam@1632
|
3141 return r;
|
adam@1365
|
3142 } else
|
adam@1444
|
3143 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@438
|
3144 }
|
adamc@436
|
3145 }
|
adamc@457
|
3146
|
adam@1352
|
3147 uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) {
|
adam@1352
|
3148 char *end = strchr(s, 0);
|
adamc@939
|
3149 struct tm stm = {};
|
adam@1392
|
3150 stm.tm_isdst = -1;
|
adamc@939
|
3151
|
adam@1365
|
3152 if (strptime(s, fmt, &stm) == end) {
|
adam@1365
|
3153 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
3154 return r;
|
adam@1365
|
3155 } else
|
adam@1444
|
3156 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@939
|
3157 }
|
adamc@939
|
3158
|
adamc@742
|
3159 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
|
adamc@742
|
3160 char *r = ctx->heap.front;
|
adamc@742
|
3161 uw_Basis_blob b = {len, r};
|
adamc@742
|
3162
|
adamc@742
|
3163 uw_check_heap(ctx, len);
|
adamc@742
|
3164
|
adam@1992
|
3165 if (s[0] == '\\' && s[1] == 'x') {
|
adam@1992
|
3166 s += 2;
|
adam@1992
|
3167
|
adam@1992
|
3168 while (*s) {
|
adam@1992
|
3169 int n;
|
adam@1992
|
3170 sscanf(s, "%02x", &n);
|
adam@1992
|
3171 *r++ = n;
|
adam@1992
|
3172 s += 2;
|
adam@1992
|
3173 }
|
adam@1992
|
3174 } else {
|
adam@1992
|
3175 while (*s) {
|
adam@1992
|
3176 if (s[0] == '\\') {
|
adam@1992
|
3177 if (s[1] == '\\') {
|
adam@1992
|
3178 *r++ = '\\';
|
adam@1992
|
3179 s += 2;
|
adam@1992
|
3180 } else if (isdigit((int)s[1]) && isdigit((int)s[2]) && isdigit((int)s[3])) {
|
adam@1992
|
3181 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
|
adam@1992
|
3182 s += 4;
|
adam@1992
|
3183 }
|
adam@1992
|
3184 else {
|
adam@1992
|
3185 *r++ = '\\';
|
adam@1992
|
3186 ++s;
|
adam@1992
|
3187 }
|
adam@1992
|
3188 } else {
|
adam@1992
|
3189 *r++ = s[0];
|
adamc@742
|
3190 ++s;
|
adamc@742
|
3191 }
|
adamc@742
|
3192 }
|
adamc@742
|
3193 }
|
adamc@742
|
3194
|
adamc@742
|
3195 b.size = r - ctx->heap.front;
|
adamc@742
|
3196 ctx->heap.front = r;
|
adam@1992
|
3197
|
adamc@742
|
3198 return b;
|
adamc@742
|
3199 }
|
adamc@742
|
3200
|
adam@1402
|
3201 #define THE_PAST "expires=Sat, 01-Jan-2011 00:00:00 GMT"
|
adamc@1052
|
3202
|
adamc@463
|
3203 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
|
adamc@463
|
3204 int len = strlen(c);
|
adamc@854
|
3205 char *p = ctx->outHeaders.start;
|
adamc@474
|
3206
|
adamc@1134
|
3207 while ((p = strstr(p, "\nSet-Cookie: "))) {
|
adamc@474
|
3208 char *p2;
|
adamc@474
|
3209 p += 13;
|
adamc@474
|
3210 p2 = strchr(p, '=');
|
adamc@474
|
3211
|
adamc@474
|
3212 if (p2) {
|
adamc@474
|
3213 size_t sz = strcspn(p2+1, ";\r\n");
|
adamc@474
|
3214
|
adamc@474
|
3215 if (!strncasecmp(p, c, p2 - p)) {
|
adamc@1052
|
3216 if (sz == 0 && strstr(p2+2, THE_PAST))
|
adamc@1052
|
3217 return NULL;
|
adamc@1052
|
3218 else {
|
adamc@1052
|
3219 char *ret = uw_malloc(ctx, sz + 1);
|
adamc@1052
|
3220 memcpy(ret, p2+1, sz);
|
adamc@1052
|
3221 ret[sz] = 0;
|
adamc@1052
|
3222 return ret;
|
adamc@1052
|
3223 }
|
adamc@474
|
3224 }
|
adamc@474
|
3225 }
|
adamc@474
|
3226 }
|
adamc@463
|
3227
|
adamc@1134
|
3228 if ((p = uw_Basis_requestHeader(ctx, "Cookie"))) {
|
adamc@1051
|
3229 char *p2;
|
adamc@1051
|
3230
|
adamc@854
|
3231 while (1) {
|
adamc@1051
|
3232 if (!strncmp(p, c, len) && p[len] == '=') {
|
adamc@1134
|
3233 if ((p2 = strchr(p, ';'))) {
|
adamc@1051
|
3234 size_t n = p2 - (p + len);
|
adamc@1051
|
3235 char *r = uw_malloc(ctx, n);
|
adamc@1051
|
3236 memcpy(r, p + 1 + len, n-1);
|
adamc@1051
|
3237 r[n-1] = 0;
|
adamc@1051
|
3238 return r;
|
adamc@1051
|
3239 } else
|
adamc@1051
|
3240 return p + 1 + len;
|
adamc@1134
|
3241 } else if ((p = strchr(p, ';')))
|
adamc@854
|
3242 p += 2;
|
adamc@463
|
3243 else
|
adamc@463
|
3244 return NULL;
|
adamc@463
|
3245 }
|
adamc@463
|
3246 }
|
adamc@792
|
3247
|
adamc@792
|
3248 return NULL;
|
adamc@457
|
3249 }
|
adamc@462
|
3250
|
adam@1877
|
3251 static void set_cookie(uw_context ctx) {
|
adam@1877
|
3252 if (ctx->usedSig)
|
adam@1877
|
3253 ctx->needsResig = 1;
|
adam@1877
|
3254 }
|
adam@1877
|
3255
|
adamc@1050
|
3256 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
|
3257 uw_write_header(ctx, "Set-Cookie: ");
|
adamc@462
|
3258 uw_write_header(ctx, c);
|
adamc@462
|
3259 uw_write_header(ctx, "=");
|
adamc@462
|
3260 uw_write_header(ctx, v);
|
adamc@466
|
3261 uw_write_header(ctx, "; path=");
|
adamc@466
|
3262 uw_write_header(ctx, prefix);
|
adamc@1050
|
3263 if (expires) {
|
adamc@1050
|
3264 char formatted[30];
|
adam@1392
|
3265 struct tm tm = {};
|
adam@1392
|
3266 tm.tm_isdst = -1;
|
adamc@1050
|
3267
|
adam@1365
|
3268 gmtime_r(&expires->seconds, &tm);
|
adamc@1050
|
3269
|
adamc@1050
|
3270 strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm);
|
adamc@1050
|
3271
|
adamc@1050
|
3272 uw_write_header(ctx, "; expires=");
|
adamc@1050
|
3273 uw_write_header(ctx, formatted);
|
adamc@1050
|
3274 }
|
adamc@1050
|
3275 if (secure)
|
adamc@1050
|
3276 uw_write_header(ctx, "; secure");
|
adamc@462
|
3277 uw_write_header(ctx, "\r\n");
|
adam@1877
|
3278 set_cookie(ctx);
|
adamc@462
|
3279
|
adamc@462
|
3280 return uw_unit_v;
|
adamc@462
|
3281 }
|
adamc@667
|
3282
|
adamc@1050
|
3283 uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c) {
|
adamc@1050
|
3284 uw_write_header(ctx, "Set-Cookie: ");
|
adamc@1050
|
3285 uw_write_header(ctx, c);
|
adamc@1050
|
3286 uw_write_header(ctx, "=; path=");
|
adamc@1050
|
3287 uw_write_header(ctx, prefix);
|
adamc@1052
|
3288 uw_write_header(ctx, "; " THE_PAST "\r\n");
|
adam@1877
|
3289 set_cookie(ctx);
|
adamc@1050
|
3290
|
adamc@1050
|
3291 return uw_unit_v;
|
adamc@1050
|
3292 }
|
adamc@1050
|
3293
|
adamc@1113
|
3294 size_t uw_deltas_max = SIZE_MAX;
|
adamc@1113
|
3295
|
adamc@682
|
3296 static delta *allocate_delta(uw_context ctx, unsigned client) {
|
adamc@682
|
3297 unsigned i;
|
adamc@682
|
3298 delta *d;
|
adamc@671
|
3299
|
adamc@682
|
3300 for (i = 0; i < ctx->used_deltas; ++i)
|
adamc@682
|
3301 if (ctx->deltas[i].client == client)
|
adamc@682
|
3302 return &ctx->deltas[i];
|
adamc@671
|
3303
|
adamc@682
|
3304 if (ctx->used_deltas >= ctx->n_deltas) {
|
adamc@1113
|
3305 if (ctx->n_deltas + 1 > uw_deltas_max)
|
adamc@1113
|
3306 uw_error(ctx, FATAL, "Exceeded limit on number of deltas");
|
adamc@1113
|
3307
|
adamc@682
|
3308 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
|
adam@1369
|
3309 uw_buffer_init(uw_messages_max, &ctx->deltas[ctx->n_deltas-1].msgs, 0);
|
adamc@682
|
3310 }
|
adamc@671
|
3311
|
adamc@682
|
3312 d = &ctx->deltas[ctx->used_deltas++];
|
adamc@682
|
3313 d->client = client;
|
adam@1369
|
3314 uw_buffer_reset(&d->msgs);
|
adamc@682
|
3315 return d;
|
adamc@671
|
3316 }
|
adamc@671
|
3317
|
adamc@668
|
3318 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
|
adamc@682
|
3319 if (ctx->client == NULL)
|
adamc@682
|
3320 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
|
adamc@671
|
3321
|
adamc@682
|
3322 return new_channel(ctx->client);
|
adamc@668
|
3323 }
|
adamc@668
|
3324
|
adamc@668
|
3325 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
|
adamc@682
|
3326 delta *d = allocate_delta(ctx, chn.cli);
|
adamc@682
|
3327 size_t len;
|
adamc@682
|
3328 int preLen;
|
adamc@682
|
3329 char pre[INTS_MAX + 2];
|
adamc@668
|
3330
|
adamc@682
|
3331 len = strlen(msg);
|
adamc@682
|
3332
|
adamc@682
|
3333 sprintf(pre, "%u\n%n", chn.chn, &preLen);
|
adamc@682
|
3334
|
adam@1369
|
3335 ctx_uw_buffer_append(ctx, "messages", &d->msgs, pre, preLen);
|
adam@1369
|
3336 ctx_uw_buffer_append(ctx, "messages", &d->msgs, msg, len);
|
adam@1369
|
3337 ctx_uw_buffer_append(ctx, "messages", &d->msgs, "\n", 1);
|
adamc@671
|
3338
|
adamc@671
|
3339 return uw_unit_v;
|
adamc@668
|
3340 }
|
adamc@671
|
3341
|
adam@1327
|
3342 int uw_rollback(uw_context ctx, int will_retry) {
|
adamc@1131
|
3343 int i;
|
adamc@1131
|
3344 cleanup *cl;
|
adamc@1131
|
3345
|
adamc@1131
|
3346 if (ctx->client)
|
adamc@1131
|
3347 release_client(ctx->client);
|
adamc@1131
|
3348
|
adamc@1131
|
3349 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@1131
|
3350 cl->func(cl->arg);
|
adamc@1131
|
3351
|
adamc@1131
|
3352 ctx->cleanup_front = ctx->cleanup;
|
adamc@1131
|
3353
|
adamc@1131
|
3354 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adamc@1131
|
3355 if (ctx->transactionals[i].rollback != NULL)
|
adamc@1131
|
3356 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
|
adamc@1131
|
3357
|
adamc@1131
|
3358 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adamc@1131
|
3359 if (ctx->transactionals[i].free)
|
adam@1327
|
3360 ctx->transactionals[i].free(ctx->transactionals[i].data, will_retry);
|
adamc@1131
|
3361
|
adam@1941
|
3362 if (ctx->app && ctx->transaction_started) {
|
adam@1941
|
3363 ctx->transaction_started = 0;
|
adam@1941
|
3364 return ctx->app->db_rollback(ctx);
|
adam@1941
|
3365 } else
|
adam@1941
|
3366 return 0;
|
adamc@1131
|
3367 }
|
adamc@1131
|
3368
|
adam@1956
|
3369 const char uw_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@1956
|
3370 const char uw_begin_html5[] = "<!DOCTYPE html><html>";
|
adam@1728
|
3371
|
adam@1877
|
3372 extern int uw_hash_blocksize;
|
adam@1877
|
3373
|
adam@1877
|
3374 static const char sig_intro[] = "<input type=\"hidden\" name=\"Sig\" value=\"";
|
adam@1877
|
3375
|
adam@1877
|
3376 static char *find_sig(char *haystack) {
|
adam@1877
|
3377 int i;
|
adam@1877
|
3378 char *s = strstr(haystack, sig_intro);
|
adam@1877
|
3379
|
adam@1877
|
3380 if (!s || strlen(haystack) - (s - haystack) - (sizeof sig_intro - 1) < uw_hash_blocksize*2+1)
|
adam@1877
|
3381 return NULL;
|
phurst@1981
|
3382
|
adam@1877
|
3383 s += sizeof sig_intro - 1;
|
adam@1877
|
3384
|
adam@1877
|
3385 for (i = 0; i < uw_hash_blocksize*2; ++i)
|
adam@1877
|
3386 if (!isxdigit((int)s[i]))
|
adam@1877
|
3387 return NULL;
|
adam@1877
|
3388
|
adam@1877
|
3389 if (s[i] != '"')
|
adam@1877
|
3390 return NULL;
|
adam@1877
|
3391
|
adam@1877
|
3392 return s;
|
adam@1877
|
3393 }
|
adam@1877
|
3394
|
adam@2033
|
3395 static pthread_mutex_t message_send_mutex = PTHREAD_MUTEX_INITIALIZER;
|
adam@2033
|
3396
|
adam@1938
|
3397 int uw_commit(uw_context ctx) {
|
adamc@1104
|
3398 int i;
|
adam@1877
|
3399 char *sig;
|
adamc@1104
|
3400
|
adamc@1131
|
3401 if (uw_has_error(ctx)) {
|
adam@1327
|
3402 uw_rollback(ctx, 0);
|
adam@1938
|
3403 return 0;
|
adamc@1131
|
3404 }
|
adamc@1131
|
3405
|
adamc@1104
|
3406 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adamc@771
|
3407 if (ctx->transactionals[i].rollback != NULL)
|
adamc@1131
|
3408 if (ctx->transactionals[i].commit) {
|
adamc@1083
|
3409 ctx->transactionals[i].commit(ctx->transactionals[i].data);
|
adamc@1131
|
3410 if (uw_has_error(ctx)) {
|
adam@1327
|
3411 uw_rollback(ctx, 0);
|
adam@1938
|
3412 return 0;
|
adamc@1131
|
3413 }
|
adamc@1131
|
3414 }
|
adamc@771
|
3415
|
adam@2033
|
3416 // Here's an important lock to provide the abstraction that all messages from one transaction are sent as an atomic unit.
|
adam@2033
|
3417 if (ctx->used_deltas > 0)
|
adam@2033
|
3418 pthread_mutex_lock(&message_send_mutex);
|
adam@2033
|
3419
|
adam@2000
|
3420 if (ctx->transaction_started) {
|
adam@2000
|
3421 int code = ctx->app->db_commit(ctx);
|
adam@2000
|
3422
|
adam@2000
|
3423 if (code) {
|
adam@2033
|
3424 if (ctx->used_deltas > 0)
|
adam@2033
|
3425 pthread_mutex_unlock(&message_send_mutex);
|
adam@2033
|
3426
|
adam@2000
|
3427 if (ctx->client)
|
adam@2000
|
3428 release_client(ctx->client);
|
adam@2000
|
3429
|
adam@2000
|
3430 if (code == -1) {
|
adam@2000
|
3431 // This case is for a serialization failure, which is not really an "error."
|
adam@2000
|
3432 // The transaction will restart, so we should rollback any transactionals
|
adam@2000
|
3433 // that triggered above.
|
adam@2000
|
3434
|
adam@2000
|
3435 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adam@2000
|
3436 if (ctx->transactionals[i].rollback != NULL)
|
adam@2000
|
3437 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
|
adam@2000
|
3438
|
adam@2000
|
3439 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adam@2000
|
3440 if (ctx->transactionals[i].free)
|
adam@2000
|
3441 ctx->transactionals[i].free(ctx->transactionals[i].data, 1);
|
adam@2000
|
3442
|
grrwlf@2030
|
3443 return 1;
|
adam@2000
|
3444 }
|
adam@2000
|
3445
|
adam@2000
|
3446 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adam@2000
|
3447 if (ctx->transactionals[i].free)
|
adam@2000
|
3448 ctx->transactionals[i].free(ctx->transactionals[i].data, 0);
|
adam@2000
|
3449
|
adam@2000
|
3450 uw_set_error_message(ctx, "Error running SQL COMMIT");
|
adam@2000
|
3451 return 0;
|
adam@2000
|
3452 }
|
adam@2000
|
3453 }
|
adam@2000
|
3454
|
adamc@1104
|
3455 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adamc@771
|
3456 if (ctx->transactionals[i].rollback == NULL)
|
adamc@1131
|
3457 if (ctx->transactionals[i].commit) {
|
adamc@1083
|
3458 ctx->transactionals[i].commit(ctx->transactionals[i].data);
|
adamc@1131
|
3459 if (uw_has_error(ctx)) {
|
adam@2033
|
3460 if (ctx->used_deltas > 0)
|
adam@2033
|
3461 pthread_mutex_unlock(&message_send_mutex);
|
adam@2033
|
3462
|
adam@2033
|
3463 if (ctx->client)
|
adam@2033
|
3464 release_client(ctx->client);
|
adam@2033
|
3465
|
adam@2033
|
3466 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adam@2033
|
3467 if (ctx->transactionals[i].rollback != NULL)
|
adam@2033
|
3468 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
|
adam@2033
|
3469
|
adam@2033
|
3470 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adam@2033
|
3471 if (ctx->transactionals[i].free)
|
adam@2033
|
3472 ctx->transactionals[i].free(ctx->transactionals[i].data, 0);
|
adam@2000
|
3473
|
adam@1938
|
3474 return 0;
|
adamc@1131
|
3475 }
|
adamc@1131
|
3476 }
|
adamc@1131
|
3477
|
adamc@682
|
3478 for (i = 0; i < ctx->used_deltas; ++i) {
|
adamc@682
|
3479 delta *d = &ctx->deltas[i];
|
adamc@682
|
3480 client *c = find_client(d->client);
|
adamc@682
|
3481
|
adam@1551
|
3482 assert (c != NULL);
|
adam@1551
|
3483 assert(c->mode == USED);
|
adamc@682
|
3484
|
adam@1447
|
3485 client_send(c, &d->msgs, ctx->script.start, uw_buffer_used(&ctx->script));
|
adamc@682
|
3486 }
|
adamc@682
|
3487
|
adam@2033
|
3488 if (ctx->used_deltas > 0)
|
adam@2033
|
3489 pthread_mutex_unlock(&message_send_mutex);
|
adam@2033
|
3490
|
adamc@682
|
3491 if (ctx->client)
|
adamc@685
|
3492 release_client(ctx->client);
|
adamc@771
|
3493
|
adamc@1104
|
3494 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adamc@1083
|
3495 if (ctx->transactionals[i].free)
|
adam@1327
|
3496 ctx->transactionals[i].free(ctx->transactionals[i].data, 0);
|
adamc@916
|
3497
|
vshabanoff@1726
|
3498 uw_check(ctx, 1);
|
vshabanoff@1726
|
3499 *ctx->page.front = 0;
|
adam@1413
|
3500
|
adam@1956
|
3501 if (!ctx->returning_indirectly
|
adam@1956
|
3502 && (ctx->app->is_html5
|
adam@1956
|
3503 ? !strncmp(ctx->page.start, uw_begin_html5, sizeof uw_begin_html5 - 1)
|
adam@1956
|
3504 : !strncmp(ctx->page.start, uw_begin_xhtml, sizeof uw_begin_xhtml - 1))) {
|
adam@1728
|
3505 char *s;
|
adam@1728
|
3506
|
adam@1728
|
3507 // Splice script data into appropriate part of page, also adding <head> if needed.
|
adam@1956
|
3508 s = ctx->page.start + (ctx->app->is_html5 ? sizeof uw_begin_html5 - 1 : sizeof uw_begin_xhtml - 1);
|
adam@1728
|
3509 s = strchr(s, '<');
|
adam@1728
|
3510 if (s == NULL) {
|
adam@1728
|
3511 // Weird. Document has no tags!
|
adam@1728
|
3512
|
adam@1728
|
3513 uw_write(ctx, "<head></head><body></body>");
|
adam@1728
|
3514 uw_check(ctx, 1);
|
adam@1728
|
3515 *ctx->page.front = 0;
|
adam@1728
|
3516 } else if (!strncmp(s, "<head>", 6)) {
|
adam@1728
|
3517 // <head> is present. Let's add the <script> tags immediately after it.
|
adam@1728
|
3518
|
adam@1729
|
3519 // Any freeform JavaScript to include?
|
adam@1729
|
3520 if (uw_buffer_used(&ctx->script) > 0) {
|
adam@1729
|
3521 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
|
adam@1729
|
3522 size_t lenP = lenH + 40 + len;
|
adam@1729
|
3523 char *start = s + 6, *oldPage = ctx->page.start;
|
adam@1729
|
3524
|
adam@1729
|
3525 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
|
adam@1729
|
3526 start += ctx->page.start - oldPage;
|
adam@1729
|
3527 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
|
adam@1729
|
3528 ctx->page.front += lenP;
|
adam@1729
|
3529 memcpy(start, ctx->script_header, lenH);
|
adam@1729
|
3530 memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
|
adam@1729
|
3531 memcpy(start + lenH + 31, ctx->script.start, len);
|
adam@1729
|
3532 memcpy(start + lenH + 31 + len, "</script>", 9);
|
adam@1729
|
3533 } else {
|
adam@1729
|
3534 size_t lenH = strlen(ctx->script_header);
|
adam@1729
|
3535 char *start = s + 6, *oldPage = ctx->page.start;
|
adam@1729
|
3536
|
adam@1729
|
3537 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenH);
|
adam@1729
|
3538 start += ctx->page.start - oldPage;
|
adam@1729
|
3539 memmove(start + lenH, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
|
adam@1729
|
3540 ctx->page.front += lenH;
|
adam@1729
|
3541 memcpy(start, ctx->script_header, lenH);
|
adam@1729
|
3542 }
|
adam@1728
|
3543 } else {
|
adam@1728
|
3544 // No <head>. At this point, add it, with <script> tags inside.
|
adam@1728
|
3545
|
adam@1729
|
3546 if (uw_buffer_used(&ctx->script) > 0) {
|
adam@1729
|
3547 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
|
adam@1729
|
3548 size_t lenP = lenH + 53 + len;
|
adam@1729
|
3549 char *start = s, *oldPage = ctx->page.start;
|
adam@1729
|
3550
|
adam@1729
|
3551 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
|
adam@1729
|
3552 start += ctx->page.start - oldPage;
|
adam@1729
|
3553 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
|
adam@1729
|
3554 ctx->page.front += lenP;
|
adam@1729
|
3555 memcpy(start, "<head>", 6);
|
adam@1729
|
3556 memcpy(start + 6, ctx->script_header, lenH);
|
adam@1729
|
3557 memcpy(start + 6 + lenH, "<script type=\"text/javascript\">", 31);
|
adam@1729
|
3558 memcpy(start + 6 + lenH + 31, ctx->script.start, len);
|
adam@1729
|
3559 memcpy(start + 6 + lenH + 31 + len, "</script></head>", 16);
|
adam@1729
|
3560 } else {
|
adam@1729
|
3561 size_t lenH = strlen(ctx->script_header);
|
adam@1729
|
3562 size_t lenP = lenH + 13;
|
adam@1729
|
3563 char *start = s, *oldPage = ctx->page.start;
|
adam@1729
|
3564
|
adam@1729
|
3565 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
|
adam@1729
|
3566 start += ctx->page.start - oldPage;
|
adam@1729
|
3567 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
|
adam@1729
|
3568 ctx->page.front += lenP;
|
adam@1729
|
3569 memcpy(start, "<head>", 6);
|
adam@1729
|
3570 memcpy(start + 6, ctx->script_header, lenH);
|
adam@1729
|
3571 memcpy(start + 6 + lenH, "</head>", 7);
|
adam@1729
|
3572 }
|
adamc@916
|
3573 }
|
adamc@916
|
3574 }
|
adam@1877
|
3575
|
adam@1877
|
3576 if (ctx->needsResig) {
|
adam@1877
|
3577 sig = find_sig(ctx->page.start);
|
adam@1877
|
3578 if (sig) {
|
adam@1877
|
3579 char *realsig = ctx->app->cookie_sig(ctx);
|
adam@1877
|
3580
|
adam@1877
|
3581 do {
|
adam@1877
|
3582 memcpy(sig, realsig, 2*uw_hash_blocksize);
|
adam@1877
|
3583 sig = find_sig(sig);
|
adam@1877
|
3584 } while (sig);
|
adam@1877
|
3585 }
|
adam@1877
|
3586 }
|
adam@1938
|
3587
|
adam@1938
|
3588 return 0;
|
adamc@671
|
3589 }
|
adamc@671
|
3590
|
adamc@683
|
3591
|
adamc@1113
|
3592 size_t uw_transactionals_max = SIZE_MAX;
|
adamc@1113
|
3593
|
adam@2001
|
3594 int uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback,
|
adam@1327
|
3595 uw_callback_with_retry free) {
|
adamc@771
|
3596 if (ctx->used_transactionals >= ctx->n_transactionals) {
|
adamc@1113
|
3597 if (ctx->used_transactionals+1 > uw_transactionals_max)
|
adam@2001
|
3598 // Exceeded limit on number of transactionals.
|
adam@2001
|
3599 return -1;
|
adamc@1084
|
3600 ctx->transactionals = realloc(ctx->transactionals, sizeof(transactional) * (ctx->used_transactionals+1));
|
adamc@771
|
3601 ++ctx->n_transactionals;
|
adamc@771
|
3602 }
|
adamc@771
|
3603
|
adamc@771
|
3604 ctx->transactionals[ctx->used_transactionals].data = data;
|
adamc@771
|
3605 ctx->transactionals[ctx->used_transactionals].commit = commit;
|
adamc@771
|
3606 ctx->transactionals[ctx->used_transactionals].rollback = rollback;
|
adamc@771
|
3607 ctx->transactionals[ctx->used_transactionals++].free = free;
|
adam@2001
|
3608
|
adam@2001
|
3609 return 0;
|
adamc@771
|
3610 }
|
adamc@771
|
3611
|
adamc@683
|
3612
|
adamc@683
|
3613 // "Garbage collection"
|
adamc@683
|
3614
|
adamc@1121
|
3615 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data);
|
adamc@1121
|
3616 void uw_post_expunge(uw_context ctx, void *data);
|
adamc@1121
|
3617
|
adamc@1121
|
3618 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
|
adamc@683
|
3619 int r = setjmp(ctx->jmp_buf);
|
adamc@683
|
3620
|
adamc@1121
|
3621 if (r == 0)
|
adamc@1121
|
3622 uw_do_expunge(ctx, cli, data);
|
adamc@1121
|
3623 else
|
adamc@1121
|
3624 ctx->app->db_rollback(ctx);
|
adamc@1121
|
3625
|
adamc@1121
|
3626 uw_post_expunge(ctx, data);
|
adamc@683
|
3627
|
adamc@683
|
3628 return r;
|
adamc@683
|
3629 }
|
adamc@683
|
3630
|
adamc@683
|
3631 void uw_prune_clients(uw_context ctx) {
|
adamc@683
|
3632 client *c, *next, *prev = NULL;
|
adamc@683
|
3633 time_t cutoff;
|
adamc@683
|
3634
|
adamc@1094
|
3635 cutoff = time(NULL) - ctx->app->timeout;
|
adamc@683
|
3636
|
adamc@683
|
3637 pthread_mutex_lock(&clients_mutex);
|
adam@1707
|
3638 pruning_thread = pthread_self();
|
adam@1707
|
3639 pruning_thread_initialized = 1;
|
adamc@683
|
3640
|
adamc@683
|
3641 for (c = clients_used; c; c = next) {
|
adamc@683
|
3642 next = c->next;
|
adamc@683
|
3643 pthread_mutex_lock(&c->lock);
|
adamc@685
|
3644 if (c->last_contact < cutoff && c->refcount == 0) {
|
adamc@683
|
3645 failure_kind fk = UNLIMITED_RETRY;
|
adamc@683
|
3646 if (prev)
|
adamc@683
|
3647 prev->next = next;
|
adamc@683
|
3648 else
|
adamc@683
|
3649 clients_used = next;
|
adamc@683
|
3650 while (fk == UNLIMITED_RETRY) {
|
adam@1941
|
3651 uw_reset(ctx);
|
adamc@1121
|
3652 fk = uw_expunge(ctx, c->id, c->data);
|
adamc@1121
|
3653 if (fk == UNLIMITED_RETRY)
|
adamc@684
|
3654 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
|
adamc@683
|
3655 }
|
adamc@684
|
3656 if (fk == SUCCESS)
|
adamc@684
|
3657 free_client(c);
|
adamc@1121
|
3658 else
|
adamc@856
|
3659 fprintf(stderr, "Expunge blocked by error: %s\n", uw_error_message(ctx));
|
adamc@683
|
3660 }
|
adamc@683
|
3661 else
|
adamc@683
|
3662 prev = c;
|
adamc@683
|
3663 pthread_mutex_unlock(&c->lock);
|
adamc@683
|
3664 }
|
adamc@683
|
3665
|
adamc@683
|
3666 pthread_mutex_unlock(&clients_mutex);
|
adamc@683
|
3667 }
|
adamc@687
|
3668
|
adamc@687
|
3669 failure_kind uw_initialize(uw_context ctx) {
|
adamc@687
|
3670 int r = setjmp(ctx->jmp_buf);
|
adamc@687
|
3671
|
adamc@687
|
3672 if (r == 0) {
|
adam@1936
|
3673 uw_ensure_transaction(ctx);
|
adamc@1094
|
3674 ctx->app->initializer(ctx);
|
adamc@1094
|
3675 if (ctx->app->db_commit(ctx))
|
adamc@687
|
3676 uw_error(ctx, FATAL, "Error running SQL COMMIT");
|
adamc@687
|
3677 }
|
adamc@687
|
3678
|
adamc@687
|
3679 return r;
|
adamc@687
|
3680 }
|
adamc@717
|
3681
|
adamc@1065
|
3682 static int url_bad(uw_Basis_string s) {
|
adamc@1065
|
3683 for (; *s; ++s)
|
adamc@1153
|
3684 if (!isgraph((int)*s))
|
adamc@1065
|
3685 return 1;
|
adamc@1065
|
3686
|
adamc@1065
|
3687 return 0;
|
adamc@1065
|
3688 }
|
adamc@1065
|
3689
|
adamc@717
|
3690 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
|
adamc@1065
|
3691 if (url_bad(s))
|
adamc@1065
|
3692 uw_error(ctx, FATAL, "Invalid URL %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@1094
|
3693 if (ctx->app->check_url(s))
|
adamc@770
|
3694 return s;
|
adamc@770
|
3695 else
|
adamc@770
|
3696 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@717
|
3697 }
|
adamc@734
|
3698
|
adamc@770
|
3699 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
|
adamc@1065
|
3700 if (url_bad(s))
|
adamc@1065
|
3701 return NULL;
|
adamc@1094
|
3702 if (ctx->app->check_url(s))
|
adamc@770
|
3703 return s;
|
adamc@770
|
3704 else
|
adamc@770
|
3705 return NULL;
|
adamc@770
|
3706 }
|
adamc@770
|
3707
|
adam@1465
|
3708 static int mime_format(const char *s) {
|
adamc@770
|
3709 for (; *s; ++s)
|
grrwlf@2024
|
3710 if (!isalnum((int)*s) && *s != '/' && *s != '-' && *s != '.' && *s != '+')
|
adamc@770
|
3711 return 0;
|
adamc@770
|
3712
|
adamc@770
|
3713 return 1;
|
adamc@770
|
3714 }
|
adamc@770
|
3715
|
adamc@741
|
3716 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
|
adamc@770
|
3717 if (!mime_format(s))
|
adamc@770
|
3718 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
|
adamc@770
|
3719
|
adamc@1094
|
3720 if (ctx->app->check_mime(s))
|
adamc@770
|
3721 return s;
|
adamc@770
|
3722 else
|
adamc@770
|
3723 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@770
|
3724 }
|
adamc@770
|
3725
|
adamc@770
|
3726 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
|
adamc@770
|
3727 if (!mime_format(s))
|
adamc@770
|
3728 return NULL;
|
adamc@770
|
3729
|
adamc@1094
|
3730 if (ctx->app->check_mime(s))
|
adamc@770
|
3731 return s;
|
adamc@770
|
3732 else
|
adamc@770
|
3733 return NULL;
|
adamc@741
|
3734 }
|
adamc@741
|
3735
|
adam@1465
|
3736 uw_Basis_string uw_Basis_blessRequestHeader(uw_context ctx, uw_Basis_string s) {
|
adam@1465
|
3737 if (!mime_format(s))
|
adam@1465
|
3738 uw_error(ctx, FATAL, "Request header \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
|
adam@1465
|
3739
|
adam@1465
|
3740 if (ctx->app->check_requestHeader(s))
|
adam@1465
|
3741 return s;
|
adam@1465
|
3742 else
|
adam@1465
|
3743 uw_error(ctx, FATAL, "Disallowed request header %s", uw_Basis_htmlifyString(ctx, s));
|
adam@1465
|
3744 }
|
adam@1465
|
3745
|
adam@1465
|
3746 uw_Basis_string uw_Basis_checkRequestHeader(uw_context ctx, uw_Basis_string s) {
|
adam@1465
|
3747 if (!mime_format(s))
|
adam@1465
|
3748 return NULL;
|
adam@1465
|
3749
|
adam@1465
|
3750 if (ctx->app->check_requestHeader(s))
|
adam@1465
|
3751 return s;
|
adam@1465
|
3752 else
|
adam@1465
|
3753 return NULL;
|
adam@1465
|
3754 }
|
adam@1465
|
3755
|
adam@1465
|
3756 uw_Basis_string uw_Basis_blessResponseHeader(uw_context ctx, uw_Basis_string s) {
|
adam@1465
|
3757 if (!mime_format(s))
|
adam@1465
|
3758 uw_error(ctx, FATAL, "Response header \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
|
adam@1465
|
3759
|
adam@1465
|
3760 if (ctx->app->check_responseHeader(s))
|
adam@1465
|
3761 return s;
|
adam@1465
|
3762 else
|
adam@1465
|
3763 uw_error(ctx, FATAL, "Disallowed response header %s", uw_Basis_htmlifyString(ctx, s));
|
adam@1465
|
3764 }
|
adam@1465
|
3765
|
adam@1799
|
3766 static int envVar_format(const char *s) {
|
adam@1799
|
3767 for (; *s; ++s)
|
adam@1799
|
3768 if (!isalnum((int)*s) && *s != '_' && *s != '.')
|
adam@1799
|
3769 return 0;
|
adam@1799
|
3770
|
adam@1799
|
3771 return 1;
|
adam@1799
|
3772 }
|
adam@1799
|
3773
|
adam@1465
|
3774 uw_Basis_string uw_Basis_checkResponseHeader(uw_context ctx, uw_Basis_string s) {
|
adam@1799
|
3775 if (!envVar_format(s))
|
adam@1465
|
3776 return NULL;
|
adam@1465
|
3777
|
adam@1465
|
3778 if (ctx->app->check_responseHeader(s))
|
adam@1465
|
3779 return s;
|
adam@1465
|
3780 else
|
adam@1465
|
3781 return NULL;
|
adam@1465
|
3782 }
|
adam@1465
|
3783
|
adam@1799
|
3784 uw_Basis_string uw_Basis_blessEnvVar(uw_context ctx, uw_Basis_string s) {
|
adam@1799
|
3785 if (!envVar_format(s))
|
adam@1799
|
3786 uw_error(ctx, FATAL, "Environment variable \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
|
adam@1799
|
3787
|
adam@1799
|
3788 if (ctx->app->check_envVar(s))
|
adam@1799
|
3789 return s;
|
adam@1799
|
3790 else
|
adam@1799
|
3791 uw_error(ctx, FATAL, "Disallowed environment variable %s", uw_Basis_htmlifyString(ctx, s));
|
adam@1799
|
3792 }
|
adam@1799
|
3793
|
adam@1799
|
3794 uw_Basis_string uw_Basis_checkEnvVar(uw_context ctx, uw_Basis_string s) {
|
adam@1799
|
3795 if (!mime_format(s))
|
adam@1799
|
3796 return NULL;
|
adam@1799
|
3797
|
adam@1799
|
3798 if (ctx->app->check_envVar(s))
|
adam@1799
|
3799 return s;
|
adam@1799
|
3800 else
|
adam@1799
|
3801 return NULL;
|
adam@1799
|
3802 }
|
adam@1799
|
3803
|
adam@1465
|
3804 uw_Basis_string uw_Basis_getHeader(uw_context ctx, uw_Basis_string name) {
|
adam@1465
|
3805 return uw_Basis_requestHeader(ctx, name);
|
adam@1465
|
3806 }
|
adam@1465
|
3807
|
adam@1465
|
3808 static int mime_value_format(const char *s) {
|
adam@1465
|
3809 for (; *s; ++s)
|
adam@1465
|
3810 if (*s == '\r' || *s == '\n')
|
adam@1465
|
3811 return 0;
|
adam@1465
|
3812
|
adam@1465
|
3813 return 1;
|
adam@1465
|
3814 }
|
adam@1465
|
3815
|
adam@1465
|
3816 uw_unit uw_Basis_setHeader(uw_context ctx, uw_Basis_string name, uw_Basis_string value) {
|
adam@1465
|
3817 if (!mime_value_format(value))
|
adam@1465
|
3818 uw_error(ctx, FATAL, "Invalid value for HTTP response header");
|
adam@1465
|
3819
|
adam@1465
|
3820 uw_write_header(ctx, name);
|
adam@1465
|
3821 uw_write_header(ctx, ": ");
|
adam@1465
|
3822 uw_write_header(ctx, value);
|
adam@1465
|
3823 uw_write_header(ctx, "\r\n");
|
adam@1465
|
3824
|
adam@1465
|
3825 return uw_unit_v;
|
adam@1465
|
3826 }
|
adam@1465
|
3827
|
adam@1799
|
3828 uw_Basis_string uw_Basis_getenv(uw_context ctx, uw_Basis_string name) {
|
adam@1993
|
3829 if (ctx->get_env)
|
adam@1993
|
3830 return ctx->get_env(ctx->get_env_data, name);
|
adam@1993
|
3831 else
|
adam@1994
|
3832 return getenv(name);
|
adam@1799
|
3833 }
|
adam@1799
|
3834
|
adamc@734
|
3835 uw_Basis_string uw_unnull(uw_Basis_string s) {
|
adamc@734
|
3836 return s ? s : "";
|
adamc@734
|
3837 }
|
adamc@734
|
3838
|
adamc@734
|
3839 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
|
adamc@734
|
3840 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
|
adamc@734
|
3841 int i;
|
phurst@1981
|
3842
|
adamc@734
|
3843 for (i = 0; i < uw_hash_blocksize; ++i)
|
adamc@734
|
3844 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
|
adamc@734
|
3845
|
adamc@734
|
3846 return r;
|
adamc@734
|
3847 }
|
adamc@734
|
3848
|
adam@1512
|
3849 /* This bit of crafty code is intended to prevent GCC from performing
|
adam@1512
|
3850 * optimizations that would enable timing attacks. See:
|
adam@1512
|
3851 * http://www.impredicative.com/pipermail/ur/2011-July/000659.html
|
adam@1512
|
3852 */
|
adam@1512
|
3853 int uw_streq(uw_Basis_string s1, uw_Basis_string s2) {
|
adam@1512
|
3854 int i, x = 0, len1 = strlen(s1);
|
adam@1512
|
3855
|
adam@1512
|
3856 if (len1 != strlen(s2)) return 0;
|
adam@1512
|
3857
|
adam@1512
|
3858 for (i = 0; i < len1; ++i) {
|
adam@1512
|
3859 __asm__ __volatile__ ("");
|
adam@1512
|
3860 x |= s1[i] ^ s2[i];
|
adam@1512
|
3861 }
|
adam@1512
|
3862
|
adam@1512
|
3863 return x == 0;
|
adam@1512
|
3864 }
|
adam@1512
|
3865
|
adamc@734
|
3866 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
|
adam@1877
|
3867 ctx->usedSig = 1;
|
adamc@1094
|
3868 return ctx->app->cookie_sig(ctx);
|
adamc@734
|
3869 }
|
adamc@737
|
3870
|
adamc@737
|
3871 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
|
adamc@737
|
3872 return f.name;
|
adamc@737
|
3873 }
|
adamc@737
|
3874
|
adamc@740
|
3875 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
|
adamc@740
|
3876 return f.type;
|
adamc@740
|
3877 }
|
adamc@740
|
3878
|
adamc@745
|
3879 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
|
adamc@745
|
3880 return b.size;
|
adamc@745
|
3881 }
|
adamc@745
|
3882
|
adamc@1119
|
3883 uw_Basis_blob uw_Basis_textBlob(uw_context ctx, uw_Basis_string s) {
|
adamc@1119
|
3884 uw_Basis_blob b = {strlen(s), s};
|
adamc@1119
|
3885
|
adamc@1119
|
3886 return b;
|
adamc@1119
|
3887 }
|
adamc@1119
|
3888
|
adamc@737
|
3889 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
|
adamc@737
|
3890 return f.data;
|
adamc@737
|
3891 }
|
adamc@741
|
3892
|
adam@1347
|
3893 uw_Basis_string uw_Basis_postType(uw_context ctx, uw_Basis_postBody pb) {
|
adam@1347
|
3894 return pb.type;
|
adam@1347
|
3895 }
|
adam@1347
|
3896
|
adam@1347
|
3897 uw_Basis_string uw_Basis_postData(uw_context ctx, uw_Basis_postBody pb) {
|
adam@1347
|
3898 return pb.data;
|
adam@1347
|
3899 }
|
adam@1347
|
3900
|
adam@1471
|
3901 static char *old_headers(uw_context ctx) {
|
adam@1471
|
3902 if (uw_buffer_used(&ctx->outHeaders) == 0)
|
adam@1471
|
3903 return NULL;
|
adam@1471
|
3904 else {
|
adam@1471
|
3905 char *s = strchr(ctx->outHeaders.start, '\n');
|
adam@1471
|
3906
|
adam@1471
|
3907 if (s == NULL || strncasecmp(s+1, "Content-type: ", 14))
|
adam@1471
|
3908 return NULL;
|
adam@1471
|
3909 else {
|
adam@1471
|
3910 s = strchr(s+15, '\n');
|
adam@1471
|
3911 if (s == NULL)
|
adam@1471
|
3912 return NULL;
|
adam@1471
|
3913 else
|
adam@1471
|
3914 return uw_strdup(ctx, s+1);
|
adam@1471
|
3915 }
|
adam@1471
|
3916 }
|
adam@1471
|
3917 }
|
adam@1471
|
3918
|
adamc@741
|
3919 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
|
adamc@741
|
3920 cleanup *cl;
|
adamc@741
|
3921 int len;
|
adam@1471
|
3922 char *oldh;
|
adamc@741
|
3923
|
adam@1451
|
3924 if (!ctx->allowed_to_return_indirectly)
|
adam@1451
|
3925 uw_error(ctx, FATAL, "Tried to return a blob from an RPC");
|
adam@1451
|
3926
|
adamc@1065
|
3927 ctx->returning_indirectly = 1;
|
adam@1471
|
3928 oldh = old_headers(ctx);
|
adam@1369
|
3929 uw_buffer_reset(&ctx->outHeaders);
|
adam@1369
|
3930 uw_buffer_reset(&ctx->page);
|
adamc@741
|
3931
|
adamc@856
|
3932 uw_write_header(ctx, on_success);
|
adamc@856
|
3933 uw_write_header(ctx, "Content-Type: ");
|
adamc@741
|
3934 uw_write_header(ctx, mimeType);
|
adam@1934
|
3935 uw_write_header(ctx, "\r\nContent-length: ");
|
adam@1369
|
3936 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
|
adamc@1134
|
3937 sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)b.size, &len);
|
adamc@741
|
3938 ctx->outHeaders.front += len;
|
adamc@1066
|
3939 uw_write_header(ctx, "\r\n");
|
adam@1471
|
3940 if (oldh) uw_write_header(ctx, oldh);
|
adamc@741
|
3941
|
adam@1369
|
3942 ctx_uw_buffer_append(ctx, "page", &ctx->page, b.data, b.size);
|
adamc@741
|
3943
|
adamc@741
|
3944 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@741
|
3945 cl->func(cl->arg);
|
adamc@741
|
3946
|
adamc@741
|
3947 ctx->cleanup_front = ctx->cleanup;
|
adamc@741
|
3948
|
adamc@1065
|
3949 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
|
adamc@1065
|
3950 }
|
adamc@1065
|
3951
|
adam@2046
|
3952 void uw_replace_page(uw_context ctx, const char *data, size_t size) {
|
adam@2046
|
3953 uw_buffer_reset(&ctx->page);
|
adam@2046
|
3954 ctx_uw_buffer_append(ctx, "page", &ctx->page, data, size);
|
adam@2046
|
3955 }
|
adam@2046
|
3956
|
adam@1932
|
3957 __attribute__((noreturn)) void uw_return_blob_from_page(uw_context ctx, uw_Basis_string mimeType) {
|
adam@1932
|
3958 cleanup *cl;
|
adam@1932
|
3959 int len;
|
adam@1932
|
3960 char *oldh;
|
adam@1932
|
3961
|
adam@1932
|
3962 if (!ctx->allowed_to_return_indirectly)
|
adam@1932
|
3963 uw_error(ctx, FATAL, "Tried to return a blob from an RPC");
|
adam@1932
|
3964
|
adam@1932
|
3965 ctx->returning_indirectly = 1;
|
adam@1932
|
3966 oldh = old_headers(ctx);
|
adam@1932
|
3967 uw_buffer_reset(&ctx->outHeaders);
|
adam@1932
|
3968
|
adam@1932
|
3969 uw_write_header(ctx, on_success);
|
adam@1932
|
3970 uw_write_header(ctx, "Content-Type: ");
|
adam@1932
|
3971 uw_write_header(ctx, mimeType);
|
adam@1934
|
3972 uw_write_header(ctx, "\r\nContent-length: ");
|
adam@1932
|
3973 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
|
adam@1932
|
3974 sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)uw_buffer_used(&ctx->page), &len);
|
adam@1932
|
3975 ctx->outHeaders.front += len;
|
adam@1932
|
3976 uw_write_header(ctx, "\r\n");
|
adam@1932
|
3977 if (oldh) uw_write_header(ctx, oldh);
|
adam@1932
|
3978
|
adam@1932
|
3979 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adam@1932
|
3980 cl->func(cl->arg);
|
adam@1932
|
3981
|
adam@1932
|
3982 ctx->cleanup_front = ctx->cleanup;
|
adam@1932
|
3983
|
adam@1932
|
3984 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
|
adam@1932
|
3985 }
|
adam@1932
|
3986
|
adamc@1065
|
3987 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) {
|
adamc@1065
|
3988 cleanup *cl;
|
adamc@1066
|
3989 char *s;
|
adam@1471
|
3990 char *oldh;
|
adamc@1065
|
3991
|
adam@1451
|
3992 if (!ctx->allowed_to_return_indirectly)
|
adam@1451
|
3993 uw_error(ctx, FATAL, "Tried to redirect from an RPC");
|
adam@1451
|
3994
|
adamc@1065
|
3995 ctx->returning_indirectly = 1;
|
adam@1471
|
3996 oldh = old_headers(ctx);
|
adam@1369
|
3997 uw_buffer_reset(&ctx->page);
|
adam@1369
|
3998 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->outHeaders)+1);
|
adam@1369
|
3999 memcpy(ctx->page.start, ctx->outHeaders.start, uw_buffer_used(&ctx->outHeaders));
|
adam@1369
|
4000 ctx->page.start[uw_buffer_used(&ctx->outHeaders)] = 0;
|
adam@1369
|
4001 uw_buffer_reset(&ctx->outHeaders);
|
adamc@1065
|
4002
|
adam@1409
|
4003 if (on_success[0])
|
adam@1409
|
4004 uw_write_header(ctx, on_redirect);
|
adam@1409
|
4005 else
|
adam@1409
|
4006 uw_write_header(ctx, "Status: 303 See Other\r\n");
|
adam@1409
|
4007 s = on_success[0] ? strchr(ctx->page.start, '\n') : ctx->page.start;
|
adamc@1066
|
4008 if (s) {
|
adamc@1066
|
4009 char *s2;
|
adam@1409
|
4010 if (s[0] == '\n') ++s;
|
adam@1409
|
4011 for (; (s2 = strchr(s, '\n')); s = s2+1) {
|
adamc@1066
|
4012 *s2 = 0;
|
adamc@1066
|
4013 if (!strncmp(s, "Set-Cookie: ", 12)) {
|
adamc@1066
|
4014 uw_write_header(ctx, s);
|
adamc@1066
|
4015 uw_write_header(ctx, "\n");
|
adamc@1066
|
4016 }
|
adamc@1066
|
4017 }
|
adamc@1066
|
4018 }
|
adamc@1066
|
4019
|
adamc@1065
|
4020 uw_write_header(ctx, "Location: ");
|
adamc@1065
|
4021 uw_write_header(ctx, url);
|
adamc@1065
|
4022 uw_write_header(ctx, "\r\n\r\n");
|
adam@1471
|
4023 if (oldh) uw_write_header(ctx, oldh);
|
adamc@1065
|
4024
|
adamc@1065
|
4025 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@1065
|
4026 cl->func(cl->arg);
|
adamc@1065
|
4027
|
adamc@1065
|
4028 ctx->cleanup_front = ctx->cleanup;
|
adamc@1065
|
4029
|
adamc@1065
|
4030 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
|
adamc@741
|
4031 }
|
adamc@874
|
4032
|
adamc@874
|
4033 uw_Basis_string uw_Basis_unAs(uw_context ctx, uw_Basis_string s) {
|
adam@1467
|
4034 uw_Basis_string ret = uw_malloc(ctx, strlen(s) + 1), r = ret;
|
adamc@874
|
4035
|
adamc@874
|
4036 for (; *s; ++s) {
|
adamc@874
|
4037 if (s[0] == '\'') {
|
adamc@874
|
4038 *r++ = '\'';
|
adamc@874
|
4039 for (++s; *s; ++s) {
|
adamc@874
|
4040 if (s[0] == '\'') {
|
adamc@874
|
4041 *r++ = '\'';
|
adamc@874
|
4042 break;
|
adamc@874
|
4043 } else if (s[0] == '\\') {
|
adam@1467
|
4044 *r++ = '\\';
|
adam@1467
|
4045 *r++ = s[1];
|
adam@1467
|
4046 ++s;
|
adamc@874
|
4047 } else
|
adamc@874
|
4048 *r++ = s[0];
|
adamc@874
|
4049 }
|
adamc@874
|
4050 if (*s == 0) break;
|
adam@1467
|
4051 } else if (s[0] == 'T' && s[1] == '_' && s[2] == 'T' && s[3] == '.')
|
adam@1467
|
4052 s += 3;
|
adamc@874
|
4053 else
|
adamc@874
|
4054 *r++ = s[0];
|
adamc@874
|
4055 }
|
adamc@874
|
4056
|
adam@1467
|
4057 *r = 0;
|
adam@1467
|
4058 return ret;
|
adamc@874
|
4059 }
|
adamc@874
|
4060
|
adamc@922
|
4061 uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) {
|
adamc@922
|
4062 va_list ap;
|
adamc@922
|
4063 size_t len = 1;
|
adamc@922
|
4064 char *s, *r, *s2;
|
phurst@1981
|
4065
|
adamc@922
|
4066 va_start(ap, ctx);
|
adamc@922
|
4067 for (s = va_arg(ap, char*); s; s = va_arg(ap, char*))
|
adamc@922
|
4068 len += strlen(s);
|
adamc@922
|
4069 va_end(ap);
|
adamc@922
|
4070
|
adamc@922
|
4071 r = uw_malloc(ctx, len);
|
adamc@922
|
4072 va_start(ap, ctx);
|
adamc@922
|
4073 for (s2 = r, s = va_arg(ap, char*); s; s = va_arg(ap, char*))
|
adamc@922
|
4074 while (*s)
|
adamc@922
|
4075 *s2++ = *s++;
|
adamc@922
|
4076 va_end(ap);
|
adamc@922
|
4077 *s2 = 0;
|
adamc@922
|
4078
|
adamc@922
|
4079 return r;
|
adamc@922
|
4080 }
|
adamc@1006
|
4081
|
adam@1365
|
4082 const uw_Basis_time uw_Basis_minTime = {};
|
adamc@1050
|
4083
|
adamc@1006
|
4084 uw_Basis_time uw_Basis_now(uw_context ctx) {
|
adam@1365
|
4085 uw_Basis_time r = { time(NULL) };
|
adam@1365
|
4086 return r;
|
adamc@1006
|
4087 }
|
adamc@1061
|
4088
|
adam@1369
|
4089 uw_Basis_time uw_Basis_addSeconds(uw_context ctx, uw_Basis_time tm, uw_Basis_int n) {
|
adam@1369
|
4090 tm.seconds += n;
|
adam@1365
|
4091 return tm;
|
adamc@1138
|
4092 }
|
adamc@1138
|
4093
|
adam@1514
|
4094 uw_Basis_int uw_Basis_diffInSeconds(uw_context ctx, uw_Basis_time tm1, uw_Basis_time tm2) {
|
adam@1514
|
4095 return difftime(tm2.seconds, tm1.seconds);
|
adam@1514
|
4096 }
|
adam@1514
|
4097
|
adam@1685
|
4098 uw_Basis_int uw_Basis_toMilliseconds(uw_context ctx, uw_Basis_time tm) {
|
adam@1685
|
4099 return tm.seconds * 1000 + tm.microseconds / 1000;
|
adam@1685
|
4100 }
|
adam@1685
|
4101
|
adam@2187
|
4102 uw_Basis_time uw_Basis_fromMilliseconds(uw_context ctx, uw_Basis_int n) {
|
adam@2187
|
4103 uw_Basis_time tm = {n / 1000, n % 1000 * 1000};
|
adam@2187
|
4104 return tm;
|
adam@2187
|
4105 }
|
adam@2187
|
4106
|
adam@1685
|
4107 uw_Basis_int uw_Basis_diffInMilliseconds(uw_context ctx, uw_Basis_time tm1, uw_Basis_time tm2) {
|
adam@1685
|
4108 return uw_Basis_toMilliseconds(ctx, tm2) - uw_Basis_toMilliseconds(ctx, tm1);
|
adam@1685
|
4109 }
|
adam@1685
|
4110
|
adam@1514
|
4111 uw_Basis_int uw_Basis_toSeconds(uw_context ctx, uw_Basis_time tm) {
|
adam@1514
|
4112 return tm.seconds;
|
adam@1514
|
4113 }
|
adam@1514
|
4114
|
phurst@1971
|
4115 uw_Basis_time uw_Basis_fromDatetime(uw_context ctx, uw_Basis_int year, uw_Basis_int month, uw_Basis_int day, uw_Basis_int hour, uw_Basis_int minute, uw_Basis_int second) {
|
phurst@1971
|
4116 struct tm tm = { .tm_year = year - 1900, .tm_mon = month, .tm_mday = day,
|
adam@2015
|
4117 .tm_hour = hour, .tm_min = minute, .tm_sec = second,
|
adam@2015
|
4118 .tm_isdst = -1 };
|
phurst@1984
|
4119 uw_Basis_time r = { timelocal(&tm) };
|
phurst@1971
|
4120 return r;
|
phurst@1971
|
4121 }
|
phurst@1971
|
4122
|
phurst@1971
|
4123 uw_Basis_int uw_Basis_datetimeYear(uw_context ctx, uw_Basis_time time) {
|
phurst@1971
|
4124 struct tm tm;
|
phurst@1982
|
4125 localtime_r(&time.seconds, &tm);
|
phurst@1971
|
4126 return tm.tm_year + 1900;
|
phurst@1971
|
4127 }
|
phurst@1971
|
4128
|
phurst@1971
|
4129 uw_Basis_int uw_Basis_datetimeMonth(uw_context ctx, uw_Basis_time time) {
|
phurst@1971
|
4130 struct tm tm;
|
phurst@1982
|
4131 localtime_r(&time.seconds, &tm);
|
phurst@1971
|
4132 return tm.tm_mon;
|
phurst@1971
|
4133 }
|
phurst@1971
|
4134
|
phurst@1971
|
4135 uw_Basis_int uw_Basis_datetimeDay(uw_context ctx, uw_Basis_time time) {
|
phurst@1971
|
4136 struct tm tm;
|
phurst@1982
|
4137 localtime_r(&time.seconds, &tm);
|
phurst@1971
|
4138 return tm.tm_mday;
|
phurst@1971
|
4139 }
|
phurst@1971
|
4140
|
phurst@1971
|
4141 uw_Basis_int uw_Basis_datetimeHour(uw_context ctx, uw_Basis_time time) {
|
phurst@1971
|
4142 struct tm tm;
|
phurst@1982
|
4143 localtime_r(&time.seconds, &tm);
|
phurst@1971
|
4144 return tm.tm_hour;
|
phurst@1971
|
4145 }
|
phurst@1971
|
4146
|
phurst@1971
|
4147 uw_Basis_int uw_Basis_datetimeMinute(uw_context ctx, uw_Basis_time time) {
|
phurst@1971
|
4148 struct tm tm;
|
phurst@1982
|
4149 localtime_r(&time.seconds, &tm);
|
phurst@1971
|
4150 return tm.tm_min;
|
phurst@1971
|
4151 }
|
phurst@1971
|
4152
|
phurst@1971
|
4153 uw_Basis_int uw_Basis_datetimeSecond(uw_context ctx, uw_Basis_time time) {
|
phurst@1971
|
4154 struct tm tm;
|
phurst@1982
|
4155 localtime_r(&time.seconds, &tm);
|
phurst@1971
|
4156 return tm.tm_sec;
|
phurst@1971
|
4157 }
|
phurst@1971
|
4158
|
phurst@1973
|
4159 uw_Basis_int uw_Basis_datetimeDayOfWeek(uw_context ctx, uw_Basis_time time) {
|
phurst@1973
|
4160 struct tm tm;
|
phurst@1982
|
4161 localtime_r(&time.seconds, &tm);
|
phurst@1973
|
4162 return tm.tm_wday;
|
phurst@1973
|
4163 }
|
phurst@1971
|
4164
|
phurst@1971
|
4165
|
adamc@1061
|
4166 void *uw_get_global(uw_context ctx, char *name) {
|
adamc@1061
|
4167 int i;
|
adamc@1061
|
4168
|
adamc@1061
|
4169 for (i = 0; i < ctx->n_globals; ++i)
|
adamc@1061
|
4170 if (!strcmp(name, ctx->globals[i].name))
|
adamc@1061
|
4171 return ctx->globals[i].data;
|
adamc@1061
|
4172
|
adamc@1061
|
4173 return NULL;
|
adamc@1061
|
4174 }
|
adamc@1061
|
4175
|
adamc@1113
|
4176 size_t uw_globals_max = SIZE_MAX;
|
adamc@1113
|
4177
|
adamc@1061
|
4178 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) {
|
adamc@1061
|
4179 int i;
|
adamc@1061
|
4180
|
adamc@1061
|
4181 for (i = 0; i < ctx->n_globals; ++i)
|
adamc@1061
|
4182 if (!strcmp(name, ctx->globals[i].name)) {
|
adamc@1089
|
4183 if (ctx->globals[i].free)
|
adamc@1061
|
4184 ctx->globals[i].free(ctx->globals[i].data);
|
adamc@1061
|
4185 ctx->globals[i].data = data;
|
adamc@1061
|
4186 ctx->globals[i].free = free;
|
adamc@1061
|
4187 return;
|
adamc@1061
|
4188 }
|
adamc@1089
|
4189
|
adamc@1113
|
4190 if (ctx->n_globals+1 > uw_globals_max)
|
adamc@1113
|
4191 uw_error(ctx, FATAL, "Exceeded limit on number of globals");
|
adamc@1113
|
4192
|
adamc@1061
|
4193 ++ctx->n_globals;
|
adamc@1061
|
4194 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global));
|
adamc@1061
|
4195 ctx->globals[ctx->n_globals-1].name = name;
|
adamc@1061
|
4196 ctx->globals[ctx->n_globals-1].data = data;
|
adamc@1061
|
4197 ctx->globals[ctx->n_globals-1].free = free;
|
adamc@1061
|
4198 }
|
adamc@1061
|
4199
|
adamc@1061
|
4200 uw_Basis_bool uw_Basis_isalnum(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4201 return !!isalnum((int)c);
|
adamc@1061
|
4202 }
|
adamc@1061
|
4203
|
adamc@1061
|
4204 uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4205 return !!isalpha((int)c);
|
adamc@1061
|
4206 }
|
adamc@1061
|
4207
|
adamc@1061
|
4208 uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4209 return !!isblank((int)c);
|
adamc@1061
|
4210 }
|
adamc@1061
|
4211
|
adamc@1061
|
4212 uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4213 return !!iscntrl((int)c);
|
adamc@1061
|
4214 }
|
adamc@1061
|
4215
|
adamc@1061
|
4216 uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4217 return !!isdigit((int)c);
|
adamc@1061
|
4218 }
|
adamc@1061
|
4219
|
adamc@1061
|
4220 uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4221 return !!isgraph((int)c);
|
adamc@1061
|
4222 }
|
adamc@1061
|
4223
|
adamc@1061
|
4224 uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4225 return !!islower((int)c);
|
adamc@1061
|
4226 }
|
adamc@1061
|
4227
|
adamc@1061
|
4228 uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4229 return !!isprint((int)c);
|
adamc@1061
|
4230 }
|
adamc@1061
|
4231
|
adamc@1061
|
4232 uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4233 return !!ispunct((int)c);
|
adamc@1061
|
4234 }
|
adamc@1061
|
4235
|
adamc@1061
|
4236 uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4237 return !!isspace((int)c);
|
adamc@1061
|
4238 }
|
adamc@1063
|
4239
|
adamc@1061
|
4240 uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4241 return !!isupper((int)c);
|
adamc@1061
|
4242 }
|
adamc@1061
|
4243
|
adamc@1061
|
4244 uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4245 return !!isxdigit((int)c);
|
adamc@1061
|
4246 }
|
adamc@1061
|
4247
|
adamc@1061
|
4248 uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4249 return tolower((int)c);
|
adamc@1061
|
4250 }
|
adamc@1061
|
4251
|
adamc@1061
|
4252 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
4253 return toupper((int)c);
|
adamc@1061
|
4254 }
|
adamc@1066
|
4255
|
adamc@1128
|
4256 uw_Basis_int uw_Basis_ord(uw_context ctx, uw_Basis_char c) {
|
adamc@1128
|
4257 return (unsigned char)c;
|
adamc@1128
|
4258 }
|
adamc@1128
|
4259
|
adamc@1128
|
4260 uw_Basis_char uw_Basis_chr(uw_context ctx, uw_Basis_int n) {
|
adamc@1128
|
4261 return n;
|
adamc@1128
|
4262 }
|
adamc@1128
|
4263
|
adamc@1066
|
4264 uw_Basis_string uw_Basis_currentUrl(uw_context ctx) {
|
adamc@1066
|
4265 return ctx->current_url;
|
adamc@1066
|
4266 }
|
adamc@1066
|
4267
|
adamc@1066
|
4268 void uw_set_currentUrl(uw_context ctx, char *s) {
|
adamc@1066
|
4269 ctx->current_url = s;
|
adamc@1066
|
4270 }
|
adamc@1114
|
4271
|
adamc@1114
|
4272 void uw_set_deadline(uw_context ctx, int n) {
|
adamc@1114
|
4273 ctx->deadline = n;
|
adamc@1114
|
4274 }
|
adamc@1114
|
4275
|
adamc@1114
|
4276 void uw_check_deadline(uw_context ctx) {
|
adamc@1114
|
4277 if (uw_time > ctx->deadline)
|
adamc@1114
|
4278 uw_error(ctx, FATAL, "Maximum running time exceeded");
|
adamc@1114
|
4279 }
|
adamc@1115
|
4280
|
adamc@1115
|
4281 size_t uw_database_max = SIZE_MAX;
|
adamc@1120
|
4282
|
adam@1389
|
4283 uw_Basis_int uw_Basis_naughtyDebug(uw_context ctx, uw_Basis_string s) {
|
adam@2143
|
4284 if (ctx->loggers->log_debug)
|
adam@2143
|
4285 ctx->loggers->log_debug(ctx->loggers->logger_data, "%s\n", s);
|
adam@2143
|
4286 else
|
adam@2143
|
4287 fprintf(stderr, "%s\n", s);
|
adam@1389
|
4288 return 0;
|
adam@1389
|
4289 }
|
adam@1389
|
4290
|
adamc@1120
|
4291 uw_Basis_unit uw_Basis_debug(uw_context ctx, uw_Basis_string s) {
|
grrwlf@1997
|
4292 if (ctx->loggers->log_debug)
|
grrwlf@1997
|
4293 ctx->loggers->log_debug(ctx->loggers->logger_data, "%s\n", s);
|
adam@1334
|
4294 else
|
adam@1334
|
4295 fprintf(stderr, "%s\n", s);
|
adamc@1120
|
4296 return uw_unit_v;
|
adamc@1120
|
4297 }
|
adamc@1250
|
4298
|
adamc@1250
|
4299 uw_Basis_int uw_Basis_rand(uw_context ctx) {
|
adam@2104
|
4300 int r = my_rand();
|
adam@2104
|
4301
|
adam@2104
|
4302 if (r >= 0)
|
adam@2104
|
4303 return r;
|
adam@1486
|
4304 else
|
adam@1486
|
4305 uw_error(ctx, FATAL, "Random number generation failed");
|
adamc@1250
|
4306 }
|
adam@1347
|
4307
|
adam@1347
|
4308 void uw_noPostBody(uw_context ctx) {
|
adam@1347
|
4309 ctx->hasPostBody = 0;
|
adam@1347
|
4310 }
|
adam@1347
|
4311
|
adam@1347
|
4312 void uw_postBody(uw_context ctx, uw_Basis_postBody pb) {
|
adam@1347
|
4313 ctx->hasPostBody = 1;
|
adam@1347
|
4314 ctx->postBody = pb;
|
adam@1347
|
4315 }
|
adam@1347
|
4316
|
adam@1347
|
4317 int uw_hasPostBody(uw_context ctx) {
|
adam@1347
|
4318 return ctx->hasPostBody;
|
adam@1347
|
4319 }
|
adam@1347
|
4320
|
adam@1386
|
4321 void uw_isPost(uw_context ctx) {
|
adam@1386
|
4322 ctx->isPost = 1;
|
adam@1386
|
4323 }
|
adam@1386
|
4324
|
adam@1386
|
4325 uw_Basis_bool uw_Basis_currentUrlHasPost(uw_context ctx) {
|
adam@1386
|
4326 return ctx->isPost;
|
adam@1386
|
4327 }
|
adam@1386
|
4328
|
adam@1485
|
4329 uw_Basis_bool uw_Basis_currentUrlHasQueryString(uw_context ctx) {
|
adam@1485
|
4330 return ctx->queryString != NULL && ctx->queryString[0] != 0;
|
adam@1485
|
4331 }
|
adam@1485
|
4332
|
adam@1370
|
4333 void uw_setQueryString(uw_context ctx, uw_Basis_string s) {
|
adam@1370
|
4334 ctx->queryString = s;
|
adam@1370
|
4335 }
|
adam@1370
|
4336
|
adam@1370
|
4337 uw_Basis_string uw_queryString(uw_context ctx) {
|
adam@1370
|
4338 return ctx->queryString;
|
adam@1370
|
4339 }
|
adam@1370
|
4340
|
adam@1347
|
4341 uw_Basis_postBody uw_getPostBody(uw_context ctx) {
|
adam@1347
|
4342 if (ctx->hasPostBody)
|
adam@1347
|
4343 return ctx->postBody;
|
adam@1347
|
4344 else
|
adam@1347
|
4345 uw_error(ctx, FATAL, "Asked for POST body when none exists");
|
adam@1347
|
4346 }
|
adam@1349
|
4347
|
adam@1349
|
4348 failure_kind uw_runCallback(uw_context ctx, void (*callback)(uw_context)) {
|
adam@1349
|
4349 int r = setjmp(ctx->jmp_buf);
|
adam@1349
|
4350
|
adam@1349
|
4351 if (r == 0) {
|
adam@1936
|
4352 uw_ensure_transaction(ctx);
|
adam@1418
|
4353
|
adam@1349
|
4354 callback(ctx);
|
adam@1349
|
4355 }
|
adam@1349
|
4356
|
adam@1349
|
4357 return r;
|
adam@1349
|
4358 }
|
adam@1360
|
4359
|
adam@1360
|
4360 uw_Basis_string uw_Basis_crypt(uw_context ctx, uw_Basis_string key, uw_Basis_string salt) {
|
adam@1397
|
4361 char buf[14];
|
adam@1397
|
4362 return uw_strdup(ctx, DES_fcrypt(key, salt, buf));
|
adam@1360
|
4363 }
|
adam@1365
|
4364
|
adam@1365
|
4365 uw_Basis_bool uw_Basis_eq_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
|
adam@1365
|
4366 return !!(t1.seconds == t2.seconds && t1.microseconds == t2.microseconds);
|
adam@1365
|
4367 }
|
adam@1365
|
4368
|
adam@1365
|
4369 uw_Basis_bool uw_Basis_lt_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
|
adam@2034
|
4370 return !!(t1.seconds < t2.seconds || (t1.seconds == t2.seconds && t1.microseconds < t2.microseconds));
|
adam@1365
|
4371 }
|
adam@1365
|
4372
|
adam@1365
|
4373 uw_Basis_bool uw_Basis_le_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
|
adam@1365
|
4374 return !!(uw_Basis_eq_time(ctx, t1, t2) || uw_Basis_lt_time(ctx, t1, t2));
|
adam@1365
|
4375 }
|
adam@1365
|
4376
|
adam@1372
|
4377 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
|
adam@1374
|
4378 struct tm stm = {};
|
adam@1375
|
4379 char *end = strchr(s, 0);
|
adam@1449
|
4380 stm.tm_isdst = -1;
|
adam@1375
|
4381
|
adam@1632
|
4382 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end || strptime(s, TIME_FMT_JS, &stm) == end) {
|
adam@1373
|
4383 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1375
|
4384
|
kkallio@1448
|
4385 r->seconds = timegm(&stm);
|
adam@1373
|
4386 r->microseconds = 0;
|
adam@1373
|
4387
|
adam@1373
|
4388 return r;
|
adam@1374
|
4389 }
|
adam@1374
|
4390 else
|
adam@1373
|
4391 return NULL;
|
adam@1372
|
4392 }
|
adam@1407
|
4393
|
adam@1407
|
4394 failure_kind uw_begin_onError(uw_context ctx, char *msg) {
|
adam@1407
|
4395 int r = setjmp(ctx->jmp_buf);
|
adam@1407
|
4396
|
adam@1407
|
4397 if (ctx->app->on_error) {
|
adam@1407
|
4398 if (r == 0) {
|
adam@1936
|
4399 uw_ensure_transaction(ctx);
|
adam@1407
|
4400
|
adam@1409
|
4401 uw_buffer_reset(&ctx->outHeaders);
|
adam@1409
|
4402 if (on_success[0])
|
adam@1409
|
4403 uw_write_header(ctx, "HTTP/1.1 ");
|
adam@1409
|
4404 else
|
adam@1409
|
4405 uw_write_header(ctx, "Status: ");
|
adam@1409
|
4406 uw_write_header(ctx, "500 Internal Server Error\r\n");
|
adam@1919
|
4407 uw_write_header(ctx, "Content-type: text/html\r\n");
|
adam@1956
|
4408 uw_write(ctx, ctx->app->is_html5 ? uw_begin_html5 : uw_begin_xhtml);
|
adam@1407
|
4409 ctx->app->on_error(ctx, msg);
|
adam@1407
|
4410 uw_write(ctx, "</html>");
|
adam@1407
|
4411 }
|
adam@1407
|
4412
|
adam@1407
|
4413 return r;
|
adam@1407
|
4414 } else
|
adam@1407
|
4415 uw_error(ctx, FATAL, "Tried to run nonexistent onError handler");
|
adam@1407
|
4416 }
|
adam@1451
|
4417
|
adam@1451
|
4418 void uw_mayReturnIndirectly(uw_context ctx) {
|
adam@1451
|
4419 ctx->allowed_to_return_indirectly = 1;
|
adam@1451
|
4420 }
|
adam@1483
|
4421
|
adam@1556
|
4422 uw_Basis_string uw_Basis_fresh(uw_context ctx) {
|
adam@1703
|
4423 int len;
|
adam@1703
|
4424 char *r;
|
adam@1703
|
4425
|
adam@1703
|
4426 uw_check_heap(ctx, 2+INTS_MAX);
|
adam@1703
|
4427 r = ctx->heap.front;
|
adam@1703
|
4428 sprintf(r, "uw%u%n", ctx->nextId++, &len);
|
adam@1703
|
4429 ctx->heap.front += len+1;
|
adam@1703
|
4430 return r;
|
adam@1556
|
4431 }
|
adam@1571
|
4432
|
adam@1571
|
4433 uw_Basis_float uw_Basis_floatFromInt(uw_context ctx, uw_Basis_int n) {
|
adam@1571
|
4434 return n;
|
adam@1571
|
4435 }
|
adam@1571
|
4436
|
adam@1571
|
4437 uw_Basis_int uw_Basis_ceil(uw_context ctx, uw_Basis_float n) {
|
adam@1571
|
4438 return ceil(n);
|
adam@1571
|
4439 }
|
adam@1571
|
4440
|
adam@1571
|
4441 uw_Basis_int uw_Basis_trunc(uw_context ctx, uw_Basis_float n) {
|
adam@1571
|
4442 return trunc(n);
|
adam@1571
|
4443 }
|
adam@1571
|
4444
|
adam@1571
|
4445 uw_Basis_int uw_Basis_round(uw_context ctx, uw_Basis_float n) {
|
adam@1571
|
4446 return round(n);
|
adam@1571
|
4447 }
|
adam@1755
|
4448
|
adam@1755
|
4449 uw_Basis_string uw_Basis_atom(uw_context ctx, uw_Basis_string s) {
|
adam@1755
|
4450 char *p;
|
adam@1755
|
4451
|
adam@1755
|
4452 for (p = s; *p; ++p) {
|
adam@1755
|
4453 char c = *p;
|
adam@1801
|
4454 if (!isalnum((int)c) && c != '+' && c != '-' && c != '.' && c != '%' && c != '#')
|
adam@1755
|
4455 uw_error(ctx, FATAL, "Disallowed character in CSS atom");
|
adam@1755
|
4456 }
|
adam@1755
|
4457
|
adam@1755
|
4458 return s;
|
adam@1755
|
4459 }
|
adam@1755
|
4460
|
adam@1755
|
4461 uw_Basis_string uw_Basis_css_url(uw_context ctx, uw_Basis_string s) {
|
adam@1755
|
4462 char *p;
|
adam@1755
|
4463
|
adam@1755
|
4464 for (p = s; *p; ++p) {
|
adam@1755
|
4465 char c = *p;
|
adam@1801
|
4466 if (!isalnum((int)c) && c != ':' && c != '/' && c != '.' && c != '_' && c != '+'
|
adam@1755
|
4467 && c != '-' && c != '%' && c != '?' && c != '&' && c != '=' && c != '#')
|
adam@1755
|
4468 uw_error(ctx, FATAL, "Disallowed character in CSS URL");
|
adam@1755
|
4469 }
|
adam@1755
|
4470
|
adam@1755
|
4471 return s;
|
adam@1755
|
4472 }
|
adam@1755
|
4473
|
adam@1755
|
4474 uw_Basis_string uw_Basis_property(uw_context ctx, uw_Basis_string s) {
|
adam@1755
|
4475 char *p;
|
adam@1755
|
4476
|
adam@1755
|
4477 if (!*s)
|
adam@1755
|
4478 uw_error(ctx, FATAL, "Empty CSS property");
|
adam@1755
|
4479
|
adam@1801
|
4480 if (!islower((int)s[0]) && s[0] != '_')
|
adam@1755
|
4481 uw_error(ctx, FATAL, "Bad initial character in CSS property");
|
adam@1755
|
4482
|
adam@1755
|
4483 for (p = s; *p; ++p) {
|
adam@1755
|
4484 char c = *p;
|
adam@1801
|
4485 if (!islower((int)c) && !isdigit((int)c) && c != '_' && c != '-')
|
adam@1755
|
4486 uw_error(ctx, FATAL, "Disallowed character in CSS property");
|
adam@1755
|
4487 }
|
adam@1755
|
4488
|
adam@1755
|
4489 return s;
|
adam@1755
|
4490 }
|
adam@1787
|
4491
|
adam@1787
|
4492 uw_Basis_string uw_Basis_fieldName(uw_context ctx, uw_Basis_postField f) {
|
adam@1787
|
4493 return f.name;
|
adam@1787
|
4494 }
|
adam@1787
|
4495
|
adam@1787
|
4496 uw_Basis_string uw_Basis_fieldValue(uw_context ctx, uw_Basis_postField f) {
|
adam@1787
|
4497 return f.value;
|
adam@1787
|
4498 }
|
adam@1787
|
4499
|
adam@1787
|
4500 uw_Basis_string uw_Basis_remainingFields(uw_context ctx, uw_Basis_postField f) {
|
adam@1787
|
4501 return f.remaining;
|
adam@1787
|
4502 }
|
adam@1787
|
4503
|
adam@1787
|
4504 uw_Basis_postField *uw_Basis_firstFormField(uw_context ctx, uw_Basis_string s) {
|
adam@1788
|
4505 char *unurl;
|
adam@1787
|
4506 uw_Basis_postField *f;
|
adam@1787
|
4507
|
adam@1788
|
4508 if (!ctx->hasPostBody)
|
adam@1788
|
4509 uw_error(ctx, FATAL, "firstFormField called when there is no POST body");
|
adam@1788
|
4510
|
adam@1788
|
4511 if (s < ctx->postBody.data || s >= ctx->postBody.data + ctx->postBody.len)
|
adam@1787
|
4512 return NULL;
|
adam@1787
|
4513
|
adam@1787
|
4514 f = uw_malloc(ctx, sizeof(uw_Basis_postField));
|
adam@1788
|
4515 unurl = s;
|
adam@1787
|
4516 f->name = uw_Basis_unurlifyString(ctx, &unurl);
|
adam@1788
|
4517 s = strchr(s, 0);
|
adam@1788
|
4518 if (!s)
|
adam@1788
|
4519 uw_error(ctx, FATAL, "firstFormField: Missing null terminator");
|
adam@1788
|
4520 ++s;
|
adam@1788
|
4521 unurl = s;
|
adam@1788
|
4522 f->value = uw_Basis_unurlifyString(ctx, &unurl);
|
adam@1788
|
4523 s = strchr(s, 0);
|
adam@1788
|
4524 if (!s)
|
adam@1788
|
4525 uw_error(ctx, FATAL, "firstFormField: Missing null terminator");
|
adam@1788
|
4526 f->remaining = s+1;
|
adam@1787
|
4527
|
adam@1787
|
4528 return f;
|
adam@1787
|
4529 }
|
adam@2008
|
4530
|
adam@2008
|
4531 uw_Basis_string uw_Basis_blessData(uw_context ctx, uw_Basis_string s) {
|
adam@2008
|
4532 char *p = s;
|
adam@2008
|
4533
|
adam@2008
|
4534 for (; *p; ++p)
|
adam@2008
|
4535 if (!isalnum(*p) && *p != '-' && *p != '_')
|
adam@2008
|
4536 uw_error(ctx, FATAL, "Illegal HTML5 data-* attribute: %s", s);
|
adam@2008
|
4537
|
adam@2008
|
4538 return s;
|
adam@2008
|
4539 }
|
adam@2073
|
4540
|
adam@2073
|
4541 int uw_remoteSock(uw_context ctx) {
|
adam@2073
|
4542 return ctx->remoteSock;
|
adam@2073
|
4543 }
|
adam@2073
|
4544
|
adam@2073
|
4545 void uw_set_remoteSock(uw_context ctx, int sock) {
|
adam@2073
|
4546 ctx->remoteSock = sock;
|
adam@2073
|
4547 }
|
ziv@2234
|
4548
|
ziv@2234
|
4549
|
ziv@2234
|
4550 // Sqlcache
|
ziv@2234
|
4551
|
ziv@2284
|
4552 typedef struct uw_Sqlcache_Entry {
|
ziv@2284
|
4553 char *key;
|
ziv@2284
|
4554 uw_Sqlcache_Value *value;
|
ziv@2284
|
4555 unsigned long timeInvalid;
|
ziv@2284
|
4556 UT_hash_handle hh;
|
ziv@2284
|
4557 } uw_Sqlcache_Entry;
|
ziv@2284
|
4558
|
ziv@2279
|
4559 void uw_Sqlcache_freeValue(uw_Sqlcache_Value *value) {
|
ziv@2234
|
4560 if (value) {
|
ziv@2234
|
4561 free(value->result);
|
ziv@2234
|
4562 free(value->output);
|
ziv@2234
|
4563 free(value);
|
ziv@2234
|
4564 }
|
ziv@2234
|
4565 }
|
ziv@2234
|
4566
|
ziv@2279
|
4567 void uw_Sqlcache_freeEntry(uw_Sqlcache_Entry* entry) {
|
ziv@2279
|
4568 if (entry) {
|
ziv@2279
|
4569 free(entry->key);
|
ziv@2279
|
4570 uw_Sqlcache_freeValue(entry->value);
|
ziv@2279
|
4571 free(entry);
|
ziv@2279
|
4572 }
|
ziv@2279
|
4573 }
|
ziv@2279
|
4574
|
ziv@2279
|
4575 // TODO: pick a number.
|
ziv@2279
|
4576 unsigned int uw_Sqlcache_maxSize = 1234567890;
|
ziv@2279
|
4577
|
ziv@2279
|
4578 void uw_Sqlcache_delete(uw_Sqlcache_Cache *cache, uw_Sqlcache_Entry *entry) {
|
ziv@2279
|
4579 HASH_DEL(cache->table, entry);
|
ziv@2279
|
4580 uw_Sqlcache_freeEntry(entry);
|
ziv@2279
|
4581 }
|
ziv@2279
|
4582
|
ziv@2279
|
4583 uw_Sqlcache_Entry *uw_Sqlcache_find(uw_Sqlcache_Cache *cache, char *key, size_t len, int bump) {
|
ziv@2279
|
4584 uw_Sqlcache_Entry *entry = NULL;
|
ziv@2279
|
4585 HASH_FIND(hh, cache->table, key, len, entry);
|
ziv@2279
|
4586 if (entry && bump) {
|
ziv@2279
|
4587 // Bump for LRU purposes.
|
ziv@2279
|
4588 HASH_DEL(cache->table, entry);
|
ziv@2279
|
4589 // Important that we use [entry->key], because [key] might be ephemeral.
|
ziv@2279
|
4590 HASH_ADD_KEYPTR(hh, cache->table, entry->key, len, entry);
|
ziv@2279
|
4591 }
|
ziv@2279
|
4592 return entry;
|
ziv@2279
|
4593 }
|
ziv@2279
|
4594
|
ziv@2279
|
4595 void uw_Sqlcache_add(uw_Sqlcache_Cache *cache, uw_Sqlcache_Entry *entry, size_t len) {
|
ziv@2279
|
4596 HASH_ADD_KEYPTR(hh, cache->table, entry->key, len, entry);
|
ziv@2279
|
4597 if (HASH_COUNT(cache->table) > uw_Sqlcache_maxSize) {
|
ziv@2279
|
4598 // Deletes the first element of the cache.
|
ziv@2279
|
4599 uw_Sqlcache_delete(cache, cache->table);
|
ziv@2279
|
4600 }
|
ziv@2279
|
4601 }
|
ziv@2279
|
4602
|
ziv@2279
|
4603 unsigned long uw_Sqlcache_getTimeNow(uw_Sqlcache_Cache *cache) {
|
ziv@2279
|
4604 return ++cache->timeNow;
|
ziv@2279
|
4605 }
|
ziv@2279
|
4606
|
ziv@2279
|
4607 unsigned long uw_Sqlcache_timeMax(unsigned long x, unsigned long y) {
|
ziv@2279
|
4608 return x > y ? x : y;
|
ziv@2279
|
4609 }
|
ziv@2279
|
4610
|
ziv@2279
|
4611 char uw_Sqlcache_keySep = '_';
|
ziv@2279
|
4612
|
ziv@2284
|
4613 char *uw_Sqlcache_allocKeyBuffer(char **keys, size_t numKeys) {
|
ziv@2279
|
4614 size_t len = 0;
|
ziv@2279
|
4615 while (numKeys-- > 0) {
|
ziv@2279
|
4616 char* k = keys[numKeys];
|
ziv@2279
|
4617 if (!k) {
|
ziv@2279
|
4618 // Can only happen when flushihg, in which case we don't need anything past the null key.
|
ziv@2279
|
4619 break;
|
ziv@2234
|
4620 }
|
ziv@2279
|
4621 // Leave room for separator.
|
ziv@2279
|
4622 len += 1 + strlen(k);
|
ziv@2234
|
4623 }
|
ziv@2279
|
4624 char *buf = malloc(len+1);
|
ziv@2279
|
4625 // If nothing is copied into the buffer, it should look like it has length 0.
|
ziv@2279
|
4626 buf[0] = 0;
|
ziv@2279
|
4627 return buf;
|
ziv@2279
|
4628 }
|
ziv@2279
|
4629
|
ziv@2279
|
4630 char *uw_Sqlcache_keyCopy(char *buf, char *key) {
|
ziv@2279
|
4631 *buf++ = uw_Sqlcache_keySep;
|
ziv@2279
|
4632 return stpcpy(buf, key);
|
ziv@2279
|
4633 }
|
ziv@2279
|
4634
|
ziv@2279
|
4635 // The NUL-terminated prefix of [key] below always looks something like "_k1_k2_k3..._kn".
|
ziv@2285
|
4636
|
ziv@2285
|
4637 uw_Sqlcache_Value *uw_Sqlcache_check(uw_context ctx, uw_Sqlcache_Cache *cache, char **keys) {
|
ziv@2285
|
4638 pthread_rwlock_rdlock(&cache->lock);
|
ziv@2281
|
4639 size_t numKeys = cache->numKeys;
|
ziv@2279
|
4640 char *key = uw_Sqlcache_allocKeyBuffer(keys, numKeys);
|
ziv@2279
|
4641 char *buf = key;
|
ziv@2279
|
4642 time_t timeInvalid = cache->timeInvalid;
|
ziv@2279
|
4643 uw_Sqlcache_Entry *entry;
|
ziv@2279
|
4644 while (numKeys-- > 0) {
|
ziv@2279
|
4645 buf = uw_Sqlcache_keyCopy(buf, keys[numKeys]);
|
ziv@2279
|
4646 size_t len = buf - key;
|
ziv@2279
|
4647 entry = uw_Sqlcache_find(cache, key, len, 1);
|
ziv@2279
|
4648 if (!entry) {
|
ziv@2279
|
4649 free(key);
|
ziv@2285
|
4650 pthread_rwlock_unlock(&cache->lock);
|
ziv@2279
|
4651 return NULL;
|
ziv@2279
|
4652 }
|
ziv@2279
|
4653 timeInvalid = uw_Sqlcache_timeMax(timeInvalid, entry->timeInvalid);
|
ziv@2234
|
4654 }
|
ziv@2279
|
4655 free(key);
|
ziv@2285
|
4656 // TODO: pass back copy of value and free it in the generated code... or use uw_malloc?
|
ziv@2279
|
4657 uw_Sqlcache_Value *value = entry->value;
|
ziv@2285
|
4658 pthread_rwlock_unlock(&cache->lock);
|
ziv@2279
|
4659 return value && value->timeValid > timeInvalid ? value : NULL;
|
ziv@2279
|
4660 }
|
ziv@2279
|
4661
|
ziv@2285
|
4662 void uw_Sqlcache_storeCommitOne(uw_Sqlcache_Cache *cache, char **keys, uw_Sqlcache_Value *value) {
|
ziv@2285
|
4663 pthread_rwlock_wrlock(&cache->lock);
|
ziv@2281
|
4664 size_t numKeys = cache->numKeys;
|
ziv@2279
|
4665 char *key = uw_Sqlcache_allocKeyBuffer(keys, numKeys);
|
ziv@2279
|
4666 char *buf = key;
|
ziv@2279
|
4667 time_t timeNow = uw_Sqlcache_getTimeNow(cache);
|
ziv@2279
|
4668 uw_Sqlcache_Entry *entry;
|
ziv@2279
|
4669 while (numKeys-- > 0) {
|
ziv@2279
|
4670 buf = uw_Sqlcache_keyCopy(buf, keys[numKeys]);
|
ziv@2279
|
4671 size_t len = buf - key;
|
ziv@2279
|
4672 entry = uw_Sqlcache_find(cache, key, len, 1);
|
ziv@2279
|
4673 if (!entry) {
|
ziv@2279
|
4674 entry = malloc(sizeof(uw_Sqlcache_Entry));
|
ziv@2279
|
4675 entry->key = strdup(key);
|
ziv@2279
|
4676 entry->value = NULL;
|
ziv@2285
|
4677 entry->timeInvalid = 0;
|
ziv@2279
|
4678 uw_Sqlcache_add(cache, entry, len);
|
ziv@2234
|
4679 }
|
ziv@2234
|
4680 }
|
ziv@2279
|
4681 free(key);
|
ziv@2279
|
4682 uw_Sqlcache_freeValue(entry->value);
|
ziv@2279
|
4683 entry->value = value;
|
ziv@2279
|
4684 entry->value->timeValid = timeNow;
|
ziv@2285
|
4685 pthread_rwlock_unlock(&cache->lock);
|
ziv@2279
|
4686 }
|
ziv@2279
|
4687
|
ziv@2282
|
4688 void uw_Sqlcache_flushCommitOne(uw_Sqlcache_Cache *cache, char **keys) {
|
ziv@2285
|
4689 pthread_rwlock_wrlock(&cache->lock);
|
ziv@2281
|
4690 size_t numKeys = cache->numKeys;
|
ziv@2279
|
4691 char *key = uw_Sqlcache_allocKeyBuffer(keys, numKeys);
|
ziv@2279
|
4692 char *buf = key;
|
ziv@2279
|
4693 time_t timeNow = uw_Sqlcache_getTimeNow(cache);
|
ziv@2279
|
4694 uw_Sqlcache_Entry *entry;
|
ziv@2279
|
4695 while (numKeys-- > 0) {
|
ziv@2279
|
4696 char *k = keys[numKeys];
|
ziv@2279
|
4697 if (!k) {
|
ziv@2279
|
4698 if (entry) {
|
ziv@2279
|
4699 entry->timeInvalid = timeNow;
|
ziv@2234
|
4700 } else {
|
ziv@2279
|
4701 // Haven't found an entry yet, so the first key was null.
|
ziv@2279
|
4702 cache->timeInvalid = timeNow;
|
ziv@2234
|
4703 }
|
ziv@2279
|
4704 free(key);
|
ziv@2279
|
4705 return;
|
ziv@2234
|
4706 }
|
ziv@2279
|
4707 buf = uw_Sqlcache_keyCopy(buf, k);
|
ziv@2279
|
4708 size_t len = buf - key;
|
ziv@2279
|
4709 entry = uw_Sqlcache_find(cache, key, len, 0);
|
ziv@2279
|
4710 if (!entry) {
|
ziv@2279
|
4711 free(key);
|
ziv@2279
|
4712 return;
|
ziv@2279
|
4713 }
|
ziv@2234
|
4714 }
|
ziv@2279
|
4715 free(key);
|
ziv@2279
|
4716 // All the keys were non-null and the relevant entry is present, so we delete it.
|
ziv@2279
|
4717 uw_Sqlcache_delete(cache, entry);
|
ziv@2285
|
4718 pthread_rwlock_unlock(&cache->lock);
|
ziv@2285
|
4719 }
|
ziv@2285
|
4720
|
ziv@2285
|
4721 void uw_Sqlcache_freeUpdate(void *data, int dontCare) {
|
ziv@2285
|
4722 uw_context ctx = (uw_context)data;
|
ziv@2285
|
4723 uw_Sqlcache_Update *update = ctx->cacheUpdate;
|
ziv@2285
|
4724 while (update) {
|
ziv@2285
|
4725 char** keys = update->keys;
|
ziv@2285
|
4726 size_t numKeys = update->cache->numKeys;
|
ziv@2282
|
4727 while (numKeys-- > 0) {
|
ziv@2282
|
4728 free(keys[numKeys]);
|
ziv@2282
|
4729 }
|
ziv@2282
|
4730 free(keys);
|
ziv@2285
|
4731 // Don't free [update->value]: it's in the cache now!
|
ziv@2285
|
4732 uw_Sqlcache_Update *nextUpdate = update->next;
|
ziv@2285
|
4733 free(update);
|
ziv@2285
|
4734 update = nextUpdate;
|
ziv@2282
|
4735 }
|
ziv@2285
|
4736 ctx->cacheUpdate = NULL;
|
ziv@2285
|
4737 ctx->cacheUpdateTail = NULL;
|
ziv@2285
|
4738 }
|
ziv@2285
|
4739
|
ziv@2285
|
4740 void uw_Sqlcache_commitUpdate(void *data) {
|
ziv@2285
|
4741 uw_context ctx = (uw_context)data;
|
ziv@2285
|
4742 uw_Sqlcache_Update *update = ctx->cacheUpdate;
|
ziv@2285
|
4743 while (update) {
|
ziv@2285
|
4744 uw_Sqlcache_Cache *cache = update->cache;
|
ziv@2285
|
4745 char **keys = update->keys;
|
ziv@2285
|
4746 if (update->value) {
|
ziv@2285
|
4747 uw_Sqlcache_storeCommitOne(cache, keys, update->value);
|
ziv@2285
|
4748 } else {
|
ziv@2285
|
4749 uw_Sqlcache_flushCommitOne(cache, keys);
|
ziv@2285
|
4750 }
|
ziv@2285
|
4751 update = update->next;
|
ziv@2282
|
4752 }
|
ziv@2284
|
4753 }
|
ziv@2284
|
4754
|
ziv@2284
|
4755 char **uw_Sqlcache_copyKeys(char **keys, size_t numKeys) {
|
ziv@2284
|
4756 char **copy = malloc(sizeof(char *) * numKeys);
|
ziv@2284
|
4757 while (numKeys-- > 0) {
|
ziv@2285
|
4758 char *k = keys[numKeys];
|
ziv@2284
|
4759 copy[numKeys] = k ? strdup(k) : NULL;
|
ziv@2284
|
4760 }
|
ziv@2284
|
4761 return copy;
|
ziv@2282
|
4762 }
|
ziv@2282
|
4763
|
ziv@2285
|
4764 void uw_Sqlcache_store(uw_context ctx, uw_Sqlcache_Cache *cache, char **keys, uw_Sqlcache_Value *value) {
|
ziv@2285
|
4765 uw_Sqlcache_Update *update = malloc(sizeof(uw_Sqlcache_Update));
|
ziv@2285
|
4766 update->cache = cache;
|
ziv@2285
|
4767 update->keys = uw_Sqlcache_copyKeys(keys, cache->numKeys);
|
ziv@2285
|
4768 update->value = value;
|
ziv@2285
|
4769 update->next = NULL;
|
ziv@2285
|
4770 if (ctx->cacheUpdateTail) {
|
ziv@2285
|
4771 // An update is already registered, so just extend it.
|
ziv@2285
|
4772 ctx->cacheUpdateTail->next = update;
|
ziv@2285
|
4773 } else {
|
ziv@2285
|
4774 ctx->cacheUpdate = update;
|
ziv@2285
|
4775 uw_register_transactional(ctx, ctx, uw_Sqlcache_commitUpdate, NULL, uw_Sqlcache_freeUpdate);
|
ziv@2285
|
4776 }
|
ziv@2285
|
4777 ctx->cacheUpdateTail = update;
|
ziv@2285
|
4778 }
|
ziv@2285
|
4779
|
ziv@2282
|
4780 void uw_Sqlcache_flush(uw_context ctx, uw_Sqlcache_Cache *cache, char **keys) {
|
ziv@2285
|
4781 // A flush is represented in the queue as storing NULL.
|
ziv@2285
|
4782 uw_Sqlcache_store(ctx, cache, keys, NULL);
|
ziv@2285
|
4783 }
|