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