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