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