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