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