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