adamc@741
|
1 #define _XOPEN_SOURCE 500
|
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@682
|
10 #include <assert.h>
|
adamc@741
|
11 #include <ctype.h>
|
adamc@102
|
12
|
adamc@667
|
13 #include <pthread.h>
|
adamc@667
|
14
|
adamc@102
|
15 #include "types.h"
|
adamc@102
|
16
|
adamc@311
|
17 uw_unit uw_unit_v = {};
|
adamc@102
|
18
|
adamc@667
|
19
|
adamc@667
|
20 // Socket extras
|
adamc@667
|
21
|
adamc@667
|
22 int uw_really_send(int sock, const void *buf, size_t len) {
|
adamc@667
|
23 while (len > 0) {
|
adamc@667
|
24 size_t n = send(sock, buf, len, 0);
|
adamc@667
|
25
|
adamc@667
|
26 if (n < 0)
|
adamc@667
|
27 return n;
|
adamc@667
|
28
|
adamc@667
|
29 buf += n;
|
adamc@667
|
30 len -= n;
|
adamc@667
|
31 }
|
adamc@667
|
32
|
adamc@667
|
33 return 0;
|
adamc@667
|
34 }
|
adamc@667
|
35
|
adamc@667
|
36
|
adamc@667
|
37 // Buffers
|
adamc@667
|
38
|
adamc@667
|
39 typedef struct {
|
adamc@667
|
40 char *start, *front, *back;
|
adamc@667
|
41 } buf;
|
adamc@667
|
42
|
adamc@667
|
43 static void buf_init(buf *b, size_t s) {
|
adamc@667
|
44 b->front = b->start = malloc(s);
|
adamc@667
|
45 b->back = b->front + s;
|
adamc@667
|
46 }
|
adamc@667
|
47
|
adamc@667
|
48 static void buf_free(buf *b) {
|
adamc@667
|
49 free(b->start);
|
adamc@667
|
50 }
|
adamc@667
|
51
|
adamc@667
|
52 static void buf_reset(buf *b) {
|
adamc@667
|
53 b->front = b->start;
|
adamc@667
|
54 }
|
adamc@667
|
55
|
adamc@667
|
56 static void buf_check(buf *b, size_t extra) {
|
adamc@667
|
57 if (b->back - b->front < extra) {
|
adamc@667
|
58 size_t desired = b->front - b->start + extra, next;
|
adamc@667
|
59 char *new_heap;
|
adamc@667
|
60
|
adamc@667
|
61 next = b->back - b->start;
|
adamc@667
|
62 if (next == 0)
|
adamc@667
|
63 next = 1;
|
adamc@667
|
64 for (; next < desired; next *= 2);
|
adamc@667
|
65
|
adamc@667
|
66 new_heap = realloc(b->start, next);
|
adamc@667
|
67 b->front = new_heap + (b->front - b->start);
|
adamc@667
|
68 b->back = new_heap + next;
|
adamc@667
|
69 b->start = new_heap;
|
adamc@667
|
70 }
|
adamc@667
|
71 }
|
adamc@667
|
72
|
adamc@667
|
73 static size_t buf_used(buf *b) {
|
adamc@667
|
74 return b->front - b->start;
|
adamc@667
|
75 }
|
adamc@667
|
76
|
adamc@667
|
77 static size_t buf_avail(buf *b) {
|
adamc@667
|
78 return b->back - b->start;
|
adamc@667
|
79 }
|
adamc@667
|
80
|
adamc@668
|
81 static void buf_append(buf *b, const char *s, size_t len) {
|
adamc@672
|
82 buf_check(b, len+1);
|
adamc@668
|
83 memcpy(b->front, s, len);
|
adamc@668
|
84 b->front += len;
|
adamc@672
|
85 *b->front = 0;
|
adamc@668
|
86 }
|
adamc@667
|
87
|
adamc@668
|
88
|
adamc@672
|
89 // Persistent state types
|
adamc@667
|
90
|
adamc@667
|
91 typedef enum { UNUSED, USED } usage;
|
adamc@667
|
92
|
adamc@667
|
93 typedef struct client {
|
adamc@682
|
94 unsigned id;
|
adamc@667
|
95 usage mode;
|
adamc@682
|
96 int pass;
|
adamc@682
|
97 struct client *next;
|
adamc@689
|
98 pthread_mutex_t lock, pull_lock;
|
adamc@682
|
99 buf msgs;
|
adamc@682
|
100 int sock;
|
adamc@682
|
101 time_t last_contact;
|
adamc@682
|
102 unsigned n_channels;
|
adamc@685
|
103 unsigned refcount;
|
adamc@667
|
104 } client;
|
adamc@667
|
105
|
adamc@672
|
106
|
adamc@672
|
107 // Persistent client state
|
adamc@672
|
108
|
adamc@682
|
109 static client **clients, *clients_free, *clients_used;
|
adamc@682
|
110 static unsigned n_clients;
|
adamc@667
|
111
|
adamc@667
|
112 static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
|
adamc@667
|
113
|
adamc@682
|
114 static client *new_client() {
|
adamc@667
|
115 client *c;
|
adamc@667
|
116
|
adamc@667
|
117 pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
118
|
adamc@667
|
119 if (clients_free) {
|
adamc@667
|
120 c = clients_free;
|
adamc@682
|
121 clients_free = clients_free->next;
|
adamc@667
|
122 }
|
adamc@667
|
123 else {
|
adamc@667
|
124 ++n_clients;
|
adamc@667
|
125 clients = realloc(clients, sizeof(client) * n_clients);
|
adamc@667
|
126 c = malloc(sizeof(client));
|
adamc@667
|
127 c->id = n_clients-1;
|
adamc@682
|
128 pthread_mutex_init(&c->lock, NULL);
|
adamc@689
|
129 pthread_mutex_init(&c->pull_lock, NULL);
|
adamc@682
|
130 buf_init(&c->msgs, 0);
|
adamc@667
|
131 clients[n_clients-1] = c;
|
adamc@667
|
132 }
|
adamc@667
|
133
|
adamc@682
|
134 pthread_mutex_lock(&c->lock);
|
adamc@667
|
135 c->mode = USED;
|
adamc@682
|
136 c->pass = rand();
|
adamc@682
|
137 c->sock = -1;
|
adamc@682
|
138 c->last_contact = time(NULL);
|
adamc@682
|
139 buf_reset(&c->msgs);
|
adamc@682
|
140 c->n_channels = 0;
|
adamc@685
|
141 c->refcount = 0;
|
adamc@682
|
142 pthread_mutex_unlock(&c->lock);
|
adamc@682
|
143
|
adamc@682
|
144 c->next = clients_used;
|
adamc@682
|
145 clients_used = c;
|
adamc@667
|
146
|
adamc@667
|
147 pthread_mutex_unlock(&clients_mutex);
|
adamc@667
|
148
|
adamc@667
|
149 return c;
|
adamc@667
|
150 }
|
adamc@667
|
151
|
adamc@685
|
152 static void use_client(client *c) {
|
adamc@685
|
153 pthread_mutex_lock(&c->lock);
|
adamc@685
|
154 ++c->refcount;
|
adamc@685
|
155 pthread_mutex_unlock(&c->lock);
|
adamc@689
|
156 pthread_mutex_lock(&c->pull_lock);
|
adamc@685
|
157 }
|
adamc@685
|
158
|
adamc@685
|
159 static void release_client(client *c) {
|
adamc@689
|
160 pthread_mutex_unlock(&c->pull_lock);
|
adamc@685
|
161 pthread_mutex_lock(&c->lock);
|
adamc@685
|
162 --c->refcount;
|
adamc@685
|
163 pthread_mutex_unlock(&c->lock);
|
adamc@689
|
164
|
adamc@685
|
165 }
|
adamc@685
|
166
|
adamc@667
|
167 static const char begin_msgs[] = "HTTP/1.1 200 OK\r\nContent-type: text/plain\r\n\r\n";
|
adamc@667
|
168
|
adamc@682
|
169 static client *find_client(unsigned id) {
|
adamc@667
|
170 client *c;
|
adamc@667
|
171
|
adamc@667
|
172 pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
173
|
adamc@667
|
174 if (id >= n_clients) {
|
adamc@667
|
175 pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
176 return NULL;
|
adamc@667
|
177 }
|
adamc@667
|
178
|
adamc@667
|
179 c = clients[id];
|
adamc@667
|
180
|
adamc@668
|
181 pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
182 return c;
|
adamc@668
|
183 }
|
adamc@668
|
184
|
adamc@682
|
185 void uw_client_connect(unsigned id, int pass, int sock) {
|
adamc@682
|
186 client *c = find_client(id);
|
adamc@668
|
187
|
adamc@668
|
188 if (c == NULL) {
|
adamc@667
|
189 close(sock);
|
adamc@682
|
190 fprintf(stderr, "Out-of-bounds client request (%u)\n", id);
|
adamc@667
|
191 return;
|
adamc@667
|
192 }
|
adamc@667
|
193
|
adamc@682
|
194 pthread_mutex_lock(&c->lock);
|
adamc@671
|
195
|
adamc@682
|
196 if (c->mode != USED) {
|
adamc@682
|
197 pthread_mutex_unlock(&c->lock);
|
adamc@667
|
198 close(sock);
|
adamc@682
|
199 fprintf(stderr, "Client request for unused slot (%u)\n", id);
|
adamc@667
|
200 return;
|
adamc@667
|
201 }
|
adamc@667
|
202
|
adamc@682
|
203 if (pass != c->pass) {
|
adamc@682
|
204 pthread_mutex_unlock(&c->lock);
|
adamc@682
|
205 close(sock);
|
adamc@682
|
206 fprintf(stderr, "Wrong client password (%u, %d)\n", id, pass);
|
adamc@682
|
207 return;
|
adamc@667
|
208 }
|
adamc@667
|
209
|
adamc@682
|
210 if (c->sock != -1) {
|
adamc@682
|
211 close(c->sock);
|
adamc@682
|
212 c->sock = -1;
|
adamc@682
|
213 }
|
adamc@667
|
214
|
adamc@682
|
215 c->last_contact = time(NULL);
|
adamc@682
|
216
|
adamc@682
|
217 if (buf_used(&c->msgs) > 0) {
|
adamc@667
|
218 uw_really_send(sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adamc@682
|
219 uw_really_send(sock, c->msgs.start, buf_used(&c->msgs));
|
adamc@682
|
220 buf_reset(&c->msgs);
|
adamc@667
|
221 close(sock);
|
adamc@667
|
222 }
|
adamc@668
|
223 else
|
adamc@682
|
224 c->sock = sock;
|
adamc@667
|
225
|
adamc@682
|
226 pthread_mutex_unlock(&c->lock);
|
adamc@667
|
227 }
|
adamc@667
|
228
|
adamc@682
|
229 static void free_client(client *c) {
|
adamc@683
|
230 printf("Freeing client %u\n", c->id);
|
adamc@667
|
231
|
adamc@682
|
232 c->mode = UNUSED;
|
adamc@682
|
233 c->pass = -1;
|
adamc@667
|
234
|
adamc@682
|
235 c->next = clients_free;
|
adamc@682
|
236 clients_free = c;
|
adamc@667
|
237 }
|
adamc@667
|
238
|
adamc@682
|
239 static uw_Basis_channel new_channel(client *c) {
|
adamc@682
|
240 uw_Basis_channel ch = {c->id, c->n_channels++};
|
adamc@668
|
241 return ch;
|
adamc@668
|
242 }
|
adamc@668
|
243
|
adamc@685
|
244 static void client_send(client *c, buf *msg) {
|
adamc@685
|
245 pthread_mutex_lock(&c->lock);
|
adamc@671
|
246
|
adamc@682
|
247 if (c->sock != -1) {
|
adamc@682
|
248 uw_really_send(c->sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adamc@682
|
249 uw_really_send(c->sock, msg->start, buf_used(msg));
|
adamc@682
|
250 close(c->sock);
|
adamc@682
|
251 c->sock = -1;
|
adamc@682
|
252 } else
|
adamc@682
|
253 buf_append(&c->msgs, msg->start, buf_used(msg));
|
adamc@671
|
254
|
adamc@685
|
255 pthread_mutex_unlock(&c->lock);
|
adamc@668
|
256 }
|
adamc@668
|
257
|
adamc@668
|
258
|
adamc@668
|
259 // Global entry points
|
adamc@668
|
260
|
adamc@668
|
261 void uw_global_init() {
|
adamc@668
|
262 srand(time(NULL) ^ getpid());
|
adamc@668
|
263
|
adamc@668
|
264 clients = malloc(0);
|
adamc@668
|
265 }
|
adamc@668
|
266
|
adamc@668
|
267
|
adamc@667
|
268 // Single-request state
|
adamc@667
|
269
|
adamc@167
|
270 #define ERROR_BUF_LEN 1024
|
adamc@167
|
271
|
adamc@323
|
272 typedef struct regions {
|
adamc@323
|
273 struct regions *next;
|
adamc@323
|
274 } regions;
|
adamc@323
|
275
|
adamc@425
|
276 typedef struct {
|
adamc@425
|
277 void (*func)(void*);
|
adamc@425
|
278 void *arg;
|
adamc@425
|
279 } cleanup;
|
adamc@425
|
280
|
adamc@671
|
281 typedef struct {
|
adamc@682
|
282 unsigned client;
|
adamc@671
|
283 buf msgs;
|
adamc@682
|
284 } delta;
|
adamc@671
|
285
|
adamc@737
|
286 typedef enum {
|
adamc@739
|
287 UNSET, NORMAL, FIL
|
adamc@737
|
288 } input_kind;
|
adamc@737
|
289
|
adamc@737
|
290 typedef struct {
|
adamc@737
|
291 input_kind kind;
|
adamc@737
|
292 union {
|
adamc@737
|
293 char *normal;
|
adamc@739
|
294 uw_Basis_file file;
|
adamc@737
|
295 } data;
|
adamc@737
|
296 } input;
|
adamc@737
|
297
|
adamc@311
|
298 struct uw_context {
|
adamc@458
|
299 char *headers, *headers_end;
|
adamc@457
|
300
|
adamc@666
|
301 buf outHeaders, page, heap, script;
|
adamc@737
|
302 input *inputs;
|
adamc@167
|
303
|
adamc@565
|
304 int source_count;
|
adamc@562
|
305
|
adamc@272
|
306 void *db;
|
adamc@272
|
307
|
adamc@167
|
308 jmp_buf jmp_buf;
|
adamc@167
|
309
|
adamc@323
|
310 regions *regions;
|
adamc@323
|
311
|
adamc@425
|
312 cleanup *cleanup, *cleanup_front, *cleanup_back;
|
adamc@425
|
313
|
adamc@667
|
314 const char *script_header, *url_prefix;
|
adamc@643
|
315
|
adamc@736
|
316 int needs_push, needs_sig;
|
adamc@693
|
317
|
adamc@682
|
318 size_t n_deltas, used_deltas;
|
adamc@682
|
319 delta *deltas;
|
adamc@671
|
320
|
adamc@673
|
321 int timeout;
|
adamc@673
|
322
|
adamc@682
|
323 client *client;
|
adamc@682
|
324
|
adamc@167
|
325 char error_message[ERROR_BUF_LEN];
|
adamc@117
|
326 };
|
adamc@117
|
327
|
adamc@683
|
328 extern int uw_inputs_len, uw_timeout;
|
adamc@144
|
329
|
adamc@687
|
330 uw_context uw_init() {
|
adamc@311
|
331 uw_context ctx = malloc(sizeof(struct uw_context));
|
adamc@136
|
332
|
adamc@458
|
333 ctx->headers = ctx->headers_end = NULL;
|
adamc@457
|
334
|
adamc@687
|
335 buf_init(&ctx->outHeaders, 0);
|
adamc@687
|
336 buf_init(&ctx->page, 0);
|
adamc@687
|
337 buf_init(&ctx->heap, 0);
|
adamc@698
|
338 buf_init(&ctx->script, 1);
|
adamc@667
|
339 ctx->script.start[0] = 0;
|
adamc@136
|
340
|
adamc@737
|
341 ctx->inputs = calloc(uw_inputs_len, sizeof(input));
|
adamc@144
|
342
|
adamc@272
|
343 ctx->db = NULL;
|
adamc@272
|
344
|
adamc@323
|
345 ctx->regions = NULL;
|
adamc@323
|
346
|
adamc@425
|
347 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
|
adamc@425
|
348
|
adamc@643
|
349 ctx->script_header = "";
|
adamc@667
|
350 ctx->url_prefix = "/";
|
adamc@693
|
351 ctx->needs_push = 0;
|
adamc@736
|
352 ctx->needs_sig = 0;
|
adamc@643
|
353
|
adamc@167
|
354 ctx->error_message[0] = 0;
|
adamc@167
|
355
|
adamc@565
|
356 ctx->source_count = 0;
|
adamc@562
|
357
|
adamc@682
|
358 ctx->n_deltas = ctx->used_deltas = 0;
|
adamc@671
|
359 ctx->deltas = malloc(0);
|
adamc@671
|
360
|
adamc@673
|
361 ctx->timeout = uw_timeout;
|
adamc@673
|
362
|
adamc@700
|
363 ctx->client = NULL;
|
adamc@700
|
364
|
adamc@700
|
365 ctx->error_message[0] = 0;
|
adamc@700
|
366
|
adamc@117
|
367 return ctx;
|
adamc@106
|
368 }
|
adamc@106
|
369
|
adamc@311
|
370 void uw_set_db(uw_context ctx, void *db) {
|
adamc@272
|
371 ctx->db = db;
|
adamc@272
|
372 }
|
adamc@272
|
373
|
adamc@311
|
374 void *uw_get_db(uw_context ctx) {
|
adamc@272
|
375 return ctx->db;
|
adamc@272
|
376 }
|
adamc@272
|
377
|
adamc@311
|
378 void uw_free(uw_context ctx) {
|
adamc@671
|
379 size_t i;
|
adamc@671
|
380
|
adamc@666
|
381 buf_free(&ctx->outHeaders);
|
adamc@666
|
382 buf_free(&ctx->script);
|
adamc@666
|
383 buf_free(&ctx->page);
|
adamc@666
|
384 buf_free(&ctx->heap);
|
adamc@144
|
385 free(ctx->inputs);
|
adamc@425
|
386 free(ctx->cleanup);
|
adamc@671
|
387
|
adamc@682
|
388 for (i = 0; i < ctx->n_deltas; ++i)
|
adamc@671
|
389 buf_free(&ctx->deltas[i].msgs);
|
adamc@671
|
390
|
adamc@136
|
391 free(ctx);
|
adamc@136
|
392 }
|
adamc@136
|
393
|
adamc@562
|
394 void uw_reset_keep_error_message(uw_context ctx) {
|
adamc@666
|
395 buf_reset(&ctx->outHeaders);
|
adamc@666
|
396 buf_reset(&ctx->script);
|
adamc@667
|
397 ctx->script.start[0] = 0;
|
adamc@666
|
398 buf_reset(&ctx->page);
|
adamc@666
|
399 buf_reset(&ctx->heap);
|
adamc@323
|
400 ctx->regions = NULL;
|
adamc@425
|
401 ctx->cleanup_front = ctx->cleanup;
|
adamc@565
|
402 ctx->source_count = 0;
|
adamc@682
|
403 ctx->used_deltas = 0;
|
adamc@682
|
404 ctx->client = NULL;
|
adamc@167
|
405 }
|
adamc@167
|
406
|
adamc@562
|
407 void uw_reset_keep_request(uw_context ctx) {
|
adamc@562
|
408 uw_reset_keep_error_message(ctx);
|
adamc@562
|
409 ctx->error_message[0] = 0;
|
adamc@167
|
410 }
|
adamc@167
|
411
|
adamc@311
|
412 void uw_reset(uw_context ctx) {
|
adamc@311
|
413 uw_reset_keep_request(ctx);
|
adamc@737
|
414 memset(ctx->inputs, 0, uw_inputs_len * sizeof(input));
|
adamc@144
|
415 }
|
adamc@144
|
416
|
adamc@311
|
417 void uw_db_init(uw_context);
|
adamc@311
|
418 void uw_handle(uw_context, char *);
|
adamc@167
|
419
|
adamc@311
|
420 failure_kind uw_begin_init(uw_context ctx) {
|
adamc@272
|
421 int r = setjmp(ctx->jmp_buf);
|
adamc@272
|
422
|
adamc@272
|
423 if (r == 0)
|
adamc@311
|
424 uw_db_init(ctx);
|
adamc@272
|
425
|
adamc@272
|
426 return r;
|
adamc@272
|
427 }
|
adamc@272
|
428
|
adamc@458
|
429 void uw_set_headers(uw_context ctx, char *headers) {
|
adamc@458
|
430 char *s = headers, *s2;
|
adamc@458
|
431 ctx->headers = headers;
|
adamc@458
|
432
|
adamc@458
|
433 while (s2 = strchr(s, '\r')) {
|
adamc@458
|
434 s = s2;
|
adamc@458
|
435
|
adamc@458
|
436 if (s[1] == 0)
|
adamc@458
|
437 break;
|
adamc@458
|
438
|
adamc@458
|
439 *s = 0;
|
adamc@458
|
440 s += 2;
|
adamc@458
|
441 }
|
adamc@458
|
442
|
adamc@458
|
443 ctx->headers_end = s;
|
adamc@458
|
444 }
|
adamc@458
|
445
|
adamc@671
|
446 int uw_db_begin(uw_context);
|
adamc@167
|
447
|
adamc@311
|
448 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
|
adamc@425
|
449 cleanup *cl;
|
adamc@425
|
450
|
adamc@167
|
451 va_list ap;
|
adamc@167
|
452 va_start(ap, fmt);
|
adamc@167
|
453
|
adamc@167
|
454 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
|
adamc@167
|
455
|
adamc@425
|
456 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@425
|
457 cl->func(cl->arg);
|
adamc@425
|
458
|
adamc@425
|
459 ctx->cleanup_front = ctx->cleanup;
|
adamc@425
|
460
|
adamc@190
|
461 longjmp(ctx->jmp_buf, fk);
|
adamc@167
|
462 }
|
adamc@167
|
463
|
adamc@425
|
464 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
|
adamc@425
|
465 if (ctx->cleanup_front >= ctx->cleanup_back) {
|
adamc@425
|
466 int len = ctx->cleanup_back - ctx->cleanup, newLen;
|
adamc@425
|
467 if (len == 0)
|
adamc@425
|
468 newLen = 1;
|
adamc@425
|
469 else
|
adamc@428
|
470 newLen = len * 2;
|
adamc@470
|
471 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
|
adamc@425
|
472 ctx->cleanup_front = ctx->cleanup + len;
|
adamc@425
|
473 ctx->cleanup_back = ctx->cleanup + newLen;
|
adamc@425
|
474 }
|
adamc@425
|
475
|
adamc@425
|
476 ctx->cleanup_front->func = func;
|
adamc@425
|
477 ctx->cleanup_front->arg = arg;
|
adamc@425
|
478 ++ctx->cleanup_front;
|
adamc@425
|
479 }
|
adamc@425
|
480
|
adamc@682
|
481 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
|
adamc@682
|
482 int len = strlen(h);
|
adamc@682
|
483 char *s = ctx->headers, *p;
|
adamc@682
|
484
|
adamc@682
|
485 while (p = strchr(s, ':')) {
|
adamc@682
|
486 if (p - s == len && !strncasecmp(s, h, len)) {
|
adamc@682
|
487 return p + 2;
|
adamc@682
|
488 } else {
|
adamc@682
|
489 if ((s = strchr(p, 0)) && s < ctx->headers_end)
|
adamc@682
|
490 s += 2;
|
adamc@682
|
491 else
|
adamc@682
|
492 return NULL;
|
adamc@682
|
493 }
|
adamc@682
|
494 }
|
adamc@682
|
495
|
adamc@682
|
496 return NULL;
|
adamc@682
|
497 }
|
adamc@682
|
498
|
adamc@682
|
499 void uw_login(uw_context ctx) {
|
adamc@693
|
500 if (ctx->needs_push) {
|
adamc@682
|
501 char *id_s, *pass_s;
|
adamc@682
|
502
|
adamc@682
|
503 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client"))
|
adamc@682
|
504 && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) {
|
adamc@682
|
505 unsigned id = atoi(id_s);
|
adamc@682
|
506 int pass = atoi(pass_s);
|
adamc@682
|
507 client *c = find_client(id);
|
adamc@682
|
508
|
adamc@682
|
509 if (c == NULL)
|
adamc@682
|
510 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", id_s, pass_s);
|
adamc@682
|
511 else {
|
adamc@685
|
512 use_client(c);
|
adamc@682
|
513 ctx->client = c;
|
adamc@682
|
514
|
adamc@682
|
515 if (c->mode != USED)
|
adamc@682
|
516 uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id);
|
adamc@682
|
517 if (c->pass != pass)
|
adamc@682
|
518 uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass);
|
adamc@682
|
519 }
|
adamc@682
|
520 } else {
|
adamc@682
|
521 client *c = new_client();
|
adamc@685
|
522 use_client(c);
|
adamc@682
|
523 ctx->client = c;
|
adamc@682
|
524 }
|
adamc@682
|
525 }
|
adamc@682
|
526 }
|
adamc@682
|
527
|
adamc@671
|
528 failure_kind uw_begin(uw_context ctx, char *path) {
|
adamc@671
|
529 int r = setjmp(ctx->jmp_buf);
|
adamc@671
|
530
|
adamc@671
|
531 if (r == 0) {
|
adamc@671
|
532 if (uw_db_begin(ctx))
|
adamc@671
|
533 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
|
adamc@682
|
534
|
adamc@671
|
535 uw_handle(ctx, path);
|
adamc@671
|
536 }
|
adamc@671
|
537
|
adamc@671
|
538 return r;
|
adamc@671
|
539 }
|
adamc@671
|
540
|
adamc@682
|
541 uw_Basis_client uw_Basis_self(uw_context ctx, uw_unit u) {
|
adamc@682
|
542 if (ctx->client == NULL)
|
adamc@682
|
543 uw_error(ctx, FATAL, "Call to Basis.self() from page that has only server-side code");
|
adamc@682
|
544
|
adamc@682
|
545 return ctx->client->id;
|
adamc@682
|
546 }
|
adamc@682
|
547
|
adamc@425
|
548 void uw_pop_cleanup(uw_context ctx) {
|
adamc@425
|
549 if (ctx->cleanup_front == ctx->cleanup)
|
adamc@425
|
550 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
|
adamc@425
|
551
|
adamc@425
|
552 --ctx->cleanup_front;
|
adamc@425
|
553 ctx->cleanup_front->func(ctx->cleanup_front->arg);
|
adamc@425
|
554 }
|
adamc@425
|
555
|
adamc@311
|
556 char *uw_error_message(uw_context ctx) {
|
adamc@167
|
557 return ctx->error_message;
|
adamc@167
|
558 }
|
adamc@167
|
559
|
adamc@737
|
560 extern int uw_input_num(const char*);
|
adamc@144
|
561
|
adamc@737
|
562 void uw_set_input(uw_context ctx, const char *name, char *value) {
|
adamc@311
|
563 int n = uw_input_num(name);
|
adamc@144
|
564
|
adamc@169
|
565 if (n < 0)
|
adamc@311
|
566 uw_error(ctx, FATAL, "Bad input name %s", name);
|
adamc@144
|
567
|
adamc@311
|
568 if (n >= uw_inputs_len)
|
adamc@311
|
569 uw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n);
|
adamc@169
|
570
|
adamc@737
|
571 ctx->inputs[n].kind = NORMAL;
|
adamc@737
|
572 ctx->inputs[n].data.normal = value;
|
adamc@144
|
573 }
|
adamc@144
|
574
|
adamc@311
|
575 char *uw_get_input(uw_context ctx, int n) {
|
adamc@169
|
576 if (n < 0)
|
adamc@311
|
577 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@311
|
578 if (n >= uw_inputs_len)
|
adamc@311
|
579 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@737
|
580
|
adamc@737
|
581 switch (ctx->inputs[n].kind) {
|
adamc@737
|
582 case UNSET:
|
adamc@737
|
583 return NULL;
|
adamc@739
|
584 case FIL:
|
adamc@739
|
585 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
|
adamc@737
|
586 case NORMAL:
|
adamc@737
|
587 return ctx->inputs[n].data.normal;
|
adamc@737
|
588 default:
|
adamc@737
|
589 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
590 }
|
adamc@138
|
591 }
|
adamc@138
|
592
|
adamc@311
|
593 char *uw_get_optional_input(uw_context ctx, int n) {
|
adamc@190
|
594 if (n < 0)
|
adamc@311
|
595 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@311
|
596 if (n >= uw_inputs_len)
|
adamc@311
|
597 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@737
|
598
|
adamc@737
|
599 switch (ctx->inputs[n].kind) {
|
adamc@737
|
600 case UNSET:
|
adamc@737
|
601 return "";
|
adamc@739
|
602 case FIL:
|
adamc@739
|
603 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
|
adamc@737
|
604 case NORMAL:
|
adamc@737
|
605 return ctx->inputs[n].data.normal;
|
adamc@737
|
606 default:
|
adamc@737
|
607 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
608 }
|
adamc@737
|
609 }
|
adamc@737
|
610
|
adamc@739
|
611 void uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) {
|
adamc@737
|
612 int n = uw_input_num(name);
|
adamc@737
|
613
|
adamc@737
|
614 if (n < 0)
|
adamc@737
|
615 uw_error(ctx, FATAL, "Bad file input name %s", name);
|
adamc@737
|
616
|
adamc@737
|
617 if (n >= uw_inputs_len)
|
adamc@737
|
618 uw_error(ctx, FATAL, "For file input name %s, index %d is out of range", name, n);
|
adamc@737
|
619
|
adamc@739
|
620 ctx->inputs[n].kind = FIL;
|
adamc@739
|
621 ctx->inputs[n].data.file = f;
|
adamc@737
|
622 }
|
adamc@737
|
623
|
adamc@739
|
624 void *uw_malloc(uw_context ctx, size_t len);
|
adamc@739
|
625
|
adamc@739
|
626 uw_Basis_file uw_get_file_input(uw_context ctx, int n) {
|
adamc@737
|
627 if (n < 0)
|
adamc@737
|
628 uw_error(ctx, FATAL, "Negative file input index %d", n);
|
adamc@737
|
629 if (n >= uw_inputs_len)
|
adamc@737
|
630 uw_error(ctx, FATAL, "Out-of-bounds file input index %d", n);
|
adamc@737
|
631
|
adamc@737
|
632 switch (ctx->inputs[n].kind) {
|
adamc@737
|
633 case UNSET:
|
adamc@737
|
634 {
|
adamc@739
|
635 char *data = uw_malloc(ctx, 0);
|
adamc@740
|
636 uw_Basis_file f = {NULL, "", {0, data}};
|
adamc@739
|
637 return f;
|
adamc@737
|
638 }
|
adamc@739
|
639 case FIL:
|
adamc@739
|
640 return ctx->inputs[n].data.file;
|
adamc@737
|
641 case NORMAL:
|
adamc@737
|
642 uw_error(ctx, FATAL, "Tried to read a normal form input as files");
|
adamc@737
|
643 default:
|
adamc@737
|
644 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
645 }
|
adamc@190
|
646 }
|
adamc@190
|
647
|
adamc@643
|
648 void uw_set_script_header(uw_context ctx, const char *s) {
|
adamc@643
|
649 ctx->script_header = s;
|
adamc@643
|
650 }
|
adamc@643
|
651
|
adamc@667
|
652 void uw_set_url_prefix(uw_context ctx, const char *s) {
|
adamc@667
|
653 ctx->url_prefix = s;
|
adamc@667
|
654 }
|
adamc@667
|
655
|
adamc@693
|
656 void uw_set_needs_push(uw_context ctx, int n) {
|
adamc@693
|
657 ctx->needs_push = n;
|
adamc@693
|
658 }
|
adamc@693
|
659
|
adamc@736
|
660 void uw_set_needs_sig(uw_context ctx, int n) {
|
adamc@736
|
661 ctx->needs_sig = n;
|
adamc@736
|
662 }
|
adamc@736
|
663
|
adamc@667
|
664
|
adamc@667
|
665 static void buf_check_ctx(uw_context ctx, buf *b, size_t extra, const char *desc) {
|
adamc@666
|
666 if (b->back - b->front < extra) {
|
adamc@666
|
667 size_t desired = b->front - b->start + extra, next;
|
adamc@136
|
668 char *new_heap;
|
adamc@136
|
669
|
adamc@666
|
670 next = b->back - b->start;
|
adamc@317
|
671 if (next == 0)
|
adamc@317
|
672 next = 1;
|
adamc@317
|
673 for (; next < desired; next *= 2);
|
adamc@136
|
674
|
adamc@666
|
675 new_heap = realloc(b->start, next);
|
adamc@666
|
676 b->front = new_heap + (b->front - b->start);
|
adamc@666
|
677 b->back = new_heap + next;
|
adamc@136
|
678
|
adamc@667
|
679 if (new_heap != b->start) {
|
adamc@666
|
680 b->start = new_heap;
|
adamc@666
|
681 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously", desc);
|
adamc@136
|
682 }
|
adamc@136
|
683
|
adamc@666
|
684 b->start = new_heap;
|
adamc@136
|
685 }
|
adamc@136
|
686 }
|
adamc@136
|
687
|
adamc@666
|
688 static void uw_check_heap(uw_context ctx, size_t extra) {
|
adamc@667
|
689 buf_check_ctx(ctx, &ctx->heap, extra, "heap chunk");
|
adamc@666
|
690 }
|
adamc@666
|
691
|
adamc@311
|
692 void *uw_malloc(uw_context ctx, size_t len) {
|
adamc@136
|
693 void *result;
|
adamc@136
|
694
|
adamc@311
|
695 uw_check_heap(ctx, len);
|
adamc@136
|
696
|
adamc@666
|
697 result = ctx->heap.front;
|
adamc@666
|
698 ctx->heap.front += len;
|
adamc@136
|
699 return result;
|
adamc@117
|
700 }
|
adamc@117
|
701
|
adamc@323
|
702 void uw_begin_region(uw_context ctx) {
|
adamc@666
|
703 regions *r = (regions *) ctx->heap.front;
|
adamc@323
|
704
|
adamc@323
|
705 uw_check_heap(ctx, sizeof(regions));
|
adamc@323
|
706
|
adamc@666
|
707 ctx->heap.front += sizeof(regions);
|
adamc@323
|
708
|
adamc@323
|
709 r->next = ctx->regions;
|
adamc@323
|
710 ctx->regions = r;
|
adamc@323
|
711 }
|
adamc@323
|
712
|
adamc@323
|
713 void uw_end_region(uw_context ctx) {
|
adamc@323
|
714 regions *r = ctx->regions;
|
adamc@323
|
715
|
adamc@323
|
716 if (r == NULL)
|
adamc@323
|
717 uw_error(ctx, FATAL, "Region stack underflow");
|
adamc@323
|
718
|
adamc@666
|
719 ctx->heap.front = (char *) r;
|
adamc@323
|
720 ctx->regions = r->next;
|
adamc@323
|
721 }
|
adamc@323
|
722
|
adamc@324
|
723 void uw_memstats(uw_context ctx) {
|
adamc@666
|
724 printf("Headers: %d/%d\n", buf_used(&ctx->outHeaders), buf_avail(&ctx->outHeaders));
|
adamc@666
|
725 printf("Script: %d/%d\n", buf_used(&ctx->script), buf_avail(&ctx->script));
|
adamc@666
|
726 printf("Page: %d/%d\n", buf_used(&ctx->page), buf_avail(&ctx->page));
|
adamc@666
|
727 printf("Heap: %d/%d\n", buf_used(&ctx->heap), buf_avail(&ctx->heap));
|
adamc@324
|
728 }
|
adamc@324
|
729
|
adamc@311
|
730 int uw_send(uw_context ctx, int sock) {
|
adamc@666
|
731 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
|
adamc@462
|
732
|
adamc@462
|
733 if (n < 0)
|
adamc@462
|
734 return n;
|
adamc@462
|
735
|
adamc@462
|
736 n = uw_really_send(sock, "\r\n", 2);
|
adamc@462
|
737
|
adamc@462
|
738 if (n < 0)
|
adamc@462
|
739 return n;
|
adamc@462
|
740
|
adamc@666
|
741 return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start);
|
adamc@462
|
742 }
|
adamc@462
|
743
|
adamc@462
|
744 static void uw_check_headers(uw_context ctx, size_t extra) {
|
adamc@667
|
745 buf_check(&ctx->outHeaders, extra);
|
adamc@462
|
746 }
|
adamc@462
|
747
|
adamc@462
|
748 void uw_write_header(uw_context ctx, uw_Basis_string s) {
|
adamc@462
|
749 int len = strlen(s);
|
adamc@462
|
750
|
adamc@462
|
751 uw_check_headers(ctx, len + 1);
|
adamc@666
|
752 strcpy(ctx->outHeaders.front, s);
|
adamc@666
|
753 ctx->outHeaders.front += len;
|
adamc@562
|
754 }
|
adamc@562
|
755
|
adamc@562
|
756 static void uw_check_script(uw_context ctx, size_t extra) {
|
adamc@667
|
757 buf_check(&ctx->script, extra);
|
adamc@562
|
758 }
|
adamc@562
|
759
|
adamc@562
|
760 void uw_write_script(uw_context ctx, uw_Basis_string s) {
|
adamc@562
|
761 int len = strlen(s);
|
adamc@562
|
762
|
adamc@562
|
763 uw_check_script(ctx, len + 1);
|
adamc@666
|
764 strcpy(ctx->script.front, s);
|
adamc@666
|
765 ctx->script.front += len;
|
adamc@562
|
766 }
|
adamc@562
|
767
|
adamc@645
|
768 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
|
adamc@667
|
769 if (ctx->script_header[0] == 0)
|
adamc@667
|
770 return "";
|
adamc@696
|
771 else if (buf_used(&ctx->script) == 0)
|
adamc@696
|
772 return ctx->script_header;
|
adamc@667
|
773 else {
|
adamc@695
|
774 char *r = uw_malloc(ctx, strlen(ctx->script_header) + 42 + buf_used(&ctx->script));
|
adamc@695
|
775 sprintf(r, "%s<script type=\"text/javascript\">%s</script>",
|
adamc@673
|
776 ctx->script_header,
|
adamc@673
|
777 ctx->script.start);
|
adamc@565
|
778 return r;
|
adamc@565
|
779 }
|
adamc@565
|
780 }
|
adamc@565
|
781
|
adamc@695
|
782 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
|
adamc@695
|
783 if (s[0] == 0)
|
adamc@695
|
784 return "";
|
adamc@695
|
785 else {
|
adamc@695
|
786 char *r = uw_malloc(ctx, 11 + strlen(s));
|
adamc@695
|
787 sprintf(r, " onload='%s'", s);
|
adamc@695
|
788 return r;
|
adamc@695
|
789 }
|
adamc@695
|
790 }
|
adamc@695
|
791
|
adamc@736
|
792 extern uw_Basis_string uw_cookie_sig(uw_context);
|
adamc@736
|
793
|
adamc@694
|
794 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
|
adamc@736
|
795 if (ctx->client == NULL) {
|
adamc@736
|
796 if (ctx->needs_sig) {
|
adamc@736
|
797 char *sig = uw_cookie_sig(ctx);
|
adamc@736
|
798 char *r = uw_malloc(ctx, strlen(sig) + 8);
|
adamc@736
|
799 sprintf(r, "sig=\"%s\";", sig);
|
adamc@736
|
800 return r;
|
adamc@736
|
801 }
|
adamc@736
|
802 else
|
adamc@736
|
803 return "";
|
adamc@736
|
804 } else {
|
adamc@736
|
805 char *sig = ctx->needs_sig ? uw_cookie_sig(ctx) : "";
|
adamc@736
|
806 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->url_prefix)
|
adamc@736
|
807 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
|
adamc@736
|
808 sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
|
adamc@682
|
809 ctx->client->id,
|
adamc@682
|
810 ctx->client->pass,
|
adamc@679
|
811 ctx->url_prefix,
|
adamc@736
|
812 ctx->timeout,
|
adamc@736
|
813 ctx->needs_sig ? "sig=\"" : "",
|
adamc@736
|
814 sig,
|
adamc@736
|
815 ctx->needs_sig ? "\";" : "");
|
adamc@679
|
816 return r;
|
adamc@668
|
817 }
|
adamc@667
|
818 }
|
adamc@667
|
819
|
adamc@574
|
820 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
821 char *r, *s2;
|
adamc@574
|
822
|
adamc@574
|
823 uw_check_heap(ctx, strlen(s) * 4 + 2);
|
adamc@574
|
824
|
adamc@666
|
825 r = s2 = ctx->heap.front;
|
adamc@574
|
826 *s2++ = '"';
|
adamc@574
|
827
|
adamc@574
|
828 for (; *s; s++) {
|
adamc@574
|
829 char c = *s;
|
adamc@574
|
830
|
adamc@574
|
831 switch (c) {
|
adamc@574
|
832 case '"':
|
adamc@574
|
833 strcpy(s2, "\\\"");
|
adamc@574
|
834 s2 += 2;
|
adamc@574
|
835 break;
|
adamc@574
|
836 case '\\':
|
adamc@574
|
837 strcpy(s2, "\\\\");
|
adamc@574
|
838 s2 += 2;
|
adamc@574
|
839 break;
|
adamc@574
|
840 default:
|
adamc@574
|
841 if (isprint(c))
|
adamc@574
|
842 *s2++ = c;
|
adamc@574
|
843 else {
|
adamc@574
|
844 sprintf(s2, "\\%3o", c);
|
adamc@574
|
845 s2 += 4;
|
adamc@574
|
846 }
|
adamc@574
|
847 }
|
adamc@574
|
848 }
|
adamc@574
|
849
|
adamc@574
|
850 strcpy(s2, "\"");
|
adamc@666
|
851 ctx->heap.front = s2 + 1;
|
adamc@574
|
852 return r;
|
adamc@574
|
853 }
|
adamc@574
|
854
|
adamc@574
|
855 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
856 char *r, *s2;
|
adamc@574
|
857
|
adamc@574
|
858 uw_check_script(ctx, strlen(s) * 4 + 2);
|
adamc@574
|
859
|
adamc@666
|
860 r = s2 = ctx->script.front;
|
adamc@574
|
861 *s2++ = '"';
|
adamc@574
|
862
|
adamc@574
|
863 for (; *s; s++) {
|
adamc@574
|
864 char c = *s;
|
adamc@574
|
865
|
adamc@574
|
866 switch (c) {
|
adamc@577
|
867 case '\'':
|
adamc@574
|
868 strcpy(s2, "\\\"");
|
adamc@574
|
869 s2 += 2;
|
adamc@574
|
870 break;
|
adamc@574
|
871 case '\\':
|
adamc@574
|
872 strcpy(s2, "\\\\");
|
adamc@574
|
873 s2 += 2;
|
adamc@574
|
874 break;
|
adamc@574
|
875 default:
|
adamc@574
|
876 if (isprint(c))
|
adamc@574
|
877 *s2++ = c;
|
adamc@574
|
878 else {
|
adamc@574
|
879 sprintf(s2, "\\%3o", c);
|
adamc@574
|
880 s2 += 4;
|
adamc@574
|
881 }
|
adamc@574
|
882 }
|
adamc@574
|
883 }
|
adamc@574
|
884
|
adamc@574
|
885 strcpy(s2, "\"");
|
adamc@666
|
886 ctx->script.front = s2 + 1;
|
adamc@574
|
887 return r;
|
adamc@574
|
888 }
|
adamc@574
|
889
|
adamc@682
|
890 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
891 if (ctx->client == NULL || chn.cli != ctx->client->id)
|
adamc@682
|
892 return "null";
|
adamc@682
|
893 else {
|
adamc@682
|
894 int len;
|
adamc@682
|
895 char *r;
|
adamc@682
|
896
|
adamc@682
|
897 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
898 r = ctx->heap.front;
|
adamc@682
|
899 sprintf(r, "%u%n", chn.chn, &len);
|
adamc@682
|
900 ctx->heap.front += len+1;
|
adamc@682
|
901 return r;
|
adamc@682
|
902 }
|
adamc@682
|
903 }
|
adamc@682
|
904
|
adamc@577
|
905 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
|
adamc@577
|
906 int len;
|
adamc@577
|
907 size_t s_len = strlen(s);
|
adamc@562
|
908
|
adamc@577
|
909 uw_check_script(ctx, 12 + INTS_MAX + s_len);
|
adamc@666
|
910 sprintf(ctx->script.front, "var s%d=sc(%n", ctx->source_count, &len);
|
adamc@666
|
911 ctx->script.front += len;
|
adamc@666
|
912 strcpy(ctx->script.front, s);
|
adamc@666
|
913 ctx->script.front += s_len;
|
adamc@666
|
914 strcpy(ctx->script.front, ");");
|
adamc@666
|
915 ctx->script.front += 2;
|
adamc@562
|
916
|
adamc@565
|
917 return ctx->source_count++;
|
adamc@117
|
918 }
|
adamc@117
|
919
|
adamc@577
|
920 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
|
adamc@577
|
921 int len;
|
adamc@577
|
922 size_t s_len = strlen(s);
|
adamc@577
|
923
|
adamc@577
|
924 uw_check_script(ctx, 6 + INTS_MAX + s_len);
|
adamc@666
|
925 sprintf(ctx->script.front, "s%d.v=%n", (int)n, &len);
|
adamc@666
|
926 ctx->script.front += len;
|
adamc@666
|
927 strcpy(ctx->script.front, s);
|
adamc@666
|
928 ctx->script.front += s_len;
|
adamc@666
|
929 strcpy(ctx->script.front, ";");
|
adamc@666
|
930 ctx->script.front++;
|
adamc@577
|
931
|
adamc@577
|
932 return uw_unit_v;
|
adamc@577
|
933 }
|
adamc@577
|
934
|
adamc@311
|
935 static void uw_check(uw_context ctx, size_t extra) {
|
adamc@667
|
936 buf_check(&ctx->page, extra);
|
adamc@117
|
937 }
|
adamc@117
|
938
|
adamc@311
|
939 static void uw_writec_unsafe(uw_context ctx, char c) {
|
adamc@666
|
940 *(ctx->page.front)++ = c;
|
adamc@117
|
941 }
|
adamc@117
|
942
|
adamc@311
|
943 void uw_writec(uw_context ctx, char c) {
|
adamc@311
|
944 uw_check(ctx, 1);
|
adamc@311
|
945 uw_writec_unsafe(ctx, c);
|
adamc@117
|
946 }
|
adamc@117
|
947
|
adamc@311
|
948 static void uw_write_unsafe(uw_context ctx, const char* s) {
|
adamc@117
|
949 int len = strlen(s);
|
adamc@666
|
950 memcpy(ctx->page.front, s, len);
|
adamc@666
|
951 ctx->page.front += len;
|
adamc@117
|
952 }
|
adamc@117
|
953
|
adamc@311
|
954 void uw_write(uw_context ctx, const char* s) {
|
adamc@311
|
955 uw_check(ctx, strlen(s) + 1);
|
adamc@311
|
956 uw_write_unsafe(ctx, s);
|
adamc@666
|
957 *ctx->page.front = 0;
|
adamc@102
|
958 }
|
adamc@106
|
959
|
adamc@311
|
960 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@136
|
961 char *result;
|
adamc@136
|
962 int len;
|
adamc@311
|
963 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
964 result = ctx->heap.front;
|
adamc@276
|
965 sprintf(result, "%lld%n", n, &len);
|
adamc@666
|
966 ctx->heap.front += len+1;
|
adamc@136
|
967 return result;
|
adamc@106
|
968 }
|
adamc@106
|
969
|
adamc@311
|
970 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@136
|
971 char *result;
|
adamc@136
|
972 int len;
|
adamc@311
|
973 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
974 result = ctx->heap.front;
|
adamc@136
|
975 sprintf(result, "%g%n", n, &len);
|
adamc@666
|
976 ctx->heap.front += len+1;
|
adamc@136
|
977 return result;
|
adamc@106
|
978 }
|
adamc@106
|
979
|
adamc@311
|
980 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@136
|
981 int len = strlen(s);
|
adamc@136
|
982 char *result, *p;
|
adamc@311
|
983 uw_check_heap(ctx, len * 6 + 1);
|
adamc@136
|
984
|
adamc@666
|
985 result = p = ctx->heap.front;
|
adamc@136
|
986
|
adamc@136
|
987 for (; *s; s++) {
|
adamc@136
|
988 char c = *s;
|
adamc@136
|
989
|
adamc@136
|
990 if (c == '"') {
|
adamc@136
|
991 strcpy(p, """);
|
adamc@136
|
992 p += 6;
|
adamc@136
|
993 } else if (c == '&') {
|
adamc@136
|
994 strcpy(p, "&");
|
adamc@136
|
995 p += 5;
|
adamc@136
|
996 }
|
adamc@136
|
997 else if (isprint(c))
|
adamc@136
|
998 *p++ = c;
|
adamc@136
|
999 else {
|
adamc@136
|
1000 int len2;
|
adamc@136
|
1001 sprintf(p, "&#%d;%n", c, &len2);
|
adamc@136
|
1002 p += len2;
|
adamc@136
|
1003 }
|
adamc@136
|
1004 }
|
adamc@136
|
1005
|
adamc@137
|
1006 *p++ = 0;
|
adamc@666
|
1007 ctx->heap.front = p;
|
adamc@136
|
1008 return result;
|
adamc@106
|
1009 }
|
adamc@106
|
1010
|
adamc@721
|
1011 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
|
adamc@721
|
1012 return s;
|
adamc@721
|
1013 }
|
adamc@721
|
1014
|
adamc@311
|
1015 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@117
|
1016 int len;
|
adamc@117
|
1017
|
adamc@666
|
1018 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1019 ctx->page.front += len;
|
adamc@106
|
1020 }
|
adamc@106
|
1021
|
adamc@428
|
1022 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
1023 uw_check(ctx, INTS_MAX);
|
adamc@311
|
1024 uw_Basis_attrifyInt_w_unsafe(ctx, n);
|
adamc@428
|
1025
|
adamc@428
|
1026 return uw_unit_v;
|
adamc@106
|
1027 }
|
adamc@106
|
1028
|
adamc@428
|
1029 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@117
|
1030 int len;
|
adamc@117
|
1031
|
adamc@311
|
1032 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1033 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1034 ctx->page.front += len;
|
adamc@428
|
1035
|
adamc@428
|
1036 return uw_unit_v;
|
adamc@117
|
1037 }
|
adamc@117
|
1038
|
adamc@428
|
1039 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1040 uw_check(ctx, strlen(s) * 6);
|
adamc@117
|
1041
|
adamc@106
|
1042 for (; *s; s++) {
|
adamc@106
|
1043 char c = *s;
|
adamc@106
|
1044
|
adamc@106
|
1045 if (c == '"')
|
adamc@311
|
1046 uw_write_unsafe(ctx, """);
|
adamc@136
|
1047 else if (c == '&')
|
adamc@311
|
1048 uw_write_unsafe(ctx, "&");
|
adamc@106
|
1049 else if (isprint(c))
|
adamc@311
|
1050 uw_writec_unsafe(ctx, c);
|
adamc@106
|
1051 else {
|
adamc@311
|
1052 uw_write_unsafe(ctx, "&#");
|
adamc@311
|
1053 uw_Basis_attrifyInt_w_unsafe(ctx, c);
|
adamc@311
|
1054 uw_writec_unsafe(ctx, ';');
|
adamc@106
|
1055 }
|
adamc@106
|
1056 }
|
adamc@428
|
1057
|
adamc@428
|
1058 return uw_unit_v;
|
adamc@106
|
1059 }
|
adamc@120
|
1060
|
adamc@120
|
1061
|
adamc@311
|
1062 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@137
|
1063 int len;
|
adamc@137
|
1064 char *r;
|
adamc@137
|
1065
|
adamc@311
|
1066 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1067 r = ctx->heap.front;
|
adamc@276
|
1068 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1069 ctx->heap.front += len+1;
|
adamc@137
|
1070 return r;
|
adamc@120
|
1071 }
|
adamc@120
|
1072
|
adamc@682
|
1073 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1074 if (ctx->client == NULL || chn.cli != ctx->client->id)
|
adamc@682
|
1075 return "";
|
adamc@682
|
1076 else {
|
adamc@682
|
1077 int len;
|
adamc@682
|
1078 char *r;
|
adamc@679
|
1079
|
adamc@682
|
1080 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
1081 r = ctx->heap.front;
|
adamc@682
|
1082 sprintf(r, "%u%n", chn.chn, &len);
|
adamc@682
|
1083 ctx->heap.front += len+1;
|
adamc@682
|
1084 return r;
|
adamc@682
|
1085 }
|
adamc@679
|
1086 }
|
adamc@679
|
1087
|
adamc@311
|
1088 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@137
|
1089 int len;
|
adamc@137
|
1090 char *r;
|
adamc@137
|
1091
|
adamc@311
|
1092 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1093 r = ctx->heap.front;
|
adamc@137
|
1094 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1095 ctx->heap.front += len+1;
|
adamc@137
|
1096 return r;
|
adamc@120
|
1097 }
|
adamc@120
|
1098
|
adamc@311
|
1099 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
1100 char *r, *p;
|
adamc@137
|
1101
|
adamc@311
|
1102 uw_check_heap(ctx, strlen(s) * 3 + 1);
|
adamc@137
|
1103
|
adamc@666
|
1104 for (r = p = ctx->heap.front; *s; s++) {
|
adamc@137
|
1105 char c = *s;
|
adamc@137
|
1106
|
adamc@137
|
1107 if (c == ' ')
|
adamc@137
|
1108 *p++ = '+';
|
adamc@137
|
1109 else if (isalnum(c))
|
adamc@137
|
1110 *p++ = c;
|
adamc@137
|
1111 else {
|
adamc@137
|
1112 sprintf(p, "%%%02X", c);
|
adamc@137
|
1113 p += 3;
|
adamc@137
|
1114 }
|
adamc@137
|
1115 }
|
adamc@137
|
1116
|
adamc@137
|
1117 *p++ = 0;
|
adamc@666
|
1118 ctx->heap.front = p;
|
adamc@137
|
1119 return r;
|
adamc@120
|
1120 }
|
adamc@120
|
1121
|
adamc@311
|
1122 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1123 if (b == uw_Basis_False)
|
adamc@186
|
1124 return "0";
|
adamc@186
|
1125 else
|
adamc@186
|
1126 return "1";
|
adamc@186
|
1127 }
|
adamc@186
|
1128
|
adamc@311
|
1129 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@120
|
1130 int len;
|
adamc@120
|
1131
|
adamc@666
|
1132 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1133 ctx->page.front += len;
|
adamc@120
|
1134 }
|
adamc@120
|
1135
|
adamc@428
|
1136 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
1137 uw_check(ctx, INTS_MAX);
|
adamc@311
|
1138 uw_Basis_urlifyInt_w_unsafe(ctx, n);
|
adamc@428
|
1139
|
adamc@428
|
1140 return uw_unit_v;
|
adamc@120
|
1141 }
|
adamc@120
|
1142
|
adamc@682
|
1143 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1144 if (ctx->client != NULL && chn.cli == ctx->client->id) {
|
adamc@682
|
1145 int len;
|
adamc@682
|
1146
|
adamc@682
|
1147 uw_check(ctx, INTS_MAX + 1);
|
adamc@682
|
1148 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
|
adamc@682
|
1149 ctx->page.front += len;
|
adamc@682
|
1150 }
|
adamc@682
|
1151
|
adamc@679
|
1152 return uw_unit_v;
|
adamc@679
|
1153 }
|
adamc@679
|
1154
|
adamc@428
|
1155 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@120
|
1156 int len;
|
adamc@120
|
1157
|
adamc@311
|
1158 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1159 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1160 ctx->page.front += len;
|
adamc@428
|
1161
|
adamc@428
|
1162 return uw_unit_v;
|
adamc@120
|
1163 }
|
adamc@120
|
1164
|
adamc@488
|
1165 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@488
|
1166 return uw_Basis_urlifyInt(ctx, t);
|
adamc@488
|
1167 }
|
adamc@488
|
1168
|
adamc@428
|
1169 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1170 uw_check(ctx, strlen(s) * 3);
|
adamc@120
|
1171
|
adamc@120
|
1172 for (; *s; s++) {
|
adamc@120
|
1173 char c = *s;
|
adamc@120
|
1174
|
adamc@120
|
1175 if (c == ' ')
|
adamc@311
|
1176 uw_writec_unsafe(ctx, '+');
|
adamc@120
|
1177 else if (isalnum(c))
|
adamc@311
|
1178 uw_writec_unsafe(ctx, c);
|
adamc@120
|
1179 else {
|
adamc@666
|
1180 sprintf(ctx->page.front, "%%%02X", c);
|
adamc@666
|
1181 ctx->page.front += 3;
|
adamc@120
|
1182 }
|
adamc@120
|
1183 }
|
adamc@428
|
1184
|
adamc@428
|
1185 return uw_unit_v;
|
adamc@120
|
1186 }
|
adamc@120
|
1187
|
adamc@428
|
1188 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1189 if (b == uw_Basis_False)
|
adamc@311
|
1190 uw_writec(ctx, '0');
|
adamc@186
|
1191 else
|
adamc@311
|
1192 uw_writec(ctx, '1');
|
adamc@428
|
1193
|
adamc@428
|
1194 return uw_unit_v;
|
adamc@186
|
1195 }
|
adamc@186
|
1196
|
adamc@120
|
1197
|
adamc@311
|
1198 static char *uw_unurlify_advance(char *s) {
|
adamc@144
|
1199 char *new_s = strchr(s, '/');
|
adamc@120
|
1200
|
adamc@120
|
1201 if (new_s)
|
adamc@120
|
1202 *new_s++ = 0;
|
adamc@120
|
1203 else
|
adamc@144
|
1204 new_s = strchr(s, 0);
|
adamc@144
|
1205
|
adamc@144
|
1206 return new_s;
|
adamc@144
|
1207 }
|
adamc@144
|
1208
|
adamc@311
|
1209 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
|
adamc@311
|
1210 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1211 uw_Basis_int r;
|
adamc@120
|
1212
|
adamc@276
|
1213 r = atoll(*s);
|
adamc@120
|
1214 *s = new_s;
|
adamc@120
|
1215 return r;
|
adamc@120
|
1216 }
|
adamc@120
|
1217
|
adamc@311
|
1218 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
|
adamc@311
|
1219 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1220 uw_Basis_float r;
|
adamc@120
|
1221
|
adamc@120
|
1222 r = atof(*s);
|
adamc@120
|
1223 *s = new_s;
|
adamc@120
|
1224 return r;
|
adamc@120
|
1225 }
|
adamc@120
|
1226
|
adamc@488
|
1227 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
|
adamc@488
|
1228 return uw_Basis_unurlifyInt(ctx, s);
|
adamc@488
|
1229 }
|
adamc@488
|
1230
|
adamc@311
|
1231 static uw_Basis_string uw_unurlifyString_to(uw_context ctx, char *r, char *s) {
|
adamc@144
|
1232 char *s1, *s2;
|
adamc@144
|
1233 int n;
|
adamc@136
|
1234
|
adamc@144
|
1235 for (s1 = r, s2 = s; *s2; ++s1, ++s2) {
|
adamc@136
|
1236 char c = *s2;
|
adamc@136
|
1237
|
adamc@136
|
1238 switch (c) {
|
adamc@136
|
1239 case '+':
|
adamc@136
|
1240 *s1 = ' ';
|
adamc@136
|
1241 break;
|
adamc@136
|
1242 case '%':
|
adamc@169
|
1243 if (s2[1] == 0)
|
adamc@311
|
1244 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
|
adamc@169
|
1245 if (s2[2] == 0)
|
adamc@311
|
1246 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
|
adamc@169
|
1247 if (sscanf(s2+1, "%02X", &n) != 1)
|
adamc@311
|
1248 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
|
adamc@136
|
1249 *s1 = n;
|
adamc@136
|
1250 s2 += 2;
|
adamc@136
|
1251 break;
|
adamc@136
|
1252 default:
|
adamc@136
|
1253 *s1 = c;
|
adamc@136
|
1254 }
|
adamc@136
|
1255 }
|
adamc@136
|
1256 *s1++ = 0;
|
adamc@144
|
1257 return s1;
|
adamc@144
|
1258 }
|
adamc@144
|
1259
|
adamc@311
|
1260 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
|
adamc@311
|
1261 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1262 uw_Basis_bool r;
|
adamc@186
|
1263
|
adamc@186
|
1264 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
|
adamc@311
|
1265 r = uw_Basis_False;
|
adamc@186
|
1266 else
|
adamc@311
|
1267 r = uw_Basis_True;
|
adamc@186
|
1268
|
adamc@186
|
1269 *s = new_s;
|
adamc@186
|
1270 return r;
|
adamc@186
|
1271 }
|
adamc@186
|
1272
|
adamc@311
|
1273 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
|
adamc@311
|
1274 char *new_s = uw_unurlify_advance(*s);
|
adamc@144
|
1275 char *r, *s1, *s2;
|
adamc@144
|
1276 int len, n;
|
adamc@144
|
1277
|
adamc@200
|
1278 len = strlen(*s);
|
adamc@311
|
1279 uw_check_heap(ctx, len + 1);
|
adamc@144
|
1280
|
adamc@666
|
1281 r = ctx->heap.front;
|
adamc@666
|
1282 ctx->heap.front = uw_unurlifyString_to(ctx, ctx->heap.front, *s);
|
adamc@136
|
1283 *s = new_s;
|
adamc@136
|
1284 return r;
|
adamc@120
|
1285 }
|
adamc@135
|
1286
|
adamc@135
|
1287
|
adamc@311
|
1288 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
1289 int len;
|
adamc@286
|
1290 char *r;
|
adamc@286
|
1291
|
adamc@311
|
1292 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1293 r = ctx->heap.front;
|
adamc@286
|
1294 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1295 ctx->heap.front += len+1;
|
adamc@286
|
1296 return r;
|
adamc@286
|
1297 }
|
adamc@286
|
1298
|
adamc@428
|
1299 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
1300 int len;
|
adamc@286
|
1301
|
adamc@311
|
1302 uw_check(ctx, INTS_MAX);
|
adamc@666
|
1303 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1304 ctx->page.front += len;
|
adamc@428
|
1305
|
adamc@428
|
1306 return uw_unit_v;
|
adamc@286
|
1307 }
|
adamc@286
|
1308
|
adamc@311
|
1309 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
1310 int len;
|
adamc@286
|
1311 char *r;
|
adamc@286
|
1312
|
adamc@311
|
1313 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1314 r = ctx->heap.front;
|
adamc@286
|
1315 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1316 ctx->heap.front += len+1;
|
adamc@286
|
1317 return r;
|
adamc@286
|
1318 }
|
adamc@286
|
1319
|
adamc@428
|
1320 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
1321 int len;
|
adamc@286
|
1322
|
adamc@311
|
1323 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1324 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1325 ctx->page.front += len;
|
adamc@428
|
1326
|
adamc@428
|
1327 return uw_unit_v;
|
adamc@286
|
1328 }
|
adamc@286
|
1329
|
adamc@311
|
1330 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
1331 char *r, *s2;
|
adamc@137
|
1332
|
adamc@311
|
1333 uw_check_heap(ctx, strlen(s) * 5 + 1);
|
adamc@137
|
1334
|
adamc@666
|
1335 for (r = s2 = ctx->heap.front; *s; s++) {
|
adamc@137
|
1336 char c = *s;
|
adamc@137
|
1337
|
adamc@137
|
1338 switch (c) {
|
adamc@137
|
1339 case '<':
|
adamc@137
|
1340 strcpy(s2, "<");
|
adamc@137
|
1341 s2 += 4;
|
adamc@137
|
1342 break;
|
adamc@137
|
1343 case '&':
|
adamc@137
|
1344 strcpy(s2, "&");
|
adamc@137
|
1345 s2 += 5;
|
adamc@137
|
1346 break;
|
adamc@137
|
1347 default:
|
adamc@137
|
1348 if (isprint(c))
|
adamc@137
|
1349 *s2++ = c;
|
adamc@137
|
1350 else {
|
adamc@137
|
1351 int len2;
|
adamc@137
|
1352 sprintf(s2, "&#%d;%n", c, &len2);
|
adamc@137
|
1353 s2 += len2;
|
adamc@137
|
1354 }
|
adamc@137
|
1355 }
|
adamc@137
|
1356 }
|
adamc@137
|
1357
|
adamc@137
|
1358 *s2++ = 0;
|
adamc@666
|
1359 ctx->heap.front = s2;
|
adamc@137
|
1360 return r;
|
adamc@135
|
1361 }
|
adamc@135
|
1362
|
adamc@428
|
1363 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@321
|
1364 uw_check(ctx, strlen(s) * 6);
|
adamc@135
|
1365
|
adamc@135
|
1366 for (; *s; s++) {
|
adamc@135
|
1367 char c = *s;
|
adamc@135
|
1368
|
adamc@135
|
1369 switch (c) {
|
adamc@135
|
1370 case '<':
|
adamc@311
|
1371 uw_write_unsafe(ctx, "<");
|
adamc@135
|
1372 break;
|
adamc@135
|
1373 case '&':
|
adamc@311
|
1374 uw_write_unsafe(ctx, "&");
|
adamc@135
|
1375 break;
|
adamc@135
|
1376 default:
|
adamc@135
|
1377 if (isprint(c))
|
adamc@311
|
1378 uw_writec_unsafe(ctx, c);
|
adamc@135
|
1379 else {
|
adamc@311
|
1380 uw_write_unsafe(ctx, "&#");
|
adamc@311
|
1381 uw_Basis_attrifyInt_w_unsafe(ctx, c);
|
adamc@311
|
1382 uw_writec_unsafe(ctx, ';');
|
adamc@135
|
1383 }
|
adamc@135
|
1384 }
|
adamc@135
|
1385 }
|
adamc@428
|
1386
|
adamc@428
|
1387 return uw_unit_v;
|
adamc@135
|
1388 }
|
adamc@180
|
1389
|
adamc@311
|
1390 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1391 if (b == uw_Basis_False)
|
adamc@286
|
1392 return "False";
|
adamc@286
|
1393 else
|
adamc@286
|
1394 return "True";
|
adamc@286
|
1395 }
|
adamc@286
|
1396
|
adamc@428
|
1397 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1398 if (b == uw_Basis_False) {
|
adamc@311
|
1399 uw_check(ctx, 6);
|
adamc@666
|
1400 strcpy(ctx->page.front, "False");
|
adamc@666
|
1401 ctx->page.front += 5;
|
adamc@286
|
1402 } else {
|
adamc@311
|
1403 uw_check(ctx, 5);
|
adamc@666
|
1404 strcpy(ctx->page.front, "True");
|
adamc@666
|
1405 ctx->page.front += 4;
|
adamc@286
|
1406 }
|
adamc@428
|
1407
|
adamc@428
|
1408 return uw_unit_v;
|
adamc@286
|
1409 }
|
adamc@286
|
1410
|
adamc@436
|
1411 #define TIME_FMT "%x %X"
|
adamc@438
|
1412 #define TIME_FMT_PG "%Y-%m-%d %T"
|
adamc@436
|
1413
|
adamc@436
|
1414 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
1415 size_t len;
|
adamc@436
|
1416 char *r;
|
adamc@436
|
1417 struct tm stm;
|
adamc@436
|
1418
|
adamc@436
|
1419 if (localtime_r(&t, &stm)) {
|
adamc@436
|
1420 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
1421 r = ctx->heap.front;
|
adamc@436
|
1422 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1423 ctx->heap.front += len+1;
|
adamc@436
|
1424 return r;
|
adamc@436
|
1425 } else
|
adamc@436
|
1426 return "<i>Invalid time</i>";
|
adamc@436
|
1427 }
|
adamc@436
|
1428
|
adamc@436
|
1429 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
1430 size_t len;
|
adamc@436
|
1431 char *r;
|
adamc@436
|
1432 struct tm stm;
|
adamc@436
|
1433
|
adamc@436
|
1434 if (localtime_r(&t, &stm)) {
|
adamc@436
|
1435 uw_check(ctx, TIMES_MAX);
|
adamc@666
|
1436 r = ctx->page.front;
|
adamc@436
|
1437 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1438 ctx->page.front += len;
|
adamc@436
|
1439 } else {
|
adamc@436
|
1440 uw_check(ctx, 20);
|
adamc@666
|
1441 strcpy(ctx->page.front, "<i>Invalid time</i>");
|
adamc@666
|
1442 ctx->page.front += 19;
|
adamc@436
|
1443 }
|
adamc@436
|
1444
|
adamc@436
|
1445 return uw_unit_v;
|
adamc@436
|
1446 }
|
adamc@436
|
1447
|
adamc@311
|
1448 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
|
adamc@180
|
1449 int len = strlen(s1) + strlen(s2) + 1;
|
adamc@180
|
1450 char *s;
|
adamc@180
|
1451
|
adamc@311
|
1452 uw_check_heap(ctx, len);
|
adamc@180
|
1453
|
adamc@666
|
1454 s = ctx->heap.front;
|
adamc@180
|
1455
|
adamc@180
|
1456 strcpy(s, s1);
|
adamc@180
|
1457 strcat(s, s2);
|
adamc@666
|
1458 ctx->heap.front += len;
|
adamc@180
|
1459
|
adamc@180
|
1460 return s;
|
adamc@180
|
1461 }
|
adamc@278
|
1462
|
adamc@737
|
1463 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@278
|
1464 int len = strlen(s1) + 1;
|
adamc@278
|
1465 char *s;
|
adamc@278
|
1466
|
adamc@311
|
1467 uw_check_heap(ctx, len);
|
adamc@278
|
1468
|
adamc@666
|
1469 s = ctx->heap.front;
|
adamc@278
|
1470
|
adamc@278
|
1471 strcpy(s, s1);
|
adamc@666
|
1472 ctx->heap.front += len;
|
adamc@278
|
1473
|
adamc@278
|
1474 return s;
|
adamc@278
|
1475 }
|
adamc@280
|
1476
|
adamc@737
|
1477 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@493
|
1478 if (s1)
|
adamc@737
|
1479 return uw_strdup(ctx, s1);
|
adamc@493
|
1480 else
|
adamc@493
|
1481 return NULL;
|
adamc@493
|
1482 }
|
adamc@493
|
1483
|
adamc@280
|
1484
|
adamc@311
|
1485 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@281
|
1486 int len;
|
adamc@281
|
1487 char *r;
|
adamc@281
|
1488
|
adamc@311
|
1489 uw_check_heap(ctx, INTS_MAX + 6);
|
adamc@666
|
1490 r = ctx->heap.front;
|
adamc@281
|
1491 sprintf(r, "%lld::int8%n", n, &len);
|
adamc@666
|
1492 ctx->heap.front += len+1;
|
adamc@281
|
1493 return r;
|
adamc@281
|
1494 }
|
adamc@281
|
1495
|
adamc@467
|
1496 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
|
adamc@467
|
1497 if (n == NULL)
|
adamc@467
|
1498 return "NULL";
|
adamc@467
|
1499 else
|
adamc@467
|
1500 return uw_Basis_sqlifyInt(ctx, *n);
|
adamc@467
|
1501 }
|
adamc@467
|
1502
|
adamc@311
|
1503 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@281
|
1504 int len;
|
adamc@281
|
1505 char *r;
|
adamc@281
|
1506
|
adamc@311
|
1507 uw_check_heap(ctx, FLOATS_MAX + 8);
|
adamc@666
|
1508 r = ctx->heap.front;
|
adamc@281
|
1509 sprintf(r, "%g::float8%n", n, &len);
|
adamc@666
|
1510 ctx->heap.front += len+1;
|
adamc@281
|
1511 return r;
|
adamc@281
|
1512 }
|
adamc@281
|
1513
|
adamc@467
|
1514 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
|
adamc@467
|
1515 if (n == NULL)
|
adamc@467
|
1516 return "NULL";
|
adamc@467
|
1517 else
|
adamc@467
|
1518 return uw_Basis_sqlifyFloat(ctx, *n);
|
adamc@467
|
1519 }
|
adamc@467
|
1520
|
adamc@281
|
1521
|
adamc@311
|
1522 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@280
|
1523 char *r, *s2;
|
adamc@280
|
1524
|
adamc@311
|
1525 uw_check_heap(ctx, strlen(s) * 2 + 10);
|
adamc@280
|
1526
|
adamc@666
|
1527 r = s2 = ctx->heap.front;
|
adamc@280
|
1528 *s2++ = 'E';
|
adamc@280
|
1529 *s2++ = '\'';
|
adamc@280
|
1530
|
adamc@280
|
1531 for (; *s; s++) {
|
adamc@280
|
1532 char c = *s;
|
adamc@280
|
1533
|
adamc@280
|
1534 switch (c) {
|
adamc@280
|
1535 case '\'':
|
adamc@280
|
1536 strcpy(s2, "\\'");
|
adamc@280
|
1537 s2 += 2;
|
adamc@280
|
1538 break;
|
adamc@280
|
1539 case '\\':
|
adamc@280
|
1540 strcpy(s2, "\\\\");
|
adamc@280
|
1541 s2 += 2;
|
adamc@280
|
1542 break;
|
adamc@280
|
1543 default:
|
adamc@280
|
1544 if (isprint(c))
|
adamc@280
|
1545 *s2++ = c;
|
adamc@280
|
1546 else {
|
adamc@737
|
1547 sprintf(s2, "\\%03o", c);
|
adamc@280
|
1548 s2 += 4;
|
adamc@280
|
1549 }
|
adamc@280
|
1550 }
|
adamc@280
|
1551 }
|
adamc@280
|
1552
|
adamc@281
|
1553 strcpy(s2, "'::text");
|
adamc@666
|
1554 ctx->heap.front = s2 + 8;
|
adamc@280
|
1555 return r;
|
adamc@280
|
1556 }
|
adamc@281
|
1557
|
adamc@737
|
1558 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
|
adamc@737
|
1559 char *r, *s2;
|
adamc@737
|
1560 size_t i;
|
adamc@737
|
1561
|
adamc@737
|
1562 uw_check_heap(ctx, b.size * 5 + 11);
|
adamc@737
|
1563
|
adamc@737
|
1564 r = s2 = ctx->heap.front;
|
adamc@737
|
1565 *s2++ = 'E';
|
adamc@737
|
1566 *s2++ = '\'';
|
adamc@737
|
1567
|
adamc@737
|
1568 for (i = 0; i < b.size; ++i) {
|
adamc@737
|
1569 char c = b.data[i];
|
adamc@737
|
1570
|
adamc@737
|
1571 switch (c) {
|
adamc@737
|
1572 case '\'':
|
adamc@737
|
1573 strcpy(s2, "\\'");
|
adamc@737
|
1574 s2 += 2;
|
adamc@737
|
1575 break;
|
adamc@737
|
1576 case '\\':
|
adamc@737
|
1577 strcpy(s2, "\\\\\\\\");
|
adamc@737
|
1578 s2 += 4;
|
adamc@737
|
1579 break;
|
adamc@737
|
1580 default:
|
adamc@737
|
1581 if (isprint(c))
|
adamc@737
|
1582 *s2++ = c;
|
adamc@737
|
1583 else {
|
adamc@737
|
1584 sprintf(s2, "\\\\%03o", c);
|
adamc@737
|
1585 s2 += 5;
|
adamc@737
|
1586 }
|
adamc@737
|
1587 }
|
adamc@737
|
1588 }
|
adamc@737
|
1589
|
adamc@737
|
1590 strcpy(s2, "'::bytea");
|
adamc@737
|
1591 ctx->heap.front = s2 + 9;
|
adamc@737
|
1592 return r;
|
adamc@737
|
1593 }
|
adamc@737
|
1594
|
adamc@682
|
1595 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1596 int len;
|
adamc@682
|
1597 char *r;
|
adamc@682
|
1598 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
|
adamc@682
|
1599
|
adamc@682
|
1600 uw_check_heap(ctx, INTS_MAX + 7);
|
adamc@682
|
1601 r = ctx->heap.front;
|
adamc@682
|
1602 sprintf(r, "%lld::int8%n", combo, &len);
|
adamc@682
|
1603 ctx->heap.front += len+1;
|
adamc@682
|
1604 return r;
|
adamc@682
|
1605 }
|
adamc@682
|
1606
|
adamc@682
|
1607 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1608 int len;
|
adamc@682
|
1609 char *r;
|
adamc@682
|
1610 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
|
adamc@682
|
1611
|
adamc@682
|
1612 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
1613 r = ctx->heap.front;
|
adamc@682
|
1614 sprintf(r, "%lld%n", combo, &len);
|
adamc@682
|
1615 ctx->heap.front += len+1;
|
adamc@682
|
1616 return r;
|
adamc@682
|
1617 }
|
adamc@682
|
1618
|
adamc@682
|
1619 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
|
adamc@682
|
1620 int len;
|
adamc@682
|
1621 char *r;
|
adamc@682
|
1622
|
adamc@682
|
1623 uw_check_heap(ctx, INTS_MAX + 7);
|
adamc@682
|
1624 r = ctx->heap.front;
|
adamc@682
|
1625 sprintf(r, "%u::int4%n", cli, &len);
|
adamc@682
|
1626 ctx->heap.front += len+1;
|
adamc@682
|
1627 return r;
|
adamc@682
|
1628 }
|
adamc@682
|
1629
|
adamc@682
|
1630 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
|
adamc@682
|
1631 int len;
|
adamc@682
|
1632 char *r;
|
adamc@682
|
1633
|
adamc@682
|
1634 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
1635 r = ctx->heap.front;
|
adamc@682
|
1636 sprintf(r, "%u%n", cli, &len);
|
adamc@682
|
1637 ctx->heap.front += len+1;
|
adamc@682
|
1638 return r;
|
adamc@682
|
1639 }
|
adamc@682
|
1640
|
adamc@467
|
1641 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
|
adamc@467
|
1642 if (s == NULL)
|
adamc@467
|
1643 return "NULL";
|
adamc@467
|
1644 else
|
adamc@467
|
1645 return uw_Basis_sqlifyString(ctx, s);
|
adamc@467
|
1646 }
|
adamc@467
|
1647
|
adamc@311
|
1648 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1649 if (b == uw_Basis_False)
|
adamc@281
|
1650 return "FALSE";
|
adamc@281
|
1651 else
|
adamc@281
|
1652 return "TRUE";
|
adamc@281
|
1653 }
|
adamc@282
|
1654
|
adamc@467
|
1655 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
|
adamc@467
|
1656 if (b == NULL)
|
adamc@467
|
1657 return "NULL";
|
adamc@467
|
1658 else
|
adamc@467
|
1659 return uw_Basis_sqlifyBool(ctx, *b);
|
adamc@467
|
1660 }
|
adamc@467
|
1661
|
adamc@439
|
1662 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@439
|
1663 size_t len;
|
adamc@682
|
1664 char *r, *s;
|
adamc@682
|
1665 struct tm stm;
|
adamc@682
|
1666
|
adamc@682
|
1667 if (localtime_r(&t, &stm)) {
|
adamc@682
|
1668 s = uw_malloc(ctx, TIMES_MAX);
|
adamc@682
|
1669 len = strftime(s, TIMES_MAX, TIME_FMT, &stm);
|
adamc@682
|
1670 r = uw_malloc(ctx, len + 14);
|
adamc@682
|
1671 sprintf(r, "'%s'::timestamp", s);
|
adamc@682
|
1672 return r;
|
adamc@682
|
1673 } else
|
adamc@682
|
1674 return "<Invalid time>";
|
adamc@682
|
1675 }
|
adamc@682
|
1676
|
adamc@682
|
1677 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@682
|
1678 size_t len;
|
adamc@439
|
1679 char *r;
|
adamc@439
|
1680 struct tm stm;
|
adamc@439
|
1681
|
adamc@439
|
1682 if (localtime_r(&t, &stm)) {
|
adamc@439
|
1683 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
1684 r = ctx->heap.front;
|
adamc@439
|
1685 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1686 ctx->heap.front += len+1;
|
adamc@439
|
1687 return r;
|
adamc@439
|
1688 } else
|
adamc@439
|
1689 return "<Invalid time>";
|
adamc@439
|
1690 }
|
adamc@439
|
1691
|
adamc@467
|
1692 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
|
adamc@467
|
1693 if (t == NULL)
|
adamc@467
|
1694 return "NULL";
|
adamc@467
|
1695 else
|
adamc@467
|
1696 return uw_Basis_sqlifyTime(ctx, *t);
|
adamc@467
|
1697 }
|
adamc@467
|
1698
|
adamc@311
|
1699 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
|
adamc@311
|
1700 static uw_Basis_int true = 1;
|
adamc@311
|
1701 static uw_Basis_int false = 0;
|
adamc@282
|
1702
|
adamc@311
|
1703 if (b == uw_Basis_False)
|
adamc@282
|
1704 return (char *)&false;
|
adamc@282
|
1705 else
|
adamc@282
|
1706 return (char *)&true;
|
adamc@282
|
1707 }
|
adamc@284
|
1708
|
adamc@311
|
1709 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
|
adamc@284
|
1710 int len;
|
adamc@284
|
1711 char *r;
|
adamc@284
|
1712
|
adamc@311
|
1713 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1714 r = ctx->heap.front;
|
adamc@284
|
1715 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1716 ctx->heap.front += len+1;
|
adamc@284
|
1717 return r;
|
adamc@284
|
1718 }
|
adamc@285
|
1719
|
adamc@311
|
1720 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
|
adamc@285
|
1721 int len;
|
adamc@285
|
1722 char *r;
|
adamc@285
|
1723
|
adamc@311
|
1724 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1725 r = ctx->heap.front;
|
adamc@285
|
1726 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1727 ctx->heap.front += len+1;
|
adamc@285
|
1728 return r;
|
adamc@285
|
1729 }
|
adamc@285
|
1730
|
adamc@311
|
1731 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1732 if (b == uw_Basis_False)
|
adamc@285
|
1733 return "False";
|
adamc@285
|
1734 else
|
adamc@285
|
1735 return "True";
|
adamc@285
|
1736 }
|
adamc@288
|
1737
|
adamc@436
|
1738 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
1739 size_t len;
|
adamc@436
|
1740 char *r;
|
adamc@436
|
1741 struct tm stm;
|
adamc@436
|
1742
|
adamc@436
|
1743 if (localtime_r(&t, &stm)) {
|
adamc@436
|
1744 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
1745 r = ctx->heap.front;
|
adamc@436
|
1746 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1747 ctx->heap.front += len+1;
|
adamc@436
|
1748 return r;
|
adamc@436
|
1749 } else
|
adamc@436
|
1750 return "<Invalid time>";
|
adamc@436
|
1751 }
|
adamc@288
|
1752
|
adamc@311
|
1753 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
|
adamc@288
|
1754 char *endptr;
|
adamc@311
|
1755 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@288
|
1756
|
adamc@288
|
1757 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
1758 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adamc@288
|
1759 *r = n;
|
adamc@288
|
1760 return r;
|
adamc@288
|
1761 } else
|
adamc@288
|
1762 return NULL;
|
adamc@288
|
1763 }
|
adamc@289
|
1764
|
adamc@311
|
1765 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
|
adamc@289
|
1766 char *endptr;
|
adamc@311
|
1767 uw_Basis_float n = strtod(s, &endptr);
|
adamc@289
|
1768
|
adamc@289
|
1769 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
1770 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
|
adamc@289
|
1771 *r = n;
|
adamc@289
|
1772 return r;
|
adamc@289
|
1773 } else
|
adamc@289
|
1774 return NULL;
|
adamc@289
|
1775 }
|
adamc@289
|
1776
|
adamc@311
|
1777 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1778 static uw_Basis_bool true = uw_Basis_True;
|
adamc@311
|
1779 static uw_Basis_bool false = uw_Basis_False;
|
adamc@289
|
1780
|
adamc@289
|
1781 if (!strcasecmp (s, "True"))
|
adamc@289
|
1782 return &true;
|
adamc@289
|
1783 else if (!strcasecmp (s, "False"))
|
adamc@289
|
1784 return &false;
|
adamc@289
|
1785 else
|
adamc@289
|
1786 return NULL;
|
adamc@289
|
1787 }
|
adamc@292
|
1788
|
adamc@436
|
1789 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
1790 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
1791 struct tm stm;
|
adamc@436
|
1792
|
adamc@439
|
1793 if (dot) {
|
adamc@439
|
1794 *dot = 0;
|
adamc@439
|
1795 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
1796 *dot = '.';
|
adamc@439
|
1797 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1798 *r = mktime(&stm);
|
adamc@439
|
1799 return r;
|
adamc@439
|
1800 }
|
adamc@439
|
1801 else {
|
adamc@439
|
1802 *dot = '.';
|
adamc@439
|
1803 return NULL;
|
adamc@439
|
1804 }
|
adamc@436
|
1805 }
|
adamc@439
|
1806 else {
|
adamc@439
|
1807 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
1808 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1809 *r = mktime(&stm);
|
adamc@439
|
1810 return r;
|
adamc@439
|
1811 }
|
adamc@439
|
1812 else if (strptime(s, TIME_FMT, &stm) == end) {
|
adamc@439
|
1813 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1814 *r = mktime(&stm);
|
adamc@439
|
1815 return r;
|
adamc@439
|
1816 }
|
adamc@439
|
1817 else
|
adamc@439
|
1818 return NULL;
|
adamc@439
|
1819 }
|
adamc@436
|
1820 }
|
adamc@436
|
1821
|
adamc@311
|
1822 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
|
adamc@292
|
1823 char *endptr;
|
adamc@311
|
1824 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@292
|
1825
|
adamc@292
|
1826 if (*s != '\0' && *endptr == '\0')
|
adamc@292
|
1827 return n;
|
adamc@292
|
1828 else
|
adamc@311
|
1829 uw_error(ctx, FATAL, "Can't parse int: %s", s);
|
adamc@292
|
1830 }
|
adamc@293
|
1831
|
adamc@682
|
1832 #include <errno.h>
|
adamc@682
|
1833
|
adamc@678
|
1834 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
|
adamc@682
|
1835 unsigned long long n;
|
adamc@682
|
1836
|
adamc@682
|
1837 if (sscanf(s, "%llu", &n) < 1)
|
adamc@682
|
1838 uw_error(ctx, FATAL, "Can't parse channel: %s", s);
|
adamc@682
|
1839 else {
|
adamc@682
|
1840 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
|
adamc@682
|
1841 return ch;
|
adamc@682
|
1842 }
|
adamc@682
|
1843 }
|
adamc@682
|
1844
|
adamc@682
|
1845 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
|
adamc@678
|
1846 char *endptr;
|
adamc@682
|
1847 unsigned long n = strtoul(s, &endptr, 10);
|
adamc@678
|
1848
|
adamc@678
|
1849 if (*s != '\0' && *endptr == '\0')
|
adamc@678
|
1850 return n;
|
adamc@678
|
1851 else
|
adamc@682
|
1852 uw_error(ctx, FATAL, "Can't parse client: %s", s);
|
adamc@678
|
1853 }
|
adamc@678
|
1854
|
adamc@311
|
1855 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
|
adamc@293
|
1856 char *endptr;
|
adamc@311
|
1857 uw_Basis_float n = strtod(s, &endptr);
|
adamc@293
|
1858
|
adamc@293
|
1859 if (*s != '\0' && *endptr == '\0')
|
adamc@293
|
1860 return n;
|
adamc@293
|
1861 else
|
adamc@311
|
1862 uw_error(ctx, FATAL, "Can't parse float: %s", s);
|
adamc@293
|
1863 }
|
adamc@293
|
1864
|
adamc@311
|
1865 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
|
adamc@296
|
1866 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
|
adamc@311
|
1867 return uw_Basis_True;
|
adamc@296
|
1868 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
|
adamc@311
|
1869 return uw_Basis_False;
|
adamc@293
|
1870 else
|
adamc@311
|
1871 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
|
adamc@293
|
1872 }
|
adamc@436
|
1873
|
adamc@436
|
1874 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
1875 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
1876 struct tm stm = {};
|
adamc@436
|
1877
|
adamc@438
|
1878 if (dot) {
|
adamc@438
|
1879 *dot = 0;
|
adamc@438
|
1880 if (strptime(s, TIME_FMT_PG, &stm)) {
|
adamc@438
|
1881 *dot = '.';
|
adamc@438
|
1882 return mktime(&stm);
|
adamc@438
|
1883 }
|
adamc@438
|
1884 else {
|
adamc@438
|
1885 *dot = '.';
|
adamc@438
|
1886 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
1887 }
|
adamc@438
|
1888 }
|
adamc@438
|
1889 else {
|
adamc@439
|
1890 if (strptime(s, TIME_FMT_PG, &stm) == end)
|
adamc@439
|
1891 return mktime(&stm);
|
adamc@439
|
1892 else if (strptime(s, TIME_FMT, &stm) == end)
|
adamc@438
|
1893 return mktime(&stm);
|
adamc@438
|
1894 else
|
adamc@438
|
1895 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
1896 }
|
adamc@436
|
1897 }
|
adamc@457
|
1898
|
adamc@463
|
1899 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
|
adamc@463
|
1900 int len = strlen(c);
|
adamc@666
|
1901 char *s = ctx->headers, *p = ctx->outHeaders.start;
|
adamc@474
|
1902
|
adamc@474
|
1903 while (p = strstr(p, "\nSet-Cookie: ")) {
|
adamc@474
|
1904 char *p2;
|
adamc@474
|
1905 p += 13;
|
adamc@474
|
1906 p2 = strchr(p, '=');
|
adamc@474
|
1907
|
adamc@474
|
1908 if (p2) {
|
adamc@474
|
1909 size_t sz = strcspn(p2+1, ";\r\n");
|
adamc@474
|
1910
|
adamc@474
|
1911 if (!strncasecmp(p, c, p2 - p)) {
|
adamc@474
|
1912 char *ret = uw_malloc(ctx, sz + 1);
|
adamc@474
|
1913 memcpy(ret, p2+1, sz);
|
adamc@474
|
1914 ret[sz] = 0;
|
adamc@474
|
1915 return ret;
|
adamc@474
|
1916 }
|
adamc@474
|
1917 }
|
adamc@474
|
1918 }
|
adamc@463
|
1919
|
adamc@463
|
1920 while (p = strchr(s, ':')) {
|
adamc@476
|
1921 if (!strncasecmp(s, "Cookie: ", 8)) {
|
adamc@476
|
1922 p += 2;
|
adamc@476
|
1923 while (1) {
|
adamc@476
|
1924 if (!strncmp(p, c, len)
|
adamc@476
|
1925 && p + len < ctx->headers_end && p[len] == '=')
|
adamc@476
|
1926 return p + 1 + len;
|
adamc@476
|
1927 else if (p = strchr(p, ';'))
|
adamc@476
|
1928 p += 2;
|
adamc@476
|
1929 else if ((s = strchr(s, 0)) && s < ctx->headers_end) {
|
adamc@476
|
1930 s += 2;
|
adamc@476
|
1931 break;
|
adamc@476
|
1932 }
|
adamc@476
|
1933 else
|
adamc@476
|
1934 return NULL;
|
adamc@476
|
1935 }
|
adamc@463
|
1936 } else {
|
adamc@463
|
1937 if ((s = strchr(p, 0)) && s < ctx->headers_end)
|
adamc@463
|
1938 s += 2;
|
adamc@463
|
1939 else
|
adamc@463
|
1940 return NULL;
|
adamc@463
|
1941 }
|
adamc@463
|
1942 }
|
adamc@457
|
1943 }
|
adamc@462
|
1944
|
adamc@466
|
1945 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v) {
|
adamc@462
|
1946 uw_write_header(ctx, "Set-Cookie: ");
|
adamc@462
|
1947 uw_write_header(ctx, c);
|
adamc@462
|
1948 uw_write_header(ctx, "=");
|
adamc@462
|
1949 uw_write_header(ctx, v);
|
adamc@466
|
1950 uw_write_header(ctx, "; path=");
|
adamc@466
|
1951 uw_write_header(ctx, prefix);
|
adamc@462
|
1952 uw_write_header(ctx, "\r\n");
|
adamc@462
|
1953
|
adamc@462
|
1954 return uw_unit_v;
|
adamc@462
|
1955 }
|
adamc@667
|
1956
|
adamc@682
|
1957 static delta *allocate_delta(uw_context ctx, unsigned client) {
|
adamc@682
|
1958 unsigned i;
|
adamc@682
|
1959 delta *d;
|
adamc@671
|
1960
|
adamc@682
|
1961 for (i = 0; i < ctx->used_deltas; ++i)
|
adamc@682
|
1962 if (ctx->deltas[i].client == client)
|
adamc@682
|
1963 return &ctx->deltas[i];
|
adamc@671
|
1964
|
adamc@682
|
1965 if (ctx->used_deltas >= ctx->n_deltas) {
|
adamc@682
|
1966 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
|
adamc@682
|
1967 buf_init(&ctx->deltas[ctx->n_deltas-1].msgs, 0);
|
adamc@682
|
1968 }
|
adamc@671
|
1969
|
adamc@682
|
1970 d = &ctx->deltas[ctx->used_deltas++];
|
adamc@682
|
1971 d->client = client;
|
adamc@682
|
1972 buf_reset(&d->msgs);
|
adamc@682
|
1973 return d;
|
adamc@671
|
1974 }
|
adamc@671
|
1975
|
adamc@668
|
1976 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
|
adamc@682
|
1977 if (ctx->client == NULL)
|
adamc@682
|
1978 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
|
adamc@671
|
1979
|
adamc@682
|
1980 return new_channel(ctx->client);
|
adamc@668
|
1981 }
|
adamc@668
|
1982
|
adamc@668
|
1983 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
|
adamc@682
|
1984 delta *d = allocate_delta(ctx, chn.cli);
|
adamc@682
|
1985 size_t len;
|
adamc@682
|
1986 int preLen;
|
adamc@682
|
1987 char pre[INTS_MAX + 2];
|
adamc@668
|
1988
|
adamc@682
|
1989 len = strlen(msg);
|
adamc@682
|
1990
|
adamc@682
|
1991 sprintf(pre, "%u\n%n", chn.chn, &preLen);
|
adamc@682
|
1992
|
adamc@682
|
1993 buf_append(&d->msgs, pre, preLen);
|
adamc@682
|
1994 buf_append(&d->msgs, msg, len);
|
adamc@682
|
1995 buf_append(&d->msgs, "\n", 1);
|
adamc@671
|
1996
|
adamc@671
|
1997 return uw_unit_v;
|
adamc@668
|
1998 }
|
adamc@671
|
1999
|
adamc@671
|
2000 int uw_db_commit(uw_context);
|
adamc@671
|
2001 int uw_db_rollback(uw_context);
|
adamc@671
|
2002
|
adamc@671
|
2003 void uw_commit(uw_context ctx) {
|
adamc@682
|
2004 unsigned i;
|
adamc@671
|
2005
|
adamc@700
|
2006 if (uw_db_commit(ctx))
|
adamc@671
|
2007 uw_error(ctx, FATAL, "Error running SQL COMMIT");
|
adamc@682
|
2008
|
adamc@682
|
2009 for (i = 0; i < ctx->used_deltas; ++i) {
|
adamc@682
|
2010 delta *d = &ctx->deltas[i];
|
adamc@682
|
2011 client *c = find_client(d->client);
|
adamc@682
|
2012
|
adamc@682
|
2013 assert (c != NULL && c->mode == USED);
|
adamc@682
|
2014
|
adamc@685
|
2015 client_send(c, &d->msgs);
|
adamc@682
|
2016 }
|
adamc@682
|
2017
|
adamc@682
|
2018 if (ctx->client)
|
adamc@685
|
2019 release_client(ctx->client);
|
adamc@671
|
2020 }
|
adamc@671
|
2021
|
adamc@671
|
2022 int uw_rollback(uw_context ctx) {
|
adamc@682
|
2023 if (ctx->client)
|
adamc@685
|
2024 release_client(ctx->client);
|
adamc@671
|
2025
|
adamc@671
|
2026 return uw_db_rollback(ctx);
|
adamc@671
|
2027 }
|
adamc@683
|
2028
|
adamc@683
|
2029
|
adamc@683
|
2030 // "Garbage collection"
|
adamc@683
|
2031
|
adamc@683
|
2032 void uw_expunger(uw_context ctx, uw_Basis_client cli);
|
adamc@683
|
2033
|
adamc@683
|
2034 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli) {
|
adamc@683
|
2035 int r = setjmp(ctx->jmp_buf);
|
adamc@683
|
2036
|
adamc@684
|
2037 if (r == 0) {
|
adamc@684
|
2038 if (uw_db_begin(ctx))
|
adamc@684
|
2039 uw_error(ctx, FATAL, "Error running SQL BEGIN");
|
adamc@683
|
2040 uw_expunger(ctx, cli);
|
adamc@684
|
2041 if (uw_db_commit(ctx))
|
adamc@684
|
2042 uw_error(ctx, FATAL, "Error running SQL COMMIT");
|
adamc@684
|
2043 }
|
adamc@683
|
2044
|
adamc@683
|
2045 return r;
|
adamc@683
|
2046 }
|
adamc@683
|
2047
|
adamc@683
|
2048 void uw_prune_clients(uw_context ctx) {
|
adamc@683
|
2049 client *c, *next, *prev = NULL;
|
adamc@683
|
2050 time_t cutoff;
|
adamc@683
|
2051
|
adamc@683
|
2052 cutoff = time(NULL) - uw_timeout;
|
adamc@683
|
2053
|
adamc@683
|
2054 pthread_mutex_lock(&clients_mutex);
|
adamc@683
|
2055
|
adamc@683
|
2056 for (c = clients_used; c; c = next) {
|
adamc@683
|
2057 next = c->next;
|
adamc@683
|
2058 pthread_mutex_lock(&c->lock);
|
adamc@685
|
2059 if (c->last_contact < cutoff && c->refcount == 0) {
|
adamc@683
|
2060 failure_kind fk = UNLIMITED_RETRY;
|
adamc@683
|
2061 if (prev)
|
adamc@683
|
2062 prev->next = next;
|
adamc@683
|
2063 else
|
adamc@683
|
2064 clients_used = next;
|
adamc@684
|
2065 uw_reset(ctx);
|
adamc@683
|
2066 while (fk == UNLIMITED_RETRY) {
|
adamc@683
|
2067 fk = uw_expunge(ctx, c->id);
|
adamc@684
|
2068 if (fk == UNLIMITED_RETRY) {
|
adamc@684
|
2069 uw_db_rollback(ctx);
|
adamc@684
|
2070 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
|
adamc@683
|
2071 }
|
adamc@683
|
2072 }
|
adamc@684
|
2073 if (fk == SUCCESS)
|
adamc@684
|
2074 free_client(c);
|
adamc@684
|
2075 else {
|
adamc@684
|
2076 uw_db_rollback(ctx);
|
adamc@683
|
2077 printf("Expunge blocked by error: %s\n", uw_error_message(ctx));
|
adamc@684
|
2078 }
|
adamc@683
|
2079 }
|
adamc@683
|
2080 else
|
adamc@683
|
2081 prev = c;
|
adamc@683
|
2082 pthread_mutex_unlock(&c->lock);
|
adamc@683
|
2083 }
|
adamc@683
|
2084
|
adamc@683
|
2085 pthread_mutex_unlock(&clients_mutex);
|
adamc@683
|
2086 }
|
adamc@687
|
2087
|
adamc@687
|
2088 void uw_initializer(uw_context ctx);
|
adamc@687
|
2089
|
adamc@687
|
2090 failure_kind uw_initialize(uw_context ctx) {
|
adamc@687
|
2091 int r = setjmp(ctx->jmp_buf);
|
adamc@687
|
2092
|
adamc@687
|
2093 if (r == 0) {
|
adamc@687
|
2094 if (uw_db_begin(ctx))
|
adamc@687
|
2095 uw_error(ctx, FATAL, "Error running SQL BEGIN");
|
adamc@687
|
2096 uw_initializer(ctx);
|
adamc@687
|
2097 if (uw_db_commit(ctx))
|
adamc@687
|
2098 uw_error(ctx, FATAL, "Error running SQL COMMIT");
|
adamc@687
|
2099 }
|
adamc@687
|
2100
|
adamc@687
|
2101 return r;
|
adamc@687
|
2102 }
|
adamc@717
|
2103
|
adamc@717
|
2104 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
|
adamc@717
|
2105 return s;
|
adamc@717
|
2106 }
|
adamc@734
|
2107
|
adamc@741
|
2108 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
|
adamc@741
|
2109 char *s2;
|
adamc@741
|
2110
|
adamc@741
|
2111 for (s2 = s; *s2; ++s2)
|
adamc@741
|
2112 if (!isalnum(*s2) && *s2 != '/' && *s2 != '-' && *s2 != '.')
|
adamc@741
|
2113 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character %c\n", s, *s2);
|
adamc@741
|
2114
|
adamc@741
|
2115 return s;
|
adamc@741
|
2116 }
|
adamc@741
|
2117
|
adamc@734
|
2118 uw_Basis_string uw_unnull(uw_Basis_string s) {
|
adamc@734
|
2119 return s ? s : "";
|
adamc@734
|
2120 }
|
adamc@734
|
2121
|
adamc@734
|
2122 extern int uw_hash_blocksize;
|
adamc@734
|
2123
|
adamc@734
|
2124 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
|
adamc@734
|
2125 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
|
adamc@734
|
2126 int i;
|
adamc@734
|
2127
|
adamc@734
|
2128 for (i = 0; i < uw_hash_blocksize; ++i)
|
adamc@734
|
2129 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
|
adamc@734
|
2130
|
adamc@734
|
2131 return r;
|
adamc@734
|
2132 }
|
adamc@734
|
2133
|
adamc@734
|
2134 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
|
adamc@734
|
2135 return uw_cookie_sig(ctx);
|
adamc@734
|
2136 }
|
adamc@737
|
2137
|
adamc@737
|
2138 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
|
adamc@737
|
2139 return f.name;
|
adamc@737
|
2140 }
|
adamc@737
|
2141
|
adamc@740
|
2142 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
|
adamc@740
|
2143 return f.type;
|
adamc@740
|
2144 }
|
adamc@740
|
2145
|
adamc@737
|
2146 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
|
adamc@737
|
2147 return f.data;
|
adamc@737
|
2148 }
|
adamc@741
|
2149
|
adamc@741
|
2150 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
|
adamc@741
|
2151 cleanup *cl;
|
adamc@741
|
2152 int len;
|
adamc@741
|
2153
|
adamc@741
|
2154 buf_reset(&ctx->outHeaders);
|
adamc@741
|
2155 buf_reset(&ctx->page);
|
adamc@741
|
2156
|
adamc@741
|
2157 uw_write_header(ctx, "HTTP/1.1 200 OK\r\nContent-Type: ");
|
adamc@741
|
2158 uw_write_header(ctx, mimeType);
|
adamc@741
|
2159 uw_write_header(ctx, "\r\nContent-Length: ");
|
adamc@741
|
2160 buf_check(&ctx->outHeaders, INTS_MAX);
|
adamc@741
|
2161 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len);
|
adamc@741
|
2162 ctx->outHeaders.front += len;
|
adamc@741
|
2163 uw_write_header(ctx, "\r\n");
|
adamc@741
|
2164
|
adamc@741
|
2165 buf_append(&ctx->page, b.data, b.size);
|
adamc@741
|
2166
|
adamc@741
|
2167 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@741
|
2168 cl->func(cl->arg);
|
adamc@741
|
2169
|
adamc@741
|
2170 ctx->cleanup_front = ctx->cleanup;
|
adamc@741
|
2171
|
adamc@741
|
2172 longjmp(ctx->jmp_buf, RETURN_BLOB);
|
adamc@741
|
2173 }
|