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