Changeset 436
- Timestamp:
- 10/26/06 15:27:48 (2 years ago)
- Files:
-
- cherokee/trunk/ChangeLog (modified) (1 diff)
- cherokee/trunk/cherokee/buffer.c (modified) (17 diffs)
- cherokee/trunk/cherokee/buffer.h (modified) (1 diff)
- cherokee/trunk/cherokee/handler_error.c (modified) (1 diff)
- cherokee/trunk/cherokee/header.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cherokee/trunk/ChangeLog
r435 r436 1 2006-10-26 A.D.F <adefacc@tin.it> 2 3 * cherokee/buffer.h, cherokee/buffer.c: sparse blanks to tab 4 conversions; cherokee_buffer_dup(), add a free() to fix a possible 5 memory leak; rename cherokee_buffer_decode() to 6 cherokee_buffer_unescape_uri() (unescape escape sequences) in 7 order to make it consistent with cherokee_buffer_escape_html() and 8 future cherokee_buffer_escape_uri(); 9 10 * cherokee/buffer.c (cherokee_buffer_escape_html): - improve speed 11 by using strpbrk() (fast function) to look for special characters; 12 add '"' to the escaped characters; make sure there are no embedded 13 '\0' in input string. 14 15 * cherokee/handler_error.c: fixes a memor leak. 16 1 17 2006-10-24 A.D.F <adefacc@tin.it> 2 18 cherokee/trunk/cherokee/buffer.c
r435 r436 45 45 46 46 ret_t 47 cherokee_buffer_new (cherokee_buffer_t **buf)47 cherokee_buffer_new (cherokee_buffer_t **buf) 48 48 { 49 49 CHEROKEE_NEW_STRUCT(n, buffer); … … 94 94 cherokee_buffer_clean (cherokee_buffer_t *buf) 95 95 { 96 if ((buf->buf != NULL) && 97 (buf->len > 0)) 96 if (likely ((buf->buf != NULL) && (buf->len != 0)) ) 98 97 { 99 98 buf->buf[0] = '\0'; 100 99 } 101 102 buf->len = 0; 100 buf->len = 0; 103 101 return ret_ok; 104 102 } … … 130 128 131 129 n->buf = (char *) malloc(buf->len + 1); 132 if (unlikely (n->buf == NULL)) return ret_nomem; 130 if (unlikely (n->buf == NULL)) { 131 free(n); 132 return ret_nomem; 133 } 133 134 134 135 memcpy (n->buf, buf->buf, buf->len + 1); … … 352 353 353 354 /* 354 * Decode a string that may have encoded characters %xx 355 * where xx is the hexadecimal number corresponding 356 * to the character ascii value. 355 * Unescape a string that may have escaped characters %xx 356 * where xx is the hexadecimal number equal to the character ascii value. 357 357 */ 358 358 ret_t 359 cherokee_buffer_ decode(cherokee_buffer_t *buffer)359 cherokee_buffer_unescape_uri (cherokee_buffer_t *buffer) 360 360 { 361 361 static const char hex2dec_tab[256] = { … … 738 738 739 739 static const char 740 b64_decode_tab le[256] = {740 b64_decode_tab[256] = { 741 741 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */ 742 742 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */ … … 758 758 759 759 for (i=0; i < buf->len; i++) { 760 d = b64_decode_tab le[(int) buf->buf[i]];760 d = b64_decode_tab[(int) buf->buf[i]]; 761 761 if (d != -1) { 762 762 switch (phase) { … … 785 785 space_idx = 0; 786 786 } 787 }787 } 788 788 789 789 space[space_idx]='\0'; … … 819 819 out = (cuchar_t *) new_buf.buf; 820 820 821 for (i=0, j=0; i < inlen; i += 3) {821 for (i=0, j=0; i < inlen; i += 3) { 822 822 int a=0,b=0,c=0; 823 823 int d, e, f, g; 824 824 825 a=in[i]; 826 b= i+1 < inlen ? in[i+1]:0; 827 c= i+2 < inlen ? in[i+2]:0; 828 829 d = base64tab [a >> 2 ]; 830 e = base64tab [((a & 3 ) << 4) | (b >> 4)]; 831 f = base64tab [((b & 15) << 2) | (c >> 6)]; 832 g = base64tab [c & 63 ]; 833 834 if (i + 1 >= inlen) f='='; 835 if (i + 2 >= inlen) g='='; 836 837 out[j++] = d; 838 out[j++] = e; 839 out[j++] = f; 840 out[j++] = g; 841 } 842 825 a=in[i]; 826 b= i+1 < inlen ? in[i+1]:0; 827 c= i+2 < inlen ? in[i+2]:0; 828 829 d = base64tab [a >> 2 ]; 830 e = base64tab [((a & 3 ) << 4) | (b >> 4)]; 831 f = base64tab [((b & 15) << 2) | (c >> 6)]; 832 g = base64tab [c & 63 ]; 833 834 if (i + 1 >= inlen) f='='; 835 if (i + 2 >= inlen) g='='; 836 837 out[j++] = d; 838 out[j++] = e; 839 out[j++] = f; 840 out[j++] = g; 841 } 843 842 844 843 out[j] = '\0'; … … 864 863 cuint_t j; 865 864 cuint_t extra = 0; 865 char *p0, *p; 866 867 /* Verify string termination, 868 * we assume there are no '\0' inside buffer. 869 */ 870 if (buf->buf[buf->len] != '\0') 871 buf->buf[buf->len] = '\0'; 872 873 /* Verify if string has to be escaped. 874 */ 875 if ((p0 = strpbrk(buf->buf, "<>&\"")) == NULL) 876 return ret_not_found; 866 877 867 878 /* Count extra characters 868 879 */ 869 for (i=0; i<buf->len; i++) { 870 if ((buf->buf[i] == '<') || (buf->buf[i] == '>')) { 871 extra += 3; 872 } else if (buf->buf[i] == '&') { 873 extra += 4; 880 for (p = p0; *p != '\0'; ++p) { 881 switch(*p) { 882 case '<': 883 case '>': 884 extra += 3; 885 continue; 886 case '&': 887 extra += 4; 888 continue; 889 case '"': 890 extra += 5; 891 continue; 892 default: 893 continue; 874 894 } 875 895 } 876 896 877 if (extra == 0) return ret_not_found; 897 /* Verify there are no embedded '\0'. 898 */ 899 if ( ((int) (p - buf->buf)) != buf->len) 900 return ret_error; 878 901 879 902 /* Create a new buffer … … 892 915 /* Make the changes 893 916 */ 894 for (i =0, j=0; i<buf->len; i++) {917 for (i = 0, j = 0; i < buf->len; i++) { 895 918 char c = buf->buf[i+j]; 896 919 … … 912 935 memcpy (&buf->buf[i+j], "&", 5); 913 936 j += 4; 937 break; 938 939 case '"': 940 memmove (&buf->buf[i+j+6], &buf->buf[i+j+1], buf->len-i); 941 memcpy (&buf->buf[i+j], """, 6); 942 j += 5; 914 943 break; 915 944 } … … 1164 1193 char *replacement, int replacement_length) 1165 1194 { 1166 int remaining_length;1195 int remaining_length; 1167 1196 int result_length; 1168 char *result;1197 char *result; 1169 1198 char *result_position; 1170 const char *p;1199 const char *p; 1171 1200 const char *substring_position; 1172 1201 … … 1174 1203 */ 1175 1204 result_length = buf->len; 1176 for (p = buf->buf; ; p = substring_position + substring_length) {1177 substring_position = strstr (p, substring);1178 if (substring_position == NULL) { 1179 break; 1180 } 1181 1182 result_length += (replacement_length - substring_length);1183 }1205 for (p = buf->buf; ; p = substring_position + substring_length) { 1206 substring_position = strstr (p, substring); 1207 1208 if (substring_position == NULL) 1209 break; 1210 1211 result_length += (replacement_length - substring_length); 1212 } 1184 1213 1185 1214 /* Take the new memory chunk … … 1192 1221 result_position = result; 1193 1222 1194 for (p = buf->buf; ; p = substring_position + substring_length) { 1195 substring_position = strstr (p, substring); 1196 if (substring_position == NULL) { 1197 remaining_length = strlen (p); 1198 memcpy (result_position, p, remaining_length); 1199 result_position += remaining_length; 1200 break; 1201 } 1202 memcpy (result_position, p, substring_position - p); 1203 result_position += (substring_position - p); 1204 1205 memcpy (result_position, replacement, replacement_length); 1206 result_position += replacement_length; 1207 } 1223 for (p = buf->buf; ; p = substring_position + substring_length) { 1224 substring_position = strstr (p, substring); 1225 1226 if (substring_position == NULL) { 1227 remaining_length = strlen (p); 1228 memcpy (result_position, p, remaining_length); 1229 result_position += remaining_length; 1230 break; 1231 } 1232 memcpy (result_position, p, substring_position - p); 1233 result_position += (substring_position - p); 1234 1235 memcpy (result_position, replacement, replacement_length); 1236 result_position += replacement_length; 1237 } 1208 1238 *result_position = '\0'; 1209 1239 … … 1240 1270 p = buf->buf + off; 1241 1271 } 1242 1272 1243 1273 for (i=0; i<num; i++) { 1244 1274 int len = (buf->buf + buf->len) - p; … … 1291 1321 cherokee_buffer_cnt_spn (cherokee_buffer_t *buf, int offset, char *str) 1292 1322 { 1293 if ((buf->buf == NULL) || 1294 (buf->len <= offset)) 1323 if (unlikely ((buf->buf == NULL) || (buf->len <= offset))) 1295 1324 return 0; 1296 1325 1297 1326 return strspn (buf->buf + offset, str); 1298 1327 } … … 1302 1331 cherokee_buffer_cnt_cspn (cherokee_buffer_t *buf, int offset, char *str) 1303 1332 { 1304 if ((buf->buf == NULL) || 1305 (buf->len <= offset)) 1333 if (unlikely ((buf->buf == NULL) || (buf->len <= offset))) 1306 1334 return 0; 1307 1335 1308 1336 return strcspn (buf->buf + offset, str); 1309 1337 } 1338 cherokee/trunk/cherokee/buffer.h
r417 r436 104 104 105 105 crc_t cherokee_buffer_crc32 (cherokee_buffer_t *buf); 106 ret_t cherokee_buffer_ decode(cherokee_buffer_t *buf);106 ret_t cherokee_buffer_unescape_uri (cherokee_buffer_t *buf); 107 107 ret_t cherokee_buffer_encode_base64 (cherokee_buffer_t *buf); 108 108 ret_t cherokee_buffer_decode_base64 (cherokee_buffer_t *buf); cherokee/trunk/cherokee/handler_error.c
r424 r436 112 112 if (escaped == NULL) 113 113 cherokee_buffer_add_va (buffer, "<p><pre>%s</pre>", cnt->header.input_buffer->buf); 114 else 114 else { 115 115 cherokee_buffer_add_va (buffer, "<p><pre>%s</pre>", escaped->buf); 116 cherokee_buffer_free (escaped); 117 escaped = NULL; 118 } 116 119 break; 117 120 case http_access_denied: cherokee/trunk/cherokee/header.c
r405 r436 780 780 if (unlikely(ret < ret_ok)) return ret; 781 781 782 return cherokee_buffer_ decode(request);782 return cherokee_buffer_unescape_uri (request); 783 783 } 784 784 … … 832 832 if (unlikely(ret < ret_ok)) return ret; 833 833 834 return cherokee_buffer_ decode(request);834 return cherokee_buffer_unescape_uri (request); 835 835 } 836 836