Mercurial > urweb
comparison src/c/urweb.c @ 1134:b08b73591d2c
Switch to gcc -Wall
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 28 Jan 2010 13:32:26 -0500 |
parents | 94e83c5533d2 |
children | b7118ffd32ae |
comparison
equal
deleted
inserted
replaced
1133:482815817e99 | 1134:b08b73591d2c |
---|---|
81 next = b->back - b->start; | 81 next = b->back - b->start; |
82 if (next == 0) | 82 if (next == 0) |
83 next = 1; | 83 next = 1; |
84 for (; next < desired; next *= 2); | 84 for (; next < desired; next *= 2); |
85 | 85 |
86 if (next > b->max) | 86 if (next > b->max) { |
87 if (desired <= b->max) | 87 if (desired <= b->max) |
88 next = desired; | 88 next = desired; |
89 else | 89 else |
90 return 1; | 90 return 1; |
91 } | |
91 | 92 |
92 new_heap = realloc(b->start, next); | 93 new_heap = realloc(b->start, next); |
93 b->front = new_heap + (b->front - b->start); | 94 b->front = new_heap + (b->front - b->start); |
94 b->back = new_heap + next; | 95 b->back = new_heap + next; |
95 b->start = new_heap; | 96 b->start = new_heap; |
517 | 518 |
518 ctx->sz_inputs = app->inputs_len; | 519 ctx->sz_inputs = app->inputs_len; |
519 ctx->inputs = realloc(ctx->inputs, ctx->sz_inputs * sizeof(input)); | 520 ctx->inputs = realloc(ctx->inputs, ctx->sz_inputs * sizeof(input)); |
520 memset(ctx->inputs, 0, ctx->sz_inputs * sizeof(input)); | 521 memset(ctx->inputs, 0, ctx->sz_inputs * sizeof(input)); |
521 } | 522 } |
523 | |
524 return 0; | |
522 } | 525 } |
523 | 526 |
524 void uw_set_client_data(uw_context ctx, void *data) { | 527 void uw_set_client_data(uw_context ctx, void *data) { |
525 uw_copy_client_data(ctx->client_data, data); | 528 uw_copy_client_data(ctx->client_data, data); |
526 } | 529 } |
555 | 558 |
556 free(ctx); | 559 free(ctx); |
557 } | 560 } |
558 | 561 |
559 void uw_reset_keep_error_message(uw_context ctx) { | 562 void uw_reset_keep_error_message(uw_context ctx) { |
560 size_t i; | |
561 | |
562 buf_reset(&ctx->outHeaders); | 563 buf_reset(&ctx->outHeaders); |
563 buf_reset(&ctx->script); | 564 buf_reset(&ctx->script); |
564 ctx->script.start[0] = 0; | 565 ctx->script.start[0] = 0; |
565 buf_reset(&ctx->page); | 566 buf_reset(&ctx->page); |
566 ctx->returning_indirectly = 0; | 567 ctx->returning_indirectly = 0; |
641 if (len == 0) | 642 if (len == 0) |
642 newLen = 1; | 643 newLen = 1; |
643 else | 644 else |
644 newLen = len * 2; | 645 newLen = len * 2; |
645 | 646 |
646 if (newLen > uw_cleanup_max) | 647 if (newLen > uw_cleanup_max) { |
647 if (len+1 <= uw_cleanup_max) | 648 if (len+1 <= uw_cleanup_max) |
648 newLen = uw_cleanup_max; | 649 newLen = uw_cleanup_max; |
649 else | 650 else |
650 uw_error(ctx, FATAL, "Exceeded limit on number of cleanup handlers"); | 651 uw_error(ctx, FATAL, "Exceeded limit on number of cleanup handlers"); |
652 } | |
651 | 653 |
652 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup)); | 654 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup)); |
653 ctx->cleanup_front = ctx->cleanup + len; | 655 ctx->cleanup_front = ctx->cleanup + len; |
654 ctx->cleanup_back = ctx->cleanup + newLen; | 656 ctx->cleanup_back = ctx->cleanup + newLen; |
655 } | 657 } |
762 break; | 764 break; |
763 case ENTRY: | 765 case ENTRY: |
764 adjust_pointer(&x->data.entry.fields, old_start, new_start, len); | 766 adjust_pointer(&x->data.entry.fields, old_start, new_start, len); |
765 adjust_pointer(&x->data.entry.next, old_start, new_start, len); | 767 adjust_pointer(&x->data.entry.next, old_start, new_start, len); |
766 adjust_pointer(&x->data.entry.parent, old_start, new_start, len); | 768 adjust_pointer(&x->data.entry.parent, old_start, new_start, len); |
769 break; | |
770 default: | |
771 break; | |
767 } | 772 } |
768 } | 773 } |
769 | 774 |
770 size_t uw_subinputs_max = SIZE_MAX; | 775 size_t uw_subinputs_max = SIZE_MAX; |
771 | 776 |
776 if (ctx->used_subinputs + len >= ctx->n_subinputs) { | 781 if (ctx->used_subinputs + len >= ctx->n_subinputs) { |
777 if (ctx->used_subinputs + len > uw_subinputs_max) | 782 if (ctx->used_subinputs + len > uw_subinputs_max) |
778 uw_error(ctx, FATAL, "Exceeded limit on number of subinputs"); | 783 uw_error(ctx, FATAL, "Exceeded limit on number of subinputs"); |
779 | 784 |
780 input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len)); | 785 input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len)); |
781 size_t offset = new_subinputs - ctx->subinputs; | |
782 | 786 |
783 if (ctx->subinputs != new_subinputs) { | 787 if (ctx->subinputs != new_subinputs) { |
784 for (i = 0; i < ctx->used_subinputs; ++i) | 788 for (i = 0; i < ctx->used_subinputs; ++i) |
785 adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs); | 789 adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs); |
786 for (i = 0; i < ctx->app->inputs_len; ++i) | 790 for (i = 0; i < ctx->app->inputs_len; ++i) |
976 | 980 |
977 return 0; | 981 return 0; |
978 } | 982 } |
979 | 983 |
980 void *uw_malloc(uw_context ctx, size_t len); | 984 void *uw_malloc(uw_context ctx, size_t len); |
981 | |
982 static void parents(input *inp) { | |
983 printf("Stack: %p\n", inp); | |
984 while (inp) { | |
985 switch (inp->kind) { | |
986 case NORMAL: | |
987 printf("Normal(%p)\n", inp); | |
988 break; | |
989 case FIL: | |
990 printf("File(%p)\n", inp); | |
991 break; | |
992 case SUBFORM: | |
993 printf("Subform; fields = %p\n", inp->data.subform.fields); | |
994 inp = inp->data.subform.parent; | |
995 break; | |
996 case SUBFORMS: | |
997 printf("Subforms; entries = %p\n", inp->data.subforms.entries); | |
998 inp = inp->data.subforms.parent; | |
999 break; | |
1000 case ENTRY: | |
1001 printf("Entry; fields = %p; next = %p\n", inp->data.entry.fields, inp->data.entry.next); | |
1002 inp = inp->data.entry.parent; | |
1003 break; | |
1004 default: | |
1005 inp = NULL; | |
1006 } | |
1007 } | |
1008 } | |
1009 | 985 |
1010 uw_Basis_file uw_get_file_input(uw_context ctx, int n) { | 986 uw_Basis_file uw_get_file_input(uw_context ctx, int n) { |
1011 if (n < 0) | 987 if (n < 0) |
1012 uw_error(ctx, FATAL, "Negative file input index %d", n); | 988 uw_error(ctx, FATAL, "Negative file input index %d", n); |
1013 if (n >= ctx->app->inputs_len) | 989 if (n >= ctx->app->inputs_len) |
1157 next = b->back - b->start; | 1133 next = b->back - b->start; |
1158 if (next == 0) | 1134 if (next == 0) |
1159 next = 1; | 1135 next = 1; |
1160 for (; next < desired; next *= 2); | 1136 for (; next < desired; next *= 2); |
1161 | 1137 |
1162 if (next > b->max) | 1138 if (next > b->max) { |
1163 if (desired <= b->max) | 1139 if (desired <= b->max) |
1164 next = desired; | 1140 next = desired; |
1165 else | 1141 else |
1166 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind); | 1142 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind); |
1143 } | |
1167 | 1144 |
1168 new_heap = realloc(b->start, next); | 1145 new_heap = realloc(b->start, next); |
1169 b->front = new_heap + (b->front - b->start); | 1146 b->front = new_heap + (b->front - b->start); |
1170 b->back = new_heap + next; | 1147 b->back = new_heap + next; |
1171 | 1148 |
1220 ctx->heap.front = (char *) r; | 1197 ctx->heap.front = (char *) r; |
1221 ctx->regions = r->next; | 1198 ctx->regions = r->next; |
1222 } | 1199 } |
1223 | 1200 |
1224 void uw_memstats(uw_context ctx) { | 1201 void uw_memstats(uw_context ctx) { |
1225 printf("Headers: %d/%d\n", buf_used(&ctx->outHeaders), buf_avail(&ctx->outHeaders)); | 1202 printf("Headers: %lu/%lu\n", (unsigned long)buf_used(&ctx->outHeaders), (unsigned long)buf_avail(&ctx->outHeaders)); |
1226 printf("Script: %d/%d\n", buf_used(&ctx->script), buf_avail(&ctx->script)); | 1203 printf("Script: %lu/%lu\n", (unsigned long)buf_used(&ctx->script), (unsigned long)buf_avail(&ctx->script)); |
1227 printf("Page: %d/%d\n", buf_used(&ctx->page), buf_avail(&ctx->page)); | 1204 printf("Page: %lu/%lu\n", (unsigned long)buf_used(&ctx->page), (unsigned long)buf_avail(&ctx->page)); |
1228 printf("Heap: %d/%d\n", buf_used(&ctx->heap), buf_avail(&ctx->heap)); | 1205 printf("Heap: %lu/%lu\n", (unsigned long)buf_used(&ctx->heap), (unsigned long)buf_avail(&ctx->heap)); |
1229 } | 1206 } |
1230 | 1207 |
1231 int uw_send(uw_context ctx, int sock) { | 1208 int uw_send(uw_context ctx, int sock) { |
1232 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start); | 1209 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start); |
1233 | 1210 |
1545 sprintf(result, "%g%n", n, &len); | 1522 sprintf(result, "%g%n", n, &len); |
1546 ctx->heap.front += len+1; | 1523 ctx->heap.front += len+1; |
1547 return result; | 1524 return result; |
1548 } | 1525 } |
1549 | 1526 |
1550 static int isCont(unsigned char ch) { | |
1551 return ch / 64 == 2; | |
1552 } | |
1553 | |
1554 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) { | 1527 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) { |
1555 int len = strlen(s); | 1528 int len = strlen(s); |
1556 char *result, *p; | 1529 char *result, *p; |
1557 uw_check_heap(ctx, len * 6 + 1); | 1530 uw_check_heap(ctx, len * 6 + 1); |
1558 | 1531 |
1891 return r; | 1864 return r; |
1892 } | 1865 } |
1893 | 1866 |
1894 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) { | 1867 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) { |
1895 char *new_s = uw_unurlify_advance(*s); | 1868 char *new_s = uw_unurlify_advance(*s); |
1896 char *r, *s1, *s2; | 1869 char *r; |
1897 int len, n; | 1870 int len; |
1898 | 1871 |
1899 len = strlen(*s); | 1872 len = strlen(*s); |
1900 uw_check_heap(ctx, len + 1); | 1873 uw_check_heap(ctx, len + 1); |
1901 | 1874 |
1902 r = ctx->heap.front; | 1875 r = ctx->heap.front; |
1910 return uw_unit_v; | 1883 return uw_unit_v; |
1911 } | 1884 } |
1912 | 1885 |
1913 uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) { | 1886 uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) { |
1914 char *new_s = uw_unurlify_advance(*s); | 1887 char *new_s = uw_unurlify_advance(*s); |
1915 char *r, *s1, *s2; | 1888 char *r; |
1916 int len, n; | 1889 int len; |
1917 | 1890 |
1918 len = strlen(*s); | 1891 len = strlen(*s); |
1919 uw_check_heap(ctx, len + 1); | 1892 uw_check_heap(ctx, len + 1); |
1920 | 1893 |
1921 r = ctx->heap.front; | 1894 r = ctx->heap.front; |
2763 | 2736 |
2764 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) { | 2737 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) { |
2765 int len = strlen(c); | 2738 int len = strlen(c); |
2766 char *p = ctx->outHeaders.start; | 2739 char *p = ctx->outHeaders.start; |
2767 | 2740 |
2768 while (p = strstr(p, "\nSet-Cookie: ")) { | 2741 while ((p = strstr(p, "\nSet-Cookie: "))) { |
2769 char *p2; | 2742 char *p2; |
2770 p += 13; | 2743 p += 13; |
2771 p2 = strchr(p, '='); | 2744 p2 = strchr(p, '='); |
2772 | 2745 |
2773 if (p2) { | 2746 if (p2) { |
2784 } | 2757 } |
2785 } | 2758 } |
2786 } | 2759 } |
2787 } | 2760 } |
2788 | 2761 |
2789 if (p = uw_Basis_requestHeader(ctx, "Cookie")) { | 2762 if ((p = uw_Basis_requestHeader(ctx, "Cookie"))) { |
2790 char *p2; | 2763 char *p2; |
2791 | 2764 |
2792 while (1) { | 2765 while (1) { |
2793 if (!strncmp(p, c, len) && p[len] == '=') { | 2766 if (!strncmp(p, c, len) && p[len] == '=') { |
2794 if (p2 = strchr(p, ';')) { | 2767 if ((p2 = strchr(p, ';'))) { |
2795 size_t n = p2 - (p + len); | 2768 size_t n = p2 - (p + len); |
2796 char *r = uw_malloc(ctx, n); | 2769 char *r = uw_malloc(ctx, n); |
2797 memcpy(r, p + 1 + len, n-1); | 2770 memcpy(r, p + 1 + len, n-1); |
2798 r[n-1] = 0; | 2771 r[n-1] = 0; |
2799 return r; | 2772 return r; |
2800 } else | 2773 } else |
2801 return p + 1 + len; | 2774 return p + 1 + len; |
2802 } else if (p = strchr(p, ';')) | 2775 } else if ((p = strchr(p, ';'))) |
2803 p += 2; | 2776 p += 2; |
2804 else | 2777 else |
2805 return NULL; | 2778 return NULL; |
2806 } | 2779 } |
2807 } | 2780 } |
3192 uw_write_header(ctx, on_success); | 3165 uw_write_header(ctx, on_success); |
3193 uw_write_header(ctx, "Content-Type: "); | 3166 uw_write_header(ctx, "Content-Type: "); |
3194 uw_write_header(ctx, mimeType); | 3167 uw_write_header(ctx, mimeType); |
3195 uw_write_header(ctx, "\r\nContent-Length: "); | 3168 uw_write_header(ctx, "\r\nContent-Length: "); |
3196 ctx_buf_check(ctx, "headers", &ctx->outHeaders, INTS_MAX); | 3169 ctx_buf_check(ctx, "headers", &ctx->outHeaders, INTS_MAX); |
3197 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len); | 3170 sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)b.size, &len); |
3198 ctx->outHeaders.front += len; | 3171 ctx->outHeaders.front += len; |
3199 uw_write_header(ctx, "\r\n"); | 3172 uw_write_header(ctx, "\r\n"); |
3200 | 3173 |
3201 ctx_buf_append(ctx, "page", &ctx->page, b.data, b.size); | 3174 ctx_buf_append(ctx, "page", &ctx->page, b.data, b.size); |
3202 | 3175 |
3208 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY); | 3181 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY); |
3209 } | 3182 } |
3210 | 3183 |
3211 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) { | 3184 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) { |
3212 cleanup *cl; | 3185 cleanup *cl; |
3213 int len; | |
3214 char *s; | 3186 char *s; |
3215 | 3187 |
3216 ctx->returning_indirectly = 1; | 3188 ctx->returning_indirectly = 1; |
3217 buf_reset(&ctx->page); | 3189 buf_reset(&ctx->page); |
3218 ctx_buf_check(ctx, "page", &ctx->page, buf_used(&ctx->outHeaders)+1); | 3190 ctx_buf_check(ctx, "page", &ctx->page, buf_used(&ctx->outHeaders)+1); |
3223 uw_write_header(ctx, on_redirect); | 3195 uw_write_header(ctx, on_redirect); |
3224 | 3196 |
3225 s = strchr(ctx->page.start, '\n'); | 3197 s = strchr(ctx->page.start, '\n'); |
3226 if (s) { | 3198 if (s) { |
3227 char *s2; | 3199 char *s2; |
3228 for (++s; s2 = strchr(s, '\n'); s = s2+1) { | 3200 for (++s; (s2 = strchr(s, '\n')); s = s2+1) { |
3229 *s2 = 0; | 3201 *s2 = 0; |
3230 if (!strncmp(s, "Set-Cookie: ", 12)) { | 3202 if (!strncmp(s, "Set-Cookie: ", 12)) { |
3231 uw_write_header(ctx, s); | 3203 uw_write_header(ctx, s); |
3232 uw_write_header(ctx, "\n"); | 3204 uw_write_header(ctx, "\n"); |
3233 } | 3205 } |