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