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