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