adamc@436
|
1 #define _XOPEN_SOURCE
|
adamc@436
|
2
|
adamc@117
|
3 #include <stdlib.h>
|
adamc@102
|
4 #include <stdio.h>
|
adamc@117
|
5 #include <string.h>
|
adamc@457
|
6 #include <strings.h>
|
adamc@106
|
7 #include <ctype.h>
|
adamc@167
|
8 #include <setjmp.h>
|
adamc@167
|
9 #include <stdarg.h>
|
adamc@102
|
10
|
adamc@102
|
11 #include "types.h"
|
adamc@102
|
12
|
adamc@311
|
13 uw_unit uw_unit_v = {};
|
adamc@102
|
14
|
adamc@167
|
15 #define ERROR_BUF_LEN 1024
|
adamc@167
|
16
|
adamc@323
|
17 typedef struct regions {
|
adamc@323
|
18 struct regions *next;
|
adamc@323
|
19 } regions;
|
adamc@323
|
20
|
adamc@425
|
21 typedef struct {
|
adamc@425
|
22 void (*func)(void*);
|
adamc@425
|
23 void *arg;
|
adamc@425
|
24 } cleanup;
|
adamc@425
|
25
|
adamc@666
|
26 typedef struct {
|
adamc@666
|
27 char *start, *front, *back;
|
adamc@666
|
28 } buf;
|
adamc@666
|
29
|
adamc@311
|
30 struct uw_context {
|
adamc@458
|
31 char *headers, *headers_end;
|
adamc@457
|
32
|
adamc@666
|
33 buf outHeaders, page, heap, script;
|
adamc@144
|
34 char **inputs;
|
adamc@167
|
35
|
adamc@565
|
36 int source_count;
|
adamc@562
|
37
|
adamc@272
|
38 void *db;
|
adamc@272
|
39
|
adamc@167
|
40 jmp_buf jmp_buf;
|
adamc@167
|
41
|
adamc@323
|
42 regions *regions;
|
adamc@323
|
43
|
adamc@425
|
44 cleanup *cleanup, *cleanup_front, *cleanup_back;
|
adamc@425
|
45
|
adamc@643
|
46 const char *script_header;
|
adamc@643
|
47
|
adamc@167
|
48 char error_message[ERROR_BUF_LEN];
|
adamc@117
|
49 };
|
adamc@117
|
50
|
adamc@311
|
51 extern int uw_inputs_len;
|
adamc@144
|
52
|
adamc@666
|
53 static void buf_init(buf *b, size_t s) {
|
adamc@666
|
54 b->front = b->start = malloc(s);
|
adamc@666
|
55 b->back = b->front + s;
|
adamc@666
|
56 }
|
adamc@666
|
57
|
adamc@562
|
58 uw_context uw_init(size_t outHeaders_len, size_t script_len, size_t page_len, size_t heap_len) {
|
adamc@311
|
59 uw_context ctx = malloc(sizeof(struct uw_context));
|
adamc@136
|
60
|
adamc@458
|
61 ctx->headers = ctx->headers_end = NULL;
|
adamc@457
|
62
|
adamc@666
|
63 buf_init(&ctx->outHeaders, outHeaders_len);
|
adamc@666
|
64 buf_init(&ctx->page, page_len);
|
adamc@666
|
65 buf_init(&ctx->heap, heap_len);
|
adamc@666
|
66 buf_init(&ctx->script, script_len);
|
adamc@136
|
67
|
adamc@311
|
68 ctx->inputs = calloc(uw_inputs_len, sizeof(char *));
|
adamc@144
|
69
|
adamc@272
|
70 ctx->db = NULL;
|
adamc@272
|
71
|
adamc@323
|
72 ctx->regions = NULL;
|
adamc@323
|
73
|
adamc@425
|
74 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
|
adamc@425
|
75
|
adamc@643
|
76 ctx->script_header = "";
|
adamc@643
|
77
|
adamc@167
|
78 ctx->error_message[0] = 0;
|
adamc@167
|
79
|
adamc@565
|
80 ctx->source_count = 0;
|
adamc@562
|
81
|
adamc@117
|
82 return ctx;
|
adamc@106
|
83 }
|
adamc@106
|
84
|
adamc@311
|
85 void uw_set_db(uw_context ctx, void *db) {
|
adamc@272
|
86 ctx->db = db;
|
adamc@272
|
87 }
|
adamc@272
|
88
|
adamc@311
|
89 void *uw_get_db(uw_context ctx) {
|
adamc@272
|
90 return ctx->db;
|
adamc@272
|
91 }
|
adamc@272
|
92
|
adamc@666
|
93 static void buf_free(buf *b) {
|
adamc@666
|
94 free(b->front);
|
adamc@666
|
95 }
|
adamc@666
|
96
|
adamc@311
|
97 void uw_free(uw_context ctx) {
|
adamc@666
|
98 buf_free(&ctx->outHeaders);
|
adamc@666
|
99 buf_free(&ctx->script);
|
adamc@666
|
100 buf_free(&ctx->page);
|
adamc@666
|
101 buf_free(&ctx->heap);
|
adamc@144
|
102 free(ctx->inputs);
|
adamc@425
|
103 free(ctx->cleanup);
|
adamc@136
|
104 free(ctx);
|
adamc@136
|
105 }
|
adamc@136
|
106
|
adamc@666
|
107 static void buf_reset(buf *b) {
|
adamc@666
|
108 b->front = b->start;
|
adamc@666
|
109 }
|
adamc@666
|
110
|
adamc@562
|
111 void uw_reset_keep_error_message(uw_context ctx) {
|
adamc@666
|
112 buf_reset(&ctx->outHeaders);
|
adamc@666
|
113 buf_reset(&ctx->script);
|
adamc@666
|
114 buf_reset(&ctx->page);
|
adamc@666
|
115 buf_reset(&ctx->heap);
|
adamc@323
|
116 ctx->regions = NULL;
|
adamc@425
|
117 ctx->cleanup_front = ctx->cleanup;
|
adamc@565
|
118 ctx->source_count = 0;
|
adamc@167
|
119 }
|
adamc@167
|
120
|
adamc@562
|
121 void uw_reset_keep_request(uw_context ctx) {
|
adamc@562
|
122 uw_reset_keep_error_message(ctx);
|
adamc@562
|
123 ctx->error_message[0] = 0;
|
adamc@167
|
124 }
|
adamc@167
|
125
|
adamc@311
|
126 void uw_reset(uw_context ctx) {
|
adamc@311
|
127 uw_reset_keep_request(ctx);
|
adamc@311
|
128 memset(ctx->inputs, 0, uw_inputs_len * sizeof(char *));
|
adamc@144
|
129 }
|
adamc@144
|
130
|
adamc@311
|
131 void uw_db_init(uw_context);
|
adamc@311
|
132 void uw_handle(uw_context, char *);
|
adamc@167
|
133
|
adamc@311
|
134 failure_kind uw_begin_init(uw_context ctx) {
|
adamc@272
|
135 int r = setjmp(ctx->jmp_buf);
|
adamc@272
|
136
|
adamc@272
|
137 if (r == 0)
|
adamc@311
|
138 uw_db_init(ctx);
|
adamc@272
|
139
|
adamc@272
|
140 return r;
|
adamc@272
|
141 }
|
adamc@272
|
142
|
adamc@458
|
143 void uw_set_headers(uw_context ctx, char *headers) {
|
adamc@458
|
144 char *s = headers, *s2;
|
adamc@458
|
145 ctx->headers = headers;
|
adamc@458
|
146
|
adamc@458
|
147 while (s2 = strchr(s, '\r')) {
|
adamc@458
|
148 s = s2;
|
adamc@458
|
149
|
adamc@458
|
150 if (s[1] == 0)
|
adamc@458
|
151 break;
|
adamc@458
|
152
|
adamc@458
|
153 *s = 0;
|
adamc@458
|
154 s += 2;
|
adamc@458
|
155 }
|
adamc@458
|
156
|
adamc@458
|
157 ctx->headers_end = s;
|
adamc@458
|
158 }
|
adamc@458
|
159
|
adamc@458
|
160 failure_kind uw_begin(uw_context ctx, char *path) {
|
adamc@190
|
161 int r = setjmp(ctx->jmp_buf);
|
adamc@190
|
162
|
adamc@190
|
163 if (r == 0)
|
adamc@311
|
164 uw_handle(ctx, path);
|
adamc@167
|
165
|
adamc@190
|
166 return r;
|
adamc@167
|
167 }
|
adamc@167
|
168
|
adamc@311
|
169 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
|
adamc@425
|
170 cleanup *cl;
|
adamc@425
|
171
|
adamc@167
|
172 va_list ap;
|
adamc@167
|
173 va_start(ap, fmt);
|
adamc@167
|
174
|
adamc@167
|
175 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
|
adamc@167
|
176
|
adamc@425
|
177 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@425
|
178 cl->func(cl->arg);
|
adamc@425
|
179
|
adamc@425
|
180 ctx->cleanup_front = ctx->cleanup;
|
adamc@425
|
181
|
adamc@190
|
182 longjmp(ctx->jmp_buf, fk);
|
adamc@167
|
183 }
|
adamc@167
|
184
|
adamc@425
|
185 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
|
adamc@425
|
186 if (ctx->cleanup_front >= ctx->cleanup_back) {
|
adamc@425
|
187 int len = ctx->cleanup_back - ctx->cleanup, newLen;
|
adamc@425
|
188 if (len == 0)
|
adamc@425
|
189 newLen = 1;
|
adamc@425
|
190 else
|
adamc@428
|
191 newLen = len * 2;
|
adamc@470
|
192 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
|
adamc@425
|
193 ctx->cleanup_front = ctx->cleanup + len;
|
adamc@425
|
194 ctx->cleanup_back = ctx->cleanup + newLen;
|
adamc@425
|
195 }
|
adamc@425
|
196
|
adamc@425
|
197 ctx->cleanup_front->func = func;
|
adamc@425
|
198 ctx->cleanup_front->arg = arg;
|
adamc@425
|
199 ++ctx->cleanup_front;
|
adamc@425
|
200 }
|
adamc@425
|
201
|
adamc@425
|
202 void uw_pop_cleanup(uw_context ctx) {
|
adamc@425
|
203 if (ctx->cleanup_front == ctx->cleanup)
|
adamc@425
|
204 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
|
adamc@425
|
205
|
adamc@425
|
206 --ctx->cleanup_front;
|
adamc@425
|
207 ctx->cleanup_front->func(ctx->cleanup_front->arg);
|
adamc@425
|
208 }
|
adamc@425
|
209
|
adamc@311
|
210 char *uw_error_message(uw_context ctx) {
|
adamc@167
|
211 return ctx->error_message;
|
adamc@167
|
212 }
|
adamc@167
|
213
|
adamc@311
|
214 int uw_input_num(char*);
|
adamc@144
|
215
|
adamc@311
|
216 void uw_set_input(uw_context ctx, char *name, char *value) {
|
adamc@311
|
217 int n = uw_input_num(name);
|
adamc@144
|
218
|
adamc@169
|
219 if (n < 0)
|
adamc@311
|
220 uw_error(ctx, FATAL, "Bad input name %s", name);
|
adamc@144
|
221
|
adamc@311
|
222 if (n >= uw_inputs_len)
|
adamc@311
|
223 uw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n);
|
adamc@169
|
224
|
adamc@144
|
225 ctx->inputs[n] = value;
|
adamc@144
|
226
|
adamc@282
|
227 //printf("[%d] %s = %s\n", n, name, value);
|
adamc@144
|
228 }
|
adamc@144
|
229
|
adamc@311
|
230 char *uw_get_input(uw_context ctx, int n) {
|
adamc@169
|
231 if (n < 0)
|
adamc@311
|
232 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@311
|
233 if (n >= uw_inputs_len)
|
adamc@311
|
234 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@282
|
235 //printf("[%d] = %s\n", n, ctx->inputs[n]);
|
adamc@144
|
236 return ctx->inputs[n];
|
adamc@138
|
237 }
|
adamc@138
|
238
|
adamc@311
|
239 char *uw_get_optional_input(uw_context ctx, int n) {
|
adamc@190
|
240 if (n < 0)
|
adamc@311
|
241 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@311
|
242 if (n >= uw_inputs_len)
|
adamc@311
|
243 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@426
|
244 //printf("[%d] = %s\n", n, ctx->inputs[n]);
|
adamc@190
|
245 return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]);
|
adamc@190
|
246 }
|
adamc@190
|
247
|
adamc@643
|
248 void uw_set_script_header(uw_context ctx, const char *s) {
|
adamc@643
|
249 ctx->script_header = s;
|
adamc@643
|
250 }
|
adamc@643
|
251
|
adamc@666
|
252 static void buf_check(uw_context ctx, buf *b, size_t extra, const char *desc) {
|
adamc@666
|
253 if (b->back - b->front < extra) {
|
adamc@666
|
254 size_t desired = b->front - b->start + extra, next;
|
adamc@136
|
255 char *new_heap;
|
adamc@136
|
256
|
adamc@666
|
257 next = b->back - b->start;
|
adamc@317
|
258 if (next == 0)
|
adamc@317
|
259 next = 1;
|
adamc@317
|
260 for (; next < desired; next *= 2);
|
adamc@136
|
261
|
adamc@666
|
262 new_heap = realloc(b->start, next);
|
adamc@666
|
263 b->front = new_heap + (b->front - b->start);
|
adamc@666
|
264 b->back = new_heap + next;
|
adamc@136
|
265
|
adamc@666
|
266 if (desc && new_heap != b->start) {
|
adamc@666
|
267 b->start = new_heap;
|
adamc@666
|
268 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously", desc);
|
adamc@136
|
269 }
|
adamc@136
|
270
|
adamc@666
|
271 b->start = new_heap;
|
adamc@136
|
272 }
|
adamc@136
|
273 }
|
adamc@136
|
274
|
adamc@666
|
275 static void uw_check_heap(uw_context ctx, size_t extra) {
|
adamc@666
|
276 buf_check(ctx, &ctx->heap, extra, "heap chunk");
|
adamc@666
|
277 }
|
adamc@666
|
278
|
adamc@311
|
279 void *uw_malloc(uw_context ctx, size_t len) {
|
adamc@136
|
280 void *result;
|
adamc@136
|
281
|
adamc@311
|
282 uw_check_heap(ctx, len);
|
adamc@136
|
283
|
adamc@666
|
284 result = ctx->heap.front;
|
adamc@666
|
285 ctx->heap.front += len;
|
adamc@136
|
286 return result;
|
adamc@117
|
287 }
|
adamc@117
|
288
|
adamc@323
|
289 void uw_begin_region(uw_context ctx) {
|
adamc@666
|
290 regions *r = (regions *) ctx->heap.front;
|
adamc@323
|
291
|
adamc@323
|
292 uw_check_heap(ctx, sizeof(regions));
|
adamc@323
|
293
|
adamc@666
|
294 ctx->heap.front += sizeof(regions);
|
adamc@323
|
295
|
adamc@323
|
296 r->next = ctx->regions;
|
adamc@323
|
297 ctx->regions = r;
|
adamc@323
|
298 }
|
adamc@323
|
299
|
adamc@323
|
300 void uw_end_region(uw_context ctx) {
|
adamc@323
|
301 regions *r = ctx->regions;
|
adamc@323
|
302
|
adamc@323
|
303 if (r == NULL)
|
adamc@323
|
304 uw_error(ctx, FATAL, "Region stack underflow");
|
adamc@323
|
305
|
adamc@666
|
306 ctx->heap.front = (char *) r;
|
adamc@323
|
307 ctx->regions = r->next;
|
adamc@323
|
308 }
|
adamc@323
|
309
|
adamc@666
|
310 static size_t buf_used(buf *b) {
|
adamc@666
|
311 return b->front - b->start;
|
adamc@666
|
312 }
|
adamc@666
|
313
|
adamc@666
|
314 static size_t buf_avail(buf *b) {
|
adamc@666
|
315 return b->back - b->start;
|
adamc@666
|
316 }
|
adamc@666
|
317
|
adamc@324
|
318 void uw_memstats(uw_context ctx) {
|
adamc@666
|
319 printf("Headers: %d/%d\n", buf_used(&ctx->outHeaders), buf_avail(&ctx->outHeaders));
|
adamc@666
|
320 printf("Script: %d/%d\n", buf_used(&ctx->script), buf_avail(&ctx->script));
|
adamc@666
|
321 printf("Page: %d/%d\n", buf_used(&ctx->page), buf_avail(&ctx->page));
|
adamc@666
|
322 printf("Heap: %d/%d\n", buf_used(&ctx->heap), buf_avail(&ctx->heap));
|
adamc@324
|
323 }
|
adamc@324
|
324
|
adamc@436
|
325 int uw_really_send(int sock, const void *buf, size_t len) {
|
adamc@117
|
326 while (len > 0) {
|
adamc@436
|
327 size_t n = send(sock, buf, len, 0);
|
adamc@117
|
328
|
adamc@117
|
329 if (n < 0)
|
adamc@117
|
330 return n;
|
adamc@117
|
331
|
adamc@117
|
332 buf += n;
|
adamc@117
|
333 len -= n;
|
adamc@117
|
334 }
|
adamc@117
|
335
|
adamc@117
|
336 return 0;
|
adamc@117
|
337 }
|
adamc@117
|
338
|
adamc@311
|
339 int uw_send(uw_context ctx, int sock) {
|
adamc@666
|
340 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
|
adamc@462
|
341
|
adamc@462
|
342 if (n < 0)
|
adamc@462
|
343 return n;
|
adamc@462
|
344
|
adamc@462
|
345 n = uw_really_send(sock, "\r\n", 2);
|
adamc@462
|
346
|
adamc@462
|
347 if (n < 0)
|
adamc@462
|
348 return n;
|
adamc@462
|
349
|
adamc@666
|
350 return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start);
|
adamc@462
|
351 }
|
adamc@462
|
352
|
adamc@462
|
353 static void uw_check_headers(uw_context ctx, size_t extra) {
|
adamc@666
|
354 buf_check(ctx, &ctx->outHeaders, extra, NULL);
|
adamc@462
|
355 }
|
adamc@462
|
356
|
adamc@462
|
357 void uw_write_header(uw_context ctx, uw_Basis_string s) {
|
adamc@462
|
358 int len = strlen(s);
|
adamc@462
|
359
|
adamc@462
|
360 uw_check_headers(ctx, len + 1);
|
adamc@666
|
361 strcpy(ctx->outHeaders.front, s);
|
adamc@666
|
362 ctx->outHeaders.front += len;
|
adamc@562
|
363 }
|
adamc@562
|
364
|
adamc@562
|
365 static void uw_check_script(uw_context ctx, size_t extra) {
|
adamc@666
|
366 buf_check(ctx, &ctx->script, extra, NULL);
|
adamc@562
|
367 }
|
adamc@562
|
368
|
adamc@562
|
369 void uw_write_script(uw_context ctx, uw_Basis_string s) {
|
adamc@562
|
370 int len = strlen(s);
|
adamc@562
|
371
|
adamc@562
|
372 uw_check_script(ctx, len + 1);
|
adamc@666
|
373 strcpy(ctx->script.front, s);
|
adamc@666
|
374 ctx->script.front += len;
|
adamc@562
|
375 }
|
adamc@562
|
376
|
adamc@645
|
377 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
|
adamc@666
|
378 if (ctx->script.front == ctx->script.start) {
|
adamc@645
|
379 return ctx->script_header;
|
adamc@565
|
380 } else {
|
adamc@666
|
381 char *r = uw_malloc(ctx, 41 + (ctx->script.front - ctx->script.start) + strlen(ctx->script_header));
|
adamc@565
|
382
|
adamc@666
|
383 sprintf(r, "%s<script>%s</script>", ctx->script_header, ctx->script.start);
|
adamc@565
|
384 return r;
|
adamc@565
|
385 }
|
adamc@565
|
386 }
|
adamc@565
|
387
|
adamc@574
|
388 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
389 char *r, *s2;
|
adamc@574
|
390
|
adamc@574
|
391 uw_check_heap(ctx, strlen(s) * 4 + 2);
|
adamc@574
|
392
|
adamc@666
|
393 r = s2 = ctx->heap.front;
|
adamc@574
|
394 *s2++ = '"';
|
adamc@574
|
395
|
adamc@574
|
396 for (; *s; s++) {
|
adamc@574
|
397 char c = *s;
|
adamc@574
|
398
|
adamc@574
|
399 switch (c) {
|
adamc@574
|
400 case '"':
|
adamc@574
|
401 strcpy(s2, "\\\"");
|
adamc@574
|
402 s2 += 2;
|
adamc@574
|
403 break;
|
adamc@574
|
404 case '\\':
|
adamc@574
|
405 strcpy(s2, "\\\\");
|
adamc@574
|
406 s2 += 2;
|
adamc@574
|
407 break;
|
adamc@574
|
408 default:
|
adamc@574
|
409 if (isprint(c))
|
adamc@574
|
410 *s2++ = c;
|
adamc@574
|
411 else {
|
adamc@574
|
412 sprintf(s2, "\\%3o", c);
|
adamc@574
|
413 s2 += 4;
|
adamc@574
|
414 }
|
adamc@574
|
415 }
|
adamc@574
|
416 }
|
adamc@574
|
417
|
adamc@574
|
418 strcpy(s2, "\"");
|
adamc@666
|
419 ctx->heap.front = s2 + 1;
|
adamc@574
|
420 return r;
|
adamc@574
|
421 }
|
adamc@574
|
422
|
adamc@574
|
423 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
424 char *r, *s2;
|
adamc@574
|
425
|
adamc@574
|
426 uw_check_script(ctx, strlen(s) * 4 + 2);
|
adamc@574
|
427
|
adamc@666
|
428 r = s2 = ctx->script.front;
|
adamc@574
|
429 *s2++ = '"';
|
adamc@574
|
430
|
adamc@574
|
431 for (; *s; s++) {
|
adamc@574
|
432 char c = *s;
|
adamc@574
|
433
|
adamc@574
|
434 switch (c) {
|
adamc@577
|
435 case '\'':
|
adamc@574
|
436 strcpy(s2, "\\\"");
|
adamc@574
|
437 s2 += 2;
|
adamc@574
|
438 break;
|
adamc@574
|
439 case '\\':
|
adamc@574
|
440 strcpy(s2, "\\\\");
|
adamc@574
|
441 s2 += 2;
|
adamc@574
|
442 break;
|
adamc@574
|
443 default:
|
adamc@574
|
444 if (isprint(c))
|
adamc@574
|
445 *s2++ = c;
|
adamc@574
|
446 else {
|
adamc@574
|
447 sprintf(s2, "\\%3o", c);
|
adamc@574
|
448 s2 += 4;
|
adamc@574
|
449 }
|
adamc@574
|
450 }
|
adamc@574
|
451 }
|
adamc@574
|
452
|
adamc@574
|
453 strcpy(s2, "\"");
|
adamc@666
|
454 ctx->script.front = s2 + 1;
|
adamc@574
|
455 return r;
|
adamc@574
|
456 }
|
adamc@574
|
457
|
adamc@577
|
458 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
|
adamc@577
|
459 int len;
|
adamc@577
|
460 size_t s_len = strlen(s);
|
adamc@562
|
461
|
adamc@577
|
462 uw_check_script(ctx, 12 + INTS_MAX + s_len);
|
adamc@666
|
463 sprintf(ctx->script.front, "var s%d=sc(%n", ctx->source_count, &len);
|
adamc@666
|
464 ctx->script.front += len;
|
adamc@666
|
465 strcpy(ctx->script.front, s);
|
adamc@666
|
466 ctx->script.front += s_len;
|
adamc@666
|
467 strcpy(ctx->script.front, ");");
|
adamc@666
|
468 ctx->script.front += 2;
|
adamc@562
|
469
|
adamc@565
|
470 return ctx->source_count++;
|
adamc@117
|
471 }
|
adamc@117
|
472
|
adamc@577
|
473 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
|
adamc@577
|
474 int len;
|
adamc@577
|
475 size_t s_len = strlen(s);
|
adamc@577
|
476
|
adamc@577
|
477 uw_check_script(ctx, 6 + INTS_MAX + s_len);
|
adamc@666
|
478 sprintf(ctx->script.front, "s%d.v=%n", (int)n, &len);
|
adamc@666
|
479 ctx->script.front += len;
|
adamc@666
|
480 strcpy(ctx->script.front, s);
|
adamc@666
|
481 ctx->script.front += s_len;
|
adamc@666
|
482 strcpy(ctx->script.front, ";");
|
adamc@666
|
483 ctx->script.front++;
|
adamc@577
|
484
|
adamc@577
|
485 return uw_unit_v;
|
adamc@577
|
486 }
|
adamc@577
|
487
|
adamc@311
|
488 static void uw_check(uw_context ctx, size_t extra) {
|
adamc@666
|
489 buf_check(ctx, &ctx->page, extra, NULL);
|
adamc@117
|
490 }
|
adamc@117
|
491
|
adamc@311
|
492 static void uw_writec_unsafe(uw_context ctx, char c) {
|
adamc@666
|
493 *(ctx->page.front)++ = c;
|
adamc@117
|
494 }
|
adamc@117
|
495
|
adamc@311
|
496 void uw_writec(uw_context ctx, char c) {
|
adamc@311
|
497 uw_check(ctx, 1);
|
adamc@311
|
498 uw_writec_unsafe(ctx, c);
|
adamc@117
|
499 }
|
adamc@117
|
500
|
adamc@311
|
501 static void uw_write_unsafe(uw_context ctx, const char* s) {
|
adamc@117
|
502 int len = strlen(s);
|
adamc@666
|
503 memcpy(ctx->page.front, s, len);
|
adamc@666
|
504 ctx->page.front += len;
|
adamc@117
|
505 }
|
adamc@117
|
506
|
adamc@311
|
507 void uw_write(uw_context ctx, const char* s) {
|
adamc@311
|
508 uw_check(ctx, strlen(s) + 1);
|
adamc@311
|
509 uw_write_unsafe(ctx, s);
|
adamc@666
|
510 *ctx->page.front = 0;
|
adamc@102
|
511 }
|
adamc@106
|
512
|
adamc@311
|
513 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@136
|
514 char *result;
|
adamc@136
|
515 int len;
|
adamc@311
|
516 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
517 result = ctx->heap.front;
|
adamc@276
|
518 sprintf(result, "%lld%n", n, &len);
|
adamc@666
|
519 ctx->heap.front += len+1;
|
adamc@136
|
520 return result;
|
adamc@106
|
521 }
|
adamc@106
|
522
|
adamc@311
|
523 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@136
|
524 char *result;
|
adamc@136
|
525 int len;
|
adamc@311
|
526 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
527 result = ctx->heap.front;
|
adamc@136
|
528 sprintf(result, "%g%n", n, &len);
|
adamc@666
|
529 ctx->heap.front += len+1;
|
adamc@136
|
530 return result;
|
adamc@106
|
531 }
|
adamc@106
|
532
|
adamc@311
|
533 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@136
|
534 int len = strlen(s);
|
adamc@136
|
535 char *result, *p;
|
adamc@311
|
536 uw_check_heap(ctx, len * 6 + 1);
|
adamc@136
|
537
|
adamc@666
|
538 result = p = ctx->heap.front;
|
adamc@136
|
539
|
adamc@136
|
540 for (; *s; s++) {
|
adamc@136
|
541 char c = *s;
|
adamc@136
|
542
|
adamc@136
|
543 if (c == '"') {
|
adamc@136
|
544 strcpy(p, """);
|
adamc@136
|
545 p += 6;
|
adamc@136
|
546 } else if (c == '&') {
|
adamc@136
|
547 strcpy(p, "&");
|
adamc@136
|
548 p += 5;
|
adamc@136
|
549 }
|
adamc@136
|
550 else if (isprint(c))
|
adamc@136
|
551 *p++ = c;
|
adamc@136
|
552 else {
|
adamc@136
|
553 int len2;
|
adamc@136
|
554 sprintf(p, "&#%d;%n", c, &len2);
|
adamc@136
|
555 p += len2;
|
adamc@136
|
556 }
|
adamc@136
|
557 }
|
adamc@136
|
558
|
adamc@137
|
559 *p++ = 0;
|
adamc@666
|
560 ctx->heap.front = p;
|
adamc@136
|
561 return result;
|
adamc@106
|
562 }
|
adamc@106
|
563
|
adamc@311
|
564 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@117
|
565 int len;
|
adamc@117
|
566
|
adamc@666
|
567 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
568 ctx->page.front += len;
|
adamc@106
|
569 }
|
adamc@106
|
570
|
adamc@428
|
571 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
572 uw_check(ctx, INTS_MAX);
|
adamc@311
|
573 uw_Basis_attrifyInt_w_unsafe(ctx, n);
|
adamc@428
|
574
|
adamc@428
|
575 return uw_unit_v;
|
adamc@106
|
576 }
|
adamc@106
|
577
|
adamc@428
|
578 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@117
|
579 int len;
|
adamc@117
|
580
|
adamc@311
|
581 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
582 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
583 ctx->page.front += len;
|
adamc@428
|
584
|
adamc@428
|
585 return uw_unit_v;
|
adamc@117
|
586 }
|
adamc@117
|
587
|
adamc@428
|
588 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
589 uw_check(ctx, strlen(s) * 6);
|
adamc@117
|
590
|
adamc@106
|
591 for (; *s; s++) {
|
adamc@106
|
592 char c = *s;
|
adamc@106
|
593
|
adamc@106
|
594 if (c == '"')
|
adamc@311
|
595 uw_write_unsafe(ctx, """);
|
adamc@136
|
596 else if (c == '&')
|
adamc@311
|
597 uw_write_unsafe(ctx, "&");
|
adamc@106
|
598 else if (isprint(c))
|
adamc@311
|
599 uw_writec_unsafe(ctx, c);
|
adamc@106
|
600 else {
|
adamc@311
|
601 uw_write_unsafe(ctx, "&#");
|
adamc@311
|
602 uw_Basis_attrifyInt_w_unsafe(ctx, c);
|
adamc@311
|
603 uw_writec_unsafe(ctx, ';');
|
adamc@106
|
604 }
|
adamc@106
|
605 }
|
adamc@428
|
606
|
adamc@428
|
607 return uw_unit_v;
|
adamc@106
|
608 }
|
adamc@120
|
609
|
adamc@120
|
610
|
adamc@311
|
611 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@137
|
612 int len;
|
adamc@137
|
613 char *r;
|
adamc@137
|
614
|
adamc@311
|
615 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
616 r = ctx->heap.front;
|
adamc@276
|
617 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
618 ctx->heap.front += len+1;
|
adamc@137
|
619 return r;
|
adamc@120
|
620 }
|
adamc@120
|
621
|
adamc@311
|
622 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@137
|
623 int len;
|
adamc@137
|
624 char *r;
|
adamc@137
|
625
|
adamc@311
|
626 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
627 r = ctx->heap.front;
|
adamc@137
|
628 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
629 ctx->heap.front += len+1;
|
adamc@137
|
630 return r;
|
adamc@120
|
631 }
|
adamc@120
|
632
|
adamc@311
|
633 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
634 char *r, *p;
|
adamc@137
|
635
|
adamc@311
|
636 uw_check_heap(ctx, strlen(s) * 3 + 1);
|
adamc@137
|
637
|
adamc@666
|
638 for (r = p = ctx->heap.front; *s; s++) {
|
adamc@137
|
639 char c = *s;
|
adamc@137
|
640
|
adamc@137
|
641 if (c == ' ')
|
adamc@137
|
642 *p++ = '+';
|
adamc@137
|
643 else if (isalnum(c))
|
adamc@137
|
644 *p++ = c;
|
adamc@137
|
645 else {
|
adamc@137
|
646 sprintf(p, "%%%02X", c);
|
adamc@137
|
647 p += 3;
|
adamc@137
|
648 }
|
adamc@137
|
649 }
|
adamc@137
|
650
|
adamc@137
|
651 *p++ = 0;
|
adamc@666
|
652 ctx->heap.front = p;
|
adamc@137
|
653 return r;
|
adamc@120
|
654 }
|
adamc@120
|
655
|
adamc@311
|
656 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
657 if (b == uw_Basis_False)
|
adamc@186
|
658 return "0";
|
adamc@186
|
659 else
|
adamc@186
|
660 return "1";
|
adamc@186
|
661 }
|
adamc@186
|
662
|
adamc@311
|
663 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@120
|
664 int len;
|
adamc@120
|
665
|
adamc@666
|
666 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
667 ctx->page.front += len;
|
adamc@120
|
668 }
|
adamc@120
|
669
|
adamc@428
|
670 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
671 uw_check(ctx, INTS_MAX);
|
adamc@311
|
672 uw_Basis_urlifyInt_w_unsafe(ctx, n);
|
adamc@428
|
673
|
adamc@428
|
674 return uw_unit_v;
|
adamc@120
|
675 }
|
adamc@120
|
676
|
adamc@428
|
677 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@120
|
678 int len;
|
adamc@120
|
679
|
adamc@311
|
680 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
681 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
682 ctx->page.front += len;
|
adamc@428
|
683
|
adamc@428
|
684 return uw_unit_v;
|
adamc@120
|
685 }
|
adamc@120
|
686
|
adamc@488
|
687 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@488
|
688 return uw_Basis_urlifyInt(ctx, t);
|
adamc@488
|
689 }
|
adamc@488
|
690
|
adamc@428
|
691 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
692 uw_check(ctx, strlen(s) * 3);
|
adamc@120
|
693
|
adamc@120
|
694 for (; *s; s++) {
|
adamc@120
|
695 char c = *s;
|
adamc@120
|
696
|
adamc@120
|
697 if (c == ' ')
|
adamc@311
|
698 uw_writec_unsafe(ctx, '+');
|
adamc@120
|
699 else if (isalnum(c))
|
adamc@311
|
700 uw_writec_unsafe(ctx, c);
|
adamc@120
|
701 else {
|
adamc@666
|
702 sprintf(ctx->page.front, "%%%02X", c);
|
adamc@666
|
703 ctx->page.front += 3;
|
adamc@120
|
704 }
|
adamc@120
|
705 }
|
adamc@428
|
706
|
adamc@428
|
707 return uw_unit_v;
|
adamc@120
|
708 }
|
adamc@120
|
709
|
adamc@428
|
710 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
711 if (b == uw_Basis_False)
|
adamc@311
|
712 uw_writec(ctx, '0');
|
adamc@186
|
713 else
|
adamc@311
|
714 uw_writec(ctx, '1');
|
adamc@428
|
715
|
adamc@428
|
716 return uw_unit_v;
|
adamc@186
|
717 }
|
adamc@186
|
718
|
adamc@120
|
719
|
adamc@311
|
720 static char *uw_unurlify_advance(char *s) {
|
adamc@144
|
721 char *new_s = strchr(s, '/');
|
adamc@120
|
722
|
adamc@120
|
723 if (new_s)
|
adamc@120
|
724 *new_s++ = 0;
|
adamc@120
|
725 else
|
adamc@144
|
726 new_s = strchr(s, 0);
|
adamc@144
|
727
|
adamc@144
|
728 return new_s;
|
adamc@144
|
729 }
|
adamc@144
|
730
|
adamc@311
|
731 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
|
adamc@311
|
732 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
733 uw_Basis_int r;
|
adamc@120
|
734
|
adamc@276
|
735 r = atoll(*s);
|
adamc@120
|
736 *s = new_s;
|
adamc@120
|
737 return r;
|
adamc@120
|
738 }
|
adamc@120
|
739
|
adamc@311
|
740 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
|
adamc@311
|
741 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
742 uw_Basis_float r;
|
adamc@120
|
743
|
adamc@120
|
744 r = atof(*s);
|
adamc@120
|
745 *s = new_s;
|
adamc@120
|
746 return r;
|
adamc@120
|
747 }
|
adamc@120
|
748
|
adamc@488
|
749 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
|
adamc@488
|
750 return uw_Basis_unurlifyInt(ctx, s);
|
adamc@488
|
751 }
|
adamc@488
|
752
|
adamc@311
|
753 static uw_Basis_string uw_unurlifyString_to(uw_context ctx, char *r, char *s) {
|
adamc@144
|
754 char *s1, *s2;
|
adamc@144
|
755 int n;
|
adamc@136
|
756
|
adamc@144
|
757 for (s1 = r, s2 = s; *s2; ++s1, ++s2) {
|
adamc@136
|
758 char c = *s2;
|
adamc@136
|
759
|
adamc@136
|
760 switch (c) {
|
adamc@136
|
761 case '+':
|
adamc@136
|
762 *s1 = ' ';
|
adamc@136
|
763 break;
|
adamc@136
|
764 case '%':
|
adamc@169
|
765 if (s2[1] == 0)
|
adamc@311
|
766 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
|
adamc@169
|
767 if (s2[2] == 0)
|
adamc@311
|
768 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
|
adamc@169
|
769 if (sscanf(s2+1, "%02X", &n) != 1)
|
adamc@311
|
770 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
|
adamc@136
|
771 *s1 = n;
|
adamc@136
|
772 s2 += 2;
|
adamc@136
|
773 break;
|
adamc@136
|
774 default:
|
adamc@136
|
775 *s1 = c;
|
adamc@136
|
776 }
|
adamc@136
|
777 }
|
adamc@136
|
778 *s1++ = 0;
|
adamc@144
|
779 return s1;
|
adamc@144
|
780 }
|
adamc@144
|
781
|
adamc@311
|
782 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
|
adamc@311
|
783 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
784 uw_Basis_bool r;
|
adamc@186
|
785
|
adamc@186
|
786 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
|
adamc@311
|
787 r = uw_Basis_False;
|
adamc@186
|
788 else
|
adamc@311
|
789 r = uw_Basis_True;
|
adamc@186
|
790
|
adamc@186
|
791 *s = new_s;
|
adamc@186
|
792 return r;
|
adamc@186
|
793 }
|
adamc@186
|
794
|
adamc@311
|
795 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
|
adamc@311
|
796 char *new_s = uw_unurlify_advance(*s);
|
adamc@144
|
797 char *r, *s1, *s2;
|
adamc@144
|
798 int len, n;
|
adamc@144
|
799
|
adamc@200
|
800 len = strlen(*s);
|
adamc@311
|
801 uw_check_heap(ctx, len + 1);
|
adamc@144
|
802
|
adamc@666
|
803 r = ctx->heap.front;
|
adamc@666
|
804 ctx->heap.front = uw_unurlifyString_to(ctx, ctx->heap.front, *s);
|
adamc@136
|
805 *s = new_s;
|
adamc@136
|
806 return r;
|
adamc@120
|
807 }
|
adamc@135
|
808
|
adamc@135
|
809
|
adamc@311
|
810 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
811 int len;
|
adamc@286
|
812 char *r;
|
adamc@286
|
813
|
adamc@311
|
814 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
815 r = ctx->heap.front;
|
adamc@286
|
816 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
817 ctx->heap.front += len+1;
|
adamc@286
|
818 return r;
|
adamc@286
|
819 }
|
adamc@286
|
820
|
adamc@428
|
821 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
822 int len;
|
adamc@286
|
823
|
adamc@311
|
824 uw_check(ctx, INTS_MAX);
|
adamc@666
|
825 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
826 ctx->page.front += len;
|
adamc@428
|
827
|
adamc@428
|
828 return uw_unit_v;
|
adamc@286
|
829 }
|
adamc@286
|
830
|
adamc@311
|
831 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
832 int len;
|
adamc@286
|
833 char *r;
|
adamc@286
|
834
|
adamc@311
|
835 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
836 r = ctx->heap.front;
|
adamc@286
|
837 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
838 ctx->heap.front += len+1;
|
adamc@286
|
839 return r;
|
adamc@286
|
840 }
|
adamc@286
|
841
|
adamc@428
|
842 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
843 int len;
|
adamc@286
|
844
|
adamc@311
|
845 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
846 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
847 ctx->page.front += len;
|
adamc@428
|
848
|
adamc@428
|
849 return uw_unit_v;
|
adamc@286
|
850 }
|
adamc@286
|
851
|
adamc@311
|
852 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
853 char *r, *s2;
|
adamc@137
|
854
|
adamc@311
|
855 uw_check_heap(ctx, strlen(s) * 5 + 1);
|
adamc@137
|
856
|
adamc@666
|
857 for (r = s2 = ctx->heap.front; *s; s++) {
|
adamc@137
|
858 char c = *s;
|
adamc@137
|
859
|
adamc@137
|
860 switch (c) {
|
adamc@137
|
861 case '<':
|
adamc@137
|
862 strcpy(s2, "<");
|
adamc@137
|
863 s2 += 4;
|
adamc@137
|
864 break;
|
adamc@137
|
865 case '&':
|
adamc@137
|
866 strcpy(s2, "&");
|
adamc@137
|
867 s2 += 5;
|
adamc@137
|
868 break;
|
adamc@137
|
869 default:
|
adamc@137
|
870 if (isprint(c))
|
adamc@137
|
871 *s2++ = c;
|
adamc@137
|
872 else {
|
adamc@137
|
873 int len2;
|
adamc@137
|
874 sprintf(s2, "&#%d;%n", c, &len2);
|
adamc@137
|
875 s2 += len2;
|
adamc@137
|
876 }
|
adamc@137
|
877 }
|
adamc@137
|
878 }
|
adamc@137
|
879
|
adamc@137
|
880 *s2++ = 0;
|
adamc@666
|
881 ctx->heap.front = s2;
|
adamc@137
|
882 return r;
|
adamc@135
|
883 }
|
adamc@135
|
884
|
adamc@428
|
885 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@321
|
886 uw_check(ctx, strlen(s) * 6);
|
adamc@135
|
887
|
adamc@135
|
888 for (; *s; s++) {
|
adamc@135
|
889 char c = *s;
|
adamc@135
|
890
|
adamc@135
|
891 switch (c) {
|
adamc@135
|
892 case '<':
|
adamc@311
|
893 uw_write_unsafe(ctx, "<");
|
adamc@135
|
894 break;
|
adamc@135
|
895 case '&':
|
adamc@311
|
896 uw_write_unsafe(ctx, "&");
|
adamc@135
|
897 break;
|
adamc@135
|
898 default:
|
adamc@135
|
899 if (isprint(c))
|
adamc@311
|
900 uw_writec_unsafe(ctx, c);
|
adamc@135
|
901 else {
|
adamc@311
|
902 uw_write_unsafe(ctx, "&#");
|
adamc@311
|
903 uw_Basis_attrifyInt_w_unsafe(ctx, c);
|
adamc@311
|
904 uw_writec_unsafe(ctx, ';');
|
adamc@135
|
905 }
|
adamc@135
|
906 }
|
adamc@135
|
907 }
|
adamc@428
|
908
|
adamc@428
|
909 return uw_unit_v;
|
adamc@135
|
910 }
|
adamc@180
|
911
|
adamc@311
|
912 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
913 if (b == uw_Basis_False)
|
adamc@286
|
914 return "False";
|
adamc@286
|
915 else
|
adamc@286
|
916 return "True";
|
adamc@286
|
917 }
|
adamc@286
|
918
|
adamc@428
|
919 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
920 if (b == uw_Basis_False) {
|
adamc@311
|
921 uw_check(ctx, 6);
|
adamc@666
|
922 strcpy(ctx->page.front, "False");
|
adamc@666
|
923 ctx->page.front += 5;
|
adamc@286
|
924 } else {
|
adamc@311
|
925 uw_check(ctx, 5);
|
adamc@666
|
926 strcpy(ctx->page.front, "True");
|
adamc@666
|
927 ctx->page.front += 4;
|
adamc@286
|
928 }
|
adamc@428
|
929
|
adamc@428
|
930 return uw_unit_v;
|
adamc@286
|
931 }
|
adamc@286
|
932
|
adamc@436
|
933 #define TIME_FMT "%x %X"
|
adamc@438
|
934 #define TIME_FMT_PG "%Y-%m-%d %T"
|
adamc@436
|
935
|
adamc@436
|
936 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
937 size_t len;
|
adamc@436
|
938 char *r;
|
adamc@436
|
939 struct tm stm;
|
adamc@436
|
940
|
adamc@436
|
941 if (localtime_r(&t, &stm)) {
|
adamc@436
|
942 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
943 r = ctx->heap.front;
|
adamc@436
|
944 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
945 ctx->heap.front += len+1;
|
adamc@436
|
946 return r;
|
adamc@436
|
947 } else
|
adamc@436
|
948 return "<i>Invalid time</i>";
|
adamc@436
|
949 }
|
adamc@436
|
950
|
adamc@436
|
951 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
952 size_t len;
|
adamc@436
|
953 char *r;
|
adamc@436
|
954 struct tm stm;
|
adamc@436
|
955
|
adamc@436
|
956 if (localtime_r(&t, &stm)) {
|
adamc@436
|
957 uw_check(ctx, TIMES_MAX);
|
adamc@666
|
958 r = ctx->page.front;
|
adamc@436
|
959 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
960 ctx->page.front += len;
|
adamc@436
|
961 } else {
|
adamc@436
|
962 uw_check(ctx, 20);
|
adamc@666
|
963 strcpy(ctx->page.front, "<i>Invalid time</i>");
|
adamc@666
|
964 ctx->page.front += 19;
|
adamc@436
|
965 }
|
adamc@436
|
966
|
adamc@436
|
967 return uw_unit_v;
|
adamc@436
|
968 }
|
adamc@436
|
969
|
adamc@311
|
970 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
|
adamc@180
|
971 int len = strlen(s1) + strlen(s2) + 1;
|
adamc@180
|
972 char *s;
|
adamc@180
|
973
|
adamc@311
|
974 uw_check_heap(ctx, len);
|
adamc@180
|
975
|
adamc@666
|
976 s = ctx->heap.front;
|
adamc@180
|
977
|
adamc@180
|
978 strcpy(s, s1);
|
adamc@180
|
979 strcat(s, s2);
|
adamc@666
|
980 ctx->heap.front += len;
|
adamc@180
|
981
|
adamc@180
|
982 return s;
|
adamc@180
|
983 }
|
adamc@278
|
984
|
adamc@311
|
985 uw_Basis_string uw_Basis_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@278
|
986 int len = strlen(s1) + 1;
|
adamc@278
|
987 char *s;
|
adamc@278
|
988
|
adamc@311
|
989 uw_check_heap(ctx, len);
|
adamc@278
|
990
|
adamc@666
|
991 s = ctx->heap.front;
|
adamc@278
|
992
|
adamc@278
|
993 strcpy(s, s1);
|
adamc@666
|
994 ctx->heap.front += len;
|
adamc@278
|
995
|
adamc@278
|
996 return s;
|
adamc@278
|
997 }
|
adamc@280
|
998
|
adamc@493
|
999 uw_Basis_string uw_Basis_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@493
|
1000 if (s1)
|
adamc@493
|
1001 return uw_Basis_strdup(ctx, s1);
|
adamc@493
|
1002 else
|
adamc@493
|
1003 return NULL;
|
adamc@493
|
1004 }
|
adamc@493
|
1005
|
adamc@280
|
1006
|
adamc@311
|
1007 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@281
|
1008 int len;
|
adamc@281
|
1009 char *r;
|
adamc@281
|
1010
|
adamc@311
|
1011 uw_check_heap(ctx, INTS_MAX + 6);
|
adamc@666
|
1012 r = ctx->heap.front;
|
adamc@281
|
1013 sprintf(r, "%lld::int8%n", n, &len);
|
adamc@666
|
1014 ctx->heap.front += len+1;
|
adamc@281
|
1015 return r;
|
adamc@281
|
1016 }
|
adamc@281
|
1017
|
adamc@467
|
1018 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
|
adamc@467
|
1019 if (n == NULL)
|
adamc@467
|
1020 return "NULL";
|
adamc@467
|
1021 else
|
adamc@467
|
1022 return uw_Basis_sqlifyInt(ctx, *n);
|
adamc@467
|
1023 }
|
adamc@467
|
1024
|
adamc@311
|
1025 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@281
|
1026 int len;
|
adamc@281
|
1027 char *r;
|
adamc@281
|
1028
|
adamc@311
|
1029 uw_check_heap(ctx, FLOATS_MAX + 8);
|
adamc@666
|
1030 r = ctx->heap.front;
|
adamc@281
|
1031 sprintf(r, "%g::float8%n", n, &len);
|
adamc@666
|
1032 ctx->heap.front += len+1;
|
adamc@281
|
1033 return r;
|
adamc@281
|
1034 }
|
adamc@281
|
1035
|
adamc@467
|
1036 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
|
adamc@467
|
1037 if (n == NULL)
|
adamc@467
|
1038 return "NULL";
|
adamc@467
|
1039 else
|
adamc@467
|
1040 return uw_Basis_sqlifyFloat(ctx, *n);
|
adamc@467
|
1041 }
|
adamc@467
|
1042
|
adamc@281
|
1043
|
adamc@311
|
1044 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@280
|
1045 char *r, *s2;
|
adamc@280
|
1046
|
adamc@311
|
1047 uw_check_heap(ctx, strlen(s) * 2 + 10);
|
adamc@280
|
1048
|
adamc@666
|
1049 r = s2 = ctx->heap.front;
|
adamc@280
|
1050 *s2++ = 'E';
|
adamc@280
|
1051 *s2++ = '\'';
|
adamc@280
|
1052
|
adamc@280
|
1053 for (; *s; s++) {
|
adamc@280
|
1054 char c = *s;
|
adamc@280
|
1055
|
adamc@280
|
1056 switch (c) {
|
adamc@280
|
1057 case '\'':
|
adamc@280
|
1058 strcpy(s2, "\\'");
|
adamc@280
|
1059 s2 += 2;
|
adamc@280
|
1060 break;
|
adamc@280
|
1061 case '\\':
|
adamc@280
|
1062 strcpy(s2, "\\\\");
|
adamc@280
|
1063 s2 += 2;
|
adamc@280
|
1064 break;
|
adamc@280
|
1065 default:
|
adamc@280
|
1066 if (isprint(c))
|
adamc@280
|
1067 *s2++ = c;
|
adamc@280
|
1068 else {
|
adamc@280
|
1069 sprintf(s2, "\\%3o", c);
|
adamc@280
|
1070 s2 += 4;
|
adamc@280
|
1071 }
|
adamc@280
|
1072 }
|
adamc@280
|
1073 }
|
adamc@280
|
1074
|
adamc@281
|
1075 strcpy(s2, "'::text");
|
adamc@666
|
1076 ctx->heap.front = s2 + 8;
|
adamc@280
|
1077 return r;
|
adamc@280
|
1078 }
|
adamc@281
|
1079
|
adamc@467
|
1080 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
|
adamc@467
|
1081 if (s == NULL)
|
adamc@467
|
1082 return "NULL";
|
adamc@467
|
1083 else
|
adamc@467
|
1084 return uw_Basis_sqlifyString(ctx, s);
|
adamc@467
|
1085 }
|
adamc@467
|
1086
|
adamc@311
|
1087 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1088 if (b == uw_Basis_False)
|
adamc@281
|
1089 return "FALSE";
|
adamc@281
|
1090 else
|
adamc@281
|
1091 return "TRUE";
|
adamc@281
|
1092 }
|
adamc@282
|
1093
|
adamc@467
|
1094 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
|
adamc@467
|
1095 if (b == NULL)
|
adamc@467
|
1096 return "NULL";
|
adamc@467
|
1097 else
|
adamc@467
|
1098 return uw_Basis_sqlifyBool(ctx, *b);
|
adamc@467
|
1099 }
|
adamc@467
|
1100
|
adamc@439
|
1101 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@439
|
1102 size_t len;
|
adamc@439
|
1103 char *r;
|
adamc@439
|
1104 struct tm stm;
|
adamc@439
|
1105
|
adamc@439
|
1106 if (localtime_r(&t, &stm)) {
|
adamc@439
|
1107 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
1108 r = ctx->heap.front;
|
adamc@439
|
1109 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1110 ctx->heap.front += len+1;
|
adamc@439
|
1111 return r;
|
adamc@439
|
1112 } else
|
adamc@439
|
1113 return "<Invalid time>";
|
adamc@439
|
1114 }
|
adamc@439
|
1115
|
adamc@467
|
1116 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
|
adamc@467
|
1117 if (t == NULL)
|
adamc@467
|
1118 return "NULL";
|
adamc@467
|
1119 else
|
adamc@467
|
1120 return uw_Basis_sqlifyTime(ctx, *t);
|
adamc@467
|
1121 }
|
adamc@467
|
1122
|
adamc@311
|
1123 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
|
adamc@311
|
1124 static uw_Basis_int true = 1;
|
adamc@311
|
1125 static uw_Basis_int false = 0;
|
adamc@282
|
1126
|
adamc@311
|
1127 if (b == uw_Basis_False)
|
adamc@282
|
1128 return (char *)&false;
|
adamc@282
|
1129 else
|
adamc@282
|
1130 return (char *)&true;
|
adamc@282
|
1131 }
|
adamc@284
|
1132
|
adamc@311
|
1133 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
|
adamc@284
|
1134 int len;
|
adamc@284
|
1135 char *r;
|
adamc@284
|
1136
|
adamc@311
|
1137 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1138 r = ctx->heap.front;
|
adamc@284
|
1139 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1140 ctx->heap.front += len+1;
|
adamc@284
|
1141 return r;
|
adamc@284
|
1142 }
|
adamc@285
|
1143
|
adamc@311
|
1144 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
|
adamc@285
|
1145 int len;
|
adamc@285
|
1146 char *r;
|
adamc@285
|
1147
|
adamc@311
|
1148 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1149 r = ctx->heap.front;
|
adamc@285
|
1150 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1151 ctx->heap.front += len+1;
|
adamc@285
|
1152 return r;
|
adamc@285
|
1153 }
|
adamc@285
|
1154
|
adamc@311
|
1155 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1156 if (b == uw_Basis_False)
|
adamc@285
|
1157 return "False";
|
adamc@285
|
1158 else
|
adamc@285
|
1159 return "True";
|
adamc@285
|
1160 }
|
adamc@288
|
1161
|
adamc@436
|
1162 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
1163 size_t len;
|
adamc@436
|
1164 char *r;
|
adamc@436
|
1165 struct tm stm;
|
adamc@436
|
1166
|
adamc@436
|
1167 if (localtime_r(&t, &stm)) {
|
adamc@436
|
1168 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
1169 r = ctx->heap.front;
|
adamc@436
|
1170 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
1171 ctx->heap.front += len+1;
|
adamc@436
|
1172 return r;
|
adamc@436
|
1173 } else
|
adamc@436
|
1174 return "<Invalid time>";
|
adamc@436
|
1175 }
|
adamc@288
|
1176
|
adamc@311
|
1177 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
|
adamc@288
|
1178 char *endptr;
|
adamc@311
|
1179 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@288
|
1180
|
adamc@288
|
1181 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
1182 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adamc@288
|
1183 *r = n;
|
adamc@288
|
1184 return r;
|
adamc@288
|
1185 } else
|
adamc@288
|
1186 return NULL;
|
adamc@288
|
1187 }
|
adamc@289
|
1188
|
adamc@311
|
1189 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
|
adamc@289
|
1190 char *endptr;
|
adamc@311
|
1191 uw_Basis_float n = strtod(s, &endptr);
|
adamc@289
|
1192
|
adamc@289
|
1193 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
1194 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
|
adamc@289
|
1195 *r = n;
|
adamc@289
|
1196 return r;
|
adamc@289
|
1197 } else
|
adamc@289
|
1198 return NULL;
|
adamc@289
|
1199 }
|
adamc@289
|
1200
|
adamc@311
|
1201 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1202 static uw_Basis_bool true = uw_Basis_True;
|
adamc@311
|
1203 static uw_Basis_bool false = uw_Basis_False;
|
adamc@289
|
1204
|
adamc@289
|
1205 if (!strcasecmp (s, "True"))
|
adamc@289
|
1206 return &true;
|
adamc@289
|
1207 else if (!strcasecmp (s, "False"))
|
adamc@289
|
1208 return &false;
|
adamc@289
|
1209 else
|
adamc@289
|
1210 return NULL;
|
adamc@289
|
1211 }
|
adamc@292
|
1212
|
adamc@436
|
1213 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
1214 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
1215 struct tm stm;
|
adamc@436
|
1216
|
adamc@439
|
1217 if (dot) {
|
adamc@439
|
1218 *dot = 0;
|
adamc@439
|
1219 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
1220 *dot = '.';
|
adamc@439
|
1221 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1222 *r = mktime(&stm);
|
adamc@439
|
1223 return r;
|
adamc@439
|
1224 }
|
adamc@439
|
1225 else {
|
adamc@439
|
1226 *dot = '.';
|
adamc@439
|
1227 return NULL;
|
adamc@439
|
1228 }
|
adamc@436
|
1229 }
|
adamc@439
|
1230 else {
|
adamc@439
|
1231 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
1232 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1233 *r = mktime(&stm);
|
adamc@439
|
1234 return r;
|
adamc@439
|
1235 }
|
adamc@439
|
1236 else if (strptime(s, TIME_FMT, &stm) == end) {
|
adamc@439
|
1237 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1238 *r = mktime(&stm);
|
adamc@439
|
1239 return r;
|
adamc@439
|
1240 }
|
adamc@439
|
1241 else
|
adamc@439
|
1242 return NULL;
|
adamc@439
|
1243 }
|
adamc@436
|
1244 }
|
adamc@436
|
1245
|
adamc@311
|
1246 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
|
adamc@292
|
1247 char *endptr;
|
adamc@311
|
1248 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@292
|
1249
|
adamc@292
|
1250 if (*s != '\0' && *endptr == '\0')
|
adamc@292
|
1251 return n;
|
adamc@292
|
1252 else
|
adamc@311
|
1253 uw_error(ctx, FATAL, "Can't parse int: %s", s);
|
adamc@292
|
1254 }
|
adamc@293
|
1255
|
adamc@311
|
1256 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
|
adamc@293
|
1257 char *endptr;
|
adamc@311
|
1258 uw_Basis_float n = strtod(s, &endptr);
|
adamc@293
|
1259
|
adamc@293
|
1260 if (*s != '\0' && *endptr == '\0')
|
adamc@293
|
1261 return n;
|
adamc@293
|
1262 else
|
adamc@311
|
1263 uw_error(ctx, FATAL, "Can't parse float: %s", s);
|
adamc@293
|
1264 }
|
adamc@293
|
1265
|
adamc@311
|
1266 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
|
adamc@296
|
1267 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
|
adamc@311
|
1268 return uw_Basis_True;
|
adamc@296
|
1269 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
|
adamc@311
|
1270 return uw_Basis_False;
|
adamc@293
|
1271 else
|
adamc@311
|
1272 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
|
adamc@293
|
1273 }
|
adamc@436
|
1274
|
adamc@436
|
1275 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
1276 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
1277 struct tm stm = {};
|
adamc@436
|
1278
|
adamc@438
|
1279 if (dot) {
|
adamc@438
|
1280 *dot = 0;
|
adamc@438
|
1281 if (strptime(s, TIME_FMT_PG, &stm)) {
|
adamc@438
|
1282 *dot = '.';
|
adamc@438
|
1283 return mktime(&stm);
|
adamc@438
|
1284 }
|
adamc@438
|
1285 else {
|
adamc@438
|
1286 *dot = '.';
|
adamc@438
|
1287 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
1288 }
|
adamc@438
|
1289 }
|
adamc@438
|
1290 else {
|
adamc@439
|
1291 if (strptime(s, TIME_FMT_PG, &stm) == end)
|
adamc@439
|
1292 return mktime(&stm);
|
adamc@439
|
1293 else if (strptime(s, TIME_FMT, &stm) == end)
|
adamc@438
|
1294 return mktime(&stm);
|
adamc@438
|
1295 else
|
adamc@438
|
1296 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
1297 }
|
adamc@436
|
1298 }
|
adamc@457
|
1299
|
adamc@457
|
1300 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
|
adamc@457
|
1301 int len = strlen(h);
|
adamc@457
|
1302 char *s = ctx->headers, *p;
|
adamc@457
|
1303
|
adamc@457
|
1304 while (p = strchr(s, ':')) {
|
adamc@457
|
1305 if (p - s == len && !strncasecmp(s, h, len)) {
|
adamc@458
|
1306 return p + 2;
|
adamc@457
|
1307 } else {
|
adamc@458
|
1308 if ((s = strchr(p, 0)) && s < ctx->headers_end)
|
adamc@458
|
1309 s += 2;
|
adamc@457
|
1310 else
|
adamc@457
|
1311 return NULL;
|
adamc@457
|
1312 }
|
adamc@457
|
1313 }
|
adamc@463
|
1314 }
|
adamc@458
|
1315
|
adamc@463
|
1316 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
|
adamc@463
|
1317 int len = strlen(c);
|
adamc@666
|
1318 char *s = ctx->headers, *p = ctx->outHeaders.start;
|
adamc@474
|
1319
|
adamc@474
|
1320 while (p = strstr(p, "\nSet-Cookie: ")) {
|
adamc@474
|
1321 char *p2;
|
adamc@474
|
1322 p += 13;
|
adamc@474
|
1323 p2 = strchr(p, '=');
|
adamc@474
|
1324
|
adamc@474
|
1325 if (p2) {
|
adamc@474
|
1326 size_t sz = strcspn(p2+1, ";\r\n");
|
adamc@474
|
1327
|
adamc@474
|
1328 if (!strncasecmp(p, c, p2 - p)) {
|
adamc@474
|
1329 char *ret = uw_malloc(ctx, sz + 1);
|
adamc@474
|
1330 memcpy(ret, p2+1, sz);
|
adamc@474
|
1331 ret[sz] = 0;
|
adamc@474
|
1332 return ret;
|
adamc@474
|
1333 }
|
adamc@474
|
1334 }
|
adamc@474
|
1335 }
|
adamc@463
|
1336
|
adamc@463
|
1337 while (p = strchr(s, ':')) {
|
adamc@476
|
1338 if (!strncasecmp(s, "Cookie: ", 8)) {
|
adamc@476
|
1339 p += 2;
|
adamc@476
|
1340 while (1) {
|
adamc@476
|
1341 if (!strncmp(p, c, len)
|
adamc@476
|
1342 && p + len < ctx->headers_end && p[len] == '=')
|
adamc@476
|
1343 return p + 1 + len;
|
adamc@476
|
1344 else if (p = strchr(p, ';'))
|
adamc@476
|
1345 p += 2;
|
adamc@476
|
1346 else if ((s = strchr(s, 0)) && s < ctx->headers_end) {
|
adamc@476
|
1347 s += 2;
|
adamc@476
|
1348 break;
|
adamc@476
|
1349 }
|
adamc@476
|
1350 else
|
adamc@476
|
1351 return NULL;
|
adamc@476
|
1352 }
|
adamc@463
|
1353 } else {
|
adamc@463
|
1354 if ((s = strchr(p, 0)) && s < ctx->headers_end)
|
adamc@463
|
1355 s += 2;
|
adamc@463
|
1356 else
|
adamc@463
|
1357 return NULL;
|
adamc@463
|
1358 }
|
adamc@463
|
1359 }
|
adamc@457
|
1360 }
|
adamc@462
|
1361
|
adamc@466
|
1362 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v) {
|
adamc@462
|
1363 uw_write_header(ctx, "Set-Cookie: ");
|
adamc@462
|
1364 uw_write_header(ctx, c);
|
adamc@462
|
1365 uw_write_header(ctx, "=");
|
adamc@462
|
1366 uw_write_header(ctx, v);
|
adamc@466
|
1367 uw_write_header(ctx, "; path=");
|
adamc@466
|
1368 uw_write_header(ctx, prefix);
|
adamc@462
|
1369 uw_write_header(ctx, "\r\n");
|
adamc@462
|
1370
|
adamc@462
|
1371 return uw_unit_v;
|
adamc@462
|
1372 }
|