Changeset 436

Show
Ignore:
Timestamp:
10/26/06 15:27:48 (2 years ago)
Author:
alo
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cherokee/trunk/ChangeLog

    r435 r436  
     12006-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 
    1172006-10-24  A.D.F  <adefacc@tin.it> 
    218 
  • cherokee/trunk/cherokee/buffer.c

    r435 r436  
    4545 
    4646ret_t 
    47 cherokee_buffer_new (cherokee_buffer_t **buf) 
     47cherokee_buffer_new (cherokee_buffer_t **buf) 
    4848{ 
    4949        CHEROKEE_NEW_STRUCT(n, buffer); 
     
    9494cherokee_buffer_clean (cherokee_buffer_t *buf) 
    9595{ 
    96         if ((buf->buf != NULL) && 
    97             (buf->len > 0)) 
     96        if (likely ((buf->buf != NULL) && (buf->len != 0)) ) 
    9897        { 
    9998                buf->buf[0] = '\0'; 
    10099        } 
    101  
    102         buf->len = 0;    
     100        buf->len = 0; 
    103101        return ret_ok; 
    104102} 
     
    130128 
    131129        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        } 
    133134 
    134135        memcpy (n->buf, buf->buf, buf->len + 1); 
     
    352353 
    353354/* 
    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. 
    357357 */ 
    358358ret_t 
    359 cherokee_buffer_decode (cherokee_buffer_t *buffer) 
     359cherokee_buffer_unescape_uri (cherokee_buffer_t *buffer) 
    360360{ 
    361361        static const char hex2dec_tab[256] = { 
     
    738738 
    739739        static const char 
    740                 b64_decode_table[256] = { 
     740                b64_decode_tab[256] = { 
    741741                        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */ 
    742742                        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */ 
     
    758758 
    759759        for (i=0; i < buf->len; i++) { 
    760                 d = b64_decode_table[(int) buf->buf[i]]; 
     760                d = b64_decode_tab[(int) buf->buf[i]]; 
    761761                if (d != -1) { 
    762762                        switch (phase) { 
     
    785785                        space_idx = 0; 
    786786                } 
    787        
     787       
    788788 
    789789        space[space_idx]='\0'; 
     
    819819        out = (cuchar_t *) new_buf.buf; 
    820820 
    821         for (i=0, j=0; i < inlen; i += 3) { 
     821       for (i=0, j=0; i < inlen; i += 3) { 
    822822                int     a=0,b=0,c=0; 
    823823                int     d, e, f, g; 
    824824 
    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        } 
    843842 
    844843        out[j]  = '\0'; 
     
    864863        cuint_t j; 
    865864        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; 
    866877 
    867878        /* Count extra characters 
    868879         */ 
    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; 
    874894                } 
    875895        } 
    876896 
    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; 
    878901 
    879902        /* Create a new buffer 
     
    892915        /* Make the changes 
    893916         */ 
    894         for (i=0, j=0; i<buf->len; i++) { 
     917        for (i = 0, j = 0; i < buf->len; i++) { 
    895918                char c = buf->buf[i+j];  
    896919 
     
    912935                        memcpy (&buf->buf[i+j], "&amp;", 5); 
    913936                        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], "&quot;", 6); 
     942                        j += 5; 
    914943                        break; 
    915944                } 
     
    11641193                                char *replacement, int replacement_length) 
    11651194{ 
    1166         int         remaining_length; 
     1195       int         remaining_length; 
    11671196        int         result_length; 
    1168         char       *result; 
     1197       char       *result; 
    11691198        char       *result_position; 
    1170         const char *p; 
     1199       const char *p; 
    11711200        const char *substring_position; 
    11721201 
     
    11741203         */ 
    11751204        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       }  
    11841213 
    11851214        /* Take the new memory chunk 
     
    11921221        result_position = result; 
    11931222 
    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        }        
    12081238        *result_position = '\0'; 
    12091239 
     
    12401270                p = buf->buf + off; 
    12411271        } 
    1242          
     1272 
    12431273        for (i=0; i<num; i++) { 
    12441274                int len = (buf->buf + buf->len) - p; 
     
    12911321cherokee_buffer_cnt_spn (cherokee_buffer_t *buf, int offset, char *str)  
    12921322{ 
    1293         if ((buf->buf == NULL) || 
    1294             (buf->len <= offset)) 
     1323        if (unlikely ((buf->buf == NULL) || (buf->len <= offset))) 
    12951324                return 0; 
    1296          
     1325 
    12971326        return strspn (buf->buf + offset, str); 
    12981327} 
     
    13021331cherokee_buffer_cnt_cspn (cherokee_buffer_t *buf, int offset, char *str)  
    13031332{ 
    1304         if ((buf->buf == NULL) || 
    1305             (buf->len <= offset)) 
     1333        if (unlikely ((buf->buf == NULL) || (buf->len <= offset))) 
    13061334                return 0; 
    13071335 
    13081336        return strcspn (buf->buf + offset, str); 
    13091337} 
     1338 
  • cherokee/trunk/cherokee/buffer.h

    r417 r436  
    104104 
    105105crc_t cherokee_buffer_crc32              (cherokee_buffer_t  *buf); 
    106 ret_t cherokee_buffer_decode             (cherokee_buffer_t  *buf); 
     106ret_t cherokee_buffer_unescape_uri       (cherokee_buffer_t  *buf); 
    107107ret_t cherokee_buffer_encode_base64      (cherokee_buffer_t  *buf); 
    108108ret_t cherokee_buffer_decode_base64      (cherokee_buffer_t  *buf); 
  • cherokee/trunk/cherokee/handler_error.c

    r424 r436  
    112112                if (escaped == NULL) 
    113113                        cherokee_buffer_add_va (buffer, "<p><pre>%s</pre>", cnt->header.input_buffer->buf); 
    114                 else 
     114                else { 
    115115                        cherokee_buffer_add_va (buffer, "<p><pre>%s</pre>", escaped->buf); 
     116                        cherokee_buffer_free (escaped); 
     117                        escaped = NULL; 
     118                } 
    116119                break; 
    117120        case http_access_denied: 
  • cherokee/trunk/cherokee/header.c

    r405 r436  
    780780        if (unlikely(ret < ret_ok)) return ret; 
    781781 
    782         return cherokee_buffer_decode (request); 
     782        return cherokee_buffer_unescape_uri (request); 
    783783} 
    784784 
     
    832832        if (unlikely(ret < ret_ok)) return ret; 
    833833 
    834         return cherokee_buffer_decode (request); 
     834        return cherokee_buffer_unescape_uri (request); 
    835835} 
    836836