adamc@436
|
1 #define _XOPEN_SOURCE
|
adamc@436
|
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@102
|
10
|
adamc@667
|
11 #include <pthread.h>
|
adamc@667
|
12
|
adamc@102
|
13 #include "types.h"
|
adamc@102
|
14
|
adamc@311
|
15 uw_unit uw_unit_v = {};
|
adamc@102
|
16
|
adamc@667
|
17
|
adamc@667
|
18 // Socket extras
|
adamc@667
|
19
|
adamc@667
|
20 int uw_really_send(int sock, const void *buf, size_t len) {
|
adamc@667
|
21 while (len > 0) {
|
adamc@667
|
22 size_t n = send(sock, buf, len, 0);
|
adamc@667
|
23
|
adamc@667
|
24 if (n < 0)
|
adamc@667
|
25 return n;
|
adamc@667
|
26
|
adamc@667
|
27 buf += n;
|
adamc@667
|
28 len -= n;
|
adamc@667
|
29 }
|
adamc@667
|
30
|
adamc@667
|
31 return 0;
|
adamc@667
|
32 }
|
adamc@667
|
33
|
adamc@667
|
34
|
adamc@667
|
35 // Buffers
|
adamc@667
|
36
|
adamc@667
|
37 typedef struct {
|
adamc@667
|
38 char *start, *front, *back;
|
adamc@667
|
39 } buf;
|
adamc@667
|
40
|
adamc@667
|
41 static void buf_init(buf *b, size_t s) {
|
adamc@667
|
42 b->front = b->start = malloc(s);
|
adamc@667
|
43 b->back = b->front + s;
|
adamc@667
|
44 }
|
adamc@667
|
45
|
adamc@667
|
46 static void buf_free(buf *b) {
|
adamc@667
|
47 free(b->start);
|
adamc@667
|
48 }
|
adamc@667
|
49
|
adamc@667
|
50 static void buf_reset(buf *b) {
|
adamc@667
|
51 b->front = b->start;
|
adamc@667
|
52 }
|
adamc@667
|
53
|
adamc@667
|
54 static void buf_check(buf *b, size_t extra) {
|
adamc@667
|
55 if (b->back - b->front < extra) {
|
adamc@667
|
56 size_t desired = b->front - b->start + extra, next;
|
adamc@667
|
57 char *new_heap;
|
adamc@667
|
58
|
adamc@667
|
59 next = b->back - b->start;
|
adamc@667
|
60 if (next == 0)
|
adamc@667
|
61 next = 1;
|
adamc@667
|
62 for (; next < desired; next *= 2);
|
adamc@667
|
63
|
adamc@667
|
64 new_heap = realloc(b->start, next);
|
adamc@667
|
65 b->front = new_heap + (b->front - b->start);
|
adamc@667
|
66 b->back = new_heap + next;
|
adamc@667
|
67 b->start = new_heap;
|
adamc@667
|
68 }
|
adamc@667
|
69 }
|
adamc@667
|
70
|
adamc@667
|
71 static size_t buf_used(buf *b) {
|
adamc@667
|
72 return b->front - b->start;
|
adamc@667
|
73 }
|
adamc@667
|
74
|
adamc@667
|
75 static size_t buf_avail(buf *b) {
|
adamc@667
|
76 return b->back - b->start;
|
adamc@667
|
77 }
|
adamc@667
|
78
|
adamc@668
|
79 static void buf_append(buf *b, const char *s, size_t len) {
|
adamc@668
|
80 buf_check(b, len);
|
adamc@668
|
81 memcpy(b->front, s, len);
|
adamc@668
|
82 b->front += len;
|
adamc@668
|
83 }
|
adamc@667
|
84
|
adamc@668
|
85
|
adamc@668
|
86 // Persistent client state
|
adamc@667
|
87
|
adamc@667
|
88 typedef enum { UNUSED, USED } usage;
|
adamc@667
|
89
|
adamc@667
|
90 typedef struct client {
|
adamc@667
|
91 size_t id;
|
adamc@667
|
92 usage mode;
|
adamc@667
|
93 union {
|
adamc@667
|
94 struct client *next;
|
adamc@667
|
95 struct {
|
adamc@667
|
96 pthread_mutex_t lock;
|
adamc@667
|
97 int pass;
|
adamc@667
|
98 buf msgs;
|
adamc@667
|
99 int sock;
|
adamc@667
|
100 time_t last_contact;
|
adamc@667
|
101 } used;
|
adamc@667
|
102 } data;
|
adamc@667
|
103 } client;
|
adamc@667
|
104
|
adamc@667
|
105 static client **clients, *clients_free;
|
adamc@667
|
106 static size_t n_clients;
|
adamc@667
|
107
|
adamc@667
|
108 static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
|
adamc@667
|
109
|
adamc@667
|
110 static client *uw_new_client() {
|
adamc@667
|
111 client *c;
|
adamc@667
|
112
|
adamc@667
|
113 pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
114
|
adamc@667
|
115 if (clients_free) {
|
adamc@667
|
116 c = clients_free;
|
adamc@667
|
117 clients_free = clients_free->data.next;
|
adamc@667
|
118 }
|
adamc@667
|
119 else {
|
adamc@667
|
120 ++n_clients;
|
adamc@667
|
121 clients = realloc(clients, sizeof(client) * n_clients);
|
adamc@667
|
122 c = malloc(sizeof(client));
|
adamc@667
|
123 c->id = n_clients-1;
|
adamc@667
|
124 clients[n_clients-1] = c;
|
adamc@667
|
125 }
|
adamc@667
|
126
|
adamc@667
|
127 c->mode = USED;
|
adamc@667
|
128 pthread_mutex_init(&c->data.used.lock, NULL);
|
adamc@667
|
129 c->data.used.pass = rand();
|
adamc@667
|
130 c->data.used.sock = -1;
|
adamc@667
|
131 c->data.used.last_contact = time(NULL);
|
adamc@667
|
132 buf_init(&c->data.used.msgs, 0);
|
adamc@667
|
133
|
adamc@667
|
134 pthread_mutex_unlock(&clients_mutex);
|
adamc@667
|
135
|
adamc@667
|
136 return c;
|
adamc@667
|
137 }
|
adamc@667
|
138
|
adamc@667
|
139 static const char begin_msgs[] = "HTTP/1.1 200 OK\r\nContent-type: text/plain\r\n\r\n";
|
adamc@667
|
140
|
adamc@668
|
141 static client *uw_find_client(size_t id) {
|
adamc@667
|
142 client *c;
|
adamc@667
|
143
|
adamc@667
|
144 pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
145
|
adamc@667
|
146 if (id >= n_clients) {
|
adamc@667
|
147 pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
148 return NULL;
|
adamc@667
|
149 }
|
adamc@667
|
150
|
adamc@667
|
151 c = clients[id];
|
adamc@667
|
152
|
adamc@667
|
153 if (c->mode != USED) {
|
adamc@667
|
154 pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
155 return NULL;
|
adamc@668
|
156 }
|
adamc@668
|
157
|
adamc@668
|
158 pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
159 return c;
|
adamc@668
|
160 }
|
adamc@668
|
161
|
adamc@668
|
162 void uw_client_connect(size_t id, int pass, int sock) {
|
adamc@668
|
163 client *c = uw_find_client(id);
|
adamc@668
|
164
|
adamc@668
|
165 if (c == NULL) {
|
adamc@667
|
166 close(sock);
|
adamc@668
|
167 fprintf(stderr, "Out-of-bounds client request (%d)\n", (int)id);
|
adamc@667
|
168 return;
|
adamc@667
|
169 }
|
adamc@667
|
170
|
adamc@667
|
171 pthread_mutex_lock(&c->data.used.lock);
|
adamc@667
|
172
|
adamc@667
|
173 if (pass != c->data.used.pass) {
|
adamc@667
|
174 pthread_mutex_unlock(&c->data.used.lock);
|
adamc@667
|
175 close(sock);
|
adamc@667
|
176 fprintf(stderr, "Wrong client password (%d)\n", (int)id);
|
adamc@667
|
177 return;
|
adamc@667
|
178 }
|
adamc@667
|
179
|
adamc@667
|
180 if (c->data.used.sock != -1) {
|
adamc@667
|
181 pthread_mutex_unlock(&c->data.used.lock);
|
adamc@667
|
182 close(sock);
|
adamc@667
|
183 fprintf(stderr, "Duplicate client connection (%d)\n", (int)id);
|
adamc@667
|
184 return;
|
adamc@667
|
185 }
|
adamc@667
|
186
|
adamc@667
|
187 c->data.used.last_contact = time(NULL);
|
adamc@667
|
188
|
adamc@667
|
189 if (buf_used(&c->data.used.msgs) > 0) {
|
adamc@667
|
190 uw_really_send(sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adamc@667
|
191 uw_really_send(sock, c->data.used.msgs.start, buf_used(&c->data.used.msgs));
|
adamc@667
|
192 close(sock);
|
adamc@667
|
193 }
|
adamc@668
|
194 else
|
adamc@668
|
195 c->data.used.sock = sock;
|
adamc@667
|
196
|
adamc@667
|
197 pthread_mutex_unlock(&c->data.used.lock);
|
adamc@667
|
198 }
|
adamc@667
|
199
|
adamc@667
|
200 static void uw_free_client(client *c) {
|
adamc@667
|
201 printf("Freeing client %d\n", c->id);
|
adamc@667
|
202
|
adamc@667
|
203 if (c->mode == USED && c->data.used.sock != -1)
|
adamc@667
|
204 close(c->data.used.sock);
|
adamc@667
|
205
|
adamc@667
|
206 pthread_mutex_destroy(&c->data.used.lock);
|
adamc@667
|
207 buf_free(&c->data.used.msgs);
|
adamc@667
|
208 c->mode = UNUSED;
|
adamc@667
|
209 c->data.next = clients_free;
|
adamc@667
|
210 clients_free = c;
|
adamc@667
|
211 }
|
adamc@667
|
212
|
adamc@667
|
213 void uw_prune_clients(time_t timeout) {
|
adamc@667
|
214 size_t i;
|
adamc@667
|
215 time_t cutoff;
|
adamc@667
|
216
|
adamc@667
|
217 cutoff = time(NULL) - timeout;
|
adamc@667
|
218
|
adamc@667
|
219 pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
220
|
adamc@667
|
221 for (i = 0; i < n_clients; ++i) {
|
adamc@667
|
222 if (clients[i]->mode == USED && clients[i]->data.used.last_contact < cutoff)
|
adamc@667
|
223 uw_free_client(clients[i]);
|
adamc@667
|
224 }
|
adamc@667
|
225
|
adamc@667
|
226 pthread_mutex_unlock(&clients_mutex);
|
adamc@667
|
227 }
|
adamc@667
|
228
|
adamc@667
|
229
|
adamc@668
|
230 // Persistent channel state
|
adamc@668
|
231
|
adamc@668
|
232 typedef struct client_list {
|
adamc@668
|
233 client *data;
|
adamc@668
|
234 struct client_list *next;
|
adamc@668
|
235 } client_list;
|
adamc@668
|
236
|
adamc@668
|
237 typedef struct channel {
|
adamc@668
|
238 size_t id;
|
adamc@668
|
239 usage mode;
|
adamc@668
|
240 union {
|
adamc@668
|
241 struct channel *next;
|
adamc@668
|
242 struct {
|
adamc@668
|
243 pthread_mutex_t lock;
|
adamc@668
|
244 client_list *clients;
|
adamc@668
|
245 } used;
|
adamc@668
|
246 } data;
|
adamc@668
|
247 } channel;
|
adamc@668
|
248
|
adamc@668
|
249 static channel **channels, *channels_free;
|
adamc@668
|
250 static size_t n_channels;
|
adamc@668
|
251
|
adamc@668
|
252 static pthread_mutex_t channels_mutex = PTHREAD_MUTEX_INITIALIZER;
|
adamc@668
|
253
|
adamc@668
|
254 static channel *uw_new_channel() {
|
adamc@668
|
255 channel *ch;
|
adamc@668
|
256
|
adamc@668
|
257 pthread_mutex_lock(&channels_mutex);
|
adamc@668
|
258
|
adamc@668
|
259 if (channels_free) {
|
adamc@668
|
260 ch = channels_free;
|
adamc@668
|
261 channels_free = channels_free->data.next;
|
adamc@668
|
262 }
|
adamc@668
|
263 else {
|
adamc@668
|
264 ++n_channels;
|
adamc@668
|
265 channels = realloc(channels, sizeof(channels) * n_channels);
|
adamc@668
|
266 ch = malloc(sizeof(channel));
|
adamc@668
|
267 ch->id = n_channels-1;
|
adamc@668
|
268 channels[n_channels-1] = ch;
|
adamc@668
|
269 }
|
adamc@668
|
270
|
adamc@668
|
271 ch->mode = USED;
|
adamc@668
|
272 pthread_mutex_init(&ch->data.used.lock, NULL);
|
adamc@668
|
273 ch->data.used.clients = NULL;
|
adamc@668
|
274
|
adamc@668
|
275 pthread_mutex_unlock(&channels_mutex);
|
adamc@668
|
276
|
adamc@668
|
277 return ch;
|
adamc@668
|
278 }
|
adamc@668
|
279
|
adamc@668
|
280 static channel *uw_find_channel(size_t id) {
|
adamc@668
|
281 channel *ch = NULL;
|
adamc@668
|
282
|
adamc@668
|
283 pthread_mutex_lock(&channels_mutex);
|
adamc@668
|
284
|
adamc@668
|
285 if (id < n_channels && channels[id]->mode == USED)
|
adamc@668
|
286 ch = channels[id];
|
adamc@668
|
287
|
adamc@668
|
288 pthread_mutex_unlock(&channels_mutex);
|
adamc@668
|
289
|
adamc@668
|
290 return ch;
|
adamc@668
|
291 }
|
adamc@668
|
292
|
adamc@668
|
293 static void uw_subscribe(channel *ch, client *c) {
|
adamc@668
|
294 client_list *cs = malloc(sizeof(client_list));
|
adamc@668
|
295
|
adamc@668
|
296 pthread_mutex_lock(&ch->data.used.lock);
|
adamc@668
|
297
|
adamc@668
|
298 cs->data = c;
|
adamc@668
|
299 cs->next = ch->data.used.clients;
|
adamc@668
|
300 ch->data.used.clients = cs;
|
adamc@668
|
301
|
adamc@668
|
302 pthread_mutex_unlock(&ch->data.used.lock);
|
adamc@668
|
303 }
|
adamc@668
|
304
|
adamc@668
|
305 static void uw_unsubscribe(channel *ch, client *c) {
|
adamc@668
|
306 client_list *prev, *cur, *tmp;
|
adamc@668
|
307
|
adamc@668
|
308 pthread_mutex_lock(&ch->data.used.lock);
|
adamc@668
|
309
|
adamc@668
|
310 for (prev = NULL, cur = ch->data.used.clients; cur; ) {
|
adamc@668
|
311 if (cur->data == c) {
|
adamc@668
|
312 if (prev)
|
adamc@668
|
313 prev->next = cur->next;
|
adamc@668
|
314 else
|
adamc@668
|
315 ch->data.used.clients = cur->next;
|
adamc@668
|
316 tmp = cur;
|
adamc@668
|
317 cur = cur->next;
|
adamc@668
|
318 free(tmp);
|
adamc@668
|
319 }
|
adamc@668
|
320 else {
|
adamc@668
|
321 prev = cur;
|
adamc@668
|
322 cur = cur->next;
|
adamc@668
|
323 }
|
adamc@668
|
324 }
|
adamc@668
|
325
|
adamc@668
|
326 pthread_mutex_unlock(&ch->data.used.lock);
|
adamc@668
|
327 }
|
adamc@668
|
328
|
adamc@668
|
329 static void uw_channel_send(channel *ch, const char *msg) {
|
adamc@668
|
330 size_t len = strlen(msg), preLen;
|
adamc@668
|
331 char pre[INTS_MAX + 2];
|
adamc@668
|
332 client_list *cs;
|
adamc@668
|
333
|
adamc@668
|
334 sprintf(pre, "%d\n", (int)ch->id);
|
adamc@668
|
335 preLen = strlen(pre);
|
adamc@668
|
336
|
adamc@668
|
337 pthread_mutex_lock(&ch->data.used.lock);
|
adamc@668
|
338
|
adamc@668
|
339 for (cs = ch->data.used.clients; cs; cs = cs->next) {
|
adamc@668
|
340 client *c = cs->data;
|
adamc@668
|
341
|
adamc@668
|
342 pthread_mutex_lock(&c->data.used.lock);
|
adamc@668
|
343
|
adamc@668
|
344 if (c->data.used.sock != -1) {
|
adamc@669
|
345 printf("Immediate send\n");
|
adamc@668
|
346 uw_really_send(c->data.used.sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adamc@668
|
347 uw_really_send(c->data.used.sock, pre, preLen);
|
adamc@668
|
348 uw_really_send(c->data.used.sock, msg, len);
|
adamc@668
|
349 uw_really_send(c->data.used.sock, "\n", 1);
|
adamc@668
|
350 close(c->data.used.sock);
|
adamc@668
|
351 c->data.used.sock = -1;
|
adamc@668
|
352 } else {
|
adamc@669
|
353 printf("Delayed send\n");
|
adamc@668
|
354 buf_append(&c->data.used.msgs, pre, preLen);
|
adamc@668
|
355 buf_append(&c->data.used.msgs, msg, len);
|
adamc@668
|
356 buf_append(&c->data.used.msgs, "\n", 1);
|
adamc@668
|
357 }
|
adamc@668
|
358
|
adamc@668
|
359 pthread_mutex_unlock(&c->data.used.lock);
|
adamc@668
|
360 }
|
adamc@668
|
361
|
adamc@668
|
362 pthread_mutex_unlock(&ch->data.used.lock);
|
adamc@668
|
363 }
|
adamc@668
|
364
|
adamc@668
|
365
|
adamc@668
|
366 // Global entry points
|
adamc@668
|
367
|
adamc@668
|
368 void uw_global_init() {
|
adamc@668
|
369 srand(time(NULL) ^ getpid());
|
adamc@668
|
370
|
adamc@668
|
371 clients = malloc(0);
|
adamc@668
|
372 channels = malloc(0);
|
adamc@668
|
373 }
|
adamc@668
|
374
|
adamc@668
|
375
|
adamc@667
|
376 // Single-request state
|
adamc@667
|
377
|
adamc@167
|
378 #define ERROR_BUF_LEN 1024
|
adamc@167
|
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@311
|
389 struct uw_context {
|
adamc@458
|
390 char *headers, *headers_end;
|
adamc@457
|
391
|
adamc@666
|
392 buf outHeaders, page, heap, script;
|
adamc@144
|
393 char **inputs;
|
adamc@167
|
394
|
adamc@565
|
395 int source_count;
|
adamc@562
|
396
|
adamc@272
|
397 void *db;
|
adamc@272
|
398
|
adamc@167
|
399 jmp_buf jmp_buf;
|
adamc@167
|
400
|
adamc@323
|
401 regions *regions;
|
adamc@323
|
402
|
adamc@425
|
403 cleanup *cleanup, *cleanup_front, *cleanup_back;
|
adamc@425
|
404
|
adamc@667
|
405 const char *script_header, *url_prefix;
|
adamc@643
|
406
|
adamc@167
|
407 char error_message[ERROR_BUF_LEN];
|
adamc@117
|
408 };
|
adamc@117
|
409
|
adamc@311
|
410 extern int uw_inputs_len;
|
adamc@144
|
411
|
adamc@562
|
412 uw_context uw_init(size_t outHeaders_len, size_t script_len, size_t page_len, size_t heap_len) {
|
adamc@311
|
413 uw_context ctx = malloc(sizeof(struct uw_context));
|
adamc@136
|
414
|
adamc@458
|
415 ctx->headers = ctx->headers_end = NULL;
|
adamc@457
|
416
|
adamc@666
|
417 buf_init(&ctx->outHeaders, outHeaders_len);
|
adamc@666
|
418 buf_init(&ctx->page, page_len);
|
adamc@666
|
419 buf_init(&ctx->heap, heap_len);
|
adamc@666
|
420 buf_init(&ctx->script, script_len);
|
adamc@667
|
421 ctx->script.start[0] = 0;
|
adamc@136
|
422
|
adamc@311
|
423 ctx->inputs = calloc(uw_inputs_len, sizeof(char *));
|
adamc@144
|
424
|
adamc@272
|
425 ctx->db = NULL;
|
adamc@272
|
426
|
adamc@323
|
427 ctx->regions = NULL;
|
adamc@323
|
428
|
adamc@425
|
429 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
|
adamc@425
|
430
|
adamc@643
|
431 ctx->script_header = "";
|
adamc@667
|
432 ctx->url_prefix = "/";
|
adamc@643
|
433
|
adamc@167
|
434 ctx->error_message[0] = 0;
|
adamc@167
|
435
|
adamc@565
|
436 ctx->source_count = 0;
|
adamc@562
|
437
|
adamc@117
|
438 return ctx;
|
adamc@106
|
439 }
|
adamc@106
|
440
|
adamc@311
|
441 void uw_set_db(uw_context ctx, void *db) {
|
adamc@272
|
442 ctx->db = db;
|
adamc@272
|
443 }
|
adamc@272
|
444
|
adamc@311
|
445 void *uw_get_db(uw_context ctx) {
|
adamc@272
|
446 return ctx->db;
|
adamc@272
|
447 }
|
adamc@272
|
448
|
adamc@311
|
449 void uw_free(uw_context ctx) {
|
adamc@666
|
450 buf_free(&ctx->outHeaders);
|
adamc@666
|
451 buf_free(&ctx->script);
|
adamc@666
|
452 buf_free(&ctx->page);
|
adamc@666
|
453 buf_free(&ctx->heap);
|
adamc@144
|
454 free(ctx->inputs);
|
adamc@425
|
455 free(ctx->cleanup);
|
adamc@136
|
456 free(ctx);
|
adamc@136
|
457 }
|
adamc@136
|
458
|
adamc@562
|
459 void uw_reset_keep_error_message(uw_context ctx) {
|
adamc@666
|
460 buf_reset(&ctx->outHeaders);
|
adamc@666
|
461 buf_reset(&ctx->script);
|
adamc@667
|
462 ctx->script.start[0] = 0;
|
adamc@666
|
463 buf_reset(&ctx->page);
|
adamc@666
|
464 buf_reset(&ctx->heap);
|
adamc@323
|
465 ctx->regions = NULL;
|
adamc@425
|
466 ctx->cleanup_front = ctx->cleanup;
|
adamc@565
|
467 ctx->source_count = 0;
|
adamc@167
|
468 }
|
adamc@167
|
469
|
adamc@562
|
470 void uw_reset_keep_request(uw_context ctx) {
|
adamc@562
|
471 uw_reset_keep_error_message(ctx);
|
adamc@562
|
472 ctx->error_message[0] = 0;
|
adamc@167
|
473 }
|
adamc@167
|
474
|
adamc@311
|
475 void uw_reset(uw_context ctx) {
|
adamc@311
|
476 uw_reset_keep_request(ctx);
|
adamc@311
|
477 memset(ctx->inputs, 0, uw_inputs_len * sizeof(char *));
|
adamc@144
|
478 }
|
adamc@144
|
479
|
adamc@311
|
480 void uw_db_init(uw_context);
|
adamc@311
|
481 void uw_handle(uw_context, char *);
|
adamc@167
|
482
|
adamc@311
|
483 failure_kind uw_begin_init(uw_context ctx) {
|
adamc@272
|
484 int r = setjmp(ctx->jmp_buf);
|
adamc@272
|
485
|
adamc@272
|
486 if (r == 0)
|
adamc@311
|
487 uw_db_init(ctx);
|
adamc@272
|
488
|
adamc@272
|
489 return r;
|
adamc@272
|
490 }
|
adamc@272
|
491
|
adamc@458
|
492 void uw_set_headers(uw_context ctx, char *headers) {
|
adamc@458
|
493 char *s = headers, *s2;
|
adamc@458
|
494 ctx->headers = headers;
|
adamc@458
|
495
|
adamc@458
|
496 while (s2 = strchr(s, '\r')) {
|
adamc@458
|
497 s = s2;
|
adamc@458
|
498
|
adamc@458
|
499 if (s[1] == 0)
|
adamc@458
|
500 break;
|
adamc@458
|
501
|
adamc@458
|
502 *s = 0;
|
adamc@458
|
503 s += 2;
|
adamc@458
|
504 }
|
adamc@458
|
505
|
adamc@458
|
506 ctx->headers_end = s;
|
adamc@458
|
507 }
|
adamc@458
|
508
|
adamc@458
|
509 failure_kind uw_begin(uw_context ctx, char *path) {
|
adamc@190
|
510 int r = setjmp(ctx->jmp_buf);
|
adamc@190
|
511
|
adamc@190
|
512 if (r == 0)
|
adamc@311
|
513 uw_handle(ctx, path);
|
adamc@167
|
514
|
adamc@190
|
515 return r;
|
adamc@167
|
516 }
|
adamc@167
|
517
|
adamc@311
|
518 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
|
adamc@425
|
519 cleanup *cl;
|
adamc@425
|
520
|
adamc@167
|
521 va_list ap;
|
adamc@167
|
522 va_start(ap, fmt);
|
adamc@167
|
523
|
adamc@167
|
524 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
|
adamc@167
|
525
|
adamc@425
|
526 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@425
|
527 cl->func(cl->arg);
|
adamc@425
|
528
|
adamc@425
|
529 ctx->cleanup_front = ctx->cleanup;
|
adamc@425
|
530
|
adamc@190
|
531 longjmp(ctx->jmp_buf, fk);
|
adamc@167
|
532 }
|
adamc@167
|
533
|
adamc@425
|
534 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
|
adamc@425
|
535 if (ctx->cleanup_front >= ctx->cleanup_back) {
|
adamc@425
|
536 int len = ctx->cleanup_back - ctx->cleanup, newLen;
|
adamc@425
|
537 if (len == 0)
|
adamc@425
|
538 newLen = 1;
|
adamc@425
|
539 else
|
adamc@428
|
540 newLen = len * 2;
|
adamc@470
|
541 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
|
adamc@425
|
542 ctx->cleanup_front = ctx->cleanup + len;
|
adamc@425
|
543 ctx->cleanup_back = ctx->cleanup + newLen;
|
adamc@425
|
544 }
|
adamc@425
|
545
|
adamc@425
|
546 ctx->cleanup_front->func = func;
|
adamc@425
|
547 ctx->cleanup_front->arg = arg;
|
adamc@425
|
548 ++ctx->cleanup_front;
|
adamc@425
|
549 }
|
adamc@425
|
550
|
adamc@425
|
551 void uw_pop_cleanup(uw_context ctx) {
|
adamc@425
|
552 if (ctx->cleanup_front == ctx->cleanup)
|
adamc@425
|
553 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
|
adamc@425
|
554
|
adamc@425
|
555 --ctx->cleanup_front;
|
adamc@425
|
556 ctx->cleanup_front->func(ctx->cleanup_front->arg);
|
adamc@425
|
557 }
|
adamc@425
|
558
|
adamc@311
|
559 char *uw_error_message(uw_context ctx) {
|
adamc@167
|
560 return ctx->error_message;
|
adamc@167
|
561 }
|
adamc@167
|
562
|
adamc@311
|
563 int uw_input_num(char*);
|
adamc@144
|
564
|
adamc@311
|
565 void uw_set_input(uw_context ctx, char *name, char *value) {
|
adamc@311
|
566 int n = uw_input_num(name);
|
adamc@144
|
567
|
adamc@169
|
568 if (n < 0)
|
adamc@311
|
569 uw_error(ctx, FATAL, "Bad input name %s", name);
|
adamc@144
|
570
|
adamc@311
|
571 if (n >= uw_inputs_len)
|
adamc@311
|
572 uw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n);
|
adamc@169
|
573
|
adamc@144
|
574 ctx->inputs[n] = value;
|
adamc@144
|
575
|
adamc@282
|
576 //printf("[%d] %s = %s\n", n, name, value);
|
adamc@144
|
577 }
|
adamc@144
|
578
|
adamc@311
|
579 char *uw_get_input(uw_context ctx, int n) {
|
adamc@169
|
580 if (n < 0)
|
adamc@311
|
581 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@311
|
582 if (n >= uw_inputs_len)
|
adamc@311
|
583 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@282
|
584 //printf("[%d] = %s\n", n, ctx->inputs[n]);
|
adamc@144
|
585 return ctx->inputs[n];
|
adamc@138
|
586 }
|
adamc@138
|
587
|
adamc@311
|
588 char *uw_get_optional_input(uw_context ctx, int n) {
|
adamc@190
|
589 if (n < 0)
|
adamc@311
|
590 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@311
|
591 if (n >= uw_inputs_len)
|
adamc@311
|
592 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@426
|
593 //printf("[%d] = %s\n", n, ctx->inputs[n]);
|
adamc@190
|
594 return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]);
|
adamc@190
|
595 }
|
adamc@190
|
596
|
adamc@643
|
597 void uw_set_script_header(uw_context ctx, const char *s) {
|
adamc@643
|
598 ctx->script_header = s;
|
adamc@643
|
599 }
|
adamc@643
|
600
|
adamc@667
|
601 void uw_set_url_prefix(uw_context ctx, const char *s) {
|
adamc@667
|
602 ctx->url_prefix = s;
|
adamc@667
|
603 }
|
adamc@667
|
604
|
adamc@667
|
605
|
adamc@667
|
606 static void buf_check_ctx(uw_context ctx, buf *b, size_t extra, const char *desc) {
|
adamc@666
|
607 if (b->back - b->front < extra) {
|
adamc@666
|
608 size_t desired = b->front - b->start + extra, next;
|
adamc@136
|
609 char *new_heap;
|
adamc@136
|
610
|
adamc@666
|
611 next = b->back - b->start;
|
adamc@317
|
612 if (next == 0)
|
adamc@317
|
613 next = 1;
|
adamc@317
|
614 for (; next < desired; next *= 2);
|
adamc@136
|
615
|
adamc@666
|
616 new_heap = realloc(b->start, next);
|
adamc@666
|
617 b->front = new_heap + (b->front - b->start);
|
adamc@666
|
618 b->back = new_heap + next;
|
adamc@136
|
619
|
adamc@667
|
620 if (new_heap != b->start) {
|
adamc@666
|
621 b->start = new_heap;
|
adamc@666
|
622 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously", desc);
|
adamc@136
|
623 }
|
adamc@136
|
624
|
adamc@666
|
625 b->start = new_heap;
|
adamc@136
|
626 }
|
adamc@136
|
627 }
|
adamc@136
|
628
|
adamc@666
|
629 static void uw_check_heap(uw_context ctx, size_t extra) {
|
adamc@667
|
630 buf_check_ctx(ctx, &ctx->heap, extra, "heap chunk");
|
adamc@666
|
631 }
|
adamc@666
|
632
|
adamc@311
|
633 void *uw_malloc(uw_context ctx, size_t len) {
|
adamc@136
|
634 void *result;
|
adamc@136
|
635
|
adamc@311
|
636 uw_check_heap(ctx, len);
|
adamc@136
|
637
|
adamc@666
|
638 result = ctx->heap.front;
|
adamc@666
|
639 ctx->heap.front += len;
|
adamc@136
|
640 return result;
|
adamc@117
|
641 }
|
adamc@117
|
642
|
adamc@323
|
643 void uw_begin_region(uw_context ctx) {
|
adamc@666
|
644 regions *r = (regions *) ctx->heap.front;
|
adamc@323
|
645
|
adamc@323
|
646 uw_check_heap(ctx, sizeof(regions));
|
adamc@323
|
647
|
adamc@666
|
648 ctx->heap.front += sizeof(regions);
|
adamc@323
|
649
|
adamc@323
|
650 r->next = ctx->regions;
|
adamc@323
|
651 ctx->regions = r;
|
adamc@323
|
652 }
|
adamc@323
|
653
|
adamc@323
|
654 void uw_end_region(uw_context ctx) {
|
adamc@323
|
655 regions *r = ctx->regions;
|
adamc@323
|
656
|
adamc@323
|
657 if (r == NULL)
|
adamc@323
|
658 uw_error(ctx, FATAL, "Region stack underflow");
|
adamc@323
|
659
|
adamc@666
|
660 ctx->heap.front = (char *) r;
|
adamc@323
|
661 ctx->regions = r->next;
|
adamc@323
|
662 }
|
adamc@323
|
663
|
adamc@324
|
664 void uw_memstats(uw_context ctx) {
|
adamc@666
|
665 printf("Headers: %d/%d\n", buf_used(&ctx->outHeaders), buf_avail(&ctx->outHeaders));
|
adamc@666
|
666 printf("Script: %d/%d\n", buf_used(&ctx->script), buf_avail(&ctx->script));
|
adamc@666
|
667 printf("Page: %d/%d\n", buf_used(&ctx->page), buf_avail(&ctx->page));
|
adamc@666
|
668 printf("Heap: %d/%d\n", buf_used(&ctx->heap), buf_avail(&ctx->heap));
|
adamc@324
|
669 }
|
adamc@324
|
670
|
adamc@311
|
671 int uw_send(uw_context ctx, int sock) {
|
adamc@666
|
672 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
|
adamc@462
|
673
|
adamc@462
|
674 if (n < 0)
|
adamc@462
|
675 return n;
|
adamc@462
|
676
|
adamc@462
|
677 n = uw_really_send(sock, "\r\n", 2);
|
adamc@462
|
678
|
adamc@462
|
679 if (n < 0)
|
adamc@462
|
680 return n;
|
adamc@462
|
681
|
adamc@666
|
682 return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start);
|
adamc@462
|
683 }
|
adamc@462
|
684
|
adamc@462
|
685 static void uw_check_headers(uw_context ctx, size_t extra) {
|
adamc@667
|
686 buf_check(&ctx->outHeaders, extra);
|
adamc@462
|
687 }
|
adamc@462
|
688
|
adamc@462
|
689 void uw_write_header(uw_context ctx, uw_Basis_string s) {
|
adamc@462
|
690 int len = strlen(s);
|
adamc@462
|
691
|
adamc@462
|
692 uw_check_headers(ctx, len + 1);
|
adamc@666
|
693 strcpy(ctx->outHeaders.front, s);
|
adamc@666
|
694 ctx->outHeaders.front += len;
|
adamc@562
|
695 }
|
adamc@562
|
696
|
adamc@562
|
697 static void uw_check_script(uw_context ctx, size_t extra) {
|
adamc@667
|
698 buf_check(&ctx->script, extra);
|
adamc@562
|
699 }
|
adamc@562
|
700
|
adamc@562
|
701 void uw_write_script(uw_context ctx, uw_Basis_string s) {
|
adamc@562
|
702 int len = strlen(s);
|
adamc@562
|
703
|
adamc@562
|
704 uw_check_script(ctx, len + 1);
|
adamc@666
|
705 strcpy(ctx->script.front, s);
|
adamc@666
|
706 ctx->script.front += len;
|
adamc@562
|
707 }
|
adamc@562
|
708
|
adamc@645
|
709 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
|
adamc@667
|
710 if (ctx->script_header[0] == 0)
|
adamc@667
|
711 return "";
|
adamc@667
|
712 else {
|
adamc@667
|
713 int pass;
|
adamc@667
|
714 client *c = uw_new_client(&pass);
|
adamc@565
|
715
|
adamc@667
|
716 char *r = uw_malloc(ctx, strlen(ctx->script_header) + 56 + 2 * INTS_MAX + buf_used(&ctx->script)
|
adamc@667
|
717 + strlen(ctx->url_prefix));
|
adamc@667
|
718 sprintf(r, "%s<script>client_id=%d;client_pass=%d;url_prefix=\"%s\";%s</script>",
|
adamc@667
|
719 ctx->script_header, (int)c->id, c->data.used.pass, ctx->url_prefix, ctx->script.start);
|
adamc@565
|
720 return r;
|
adamc@565
|
721 }
|
adamc@565
|
722 }
|
adamc@565
|
723
|
adamc@668
|
724 const char *uw_Basis_get_listener(uw_context ctx, uw_Basis_string onload) {
|
adamc@667
|
725 if (ctx->script_header[0] == 0)
|
adamc@667
|
726 return "";
|
adamc@668
|
727 else if (onload[0] == 0)
|
adamc@667
|
728 return " onload='listener()'";
|
adamc@668
|
729 else {
|
adamc@668
|
730 uw_Basis_string s = uw_malloc(ctx, strlen(onload) + 22);
|
adamc@668
|
731
|
adamc@668
|
732 sprintf(s, " onload='listener();%s'", onload);
|
adamc@668
|
733 return s;
|
adamc@668
|
734 }
|
adamc@667
|
735 }
|
adamc@667
|
736
|
adamc@574
|
737 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
738 char *r, *s2;
|
adamc@574
|
739
|
adamc@574
|
740 uw_check_heap(ctx, strlen(s) * 4 + 2);
|
adamc@574
|
741
|
adamc@666
|
742 r = s2 = ctx->heap.front;
|
adamc@574
|
743 *s2++ = '"';
|
adamc@574
|
744
|
adamc@574
|
745 for (; *s; s++) {
|
adamc@574
|
746 char c = *s;
|
adamc@574
|
747
|
adamc@574
|
748 switch (c) {
|
adamc@574
|
749 case '"':
|
adamc@574
|
750 strcpy(s2, "\\\"");
|
adamc@574
|
751 s2 += 2;
|
adamc@574
|
752 break;
|
adamc@574
|
753 case '\\':
|
adamc@574
|
754 strcpy(s2, "\\\\");
|
adamc@574
|
755 s2 += 2;
|
adamc@574
|
756 break;
|
adamc@574
|
757 default:
|
adamc@574
|
758 if (isprint(c))
|
adamc@574
|
759 *s2++ = c;
|
adamc@574
|
760 else {
|
adamc@574
|
761 sprintf(s2, "\\%3o", c);
|
adamc@574
|
762 s2 += 4;
|
adamc@574
|
763 }
|
adamc@574
|
764 }
|
adamc@574
|
765 }
|
adamc@574
|
766
|
adamc@574
|
767 strcpy(s2, "\"");
|
adamc@666
|
768 ctx->heap.front = s2 + 1;
|
adamc@574
|
769 return r;
|
adamc@574
|
770 }
|
adamc@574
|
771
|
adamc@574
|
772 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
773 char *r, *s2;
|
adamc@574
|
774
|
adamc@574
|
775 uw_check_script(ctx, strlen(s) * 4 + 2);
|
adamc@574
|
776
|
adamc@666
|
777 r = s2 = ctx->script.front;
|
adamc@574
|
778 *s2++ = '"';
|
adamc@574
|
779
|
adamc@574
|
780 for (; *s; s++) {
|
adamc@574
|
781 char c = *s;
|
adamc@574
|
782
|
adamc@574
|
783 switch (c) {
|
adamc@577
|
784 case '\'':
|
adamc@574
|
785 strcpy(s2, "\\\"");
|
adamc@574
|
786 s2 += 2;
|
adamc@574
|
787 break;
|
adamc@574
|
788 case '\\':
|
adamc@574
|
789 strcpy(s2, "\\\\");
|
adamc@574
|
790 s2 += 2;
|
adamc@574
|
791 break;
|
adamc@574
|
792 default:
|
adamc@574
|
793 if (isprint(c))
|
adamc@574
|
794 *s2++ = c;
|
adamc@574
|
795 else {
|
adamc@574
|
796 sprintf(s2, "\\%3o", c);
|
adamc@574
|
797 s2 += 4;
|
adamc@574
|
798 }
|
adamc@574
|
799 }
|
adamc@574
|
800 }
|
adamc@574
|
801
|
adamc@574
|
802 strcpy(s2, "\"");
|
adamc@666
|
803 ctx->script.front = s2 + 1;
|
adamc@574
|
804 return r;
|
adamc@574
|
805 }
|
adamc@574
|
806
|
adamc@577
|
807 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
|
adamc@577
|
808 int len;
|
adamc@577
|
809 size_t s_len = strlen(s);
|
adamc@562
|
810
|
adamc@577
|
811 uw_check_script(ctx, 12 + INTS_MAX + s_len);
|
adamc@666
|
812 sprintf(ctx->script.front, "var s%d=sc(%n", ctx->source_count, &len);
|
adamc@666
|
813 ctx->script.front += len;
|
adamc@666
|
814 strcpy(ctx->script.front, s);
|
adamc@666
|
815 ctx->script.front += s_len;
|
adamc@666
|
816 strcpy(ctx->script.front, ");");
|
adamc@666
|
817 ctx->script.front += 2;
|
adamc@562
|
818
|
adamc@565
|
819 return ctx->source_count++;
|
adamc@117
|
820 }
|
adamc@117
|
821
|
adamc@577
|
822 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
|
adamc@577
|
823 int len;
|
adamc@577
|
824 size_t s_len = strlen(s);
|
adamc@577
|
825
|
adamc@577
|
826 uw_check_script(ctx, 6 + INTS_MAX + s_len);
|
adamc@666
|
827 sprintf(ctx->script.front, "s%d.v=%n", (int)n, &len);
|
adamc@666
|
828 ctx->script.front += len;
|
adamc@666
|
829 strcpy(ctx->script.front, s);
|
adamc@666
|
830 ctx->script.front += s_len;
|
adamc@666
|
831 strcpy(ctx->script.front, ";");
|
adamc@666
|
832 ctx->script.front++;
|
adamc@577
|
833
|
adamc@577
|
834 return uw_unit_v;
|
adamc@577
|
835 }
|
adamc@577
|
836
|
adamc@311
|
837 static void uw_check(uw_context ctx, size_t extra) {
|
adamc@667
|
838 buf_check(&ctx->page, extra);
|
adamc@117
|
839 }
|
adamc@117
|
840
|
adamc@311
|
841 static void uw_writec_unsafe(uw_context ctx, char c) {
|
adamc@666
|
842 *(ctx->page.front)++ = c;
|
adamc@117
|
843 }
|
adamc@117
|
844
|
adamc@311
|
845 void uw_writec(uw_context ctx, char c) {
|
adamc@311
|
846 uw_check(ctx, 1);
|
adamc@311
|
847 uw_writec_unsafe(ctx, c);
|
adamc@117
|
848 }
|
adamc@117
|
849
|
adamc@311
|
850 static void uw_write_unsafe(uw_context ctx, const char* s) {
|
adamc@117
|
851 int len = strlen(s);
|
adamc@666
|
852 memcpy(ctx->page.front, s, len);
|
adamc@666
|
853 ctx->page.front += len;
|
adamc@117
|
854 }
|
adamc@117
|
855
|
adamc@311
|
856 void uw_write(uw_context ctx, const char* s) {
|
adamc@311
|
857 uw_check(ctx, strlen(s) + 1);
|
adamc@311
|
858 uw_write_unsafe(ctx, s);
|
adamc@666
|
859 *ctx->page.front = 0;
|
adamc@102
|
860 }
|
adamc@106
|
861
|
adamc@311
|
862 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@136
|
863 char *result;
|
adamc@136
|
864 int len;
|
adamc@311
|
865 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
866 result = ctx->heap.front;
|
adamc@276
|
867 sprintf(result, "%lld%n", n, &len);
|
adamc@666
|
868 ctx->heap.front += len+1;
|
adamc@136
|
869 return result;
|
adamc@106
|
870 }
|
adamc@106
|
871
|
adamc@311
|
872 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@136
|
873 char *result;
|
adamc@136
|
874 int len;
|
adamc@311
|
875 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
876 result = ctx->heap.front;
|
adamc@136
|
877 sprintf(result, "%g%n", n, &len);
|
adamc@666
|
878 ctx->heap.front += len+1;
|
adamc@136
|
879 return result;
|
adamc@106
|
880 }
|
adamc@106
|
881
|
adamc@311
|
882 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@136
|
883 int len = strlen(s);
|
adamc@136
|
884 char *result, *p;
|
adamc@311
|
885 uw_check_heap(ctx, len * 6 + 1);
|
adamc@136
|
886
|
adamc@666
|
887 result = p = ctx->heap.front;
|
adamc@136
|
888
|
adamc@136
|
889 for (; *s; s++) {
|
adamc@136
|
890 char c = *s;
|
adamc@136
|
891
|
adamc@136
|
892 if (c == '"') {
|
adamc@136
|
893 strcpy(p, """);
|
adamc@136
|
894 p += 6;
|
adamc@136
|
895 } else if (c == '&') {
|
adamc@136
|
896 strcpy(p, "&");
|
adamc@136
|
897 p += 5;
|
adamc@136
|
898 }
|
adamc@136
|
899 else if (isprint(c))
|
adamc@136
|
900 *p++ = c;
|
adamc@136
|
901 else {
|
adamc@136
|
902 int len2;
|
adamc@136
|
903 sprintf(p, "&#%d;%n", c, &len2);
|
adamc@136
|
904 p += len2;
|
adamc@136
|
905 }
|
adamc@136
|
906 }
|
adamc@136
|
907
|
adamc@137
|
908 *p++ = 0;
|
adamc@666
|
909 ctx->heap.front = p;
|
adamc@136
|
910 return result;
|
adamc@106
|
911 }
|
adamc@106
|
912
|
adamc@311
|
913 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@117
|
914 int len;
|
adamc@117
|
915
|
adamc@666
|
916 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
917 ctx->page.front += len;
|
adamc@106
|
918 }
|
adamc@106
|
919
|
adamc@428
|
920 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
921 uw_check(ctx, INTS_MAX);
|
adamc@311
|
922 uw_Basis_attrifyInt_w_unsafe(ctx, n);
|
adamc@428
|
923
|
adamc@428
|
924 return uw_unit_v;
|
adamc@106
|
925 }
|
adamc@106
|
926
|
adamc@428
|
927 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@117
|
928 int len;
|
adamc@117
|
929
|
adamc@311
|
930 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
931 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
932 ctx->page.front += len;
|
adamc@428
|
933
|
adamc@428
|
934 return uw_unit_v;
|
adamc@117
|
935 }
|
adamc@117
|
936
|
adamc@428
|
937 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
938 uw_check(ctx, strlen(s) * 6);
|
adamc@117
|
939
|
adamc@106
|
940 for (; *s; s++) {
|
adamc@106
|
941 char c = *s;
|
adamc@106
|
942
|
adamc@106
|
943 if (c == '"')
|
adamc@311
|
944 uw_write_unsafe(ctx, """);
|
adamc@136
|
945 else if (c == '&')
|
adamc@311
|
946 uw_write_unsafe(ctx, "&");
|
adamc@106
|
947 else if (isprint(c))
|
adamc@311
|
948 uw_writec_unsafe(ctx, c);
|
adamc@106
|
949 else {
|
adamc@311
|
950 uw_write_unsafe(ctx, "&#");
|
adamc@311
|
951 uw_Basis_attrifyInt_w_unsafe(ctx, c);
|
adamc@311
|
952 uw_writec_unsafe(ctx, ';');
|
adamc@106
|
953 }
|
adamc@106
|
954 }
|
adamc@428
|
955
|
adamc@428
|
956 return uw_unit_v;
|
adamc@106
|
957 }
|
adamc@120
|
958
|
adamc@120
|
959
|
adamc@311
|
960 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@137
|
961 int len;
|
adamc@137
|
962 char *r;
|
adamc@137
|
963
|
adamc@311
|
964 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
965 r = ctx->heap.front;
|
adamc@276
|
966 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
967 ctx->heap.front += len+1;
|
adamc@137
|
968 return r;
|
adamc@120
|
969 }
|
adamc@120
|
970
|
adamc@311
|
971 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@137
|
972 int len;
|
adamc@137
|
973 char *r;
|
adamc@137
|
974
|
adamc@311
|
975 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
976 r = ctx->heap.front;
|
adamc@137
|
977 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
978 ctx->heap.front += len+1;
|
adamc@137
|
979 return r;
|
adamc@120
|
980 }
|
adamc@120
|
981
|
adamc@311
|
982 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
983 char *r, *p;
|
adamc@137
|
984
|
adamc@311
|
985 uw_check_heap(ctx, strlen(s) * 3 + 1);
|
adamc@137
|
986
|
adamc@666
|
987 for (r = p = ctx->heap.front; *s; s++) {
|
adamc@137
|
988 char c = *s;
|
adamc@137
|
989
|
adamc@137
|
990 if (c == ' ')
|
adamc@137
|
991 *p++ = '+';
|
adamc@137
|
992 else if (isalnum(c))
|
adamc@137
|
993 *p++ = c;
|
adamc@137
|
994 else {
|
adamc@137
|
995 sprintf(p, "%%%02X", c);
|
adamc@137
|
996 p += 3;
|
adamc@137
|
997 }
|
adamc@137
|
998 }
|
adamc@137
|
999
|
adamc@137
|
1000 *p++ = 0;
|
adamc@666
|
1001 ctx->heap.front = p;
|
adamc@137
|
1002 return r;
|
adamc@120
|
1003 }
|
adamc@120
|
1004
|
adamc@311
|
1005 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1006 if (b == uw_Basis_False)
|
adamc@186
|
1007 return "0";
|
adamc@186
|
1008 else
|
adamc@186
|
1009 return "1";
|
adamc@186
|
1010 }
|
adamc@186
|
1011
|
adamc@311
|
1012 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@120
|
1013 int len;
|
adamc@120
|
1014
|
adamc@666
|
1015 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1016 ctx->page.front += len;
|
adamc@120
|
1017 }
|
adamc@120
|
1018
|
adamc@428
|
1019 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
1020 uw_check(ctx, INTS_MAX);
|
adamc@311
|
1021 uw_Basis_urlifyInt_w_unsafe(ctx, n);
|
adamc@428
|
1022
|
adamc@428
|
1023 return uw_unit_v;
|
adamc@120
|
1024 }
|
adamc@120
|
1025
|
adamc@428
|
1026 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@120
|
1027 int len;
|
adamc@120
|
1028
|
adamc@311
|
1029 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1030 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1031 ctx->page.front += len;
|
adamc@428
|
1032
|
adamc@428
|
1033 return uw_unit_v;
|
adamc@120
|
1034 }
|
adamc@120
|
1035
|
adamc@488
|
1036 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@488
|
1037 return uw_Basis_urlifyInt(ctx, t);
|
adamc@488
|
1038 }
|
adamc@488
|
1039
|
adamc@428
|
1040 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1041 uw_check(ctx, strlen(s) * 3);
|
adamc@120
|
1042
|
adamc@120
|
1043 for (; *s; s++) {
|
adamc@120
|
1044 char c = *s;
|
adamc@120
|
1045
|
adamc@120
|
1046 if (c == ' ')
|
adamc@311
|
1047 uw_writec_unsafe(ctx, '+');
|
adamc@120
|
1048 else if (isalnum(c))
|
adamc@311
|
1049 uw_writec_unsafe(ctx, c);
|
adamc@120
|
1050 else {
|
adamc@666
|
1051 sprintf(ctx->page.front, "%%%02X", c);
|
adamc@666
|
1052 ctx->page.front += 3;
|
adamc@120
|
1053 }
|
adamc@120
|
1054 }
|
adamc@428
|
1055
|
adamc@428
|
1056 return uw_unit_v;
|
adamc@120
|
1057 }
|
adamc@120
|
1058
|
adamc@428
|
1059 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1060 if (b == uw_Basis_False)
|
adamc@311
|
1061 uw_writec(ctx, '0');
|
adamc@186
|
1062 else
|
adamc@311
|
1063 uw_writec(ctx, '1');
|
adamc@428
|
1064
|
adamc@428
|
1065 return uw_unit_v;
|
adamc@186
|
1066 }
|
adamc@186
|
1067
|
adamc@120
|
1068
|
adamc@311
|
1069 static char *uw_unurlify_advance(char *s) {
|
adamc@144
|
1070 char *new_s = strchr(s, '/');
|
adamc@120
|
1071
|
adamc@120
|
1072 if (new_s)
|
adamc@120
|
1073 *new_s++ = 0;
|
adamc@120
|
1074 else
|
adamc@144
|
1075 new_s = strchr(s, 0);
|
adamc@144
|
1076
|
adamc@144
|
1077 return new_s;
|
adamc@144
|
1078 }
|
adamc@144
|
1079
|
adamc@311
|
1080 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
|
adamc@311
|
1081 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1082 uw_Basis_int r;
|
adamc@120
|
1083
|
adamc@276
|
1084 r = atoll(*s);
|
adamc@120
|
1085 *s = new_s;
|
adamc@120
|
1086 return r;
|
adamc@120
|
1087 }
|
adamc@120
|
1088
|
adamc@668
|
1089 uw_Basis_channel uw_Basis_unurlifyChannel(uw_context ctx, char **s) {
|
adamc@668
|
1090 return uw_Basis_unurlifyInt(ctx, s);
|
adamc@668
|
1091 }
|
adamc@668
|
1092
|
adamc@311
|
1093 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
|
adamc@311
|
1094 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1095 uw_Basis_float r;
|
adamc@120
|
1096
|
adamc@120
|
1097 r = atof(*s);
|
adamc@120
|
1098 *s = new_s;
|
adamc@120
|
1099 return r;
|
adamc@120
|
1100 }
|
adamc@120
|
1101
|
adamc@488
|
1102 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
|
adamc@488
|
1103 return uw_Basis_unurlifyInt(ctx, s);
|
adamc@488
|
1104 }
|
adamc@488
|
1105
|
adamc@311
|
1106 static uw_Basis_string uw_unurlifyString_to(uw_context ctx, char *r, char *s) {
|
adamc@144
|
1107 char *s1, *s2;
|
adamc@144
|
1108 int n;
|
adamc@136
|
1109
|
adamc@144
|
1110 for (s1 = r, s2 = s; *s2; ++s1, ++s2) {
|
adamc@136
|
1111 char c = *s2;
|
adamc@136
|
1112
|
adamc@136
|
1113 switch (c) {
|
adamc@136
|
1114 case '+':
|
adamc@136
|
1115 *s1 = ' ';
|
adamc@136
|
1116 break;
|
adamc@136
|
1117 case '%':
|
adamc@169
|
1118 if (s2[1] == 0)
|
adamc@311
|
1119 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
|
adamc@169
|
1120 if (s2[2] == 0)
|
adamc@311
|
1121 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
|
adamc@169
|
1122 if (sscanf(s2+1, "%02X", &n) != 1)
|
adamc@311
|
1123 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
|
adamc@136
|
1124 *s1 = n;
|
adamc@136
|
1125 s2 += 2;
|
adamc@136
|
1126 break;
|
adamc@136
|
1127 default:
|
adamc@136
|
1128 *s1 = c;
|
adamc@136
|
1129 }
|
adamc@136
|
1130 }
|
adamc@136
|
1131 *s1++ = 0;
|
adamc@144
|
1132 return s1;
|
adamc@144
|
1133 }
|
adamc@144
|
1134
|
adamc@311
|
1135 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
|
adamc@311
|
1136 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1137 uw_Basis_bool r;
|
adamc@186
|
1138
|
adamc@186
|
1139 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
|
adamc@311
|
1140 r = uw_Basis_False;
|
adamc@186
|
1141 else
|
adamc@311
|
1142 r = uw_Basis_True;
|
adamc@186
|
1143
|
adamc@186
|
1144 *s = new_s;
|
adamc@186
|
1145 return r;
|
adamc@186
|
1146 }
|
adamc@186
|
1147
|
adamc@311
|
1148 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
|
adamc@311
|
1149 char *new_s = uw_unurlify_advance(*s);
|
adamc@144
|
1150 char *r, *s1, *s2;
|
adamc@144
|
1151 int len, n;
|
adamc@144
|
1152
|
adamc@200
|
1153 len = strlen(*s);
|
adamc@311
|
1154 uw_check_heap(ctx, len + 1);
|
adamc@144
|
1155
|
adamc@666
|
1156 r = ctx->heap.front;
|
adamc@666
|
1157 ctx->heap.front = uw_unurlifyString_to(ctx, ctx->heap.front, *s);
|
adamc@136
|
1158 *s = new_s;
|
adamc@136
|
1159 return r;
|
adamc@120
|
1160 }
|
adamc@135
|
1161
|
adamc@135
|
1162
|
adamc@311
|
1163 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
1164 int len;
|
adamc@286
|
1165 char *r;
|
adamc@286
|
1166
|
adamc@311
|
1167 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1168 r = ctx->heap.front;
|
adamc@286
|
1169 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1170 ctx->heap.front += len+1;
|
adamc@286
|
1171 return r;
|
adamc@286
|
1172 }
|
adamc@286
|
1173
|
adamc@428
|
1174 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
1175 int len;
|
adamc@286
|
1176
|
adamc@311
|
1177 uw_check(ctx, INTS_MAX);
|
adamc@666
|
1178 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1179 ctx->page.front += len;
|
adamc@428
|
1180
|
adamc@428
|
1181 return uw_unit_v;
|
adamc@286
|
1182 }
|
adamc@286
|
1183
|
adamc@668
|
1184 char *uw_Basis_htmlifyChannel(uw_context ctx, uw_Basis_channel ch) {
|
adamc@668
|
1185 return uw_Basis_htmlifyInt(ctx, ch);
|
adamc@668
|
1186 }
|
adamc@668
|
1187
|
adamc@311
|
1188 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
1189 int len;
|
adamc@286
|
1190 char *r;
|
adamc@286
|
1191
|
adamc@311
|
1192 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1193 r = ctx->heap.front;
|
adamc@286
|
1194 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1195 ctx->heap.front += len+1;
|
adamc@286
|
1196 return r;
|
adamc@286
|
1197 }
|
adamc@286
|
1198
|
adamc@428
|
1199 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
1200 int len;
|
adamc@286
|
1201
|
adamc@311
|
1202 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1203 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1204 ctx->page.front += len;
|
adamc@428
|
1205
|
adamc@428
|
1206 return uw_unit_v;
|
adamc@286
|
1207 }
|
adamc@286
|
1208
|
adamc@311
|
1209 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
1210 char *r, *s2;
|
adamc@137
|
1211
|
adamc@311
|
1212 uw_check_heap(ctx, strlen(s) * 5 + 1);
|
adamc@137
|
1213
|
adamc@666
|
1214 for (r = s2 = ctx->heap.front; *s; s++) {
|
adamc@137
|
1215 char c = *s;
|
adamc@137
|
1216
|
adamc@137
|
1217 switch (c) {
|
adamc@137
|
1218 case '<':
|
adamc@137
|
1219 strcpy(s2, "<");
|
adamc@137
|
1220 s2 += 4;
|
adamc@137
|
1221 break;
|
adamc@137
|
1222 case '&':
|
adamc@137
|
1223 strcpy(s2, "&");
|
adamc@137
|
1224 s2 += 5;
|
adamc@137
|
1225 break;
|
adamc@137
|
1226 default:
|
adamc@137
|
1227 if (isprint(c))
|
adamc@137
|
1228 *s2++ = c;
|
adamc@137
|
1229 else {
|
adamc@137
|
1230 int len2;
|
adamc@137
|
1231 sprintf(s2, "&#%d;%n", c, &len2);
|
adamc@137
|
1232 s2 += len2;
|
adamc@137
|
1233 }
|
adamc@137
|
1234 }
|
adamc@137
|
1235 }
|
adamc@137
|
1236
|
adamc@137
|
1237 *s2++ = 0;
|
adamc@666
|
1238 ctx->heap.front = s2;
|
adamc@137
|
1239 return r;
|
adamc@135
|
1240 }
|
adamc@135
|
1241
|
adamc@428
|
1242 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@321
|
1243 uw_check(ctx, strlen(s) * 6);
|
adamc@135
|
1244
|
adamc@135
|
1245 for (; *s; s++) {
|
adamc@135
|
1246 char c = *s;
|
adamc@135
|
1247
|
adamc@135
|
1248 switch (c) {
|
adamc@135
|
1249 case '<':
|
adamc@311
|
1250 uw_write_unsafe(ctx, "<");
|
adamc@135
|
1251 break;
|
adamc@135
|
1252 case '&':
|
adamc@311
|
1253 uw_write_unsafe(ctx, "&");
|
adamc@135
|
1254 break;
|
adamc@135
|
1255 default:
|
adamc@135
|
1256 if (isprint(c))
|
adamc@311
|
1257 uw_writec_unsafe(ctx, c);
|
adamc@135
|
1258 else {
|
adamc@311
|
1259 uw_write_unsafe(ctx, "&#");
|
adamc@311
|
1260 uw_Basis_attrifyInt_w_unsafe(ctx, c);
|
adamc@311
|
1261 uw_writec_unsafe(ctx, ';');
|
adamc@135
|
1262 }
|
adamc@135
|
1263 }
|
adamc@135
|
1264 }
|
adamc@428
|
1265
|
adamc@428
|
1266 return uw_unit_v;
|
adamc@135
|
1267 }
|
adamc@180
|
1268
|
adamc@311
|
1269 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1270 if (b == uw_Basis_False)
|
adamc@286
|
1271 return "False";
|
adamc@286
|
1272 else
|
adamc@286
|
1273 return "True";
|
adamc@286
|
1274 }
|
adamc@286
|
1275
|
adamc@428
|
1276 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1277 if (b == uw_Basis_False) {
|
adamc@311
|
1278 uw_check(ctx, 6);
|
adamc@666
|
1279 strcpy(ctx->page.front, "False");
|
adamc@666
|
1280 ctx->page.front += 5;
|
adamc@286
|
1281 } else {
|
adamc@311
|
1282 uw_check(ctx, 5);
|
adamc@666
|
1283 strcpy(ctx->page.front, "True");
|
adamc@666
|
1284 ctx->page.front += 4;
|
adamc@286
|
1285 }
|
adamc@428
|
1286
|
adamc@428
|
1287 return uw_unit_v;
|
adamc@286
|
1288 }
|
adamc@286
|
1289
|
adamc@436
|
1290 #define TIME_FMT "%x %X"
|
adamc@438
|
1291 #define TIME_FMT_PG "%Y-%m-%d %T"
|
adamc@436
|
1292
|
adamc@436
|
1293 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
1294 size_t len;
|
adamc@436
|
1295 char *r;
|
adamc@436
|
1296 struct tm stm;
|
adamc@436
|
1297
|
adamc@436
|
1298 if (localtime_r(&t, &stm)) {
|
adamc@436
|
1299 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
1300 r = ctx->heap.front;
|
adamc@436
|
1301 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1302 ctx->heap.front += len+1;
|
adamc@436
|
1303 return r;
|
adamc@436
|
1304 } else
|
adamc@436
|
1305 return "<i>Invalid time</i>";
|
adamc@436
|
1306 }
|
adamc@436
|
1307
|
adamc@436
|
1308 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
1309 size_t len;
|
adamc@436
|
1310 char *r;
|
adamc@436
|
1311 struct tm stm;
|
adamc@436
|
1312
|
adamc@436
|
1313 if (localtime_r(&t, &stm)) {
|
adamc@436
|
1314 uw_check(ctx, TIMES_MAX);
|
adamc@666
|
1315 r = ctx->page.front;
|
adamc@436
|
1316 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1317 ctx->page.front += len;
|
adamc@436
|
1318 } else {
|
adamc@436
|
1319 uw_check(ctx, 20);
|
adamc@666
|
1320 strcpy(ctx->page.front, "<i>Invalid time</i>");
|
adamc@666
|
1321 ctx->page.front += 19;
|
adamc@436
|
1322 }
|
adamc@436
|
1323
|
adamc@436
|
1324 return uw_unit_v;
|
adamc@436
|
1325 }
|
adamc@436
|
1326
|
adamc@311
|
1327 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
|
adamc@180
|
1328 int len = strlen(s1) + strlen(s2) + 1;
|
adamc@180
|
1329 char *s;
|
adamc@180
|
1330
|
adamc@311
|
1331 uw_check_heap(ctx, len);
|
adamc@180
|
1332
|
adamc@666
|
1333 s = ctx->heap.front;
|
adamc@180
|
1334
|
adamc@180
|
1335 strcpy(s, s1);
|
adamc@180
|
1336 strcat(s, s2);
|
adamc@666
|
1337 ctx->heap.front += len;
|
adamc@180
|
1338
|
adamc@180
|
1339 return s;
|
adamc@180
|
1340 }
|
adamc@278
|
1341
|
adamc@311
|
1342 uw_Basis_string uw_Basis_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@278
|
1343 int len = strlen(s1) + 1;
|
adamc@278
|
1344 char *s;
|
adamc@278
|
1345
|
adamc@311
|
1346 uw_check_heap(ctx, len);
|
adamc@278
|
1347
|
adamc@666
|
1348 s = ctx->heap.front;
|
adamc@278
|
1349
|
adamc@278
|
1350 strcpy(s, s1);
|
adamc@666
|
1351 ctx->heap.front += len;
|
adamc@278
|
1352
|
adamc@278
|
1353 return s;
|
adamc@278
|
1354 }
|
adamc@280
|
1355
|
adamc@493
|
1356 uw_Basis_string uw_Basis_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@493
|
1357 if (s1)
|
adamc@493
|
1358 return uw_Basis_strdup(ctx, s1);
|
adamc@493
|
1359 else
|
adamc@493
|
1360 return NULL;
|
adamc@493
|
1361 }
|
adamc@493
|
1362
|
adamc@280
|
1363
|
adamc@311
|
1364 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@281
|
1365 int len;
|
adamc@281
|
1366 char *r;
|
adamc@281
|
1367
|
adamc@311
|
1368 uw_check_heap(ctx, INTS_MAX + 6);
|
adamc@666
|
1369 r = ctx->heap.front;
|
adamc@281
|
1370 sprintf(r, "%lld::int8%n", n, &len);
|
adamc@666
|
1371 ctx->heap.front += len+1;
|
adamc@281
|
1372 return r;
|
adamc@281
|
1373 }
|
adamc@281
|
1374
|
adamc@467
|
1375 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
|
adamc@467
|
1376 if (n == NULL)
|
adamc@467
|
1377 return "NULL";
|
adamc@467
|
1378 else
|
adamc@467
|
1379 return uw_Basis_sqlifyInt(ctx, *n);
|
adamc@467
|
1380 }
|
adamc@467
|
1381
|
adamc@311
|
1382 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@281
|
1383 int len;
|
adamc@281
|
1384 char *r;
|
adamc@281
|
1385
|
adamc@311
|
1386 uw_check_heap(ctx, FLOATS_MAX + 8);
|
adamc@666
|
1387 r = ctx->heap.front;
|
adamc@281
|
1388 sprintf(r, "%g::float8%n", n, &len);
|
adamc@666
|
1389 ctx->heap.front += len+1;
|
adamc@281
|
1390 return r;
|
adamc@281
|
1391 }
|
adamc@281
|
1392
|
adamc@467
|
1393 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
|
adamc@467
|
1394 if (n == NULL)
|
adamc@467
|
1395 return "NULL";
|
adamc@467
|
1396 else
|
adamc@467
|
1397 return uw_Basis_sqlifyFloat(ctx, *n);
|
adamc@467
|
1398 }
|
adamc@467
|
1399
|
adamc@281
|
1400
|
adamc@311
|
1401 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@280
|
1402 char *r, *s2;
|
adamc@280
|
1403
|
adamc@311
|
1404 uw_check_heap(ctx, strlen(s) * 2 + 10);
|
adamc@280
|
1405
|
adamc@666
|
1406 r = s2 = ctx->heap.front;
|
adamc@280
|
1407 *s2++ = 'E';
|
adamc@280
|
1408 *s2++ = '\'';
|
adamc@280
|
1409
|
adamc@280
|
1410 for (; *s; s++) {
|
adamc@280
|
1411 char c = *s;
|
adamc@280
|
1412
|
adamc@280
|
1413 switch (c) {
|
adamc@280
|
1414 case '\'':
|
adamc@280
|
1415 strcpy(s2, "\\'");
|
adamc@280
|
1416 s2 += 2;
|
adamc@280
|
1417 break;
|
adamc@280
|
1418 case '\\':
|
adamc@280
|
1419 strcpy(s2, "\\\\");
|
adamc@280
|
1420 s2 += 2;
|
adamc@280
|
1421 break;
|
adamc@280
|
1422 default:
|
adamc@280
|
1423 if (isprint(c))
|
adamc@280
|
1424 *s2++ = c;
|
adamc@280
|
1425 else {
|
adamc@280
|
1426 sprintf(s2, "\\%3o", c);
|
adamc@280
|
1427 s2 += 4;
|
adamc@280
|
1428 }
|
adamc@280
|
1429 }
|
adamc@280
|
1430 }
|
adamc@280
|
1431
|
adamc@281
|
1432 strcpy(s2, "'::text");
|
adamc@666
|
1433 ctx->heap.front = s2 + 8;
|
adamc@280
|
1434 return r;
|
adamc@280
|
1435 }
|
adamc@281
|
1436
|
adamc@467
|
1437 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
|
adamc@467
|
1438 if (s == NULL)
|
adamc@467
|
1439 return "NULL";
|
adamc@467
|
1440 else
|
adamc@467
|
1441 return uw_Basis_sqlifyString(ctx, s);
|
adamc@467
|
1442 }
|
adamc@467
|
1443
|
adamc@311
|
1444 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1445 if (b == uw_Basis_False)
|
adamc@281
|
1446 return "FALSE";
|
adamc@281
|
1447 else
|
adamc@281
|
1448 return "TRUE";
|
adamc@281
|
1449 }
|
adamc@282
|
1450
|
adamc@467
|
1451 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
|
adamc@467
|
1452 if (b == NULL)
|
adamc@467
|
1453 return "NULL";
|
adamc@467
|
1454 else
|
adamc@467
|
1455 return uw_Basis_sqlifyBool(ctx, *b);
|
adamc@467
|
1456 }
|
adamc@467
|
1457
|
adamc@439
|
1458 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@439
|
1459 size_t len;
|
adamc@439
|
1460 char *r;
|
adamc@439
|
1461 struct tm stm;
|
adamc@439
|
1462
|
adamc@439
|
1463 if (localtime_r(&t, &stm)) {
|
adamc@439
|
1464 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
1465 r = ctx->heap.front;
|
adamc@439
|
1466 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1467 ctx->heap.front += len+1;
|
adamc@439
|
1468 return r;
|
adamc@439
|
1469 } else
|
adamc@439
|
1470 return "<Invalid time>";
|
adamc@439
|
1471 }
|
adamc@439
|
1472
|
adamc@467
|
1473 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
|
adamc@467
|
1474 if (t == NULL)
|
adamc@467
|
1475 return "NULL";
|
adamc@467
|
1476 else
|
adamc@467
|
1477 return uw_Basis_sqlifyTime(ctx, *t);
|
adamc@467
|
1478 }
|
adamc@467
|
1479
|
adamc@311
|
1480 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
|
adamc@311
|
1481 static uw_Basis_int true = 1;
|
adamc@311
|
1482 static uw_Basis_int false = 0;
|
adamc@282
|
1483
|
adamc@311
|
1484 if (b == uw_Basis_False)
|
adamc@282
|
1485 return (char *)&false;
|
adamc@282
|
1486 else
|
adamc@282
|
1487 return (char *)&true;
|
adamc@282
|
1488 }
|
adamc@284
|
1489
|
adamc@311
|
1490 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
|
adamc@284
|
1491 int len;
|
adamc@284
|
1492 char *r;
|
adamc@284
|
1493
|
adamc@311
|
1494 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1495 r = ctx->heap.front;
|
adamc@284
|
1496 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1497 ctx->heap.front += len+1;
|
adamc@284
|
1498 return r;
|
adamc@284
|
1499 }
|
adamc@285
|
1500
|
adamc@311
|
1501 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
|
adamc@285
|
1502 int len;
|
adamc@285
|
1503 char *r;
|
adamc@285
|
1504
|
adamc@311
|
1505 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1506 r = ctx->heap.front;
|
adamc@285
|
1507 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1508 ctx->heap.front += len+1;
|
adamc@285
|
1509 return r;
|
adamc@285
|
1510 }
|
adamc@285
|
1511
|
adamc@311
|
1512 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1513 if (b == uw_Basis_False)
|
adamc@285
|
1514 return "False";
|
adamc@285
|
1515 else
|
adamc@285
|
1516 return "True";
|
adamc@285
|
1517 }
|
adamc@288
|
1518
|
adamc@436
|
1519 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
1520 size_t len;
|
adamc@436
|
1521 char *r;
|
adamc@436
|
1522 struct tm stm;
|
adamc@436
|
1523
|
adamc@436
|
1524 if (localtime_r(&t, &stm)) {
|
adamc@436
|
1525 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
1526 r = ctx->heap.front;
|
adamc@436
|
1527 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1528 ctx->heap.front += len+1;
|
adamc@436
|
1529 return r;
|
adamc@436
|
1530 } else
|
adamc@436
|
1531 return "<Invalid time>";
|
adamc@436
|
1532 }
|
adamc@288
|
1533
|
adamc@311
|
1534 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
|
adamc@288
|
1535 char *endptr;
|
adamc@311
|
1536 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@288
|
1537
|
adamc@288
|
1538 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
1539 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adamc@288
|
1540 *r = n;
|
adamc@288
|
1541 return r;
|
adamc@288
|
1542 } else
|
adamc@288
|
1543 return NULL;
|
adamc@288
|
1544 }
|
adamc@289
|
1545
|
adamc@311
|
1546 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
|
adamc@289
|
1547 char *endptr;
|
adamc@311
|
1548 uw_Basis_float n = strtod(s, &endptr);
|
adamc@289
|
1549
|
adamc@289
|
1550 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
1551 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
|
adamc@289
|
1552 *r = n;
|
adamc@289
|
1553 return r;
|
adamc@289
|
1554 } else
|
adamc@289
|
1555 return NULL;
|
adamc@289
|
1556 }
|
adamc@289
|
1557
|
adamc@311
|
1558 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1559 static uw_Basis_bool true = uw_Basis_True;
|
adamc@311
|
1560 static uw_Basis_bool false = uw_Basis_False;
|
adamc@289
|
1561
|
adamc@289
|
1562 if (!strcasecmp (s, "True"))
|
adamc@289
|
1563 return &true;
|
adamc@289
|
1564 else if (!strcasecmp (s, "False"))
|
adamc@289
|
1565 return &false;
|
adamc@289
|
1566 else
|
adamc@289
|
1567 return NULL;
|
adamc@289
|
1568 }
|
adamc@292
|
1569
|
adamc@436
|
1570 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
1571 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
1572 struct tm stm;
|
adamc@436
|
1573
|
adamc@439
|
1574 if (dot) {
|
adamc@439
|
1575 *dot = 0;
|
adamc@439
|
1576 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
1577 *dot = '.';
|
adamc@439
|
1578 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1579 *r = mktime(&stm);
|
adamc@439
|
1580 return r;
|
adamc@439
|
1581 }
|
adamc@439
|
1582 else {
|
adamc@439
|
1583 *dot = '.';
|
adamc@439
|
1584 return NULL;
|
adamc@439
|
1585 }
|
adamc@436
|
1586 }
|
adamc@439
|
1587 else {
|
adamc@439
|
1588 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
1589 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1590 *r = mktime(&stm);
|
adamc@439
|
1591 return r;
|
adamc@439
|
1592 }
|
adamc@439
|
1593 else if (strptime(s, TIME_FMT, &stm) == end) {
|
adamc@439
|
1594 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1595 *r = mktime(&stm);
|
adamc@439
|
1596 return r;
|
adamc@439
|
1597 }
|
adamc@439
|
1598 else
|
adamc@439
|
1599 return NULL;
|
adamc@439
|
1600 }
|
adamc@436
|
1601 }
|
adamc@436
|
1602
|
adamc@311
|
1603 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
|
adamc@292
|
1604 char *endptr;
|
adamc@311
|
1605 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@292
|
1606
|
adamc@292
|
1607 if (*s != '\0' && *endptr == '\0')
|
adamc@292
|
1608 return n;
|
adamc@292
|
1609 else
|
adamc@311
|
1610 uw_error(ctx, FATAL, "Can't parse int: %s", s);
|
adamc@292
|
1611 }
|
adamc@293
|
1612
|
adamc@311
|
1613 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
|
adamc@293
|
1614 char *endptr;
|
adamc@311
|
1615 uw_Basis_float n = strtod(s, &endptr);
|
adamc@293
|
1616
|
adamc@293
|
1617 if (*s != '\0' && *endptr == '\0')
|
adamc@293
|
1618 return n;
|
adamc@293
|
1619 else
|
adamc@311
|
1620 uw_error(ctx, FATAL, "Can't parse float: %s", s);
|
adamc@293
|
1621 }
|
adamc@293
|
1622
|
adamc@311
|
1623 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
|
adamc@296
|
1624 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
|
adamc@311
|
1625 return uw_Basis_True;
|
adamc@296
|
1626 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
|
adamc@311
|
1627 return uw_Basis_False;
|
adamc@293
|
1628 else
|
adamc@311
|
1629 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
|
adamc@293
|
1630 }
|
adamc@436
|
1631
|
adamc@436
|
1632 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
1633 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
1634 struct tm stm = {};
|
adamc@436
|
1635
|
adamc@438
|
1636 if (dot) {
|
adamc@438
|
1637 *dot = 0;
|
adamc@438
|
1638 if (strptime(s, TIME_FMT_PG, &stm)) {
|
adamc@438
|
1639 *dot = '.';
|
adamc@438
|
1640 return mktime(&stm);
|
adamc@438
|
1641 }
|
adamc@438
|
1642 else {
|
adamc@438
|
1643 *dot = '.';
|
adamc@438
|
1644 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
1645 }
|
adamc@438
|
1646 }
|
adamc@438
|
1647 else {
|
adamc@439
|
1648 if (strptime(s, TIME_FMT_PG, &stm) == end)
|
adamc@439
|
1649 return mktime(&stm);
|
adamc@439
|
1650 else if (strptime(s, TIME_FMT, &stm) == end)
|
adamc@438
|
1651 return mktime(&stm);
|
adamc@438
|
1652 else
|
adamc@438
|
1653 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
1654 }
|
adamc@436
|
1655 }
|
adamc@457
|
1656
|
adamc@457
|
1657 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
|
adamc@457
|
1658 int len = strlen(h);
|
adamc@457
|
1659 char *s = ctx->headers, *p;
|
adamc@457
|
1660
|
adamc@457
|
1661 while (p = strchr(s, ':')) {
|
adamc@457
|
1662 if (p - s == len && !strncasecmp(s, h, len)) {
|
adamc@458
|
1663 return p + 2;
|
adamc@457
|
1664 } else {
|
adamc@458
|
1665 if ((s = strchr(p, 0)) && s < ctx->headers_end)
|
adamc@458
|
1666 s += 2;
|
adamc@457
|
1667 else
|
adamc@457
|
1668 return NULL;
|
adamc@457
|
1669 }
|
adamc@457
|
1670 }
|
adamc@463
|
1671 }
|
adamc@458
|
1672
|
adamc@463
|
1673 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
|
adamc@463
|
1674 int len = strlen(c);
|
adamc@666
|
1675 char *s = ctx->headers, *p = ctx->outHeaders.start;
|
adamc@474
|
1676
|
adamc@474
|
1677 while (p = strstr(p, "\nSet-Cookie: ")) {
|
adamc@474
|
1678 char *p2;
|
adamc@474
|
1679 p += 13;
|
adamc@474
|
1680 p2 = strchr(p, '=');
|
adamc@474
|
1681
|
adamc@474
|
1682 if (p2) {
|
adamc@474
|
1683 size_t sz = strcspn(p2+1, ";\r\n");
|
adamc@474
|
1684
|
adamc@474
|
1685 if (!strncasecmp(p, c, p2 - p)) {
|
adamc@474
|
1686 char *ret = uw_malloc(ctx, sz + 1);
|
adamc@474
|
1687 memcpy(ret, p2+1, sz);
|
adamc@474
|
1688 ret[sz] = 0;
|
adamc@474
|
1689 return ret;
|
adamc@474
|
1690 }
|
adamc@474
|
1691 }
|
adamc@474
|
1692 }
|
adamc@463
|
1693
|
adamc@463
|
1694 while (p = strchr(s, ':')) {
|
adamc@476
|
1695 if (!strncasecmp(s, "Cookie: ", 8)) {
|
adamc@476
|
1696 p += 2;
|
adamc@476
|
1697 while (1) {
|
adamc@476
|
1698 if (!strncmp(p, c, len)
|
adamc@476
|
1699 && p + len < ctx->headers_end && p[len] == '=')
|
adamc@476
|
1700 return p + 1 + len;
|
adamc@476
|
1701 else if (p = strchr(p, ';'))
|
adamc@476
|
1702 p += 2;
|
adamc@476
|
1703 else if ((s = strchr(s, 0)) && s < ctx->headers_end) {
|
adamc@476
|
1704 s += 2;
|
adamc@476
|
1705 break;
|
adamc@476
|
1706 }
|
adamc@476
|
1707 else
|
adamc@476
|
1708 return NULL;
|
adamc@476
|
1709 }
|
adamc@463
|
1710 } else {
|
adamc@463
|
1711 if ((s = strchr(p, 0)) && s < ctx->headers_end)
|
adamc@463
|
1712 s += 2;
|
adamc@463
|
1713 else
|
adamc@463
|
1714 return NULL;
|
adamc@463
|
1715 }
|
adamc@463
|
1716 }
|
adamc@457
|
1717 }
|
adamc@462
|
1718
|
adamc@466
|
1719 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v) {
|
adamc@462
|
1720 uw_write_header(ctx, "Set-Cookie: ");
|
adamc@462
|
1721 uw_write_header(ctx, c);
|
adamc@462
|
1722 uw_write_header(ctx, "=");
|
adamc@462
|
1723 uw_write_header(ctx, v);
|
adamc@466
|
1724 uw_write_header(ctx, "; path=");
|
adamc@466
|
1725 uw_write_header(ctx, prefix);
|
adamc@462
|
1726 uw_write_header(ctx, "\r\n");
|
adamc@462
|
1727
|
adamc@462
|
1728 return uw_unit_v;
|
adamc@462
|
1729 }
|
adamc@667
|
1730
|
adamc@668
|
1731 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
|
adamc@668
|
1732 return uw_new_channel()->id;
|
adamc@668
|
1733 }
|
adamc@667
|
1734
|
adamc@668
|
1735 uw_unit uw_Basis_subscribe(uw_context ctx, uw_Basis_channel chn) {
|
adamc@668
|
1736 channel *ch = uw_find_channel(chn);
|
adamc@668
|
1737
|
adamc@668
|
1738 if (ch == NULL)
|
adamc@668
|
1739 uw_error(ctx, FATAL, "Bad channel ID %d", (int)chn);
|
adamc@668
|
1740 else {
|
adamc@668
|
1741 size_t id = atoi(uw_Basis_requestHeader(ctx, "UrWeb-Client"));
|
adamc@668
|
1742 int pass = atoi(uw_Basis_requestHeader(ctx, "UrWeb-Pass"));
|
adamc@668
|
1743 client *c = uw_find_client(id);
|
adamc@668
|
1744
|
adamc@668
|
1745 if (c == NULL)
|
adamc@668
|
1746 uw_error(ctx, FATAL, "Unknown client ID in subscription request");
|
adamc@668
|
1747 else if (c->data.used.pass != pass)
|
adamc@668
|
1748 uw_error(ctx, FATAL, "Wrong client password in subscription request");
|
adamc@668
|
1749 else
|
adamc@668
|
1750 uw_subscribe(ch, c);
|
adamc@668
|
1751 }
|
adamc@668
|
1752 }
|
adamc@668
|
1753
|
adamc@668
|
1754 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
|
adamc@668
|
1755 channel *ch = uw_find_channel(chn);
|
adamc@668
|
1756
|
adamc@668
|
1757 if (ch == NULL)
|
adamc@668
|
1758 uw_error(ctx, FATAL, "Bad channel ID %d", (int)chn);
|
adamc@668
|
1759 else
|
adamc@668
|
1760 uw_channel_send(ch, msg);
|
adamc@668
|
1761 }
|