------------------------------------------------------------------------- Encoding::escapify_url_common This routine will escapify a URL to remove spaces (and perhaps other ugly characters) from a URL and replace them with a hex escape sequence. Since the escapes are larger (multi-byte) than the characters being replaced, the string returned will be longer than the string passed. Thi
| 43 | double URL encoding (escapify_url) or not (pure_escapify_url) |
| 44 | -------------------------------------------------------------------------*/ |
| 45 | char * |
| 46 | escapify_url_common(Arena *arena, char *url, size_t len_in, int *len_out, char *dst, size_t dst_size, const unsigned char *map, |
| 47 | bool pure_escape) |
| 48 | { |
| 49 | // codes_to_escape is a bitmap encoding the codes that should be escaped. |
| 50 | // These are all the codes defined in section 2.4.3 of RFC 2396 |
| 51 | // (control, space, delims, and unwise) plus the tilde. In RFC 2396 |
| 52 | // the tilde is an "unreserved" character, but we escape it because |
| 53 | // historically this is what the traffic_server has done. |
| 54 | // Note that we leave codes beyond 127 unmodified. |
| 55 | // |
| 56 | // NOTE: any updates to this table should result in an update to: |
| 57 | // tools/escape_mapper/escape_mapper.cc. |
| 58 | static const unsigned char codes_to_escape[32] = { |
| 59 | 0xFF, 0xFF, 0xFF, |
| 60 | 0xFF, // control |
| 61 | 0xB4, // space " # % |
| 62 | 0x00, 0x00, // |
| 63 | 0x0A, // < > |
| 64 | 0x00, 0x00, 0x00, // |
| 65 | 0x1E, 0x80, // [ \ ] ^ ` |
| 66 | 0x00, 0x00, // |
| 67 | 0x1F, // { | } ~ DEL |
| 68 | 0x00, 0x00, 0x00, |
| 69 | 0x00, // all non-ascii characters unmodified |
| 70 | 0x00, 0x00, 0x00, |
| 71 | 0x00, // . |
| 72 | 0x00, 0x00, 0x00, |
| 73 | 0x00, // . |
| 74 | 0x00, 0x00, 0x00, |
| 75 | 0x00 // . |
| 76 | }; |
| 77 | |
| 78 | static char hex_digit[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
| 79 | |
| 80 | if (!url || (dst && dst_size < len_in)) { |
| 81 | *len_out = 0; |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | if (!map) { |
| 86 | map = codes_to_escape; |
| 87 | } |
| 88 | |
| 89 | // Count specials in the url, assuming that there won't be any. |
| 90 | // |
| 91 | int count = 0; |
| 92 | char *p = url; |
| 93 | char *in_url_end = url + len_in; |
| 94 | |
| 95 | while (p < in_url_end) { |
| 96 | unsigned char c = *p; |
| 97 | if (map[c / 8] & (1 << (7 - c % 8))) { |
| 98 | ++count; |
| 99 | } |
| 100 | ++p; |
| 101 | } |
| 102 |
no test coverage detected