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