Mercurial > urweb
comparison src/c/urweb.c @ 458:8f65b0fa3b29
Avoid allocating strings for requestHeader
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 06 Nov 2008 10:04:03 -0500 |
parents | 360cbc202756 |
children | 21bb5bbba2e9 |
comparison
equal
deleted
inserted
replaced
457:360cbc202756 | 458:8f65b0fa3b29 |
---|---|
22 void (*func)(void*); | 22 void (*func)(void*); |
23 void *arg; | 23 void *arg; |
24 } cleanup; | 24 } cleanup; |
25 | 25 |
26 struct uw_context { | 26 struct uw_context { |
27 char *headers; | 27 char *headers, *headers_end; |
28 | 28 |
29 char *page, *page_front, *page_back; | 29 char *page, *page_front, *page_back; |
30 char *heap, *heap_front, *heap_back; | 30 char *heap, *heap_front, *heap_back; |
31 char **inputs; | 31 char **inputs; |
32 | 32 |
44 extern int uw_inputs_len; | 44 extern int uw_inputs_len; |
45 | 45 |
46 uw_context uw_init(size_t page_len, size_t heap_len) { | 46 uw_context uw_init(size_t page_len, size_t heap_len) { |
47 uw_context ctx = malloc(sizeof(struct uw_context)); | 47 uw_context ctx = malloc(sizeof(struct uw_context)); |
48 | 48 |
49 ctx->headers = NULL; | 49 ctx->headers = ctx->headers_end = NULL; |
50 | 50 |
51 ctx->page_front = ctx->page = malloc(page_len); | 51 ctx->page_front = ctx->page = malloc(page_len); |
52 ctx->page_back = ctx->page_front + page_len; | 52 ctx->page_back = ctx->page_front + page_len; |
53 | 53 |
54 ctx->heap_front = ctx->heap = malloc(heap_len); | 54 ctx->heap_front = ctx->heap = malloc(heap_len); |
114 uw_db_init(ctx); | 114 uw_db_init(ctx); |
115 | 115 |
116 return r; | 116 return r; |
117 } | 117 } |
118 | 118 |
119 failure_kind uw_begin(uw_context ctx, char *headers, char *path) { | 119 void uw_set_headers(uw_context ctx, char *headers) { |
120 char *s = headers, *s2; | |
121 ctx->headers = headers; | |
122 | |
123 while (s2 = strchr(s, '\r')) { | |
124 s = s2; | |
125 | |
126 if (s[1] == 0) | |
127 break; | |
128 | |
129 *s = 0; | |
130 s += 2; | |
131 } | |
132 | |
133 ctx->headers_end = s; | |
134 } | |
135 | |
136 failure_kind uw_begin(uw_context ctx, char *path) { | |
120 int r = setjmp(ctx->jmp_buf); | 137 int r = setjmp(ctx->jmp_buf); |
121 | |
122 ctx->headers = headers; | |
123 | 138 |
124 if (r == 0) | 139 if (r == 0) |
125 uw_handle(ctx, path); | 140 uw_handle(ctx, path); |
126 | 141 |
127 return r; | 142 return r; |
1063 int len = strlen(h); | 1078 int len = strlen(h); |
1064 char *s = ctx->headers, *p; | 1079 char *s = ctx->headers, *p; |
1065 | 1080 |
1066 while (p = strchr(s, ':')) { | 1081 while (p = strchr(s, ':')) { |
1067 if (p - s == len && !strncasecmp(s, h, len)) { | 1082 if (p - s == len && !strncasecmp(s, h, len)) { |
1068 s = p + 2; | 1083 return p + 2; |
1069 if (p = strchr(s, '\r')) { | 1084 } else { |
1070 uw_Basis_string ret = uw_malloc(ctx, p - s + 1); | 1085 if ((s = strchr(p, 0)) && s < ctx->headers_end) |
1071 memcpy(ret, s, p - s); | 1086 s += 2; |
1072 ret[p - s] = 0; | |
1073 return ret; | |
1074 } | |
1075 else | 1087 else |
1076 return NULL; | 1088 return NULL; |
1077 } else { | 1089 } |
1078 if (s = strchr(s, '\n')) | 1090 } |
1079 ++s; | 1091 |
1080 else | 1092 } |
1081 return NULL; | |
1082 } | |
1083 } | |
1084 | |
1085 } |