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