Mercurial > urweb
comparison src/c/urweb.c @ 462:21bb5bbba2e9
Setting a cookie
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 06 Nov 2008 11:29:16 -0500 |
parents | 8f65b0fa3b29 |
children | bb27c7efcd90 |
comparison
equal
deleted
inserted
replaced
461:5c9606deacb6 | 462:21bb5bbba2e9 |
---|---|
24 } cleanup; | 24 } cleanup; |
25 | 25 |
26 struct uw_context { | 26 struct uw_context { |
27 char *headers, *headers_end; | 27 char *headers, *headers_end; |
28 | 28 |
29 char *outHeaders, *outHeaders_front, *outHeaders_back; | |
29 char *page, *page_front, *page_back; | 30 char *page, *page_front, *page_back; |
30 char *heap, *heap_front, *heap_back; | 31 char *heap, *heap_front, *heap_back; |
31 char **inputs; | 32 char **inputs; |
32 | 33 |
33 void *db; | 34 void *db; |
41 char error_message[ERROR_BUF_LEN]; | 42 char error_message[ERROR_BUF_LEN]; |
42 }; | 43 }; |
43 | 44 |
44 extern int uw_inputs_len; | 45 extern int uw_inputs_len; |
45 | 46 |
46 uw_context uw_init(size_t page_len, size_t heap_len) { | 47 uw_context uw_init(size_t outHeaders_len, size_t page_len, size_t heap_len) { |
47 uw_context ctx = malloc(sizeof(struct uw_context)); | 48 uw_context ctx = malloc(sizeof(struct uw_context)); |
48 | 49 |
49 ctx->headers = ctx->headers_end = NULL; | 50 ctx->headers = ctx->headers_end = NULL; |
51 | |
52 ctx->outHeaders_front = ctx->outHeaders = malloc(outHeaders_len); | |
53 ctx->outHeaders_back = ctx->outHeaders_front + outHeaders_len; | |
54 | |
55 ctx->heap_front = ctx->heap = malloc(heap_len); | |
50 | 56 |
51 ctx->page_front = ctx->page = malloc(page_len); | 57 ctx->page_front = ctx->page = malloc(page_len); |
52 ctx->page_back = ctx->page_front + page_len; | 58 ctx->page_back = ctx->page_front + page_len; |
53 | 59 |
54 ctx->heap_front = ctx->heap = malloc(heap_len); | 60 ctx->heap_front = ctx->heap = malloc(heap_len); |
74 void *uw_get_db(uw_context ctx) { | 80 void *uw_get_db(uw_context ctx) { |
75 return ctx->db; | 81 return ctx->db; |
76 } | 82 } |
77 | 83 |
78 void uw_free(uw_context ctx) { | 84 void uw_free(uw_context ctx) { |
85 free(ctx->outHeaders); | |
79 free(ctx->page); | 86 free(ctx->page); |
80 free(ctx->heap); | 87 free(ctx->heap); |
81 free(ctx->inputs); | 88 free(ctx->inputs); |
82 free(ctx->cleanup); | 89 free(ctx->cleanup); |
83 free(ctx); | 90 free(ctx); |
84 } | 91 } |
85 | 92 |
86 void uw_reset_keep_request(uw_context ctx) { | 93 void uw_reset_keep_request(uw_context ctx) { |
94 ctx->outHeaders_front = ctx->outHeaders; | |
87 ctx->page_front = ctx->page; | 95 ctx->page_front = ctx->page; |
88 ctx->heap_front = ctx->heap; | 96 ctx->heap_front = ctx->heap; |
89 ctx->regions = NULL; | 97 ctx->regions = NULL; |
90 ctx->cleanup_front = ctx->cleanup; | 98 ctx->cleanup_front = ctx->cleanup; |
91 | 99 |
92 ctx->error_message[0] = 0; | 100 ctx->error_message[0] = 0; |
93 } | 101 } |
94 | 102 |
95 void uw_reset_keep_error_message(uw_context ctx) { | 103 void uw_reset_keep_error_message(uw_context ctx) { |
104 ctx->outHeaders_front = ctx->outHeaders; | |
96 ctx->page_front = ctx->page; | 105 ctx->page_front = ctx->page; |
97 ctx->heap_front = ctx->heap; | 106 ctx->heap_front = ctx->heap; |
98 ctx->regions = NULL; | 107 ctx->regions = NULL; |
99 ctx->cleanup_front = ctx->cleanup; | 108 ctx->cleanup_front = ctx->cleanup; |
100 } | 109 } |
274 ctx->heap_front = (char *) r; | 283 ctx->heap_front = (char *) r; |
275 ctx->regions = r->next; | 284 ctx->regions = r->next; |
276 } | 285 } |
277 | 286 |
278 void uw_memstats(uw_context ctx) { | 287 void uw_memstats(uw_context ctx) { |
288 printf("Headers: %d/%d\n", ctx->outHeaders_front - ctx->outHeaders, ctx->outHeaders_back - ctx->outHeaders); | |
279 printf("Page: %d/%d\n", ctx->page_front - ctx->page, ctx->page_back - ctx->page); | 289 printf("Page: %d/%d\n", ctx->page_front - ctx->page, ctx->page_back - ctx->page); |
280 printf("Heap: %d/%d\n", ctx->heap_front - ctx->heap, ctx->heap_back - ctx->heap); | 290 printf("Heap: %d/%d\n", ctx->heap_front - ctx->heap, ctx->heap_back - ctx->heap); |
281 } | 291 } |
282 | 292 |
283 int uw_really_send(int sock, const void *buf, size_t len) { | 293 int uw_really_send(int sock, const void *buf, size_t len) { |
293 | 303 |
294 return 0; | 304 return 0; |
295 } | 305 } |
296 | 306 |
297 int uw_send(uw_context ctx, int sock) { | 307 int uw_send(uw_context ctx, int sock) { |
298 return uw_really_send(sock, ctx->page, ctx->page_front - ctx->page); | 308 int n = uw_really_send(sock, ctx->outHeaders, ctx->outHeaders_front - ctx->outHeaders); |
309 | |
310 if (n < 0) | |
311 return n; | |
312 | |
313 n = uw_really_send(sock, "\r\n", 2); | |
314 | |
315 if (n < 0) | |
316 return n; | |
317 | |
318 n = uw_really_send(sock, "<html>", 6); | |
319 | |
320 if (n < 0) | |
321 return n; | |
322 | |
323 n = uw_really_send(sock, ctx->page, ctx->page_front - ctx->page); | |
324 | |
325 if (n < 0) | |
326 return n; | |
327 | |
328 return uw_really_send(sock, "</html>", 7); | |
329 } | |
330 | |
331 static void uw_check_headers(uw_context ctx, size_t extra) { | |
332 size_t desired = ctx->outHeaders_front - ctx->outHeaders + extra, next; | |
333 char *new_outHeaders; | |
334 | |
335 next = ctx->outHeaders_back - ctx->outHeaders; | |
336 if (next < desired) { | |
337 if (next == 0) | |
338 next = 1; | |
339 for (; next < desired; next *= 2); | |
340 | |
341 new_outHeaders = realloc(ctx->outHeaders, next); | |
342 ctx->outHeaders_front = new_outHeaders + (ctx->outHeaders_front - ctx->outHeaders); | |
343 ctx->outHeaders_back = new_outHeaders + next; | |
344 ctx->outHeaders = new_outHeaders; | |
345 } | |
346 } | |
347 | |
348 void uw_write_header(uw_context ctx, uw_Basis_string s) { | |
349 int len = strlen(s); | |
350 | |
351 uw_check_headers(ctx, len + 1); | |
352 strcpy(ctx->outHeaders_front, s); | |
353 ctx->outHeaders_front += len; | |
299 } | 354 } |
300 | 355 |
301 static void uw_check(uw_context ctx, size_t extra) { | 356 static void uw_check(uw_context ctx, size_t extra) { |
302 size_t desired = ctx->page_front - ctx->page + extra, next; | 357 size_t desired = ctx->page_front - ctx->page + extra, next; |
303 char *new_page; | 358 char *new_page; |
1088 return NULL; | 1143 return NULL; |
1089 } | 1144 } |
1090 } | 1145 } |
1091 | 1146 |
1092 } | 1147 } |
1148 | |
1149 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string c, uw_Basis_string v) { | |
1150 uw_write_header(ctx, "Set-Cookie: "); | |
1151 uw_write_header(ctx, c); | |
1152 uw_write_header(ctx, "="); | |
1153 uw_write_header(ctx, v); | |
1154 uw_write_header(ctx, "\r\n"); | |
1155 | |
1156 return uw_unit_v; | |
1157 } |