comparison src/c/urweb.c @ 1444:0fc7b676b88b

Escape strings as needed in some error messages within urweb.c
author Adam Chlipala <adam@chlipala.net>
date Sun, 27 Mar 2011 15:51:37 -0400
parents 6ff10428de4a
children 36f7d1debb37
comparison
equal deleted inserted replaced
1443:19e8e3d556d6 1444:0fc7b676b88b
682 682
683 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) { 683 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
684 return ctx->get_header(ctx->get_header_data, h); 684 return ctx->get_header(ctx->get_header_data, h);
685 } 685 }
686 686
687 char *uw_Basis_htmlifyString(uw_context, const char *);
688
687 void uw_login(uw_context ctx) { 689 void uw_login(uw_context ctx) {
688 if (ctx->needs_push) { 690 if (ctx->needs_push) {
689 char *id_s, *pass_s; 691 char *id_s, *pass_s;
690 692
691 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client")) 693 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client"))
693 unsigned id = atoi(id_s); 695 unsigned id = atoi(id_s);
694 int pass = atoi(pass_s); 696 int pass = atoi(pass_s);
695 client *c = find_client(id); 697 client *c = find_client(id);
696 698
697 if (c == NULL) 699 if (c == NULL)
698 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", id_s, pass_s); 700 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", uw_Basis_htmlifyString(ctx, id_s), uw_Basis_htmlifyString(ctx, pass_s));
699 else { 701 else {
700 use_client(c); 702 use_client(c);
701 ctx->client = c; 703 ctx->client = c;
702 704
703 if (c->mode != USED) 705 if (c->mode != USED)
830 if (!strcasecmp(name, ".b")) { 832 if (!strcasecmp(name, ".b")) {
831 int n = ctx->app->input_num(value); 833 int n = ctx->app->input_num(value);
832 input *inps; 834 input *inps;
833 835
834 if (n < 0) { 836 if (n < 0) {
835 uw_set_error(ctx, "Bad subform name %s", value); 837 uw_set_error(ctx, "Bad subform name %s", uw_Basis_htmlifyString(ctx, value));
836 return -1; 838 return -1;
837 } 839 }
838 840
839 if (n >= ctx->app->inputs_len) { 841 if (n >= ctx->app->inputs_len) {
840 uw_set_error(ctx, "For subform name %s, index %d is out of range", value, n); 842 uw_set_error(ctx, "For subform name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, value), n);
841 return -1; 843 return -1;
842 } 844 }
843 845
844 inps = check_input_space(ctx, ctx->app->inputs_len); 846 inps = check_input_space(ctx, ctx->app->inputs_len);
845 847
874 } 876 }
875 } else if (!strcasecmp(name, ".s")) { 877 } else if (!strcasecmp(name, ".s")) {
876 int n = ctx->app->input_num(value); 878 int n = ctx->app->input_num(value);
877 879
878 if (n < 0) { 880 if (n < 0) {
879 uw_set_error(ctx, "Bad subforms name %s", value); 881 uw_set_error(ctx, "Bad subforms name %s", uw_Basis_htmlifyString(ctx, value));
880 return -1; 882 return -1;
881 } 883 }
882 884
883 if (n >= ctx->app->inputs_len) { 885 if (n >= ctx->app->inputs_len) {
884 uw_set_error(ctx, "For subforms name %s, index %d is out of range", value, n); 886 uw_set_error(ctx, "For subforms name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, value), n);
885 return -1; 887 return -1;
886 } 888 }
887 889
888 INP(ctx)[n].kind = SUBFORMS; 890 INP(ctx)[n].kind = SUBFORMS;
889 INP(ctx)[n].data.subforms.parent = ctx->cur_container; 891 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
916 918
917 if (n < 0) 919 if (n < 0)
918 return 0; 920 return 0;
919 921
920 if (n >= ctx->app->inputs_len) { 922 if (n >= ctx->app->inputs_len) {
921 uw_set_error(ctx, "For input name %s, index %d is out of range", name, n); 923 uw_set_error(ctx, "For input name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, name), n);
922 return -1; 924 return -1;
923 } 925 }
924 926
925 INP(ctx)[n].kind = NORMAL; 927 INP(ctx)[n].kind = NORMAL;
926 INP(ctx)[n].data.normal = value; 928 INP(ctx)[n].data.normal = value;
979 981
980 int uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) { 982 int uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) {
981 int n = ctx->app->input_num(name); 983 int n = ctx->app->input_num(name);
982 984
983 if (n < 0) { 985 if (n < 0) {
984 uw_set_error(ctx, "Bad file input name %s", name); 986 uw_set_error(ctx, "Bad file input name %s", uw_Basis_htmlifyString(ctx, name));
985 return -1; 987 return -1;
986 } 988 }
987 989
988 if (n >= ctx->app->inputs_len) { 990 if (n >= ctx->app->inputs_len) {
989 uw_set_error(ctx, "For file input name %s, index %d is out of range", name, n); 991 uw_set_error(ctx, "For file input name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, name), n);
990 return -1; 992 return -1;
991 } 993 }
992 994
993 ctx->inputs[n].kind = FIL; 995 ctx->inputs[n].kind = FIL;
994 ctx->inputs[n].data.file = f; 996 ctx->inputs[n].data.file = f;
1860 if (s2[1] == 0) 1862 if (s2[1] == 0)
1861 uw_error(ctx, FATAL, "Missing first character of escaped URL byte"); 1863 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
1862 if (s2[2] == 0) 1864 if (s2[2] == 0)
1863 uw_error(ctx, FATAL, "Missing second character of escaped URL byte"); 1865 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
1864 if (sscanf(s2+1, "%02X", &n) != 1) 1866 if (sscanf(s2+1, "%02X", &n) != 1)
1865 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2); 1867 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", uw_Basis_htmlifyString(ctx, s2));
1866 *s1 = n; 1868 *s1 = n;
1867 s2 += 2; 1869 s2 += 2;
1868 break; 1870 break;
1869 case '.': 1871 case '.':
1870 if (!fromClient) { 1872 if (!fromClient) {
1871 if (s2[1] == 0) 1873 if (s2[1] == 0)
1872 uw_error(ctx, FATAL, "Missing first character of escaped URL byte"); 1874 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
1873 if (s2[2] == 0) 1875 if (s2[2] == 0)
1874 uw_error(ctx, FATAL, "Missing second character of escaped URL byte"); 1876 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
1875 if (sscanf(s2+1, "%02X", &n) != 1) 1877 if (sscanf(s2+1, "%02X", &n) != 1)
1876 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2); 1878 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", uw_Basis_htmlifyString(ctx, s2));
1877 *s1 = n; 1879 *s1 = n;
1878 s2 += 2; 1880 s2 += 2;
1879 break; 1881 break;
1880 } 1882 }
1881 default: 1883 default:
2016 ctx->page.front += len; 2018 ctx->page.front += len;
2017 2019
2018 return uw_unit_v; 2020 return uw_unit_v;
2019 } 2021 }
2020 2022
2021 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) { 2023 char *uw_Basis_htmlifyString(uw_context ctx, const char *s) {
2022 char *r, *s2; 2024 char *r, *s2;
2023 2025
2024 uw_check_heap(ctx, strlen(s) * 5 + 1); 2026 uw_check_heap(ctx, strlen(s) * 5 + 1);
2025 2027
2026 for (r = s2 = ctx->heap.front; *s; s++) { 2028 for (r = s2 = ctx->heap.front; *s; s++) {
2781 uw_Basis_int n = strtoll(s, &endptr, 10); 2783 uw_Basis_int n = strtoll(s, &endptr, 10);
2782 2784
2783 if (*s != '\0' && *endptr == '\0') 2785 if (*s != '\0' && *endptr == '\0')
2784 return n; 2786 return n;
2785 else 2787 else
2786 uw_error(ctx, FATAL, "Can't parse int: %s", s); 2788 uw_error(ctx, FATAL, "Can't parse int: %s", uw_Basis_htmlifyString(ctx, s));
2787 } 2789 }
2788 2790
2789 #include <errno.h> 2791 #include <errno.h>
2790 2792
2791 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) { 2793 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
2792 unsigned long long n; 2794 unsigned long long n;
2793 2795
2794 if (sscanf(s, "%llu", &n) < 1) 2796 if (sscanf(s, "%llu", &n) < 1)
2795 uw_error(ctx, FATAL, "Can't parse channel: %s", s); 2797 uw_error(ctx, FATAL, "Can't parse channel: %s", uw_Basis_htmlifyString(ctx, s));
2796 else { 2798 else {
2797 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)}; 2799 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
2798 return ch; 2800 return ch;
2799 } 2801 }
2800 } 2802 }
2804 unsigned long n = strtoul(s, &endptr, 10); 2806 unsigned long n = strtoul(s, &endptr, 10);
2805 2807
2806 if (*s != '\0' && *endptr == '\0') 2808 if (*s != '\0' && *endptr == '\0')
2807 return n; 2809 return n;
2808 else 2810 else
2809 uw_error(ctx, FATAL, "Can't parse client: %s", s); 2811 uw_error(ctx, FATAL, "Can't parse client: %s", uw_Basis_htmlifyString(ctx, s));
2810 } 2812 }
2811 2813
2812 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) { 2814 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
2813 char *endptr; 2815 char *endptr;
2814 uw_Basis_float n = strtod(s, &endptr); 2816 uw_Basis_float n = strtod(s, &endptr);
2815 2817
2816 if (*s != '\0' && *endptr == '\0') 2818 if (*s != '\0' && *endptr == '\0')
2817 return n; 2819 return n;
2818 else 2820 else
2819 uw_error(ctx, FATAL, "Can't parse float: %s", s); 2821 uw_error(ctx, FATAL, "Can't parse float: %s", uw_Basis_htmlifyString(ctx, s));
2820 } 2822 }
2821 2823
2822 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) { 2824 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
2823 if (s[0] == 0) 2825 if (s[0] == 0)
2824 return 0; 2826 return 0;
2825 else if (s[1] != 0) 2827 else if (s[1] != 0)
2826 uw_error(ctx, FATAL, "Can't parse char: %s", s); 2828 uw_error(ctx, FATAL, "Can't parse char: %s", uw_Basis_htmlifyString(ctx, s));
2827 else 2829 else
2828 return s[0]; 2830 return s[0];
2829 } 2831 }
2830 2832
2831 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) { 2833 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
2832 if (!strcasecmp(s, "T") || !strcasecmp (s, "True")) 2834 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
2833 return uw_Basis_True; 2835 return uw_Basis_True;
2834 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False")) 2836 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
2835 return uw_Basis_False; 2837 return uw_Basis_False;
2836 else 2838 else
2837 uw_error(ctx, FATAL, "Can't parse bool: %s", s); 2839 uw_error(ctx, FATAL, "Can't parse bool: %s", uw_Basis_htmlifyString(ctx, s));
2838 } 2840 }
2839 2841
2840 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) { 2842 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
2841 char *dot = strchr(s, '.'), *end = strchr(s, 0); 2843 char *dot = strchr(s, '.'), *end = strchr(s, 0);
2842 struct tm stm = {}; 2844 struct tm stm = {};
2852 uw_Basis_time r = { mktime(&stm), atoi(usec) }; 2854 uw_Basis_time r = { mktime(&stm), atoi(usec) };
2853 return r; 2855 return r;
2854 } 2856 }
2855 else { 2857 else {
2856 *dot = '.'; 2858 *dot = '.';
2857 uw_error(ctx, FATAL, "Can't parse time: %s", s); 2859 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
2858 } 2860 }
2859 } 2861 }
2860 else { 2862 else {
2861 if (strptime(s, TIME_FMT_PG, &stm) == end) { 2863 if (strptime(s, TIME_FMT_PG, &stm) == end) {
2862 uw_Basis_time r = { mktime(&stm) }; 2864 uw_Basis_time r = { mktime(&stm) };
2863 return r; 2865 return r;
2864 } else if (strptime(s, TIME_FMT, &stm) == end) { 2866 } else if (strptime(s, TIME_FMT, &stm) == end) {
2865 uw_Basis_time r = { mktime(&stm) }; 2867 uw_Basis_time r = { mktime(&stm) };
2866 return r; 2868 return r;
2867 } else 2869 } else
2868 uw_error(ctx, FATAL, "Can't parse time: %s", s); 2870 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
2869 } 2871 }
2870 } 2872 }
2871 2873
2872 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) { 2874 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
2873 char *dot = strchr(s, '.'), *end = strchr(s, 0); 2875 char *dot = strchr(s, '.'), *end = strchr(s, 0);
2883 return r; 2885 return r;
2884 } 2886 }
2885 } 2887 }
2886 else { 2888 else {
2887 *dot = '.'; 2889 *dot = '.';
2888 uw_error(ctx, FATAL, "Can't parse time: %s", s); 2890 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
2889 } 2891 }
2890 } 2892 }
2891 else { 2893 else {
2892 if (strptime(s, TIME_FMT_PG, &stm) == end) { 2894 if (strptime(s, TIME_FMT_PG, &stm) == end) {
2893 uw_Basis_time r = { mktime(&stm) }; 2895 uw_Basis_time r = { mktime(&stm) };
2894 return r; 2896 return r;
2895 } else if (strptime(s, TIME_FMT, &stm) == end) { 2897 } else if (strptime(s, TIME_FMT, &stm) == end) {
2896 uw_Basis_time r = { mktime(&stm) }; 2898 uw_Basis_time r = { mktime(&stm) };
2897 return r; 2899 return r;
2898 } else 2900 } else
2899 uw_error(ctx, FATAL, "Can't parse time: %s", s); 2901 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
2900 } 2902 }
2901 } 2903 }
2902 2904
2903 uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) { 2905 uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) {
2904 char *end = strchr(s, 0); 2906 char *end = strchr(s, 0);
2907 2909
2908 if (strptime(s, fmt, &stm) == end) { 2910 if (strptime(s, fmt, &stm) == end) {
2909 uw_Basis_time r = { mktime(&stm) }; 2911 uw_Basis_time r = { mktime(&stm) };
2910 return r; 2912 return r;
2911 } else 2913 } else
2912 uw_error(ctx, FATAL, "Can't parse time: %s", s); 2914 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
2913 } 2915 }
2914 2916
2915 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) { 2917 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
2916 char *r = ctx->heap.front; 2918 char *r = ctx->heap.front;
2917 uw_Basis_blob b = {len, r}; 2919 uw_Basis_blob b = {len, r};