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