adamc@1268
|
1 #include "config.h"
|
adamc@1268
|
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@741
|
11 #include <ctype.h>
|
adamc@1114
|
12 #include <limits.h>
|
adamc@1113
|
13 #include <stdint.h>
|
adamc@1094
|
14 #include <sys/types.h>
|
adamc@1094
|
15 #include <sys/socket.h>
|
adam@1397
|
16 #include <openssl/des.h>
|
adam@1374
|
17 #include <time.h>
|
adamc@102
|
18
|
adamc@667
|
19 #include <pthread.h>
|
adamc@667
|
20
|
adamc@102
|
21 #include "types.h"
|
adamc@102
|
22
|
adamc@311
|
23 uw_unit uw_unit_v = {};
|
adamc@102
|
24
|
adamc@667
|
25
|
adamc@667
|
26 // Socket extras
|
adamc@667
|
27
|
adamc@863
|
28 int uw_really_send(int sock, const void *buf, ssize_t len) {
|
adamc@667
|
29 while (len > 0) {
|
adamc@856
|
30 ssize_t n = send(sock, buf, len, 0);
|
adamc@856
|
31
|
adamc@856
|
32 if (n < 0)
|
adamc@856
|
33 return n;
|
adamc@856
|
34
|
adamc@856
|
35 buf += n;
|
adamc@856
|
36 len -= n;
|
adamc@856
|
37 }
|
adamc@856
|
38
|
adamc@856
|
39 return 0;
|
adamc@856
|
40 }
|
adamc@856
|
41
|
adamc@856
|
42 int uw_really_write(int fd, const void *buf, size_t len) {
|
adamc@856
|
43 while (len > 0) {
|
adamc@856
|
44 ssize_t n = write(fd, buf, len);
|
adamc@667
|
45
|
adamc@667
|
46 if (n < 0)
|
adamc@667
|
47 return n;
|
adamc@667
|
48
|
adamc@667
|
49 buf += n;
|
adamc@667
|
50 len -= n;
|
adamc@667
|
51 }
|
adamc@667
|
52
|
adamc@667
|
53 return 0;
|
adamc@667
|
54 }
|
adamc@667
|
55
|
adamc@667
|
56
|
adamc@667
|
57 // Buffers
|
adamc@667
|
58
|
adam@1369
|
59 void uw_buffer_init(size_t max, uw_buffer *b, size_t s) {
|
adamc@1113
|
60 b->max = max;
|
adamc@667
|
61 b->front = b->start = malloc(s);
|
adamc@667
|
62 b->back = b->front + s;
|
adamc@667
|
63 }
|
adamc@667
|
64
|
adam@1369
|
65 void uw_buffer_free(uw_buffer *b) {
|
adamc@667
|
66 free(b->start);
|
adamc@667
|
67 }
|
adamc@667
|
68
|
adam@1369
|
69 void uw_buffer_reset(uw_buffer *b) {
|
adamc@667
|
70 b->front = b->start;
|
adamc@667
|
71 }
|
adamc@667
|
72
|
adam@1369
|
73 int uw_buffer_check(uw_buffer *b, size_t extra) {
|
adamc@667
|
74 if (b->back - b->front < extra) {
|
adamc@667
|
75 size_t desired = b->front - b->start + extra, next;
|
adamc@667
|
76 char *new_heap;
|
adamc@667
|
77
|
adamc@667
|
78 next = b->back - b->start;
|
adamc@667
|
79 if (next == 0)
|
adamc@667
|
80 next = 1;
|
adamc@667
|
81 for (; next < desired; next *= 2);
|
adamc@667
|
82
|
adamc@1134
|
83 if (next > b->max) {
|
adamc@1113
|
84 if (desired <= b->max)
|
adamc@1113
|
85 next = desired;
|
adamc@1113
|
86 else
|
adamc@1113
|
87 return 1;
|
adamc@1134
|
88 }
|
adamc@1113
|
89
|
adamc@667
|
90 new_heap = realloc(b->start, next);
|
adamc@667
|
91 b->front = new_heap + (b->front - b->start);
|
adamc@667
|
92 b->back = new_heap + next;
|
adamc@667
|
93 b->start = new_heap;
|
adamc@667
|
94 }
|
adamc@1113
|
95
|
adamc@1113
|
96 return 0;
|
adamc@1113
|
97 }
|
adamc@1113
|
98
|
adamc@1113
|
99 __attribute__((noreturn)) void uw_error(uw_context, failure_kind, const char *, ...);
|
adamc@1113
|
100
|
adam@1369
|
101 static void ctx_uw_buffer_check(uw_context ctx, const char *kind, uw_buffer *b, size_t extra) {
|
adam@1369
|
102 if (uw_buffer_check(b, extra))
|
adamc@1113
|
103 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind);
|
adamc@667
|
104 }
|
adamc@667
|
105
|
adam@1369
|
106 size_t uw_buffer_used(uw_buffer *b) {
|
adamc@667
|
107 return b->front - b->start;
|
adamc@667
|
108 }
|
adamc@667
|
109
|
adam@1369
|
110 size_t uw_buffer_avail(uw_buffer *b) {
|
adamc@667
|
111 return b->back - b->start;
|
adamc@667
|
112 }
|
adamc@667
|
113
|
adam@1369
|
114 int uw_buffer_append(uw_buffer *b, const char *s, size_t len) {
|
adam@1369
|
115 if (uw_buffer_check(b, len+1))
|
adamc@1113
|
116 return 1;
|
adamc@1113
|
117
|
adamc@1113
|
118 memcpy(b->front, s, len);
|
adamc@1113
|
119 b->front += len;
|
adamc@1113
|
120 *b->front = 0;
|
adamc@1113
|
121
|
adamc@1113
|
122 return 0;
|
adamc@1113
|
123 }
|
adamc@1113
|
124
|
adam@1369
|
125 static void ctx_uw_buffer_append(uw_context ctx, const char *kind, uw_buffer *b, const char *s, size_t len) {
|
adam@1369
|
126 ctx_uw_buffer_check(ctx, kind, b, len+1);
|
adamc@1113
|
127
|
adamc@668
|
128 memcpy(b->front, s, len);
|
adamc@668
|
129 b->front += len;
|
adamc@672
|
130 *b->front = 0;
|
adamc@668
|
131 }
|
adamc@667
|
132
|
adamc@668
|
133
|
adamc@672
|
134 // Persistent state types
|
adamc@667
|
135
|
adamc@667
|
136 typedef enum { UNUSED, USED } usage;
|
adamc@667
|
137
|
adamc@667
|
138 typedef struct client {
|
adamc@682
|
139 unsigned id;
|
adamc@667
|
140 usage mode;
|
adamc@682
|
141 int pass;
|
adamc@682
|
142 struct client *next;
|
adamc@689
|
143 pthread_mutex_t lock, pull_lock;
|
adam@1369
|
144 uw_buffer msgs;
|
adamc@682
|
145 int sock;
|
adamc@863
|
146 int (*send)(int sockfd, const void *buf, ssize_t len);
|
adamc@863
|
147 int (*close)(int fd);
|
adamc@682
|
148 time_t last_contact;
|
adamc@682
|
149 unsigned n_channels;
|
adamc@685
|
150 unsigned refcount;
|
adamc@1121
|
151 void *data;
|
adamc@667
|
152 } client;
|
adamc@667
|
153
|
adamc@672
|
154
|
adamc@672
|
155 // Persistent client state
|
adamc@672
|
156
|
adamc@682
|
157 static client **clients, *clients_free, *clients_used;
|
adamc@682
|
158 static unsigned n_clients;
|
adamc@667
|
159
|
adamc@667
|
160 static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
|
adamc@667
|
161
|
adamc@1113
|
162 size_t uw_messages_max = SIZE_MAX;
|
adamc@1113
|
163 size_t uw_clients_max = SIZE_MAX;
|
adamc@1113
|
164
|
adamc@1121
|
165 void *uw_init_client_data();
|
adamc@1121
|
166 void uw_free_client_data(void *);
|
adamc@1121
|
167 void uw_copy_client_data(void *dst, void *src);
|
adamc@1121
|
168
|
adamc@682
|
169 static client *new_client() {
|
adamc@667
|
170 client *c;
|
adamc@667
|
171
|
adamc@667
|
172 pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
173
|
adamc@667
|
174 if (clients_free) {
|
adamc@667
|
175 c = clients_free;
|
adamc@682
|
176 clients_free = clients_free->next;
|
adamc@667
|
177 }
|
adamc@1113
|
178 else if (n_clients >= uw_clients_max)
|
adamc@1113
|
179 return NULL;
|
adamc@667
|
180 else {
|
adamc@667
|
181 ++n_clients;
|
adamc@667
|
182 clients = realloc(clients, sizeof(client) * n_clients);
|
adamc@667
|
183 c = malloc(sizeof(client));
|
adamc@667
|
184 c->id = n_clients-1;
|
adamc@682
|
185 pthread_mutex_init(&c->lock, NULL);
|
adamc@689
|
186 pthread_mutex_init(&c->pull_lock, NULL);
|
adam@1369
|
187 uw_buffer_init(uw_messages_max, &c->msgs, 0);
|
adamc@667
|
188 clients[n_clients-1] = c;
|
adamc@667
|
189 }
|
adamc@667
|
190
|
adamc@682
|
191 pthread_mutex_lock(&c->lock);
|
adamc@667
|
192 c->mode = USED;
|
adamc@682
|
193 c->pass = rand();
|
adamc@682
|
194 c->sock = -1;
|
adamc@682
|
195 c->last_contact = time(NULL);
|
adam@1369
|
196 uw_buffer_reset(&c->msgs);
|
adamc@682
|
197 c->n_channels = 0;
|
adamc@685
|
198 c->refcount = 0;
|
adamc@1121
|
199 c->data = uw_init_client_data();
|
adamc@682
|
200 pthread_mutex_unlock(&c->lock);
|
adamc@682
|
201
|
adamc@682
|
202 c->next = clients_used;
|
adamc@682
|
203 clients_used = c;
|
adamc@667
|
204
|
adamc@667
|
205 pthread_mutex_unlock(&clients_mutex);
|
adamc@667
|
206
|
adamc@667
|
207 return c;
|
adamc@667
|
208 }
|
adamc@667
|
209
|
adamc@685
|
210 static void use_client(client *c) {
|
adamc@685
|
211 pthread_mutex_lock(&c->lock);
|
adamc@685
|
212 ++c->refcount;
|
adamc@685
|
213 pthread_mutex_unlock(&c->lock);
|
adamc@689
|
214 pthread_mutex_lock(&c->pull_lock);
|
adamc@685
|
215 }
|
adamc@685
|
216
|
adamc@685
|
217 static void release_client(client *c) {
|
adamc@689
|
218 pthread_mutex_unlock(&c->pull_lock);
|
adamc@685
|
219 pthread_mutex_lock(&c->lock);
|
adamc@685
|
220 --c->refcount;
|
adamc@685
|
221 pthread_mutex_unlock(&c->lock);
|
adamc@689
|
222
|
adamc@685
|
223 }
|
adamc@685
|
224
|
adamc@856
|
225 static const char begin_msgs[] = "Content-type: text/plain\r\n\r\n";
|
adamc@667
|
226
|
adamc@682
|
227 static client *find_client(unsigned id) {
|
adamc@667
|
228 client *c;
|
adamc@667
|
229
|
adamc@667
|
230 pthread_mutex_lock(&clients_mutex);
|
adamc@667
|
231
|
adamc@667
|
232 if (id >= n_clients) {
|
adamc@667
|
233 pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
234 return NULL;
|
adamc@667
|
235 }
|
adamc@667
|
236
|
adamc@667
|
237 c = clients[id];
|
adamc@667
|
238
|
adamc@668
|
239 pthread_mutex_unlock(&clients_mutex);
|
adamc@668
|
240 return c;
|
adamc@668
|
241 }
|
adamc@668
|
242
|
adamc@856
|
243 static char *on_success = "HTTP/1.1 200 OK\r\n";
|
adamc@1065
|
244 static char *on_redirect = "HTTP/1.1 303 See Other\r\n";
|
adamc@856
|
245
|
adamc@856
|
246 void uw_set_on_success(char *s) {
|
adamc@856
|
247 on_success = s;
|
adamc@856
|
248 }
|
adamc@856
|
249
|
adamc@863
|
250 void uw_client_connect(unsigned id, int pass, int sock,
|
adamc@863
|
251 int (*send)(int sockfd, const void *buf, ssize_t len),
|
adamc@864
|
252 int (*close)(int fd),
|
adamc@864
|
253 void *logger_data, uw_logger log_error) {
|
adamc@682
|
254 client *c = find_client(id);
|
adamc@668
|
255
|
adamc@668
|
256 if (c == NULL) {
|
adamc@667
|
257 close(sock);
|
adamc@864
|
258 log_error(logger_data, "Out-of-bounds client request (%u)\n", id);
|
adamc@667
|
259 return;
|
adamc@667
|
260 }
|
adamc@667
|
261
|
adamc@682
|
262 pthread_mutex_lock(&c->lock);
|
adamc@671
|
263
|
adamc@682
|
264 if (c->mode != USED) {
|
adamc@682
|
265 pthread_mutex_unlock(&c->lock);
|
adamc@667
|
266 close(sock);
|
adamc@864
|
267 log_error(logger_data, "Client request for unused slot (%u)\n", id);
|
adamc@667
|
268 return;
|
adamc@667
|
269 }
|
adamc@667
|
270
|
adamc@682
|
271 if (pass != c->pass) {
|
adamc@682
|
272 pthread_mutex_unlock(&c->lock);
|
adamc@682
|
273 close(sock);
|
adamc@864
|
274 log_error(logger_data, "Wrong client password (%u, %d)\n", id, pass);
|
adamc@682
|
275 return;
|
adamc@667
|
276 }
|
adamc@667
|
277
|
adamc@682
|
278 if (c->sock != -1) {
|
adamc@863
|
279 c->close(c->sock);
|
adamc@682
|
280 c->sock = -1;
|
adamc@682
|
281 }
|
adamc@667
|
282
|
adamc@682
|
283 c->last_contact = time(NULL);
|
adamc@682
|
284
|
adam@1369
|
285 if (uw_buffer_used(&c->msgs) > 0) {
|
adamc@863
|
286 send(sock, on_success, strlen(on_success));
|
adamc@863
|
287 send(sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adam@1369
|
288 send(sock, c->msgs.start, uw_buffer_used(&c->msgs));
|
adam@1369
|
289 uw_buffer_reset(&c->msgs);
|
adamc@667
|
290 close(sock);
|
adamc@667
|
291 }
|
adamc@863
|
292 else {
|
adamc@682
|
293 c->sock = sock;
|
adamc@863
|
294 c->send = send;
|
adamc@863
|
295 c->close = close;
|
adamc@863
|
296 }
|
adamc@667
|
297
|
adamc@682
|
298 pthread_mutex_unlock(&c->lock);
|
adamc@667
|
299 }
|
adamc@667
|
300
|
adamc@682
|
301 static void free_client(client *c) {
|
adamc@682
|
302 c->mode = UNUSED;
|
adamc@682
|
303 c->pass = -1;
|
adamc@667
|
304
|
adamc@682
|
305 c->next = clients_free;
|
adamc@682
|
306 clients_free = c;
|
adamc@667
|
307 }
|
adamc@667
|
308
|
adamc@682
|
309 static uw_Basis_channel new_channel(client *c) {
|
adamc@682
|
310 uw_Basis_channel ch = {c->id, c->n_channels++};
|
adamc@668
|
311 return ch;
|
adamc@668
|
312 }
|
adamc@668
|
313
|
adam@1369
|
314 static void client_send(client *c, uw_buffer *msg) {
|
adamc@685
|
315 pthread_mutex_lock(&c->lock);
|
adamc@671
|
316
|
adamc@682
|
317 if (c->sock != -1) {
|
adamc@863
|
318 c->send(c->sock, on_success, strlen(on_success));
|
adamc@863
|
319 c->send(c->sock, begin_msgs, sizeof(begin_msgs) - 1);
|
adam@1369
|
320 c->send(c->sock, msg->start, uw_buffer_used(msg));
|
adamc@863
|
321 c->close(c->sock);
|
adamc@682
|
322 c->sock = -1;
|
adam@1369
|
323 } else if (uw_buffer_append(&c->msgs, msg->start, uw_buffer_used(msg)))
|
adamc@1113
|
324 fprintf(stderr, "Client message buffer size exceeded");
|
adamc@671
|
325
|
adamc@685
|
326 pthread_mutex_unlock(&c->lock);
|
adamc@668
|
327 }
|
adamc@668
|
328
|
adamc@668
|
329
|
adamc@668
|
330 // Global entry points
|
adamc@668
|
331
|
adamc@1164
|
332 extern void uw_global_custom();
|
adamc@1104
|
333 extern void uw_init_crypto();
|
adamc@1104
|
334
|
adamc@668
|
335 void uw_global_init() {
|
adamc@668
|
336 srand(time(NULL) ^ getpid());
|
adamc@668
|
337
|
adamc@668
|
338 clients = malloc(0);
|
adamc@1104
|
339
|
adamc@1164
|
340 uw_global_custom();
|
adamc@1104
|
341 uw_init_crypto();
|
adamc@1094
|
342 }
|
adamc@1094
|
343
|
adamc@1094
|
344 void uw_app_init(uw_app *app) {
|
adamc@1094
|
345 app->client_init();
|
adamc@668
|
346 }
|
adamc@668
|
347
|
adam@1332
|
348 int uw_time = 0, uw_time_max = 0, uw_min_heap = 0;
|
adamc@1114
|
349
|
adamc@668
|
350
|
adamc@667
|
351 // Single-request state
|
adamc@667
|
352
|
adamc@323
|
353 typedef struct regions {
|
adamc@323
|
354 struct regions *next;
|
adamc@323
|
355 } regions;
|
adamc@323
|
356
|
adamc@425
|
357 typedef struct {
|
adamc@425
|
358 void (*func)(void*);
|
adamc@425
|
359 void *arg;
|
adamc@425
|
360 } cleanup;
|
adamc@425
|
361
|
adamc@671
|
362 typedef struct {
|
adamc@682
|
363 unsigned client;
|
adam@1369
|
364 uw_buffer msgs;
|
adamc@682
|
365 } delta;
|
adamc@671
|
366
|
adamc@737
|
367 typedef enum {
|
adamc@759
|
368 UNSET, NORMAL, FIL, SUBFORM, SUBFORMS, ENTRY
|
adamc@737
|
369 } input_kind;
|
adamc@737
|
370
|
adamc@756
|
371 typedef struct input {
|
adamc@737
|
372 input_kind kind;
|
adamc@737
|
373 union {
|
adamc@737
|
374 char *normal;
|
adamc@739
|
375 uw_Basis_file file;
|
adamc@756
|
376 struct {
|
adamc@759
|
377 struct input *fields, *parent;
|
adamc@756
|
378 } subform;
|
adamc@759
|
379 struct {
|
adamc@759
|
380 struct input *entries, *parent;
|
adamc@759
|
381 } subforms;
|
adamc@759
|
382 struct {
|
adamc@759
|
383 struct input *fields, *next, *parent;
|
adamc@759
|
384 } entry;
|
adamc@737
|
385 } data;
|
adamc@737
|
386 } input;
|
adamc@737
|
387
|
adamc@771
|
388 typedef struct {
|
adamc@771
|
389 void *data;
|
adam@1327
|
390 uw_callback commit, rollback;
|
adam@1327
|
391 uw_callback_with_retry free;
|
adamc@771
|
392 } transactional;
|
adamc@771
|
393
|
adamc@1061
|
394 typedef struct {
|
adamc@1061
|
395 char *name;
|
adamc@1061
|
396 void *data;
|
adamc@1061
|
397 void (*free)(void*);
|
adamc@1061
|
398 } global;
|
adamc@1061
|
399
|
adamc@311
|
400 struct uw_context {
|
adamc@1094
|
401 uw_app *app;
|
adamc@1094
|
402
|
adamc@854
|
403 char *(*get_header)(void *, const char *);
|
adamc@854
|
404 void *get_header_data;
|
adamc@457
|
405
|
adam@1369
|
406 uw_buffer outHeaders, page, heap, script;
|
adamc@1065
|
407 int returning_indirectly;
|
adamc@759
|
408 input *inputs, *subinputs, *cur_container;
|
adamc@1094
|
409 size_t sz_inputs, n_subinputs, used_subinputs;
|
adamc@167
|
410
|
adamc@565
|
411 int source_count;
|
adamc@562
|
412
|
adamc@272
|
413 void *db;
|
adamc@272
|
414
|
adamc@167
|
415 jmp_buf jmp_buf;
|
adamc@167
|
416
|
adamc@323
|
417 regions *regions;
|
adamc@323
|
418
|
adamc@425
|
419 cleanup *cleanup, *cleanup_front, *cleanup_back;
|
adamc@425
|
420
|
adamc@1094
|
421 const char *script_header;
|
adamc@643
|
422
|
adamc@736
|
423 int needs_push, needs_sig;
|
adamc@693
|
424
|
adamc@682
|
425 size_t n_deltas, used_deltas;
|
adamc@682
|
426 delta *deltas;
|
adamc@671
|
427
|
adamc@682
|
428 client *client;
|
adamc@682
|
429
|
adamc@771
|
430 transactional *transactionals;
|
adamc@771
|
431 size_t n_transactionals, used_transactionals;
|
adamc@771
|
432
|
adamc@1061
|
433 global *globals;
|
adamc@1061
|
434 size_t n_globals;
|
adamc@1061
|
435
|
adamc@1066
|
436 char *current_url;
|
adamc@1066
|
437
|
adamc@1114
|
438 int deadline;
|
adamc@1114
|
439
|
adamc@1121
|
440 void *client_data;
|
adamc@1121
|
441
|
adam@1334
|
442 void *logger_data;
|
adam@1334
|
443 uw_logger log_debug;
|
adam@1334
|
444
|
adam@1386
|
445 int isPost, hasPostBody;
|
adam@1347
|
446 uw_Basis_postBody postBody;
|
adam@1370
|
447 uw_Basis_string queryString;
|
adam@1347
|
448
|
adamc@167
|
449 char error_message[ERROR_BUF_LEN];
|
adamc@117
|
450 };
|
adamc@117
|
451
|
adamc@1113
|
452 size_t uw_headers_max = SIZE_MAX;
|
adamc@1113
|
453 size_t uw_page_max = SIZE_MAX;
|
adamc@1113
|
454 size_t uw_heap_max = SIZE_MAX;
|
adamc@1113
|
455 size_t uw_script_max = SIZE_MAX;
|
adamc@1113
|
456
|
adam@1334
|
457 uw_context uw_init(void *logger_data, uw_logger log_debug) {
|
adamc@311
|
458 uw_context ctx = malloc(sizeof(struct uw_context));
|
adamc@136
|
459
|
adamc@1094
|
460 ctx->app = NULL;
|
adamc@1094
|
461
|
adamc@854
|
462 ctx->get_header = NULL;
|
adamc@854
|
463 ctx->get_header_data = NULL;
|
adamc@457
|
464
|
adam@1369
|
465 uw_buffer_init(uw_headers_max, &ctx->outHeaders, 0);
|
adam@1369
|
466 uw_buffer_init(uw_page_max, &ctx->page, 0);
|
adamc@1065
|
467 ctx->returning_indirectly = 0;
|
adam@1369
|
468 uw_buffer_init(uw_heap_max, &ctx->heap, uw_min_heap);
|
adam@1369
|
469 uw_buffer_init(uw_script_max, &ctx->script, 1);
|
adamc@667
|
470 ctx->script.start[0] = 0;
|
adamc@136
|
471
|
adamc@1094
|
472 ctx->inputs = malloc(0);
|
adamc@759
|
473 ctx->cur_container = NULL;
|
adamc@756
|
474 ctx->subinputs = malloc(0);
|
adamc@1094
|
475 ctx->sz_inputs = ctx->n_subinputs = ctx->used_subinputs = 0;
|
adamc@144
|
476
|
adamc@272
|
477 ctx->db = NULL;
|
adamc@272
|
478
|
adamc@323
|
479 ctx->regions = NULL;
|
adamc@323
|
480
|
adamc@425
|
481 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
|
adamc@425
|
482
|
adamc@643
|
483 ctx->script_header = "";
|
adamc@693
|
484 ctx->needs_push = 0;
|
adamc@736
|
485 ctx->needs_sig = 0;
|
adamc@1104
|
486
|
adamc@565
|
487 ctx->source_count = 0;
|
adamc@562
|
488
|
adamc@682
|
489 ctx->n_deltas = ctx->used_deltas = 0;
|
adamc@671
|
490 ctx->deltas = malloc(0);
|
adamc@671
|
491
|
adamc@700
|
492 ctx->client = NULL;
|
adamc@700
|
493
|
adamc@700
|
494 ctx->error_message[0] = 0;
|
adamc@700
|
495
|
adamc@771
|
496 ctx->transactionals = malloc(0);
|
adamc@771
|
497 ctx->n_transactionals = ctx->used_transactionals = 0;
|
adamc@771
|
498
|
adamc@1061
|
499 ctx->globals = malloc(0);
|
adamc@1061
|
500 ctx->n_globals = 0;
|
adamc@1061
|
501
|
adamc@1066
|
502 ctx->current_url = "";
|
adamc@1066
|
503
|
adamc@1114
|
504 ctx->deadline = INT_MAX;
|
adamc@1114
|
505
|
adamc@1121
|
506 ctx->client_data = uw_init_client_data();
|
adamc@1121
|
507
|
adam@1334
|
508 ctx->logger_data = logger_data;
|
adam@1334
|
509 ctx->log_debug = log_debug;
|
adam@1334
|
510
|
adam@1386
|
511 ctx->isPost = ctx->hasPostBody = 0;
|
adam@1347
|
512
|
adam@1370
|
513 ctx->queryString = NULL;
|
adam@1370
|
514
|
adamc@117
|
515 return ctx;
|
adamc@106
|
516 }
|
adamc@106
|
517
|
adamc@1113
|
518 size_t uw_inputs_max = SIZE_MAX;
|
adamc@1113
|
519
|
adamc@1121
|
520 uw_app *uw_get_app(uw_context ctx) {
|
adamc@1121
|
521 return ctx->app;
|
adamc@1121
|
522 }
|
adamc@1121
|
523
|
adamc@1113
|
524 int uw_set_app(uw_context ctx, uw_app *app) {
|
adamc@1094
|
525 ctx->app = app;
|
adamc@1094
|
526
|
adamc@1105
|
527 if (app && app->inputs_len > ctx->sz_inputs) {
|
adamc@1113
|
528 if (app->inputs_len > uw_inputs_max)
|
adamc@1113
|
529 return 1;
|
adamc@1113
|
530
|
adamc@1094
|
531 ctx->sz_inputs = app->inputs_len;
|
adamc@1094
|
532 ctx->inputs = realloc(ctx->inputs, ctx->sz_inputs * sizeof(input));
|
adamc@1094
|
533 memset(ctx->inputs, 0, ctx->sz_inputs * sizeof(input));
|
adamc@1094
|
534 }
|
adamc@1134
|
535
|
adamc@1134
|
536 return 0;
|
adamc@1094
|
537 }
|
adamc@1094
|
538
|
adamc@1121
|
539 void uw_set_client_data(uw_context ctx, void *data) {
|
adamc@1121
|
540 uw_copy_client_data(ctx->client_data, data);
|
adamc@1121
|
541 }
|
adamc@1121
|
542
|
adamc@311
|
543 void uw_set_db(uw_context ctx, void *db) {
|
adamc@272
|
544 ctx->db = db;
|
adamc@272
|
545 }
|
adamc@272
|
546
|
adamc@311
|
547 void *uw_get_db(uw_context ctx) {
|
adamc@272
|
548 return ctx->db;
|
adamc@272
|
549 }
|
adamc@272
|
550
|
adamc@311
|
551 void uw_free(uw_context ctx) {
|
adamc@671
|
552 size_t i;
|
adamc@671
|
553
|
adam@1369
|
554 uw_buffer_free(&ctx->outHeaders);
|
adam@1369
|
555 uw_buffer_free(&ctx->script);
|
adam@1369
|
556 uw_buffer_free(&ctx->page);
|
adam@1369
|
557 uw_buffer_free(&ctx->heap);
|
adamc@144
|
558 free(ctx->inputs);
|
adamc@756
|
559 free(ctx->subinputs);
|
adamc@425
|
560 free(ctx->cleanup);
|
adamc@771
|
561 free(ctx->transactionals);
|
adamc@1121
|
562 uw_free_client_data(ctx->client_data);
|
adamc@671
|
563
|
adamc@682
|
564 for (i = 0; i < ctx->n_deltas; ++i)
|
adam@1369
|
565 uw_buffer_free(&ctx->deltas[i].msgs);
|
marco-oweber@1330
|
566 free(ctx->deltas);
|
adamc@671
|
567
|
adamc@1061
|
568 for (i = 0; i < ctx->n_globals; ++i)
|
adamc@1089
|
569 if (ctx->globals[i].free)
|
adamc@1089
|
570 ctx->globals[i].free(ctx->globals[i].data);
|
marco-oweber@1330
|
571 free(ctx->globals);
|
adamc@1061
|
572
|
adamc@136
|
573 free(ctx);
|
adamc@136
|
574 }
|
adamc@136
|
575
|
adamc@562
|
576 void uw_reset_keep_error_message(uw_context ctx) {
|
adam@1369
|
577 uw_buffer_reset(&ctx->outHeaders);
|
adam@1369
|
578 uw_buffer_reset(&ctx->script);
|
adamc@667
|
579 ctx->script.start[0] = 0;
|
adam@1369
|
580 uw_buffer_reset(&ctx->page);
|
adamc@1065
|
581 ctx->returning_indirectly = 0;
|
adam@1369
|
582 uw_buffer_reset(&ctx->heap);
|
adamc@323
|
583 ctx->regions = NULL;
|
adamc@425
|
584 ctx->cleanup_front = ctx->cleanup;
|
adamc@565
|
585 ctx->source_count = 0;
|
adamc@682
|
586 ctx->used_deltas = 0;
|
adamc@682
|
587 ctx->client = NULL;
|
adamc@759
|
588 ctx->cur_container = NULL;
|
adamc@771
|
589 ctx->used_transactionals = 0;
|
adamc@1117
|
590 ctx->script_header = "";
|
adam@1386
|
591 ctx->isPost = ctx->hasPostBody = 0;
|
adam@1370
|
592 ctx->queryString = NULL;
|
adamc@167
|
593 }
|
adamc@167
|
594
|
adamc@562
|
595 void uw_reset_keep_request(uw_context ctx) {
|
adamc@562
|
596 uw_reset_keep_error_message(ctx);
|
adamc@562
|
597 ctx->error_message[0] = 0;
|
adamc@167
|
598 }
|
adamc@167
|
599
|
adamc@311
|
600 void uw_reset(uw_context ctx) {
|
adamc@311
|
601 uw_reset_keep_request(ctx);
|
adamc@1105
|
602 if (ctx->app)
|
adamc@1105
|
603 memset(ctx->inputs, 0, ctx->app->inputs_len * sizeof(input));
|
adamc@756
|
604 memset(ctx->subinputs, 0, ctx->n_subinputs * sizeof(input));
|
adamc@756
|
605 ctx->used_subinputs = 0;
|
adamc@144
|
606 }
|
adamc@144
|
607
|
adamc@311
|
608 failure_kind uw_begin_init(uw_context ctx) {
|
adamc@272
|
609 int r = setjmp(ctx->jmp_buf);
|
adamc@272
|
610
|
adamc@1105
|
611 if (r == 0 && ctx->app)
|
adamc@1094
|
612 ctx->app->db_init(ctx);
|
adamc@272
|
613
|
adamc@272
|
614 return r;
|
adamc@272
|
615 }
|
adamc@272
|
616
|
adamc@1118
|
617 void uw_close(uw_context ctx) {
|
adamc@1118
|
618 ctx->app->db_close(ctx);
|
adamc@1118
|
619 }
|
adamc@1118
|
620
|
adamc@854
|
621 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) {
|
adamc@854
|
622 ctx->get_header = get_header;
|
adamc@854
|
623 ctx->get_header_data = get_header_data;
|
adamc@742
|
624 }
|
adamc@742
|
625
|
adamc@801
|
626 static void uw_set_error(uw_context ctx, const char *fmt, ...) {
|
adamc@801
|
627 va_list ap;
|
adamc@801
|
628 va_start(ap, fmt);
|
adamc@801
|
629
|
adamc@801
|
630 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
|
adamc@801
|
631 }
|
adamc@801
|
632
|
adamc@1131
|
633 int uw_has_error(uw_context ctx) {
|
adamc@1131
|
634 return ctx->error_message[0] != 0;
|
adamc@1131
|
635 }
|
adamc@1131
|
636
|
adamc@311
|
637 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
|
adamc@425
|
638 cleanup *cl;
|
adamc@425
|
639
|
adamc@167
|
640 va_list ap;
|
adamc@167
|
641 va_start(ap, fmt);
|
adamc@167
|
642
|
adamc@167
|
643 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
|
adamc@167
|
644
|
adamc@425
|
645 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@425
|
646 cl->func(cl->arg);
|
adamc@425
|
647
|
adamc@425
|
648 ctx->cleanup_front = ctx->cleanup;
|
adamc@425
|
649
|
adamc@190
|
650 longjmp(ctx->jmp_buf, fk);
|
adamc@167
|
651 }
|
adamc@167
|
652
|
adamc@1113
|
653 size_t uw_cleanup_max = SIZE_MAX;
|
adamc@1113
|
654
|
adamc@425
|
655 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
|
adamc@425
|
656 if (ctx->cleanup_front >= ctx->cleanup_back) {
|
adamc@425
|
657 int len = ctx->cleanup_back - ctx->cleanup, newLen;
|
adamc@425
|
658 if (len == 0)
|
adamc@425
|
659 newLen = 1;
|
adamc@425
|
660 else
|
adamc@428
|
661 newLen = len * 2;
|
adamc@1113
|
662
|
adamc@1134
|
663 if (newLen > uw_cleanup_max) {
|
adamc@1113
|
664 if (len+1 <= uw_cleanup_max)
|
adamc@1113
|
665 newLen = uw_cleanup_max;
|
adamc@1113
|
666 else
|
adamc@1113
|
667 uw_error(ctx, FATAL, "Exceeded limit on number of cleanup handlers");
|
adamc@1134
|
668 }
|
adamc@1113
|
669
|
adamc@470
|
670 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
|
adamc@425
|
671 ctx->cleanup_front = ctx->cleanup + len;
|
adamc@425
|
672 ctx->cleanup_back = ctx->cleanup + newLen;
|
adamc@425
|
673 }
|
adamc@425
|
674
|
adamc@425
|
675 ctx->cleanup_front->func = func;
|
adamc@425
|
676 ctx->cleanup_front->arg = arg;
|
adamc@425
|
677 ++ctx->cleanup_front;
|
adamc@425
|
678 }
|
adamc@425
|
679
|
adamc@682
|
680 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
|
adamc@854
|
681 return ctx->get_header(ctx->get_header_data, h);
|
adamc@682
|
682 }
|
adamc@682
|
683
|
adamc@682
|
684 void uw_login(uw_context ctx) {
|
adamc@693
|
685 if (ctx->needs_push) {
|
adamc@682
|
686 char *id_s, *pass_s;
|
adamc@682
|
687
|
adamc@682
|
688 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client"))
|
adamc@682
|
689 && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) {
|
adamc@682
|
690 unsigned id = atoi(id_s);
|
adamc@682
|
691 int pass = atoi(pass_s);
|
adamc@682
|
692 client *c = find_client(id);
|
adamc@682
|
693
|
adamc@682
|
694 if (c == NULL)
|
adamc@682
|
695 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", id_s, pass_s);
|
adamc@682
|
696 else {
|
adamc@685
|
697 use_client(c);
|
adamc@682
|
698 ctx->client = c;
|
adamc@682
|
699
|
adamc@682
|
700 if (c->mode != USED)
|
adamc@682
|
701 uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id);
|
adamc@682
|
702 if (c->pass != pass)
|
adamc@682
|
703 uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass);
|
adamc@682
|
704 }
|
adamc@682
|
705 } else {
|
adamc@682
|
706 client *c = new_client();
|
adamc@1113
|
707
|
adamc@1113
|
708 if (c == NULL)
|
adamc@1113
|
709 uw_error(ctx, FATAL, "Limit exceeded on number of message-passing clients");
|
adamc@1113
|
710
|
adamc@685
|
711 use_client(c);
|
adamc@1121
|
712 uw_copy_client_data(c->data, ctx->client_data);
|
adamc@682
|
713 ctx->client = c;
|
adamc@864
|
714 }
|
adamc@682
|
715 }
|
adamc@682
|
716 }
|
adamc@682
|
717
|
adamc@671
|
718 failure_kind uw_begin(uw_context ctx, char *path) {
|
adamc@671
|
719 int r = setjmp(ctx->jmp_buf);
|
adamc@671
|
720
|
adamc@671
|
721 if (r == 0) {
|
adamc@1094
|
722 if (ctx->app->db_begin(ctx))
|
adamc@671
|
723 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
|
adamc@682
|
724
|
adamc@1094
|
725 ctx->app->handle(ctx, path);
|
adamc@671
|
726 }
|
adamc@671
|
727
|
adamc@671
|
728 return r;
|
adamc@671
|
729 }
|
adamc@671
|
730
|
adam@1294
|
731 failure_kind uw_begin_onError(uw_context ctx, char *msg) {
|
adam@1294
|
732 int r = setjmp(ctx->jmp_buf);
|
adam@1294
|
733
|
adam@1294
|
734 if (ctx->app->on_error) {
|
adam@1294
|
735 if (r == 0) {
|
adam@1294
|
736 if (ctx->app->db_begin(ctx))
|
adam@1294
|
737 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
|
adam@1294
|
738
|
adam@1294
|
739 ctx->app->on_error(ctx, msg);
|
adam@1294
|
740 }
|
adam@1294
|
741
|
adam@1294
|
742 return r;
|
adam@1294
|
743 } else
|
adam@1294
|
744 uw_error(ctx, FATAL, "Tried to run nonexistent onError handler");
|
adam@1294
|
745 }
|
adam@1294
|
746
|
adamc@772
|
747 uw_Basis_client uw_Basis_self(uw_context ctx) {
|
adamc@682
|
748 if (ctx->client == NULL)
|
adamc@682
|
749 uw_error(ctx, FATAL, "Call to Basis.self() from page that has only server-side code");
|
adamc@682
|
750
|
adamc@682
|
751 return ctx->client->id;
|
adamc@682
|
752 }
|
adamc@682
|
753
|
adamc@425
|
754 void uw_pop_cleanup(uw_context ctx) {
|
adamc@425
|
755 if (ctx->cleanup_front == ctx->cleanup)
|
adamc@425
|
756 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
|
adamc@425
|
757
|
adamc@425
|
758 --ctx->cleanup_front;
|
adamc@425
|
759 ctx->cleanup_front->func(ctx->cleanup_front->arg);
|
adamc@425
|
760 }
|
adamc@425
|
761
|
adamc@311
|
762 char *uw_error_message(uw_context ctx) {
|
adamc@167
|
763 return ctx->error_message;
|
adamc@167
|
764 }
|
adamc@167
|
765
|
adamc@1115
|
766 void uw_set_error_message(uw_context ctx, const char *msg) {
|
adamc@1115
|
767 strncpy(ctx->error_message, msg, sizeof(ctx->error_message));
|
adamc@1115
|
768 ctx->error_message[sizeof(ctx->error_message)-1] = 0;
|
adamc@1115
|
769 }
|
adamc@1115
|
770
|
adamc@759
|
771 static input *INP(uw_context ctx) {
|
adamc@759
|
772 if (ctx->cur_container == NULL)
|
adamc@759
|
773 return ctx->inputs;
|
adamc@759
|
774 else if (ctx->cur_container->kind == SUBFORM)
|
adamc@759
|
775 return ctx->cur_container->data.subform.fields;
|
adamc@759
|
776 else if (ctx->cur_container->kind == ENTRY)
|
adamc@759
|
777 return ctx->cur_container->data.entry.fields;
|
adamc@759
|
778 else
|
adamc@760
|
779 uw_error(ctx, FATAL, "INP: Wrong kind (%d, %p)", ctx->cur_container->kind, ctx->cur_container);
|
adamc@759
|
780 }
|
adamc@759
|
781
|
adamc@760
|
782 static void adjust_pointer(input **ptr, input *old_start, input *new_start, size_t len) {
|
adamc@760
|
783 if (*ptr != NULL && *ptr >= old_start && *ptr < old_start + len)
|
adamc@760
|
784 *ptr += new_start - old_start;
|
adamc@760
|
785 }
|
adamc@760
|
786
|
adamc@760
|
787 static void adjust_input(input *x, input *old_start, input *new_start, size_t len) {
|
adamc@759
|
788 switch (x->kind) {
|
adamc@759
|
789 case SUBFORM:
|
adamc@760
|
790 adjust_pointer(&x->data.subform.fields, old_start, new_start, len);
|
adamc@760
|
791 adjust_pointer(&x->data.subform.parent, old_start, new_start, len);
|
adamc@759
|
792 break;
|
adamc@759
|
793 case SUBFORMS:
|
adamc@760
|
794 adjust_pointer(&x->data.subforms.entries, old_start, new_start, len);
|
adamc@760
|
795 adjust_pointer(&x->data.subforms.parent, old_start, new_start, len);
|
adamc@759
|
796 break;
|
adamc@759
|
797 case ENTRY:
|
adamc@760
|
798 adjust_pointer(&x->data.entry.fields, old_start, new_start, len);
|
adamc@760
|
799 adjust_pointer(&x->data.entry.next, old_start, new_start, len);
|
adamc@760
|
800 adjust_pointer(&x->data.entry.parent, old_start, new_start, len);
|
adamc@1134
|
801 break;
|
adamc@1134
|
802 default:
|
adamc@1134
|
803 break;
|
adamc@759
|
804 }
|
adamc@759
|
805 }
|
adamc@759
|
806
|
adamc@1113
|
807 size_t uw_subinputs_max = SIZE_MAX;
|
adamc@1113
|
808
|
adamc@759
|
809 static input *check_input_space(uw_context ctx, size_t len) {
|
adamc@759
|
810 size_t i;
|
adamc@759
|
811 input *r;
|
adamc@759
|
812
|
adamc@759
|
813 if (ctx->used_subinputs + len >= ctx->n_subinputs) {
|
adamc@1113
|
814 if (ctx->used_subinputs + len > uw_subinputs_max)
|
adamc@1113
|
815 uw_error(ctx, FATAL, "Exceeded limit on number of subinputs");
|
adamc@1113
|
816
|
adamc@759
|
817 input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len));
|
adamc@759
|
818
|
adamc@760
|
819 if (ctx->subinputs != new_subinputs) {
|
adamc@760
|
820 for (i = 0; i < ctx->used_subinputs; ++i)
|
adamc@760
|
821 adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
|
adamc@1094
|
822 for (i = 0; i < ctx->app->inputs_len; ++i)
|
adamc@760
|
823 adjust_input(&ctx->inputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
|
adamc@760
|
824
|
adamc@760
|
825 adjust_pointer(&ctx->cur_container, ctx->subinputs, new_subinputs, ctx->used_subinputs);
|
adamc@760
|
826
|
adamc@760
|
827 ctx->n_subinputs = ctx->used_subinputs + len;
|
adamc@760
|
828 ctx->subinputs = new_subinputs;
|
adamc@760
|
829 }
|
adamc@759
|
830 }
|
adamc@759
|
831
|
adamc@759
|
832 r = &ctx->subinputs[ctx->used_subinputs];
|
adamc@759
|
833
|
adamc@759
|
834 for (i = 0; i < len; ++i)
|
adamc@759
|
835 ctx->subinputs[ctx->used_subinputs++].kind = UNUSED;
|
adamc@759
|
836
|
adamc@759
|
837 return r;
|
adamc@759
|
838 }
|
adamc@756
|
839
|
adamc@801
|
840 int uw_set_input(uw_context ctx, const char *name, char *value) {
|
adamc@1050
|
841 //printf("Input name %s\n", name);
|
adamc@1038
|
842
|
adamc@756
|
843 if (!strcasecmp(name, ".b")) {
|
adamc@1094
|
844 int n = ctx->app->input_num(value);
|
adamc@756
|
845 input *inps;
|
adamc@756
|
846
|
adamc@801
|
847 if (n < 0) {
|
adamc@801
|
848 uw_set_error(ctx, "Bad subform name %s", value);
|
adamc@801
|
849 return -1;
|
adamc@801
|
850 }
|
adamc@801
|
851
|
adamc@1094
|
852 if (n >= ctx->app->inputs_len) {
|
adamc@801
|
853 uw_set_error(ctx, "For subform name %s, index %d is out of range", value, n);
|
adamc@801
|
854 return -1;
|
adamc@801
|
855 }
|
adamc@756
|
856
|
adamc@1094
|
857 inps = check_input_space(ctx, ctx->app->inputs_len);
|
adamc@759
|
858
|
adamc@759
|
859 INP(ctx)[n].kind = SUBFORM;
|
adamc@759
|
860 INP(ctx)[n].data.subform.parent = ctx->cur_container;
|
adamc@759
|
861 INP(ctx)[n].data.subform.fields = inps;
|
adamc@759
|
862 ctx->cur_container = &INP(ctx)[n];
|
adamc@756
|
863 } else if (!strcasecmp(name, ".e")) {
|
adamc@756
|
864 input *tmp;
|
adamc@756
|
865
|
adamc@801
|
866 if (ctx->cur_container == NULL) {
|
adamc@801
|
867 uw_set_error(ctx, "Unmatched subform closer");
|
adamc@801
|
868 return -1;
|
adamc@801
|
869 }
|
adamc@756
|
870
|
adamc@759
|
871 tmp = ctx->cur_container;
|
adamc@759
|
872 switch (tmp->kind) {
|
adamc@759
|
873 case SUBFORM:
|
adamc@759
|
874 ctx->cur_container = tmp->data.subform.parent;
|
adamc@759
|
875 tmp->data.subform.parent = NULL;
|
adamc@759
|
876 break;
|
adamc@759
|
877 case SUBFORMS:
|
adamc@759
|
878 ctx->cur_container = tmp->data.subforms.parent;
|
adamc@759
|
879 tmp->data.subforms.parent = NULL;
|
adamc@759
|
880 break;
|
adamc@759
|
881 case ENTRY:
|
adamc@759
|
882 ctx->cur_container = tmp->data.entry.parent;
|
adamc@759
|
883 break;
|
adamc@759
|
884 default:
|
adamc@801
|
885 uw_set_error(ctx, "uw_set_input: Wrong kind");
|
adamc@801
|
886 return -1;
|
adamc@759
|
887 }
|
adamc@759
|
888 } else if (!strcasecmp(name, ".s")) {
|
adamc@1094
|
889 int n = ctx->app->input_num(value);
|
adamc@759
|
890
|
adamc@801
|
891 if (n < 0) {
|
adamc@801
|
892 uw_set_error(ctx, "Bad subforms name %s", value);
|
adamc@801
|
893 return -1;
|
adamc@801
|
894 }
|
adamc@801
|
895
|
adamc@1094
|
896 if (n >= ctx->app->inputs_len) {
|
adamc@801
|
897 uw_set_error(ctx, "For subforms name %s, index %d is out of range", value, n);
|
adamc@801
|
898 return -1;
|
adamc@801
|
899 }
|
adamc@759
|
900
|
adamc@759
|
901 INP(ctx)[n].kind = SUBFORMS;
|
adamc@759
|
902 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
|
adamc@759
|
903 INP(ctx)[n].data.subforms.entries = NULL;
|
adamc@759
|
904 ctx->cur_container = &INP(ctx)[n];
|
adamc@759
|
905 } else if (!strcasecmp(name, ".i")) {
|
adamc@759
|
906 input *inps;
|
adamc@759
|
907
|
adamc@801
|
908 if (!ctx->cur_container) {
|
adamc@801
|
909 uw_set_error(ctx, "New entry without container");
|
adamc@801
|
910 return -1;
|
adamc@801
|
911 }
|
adamc@801
|
912
|
adamc@801
|
913 if (ctx->cur_container->kind != SUBFORMS) {
|
adamc@801
|
914 uw_set_error(ctx, "Bad kind for entry parent");
|
adamc@801
|
915 return -1;
|
adamc@801
|
916 }
|
adamc@759
|
917
|
adamc@1094
|
918 inps = check_input_space(ctx, ctx->app->inputs_len + 1);
|
adamc@759
|
919
|
adamc@759
|
920 inps->kind = ENTRY;
|
adamc@759
|
921 inps->data.entry.parent = ctx->cur_container;
|
adamc@759
|
922 inps->data.entry.next = ctx->cur_container->data.subforms.entries;
|
adamc@759
|
923 ctx->cur_container->data.subforms.entries = inps;
|
adamc@759
|
924
|
adamc@759
|
925 inps->data.entry.fields = inps+1;
|
adamc@759
|
926 ctx->cur_container = inps;
|
adamc@756
|
927 } else {
|
adamc@1094
|
928 int n = ctx->app->input_num(name);
|
adamc@756
|
929
|
adam@1384
|
930 if (n < 0)
|
adam@1384
|
931 return 0;
|
adamc@801
|
932
|
adamc@1094
|
933 if (n >= ctx->app->inputs_len) {
|
adamc@801
|
934 uw_set_error(ctx, "For input name %s, index %d is out of range", name, n);
|
adamc@801
|
935 return -1;
|
adamc@801
|
936 }
|
adamc@756
|
937
|
adamc@756
|
938 INP(ctx)[n].kind = NORMAL;
|
adamc@756
|
939 INP(ctx)[n].data.normal = value;
|
adamc@756
|
940 }
|
adamc@801
|
941
|
adamc@801
|
942 return 0;
|
adamc@144
|
943 }
|
adamc@144
|
944
|
adamc@311
|
945 char *uw_get_input(uw_context ctx, int n) {
|
adamc@169
|
946 if (n < 0)
|
adamc@311
|
947 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@1094
|
948 if (n >= ctx->app->inputs_len)
|
adamc@311
|
949 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@737
|
950
|
adamc@756
|
951 switch (INP(ctx)[n].kind) {
|
adamc@737
|
952 case UNSET:
|
adamc@737
|
953 return NULL;
|
adamc@739
|
954 case FIL:
|
adamc@739
|
955 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
|
adamc@756
|
956 case SUBFORM:
|
adamc@756
|
957 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
|
adamc@759
|
958 case SUBFORMS:
|
adamc@759
|
959 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
|
adamc@759
|
960 case ENTRY:
|
adamc@759
|
961 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
|
adamc@737
|
962 case NORMAL:
|
adamc@756
|
963 return INP(ctx)[n].data.normal;
|
adamc@737
|
964 default:
|
adamc@737
|
965 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
966 }
|
adamc@138
|
967 }
|
adamc@138
|
968
|
adamc@311
|
969 char *uw_get_optional_input(uw_context ctx, int n) {
|
adamc@190
|
970 if (n < 0)
|
adamc@311
|
971 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@1094
|
972 if (n >= ctx->app->inputs_len)
|
adamc@311
|
973 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@737
|
974
|
adamc@756
|
975 switch (INP(ctx)[n].kind) {
|
adamc@737
|
976 case UNSET:
|
adamc@737
|
977 return "";
|
adamc@739
|
978 case FIL:
|
adamc@739
|
979 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
|
adamc@756
|
980 case SUBFORM:
|
adamc@756
|
981 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
|
adamc@759
|
982 case SUBFORMS:
|
adamc@759
|
983 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
|
adamc@759
|
984 case ENTRY:
|
adamc@759
|
985 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
|
adamc@737
|
986 case NORMAL:
|
adamc@756
|
987 return INP(ctx)[n].data.normal;
|
adamc@737
|
988 default:
|
adamc@737
|
989 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
990 }
|
adamc@737
|
991 }
|
adamc@737
|
992
|
adamc@801
|
993 int uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) {
|
adamc@1094
|
994 int n = ctx->app->input_num(name);
|
adamc@737
|
995
|
adamc@801
|
996 if (n < 0) {
|
adamc@801
|
997 uw_set_error(ctx, "Bad file input name %s", name);
|
adamc@801
|
998 return -1;
|
adamc@801
|
999 }
|
adamc@801
|
1000
|
adamc@1094
|
1001 if (n >= ctx->app->inputs_len) {
|
adamc@801
|
1002 uw_set_error(ctx, "For file input name %s, index %d is out of range", name, n);
|
adamc@801
|
1003 return -1;
|
adamc@801
|
1004 }
|
adamc@737
|
1005
|
adamc@739
|
1006 ctx->inputs[n].kind = FIL;
|
adamc@739
|
1007 ctx->inputs[n].data.file = f;
|
adamc@801
|
1008
|
adamc@801
|
1009 return 0;
|
adamc@737
|
1010 }
|
adamc@737
|
1011
|
adamc@739
|
1012 void *uw_malloc(uw_context ctx, size_t len);
|
adamc@739
|
1013
|
adamc@739
|
1014 uw_Basis_file uw_get_file_input(uw_context ctx, int n) {
|
adamc@737
|
1015 if (n < 0)
|
adamc@737
|
1016 uw_error(ctx, FATAL, "Negative file input index %d", n);
|
adamc@1094
|
1017 if (n >= ctx->app->inputs_len)
|
adamc@737
|
1018 uw_error(ctx, FATAL, "Out-of-bounds file input index %d", n);
|
adamc@737
|
1019
|
adamc@756
|
1020 switch (INP(ctx)[n].kind) {
|
adamc@737
|
1021 case UNSET:
|
adamc@737
|
1022 {
|
adamc@739
|
1023 char *data = uw_malloc(ctx, 0);
|
adamc@740
|
1024 uw_Basis_file f = {NULL, "", {0, data}};
|
adamc@739
|
1025 return f;
|
adamc@737
|
1026 }
|
adamc@739
|
1027 case FIL:
|
adamc@756
|
1028 return INP(ctx)[n].data.file;
|
adamc@737
|
1029 case NORMAL:
|
adamc@737
|
1030 uw_error(ctx, FATAL, "Tried to read a normal form input as files");
|
adamc@756
|
1031 case SUBFORM:
|
adamc@756
|
1032 uw_error(ctx, FATAL, "Tried to read a subform form input as files");
|
adamc@759
|
1033 case SUBFORMS:
|
adamc@759
|
1034 uw_error(ctx, FATAL, "Tried to read a subforms form input as files");
|
adamc@759
|
1035 case ENTRY:
|
adamc@759
|
1036 uw_error(ctx, FATAL, "Tried to read an entry form input as files");
|
adamc@737
|
1037 default:
|
adamc@737
|
1038 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@737
|
1039 }
|
adamc@190
|
1040 }
|
adamc@190
|
1041
|
adamc@756
|
1042 void uw_enter_subform(uw_context ctx, int n) {
|
adamc@756
|
1043 if (n < 0)
|
adamc@756
|
1044 uw_error(ctx, FATAL, "Negative subform index %d", n);
|
adamc@1094
|
1045 if (n >= ctx->app->inputs_len)
|
adamc@756
|
1046 uw_error(ctx, FATAL, "Out-of-bounds subform index %d", n);
|
adamc@756
|
1047
|
adamc@756
|
1048 switch (INP(ctx)[n].kind) {
|
adamc@756
|
1049 case UNSET:
|
adamc@756
|
1050 uw_error(ctx, FATAL, "Missing subform");
|
adamc@756
|
1051 case FIL:
|
adamc@756
|
1052 uw_error(ctx, FATAL, "Tried to read a file form input as subform");
|
adamc@756
|
1053 case NORMAL:
|
adamc@756
|
1054 uw_error(ctx, FATAL, "Tried to read a normal form input as subform");
|
adamc@759
|
1055 case SUBFORMS:
|
adamc@759
|
1056 uw_error(ctx, FATAL, "Tried to read a subforms form input as subform");
|
adamc@759
|
1057 case ENTRY:
|
adamc@759
|
1058 uw_error(ctx, FATAL, "Tried to read an entry form input as subform");
|
adamc@756
|
1059 case SUBFORM:
|
adamc@759
|
1060 INP(ctx)[n].data.subform.parent = ctx->cur_container;
|
adamc@760
|
1061 ctx->cur_container = &INP(ctx)[n];
|
adamc@756
|
1062 return;
|
adamc@756
|
1063 default:
|
adamc@756
|
1064 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@756
|
1065 }
|
adamc@756
|
1066 }
|
adamc@756
|
1067
|
adamc@756
|
1068 void uw_leave_subform(uw_context ctx) {
|
adamc@756
|
1069 input *tmp;
|
adamc@756
|
1070
|
adamc@759
|
1071 if (ctx->cur_container == NULL)
|
adamc@756
|
1072 uw_error(ctx, FATAL, "Unmatched uw_leave_subform");
|
adamc@756
|
1073
|
adamc@759
|
1074 tmp = ctx->cur_container;
|
adamc@759
|
1075 ctx->cur_container = tmp->data.subform.parent;
|
adamc@759
|
1076 tmp->data.subform.parent = NULL;
|
adamc@759
|
1077 }
|
adamc@759
|
1078
|
adamc@759
|
1079 int uw_enter_subforms(uw_context ctx, int n) {
|
adamc@759
|
1080 input *inps;
|
adamc@759
|
1081
|
adamc@759
|
1082 if (n < 0)
|
adamc@759
|
1083 uw_error(ctx, FATAL, "Negative subforms index %d", n);
|
adamc@1094
|
1084 if (n >= ctx->app->inputs_len)
|
adamc@759
|
1085 uw_error(ctx, FATAL, "Out-of-bounds subforms index %d", n);
|
adamc@759
|
1086
|
adamc@759
|
1087 switch (INP(ctx)[n].kind) {
|
adamc@759
|
1088 case UNSET:
|
adamc@759
|
1089 uw_error(ctx, FATAL, "Missing subforms");
|
adamc@759
|
1090 case FIL:
|
adamc@759
|
1091 uw_error(ctx, FATAL, "Tried to read a file form input as subforms");
|
adamc@759
|
1092 case NORMAL:
|
adamc@759
|
1093 uw_error(ctx, FATAL, "Tried to read a normal form input %p as subforms", &INP(ctx)[n]);
|
adamc@759
|
1094 case SUBFORM:
|
adamc@759
|
1095 uw_error(ctx, FATAL, "Tried to read a subform form input as subforms");
|
adamc@759
|
1096 case ENTRY:
|
adamc@759
|
1097 uw_error(ctx, FATAL, "Tried to read an entry form input as subforms");
|
adamc@759
|
1098 case SUBFORMS:
|
adamc@759
|
1099 inps = INP(ctx)[n].data.subforms.entries;
|
adamc@759
|
1100 if (inps) {
|
adamc@759
|
1101 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
|
adamc@759
|
1102 ctx->cur_container = INP(ctx)[n].data.subforms.entries;
|
adamc@759
|
1103 return 1;
|
adamc@759
|
1104 } else
|
adamc@759
|
1105 return 0;
|
adamc@759
|
1106 default:
|
adamc@759
|
1107 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@759
|
1108 }
|
adamc@759
|
1109 }
|
adamc@759
|
1110
|
adamc@759
|
1111 int uw_next_entry(uw_context ctx) {
|
adamc@759
|
1112 if (ctx->cur_container == NULL)
|
adamc@759
|
1113 uw_error(ctx, FATAL, "uw_next_entry(NULL)");
|
adamc@759
|
1114
|
adamc@759
|
1115 switch (ctx->cur_container->kind) {
|
adamc@759
|
1116 case UNSET:
|
adamc@759
|
1117 uw_error(ctx, FATAL, "Missing entry");
|
adamc@759
|
1118 case FIL:
|
adamc@759
|
1119 uw_error(ctx, FATAL, "Tried to read a file form input as entry");
|
adamc@759
|
1120 case NORMAL:
|
adamc@759
|
1121 uw_error(ctx, FATAL, "Tried to read a normal form input as entry");
|
adamc@759
|
1122 case SUBFORM:
|
adamc@759
|
1123 uw_error(ctx, FATAL, "Tried to read a subform form input as entry");
|
adamc@759
|
1124 case SUBFORMS:
|
adamc@759
|
1125 uw_error(ctx, FATAL, "Tried to read a subforms form input as entry");
|
adamc@759
|
1126 case ENTRY:
|
adamc@759
|
1127 if (ctx->cur_container->data.entry.next) {
|
adamc@759
|
1128 ctx->cur_container = ctx->cur_container->data.entry.next;
|
adamc@759
|
1129 return 1;
|
adamc@759
|
1130 } else {
|
adamc@759
|
1131 ctx->cur_container = ctx->cur_container->data.entry.parent->data.subforms.parent;
|
adamc@759
|
1132 return 0;
|
adamc@759
|
1133 }
|
adamc@759
|
1134 default:
|
adamc@759
|
1135 uw_error(ctx, FATAL, "Impossible input kind");
|
adamc@759
|
1136 }
|
adamc@756
|
1137 }
|
adamc@756
|
1138
|
adamc@643
|
1139 void uw_set_script_header(uw_context ctx, const char *s) {
|
adamc@643
|
1140 ctx->script_header = s;
|
adamc@643
|
1141 }
|
adamc@643
|
1142
|
adamc@1094
|
1143 const char *uw_get_url_prefix(uw_context ctx) {
|
adamc@1094
|
1144 return ctx->app->url_prefix;
|
adamc@667
|
1145 }
|
adamc@667
|
1146
|
adamc@693
|
1147 void uw_set_needs_push(uw_context ctx, int n) {
|
adamc@693
|
1148 ctx->needs_push = n;
|
adamc@693
|
1149 }
|
adamc@693
|
1150
|
adamc@736
|
1151 void uw_set_needs_sig(uw_context ctx, int n) {
|
adamc@736
|
1152 ctx->needs_sig = n;
|
adamc@736
|
1153 }
|
adamc@736
|
1154
|
adamc@667
|
1155
|
adam@1369
|
1156 static void uw_buffer_check_ctx(uw_context ctx, const char *kind, uw_buffer *b, size_t extra, const char *desc) {
|
adamc@666
|
1157 if (b->back - b->front < extra) {
|
adamc@666
|
1158 size_t desired = b->front - b->start + extra, next;
|
adamc@136
|
1159 char *new_heap;
|
adamc@136
|
1160
|
adamc@666
|
1161 next = b->back - b->start;
|
adamc@317
|
1162 if (next == 0)
|
adamc@317
|
1163 next = 1;
|
adamc@317
|
1164 for (; next < desired; next *= 2);
|
adamc@136
|
1165
|
adamc@1134
|
1166 if (next > b->max) {
|
adamc@1113
|
1167 if (desired <= b->max)
|
adamc@1113
|
1168 next = desired;
|
adamc@1113
|
1169 else
|
adamc@1113
|
1170 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind);
|
adamc@1134
|
1171 }
|
adamc@1113
|
1172
|
adamc@666
|
1173 new_heap = realloc(b->start, next);
|
adamc@666
|
1174 b->front = new_heap + (b->front - b->start);
|
adamc@666
|
1175 b->back = new_heap + next;
|
adamc@136
|
1176
|
adamc@667
|
1177 if (new_heap != b->start) {
|
adamc@666
|
1178 b->start = new_heap;
|
adam@1389
|
1179 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously; increasing size to %llu", desc, (unsigned long long)next);
|
adamc@136
|
1180 }
|
adamc@136
|
1181
|
adamc@666
|
1182 b->start = new_heap;
|
adamc@136
|
1183 }
|
adamc@136
|
1184 }
|
adamc@136
|
1185
|
adamc@832
|
1186 void uw_check_heap(uw_context ctx, size_t extra) {
|
adam@1369
|
1187 uw_buffer_check_ctx(ctx, "heap", &ctx->heap, extra, "heap chunk");
|
adamc@666
|
1188 }
|
adamc@666
|
1189
|
adamc@832
|
1190 char *uw_heap_front(uw_context ctx) {
|
adamc@832
|
1191 return ctx->heap.front;
|
adamc@832
|
1192 }
|
adamc@832
|
1193
|
adamc@832
|
1194 void uw_set_heap_front(uw_context ctx, char *fr) {
|
adamc@832
|
1195 ctx->heap.front = fr;
|
adamc@832
|
1196 }
|
adamc@832
|
1197
|
adamc@311
|
1198 void *uw_malloc(uw_context ctx, size_t len) {
|
adamc@136
|
1199 void *result;
|
adamc@136
|
1200
|
adamc@311
|
1201 uw_check_heap(ctx, len);
|
adamc@136
|
1202
|
adamc@666
|
1203 result = ctx->heap.front;
|
adamc@666
|
1204 ctx->heap.front += len;
|
adamc@136
|
1205 return result;
|
adamc@117
|
1206 }
|
adamc@117
|
1207
|
adamc@323
|
1208 void uw_begin_region(uw_context ctx) {
|
adamc@666
|
1209 regions *r = (regions *) ctx->heap.front;
|
adamc@323
|
1210
|
adamc@323
|
1211 uw_check_heap(ctx, sizeof(regions));
|
adamc@323
|
1212
|
adamc@666
|
1213 ctx->heap.front += sizeof(regions);
|
adamc@323
|
1214
|
adamc@323
|
1215 r->next = ctx->regions;
|
adamc@323
|
1216 ctx->regions = r;
|
adamc@323
|
1217 }
|
adamc@323
|
1218
|
adamc@323
|
1219 void uw_end_region(uw_context ctx) {
|
adamc@323
|
1220 regions *r = ctx->regions;
|
adamc@323
|
1221
|
adamc@323
|
1222 if (r == NULL)
|
adamc@323
|
1223 uw_error(ctx, FATAL, "Region stack underflow");
|
adamc@323
|
1224
|
adamc@666
|
1225 ctx->heap.front = (char *) r;
|
adamc@323
|
1226 ctx->regions = r->next;
|
adamc@323
|
1227 }
|
adamc@323
|
1228
|
adamc@324
|
1229 void uw_memstats(uw_context ctx) {
|
adam@1369
|
1230 printf("Headers: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->outHeaders), (unsigned long)uw_buffer_avail(&ctx->outHeaders));
|
adam@1369
|
1231 printf("Script: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->script), (unsigned long)uw_buffer_avail(&ctx->script));
|
adam@1369
|
1232 printf("Page: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->page), (unsigned long)uw_buffer_avail(&ctx->page));
|
adam@1369
|
1233 printf("Heap: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->heap), (unsigned long)uw_buffer_avail(&ctx->heap));
|
adamc@324
|
1234 }
|
adamc@324
|
1235
|
adamc@311
|
1236 int uw_send(uw_context ctx, int sock) {
|
adamc@666
|
1237 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
|
adamc@462
|
1238
|
adamc@462
|
1239 if (n < 0)
|
adamc@462
|
1240 return n;
|
adamc@462
|
1241
|
adamc@462
|
1242 n = uw_really_send(sock, "\r\n", 2);
|
adamc@462
|
1243
|
adamc@462
|
1244 if (n < 0)
|
adamc@462
|
1245 return n;
|
adamc@462
|
1246
|
adamc@666
|
1247 return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start);
|
adamc@462
|
1248 }
|
adamc@462
|
1249
|
adamc@856
|
1250 int uw_print(uw_context ctx, int fd) {
|
adamc@856
|
1251 int n = uw_really_write(fd, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
|
adamc@856
|
1252
|
adamc@856
|
1253 if (n < 0)
|
adamc@856
|
1254 return n;
|
adamc@856
|
1255
|
adamc@856
|
1256 n = uw_really_write(fd, "\r\n", 2);
|
adamc@856
|
1257
|
adamc@856
|
1258 if (n < 0)
|
adamc@856
|
1259 return n;
|
adamc@856
|
1260
|
adamc@856
|
1261 return uw_really_write(fd, ctx->page.start, ctx->page.front - ctx->page.start);
|
adamc@856
|
1262 }
|
adamc@856
|
1263
|
adamc@860
|
1264 int uw_output(uw_context ctx, int (*output)(void *data, char *buf, size_t len), void *data) {
|
adamc@860
|
1265 int n = output(data, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
|
adamc@860
|
1266
|
adamc@860
|
1267 if (n < 0)
|
adamc@860
|
1268 return n;
|
adamc@860
|
1269
|
adamc@860
|
1270 n = output(data, "\r\n", 2);
|
adamc@860
|
1271
|
adamc@860
|
1272 if (n < 0)
|
adamc@860
|
1273 return n;
|
adamc@860
|
1274
|
adamc@860
|
1275 return output(data, ctx->page.start, ctx->page.front - ctx->page.start);
|
adamc@860
|
1276 }
|
adamc@860
|
1277
|
adamc@462
|
1278 static void uw_check_headers(uw_context ctx, size_t extra) {
|
adam@1369
|
1279 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, extra);
|
adamc@462
|
1280 }
|
adamc@462
|
1281
|
adamc@462
|
1282 void uw_write_header(uw_context ctx, uw_Basis_string s) {
|
adamc@462
|
1283 int len = strlen(s);
|
adamc@462
|
1284
|
adamc@462
|
1285 uw_check_headers(ctx, len + 1);
|
adamc@666
|
1286 strcpy(ctx->outHeaders.front, s);
|
adamc@666
|
1287 ctx->outHeaders.front += len;
|
adamc@562
|
1288 }
|
adamc@562
|
1289
|
adamc@1110
|
1290 void uw_clear_headers(uw_context ctx) {
|
adam@1369
|
1291 uw_buffer_reset(&ctx->outHeaders);
|
adamc@1110
|
1292 }
|
adamc@1110
|
1293
|
adamc@562
|
1294 static void uw_check_script(uw_context ctx, size_t extra) {
|
adam@1369
|
1295 ctx_uw_buffer_check(ctx, "script", &ctx->script, extra);
|
adamc@562
|
1296 }
|
adamc@562
|
1297
|
adamc@562
|
1298 void uw_write_script(uw_context ctx, uw_Basis_string s) {
|
adamc@562
|
1299 int len = strlen(s);
|
adamc@562
|
1300
|
adamc@562
|
1301 uw_check_script(ctx, len + 1);
|
adamc@666
|
1302 strcpy(ctx->script.front, s);
|
adamc@666
|
1303 ctx->script.front += len;
|
adamc@562
|
1304 }
|
adamc@562
|
1305
|
adamc@645
|
1306 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
|
adamc@916
|
1307 return "<sc>";
|
adamc@565
|
1308 }
|
adamc@565
|
1309
|
adamc@695
|
1310 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
|
adamc@695
|
1311 if (s[0] == 0)
|
adamc@695
|
1312 return "";
|
adamc@695
|
1313 else {
|
adamc@695
|
1314 char *r = uw_malloc(ctx, 11 + strlen(s));
|
adamc@695
|
1315 sprintf(r, " onload='%s'", s);
|
adamc@695
|
1316 return r;
|
adamc@695
|
1317 }
|
adamc@695
|
1318 }
|
adamc@695
|
1319
|
adamc@1042
|
1320 uw_Basis_string uw_Basis_maybe_onunload(uw_context ctx, uw_Basis_string s) {
|
adamc@1042
|
1321 if (ctx->script_header[0] == 0)
|
adamc@1042
|
1322 return "";
|
adamc@1042
|
1323 else {
|
adamc@1042
|
1324 char *r = uw_malloc(ctx, 22 + strlen(s));
|
adamc@1042
|
1325 sprintf(r, " onunload='unload();%s'", s);
|
adamc@1042
|
1326 return r;
|
adamc@1042
|
1327 }
|
adamc@1042
|
1328 }
|
adamc@1042
|
1329
|
adamc@694
|
1330 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
|
adamc@736
|
1331 if (ctx->client == NULL) {
|
adamc@736
|
1332 if (ctx->needs_sig) {
|
adamc@1094
|
1333 char *sig = ctx->app->cookie_sig(ctx);
|
adamc@736
|
1334 char *r = uw_malloc(ctx, strlen(sig) + 8);
|
adamc@736
|
1335 sprintf(r, "sig=\"%s\";", sig);
|
adamc@736
|
1336 return r;
|
adamc@736
|
1337 }
|
adamc@736
|
1338 else
|
adamc@736
|
1339 return "";
|
adamc@736
|
1340 } else {
|
adamc@1094
|
1341 char *sig = ctx->needs_sig ? ctx->app->cookie_sig(ctx) : "";
|
adamc@1094
|
1342 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->app->url_prefix)
|
adamc@736
|
1343 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
|
adamc@736
|
1344 sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
|
adamc@682
|
1345 ctx->client->id,
|
adamc@682
|
1346 ctx->client->pass,
|
adamc@1094
|
1347 ctx->app->url_prefix,
|
adamc@1094
|
1348 ctx->app->timeout,
|
adamc@736
|
1349 ctx->needs_sig ? "sig=\"" : "",
|
adamc@736
|
1350 sig,
|
adamc@736
|
1351 ctx->needs_sig ? "\";" : "");
|
adamc@679
|
1352 return r;
|
adamc@668
|
1353 }
|
adamc@667
|
1354 }
|
adamc@667
|
1355
|
adamc@574
|
1356 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
1357 char *r, *s2;
|
adamc@574
|
1358
|
adamc@574
|
1359 uw_check_heap(ctx, strlen(s) * 4 + 2);
|
adamc@574
|
1360
|
adamc@666
|
1361 r = s2 = ctx->heap.front;
|
adamc@574
|
1362 *s2++ = '"';
|
adamc@574
|
1363
|
adamc@574
|
1364 for (; *s; s++) {
|
adam@1286
|
1365 unsigned char c = *s;
|
adamc@574
|
1366
|
adamc@574
|
1367 switch (c) {
|
adamc@574
|
1368 case '"':
|
adamc@574
|
1369 strcpy(s2, "\\\"");
|
adamc@574
|
1370 s2 += 2;
|
adamc@574
|
1371 break;
|
adamc@1027
|
1372 case '\'':
|
adamc@1027
|
1373 strcpy(s2, "\\047");
|
adamc@1027
|
1374 s2 += 4;
|
adamc@1027
|
1375 break;
|
adamc@574
|
1376 case '\\':
|
adamc@574
|
1377 strcpy(s2, "\\\\");
|
adamc@574
|
1378 s2 += 2;
|
adamc@574
|
1379 break;
|
adamc@574
|
1380 default:
|
adam@1286
|
1381 if (isprint((int)c) || c >= 128)
|
adamc@574
|
1382 *s2++ = c;
|
adamc@574
|
1383 else {
|
adamc@574
|
1384 sprintf(s2, "\\%3o", c);
|
adamc@574
|
1385 s2 += 4;
|
adamc@574
|
1386 }
|
adamc@574
|
1387 }
|
adamc@574
|
1388 }
|
adamc@574
|
1389
|
adamc@574
|
1390 strcpy(s2, "\"");
|
adamc@910
|
1391 ctx->heap.front = s2 + 2;
|
adamc@574
|
1392 return r;
|
adamc@574
|
1393 }
|
adamc@574
|
1394
|
adam@1286
|
1395 uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c1) {
|
adam@1286
|
1396 unsigned char c = c1;
|
adamc@1025
|
1397 char *r, *s2;
|
adamc@1025
|
1398
|
adamc@1025
|
1399 uw_check_heap(ctx, 6);
|
adamc@1025
|
1400
|
adamc@1025
|
1401 r = s2 = ctx->heap.front;
|
adamc@1025
|
1402 *s2++ = '"';
|
adamc@1025
|
1403
|
adamc@1025
|
1404 switch (c) {
|
adamc@1025
|
1405 case '"':
|
adamc@1025
|
1406 strcpy(s2, "\\\"");
|
adamc@1025
|
1407 s2 += 2;
|
adamc@1025
|
1408 break;
|
adamc@1027
|
1409 case '\'':
|
adamc@1027
|
1410 strcpy(s2, "\\047");
|
adamc@1027
|
1411 s2 += 4;
|
adamc@1027
|
1412 break;
|
adamc@1025
|
1413 case '\\':
|
adamc@1025
|
1414 strcpy(s2, "\\\\");
|
adamc@1025
|
1415 s2 += 2;
|
adamc@1025
|
1416 break;
|
adamc@1025
|
1417 default:
|
adam@1286
|
1418 if (isprint((int)c) || c >= 128)
|
adamc@1025
|
1419 *s2++ = c;
|
adamc@1025
|
1420 else {
|
adam@1286
|
1421 sprintf(s2, "\\%3o", (unsigned char)c);
|
adamc@1025
|
1422 s2 += 4;
|
adamc@1025
|
1423 }
|
adamc@1025
|
1424 }
|
adamc@1025
|
1425
|
adamc@1025
|
1426 strcpy(s2, "\"");
|
adamc@1025
|
1427 ctx->heap.front = s2 + 2;
|
adamc@1025
|
1428 return r;
|
adamc@1025
|
1429 }
|
adamc@1025
|
1430
|
adamc@574
|
1431 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
|
adamc@574
|
1432 char *r, *s2;
|
adamc@574
|
1433
|
adamc@574
|
1434 uw_check_script(ctx, strlen(s) * 4 + 2);
|
adamc@574
|
1435
|
adamc@666
|
1436 r = s2 = ctx->script.front;
|
adamc@574
|
1437 *s2++ = '"';
|
adamc@574
|
1438
|
adamc@574
|
1439 for (; *s; s++) {
|
adam@1286
|
1440 unsigned char c = *s;
|
adamc@574
|
1441
|
adamc@574
|
1442 switch (c) {
|
adamc@577
|
1443 case '\'':
|
adamc@1027
|
1444 strcpy(s2, "\\");
|
adamc@574
|
1445 s2 += 2;
|
adamc@574
|
1446 break;
|
adamc@574
|
1447 case '\\':
|
adamc@574
|
1448 strcpy(s2, "\\\\");
|
adamc@574
|
1449 s2 += 2;
|
adamc@574
|
1450 break;
|
adamc@574
|
1451 default:
|
adam@1286
|
1452 if (isprint((int)c) || c >= 128)
|
adamc@574
|
1453 *s2++ = c;
|
adamc@574
|
1454 else {
|
adamc@574
|
1455 sprintf(s2, "\\%3o", c);
|
adamc@574
|
1456 s2 += 4;
|
adamc@574
|
1457 }
|
adamc@574
|
1458 }
|
adamc@574
|
1459 }
|
adamc@574
|
1460
|
adamc@574
|
1461 strcpy(s2, "\"");
|
adamc@666
|
1462 ctx->script.front = s2 + 1;
|
adamc@574
|
1463 return r;
|
adamc@574
|
1464 }
|
adamc@574
|
1465
|
adamc@682
|
1466 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1467 if (ctx->client == NULL || chn.cli != ctx->client->id)
|
adamc@682
|
1468 return "null";
|
adamc@682
|
1469 else {
|
adamc@682
|
1470 int len;
|
adamc@682
|
1471 char *r;
|
adamc@682
|
1472
|
adamc@682
|
1473 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
1474 r = ctx->heap.front;
|
adamc@682
|
1475 sprintf(r, "%u%n", chn.chn, &len);
|
adamc@682
|
1476 ctx->heap.front += len+1;
|
adamc@682
|
1477 return r;
|
adamc@682
|
1478 }
|
adamc@682
|
1479 }
|
adamc@682
|
1480
|
adamc@577
|
1481 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
|
adamc@577
|
1482 int len;
|
adamc@577
|
1483 size_t s_len = strlen(s);
|
adamc@562
|
1484
|
adamc@970
|
1485 uw_check_script(ctx, 18 + INTS_MAX + s_len);
|
adamc@970
|
1486 sprintf(ctx->script.front, "var s%d=sc(exec(%n", ctx->source_count, &len);
|
adamc@666
|
1487 ctx->script.front += len;
|
adamc@666
|
1488 strcpy(ctx->script.front, s);
|
adamc@666
|
1489 ctx->script.front += s_len;
|
adamc@970
|
1490 strcpy(ctx->script.front, "));");
|
adamc@972
|
1491 ctx->script.front += 3;
|
adamc@562
|
1492
|
adamc@565
|
1493 return ctx->source_count++;
|
adamc@117
|
1494 }
|
adamc@117
|
1495
|
adamc@577
|
1496 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
|
adamc@577
|
1497 int len;
|
adamc@577
|
1498 size_t s_len = strlen(s);
|
adamc@577
|
1499
|
adamc@970
|
1500 uw_check_script(ctx, 12 + INTS_MAX + s_len);
|
adamc@970
|
1501 sprintf(ctx->script.front, "sv(s%d,exec(%n", (int)n, &len);
|
adamc@666
|
1502 ctx->script.front += len;
|
adamc@666
|
1503 strcpy(ctx->script.front, s);
|
adamc@666
|
1504 ctx->script.front += s_len;
|
adamc@970
|
1505 strcpy(ctx->script.front, "));");
|
adamc@972
|
1506 ctx->script.front += 3;
|
adamc@577
|
1507
|
adamc@577
|
1508 return uw_unit_v;
|
adamc@577
|
1509 }
|
adamc@577
|
1510
|
adamc@311
|
1511 static void uw_check(uw_context ctx, size_t extra) {
|
adam@1369
|
1512 ctx_uw_buffer_check(ctx, "page", &ctx->page, extra);
|
adamc@117
|
1513 }
|
adamc@117
|
1514
|
adamc@311
|
1515 static void uw_writec_unsafe(uw_context ctx, char c) {
|
adamc@666
|
1516 *(ctx->page.front)++ = c;
|
adamc@117
|
1517 }
|
adamc@117
|
1518
|
adamc@311
|
1519 void uw_writec(uw_context ctx, char c) {
|
adamc@311
|
1520 uw_check(ctx, 1);
|
adamc@311
|
1521 uw_writec_unsafe(ctx, c);
|
adamc@117
|
1522 }
|
adamc@117
|
1523
|
adamc@311
|
1524 static void uw_write_unsafe(uw_context ctx, const char* s) {
|
adamc@117
|
1525 int len = strlen(s);
|
adamc@666
|
1526 memcpy(ctx->page.front, s, len);
|
adamc@666
|
1527 ctx->page.front += len;
|
adamc@117
|
1528 }
|
adamc@117
|
1529
|
adamc@311
|
1530 void uw_write(uw_context ctx, const char* s) {
|
adamc@311
|
1531 uw_check(ctx, strlen(s) + 1);
|
adamc@311
|
1532 uw_write_unsafe(ctx, s);
|
adamc@666
|
1533 *ctx->page.front = 0;
|
adamc@102
|
1534 }
|
adamc@106
|
1535
|
adamc@311
|
1536 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@136
|
1537 char *result;
|
adamc@136
|
1538 int len;
|
adamc@311
|
1539 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1540 result = ctx->heap.front;
|
adamc@276
|
1541 sprintf(result, "%lld%n", n, &len);
|
adamc@666
|
1542 ctx->heap.front += len+1;
|
adamc@136
|
1543 return result;
|
adamc@106
|
1544 }
|
adamc@106
|
1545
|
adamc@311
|
1546 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@136
|
1547 char *result;
|
adamc@136
|
1548 int len;
|
adamc@311
|
1549 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1550 result = ctx->heap.front;
|
adamc@136
|
1551 sprintf(result, "%g%n", n, &len);
|
adamc@666
|
1552 ctx->heap.front += len+1;
|
adamc@136
|
1553 return result;
|
adamc@106
|
1554 }
|
adamc@106
|
1555
|
adamc@311
|
1556 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@136
|
1557 int len = strlen(s);
|
adamc@136
|
1558 char *result, *p;
|
adamc@311
|
1559 uw_check_heap(ctx, len * 6 + 1);
|
adamc@136
|
1560
|
adamc@666
|
1561 result = p = ctx->heap.front;
|
adamc@136
|
1562
|
adamc@136
|
1563 for (; *s; s++) {
|
adamc@1054
|
1564 unsigned char c = *s;
|
adamc@136
|
1565
|
adamc@136
|
1566 if (c == '"') {
|
adamc@136
|
1567 strcpy(p, """);
|
adamc@136
|
1568 p += 6;
|
adamc@136
|
1569 } else if (c == '&') {
|
adamc@136
|
1570 strcpy(p, "&");
|
adamc@136
|
1571 p += 5;
|
adamc@136
|
1572 }
|
adamc@1059
|
1573 else
|
adamc@136
|
1574 *p++ = c;
|
adamc@136
|
1575 }
|
adamc@136
|
1576
|
adamc@137
|
1577 *p++ = 0;
|
adamc@666
|
1578 ctx->heap.front = p;
|
adamc@136
|
1579 return result;
|
adamc@106
|
1580 }
|
adamc@106
|
1581
|
adamc@1011
|
1582 char *uw_Basis_attrifyChar(uw_context ctx, uw_Basis_char c) {
|
adamc@1011
|
1583 char *result, *p;
|
adamc@1011
|
1584 uw_check_heap(ctx, 7);
|
adamc@1011
|
1585
|
adamc@1011
|
1586 result = p = ctx->heap.front;
|
adamc@1011
|
1587
|
adamc@1011
|
1588 if (c == '"') {
|
adamc@1011
|
1589 strcpy(p, """);
|
adamc@1011
|
1590 p += 6;
|
adamc@1011
|
1591 } else if (c == '&') {
|
adamc@1011
|
1592 strcpy(p, "&");
|
adamc@1011
|
1593 p += 5;
|
adamc@1011
|
1594 }
|
adamc@1059
|
1595 else
|
adamc@1011
|
1596 *p++ = c;
|
adamc@1011
|
1597
|
adamc@1011
|
1598 *p++ = 0;
|
adamc@1011
|
1599 ctx->heap.front = p;
|
adamc@1011
|
1600 return result;
|
adamc@1011
|
1601 }
|
adamc@1011
|
1602
|
adamc@721
|
1603 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
|
adamc@721
|
1604 return s;
|
adamc@721
|
1605 }
|
adamc@721
|
1606
|
adamc@311
|
1607 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@117
|
1608 int len;
|
adamc@117
|
1609
|
adamc@666
|
1610 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1611 ctx->page.front += len;
|
adamc@106
|
1612 }
|
adamc@106
|
1613
|
adamc@428
|
1614 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
1615 uw_check(ctx, INTS_MAX);
|
adamc@311
|
1616 uw_Basis_attrifyInt_w_unsafe(ctx, n);
|
adamc@428
|
1617
|
adamc@428
|
1618 return uw_unit_v;
|
adamc@106
|
1619 }
|
adamc@106
|
1620
|
adamc@428
|
1621 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@117
|
1622 int len;
|
adamc@117
|
1623
|
adamc@311
|
1624 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1625 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1626 ctx->page.front += len;
|
adamc@428
|
1627
|
adamc@428
|
1628 return uw_unit_v;
|
adamc@117
|
1629 }
|
adamc@117
|
1630
|
adamc@428
|
1631 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1632 uw_check(ctx, strlen(s) * 6);
|
adamc@117
|
1633
|
adamc@106
|
1634 for (; *s; s++) {
|
adamc@1054
|
1635 unsigned char c = *s;
|
adamc@106
|
1636
|
adamc@106
|
1637 if (c == '"')
|
adamc@311
|
1638 uw_write_unsafe(ctx, """);
|
adamc@136
|
1639 else if (c == '&')
|
adamc@311
|
1640 uw_write_unsafe(ctx, "&");
|
adamc@1059
|
1641 else
|
adamc@311
|
1642 uw_writec_unsafe(ctx, c);
|
adamc@106
|
1643 }
|
adamc@428
|
1644
|
adamc@428
|
1645 return uw_unit_v;
|
adamc@106
|
1646 }
|
adamc@120
|
1647
|
adamc@1011
|
1648 uw_unit uw_Basis_attrifyChar_w(uw_context ctx, uw_Basis_char c) {
|
adamc@1011
|
1649 uw_check(ctx, 6);
|
adamc@1011
|
1650
|
adamc@1011
|
1651 if (c == '"')
|
adamc@1011
|
1652 uw_write_unsafe(ctx, """);
|
adamc@1011
|
1653 else if (c == '&')
|
adamc@1011
|
1654 uw_write_unsafe(ctx, "&");
|
adamc@1059
|
1655 else
|
adamc@1011
|
1656 uw_writec_unsafe(ctx, c);
|
adamc@1011
|
1657
|
adamc@1011
|
1658 return uw_unit_v;
|
adamc@1011
|
1659 }
|
adamc@1011
|
1660
|
adamc@120
|
1661
|
adamc@311
|
1662 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@137
|
1663 int len;
|
adamc@137
|
1664 char *r;
|
adamc@137
|
1665
|
adamc@311
|
1666 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1667 r = ctx->heap.front;
|
adamc@276
|
1668 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1669 ctx->heap.front += len+1;
|
adamc@137
|
1670 return r;
|
adamc@120
|
1671 }
|
adamc@120
|
1672
|
adamc@682
|
1673 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1674 if (ctx->client == NULL || chn.cli != ctx->client->id)
|
adamc@682
|
1675 return "";
|
adamc@682
|
1676 else {
|
adamc@682
|
1677 int len;
|
adamc@682
|
1678 char *r;
|
adamc@679
|
1679
|
adamc@682
|
1680 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
1681 r = ctx->heap.front;
|
adamc@682
|
1682 sprintf(r, "%u%n", chn.chn, &len);
|
adamc@682
|
1683 ctx->heap.front += len+1;
|
adamc@682
|
1684 return r;
|
adamc@682
|
1685 }
|
adamc@679
|
1686 }
|
adamc@679
|
1687
|
adamc@311
|
1688 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@137
|
1689 int len;
|
adamc@137
|
1690 char *r;
|
adamc@137
|
1691
|
adamc@311
|
1692 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1693 r = ctx->heap.front;
|
adamc@137
|
1694 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1695 ctx->heap.front += len+1;
|
adamc@137
|
1696 return r;
|
adamc@120
|
1697 }
|
adamc@120
|
1698
|
adamc@311
|
1699 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
1700 char *r, *p;
|
adamc@137
|
1701
|
adamc@925
|
1702 if (s[0] == '\0')
|
adamc@925
|
1703 return "_";
|
adamc@925
|
1704
|
adamc@925
|
1705 uw_check_heap(ctx, strlen(s) * 3 + 1 + !!(s[0] == '_'));
|
adamc@925
|
1706
|
adamc@925
|
1707 r = p = ctx->heap.front;
|
adamc@925
|
1708 if (s[0] == '_')
|
adamc@925
|
1709 *p++ = '_';
|
adamc@925
|
1710
|
adamc@925
|
1711 for (; *s; s++) {
|
adamc@1087
|
1712 unsigned char c = *s;
|
adamc@137
|
1713
|
adamc@137
|
1714 if (c == ' ')
|
adamc@137
|
1715 *p++ = '+';
|
adamc@137
|
1716 else if (isalnum(c))
|
adamc@137
|
1717 *p++ = c;
|
adamc@137
|
1718 else {
|
adamc@1259
|
1719 sprintf(p, ".%02X", c);
|
adamc@137
|
1720 p += 3;
|
adamc@137
|
1721 }
|
adamc@137
|
1722 }
|
adamc@137
|
1723
|
adamc@137
|
1724 *p++ = 0;
|
adamc@666
|
1725 ctx->heap.front = p;
|
adamc@137
|
1726 return r;
|
adamc@120
|
1727 }
|
adamc@120
|
1728
|
adamc@311
|
1729 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1730 if (b == uw_Basis_False)
|
adamc@186
|
1731 return "0";
|
adamc@186
|
1732 else
|
adamc@186
|
1733 return "1";
|
adamc@186
|
1734 }
|
adamc@186
|
1735
|
adamc@311
|
1736 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@120
|
1737 int len;
|
adamc@120
|
1738
|
adamc@666
|
1739 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1740 ctx->page.front += len;
|
adamc@120
|
1741 }
|
adamc@120
|
1742
|
adamc@428
|
1743 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
1744 uw_check(ctx, INTS_MAX);
|
adamc@311
|
1745 uw_Basis_urlifyInt_w_unsafe(ctx, n);
|
adamc@428
|
1746
|
adamc@428
|
1747 return uw_unit_v;
|
adamc@120
|
1748 }
|
adamc@120
|
1749
|
adamc@682
|
1750 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
1751 if (ctx->client != NULL && chn.cli == ctx->client->id) {
|
adamc@682
|
1752 int len;
|
adamc@682
|
1753
|
adamc@682
|
1754 uw_check(ctx, INTS_MAX + 1);
|
adamc@682
|
1755 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
|
adamc@682
|
1756 ctx->page.front += len;
|
adamc@682
|
1757 }
|
adamc@682
|
1758
|
adamc@679
|
1759 return uw_unit_v;
|
adamc@679
|
1760 }
|
adamc@679
|
1761
|
adamc@428
|
1762 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@120
|
1763 int len;
|
adamc@120
|
1764
|
adamc@311
|
1765 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
1766 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
1767 ctx->page.front += len;
|
adamc@428
|
1768
|
adamc@428
|
1769 return uw_unit_v;
|
adamc@120
|
1770 }
|
adamc@120
|
1771
|
adamc@488
|
1772 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@1376
|
1773 return uw_Basis_urlifyInt(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds);
|
adamc@488
|
1774 }
|
adamc@488
|
1775
|
adam@1404
|
1776 uw_unit uw_Basis_urlifyTime_w(uw_context ctx, uw_Basis_time t) {
|
adam@1404
|
1777 return uw_Basis_urlifyInt_w(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds);
|
adam@1404
|
1778 }
|
adam@1404
|
1779
|
adamc@428
|
1780 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@925
|
1781 if (s[0] == '\0') {
|
adamc@925
|
1782 uw_check(ctx, 1);
|
adamc@925
|
1783 uw_writec_unsafe(ctx, '_');
|
adamc@925
|
1784 return uw_unit_v;
|
adamc@925
|
1785 }
|
adamc@925
|
1786
|
adamc@925
|
1787 uw_check(ctx, strlen(s) * 3 + !!(s[0] == '_'));
|
adamc@925
|
1788
|
adamc@925
|
1789 if (s[0] == '_')
|
adamc@925
|
1790 uw_writec_unsafe(ctx, '_');
|
adamc@120
|
1791
|
adamc@120
|
1792 for (; *s; s++) {
|
adamc@1087
|
1793 unsigned char c = *s;
|
adamc@120
|
1794
|
adamc@120
|
1795 if (c == ' ')
|
adamc@311
|
1796 uw_writec_unsafe(ctx, '+');
|
adamc@120
|
1797 else if (isalnum(c))
|
adamc@311
|
1798 uw_writec_unsafe(ctx, c);
|
adamc@120
|
1799 else {
|
adamc@1259
|
1800 sprintf(ctx->page.front, ".%02X", c);
|
adamc@666
|
1801 ctx->page.front += 3;
|
adamc@120
|
1802 }
|
adamc@120
|
1803 }
|
adamc@428
|
1804
|
adamc@428
|
1805 return uw_unit_v;
|
adamc@120
|
1806 }
|
adamc@120
|
1807
|
adamc@428
|
1808 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1809 if (b == uw_Basis_False)
|
adamc@311
|
1810 uw_writec(ctx, '0');
|
adamc@186
|
1811 else
|
adamc@311
|
1812 uw_writec(ctx, '1');
|
adamc@428
|
1813
|
adamc@428
|
1814 return uw_unit_v;
|
adamc@186
|
1815 }
|
adamc@186
|
1816
|
adamc@120
|
1817
|
adamc@311
|
1818 static char *uw_unurlify_advance(char *s) {
|
adamc@144
|
1819 char *new_s = strchr(s, '/');
|
adamc@120
|
1820
|
adamc@120
|
1821 if (new_s)
|
adamc@120
|
1822 *new_s++ = 0;
|
adamc@120
|
1823 else
|
adamc@144
|
1824 new_s = strchr(s, 0);
|
adamc@144
|
1825
|
adamc@144
|
1826 return new_s;
|
adamc@144
|
1827 }
|
adamc@144
|
1828
|
adamc@311
|
1829 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
|
adamc@311
|
1830 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1831 uw_Basis_int r;
|
adamc@120
|
1832
|
adamc@276
|
1833 r = atoll(*s);
|
adamc@120
|
1834 *s = new_s;
|
adamc@120
|
1835 return r;
|
adamc@120
|
1836 }
|
adamc@120
|
1837
|
adamc@311
|
1838 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
|
adamc@311
|
1839 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1840 uw_Basis_float r;
|
adamc@120
|
1841
|
adamc@120
|
1842 r = atof(*s);
|
adamc@120
|
1843 *s = new_s;
|
adamc@120
|
1844 return r;
|
adamc@120
|
1845 }
|
adamc@120
|
1846
|
adamc@488
|
1847 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
|
adam@1365
|
1848 uw_Basis_int n = uw_Basis_unurlifyInt(ctx, s);
|
adam@1365
|
1849 uw_Basis_time r = {n / 1000000, n % 1000000};
|
adam@1365
|
1850 return r;
|
adamc@488
|
1851 }
|
adamc@488
|
1852
|
adamc@1023
|
1853 static uw_Basis_string uw_unurlifyString_to(int fromClient, uw_context ctx, char *r, char *s) {
|
adamc@927
|
1854 char *s1, *s2 = s;
|
adamc@144
|
1855 int n;
|
adamc@136
|
1856
|
adamc@1023
|
1857 if (!fromClient) {
|
adamc@1023
|
1858 if (*s2 == '_')
|
adamc@1023
|
1859 ++s2;
|
adamc@1259
|
1860 else if ((s2[0] == '%' || s2[0] == '.') && s2[1] == '5' && (s2[2] == 'f' || s2[2] == 'F'))
|
adamc@1023
|
1861 s2 += 3;
|
adamc@1023
|
1862 }
|
adamc@925
|
1863
|
adamc@927
|
1864 for (s1 = r; *s2; ++s1, ++s2) {
|
adamc@1087
|
1865 unsigned char c = *s2;
|
adamc@136
|
1866
|
adamc@136
|
1867 switch (c) {
|
adamc@136
|
1868 case '+':
|
adamc@136
|
1869 *s1 = ' ';
|
adamc@136
|
1870 break;
|
adamc@136
|
1871 case '%':
|
adamc@169
|
1872 if (s2[1] == 0)
|
adamc@311
|
1873 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
|
adamc@169
|
1874 if (s2[2] == 0)
|
adamc@311
|
1875 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
|
adamc@169
|
1876 if (sscanf(s2+1, "%02X", &n) != 1)
|
adamc@311
|
1877 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
|
adamc@136
|
1878 *s1 = n;
|
adamc@136
|
1879 s2 += 2;
|
adamc@136
|
1880 break;
|
adamc@1259
|
1881 case '.':
|
adamc@1259
|
1882 if (!fromClient) {
|
adamc@1259
|
1883 if (s2[1] == 0)
|
adamc@1259
|
1884 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
|
adamc@1259
|
1885 if (s2[2] == 0)
|
adamc@1259
|
1886 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
|
adamc@1259
|
1887 if (sscanf(s2+1, "%02X", &n) != 1)
|
adamc@1259
|
1888 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
|
adamc@1259
|
1889 *s1 = n;
|
adamc@1259
|
1890 s2 += 2;
|
adamc@1259
|
1891 break;
|
adamc@1259
|
1892 }
|
adamc@136
|
1893 default:
|
adamc@136
|
1894 *s1 = c;
|
adamc@136
|
1895 }
|
adamc@136
|
1896 }
|
adamc@136
|
1897 *s1++ = 0;
|
adamc@144
|
1898 return s1;
|
adamc@144
|
1899 }
|
adamc@144
|
1900
|
adamc@311
|
1901 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
|
adamc@311
|
1902 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
1903 uw_Basis_bool r;
|
adamc@186
|
1904
|
adamc@186
|
1905 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
|
adamc@311
|
1906 r = uw_Basis_False;
|
adamc@186
|
1907 else
|
adamc@311
|
1908 r = uw_Basis_True;
|
adamc@186
|
1909
|
adamc@186
|
1910 *s = new_s;
|
adamc@186
|
1911 return r;
|
adamc@186
|
1912 }
|
adamc@186
|
1913
|
adamc@311
|
1914 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
|
adamc@311
|
1915 char *new_s = uw_unurlify_advance(*s);
|
adamc@1134
|
1916 char *r;
|
adamc@1134
|
1917 int len;
|
adamc@144
|
1918
|
adamc@200
|
1919 len = strlen(*s);
|
adamc@311
|
1920 uw_check_heap(ctx, len + 1);
|
adamc@144
|
1921
|
adamc@666
|
1922 r = ctx->heap.front;
|
adamc@1023
|
1923 ctx->heap.front = uw_unurlifyString_to(0, ctx, ctx->heap.front, *s);
|
adamc@1023
|
1924 *s = new_s;
|
adamc@1023
|
1925 return r;
|
adamc@1023
|
1926 }
|
adamc@1023
|
1927
|
adamc@1109
|
1928 uw_Basis_unit uw_Basis_unurlifyUnit(uw_context ctx, char **s) {
|
adamc@1109
|
1929 *s = uw_unurlify_advance(*s);
|
adamc@1109
|
1930 return uw_unit_v;
|
adamc@1109
|
1931 }
|
adamc@1109
|
1932
|
adamc@1023
|
1933 uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) {
|
adamc@1023
|
1934 char *new_s = uw_unurlify_advance(*s);
|
adamc@1134
|
1935 char *r;
|
adamc@1134
|
1936 int len;
|
adamc@1023
|
1937
|
adamc@1023
|
1938 len = strlen(*s);
|
adamc@1023
|
1939 uw_check_heap(ctx, len + 1);
|
adamc@1023
|
1940
|
adamc@1023
|
1941 r = ctx->heap.front;
|
adamc@1023
|
1942 ctx->heap.front = uw_unurlifyString_to(1, ctx, ctx->heap.front, *s);
|
adamc@136
|
1943 *s = new_s;
|
adamc@136
|
1944 return r;
|
adamc@120
|
1945 }
|
adamc@135
|
1946
|
adamc@135
|
1947
|
adamc@311
|
1948 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
1949 int len;
|
adamc@286
|
1950 char *r;
|
adamc@286
|
1951
|
adamc@311
|
1952 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
1953 r = ctx->heap.front;
|
adamc@286
|
1954 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
1955 ctx->heap.front += len+1;
|
adamc@286
|
1956 return r;
|
adamc@286
|
1957 }
|
adamc@286
|
1958
|
adamc@428
|
1959 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
1960 int len;
|
adamc@286
|
1961
|
adamc@311
|
1962 uw_check(ctx, INTS_MAX);
|
adamc@666
|
1963 sprintf(ctx->page.front, "%lld%n", n, &len);
|
adamc@666
|
1964 ctx->page.front += len;
|
adamc@428
|
1965
|
adamc@428
|
1966 return uw_unit_v;
|
adamc@286
|
1967 }
|
adamc@286
|
1968
|
adam@1358
|
1969 char *uw_Basis_htmlifySpecialChar(uw_context ctx, unsigned char ch) {
|
adam@1358
|
1970 unsigned int n = ch;
|
adam@1358
|
1971 int len;
|
adam@1358
|
1972 char *r;
|
adam@1358
|
1973
|
adam@1358
|
1974 uw_check_heap(ctx, INTS_MAX+3);
|
adam@1358
|
1975 r = ctx->heap.front;
|
adam@1358
|
1976 sprintf(r, "&#%u;%n", n, &len);
|
adam@1358
|
1977 ctx->heap.front += len+1;
|
adam@1358
|
1978 return r;
|
adam@1358
|
1979 }
|
adam@1358
|
1980
|
adam@1358
|
1981 uw_unit uw_Basis_htmlifySpecialChar_w(uw_context ctx, unsigned char ch) {
|
adam@1358
|
1982 unsigned int n = ch;
|
adam@1358
|
1983 int len;
|
adam@1358
|
1984
|
adam@1358
|
1985 uw_check(ctx, INTS_MAX+3);
|
adam@1358
|
1986 sprintf(ctx->page.front, "&#%u;%n", n, &len);
|
adam@1358
|
1987 ctx->page.front += len;
|
adam@1358
|
1988 return uw_unit_v;
|
adam@1358
|
1989 }
|
adam@1358
|
1990
|
adamc@311
|
1991 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
1992 int len;
|
adamc@286
|
1993 char *r;
|
adamc@286
|
1994
|
adamc@311
|
1995 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
1996 r = ctx->heap.front;
|
adamc@286
|
1997 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
1998 ctx->heap.front += len+1;
|
adamc@286
|
1999 return r;
|
adamc@286
|
2000 }
|
adamc@286
|
2001
|
adamc@428
|
2002 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
2003 int len;
|
adamc@286
|
2004
|
adamc@311
|
2005 uw_check(ctx, FLOATS_MAX);
|
adamc@666
|
2006 sprintf(ctx->page.front, "%g%n", n, &len);
|
adamc@666
|
2007 ctx->page.front += len;
|
adamc@428
|
2008
|
adamc@428
|
2009 return uw_unit_v;
|
adamc@286
|
2010 }
|
adamc@286
|
2011
|
adam@1365
|
2012 char *uw_Basis_jsifyTime(uw_context ctx, uw_Basis_time t) {
|
adam@1360
|
2013 int len;
|
adam@1360
|
2014 char *r;
|
adam@1360
|
2015
|
adam@1360
|
2016 uw_check_heap(ctx, INTS_MAX);
|
adam@1360
|
2017 r = ctx->heap.front;
|
adamc@1377
|
2018 sprintf(r, "%lld%n", (uw_Basis_int)t.seconds * 1000000 + t.microseconds, &len);
|
adam@1360
|
2019 ctx->heap.front += len+1;
|
adam@1360
|
2020 return r;
|
adam@1360
|
2021 }
|
adam@1360
|
2022
|
adam@1365
|
2023 uw_unit uw_Basis_jsifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adam@1360
|
2024 int len;
|
adam@1360
|
2025
|
adam@1360
|
2026 uw_check(ctx, INTS_MAX);
|
adam@1360
|
2027 sprintf(ctx->page.front, "%lld%n", (uw_Basis_int)n, &len);
|
adam@1360
|
2028 ctx->page.front += len;
|
adam@1360
|
2029
|
adam@1360
|
2030 return uw_unit_v;
|
adam@1360
|
2031 }
|
adam@1360
|
2032
|
adamc@311
|
2033 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
2034 char *r, *s2;
|
adamc@137
|
2035
|
adamc@311
|
2036 uw_check_heap(ctx, strlen(s) * 5 + 1);
|
adamc@137
|
2037
|
adamc@666
|
2038 for (r = s2 = ctx->heap.front; *s; s++) {
|
adamc@1054
|
2039 unsigned char c = *s;
|
adamc@137
|
2040
|
adamc@137
|
2041 switch (c) {
|
adamc@137
|
2042 case '<':
|
adamc@137
|
2043 strcpy(s2, "<");
|
adamc@137
|
2044 s2 += 4;
|
adamc@137
|
2045 break;
|
adamc@137
|
2046 case '&':
|
adamc@137
|
2047 strcpy(s2, "&");
|
adamc@137
|
2048 s2 += 5;
|
adamc@137
|
2049 break;
|
adamc@137
|
2050 default:
|
adamc@1059
|
2051 *s2++ = c;
|
adamc@137
|
2052 }
|
adamc@137
|
2053 }
|
adamc@137
|
2054
|
adamc@137
|
2055 *s2++ = 0;
|
adamc@666
|
2056 ctx->heap.front = s2;
|
adamc@137
|
2057 return r;
|
adamc@135
|
2058 }
|
adamc@135
|
2059
|
adamc@428
|
2060 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@321
|
2061 uw_check(ctx, strlen(s) * 6);
|
adamc@135
|
2062
|
adamc@135
|
2063 for (; *s; s++) {
|
adamc@1054
|
2064 unsigned char c = *s;
|
adamc@135
|
2065
|
adamc@135
|
2066 switch (c) {
|
adamc@135
|
2067 case '<':
|
adamc@311
|
2068 uw_write_unsafe(ctx, "<");
|
adamc@135
|
2069 break;
|
adamc@135
|
2070 case '&':
|
adamc@311
|
2071 uw_write_unsafe(ctx, "&");
|
adamc@135
|
2072 break;
|
adamc@135
|
2073 default:
|
adamc@1059
|
2074 uw_writec_unsafe(ctx, c);
|
adamc@135
|
2075 }
|
adamc@135
|
2076 }
|
adamc@428
|
2077
|
adamc@428
|
2078 return uw_unit_v;
|
adamc@135
|
2079 }
|
adamc@180
|
2080
|
adamc@311
|
2081 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2082 if (b == uw_Basis_False)
|
adamc@286
|
2083 return "False";
|
adamc@286
|
2084 else
|
adamc@286
|
2085 return "True";
|
adamc@286
|
2086 }
|
adamc@286
|
2087
|
adamc@428
|
2088 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2089 if (b == uw_Basis_False) {
|
adamc@311
|
2090 uw_check(ctx, 6);
|
adamc@666
|
2091 strcpy(ctx->page.front, "False");
|
adamc@666
|
2092 ctx->page.front += 5;
|
adamc@286
|
2093 } else {
|
adamc@311
|
2094 uw_check(ctx, 5);
|
adamc@666
|
2095 strcpy(ctx->page.front, "True");
|
adamc@666
|
2096 ctx->page.front += 4;
|
adamc@286
|
2097 }
|
adamc@428
|
2098
|
adamc@428
|
2099 return uw_unit_v;
|
adamc@286
|
2100 }
|
adamc@286
|
2101
|
adamc@436
|
2102 #define TIME_FMT "%x %X"
|
adamc@438
|
2103 #define TIME_FMT_PG "%Y-%m-%d %T"
|
adamc@436
|
2104
|
adamc@436
|
2105 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
2106 size_t len;
|
adamc@436
|
2107 char *r;
|
adam@1392
|
2108 struct tm stm = {};
|
adam@1392
|
2109 stm.tm_isdst = -1;
|
adamc@436
|
2110
|
adam@1365
|
2111 if (localtime_r(&t.seconds, &stm)) {
|
adamc@436
|
2112 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
2113 r = ctx->heap.front;
|
adamc@436
|
2114 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
2115 ctx->heap.front += len+1;
|
adamc@436
|
2116 return r;
|
adamc@436
|
2117 } else
|
adamc@436
|
2118 return "<i>Invalid time</i>";
|
adamc@436
|
2119 }
|
adamc@436
|
2120
|
adamc@436
|
2121 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
2122 size_t len;
|
adamc@436
|
2123 char *r;
|
adam@1392
|
2124 struct tm stm = {};
|
adam@1392
|
2125 stm.tm_isdst = -1;
|
adamc@436
|
2126
|
adam@1365
|
2127 if (localtime_r(&t.seconds, &stm)) {
|
adamc@436
|
2128 uw_check(ctx, TIMES_MAX);
|
adamc@666
|
2129 r = ctx->page.front;
|
adamc@436
|
2130 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
2131 ctx->page.front += len;
|
adamc@436
|
2132 } else {
|
adamc@436
|
2133 uw_check(ctx, 20);
|
adamc@666
|
2134 strcpy(ctx->page.front, "<i>Invalid time</i>");
|
adamc@666
|
2135 ctx->page.front += 19;
|
adamc@436
|
2136 }
|
adamc@436
|
2137
|
adamc@436
|
2138 return uw_unit_v;
|
adamc@436
|
2139 }
|
adamc@436
|
2140
|
adamc@821
|
2141 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
|
adam@1388
|
2142 while (n >= 0) {
|
adam@1388
|
2143 if (*s == 0)
|
adam@1388
|
2144 uw_error(ctx, FATAL, "Out-of-bounds strsub");
|
adam@1388
|
2145
|
adam@1388
|
2146 if (n == 0)
|
adam@1388
|
2147 return *s;
|
adam@1388
|
2148
|
adam@1388
|
2149 --n;
|
adam@1388
|
2150 ++s;
|
adam@1388
|
2151 }
|
adam@1388
|
2152
|
adam@1388
|
2153 uw_error(ctx, FATAL, "Negative strsub bound");
|
adamc@821
|
2154 }
|
adamc@821
|
2155
|
adamc@821
|
2156 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
|
adam@1388
|
2157 while (n >= 0) {
|
adam@1388
|
2158 if (*s == 0 || n == 0)
|
adam@1388
|
2159 return s;
|
adam@1388
|
2160
|
adam@1388
|
2161 --n;
|
adam@1388
|
2162 ++s;
|
adam@1388
|
2163 }
|
adam@1388
|
2164
|
adam@1388
|
2165 uw_error(ctx, FATAL, "Negative strsuffix bound");
|
adamc@821
|
2166 }
|
adamc@821
|
2167
|
adamc@828
|
2168 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
|
adamc@828
|
2169 return strlen(s);
|
adamc@828
|
2170 }
|
adamc@828
|
2171
|
adam@1388
|
2172 uw_Basis_bool uw_Basis_strlenGe(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
|
adam@1388
|
2173 while (n > 0) {
|
adam@1388
|
2174 if (*s == 0)
|
adam@1388
|
2175 return uw_Basis_False;
|
adam@1388
|
2176
|
adam@1388
|
2177 --n;
|
adam@1388
|
2178 ++s;
|
adam@1388
|
2179 }
|
adam@1388
|
2180
|
adam@1388
|
2181 return uw_Basis_True;
|
adam@1388
|
2182 }
|
adam@1388
|
2183
|
adamc@829
|
2184 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
|
adamc@829
|
2185 return strchr(s, ch);
|
adamc@829
|
2186 }
|
adamc@829
|
2187
|
adamc@831
|
2188 uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) {
|
adamc@831
|
2189 return strcspn(s, chs);
|
adamc@831
|
2190 }
|
adamc@831
|
2191
|
adamc@829
|
2192 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
|
adamc@829
|
2193 uw_Basis_string r = strchr(s, ch);
|
adamc@829
|
2194 if (r == NULL)
|
adamc@829
|
2195 return NULL;
|
adamc@829
|
2196 else {
|
adamc@829
|
2197 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adamc@829
|
2198 *nr = r - s;
|
adamc@829
|
2199 return nr;
|
adamc@829
|
2200 }
|
adamc@829
|
2201 }
|
adamc@829
|
2202
|
adam@1390
|
2203 uw_Basis_int *uw_Basis_strsindex(uw_context ctx, const char *haystack, const char *needle) {
|
adam@1390
|
2204 uw_Basis_string r = strstr(haystack, needle);
|
adam@1390
|
2205 if (r == NULL)
|
adam@1390
|
2206 return NULL;
|
adam@1390
|
2207 else {
|
adam@1390
|
2208 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adam@1390
|
2209 *nr = r - haystack;
|
adam@1390
|
2210 return nr;
|
adam@1390
|
2211 }
|
adam@1390
|
2212 }
|
adam@1390
|
2213
|
adamc@311
|
2214 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
|
adamc@828
|
2215 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
|
adamc@180
|
2216 char *s;
|
adamc@180
|
2217
|
adamc@311
|
2218 uw_check_heap(ctx, len);
|
adamc@180
|
2219
|
adamc@666
|
2220 s = ctx->heap.front;
|
adamc@180
|
2221
|
adamc@180
|
2222 strcpy(s, s1);
|
adamc@180
|
2223 strcat(s, s2);
|
adamc@666
|
2224 ctx->heap.front += len;
|
adamc@180
|
2225
|
adamc@180
|
2226 return s;
|
adamc@180
|
2227 }
|
adamc@278
|
2228
|
adamc@829
|
2229 uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) {
|
adamc@829
|
2230 size_t full_len = uw_Basis_strlen(ctx, s);
|
adamc@829
|
2231
|
adamc@829
|
2232 if (start < 0)
|
adamc@829
|
2233 uw_error(ctx, FATAL, "substring: Negative start index");
|
adamc@829
|
2234 if (len < 0)
|
adamc@829
|
2235 uw_error(ctx, FATAL, "substring: Negative length");
|
adamc@829
|
2236 if (start + len > full_len)
|
adamc@829
|
2237 uw_error(ctx, FATAL, "substring: Start index plus length is too large");
|
adamc@829
|
2238
|
adamc@829
|
2239 if (start + len == full_len)
|
adamc@829
|
2240 return &s[start];
|
adamc@829
|
2241 else {
|
adamc@829
|
2242 uw_Basis_string r = uw_malloc(ctx, len+1);
|
adamc@959
|
2243 memcpy(r, s+start, len);
|
adamc@829
|
2244 r[len] = 0;
|
adamc@829
|
2245 return r;
|
adamc@829
|
2246 }
|
adamc@829
|
2247
|
adamc@829
|
2248 }
|
adamc@829
|
2249
|
adamc@1023
|
2250 uw_Basis_string uw_Basis_str1(uw_context ctx, uw_Basis_char ch) {
|
adamc@1023
|
2251 char *r;
|
adamc@1023
|
2252
|
adamc@1023
|
2253 uw_check_heap(ctx, 2);
|
adamc@1023
|
2254 r = ctx->heap.front;
|
adamc@1023
|
2255 r[0] = ch;
|
adamc@1023
|
2256 r[1] = 0;
|
adamc@1023
|
2257
|
adamc@1023
|
2258 ctx->heap.front += 2;
|
adamc@1023
|
2259
|
adamc@1023
|
2260 return r;
|
adamc@1023
|
2261 }
|
adamc@1023
|
2262
|
adamc@737
|
2263 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@828
|
2264 int len = uw_Basis_strlen(ctx, s1) + 1;
|
adamc@278
|
2265 char *s;
|
adamc@278
|
2266
|
adamc@311
|
2267 uw_check_heap(ctx, len);
|
adamc@278
|
2268
|
adamc@666
|
2269 s = ctx->heap.front;
|
adamc@278
|
2270
|
adamc@278
|
2271 strcpy(s, s1);
|
adamc@666
|
2272 ctx->heap.front += len;
|
adamc@278
|
2273
|
adamc@278
|
2274 return s;
|
adamc@278
|
2275 }
|
adamc@280
|
2276
|
adam@1295
|
2277 uw_Basis_string uw_dup_and_clear_error_message(uw_context ctx) {
|
adam@1295
|
2278 if (ctx->error_message[0]) {
|
adam@1295
|
2279 char *s = uw_strdup(ctx, ctx->error_message);
|
adam@1295
|
2280 ctx->error_message[0] = 0;
|
adam@1295
|
2281 return s;
|
adam@1295
|
2282 } else
|
adam@1295
|
2283 return NULL;
|
adam@1295
|
2284 }
|
adam@1295
|
2285
|
adamc@737
|
2286 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@493
|
2287 if (s1)
|
adamc@737
|
2288 return uw_strdup(ctx, s1);
|
adamc@493
|
2289 else
|
adamc@493
|
2290 return NULL;
|
adamc@493
|
2291 }
|
adamc@493
|
2292
|
adamc@742
|
2293 char *uw_memdup(uw_context ctx, const char *p, size_t len) {
|
adamc@742
|
2294 char *r = uw_malloc(ctx, len);
|
adamc@742
|
2295 memcpy(r, p, len);
|
adamc@742
|
2296 return r;
|
adamc@742
|
2297 }
|
adamc@280
|
2298
|
adamc@879
|
2299 char *uw_sqlfmtInt = "%lld::int8%n";
|
adamc@879
|
2300
|
adamc@311
|
2301 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@281
|
2302 int len;
|
adamc@281
|
2303 char *r;
|
adamc@281
|
2304
|
adamc@311
|
2305 uw_check_heap(ctx, INTS_MAX + 6);
|
adamc@666
|
2306 r = ctx->heap.front;
|
adamc@879
|
2307 sprintf(r, uw_sqlfmtInt, n, &len);
|
adamc@666
|
2308 ctx->heap.front += len+1;
|
adamc@281
|
2309 return r;
|
adamc@281
|
2310 }
|
adamc@281
|
2311
|
adamc@467
|
2312 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
|
adamc@467
|
2313 if (n == NULL)
|
adamc@467
|
2314 return "NULL";
|
adamc@467
|
2315 else
|
adamc@467
|
2316 return uw_Basis_sqlifyInt(ctx, *n);
|
adamc@467
|
2317 }
|
adamc@467
|
2318
|
adamc@879
|
2319 char *uw_sqlfmtFloat = "%g::float8%n";
|
adamc@879
|
2320
|
adamc@311
|
2321 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@281
|
2322 int len;
|
adamc@281
|
2323 char *r;
|
adamc@281
|
2324
|
adamc@311
|
2325 uw_check_heap(ctx, FLOATS_MAX + 8);
|
adamc@666
|
2326 r = ctx->heap.front;
|
adamc@879
|
2327 sprintf(r, uw_sqlfmtFloat, n, &len);
|
adamc@666
|
2328 ctx->heap.front += len+1;
|
adamc@281
|
2329 return r;
|
adamc@281
|
2330 }
|
adamc@281
|
2331
|
adamc@467
|
2332 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
|
adamc@467
|
2333 if (n == NULL)
|
adamc@467
|
2334 return "NULL";
|
adamc@467
|
2335 else
|
adamc@467
|
2336 return uw_Basis_sqlifyFloat(ctx, *n);
|
adamc@467
|
2337 }
|
adamc@467
|
2338
|
adamc@879
|
2339 int uw_Estrings = 1;
|
adamc@879
|
2340 char *uw_sqlsuffixString = "::text";
|
adamc@1011
|
2341 char *uw_sqlsuffixChar = "::char";
|
adamc@281
|
2342
|
adamc@311
|
2343 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@280
|
2344 char *r, *s2;
|
adamc@280
|
2345
|
adamc@879
|
2346 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString));
|
adamc@280
|
2347
|
adamc@666
|
2348 r = s2 = ctx->heap.front;
|
adamc@879
|
2349 if (uw_Estrings)
|
adamc@879
|
2350 *s2++ = 'E';
|
adamc@280
|
2351 *s2++ = '\'';
|
adamc@280
|
2352
|
adamc@280
|
2353 for (; *s; s++) {
|
adamc@280
|
2354 char c = *s;
|
adamc@280
|
2355
|
adamc@280
|
2356 switch (c) {
|
adamc@280
|
2357 case '\'':
|
adamc@885
|
2358 if (uw_Estrings)
|
adamc@885
|
2359 strcpy(s2, "\\'");
|
adamc@885
|
2360 else
|
adamc@885
|
2361 strcpy(s2, "''");
|
adamc@280
|
2362 s2 += 2;
|
adamc@280
|
2363 break;
|
adamc@280
|
2364 case '\\':
|
adamc@885
|
2365 if (uw_Estrings) {
|
adamc@885
|
2366 strcpy(s2, "\\\\");
|
adamc@885
|
2367 s2 += 2;
|
adamc@885
|
2368 } else
|
adamc@885
|
2369 *s2++ = '\\';
|
adamc@280
|
2370 break;
|
adamc@280
|
2371 default:
|
adamc@1150
|
2372 if (isprint((int)c))
|
adamc@280
|
2373 *s2++ = c;
|
adamc@879
|
2374 else if (uw_Estrings) {
|
adamc@737
|
2375 sprintf(s2, "\\%03o", c);
|
adamc@280
|
2376 s2 += 4;
|
adamc@280
|
2377 }
|
adamc@879
|
2378 else
|
adamc@879
|
2379 uw_error(ctx, FATAL, "Non-printable character %u in string to SQLify", c);
|
adamc@280
|
2380 }
|
adamc@280
|
2381 }
|
adamc@280
|
2382
|
adamc@879
|
2383 *s2++ = '\'';
|
adamc@879
|
2384 strcpy(s2, uw_sqlsuffixString);
|
adamc@879
|
2385 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString);
|
adamc@280
|
2386 return r;
|
adamc@280
|
2387 }
|
adamc@281
|
2388
|
adamc@1011
|
2389 uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) {
|
adamc@1011
|
2390 char *r, *s2;
|
adamc@1011
|
2391
|
adamc@1011
|
2392 uw_check_heap(ctx, 5 + uw_Estrings + strlen(uw_sqlsuffixChar));
|
adamc@1011
|
2393
|
adamc@1011
|
2394 r = s2 = ctx->heap.front;
|
adamc@1011
|
2395 if (uw_Estrings)
|
adamc@1011
|
2396 *s2++ = 'E';
|
adamc@1011
|
2397 *s2++ = '\'';
|
adamc@1011
|
2398
|
adamc@1011
|
2399 switch (c) {
|
adamc@1011
|
2400 case '\'':
|
adamc@1011
|
2401 if (uw_Estrings)
|
adamc@1011
|
2402 strcpy(s2, "\\'");
|
adamc@1011
|
2403 else
|
adamc@1011
|
2404 strcpy(s2, "''");
|
adamc@1011
|
2405 s2 += 2;
|
adamc@1011
|
2406 break;
|
adamc@1011
|
2407 case '\\':
|
adamc@1011
|
2408 if (uw_Estrings) {
|
adamc@1011
|
2409 strcpy(s2, "\\\\");
|
adamc@1011
|
2410 s2 += 2;
|
adamc@1011
|
2411 } else
|
adamc@1011
|
2412 *s2++ = '\\';
|
adamc@1011
|
2413 break;
|
adamc@1011
|
2414 default:
|
adamc@1150
|
2415 if (isprint((int)c))
|
adamc@1011
|
2416 *s2++ = c;
|
adamc@1011
|
2417 else if (uw_Estrings) {
|
adamc@1011
|
2418 sprintf(s2, "\\%03o", c);
|
adamc@1011
|
2419 s2 += 4;
|
adamc@1011
|
2420 }
|
adamc@1011
|
2421 else
|
adamc@1011
|
2422 uw_error(ctx, FATAL, "Non-printable character %u in char to SQLify", c);
|
adamc@1011
|
2423 }
|
adamc@1011
|
2424
|
adamc@1011
|
2425 *s2++ = '\'';
|
adamc@1011
|
2426 strcpy(s2, uw_sqlsuffixChar);
|
adamc@1011
|
2427 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixChar);
|
adamc@1011
|
2428 return r;
|
adamc@1011
|
2429 }
|
adamc@1011
|
2430
|
adamc@879
|
2431 char *uw_sqlsuffixBlob = "::bytea";
|
adamc@879
|
2432
|
adamc@737
|
2433 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
|
adamc@737
|
2434 char *r, *s2;
|
adamc@737
|
2435 size_t i;
|
adamc@737
|
2436
|
adamc@879
|
2437 uw_check_heap(ctx, b.size * 5 + 3 + uw_Estrings + strlen(uw_sqlsuffixBlob));
|
adamc@737
|
2438
|
adamc@737
|
2439 r = s2 = ctx->heap.front;
|
adamc@879
|
2440 if (uw_Estrings)
|
adamc@879
|
2441 *s2++ = 'E';
|
adamc@737
|
2442 *s2++ = '\'';
|
adamc@737
|
2443
|
adamc@737
|
2444 for (i = 0; i < b.size; ++i) {
|
adamc@737
|
2445 char c = b.data[i];
|
adamc@737
|
2446
|
adamc@737
|
2447 switch (c) {
|
adamc@737
|
2448 case '\'':
|
adamc@885
|
2449 if (uw_Estrings)
|
adamc@885
|
2450 strcpy(s2, "\\'");
|
adamc@885
|
2451 else
|
adamc@885
|
2452 strcpy(s2, "''");
|
adamc@737
|
2453 s2 += 2;
|
adamc@737
|
2454 break;
|
adamc@737
|
2455 case '\\':
|
adamc@885
|
2456 if (uw_Estrings) {
|
adamc@885
|
2457 strcpy(s2, "\\\\\\\\");
|
adamc@885
|
2458 s2 += 4;
|
adamc@885
|
2459 } else
|
adamc@885
|
2460 *s2++ = '\\';
|
adamc@737
|
2461 break;
|
adamc@737
|
2462 default:
|
adamc@1150
|
2463 if (isprint((int)c))
|
adamc@737
|
2464 *s2++ = c;
|
adamc@879
|
2465 else if (uw_Estrings) {
|
adamc@737
|
2466 sprintf(s2, "\\\\%03o", c);
|
adamc@737
|
2467 s2 += 5;
|
adamc@737
|
2468 }
|
adamc@879
|
2469 else
|
adamc@879
|
2470 uw_error(ctx, FATAL, "Non-printable character %u in blob to SQLify", c);
|
adamc@737
|
2471 }
|
adamc@737
|
2472 }
|
adamc@737
|
2473
|
adamc@879
|
2474 *s2++ = '\'';
|
adamc@879
|
2475 strcpy(s2, uw_sqlsuffixBlob);
|
adamc@879
|
2476 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob);
|
adamc@737
|
2477 return r;
|
adamc@737
|
2478 }
|
adamc@737
|
2479
|
adamc@682
|
2480 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
2481 int len;
|
adamc@682
|
2482 char *r;
|
adamc@682
|
2483 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
|
adamc@682
|
2484
|
adamc@682
|
2485 uw_check_heap(ctx, INTS_MAX + 7);
|
adamc@682
|
2486 r = ctx->heap.front;
|
adamc@879
|
2487 sprintf(r, uw_sqlfmtInt, combo, &len);
|
adamc@682
|
2488 ctx->heap.front += len+1;
|
adamc@682
|
2489 return r;
|
adamc@682
|
2490 }
|
adamc@682
|
2491
|
adamc@682
|
2492 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
|
adamc@682
|
2493 int len;
|
adamc@682
|
2494 char *r;
|
adamc@682
|
2495 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
|
adamc@682
|
2496
|
adamc@682
|
2497 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
2498 r = ctx->heap.front;
|
adamc@682
|
2499 sprintf(r, "%lld%n", combo, &len);
|
adamc@682
|
2500 ctx->heap.front += len+1;
|
adamc@682
|
2501 return r;
|
adamc@682
|
2502 }
|
adamc@682
|
2503
|
adamc@879
|
2504 char *uw_sqlfmtUint4 = "%u::int4%n";
|
adamc@879
|
2505
|
adamc@682
|
2506 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
|
adamc@682
|
2507 int len;
|
adamc@682
|
2508 char *r;
|
adamc@682
|
2509
|
adamc@682
|
2510 uw_check_heap(ctx, INTS_MAX + 7);
|
adamc@682
|
2511 r = ctx->heap.front;
|
adamc@879
|
2512 sprintf(r, uw_sqlfmtUint4, cli, &len);
|
adamc@682
|
2513 ctx->heap.front += len+1;
|
adamc@682
|
2514 return r;
|
adamc@682
|
2515 }
|
adamc@682
|
2516
|
adamc@682
|
2517 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
|
adamc@682
|
2518 int len;
|
adamc@682
|
2519 char *r;
|
adamc@682
|
2520
|
adamc@682
|
2521 uw_check_heap(ctx, INTS_MAX + 1);
|
adamc@682
|
2522 r = ctx->heap.front;
|
adamc@682
|
2523 sprintf(r, "%u%n", cli, &len);
|
adamc@682
|
2524 ctx->heap.front += len+1;
|
adamc@682
|
2525 return r;
|
adamc@682
|
2526 }
|
adamc@682
|
2527
|
adamc@467
|
2528 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
|
adamc@467
|
2529 if (s == NULL)
|
adamc@467
|
2530 return "NULL";
|
adamc@467
|
2531 else
|
adamc@467
|
2532 return uw_Basis_sqlifyString(ctx, s);
|
adamc@467
|
2533 }
|
adamc@467
|
2534
|
adamc@311
|
2535 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2536 if (b == uw_Basis_False)
|
adamc@281
|
2537 return "FALSE";
|
adamc@281
|
2538 else
|
adamc@281
|
2539 return "TRUE";
|
adamc@281
|
2540 }
|
adamc@282
|
2541
|
adamc@467
|
2542 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
|
adamc@467
|
2543 if (b == NULL)
|
adamc@467
|
2544 return "NULL";
|
adamc@467
|
2545 else
|
adamc@467
|
2546 return uw_Basis_sqlifyBool(ctx, *b);
|
adamc@467
|
2547 }
|
adamc@467
|
2548
|
adamc@439
|
2549 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@439
|
2550 size_t len;
|
adamc@682
|
2551 char *r, *s;
|
adam@1392
|
2552 struct tm stm = {};
|
adam@1392
|
2553 stm.tm_isdst = -1;
|
adamc@682
|
2554
|
adam@1365
|
2555 if (localtime_r(&t.seconds, &stm)) {
|
adamc@682
|
2556 s = uw_malloc(ctx, TIMES_MAX);
|
adam@1364
|
2557 len = strftime(s, TIMES_MAX, TIME_FMT_PG, &stm);
|
adam@1367
|
2558 if (t.microseconds) {
|
adam@1367
|
2559 r = uw_malloc(ctx, len + 21);
|
adam@1367
|
2560 sprintf(r, "'%s.%06u'::timestamp", s, t.microseconds);
|
adam@1367
|
2561 } else {
|
adam@1367
|
2562 r = uw_malloc(ctx, len + 14);
|
adam@1367
|
2563 sprintf(r, "'%s'::timestamp", s);
|
adam@1367
|
2564 }
|
adamc@682
|
2565 return r;
|
adamc@682
|
2566 } else
|
adamc@682
|
2567 return "<Invalid time>";
|
adamc@682
|
2568 }
|
adamc@682
|
2569
|
adamc@682
|
2570 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@682
|
2571 size_t len;
|
adamc@439
|
2572 char *r;
|
adam@1392
|
2573 struct tm stm = {};
|
adam@1392
|
2574 stm.tm_isdst = -1;
|
adamc@439
|
2575
|
adam@1365
|
2576 if (localtime_r(&t.seconds, &stm)) {
|
adamc@439
|
2577 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
2578 r = ctx->heap.front;
|
adamc@439
|
2579 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
2580 ctx->heap.front += len+1;
|
adamc@439
|
2581 return r;
|
adamc@439
|
2582 } else
|
adamc@439
|
2583 return "<Invalid time>";
|
adamc@439
|
2584 }
|
adamc@439
|
2585
|
adam@1364
|
2586 char *uw_Basis_ensqlTime(uw_context ctx, uw_Basis_time t) {
|
adam@1364
|
2587 size_t len;
|
adam@1364
|
2588 char *r;
|
adam@1392
|
2589 struct tm stm = {};
|
adam@1392
|
2590 stm.tm_isdst = -1;
|
adam@1364
|
2591
|
adam@1365
|
2592 if (localtime_r(&t.seconds, &stm)) {
|
adam@1364
|
2593 uw_check_heap(ctx, TIMES_MAX);
|
adam@1364
|
2594 r = ctx->heap.front;
|
adam@1365
|
2595 len = strftime(r, TIMES_MAX-7, TIME_FMT_PG, &stm);
|
adam@1365
|
2596 ctx->heap.front += len;
|
adam@1365
|
2597 sprintf(ctx->heap.front, ".%06u", t.microseconds);
|
adam@1365
|
2598 ctx->heap.front += 8;
|
adam@1364
|
2599 return r;
|
adam@1364
|
2600 } else
|
adam@1364
|
2601 return "<Invalid time>";
|
adam@1364
|
2602 }
|
adam@1364
|
2603
|
adamc@467
|
2604 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
|
adamc@467
|
2605 if (t == NULL)
|
adamc@467
|
2606 return "NULL";
|
adamc@467
|
2607 else
|
adamc@467
|
2608 return uw_Basis_sqlifyTime(ctx, *t);
|
adamc@467
|
2609 }
|
adamc@467
|
2610
|
adamc@311
|
2611 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
|
adamc@311
|
2612 static uw_Basis_int true = 1;
|
adamc@311
|
2613 static uw_Basis_int false = 0;
|
adamc@282
|
2614
|
adamc@311
|
2615 if (b == uw_Basis_False)
|
adamc@282
|
2616 return (char *)&false;
|
adamc@282
|
2617 else
|
adamc@282
|
2618 return (char *)&true;
|
adamc@282
|
2619 }
|
adamc@284
|
2620
|
adamc@311
|
2621 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
|
adamc@284
|
2622 int len;
|
adamc@284
|
2623 char *r;
|
adamc@284
|
2624
|
adamc@311
|
2625 uw_check_heap(ctx, INTS_MAX);
|
adamc@666
|
2626 r = ctx->heap.front;
|
adamc@284
|
2627 sprintf(r, "%lld%n", n, &len);
|
adamc@666
|
2628 ctx->heap.front += len+1;
|
adamc@284
|
2629 return r;
|
adamc@284
|
2630 }
|
adamc@285
|
2631
|
adamc@311
|
2632 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
|
adamc@285
|
2633 int len;
|
adamc@285
|
2634 char *r;
|
adamc@285
|
2635
|
adamc@311
|
2636 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@666
|
2637 r = ctx->heap.front;
|
adamc@285
|
2638 sprintf(r, "%g%n", n, &len);
|
adamc@666
|
2639 ctx->heap.front += len+1;
|
adamc@285
|
2640 return r;
|
adamc@285
|
2641 }
|
adamc@285
|
2642
|
adamc@821
|
2643 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
|
adamc@821
|
2644 char *r = uw_malloc(ctx, 2);
|
adamc@821
|
2645 r[0] = ch;
|
adamc@821
|
2646 r[1] = 0;
|
adamc@821
|
2647 return r;
|
adamc@821
|
2648 }
|
adamc@821
|
2649
|
adamc@311
|
2650 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
2651 if (b == uw_Basis_False)
|
adamc@285
|
2652 return "False";
|
adamc@285
|
2653 else
|
adamc@285
|
2654 return "True";
|
adamc@285
|
2655 }
|
adamc@288
|
2656
|
adamc@436
|
2657 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
2658 size_t len;
|
adamc@436
|
2659 char *r;
|
adam@1392
|
2660 struct tm stm = {};
|
adam@1392
|
2661 stm.tm_isdst = -1;
|
adamc@436
|
2662
|
adam@1365
|
2663 if (localtime_r(&t.seconds, &stm)) {
|
adamc@436
|
2664 uw_check_heap(ctx, TIMES_MAX);
|
adamc@666
|
2665 r = ctx->heap.front;
|
adamc@436
|
2666 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@666
|
2667 ctx->heap.front += len+1;
|
adamc@436
|
2668 return r;
|
adamc@436
|
2669 } else
|
adamc@436
|
2670 return "<Invalid time>";
|
adamc@436
|
2671 }
|
adamc@288
|
2672
|
adam@1359
|
2673 uw_Basis_string uw_Basis_timef(uw_context ctx, const char *fmt, uw_Basis_time t) {
|
adam@1352
|
2674 size_t len;
|
adam@1352
|
2675 char *r;
|
adam@1392
|
2676 struct tm stm = {};
|
adam@1392
|
2677 stm.tm_isdst = -1;
|
adam@1352
|
2678
|
adam@1365
|
2679 if (localtime_r(&t.seconds, &stm)) {
|
adam@1352
|
2680 uw_check_heap(ctx, TIMES_MAX);
|
adam@1352
|
2681 r = ctx->heap.front;
|
adam@1352
|
2682 len = strftime(r, TIMES_MAX, fmt, &stm);
|
adam@1352
|
2683 ctx->heap.front += len+1;
|
adam@1352
|
2684 return r;
|
adam@1352
|
2685 } else
|
adam@1352
|
2686 return "<Invalid time>";
|
adam@1352
|
2687 }
|
adam@1352
|
2688
|
adamc@311
|
2689 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
|
adamc@288
|
2690 char *endptr;
|
adamc@311
|
2691 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@288
|
2692
|
adamc@288
|
2693 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
2694 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adamc@288
|
2695 *r = n;
|
adamc@288
|
2696 return r;
|
adamc@288
|
2697 } else
|
adamc@288
|
2698 return NULL;
|
adamc@288
|
2699 }
|
adamc@289
|
2700
|
adamc@311
|
2701 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
|
adamc@289
|
2702 char *endptr;
|
adamc@311
|
2703 uw_Basis_float n = strtod(s, &endptr);
|
adamc@289
|
2704
|
adamc@289
|
2705 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
2706 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
|
adamc@289
|
2707 *r = n;
|
adamc@289
|
2708 return r;
|
adamc@289
|
2709 } else
|
adamc@289
|
2710 return NULL;
|
adamc@289
|
2711 }
|
adamc@289
|
2712
|
adamc@821
|
2713 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
|
adamc@821
|
2714 if (s[0] == 0) {
|
adamc@821
|
2715 uw_Basis_char *r = uw_malloc(ctx, 1);
|
adamc@821
|
2716 r[0] = 0;
|
adamc@821
|
2717 return r;
|
adamc@821
|
2718 } else if (s[1] != 0)
|
adamc@821
|
2719 return NULL;
|
adamc@821
|
2720 else {
|
adamc@821
|
2721 uw_Basis_char *r = uw_malloc(ctx, 1);
|
adamc@821
|
2722 r[0] = s[0];
|
adamc@821
|
2723 return r;
|
adamc@821
|
2724 }
|
adamc@821
|
2725 }
|
adamc@821
|
2726
|
adamc@311
|
2727 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
2728 static uw_Basis_bool true = uw_Basis_True;
|
adamc@311
|
2729 static uw_Basis_bool false = uw_Basis_False;
|
adamc@289
|
2730
|
adamc@289
|
2731 if (!strcasecmp (s, "True"))
|
adamc@289
|
2732 return &true;
|
adamc@289
|
2733 else if (!strcasecmp (s, "False"))
|
adamc@289
|
2734 return &false;
|
adamc@289
|
2735 else
|
adamc@289
|
2736 return NULL;
|
adamc@289
|
2737 }
|
adamc@292
|
2738
|
adamc@436
|
2739 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
2740 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adam@1374
|
2741 struct tm stm = {};
|
adam@1392
|
2742 stm.tm_isdst = -1;
|
adamc@436
|
2743
|
adamc@439
|
2744 if (dot) {
|
adamc@439
|
2745 *dot = 0;
|
adamc@439
|
2746 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
2747 *dot = '.';
|
adamc@439
|
2748 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1365
|
2749 r->seconds = mktime(&stm);
|
adam@1365
|
2750 r->microseconds = 0;
|
adamc@439
|
2751 return r;
|
adamc@439
|
2752 }
|
adamc@439
|
2753 else {
|
adamc@439
|
2754 *dot = '.';
|
adamc@439
|
2755 return NULL;
|
adamc@439
|
2756 }
|
adamc@436
|
2757 }
|
adamc@439
|
2758 else {
|
adamc@439
|
2759 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
2760 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1365
|
2761 r->seconds = mktime(&stm);
|
adam@1365
|
2762 r->microseconds = 0;
|
adamc@439
|
2763 return r;
|
adamc@439
|
2764 }
|
adamc@439
|
2765 else if (strptime(s, TIME_FMT, &stm) == end) {
|
adamc@439
|
2766 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1365
|
2767 r->seconds = mktime(&stm);
|
adam@1365
|
2768 r->microseconds = 0;
|
adamc@439
|
2769 return r;
|
adamc@439
|
2770 }
|
adamc@439
|
2771 else
|
adamc@439
|
2772 return NULL;
|
adamc@439
|
2773 }
|
adamc@436
|
2774 }
|
adamc@436
|
2775
|
adam@1352
|
2776 uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) {
|
adam@1352
|
2777 char *end = strchr(s, 0);
|
adam@1374
|
2778 struct tm stm = {};
|
adam@1392
|
2779 stm.tm_isdst = -1;
|
adam@1352
|
2780
|
adam@1352
|
2781 if (strptime(s, fmt, &stm) == end) {
|
adam@1352
|
2782 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1365
|
2783 r->seconds = mktime(&stm);
|
adam@1365
|
2784 r->microseconds = 0;
|
adam@1352
|
2785 return r;
|
adam@1352
|
2786 }
|
adam@1352
|
2787 else
|
adam@1352
|
2788 return NULL;
|
adam@1352
|
2789 }
|
adam@1352
|
2790
|
adamc@311
|
2791 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
|
adamc@292
|
2792 char *endptr;
|
adamc@311
|
2793 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@292
|
2794
|
adamc@292
|
2795 if (*s != '\0' && *endptr == '\0')
|
adamc@292
|
2796 return n;
|
adamc@292
|
2797 else
|
adamc@311
|
2798 uw_error(ctx, FATAL, "Can't parse int: %s", s);
|
adamc@292
|
2799 }
|
adamc@293
|
2800
|
adamc@682
|
2801 #include <errno.h>
|
adamc@682
|
2802
|
adamc@678
|
2803 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
|
adamc@682
|
2804 unsigned long long n;
|
adamc@682
|
2805
|
adamc@682
|
2806 if (sscanf(s, "%llu", &n) < 1)
|
adamc@682
|
2807 uw_error(ctx, FATAL, "Can't parse channel: %s", s);
|
adamc@682
|
2808 else {
|
adamc@682
|
2809 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
|
adamc@682
|
2810 return ch;
|
adamc@682
|
2811 }
|
adamc@682
|
2812 }
|
adamc@682
|
2813
|
adamc@682
|
2814 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
|
adamc@678
|
2815 char *endptr;
|
adamc@682
|
2816 unsigned long n = strtoul(s, &endptr, 10);
|
adamc@678
|
2817
|
adamc@678
|
2818 if (*s != '\0' && *endptr == '\0')
|
adamc@678
|
2819 return n;
|
adamc@678
|
2820 else
|
adamc@682
|
2821 uw_error(ctx, FATAL, "Can't parse client: %s", s);
|
adamc@678
|
2822 }
|
adamc@678
|
2823
|
adamc@311
|
2824 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
|
adamc@293
|
2825 char *endptr;
|
adamc@311
|
2826 uw_Basis_float n = strtod(s, &endptr);
|
adamc@293
|
2827
|
adamc@293
|
2828 if (*s != '\0' && *endptr == '\0')
|
adamc@293
|
2829 return n;
|
adamc@293
|
2830 else
|
adamc@311
|
2831 uw_error(ctx, FATAL, "Can't parse float: %s", s);
|
adamc@293
|
2832 }
|
adamc@293
|
2833
|
adamc@821
|
2834 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
|
adamc@821
|
2835 if (s[0] == 0)
|
adamc@821
|
2836 return 0;
|
adamc@821
|
2837 else if (s[1] != 0)
|
adamc@821
|
2838 uw_error(ctx, FATAL, "Can't parse char: %s", s);
|
adamc@821
|
2839 else
|
adamc@821
|
2840 return s[0];
|
adamc@821
|
2841 }
|
adamc@821
|
2842
|
adamc@311
|
2843 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
|
adamc@296
|
2844 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
|
adamc@311
|
2845 return uw_Basis_True;
|
adamc@296
|
2846 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
|
adamc@311
|
2847 return uw_Basis_False;
|
adamc@293
|
2848 else
|
adamc@311
|
2849 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
|
adamc@293
|
2850 }
|
adamc@436
|
2851
|
adam@1352
|
2852 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
|
adam@1352
|
2853 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adam@1352
|
2854 struct tm stm = {};
|
adam@1392
|
2855 stm.tm_isdst = -1;
|
adam@1352
|
2856
|
adam@1352
|
2857 if (dot) {
|
adam@1352
|
2858 *dot = 0;
|
adam@1352
|
2859 if (strptime(s, TIME_FMT_PG, &stm)) {
|
adam@1352
|
2860 *dot = '.';
|
adam@1365
|
2861 uw_Basis_time r = { mktime(&stm), atoi(dot+1) };
|
adam@1365
|
2862 return r;
|
adam@1352
|
2863 }
|
adam@1352
|
2864 else {
|
adam@1352
|
2865 *dot = '.';
|
adam@1352
|
2866 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adam@1352
|
2867 }
|
adam@1352
|
2868 }
|
adam@1352
|
2869 else {
|
adam@1352
|
2870 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adam@1365
|
2871 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
2872 return r;
|
adam@1352
|
2873 } else if (strptime(s, TIME_FMT, &stm) == end) {
|
adam@1365
|
2874 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
2875 return r;
|
adam@1352
|
2876 } else
|
adam@1352
|
2877 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adam@1352
|
2878 }
|
adam@1352
|
2879 }
|
adam@1352
|
2880
|
adamc@436
|
2881 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
2882 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
2883 struct tm stm = {};
|
adam@1392
|
2884 stm.tm_isdst = -1;
|
adamc@436
|
2885
|
adamc@438
|
2886 if (dot) {
|
adamc@438
|
2887 *dot = 0;
|
adamc@438
|
2888 if (strptime(s, TIME_FMT_PG, &stm)) {
|
adamc@438
|
2889 *dot = '.';
|
adam@1365
|
2890 {
|
adam@1365
|
2891 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
2892 return r;
|
adam@1365
|
2893 }
|
adamc@438
|
2894 }
|
adamc@438
|
2895 else {
|
adamc@438
|
2896 *dot = '.';
|
adamc@438
|
2897 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
2898 }
|
adamc@438
|
2899 }
|
adamc@438
|
2900 else {
|
adam@1365
|
2901 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adam@1365
|
2902 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
2903 return r;
|
adam@1365
|
2904 } else if (strptime(s, TIME_FMT, &stm) == end) {
|
adam@1365
|
2905 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
2906 return r;
|
adam@1365
|
2907 } else
|
adamc@438
|
2908 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
2909 }
|
adamc@436
|
2910 }
|
adamc@457
|
2911
|
adam@1352
|
2912 uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) {
|
adam@1352
|
2913 char *end = strchr(s, 0);
|
adamc@939
|
2914 struct tm stm = {};
|
adam@1392
|
2915 stm.tm_isdst = -1;
|
adamc@939
|
2916
|
adam@1365
|
2917 if (strptime(s, fmt, &stm) == end) {
|
adam@1365
|
2918 uw_Basis_time r = { mktime(&stm) };
|
adam@1365
|
2919 return r;
|
adam@1365
|
2920 } else
|
adam@1352
|
2921 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@939
|
2922 }
|
adamc@939
|
2923
|
adamc@742
|
2924 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
|
adamc@742
|
2925 char *r = ctx->heap.front;
|
adamc@742
|
2926 uw_Basis_blob b = {len, r};
|
adamc@742
|
2927
|
adamc@742
|
2928 uw_check_heap(ctx, len);
|
adamc@742
|
2929
|
adamc@742
|
2930 while (*s) {
|
adamc@742
|
2931 if (s[0] == '\\') {
|
adamc@742
|
2932 if (s[1] == '\\') {
|
adamc@742
|
2933 *r++ = '\\';
|
adamc@742
|
2934 s += 2;
|
adamc@1153
|
2935 } else if (isdigit((int)s[1]) && isdigit((int)s[2]) && isdigit((int)s[3])) {
|
adamc@742
|
2936 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
|
adamc@742
|
2937 s += 4;
|
adamc@742
|
2938 }
|
adamc@742
|
2939 else {
|
adamc@742
|
2940 *r++ = '\\';
|
adamc@742
|
2941 ++s;
|
adamc@742
|
2942 }
|
adamc@742
|
2943 } else {
|
adamc@742
|
2944 *r++ = s[0];
|
adamc@742
|
2945 ++s;
|
adamc@742
|
2946 }
|
adamc@742
|
2947 }
|
adamc@742
|
2948
|
adamc@742
|
2949 b.size = r - ctx->heap.front;
|
adamc@742
|
2950 ctx->heap.front = r;
|
adamc@742
|
2951 return b;
|
adamc@742
|
2952 }
|
adamc@742
|
2953
|
adam@1402
|
2954 #define THE_PAST "expires=Sat, 01-Jan-2011 00:00:00 GMT"
|
adamc@1052
|
2955
|
adamc@463
|
2956 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
|
adamc@463
|
2957 int len = strlen(c);
|
adamc@854
|
2958 char *p = ctx->outHeaders.start;
|
adamc@474
|
2959
|
adamc@1134
|
2960 while ((p = strstr(p, "\nSet-Cookie: "))) {
|
adamc@474
|
2961 char *p2;
|
adamc@474
|
2962 p += 13;
|
adamc@474
|
2963 p2 = strchr(p, '=');
|
adamc@474
|
2964
|
adamc@474
|
2965 if (p2) {
|
adamc@474
|
2966 size_t sz = strcspn(p2+1, ";\r\n");
|
adamc@474
|
2967
|
adamc@474
|
2968 if (!strncasecmp(p, c, p2 - p)) {
|
adamc@1052
|
2969 if (sz == 0 && strstr(p2+2, THE_PAST))
|
adamc@1052
|
2970 return NULL;
|
adamc@1052
|
2971 else {
|
adamc@1052
|
2972 char *ret = uw_malloc(ctx, sz + 1);
|
adamc@1052
|
2973 memcpy(ret, p2+1, sz);
|
adamc@1052
|
2974 ret[sz] = 0;
|
adamc@1052
|
2975 return ret;
|
adamc@1052
|
2976 }
|
adamc@474
|
2977 }
|
adamc@474
|
2978 }
|
adamc@474
|
2979 }
|
adamc@463
|
2980
|
adamc@1134
|
2981 if ((p = uw_Basis_requestHeader(ctx, "Cookie"))) {
|
adamc@1051
|
2982 char *p2;
|
adamc@1051
|
2983
|
adamc@854
|
2984 while (1) {
|
adamc@1051
|
2985 if (!strncmp(p, c, len) && p[len] == '=') {
|
adamc@1134
|
2986 if ((p2 = strchr(p, ';'))) {
|
adamc@1051
|
2987 size_t n = p2 - (p + len);
|
adamc@1051
|
2988 char *r = uw_malloc(ctx, n);
|
adamc@1051
|
2989 memcpy(r, p + 1 + len, n-1);
|
adamc@1051
|
2990 r[n-1] = 0;
|
adamc@1051
|
2991 return r;
|
adamc@1051
|
2992 } else
|
adamc@1051
|
2993 return p + 1 + len;
|
adamc@1134
|
2994 } else if ((p = strchr(p, ';')))
|
adamc@854
|
2995 p += 2;
|
adamc@463
|
2996 else
|
adamc@463
|
2997 return NULL;
|
adamc@463
|
2998 }
|
adamc@463
|
2999 }
|
adamc@792
|
3000
|
adamc@792
|
3001 return NULL;
|
adamc@457
|
3002 }
|
adamc@462
|
3003
|
adamc@1050
|
3004 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v, uw_Basis_time *expires, uw_Basis_bool secure) {
|
adamc@462
|
3005 uw_write_header(ctx, "Set-Cookie: ");
|
adamc@462
|
3006 uw_write_header(ctx, c);
|
adamc@462
|
3007 uw_write_header(ctx, "=");
|
adamc@462
|
3008 uw_write_header(ctx, v);
|
adamc@466
|
3009 uw_write_header(ctx, "; path=");
|
adamc@466
|
3010 uw_write_header(ctx, prefix);
|
adamc@1050
|
3011 if (expires) {
|
adamc@1050
|
3012 char formatted[30];
|
adam@1392
|
3013 struct tm tm = {};
|
adam@1392
|
3014 tm.tm_isdst = -1;
|
adamc@1050
|
3015
|
adam@1365
|
3016 gmtime_r(&expires->seconds, &tm);
|
adamc@1050
|
3017
|
adamc@1050
|
3018 strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm);
|
adamc@1050
|
3019
|
adamc@1050
|
3020 uw_write_header(ctx, "; expires=");
|
adamc@1050
|
3021 uw_write_header(ctx, formatted);
|
adamc@1050
|
3022 }
|
adamc@1050
|
3023 if (secure)
|
adamc@1050
|
3024 uw_write_header(ctx, "; secure");
|
adamc@462
|
3025 uw_write_header(ctx, "\r\n");
|
adamc@462
|
3026
|
adamc@462
|
3027 return uw_unit_v;
|
adamc@462
|
3028 }
|
adamc@667
|
3029
|
adamc@1050
|
3030 uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c) {
|
adamc@1050
|
3031 uw_write_header(ctx, "Set-Cookie: ");
|
adamc@1050
|
3032 uw_write_header(ctx, c);
|
adamc@1050
|
3033 uw_write_header(ctx, "=; path=");
|
adamc@1050
|
3034 uw_write_header(ctx, prefix);
|
adamc@1052
|
3035 uw_write_header(ctx, "; " THE_PAST "\r\n");
|
adamc@1050
|
3036
|
adamc@1050
|
3037 return uw_unit_v;
|
adamc@1050
|
3038 }
|
adamc@1050
|
3039
|
adamc@1113
|
3040 size_t uw_deltas_max = SIZE_MAX;
|
adamc@1113
|
3041
|
adamc@682
|
3042 static delta *allocate_delta(uw_context ctx, unsigned client) {
|
adamc@682
|
3043 unsigned i;
|
adamc@682
|
3044 delta *d;
|
adamc@671
|
3045
|
adamc@682
|
3046 for (i = 0; i < ctx->used_deltas; ++i)
|
adamc@682
|
3047 if (ctx->deltas[i].client == client)
|
adamc@682
|
3048 return &ctx->deltas[i];
|
adamc@671
|
3049
|
adamc@682
|
3050 if (ctx->used_deltas >= ctx->n_deltas) {
|
adamc@1113
|
3051 if (ctx->n_deltas + 1 > uw_deltas_max)
|
adamc@1113
|
3052 uw_error(ctx, FATAL, "Exceeded limit on number of deltas");
|
adamc@1113
|
3053
|
adamc@682
|
3054 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
|
adam@1369
|
3055 uw_buffer_init(uw_messages_max, &ctx->deltas[ctx->n_deltas-1].msgs, 0);
|
adamc@682
|
3056 }
|
adamc@671
|
3057
|
adamc@682
|
3058 d = &ctx->deltas[ctx->used_deltas++];
|
adamc@682
|
3059 d->client = client;
|
adam@1369
|
3060 uw_buffer_reset(&d->msgs);
|
adamc@682
|
3061 return d;
|
adamc@671
|
3062 }
|
adamc@671
|
3063
|
adamc@668
|
3064 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
|
adamc@682
|
3065 if (ctx->client == NULL)
|
adamc@682
|
3066 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
|
adamc@671
|
3067
|
adamc@682
|
3068 return new_channel(ctx->client);
|
adamc@668
|
3069 }
|
adamc@668
|
3070
|
adamc@668
|
3071 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
|
adamc@682
|
3072 delta *d = allocate_delta(ctx, chn.cli);
|
adamc@682
|
3073 size_t len;
|
adamc@682
|
3074 int preLen;
|
adamc@682
|
3075 char pre[INTS_MAX + 2];
|
adamc@668
|
3076
|
adamc@682
|
3077 len = strlen(msg);
|
adamc@682
|
3078
|
adamc@682
|
3079 sprintf(pre, "%u\n%n", chn.chn, &preLen);
|
adamc@682
|
3080
|
adam@1369
|
3081 ctx_uw_buffer_append(ctx, "messages", &d->msgs, pre, preLen);
|
adam@1369
|
3082 ctx_uw_buffer_append(ctx, "messages", &d->msgs, msg, len);
|
adam@1369
|
3083 ctx_uw_buffer_append(ctx, "messages", &d->msgs, "\n", 1);
|
adamc@671
|
3084
|
adamc@671
|
3085 return uw_unit_v;
|
adamc@668
|
3086 }
|
adamc@671
|
3087
|
adam@1327
|
3088 int uw_rollback(uw_context ctx, int will_retry) {
|
adamc@1131
|
3089 int i;
|
adamc@1131
|
3090 cleanup *cl;
|
adamc@1131
|
3091
|
adamc@1131
|
3092 if (ctx->client)
|
adamc@1131
|
3093 release_client(ctx->client);
|
adamc@1131
|
3094
|
adamc@1131
|
3095 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@1131
|
3096 cl->func(cl->arg);
|
adamc@1131
|
3097
|
adamc@1131
|
3098 ctx->cleanup_front = ctx->cleanup;
|
adamc@1131
|
3099
|
adamc@1131
|
3100 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adamc@1131
|
3101 if (ctx->transactionals[i].rollback != NULL)
|
adamc@1131
|
3102 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
|
adamc@1131
|
3103
|
adamc@1131
|
3104 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adamc@1131
|
3105 if (ctx->transactionals[i].free)
|
adam@1327
|
3106 ctx->transactionals[i].free(ctx->transactionals[i].data, will_retry);
|
adamc@1131
|
3107
|
adamc@1144
|
3108 return ctx->app ? ctx->app->db_rollback(ctx) : 0;
|
adamc@1131
|
3109 }
|
adamc@1131
|
3110
|
adamc@671
|
3111 void uw_commit(uw_context ctx) {
|
adamc@1104
|
3112 int i;
|
adamc@1104
|
3113
|
adamc@1131
|
3114 if (uw_has_error(ctx)) {
|
adam@1327
|
3115 uw_rollback(ctx, 0);
|
adamc@1131
|
3116 return;
|
adamc@1131
|
3117 }
|
adamc@1131
|
3118
|
adamc@1104
|
3119 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adamc@771
|
3120 if (ctx->transactionals[i].rollback != NULL)
|
adamc@1131
|
3121 if (ctx->transactionals[i].commit) {
|
adamc@1083
|
3122 ctx->transactionals[i].commit(ctx->transactionals[i].data);
|
adamc@1131
|
3123 if (uw_has_error(ctx)) {
|
adam@1327
|
3124 uw_rollback(ctx, 0);
|
adamc@1131
|
3125 return;
|
adamc@1131
|
3126 }
|
adamc@1131
|
3127 }
|
adamc@771
|
3128
|
adamc@1104
|
3129 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adamc@771
|
3130 if (ctx->transactionals[i].rollback == NULL)
|
adamc@1131
|
3131 if (ctx->transactionals[i].commit) {
|
adamc@1083
|
3132 ctx->transactionals[i].commit(ctx->transactionals[i].data);
|
adamc@1131
|
3133 if (uw_has_error(ctx)) {
|
adam@1327
|
3134 uw_rollback(ctx, 0);
|
adamc@1131
|
3135 return;
|
adamc@1131
|
3136 }
|
adamc@1131
|
3137 }
|
adamc@1131
|
3138
|
adamc@1131
|
3139 if (ctx->app->db_commit(ctx)) {
|
adamc@1131
|
3140 uw_set_error_message(ctx, "Error running SQL COMMIT");
|
adamc@1131
|
3141 return;
|
adamc@1131
|
3142 }
|
adamc@682
|
3143
|
adamc@682
|
3144 for (i = 0; i < ctx->used_deltas; ++i) {
|
adamc@682
|
3145 delta *d = &ctx->deltas[i];
|
adamc@682
|
3146 client *c = find_client(d->client);
|
adamc@682
|
3147
|
adamc@682
|
3148 assert (c != NULL && c->mode == USED);
|
adamc@682
|
3149
|
adamc@685
|
3150 client_send(c, &d->msgs);
|
adamc@682
|
3151 }
|
adamc@682
|
3152
|
adamc@682
|
3153 if (ctx->client)
|
adamc@685
|
3154 release_client(ctx->client);
|
adamc@771
|
3155
|
adamc@1104
|
3156 for (i = ctx->used_transactionals-1; i >= 0; --i)
|
adamc@1083
|
3157 if (ctx->transactionals[i].free)
|
adam@1327
|
3158 ctx->transactionals[i].free(ctx->transactionals[i].data, 0);
|
adamc@916
|
3159
|
adamc@916
|
3160 // Splice script data into appropriate part of page
|
adamc@1065
|
3161 if (ctx->returning_indirectly || ctx->script_header[0] == 0) {
|
adamc@995
|
3162 char *start = strstr(ctx->page.start, "<sc>");
|
adamc@995
|
3163 if (start) {
|
adam@1369
|
3164 memmove(start, start + 4, uw_buffer_used(&ctx->page) - (start - ctx->page.start) - 4);
|
adamc@995
|
3165 ctx->page.front -= 4;
|
adamc@995
|
3166 }
|
adam@1369
|
3167 } else if (uw_buffer_used(&ctx->script) == 0) {
|
adamc@916
|
3168 size_t len = strlen(ctx->script_header);
|
adamc@916
|
3169 char *start = strstr(ctx->page.start, "<sc>");
|
adamc@916
|
3170 if (start) {
|
adam@1369
|
3171 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) - 4 + len);
|
adamc@917
|
3172 start = strstr(ctx->page.start, "<sc>");
|
adam@1369
|
3173 memmove(start + len, start + 4, uw_buffer_used(&ctx->page) - (start - ctx->page.start) - 3);
|
adamc@916
|
3174 ctx->page.front += len - 4;
|
adamc@916
|
3175 memcpy(start, ctx->script_header, len);
|
adamc@916
|
3176 }
|
adamc@916
|
3177 } else {
|
adam@1369
|
3178 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
|
adamc@916
|
3179 size_t lenP = lenH + 40 + len;
|
adamc@916
|
3180 char *start = strstr(ctx->page.start, "<sc>");
|
adamc@916
|
3181 if (start) {
|
adam@1369
|
3182 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) - 4 + lenP);
|
adamc@917
|
3183 start = strstr(ctx->page.start, "<sc>");
|
adam@1369
|
3184 memmove(start + lenP, start + 4, uw_buffer_used(&ctx->page) - (start - ctx->page.start) - 3);
|
adamc@916
|
3185 ctx->page.front += lenP - 4;
|
adamc@916
|
3186 memcpy(start, ctx->script_header, lenH);
|
adamc@916
|
3187 memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
|
adamc@916
|
3188 memcpy(start + lenH + 31, ctx->script.start, len);
|
adamc@916
|
3189 memcpy(start + lenH + 31 + len, "</script>", 9);
|
adamc@916
|
3190 }
|
adamc@916
|
3191 }
|
adamc@671
|
3192 }
|
adamc@671
|
3193
|
adamc@683
|
3194
|
adamc@1113
|
3195 size_t uw_transactionals_max = SIZE_MAX;
|
adamc@1113
|
3196
|
adamc@771
|
3197 void uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback,
|
adam@1327
|
3198 uw_callback_with_retry free) {
|
adamc@771
|
3199 if (ctx->used_transactionals >= ctx->n_transactionals) {
|
adamc@1113
|
3200 if (ctx->used_transactionals+1 > uw_transactionals_max)
|
adamc@1113
|
3201 uw_error(ctx, FATAL, "Exceeded limit on number of transactionals");
|
adamc@1084
|
3202 ctx->transactionals = realloc(ctx->transactionals, sizeof(transactional) * (ctx->used_transactionals+1));
|
adamc@771
|
3203 ++ctx->n_transactionals;
|
adamc@771
|
3204 }
|
adamc@771
|
3205
|
adamc@771
|
3206 ctx->transactionals[ctx->used_transactionals].data = data;
|
adamc@771
|
3207 ctx->transactionals[ctx->used_transactionals].commit = commit;
|
adamc@771
|
3208 ctx->transactionals[ctx->used_transactionals].rollback = rollback;
|
adamc@771
|
3209 ctx->transactionals[ctx->used_transactionals++].free = free;
|
adamc@771
|
3210 }
|
adamc@771
|
3211
|
adamc@683
|
3212
|
adamc@683
|
3213 // "Garbage collection"
|
adamc@683
|
3214
|
adamc@1121
|
3215 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data);
|
adamc@1121
|
3216 void uw_post_expunge(uw_context ctx, void *data);
|
adamc@1121
|
3217
|
adamc@1121
|
3218 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
|
adamc@683
|
3219 int r = setjmp(ctx->jmp_buf);
|
adamc@683
|
3220
|
adamc@1121
|
3221 if (r == 0)
|
adamc@1121
|
3222 uw_do_expunge(ctx, cli, data);
|
adamc@1121
|
3223 else
|
adamc@1121
|
3224 ctx->app->db_rollback(ctx);
|
adamc@1121
|
3225
|
adamc@1121
|
3226 uw_post_expunge(ctx, data);
|
adamc@683
|
3227
|
adamc@683
|
3228 return r;
|
adamc@683
|
3229 }
|
adamc@683
|
3230
|
adamc@683
|
3231 void uw_prune_clients(uw_context ctx) {
|
adamc@683
|
3232 client *c, *next, *prev = NULL;
|
adamc@683
|
3233 time_t cutoff;
|
adamc@683
|
3234
|
adamc@1094
|
3235 cutoff = time(NULL) - ctx->app->timeout;
|
adamc@683
|
3236
|
adamc@683
|
3237 pthread_mutex_lock(&clients_mutex);
|
adamc@683
|
3238
|
adamc@683
|
3239 for (c = clients_used; c; c = next) {
|
adamc@683
|
3240 next = c->next;
|
adamc@683
|
3241 pthread_mutex_lock(&c->lock);
|
adamc@685
|
3242 if (c->last_contact < cutoff && c->refcount == 0) {
|
adamc@683
|
3243 failure_kind fk = UNLIMITED_RETRY;
|
adamc@683
|
3244 if (prev)
|
adamc@683
|
3245 prev->next = next;
|
adamc@683
|
3246 else
|
adamc@683
|
3247 clients_used = next;
|
adamc@684
|
3248 uw_reset(ctx);
|
adamc@683
|
3249 while (fk == UNLIMITED_RETRY) {
|
adamc@1121
|
3250 fk = uw_expunge(ctx, c->id, c->data);
|
adamc@1121
|
3251 if (fk == UNLIMITED_RETRY)
|
adamc@684
|
3252 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
|
adamc@683
|
3253 }
|
adamc@684
|
3254 if (fk == SUCCESS)
|
adamc@684
|
3255 free_client(c);
|
adamc@1121
|
3256 else
|
adamc@856
|
3257 fprintf(stderr, "Expunge blocked by error: %s\n", uw_error_message(ctx));
|
adamc@683
|
3258 }
|
adamc@683
|
3259 else
|
adamc@683
|
3260 prev = c;
|
adamc@683
|
3261 pthread_mutex_unlock(&c->lock);
|
adamc@683
|
3262 }
|
adamc@683
|
3263
|
adamc@683
|
3264 pthread_mutex_unlock(&clients_mutex);
|
adamc@683
|
3265 }
|
adamc@687
|
3266
|
adamc@687
|
3267 failure_kind uw_initialize(uw_context ctx) {
|
adamc@687
|
3268 int r = setjmp(ctx->jmp_buf);
|
adamc@687
|
3269
|
adamc@687
|
3270 if (r == 0) {
|
adamc@1094
|
3271 if (ctx->app->db_begin(ctx))
|
adamc@687
|
3272 uw_error(ctx, FATAL, "Error running SQL BEGIN");
|
adamc@1094
|
3273 ctx->app->initializer(ctx);
|
adamc@1094
|
3274 if (ctx->app->db_commit(ctx))
|
adamc@687
|
3275 uw_error(ctx, FATAL, "Error running SQL COMMIT");
|
adamc@687
|
3276 }
|
adamc@687
|
3277
|
adamc@687
|
3278 return r;
|
adamc@687
|
3279 }
|
adamc@717
|
3280
|
adamc@1065
|
3281 static int url_bad(uw_Basis_string s) {
|
adamc@1065
|
3282 for (; *s; ++s)
|
adamc@1153
|
3283 if (!isgraph((int)*s))
|
adamc@1065
|
3284 return 1;
|
adamc@1065
|
3285
|
adamc@1065
|
3286 return 0;
|
adamc@1065
|
3287 }
|
adamc@1065
|
3288
|
adamc@717
|
3289 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
|
adamc@1065
|
3290 if (url_bad(s))
|
adamc@1065
|
3291 uw_error(ctx, FATAL, "Invalid URL %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@1094
|
3292 if (ctx->app->check_url(s))
|
adamc@770
|
3293 return s;
|
adamc@770
|
3294 else
|
adamc@770
|
3295 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@717
|
3296 }
|
adamc@734
|
3297
|
adamc@770
|
3298 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
|
adamc@1065
|
3299 if (url_bad(s))
|
adamc@1065
|
3300 return NULL;
|
adamc@1094
|
3301 if (ctx->app->check_url(s))
|
adamc@770
|
3302 return s;
|
adamc@770
|
3303 else
|
adamc@770
|
3304 return NULL;
|
adamc@770
|
3305 }
|
adamc@770
|
3306
|
adamc@770
|
3307 int mime_format(const char *s) {
|
adamc@770
|
3308 for (; *s; ++s)
|
adamc@1153
|
3309 if (!isalnum((int)*s) && *s != '/' && *s != '-' && *s != '.')
|
adamc@770
|
3310 return 0;
|
adamc@770
|
3311
|
adamc@770
|
3312 return 1;
|
adamc@770
|
3313 }
|
adamc@770
|
3314
|
adamc@741
|
3315 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
|
adamc@770
|
3316 if (!mime_format(s))
|
adamc@770
|
3317 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
|
adamc@770
|
3318
|
adamc@1094
|
3319 if (ctx->app->check_mime(s))
|
adamc@770
|
3320 return s;
|
adamc@770
|
3321 else
|
adamc@770
|
3322 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
|
adamc@770
|
3323 }
|
adamc@770
|
3324
|
adamc@770
|
3325 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
|
adamc@770
|
3326 if (!mime_format(s))
|
adamc@770
|
3327 return NULL;
|
adamc@770
|
3328
|
adamc@1094
|
3329 if (ctx->app->check_mime(s))
|
adamc@770
|
3330 return s;
|
adamc@770
|
3331 else
|
adamc@770
|
3332 return NULL;
|
adamc@741
|
3333 }
|
adamc@741
|
3334
|
adamc@734
|
3335 uw_Basis_string uw_unnull(uw_Basis_string s) {
|
adamc@734
|
3336 return s ? s : "";
|
adamc@734
|
3337 }
|
adamc@734
|
3338
|
adamc@734
|
3339 extern int uw_hash_blocksize;
|
adamc@734
|
3340
|
adamc@734
|
3341 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
|
adamc@734
|
3342 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
|
adamc@734
|
3343 int i;
|
adamc@734
|
3344
|
adamc@734
|
3345 for (i = 0; i < uw_hash_blocksize; ++i)
|
adamc@734
|
3346 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
|
adamc@734
|
3347
|
adamc@734
|
3348 return r;
|
adamc@734
|
3349 }
|
adamc@734
|
3350
|
adamc@734
|
3351 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
|
adamc@1094
|
3352 return ctx->app->cookie_sig(ctx);
|
adamc@734
|
3353 }
|
adamc@737
|
3354
|
adamc@737
|
3355 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
|
adamc@737
|
3356 return f.name;
|
adamc@737
|
3357 }
|
adamc@737
|
3358
|
adamc@740
|
3359 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
|
adamc@740
|
3360 return f.type;
|
adamc@740
|
3361 }
|
adamc@740
|
3362
|
adamc@745
|
3363 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
|
adamc@745
|
3364 return b.size;
|
adamc@745
|
3365 }
|
adamc@745
|
3366
|
adamc@1119
|
3367 uw_Basis_blob uw_Basis_textBlob(uw_context ctx, uw_Basis_string s) {
|
adamc@1119
|
3368 uw_Basis_blob b = {strlen(s), s};
|
adamc@1119
|
3369
|
adamc@1119
|
3370 return b;
|
adamc@1119
|
3371 }
|
adamc@1119
|
3372
|
adamc@737
|
3373 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
|
adamc@737
|
3374 return f.data;
|
adamc@737
|
3375 }
|
adamc@741
|
3376
|
adam@1347
|
3377 uw_Basis_string uw_Basis_postType(uw_context ctx, uw_Basis_postBody pb) {
|
adam@1347
|
3378 return pb.type;
|
adam@1347
|
3379 }
|
adam@1347
|
3380
|
adam@1347
|
3381 uw_Basis_string uw_Basis_postData(uw_context ctx, uw_Basis_postBody pb) {
|
adam@1347
|
3382 return pb.data;
|
adam@1347
|
3383 }
|
adam@1347
|
3384
|
adamc@741
|
3385 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
|
adamc@741
|
3386 cleanup *cl;
|
adamc@741
|
3387 int len;
|
adamc@741
|
3388
|
adamc@1065
|
3389 ctx->returning_indirectly = 1;
|
adam@1369
|
3390 uw_buffer_reset(&ctx->outHeaders);
|
adam@1369
|
3391 uw_buffer_reset(&ctx->page);
|
adamc@741
|
3392
|
adamc@856
|
3393 uw_write_header(ctx, on_success);
|
adamc@856
|
3394 uw_write_header(ctx, "Content-Type: ");
|
adamc@741
|
3395 uw_write_header(ctx, mimeType);
|
adamc@741
|
3396 uw_write_header(ctx, "\r\nContent-Length: ");
|
adam@1369
|
3397 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
|
adamc@1134
|
3398 sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)b.size, &len);
|
adamc@741
|
3399 ctx->outHeaders.front += len;
|
adamc@1066
|
3400 uw_write_header(ctx, "\r\n");
|
adamc@741
|
3401
|
adam@1369
|
3402 ctx_uw_buffer_append(ctx, "page", &ctx->page, b.data, b.size);
|
adamc@741
|
3403
|
adamc@741
|
3404 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@741
|
3405 cl->func(cl->arg);
|
adamc@741
|
3406
|
adamc@741
|
3407 ctx->cleanup_front = ctx->cleanup;
|
adamc@741
|
3408
|
adamc@1065
|
3409 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
|
adamc@1065
|
3410 }
|
adamc@1065
|
3411
|
adamc@1065
|
3412 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) {
|
adamc@1065
|
3413 cleanup *cl;
|
adamc@1066
|
3414 char *s;
|
adamc@1065
|
3415
|
adamc@1065
|
3416 ctx->returning_indirectly = 1;
|
adam@1369
|
3417 uw_buffer_reset(&ctx->page);
|
adam@1369
|
3418 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->outHeaders)+1);
|
adam@1369
|
3419 memcpy(ctx->page.start, ctx->outHeaders.start, uw_buffer_used(&ctx->outHeaders));
|
adam@1369
|
3420 ctx->page.start[uw_buffer_used(&ctx->outHeaders)] = 0;
|
adam@1369
|
3421 uw_buffer_reset(&ctx->outHeaders);
|
adamc@1065
|
3422
|
adamc@1065
|
3423 uw_write_header(ctx, on_redirect);
|
adamc@1066
|
3424
|
adamc@1066
|
3425 s = strchr(ctx->page.start, '\n');
|
adamc@1066
|
3426 if (s) {
|
adamc@1066
|
3427 char *s2;
|
adamc@1134
|
3428 for (++s; (s2 = strchr(s, '\n')); s = s2+1) {
|
adamc@1066
|
3429 *s2 = 0;
|
adamc@1066
|
3430 if (!strncmp(s, "Set-Cookie: ", 12)) {
|
adamc@1066
|
3431 uw_write_header(ctx, s);
|
adamc@1066
|
3432 uw_write_header(ctx, "\n");
|
adamc@1066
|
3433 }
|
adamc@1066
|
3434 }
|
adamc@1066
|
3435 }
|
adamc@1066
|
3436
|
adamc@1065
|
3437 uw_write_header(ctx, "Location: ");
|
adamc@1065
|
3438 uw_write_header(ctx, url);
|
adamc@1065
|
3439 uw_write_header(ctx, "\r\n\r\n");
|
adamc@1065
|
3440
|
adamc@1065
|
3441 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@1065
|
3442 cl->func(cl->arg);
|
adamc@1065
|
3443
|
adamc@1065
|
3444 ctx->cleanup_front = ctx->cleanup;
|
adamc@1065
|
3445
|
adamc@1065
|
3446 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
|
adamc@741
|
3447 }
|
adamc@874
|
3448
|
adamc@874
|
3449 uw_Basis_string uw_Basis_unAs(uw_context ctx, uw_Basis_string s) {
|
adamc@874
|
3450 uw_Basis_string r = uw_malloc(ctx, strlen(s) + 1);
|
adamc@874
|
3451
|
adamc@874
|
3452 for (; *s; ++s) {
|
adamc@874
|
3453 if (s[0] == '\'') {
|
adamc@874
|
3454 *r++ = '\'';
|
adamc@874
|
3455 for (++s; *s; ++s) {
|
adamc@874
|
3456 if (s[0] == '\'') {
|
adamc@874
|
3457 *r++ = '\'';
|
adamc@874
|
3458 break;
|
adamc@874
|
3459 } else if (s[0] == '\\') {
|
adamc@874
|
3460 if (s[1] == '\\') {
|
adamc@874
|
3461 *r++ = '\\';
|
adamc@874
|
3462 *r++ = '\\';
|
adamc@874
|
3463 ++s;
|
adamc@874
|
3464 } else if (s[1] == '\'') {
|
adamc@874
|
3465 *r++ = '\\';
|
adamc@874
|
3466 *r++ = '\'';
|
adamc@874
|
3467 ++s;
|
adamc@874
|
3468 } else
|
adamc@874
|
3469 *r++ = '\'';
|
adamc@874
|
3470 } else
|
adamc@874
|
3471 *r++ = s[0];
|
adamc@874
|
3472 }
|
adamc@874
|
3473 if (*s == 0) break;
|
adamc@874
|
3474 } else if (s[0] == 'T' && s[1] == '.')
|
adamc@874
|
3475 ++s;
|
adamc@874
|
3476 else
|
adamc@874
|
3477 *r++ = s[0];
|
adamc@874
|
3478 }
|
adamc@874
|
3479
|
adamc@874
|
3480 return r;
|
adamc@874
|
3481 }
|
adamc@874
|
3482
|
adamc@922
|
3483 uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) {
|
adamc@922
|
3484 va_list ap;
|
adamc@922
|
3485 size_t len = 1;
|
adamc@922
|
3486 char *s, *r, *s2;
|
adamc@922
|
3487
|
adamc@922
|
3488 va_start(ap, ctx);
|
adamc@922
|
3489 for (s = va_arg(ap, char*); s; s = va_arg(ap, char*))
|
adamc@922
|
3490 len += strlen(s);
|
adamc@922
|
3491 va_end(ap);
|
adamc@922
|
3492
|
adamc@922
|
3493 r = uw_malloc(ctx, len);
|
adamc@922
|
3494 va_start(ap, ctx);
|
adamc@922
|
3495 for (s2 = r, s = va_arg(ap, char*); s; s = va_arg(ap, char*))
|
adamc@922
|
3496 while (*s)
|
adamc@922
|
3497 *s2++ = *s++;
|
adamc@922
|
3498 va_end(ap);
|
adamc@922
|
3499 *s2 = 0;
|
adamc@922
|
3500
|
adamc@922
|
3501 return r;
|
adamc@922
|
3502 }
|
adamc@1006
|
3503
|
adam@1365
|
3504 const uw_Basis_time uw_Basis_minTime = {};
|
adamc@1050
|
3505
|
adamc@1006
|
3506 uw_Basis_time uw_Basis_now(uw_context ctx) {
|
adam@1365
|
3507 uw_Basis_time r = { time(NULL) };
|
adam@1365
|
3508 return r;
|
adamc@1006
|
3509 }
|
adamc@1061
|
3510
|
adam@1369
|
3511 uw_Basis_time uw_Basis_addSeconds(uw_context ctx, uw_Basis_time tm, uw_Basis_int n) {
|
adam@1369
|
3512 tm.seconds += n;
|
adam@1365
|
3513 return tm;
|
adamc@1138
|
3514 }
|
adamc@1138
|
3515
|
adamc@1061
|
3516 void *uw_get_global(uw_context ctx, char *name) {
|
adamc@1061
|
3517 int i;
|
adamc@1061
|
3518
|
adamc@1061
|
3519 for (i = 0; i < ctx->n_globals; ++i)
|
adamc@1061
|
3520 if (!strcmp(name, ctx->globals[i].name))
|
adamc@1061
|
3521 return ctx->globals[i].data;
|
adamc@1061
|
3522
|
adamc@1061
|
3523 return NULL;
|
adamc@1061
|
3524 }
|
adamc@1061
|
3525
|
adamc@1113
|
3526 size_t uw_globals_max = SIZE_MAX;
|
adamc@1113
|
3527
|
adamc@1061
|
3528 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) {
|
adamc@1061
|
3529 int i;
|
adamc@1061
|
3530
|
adamc@1061
|
3531 for (i = 0; i < ctx->n_globals; ++i)
|
adamc@1061
|
3532 if (!strcmp(name, ctx->globals[i].name)) {
|
adamc@1089
|
3533 if (ctx->globals[i].free)
|
adamc@1061
|
3534 ctx->globals[i].free(ctx->globals[i].data);
|
adamc@1061
|
3535 ctx->globals[i].data = data;
|
adamc@1061
|
3536 ctx->globals[i].free = free;
|
adamc@1061
|
3537 return;
|
adamc@1061
|
3538 }
|
adamc@1089
|
3539
|
adamc@1113
|
3540 if (ctx->n_globals+1 > uw_globals_max)
|
adamc@1113
|
3541 uw_error(ctx, FATAL, "Exceeded limit on number of globals");
|
adamc@1113
|
3542
|
adamc@1061
|
3543 ++ctx->n_globals;
|
adamc@1061
|
3544 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global));
|
adamc@1061
|
3545 ctx->globals[ctx->n_globals-1].name = name;
|
adamc@1061
|
3546 ctx->globals[ctx->n_globals-1].data = data;
|
adamc@1061
|
3547 ctx->globals[ctx->n_globals-1].free = free;
|
adamc@1061
|
3548 }
|
adamc@1061
|
3549
|
adamc@1061
|
3550 uw_Basis_bool uw_Basis_isalnum(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3551 return !!isalnum((int)c);
|
adamc@1061
|
3552 }
|
adamc@1061
|
3553
|
adamc@1061
|
3554 uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3555 return !!isalpha((int)c);
|
adamc@1061
|
3556 }
|
adamc@1061
|
3557
|
adamc@1061
|
3558 uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3559 return !!isblank((int)c);
|
adamc@1061
|
3560 }
|
adamc@1061
|
3561
|
adamc@1061
|
3562 uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3563 return !!iscntrl((int)c);
|
adamc@1061
|
3564 }
|
adamc@1061
|
3565
|
adamc@1061
|
3566 uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3567 return !!isdigit((int)c);
|
adamc@1061
|
3568 }
|
adamc@1061
|
3569
|
adamc@1061
|
3570 uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3571 return !!isgraph((int)c);
|
adamc@1061
|
3572 }
|
adamc@1061
|
3573
|
adamc@1061
|
3574 uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3575 return !!islower((int)c);
|
adamc@1061
|
3576 }
|
adamc@1061
|
3577
|
adamc@1061
|
3578 uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3579 return !!isprint((int)c);
|
adamc@1061
|
3580 }
|
adamc@1061
|
3581
|
adamc@1061
|
3582 uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3583 return !!ispunct((int)c);
|
adamc@1061
|
3584 }
|
adamc@1061
|
3585
|
adamc@1061
|
3586 uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3587 return !!isspace((int)c);
|
adamc@1061
|
3588 }
|
adamc@1063
|
3589
|
adamc@1061
|
3590 uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3591 return !!isupper((int)c);
|
adamc@1061
|
3592 }
|
adamc@1061
|
3593
|
adamc@1061
|
3594 uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3595 return !!isxdigit((int)c);
|
adamc@1061
|
3596 }
|
adamc@1061
|
3597
|
adamc@1061
|
3598 uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3599 return tolower((int)c);
|
adamc@1061
|
3600 }
|
adamc@1061
|
3601
|
adamc@1061
|
3602 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
|
adamc@1153
|
3603 return toupper((int)c);
|
adamc@1061
|
3604 }
|
adamc@1066
|
3605
|
adamc@1128
|
3606 uw_Basis_int uw_Basis_ord(uw_context ctx, uw_Basis_char c) {
|
adamc@1128
|
3607 return (unsigned char)c;
|
adamc@1128
|
3608 }
|
adamc@1128
|
3609
|
adamc@1128
|
3610 uw_Basis_char uw_Basis_chr(uw_context ctx, uw_Basis_int n) {
|
adamc@1128
|
3611 return n;
|
adamc@1128
|
3612 }
|
adamc@1128
|
3613
|
adamc@1066
|
3614 uw_Basis_string uw_Basis_currentUrl(uw_context ctx) {
|
adamc@1066
|
3615 return ctx->current_url;
|
adamc@1066
|
3616 }
|
adamc@1066
|
3617
|
adamc@1066
|
3618 void uw_set_currentUrl(uw_context ctx, char *s) {
|
adamc@1066
|
3619 ctx->current_url = s;
|
adamc@1066
|
3620 }
|
adamc@1114
|
3621
|
adamc@1114
|
3622 void uw_set_deadline(uw_context ctx, int n) {
|
adamc@1114
|
3623 ctx->deadline = n;
|
adamc@1114
|
3624 }
|
adamc@1114
|
3625
|
adamc@1114
|
3626 void uw_check_deadline(uw_context ctx) {
|
adamc@1114
|
3627 if (uw_time > ctx->deadline)
|
adamc@1114
|
3628 uw_error(ctx, FATAL, "Maximum running time exceeded");
|
adamc@1114
|
3629 }
|
adamc@1115
|
3630
|
adamc@1115
|
3631 size_t uw_database_max = SIZE_MAX;
|
adamc@1120
|
3632
|
adam@1389
|
3633 uw_Basis_int uw_Basis_naughtyDebug(uw_context ctx, uw_Basis_string s) {
|
adam@1389
|
3634 fprintf(stderr, "%s\n", s);
|
adam@1389
|
3635 return 0;
|
adam@1389
|
3636 }
|
adam@1389
|
3637
|
adamc@1120
|
3638 uw_Basis_unit uw_Basis_debug(uw_context ctx, uw_Basis_string s) {
|
adam@1334
|
3639 if (ctx->log_debug)
|
adam@1334
|
3640 ctx->log_debug(ctx->logger_data, "%s\n", s);
|
adam@1334
|
3641 else
|
adam@1334
|
3642 fprintf(stderr, "%s\n", s);
|
adamc@1120
|
3643 return uw_unit_v;
|
adamc@1120
|
3644 }
|
adamc@1250
|
3645
|
adamc@1250
|
3646 uw_Basis_int uw_Basis_rand(uw_context ctx) {
|
adamc@1250
|
3647 uw_Basis_int n = abs(rand());
|
adamc@1250
|
3648 return n;
|
adamc@1250
|
3649 }
|
adam@1347
|
3650
|
adam@1347
|
3651 void uw_noPostBody(uw_context ctx) {
|
adam@1347
|
3652 ctx->hasPostBody = 0;
|
adam@1347
|
3653 }
|
adam@1347
|
3654
|
adam@1347
|
3655 void uw_postBody(uw_context ctx, uw_Basis_postBody pb) {
|
adam@1347
|
3656 ctx->hasPostBody = 1;
|
adam@1347
|
3657 ctx->postBody = pb;
|
adam@1347
|
3658 }
|
adam@1347
|
3659
|
adam@1347
|
3660 int uw_hasPostBody(uw_context ctx) {
|
adam@1347
|
3661 return ctx->hasPostBody;
|
adam@1347
|
3662 }
|
adam@1347
|
3663
|
adam@1386
|
3664 void uw_isPost(uw_context ctx) {
|
adam@1386
|
3665 ctx->isPost = 1;
|
adam@1386
|
3666 }
|
adam@1386
|
3667
|
adam@1386
|
3668 uw_Basis_bool uw_Basis_currentUrlHasPost(uw_context ctx) {
|
adam@1386
|
3669 return ctx->isPost;
|
adam@1386
|
3670 }
|
adam@1386
|
3671
|
adam@1370
|
3672 void uw_setQueryString(uw_context ctx, uw_Basis_string s) {
|
adam@1370
|
3673 ctx->queryString = s;
|
adam@1370
|
3674 }
|
adam@1370
|
3675
|
adam@1370
|
3676 uw_Basis_string uw_queryString(uw_context ctx) {
|
adam@1370
|
3677 return ctx->queryString;
|
adam@1370
|
3678 }
|
adam@1370
|
3679
|
adam@1347
|
3680 uw_Basis_postBody uw_getPostBody(uw_context ctx) {
|
adam@1347
|
3681 if (ctx->hasPostBody)
|
adam@1347
|
3682 return ctx->postBody;
|
adam@1347
|
3683 else
|
adam@1347
|
3684 uw_error(ctx, FATAL, "Asked for POST body when none exists");
|
adam@1347
|
3685 }
|
adam@1349
|
3686
|
adam@1349
|
3687 failure_kind uw_runCallback(uw_context ctx, void (*callback)(uw_context)) {
|
adam@1349
|
3688 int r = setjmp(ctx->jmp_buf);
|
adam@1349
|
3689
|
adam@1349
|
3690 if (ctx->app->db_begin(ctx))
|
adam@1349
|
3691 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
|
adam@1349
|
3692
|
adam@1349
|
3693 if (r == 0) {
|
adam@1349
|
3694 callback(ctx);
|
adam@1349
|
3695 uw_commit(ctx);
|
adam@1349
|
3696 }
|
adam@1349
|
3697 else
|
adam@1349
|
3698 uw_rollback(ctx, 0);
|
adam@1349
|
3699
|
adam@1349
|
3700 return r;
|
adam@1349
|
3701 }
|
adam@1360
|
3702
|
adam@1360
|
3703 uw_Basis_string uw_Basis_crypt(uw_context ctx, uw_Basis_string key, uw_Basis_string salt) {
|
adam@1397
|
3704 char buf[14];
|
adam@1397
|
3705 return uw_strdup(ctx, DES_fcrypt(key, salt, buf));
|
adam@1360
|
3706 }
|
adam@1365
|
3707
|
adam@1365
|
3708 uw_Basis_bool uw_Basis_eq_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
|
adam@1365
|
3709 return !!(t1.seconds == t2.seconds && t1.microseconds == t2.microseconds);
|
adam@1365
|
3710 }
|
adam@1365
|
3711
|
adam@1365
|
3712 uw_Basis_bool uw_Basis_lt_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
|
adam@1365
|
3713 return !!(t1.seconds < t2.seconds || t1.microseconds < t2.microseconds);
|
adam@1365
|
3714 }
|
adam@1365
|
3715
|
adam@1365
|
3716 uw_Basis_bool uw_Basis_le_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
|
adam@1365
|
3717 return !!(uw_Basis_eq_time(ctx, t1, t2) || uw_Basis_lt_time(ctx, t1, t2));
|
adam@1365
|
3718 }
|
adam@1365
|
3719
|
adam@1374
|
3720 /*uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
|
adam@1374
|
3721 uw_Basis_time *r = uw_Basis_stringToTime(ctx, s);
|
adam@1374
|
3722
|
adam@1374
|
3723 printf("timezone = %ld\n", timezone);
|
adam@1374
|
3724
|
adam@1374
|
3725 if (r)
|
adam@1374
|
3726 r->seconds -= timezone;
|
adam@1374
|
3727
|
adam@1374
|
3728 return r;
|
adam@1374
|
3729 }*/
|
adam@1374
|
3730
|
adam@1372
|
3731 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
|
adam@1374
|
3732 struct tm stm = {};
|
adam@1375
|
3733 char *end = strchr(s, 0);
|
adam@1392
|
3734 stm.tm_isdst = -1;
|
adam@1375
|
3735
|
adam@1375
|
3736 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end) {
|
adam@1373
|
3737 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adam@1375
|
3738
|
adam@1375
|
3739 tzset();
|
adam@1374
|
3740 stm.tm_hour -= timezone / (60 * 60);
|
adam@1375
|
3741
|
adam@1374
|
3742 r->seconds = mktime(&stm);
|
adam@1373
|
3743 r->microseconds = 0;
|
adam@1373
|
3744
|
adam@1373
|
3745 return r;
|
adam@1374
|
3746 }
|
adam@1374
|
3747 else
|
adam@1373
|
3748 return NULL;
|
adam@1372
|
3749 }
|