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