]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
2b6662ba | 2 | * $Id: rfc1738.c,v 1.22 2001/01/12 00:37:13 wessels Exp $ |
30a4f2a8 | 3 | * |
4 | * DEBUG: | |
5 | * AUTHOR: Harvest Derived | |
6 | * | |
2b6662ba | 7 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 8 | * ---------------------------------------------------------- |
30a4f2a8 | 9 | * |
2b6662ba | 10 | * Squid is the result of efforts by numerous individuals from |
11 | * the Internet community; see the CONTRIBUTORS file for full | |
12 | * details. Many organizations have provided support for Squid's | |
13 | * development; see the SPONSORS file for full details. Squid is | |
14 | * Copyrighted (C) 2001 by the Regents of the University of | |
15 | * California; see the COPYRIGHT file for full details. Squid | |
16 | * incorporates software developed and/or copyrighted by other | |
17 | * sources; see the CREDITS file for full details. | |
30a4f2a8 | 18 | * |
19 | * This program is free software; you can redistribute it and/or modify | |
20 | * it under the terms of the GNU General Public License as published by | |
21 | * the Free Software Foundation; either version 2 of the License, or | |
22 | * (at your option) any later version. | |
23 | * | |
24 | * This program is distributed in the hope that it will be useful, | |
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | * GNU General Public License for more details. | |
28 | * | |
29 | * You should have received a copy of the GNU General Public License | |
30 | * along with this program; if not, write to the Free Software | |
cbdec147 | 31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 32 | * |
30a4f2a8 | 33 | */ |
34 | ||
35 | #include "config.h" | |
0d94e9fe | 36 | |
30a4f2a8 | 37 | #if HAVE_STDIO_H |
090089c4 | 38 | #include <stdio.h> |
30a4f2a8 | 39 | #endif |
40 | #if HAVE_STRING_H | |
090089c4 | 41 | #include <string.h> |
30a4f2a8 | 42 | #endif |
43 | ||
090089c4 | 44 | #include "util.h" |
5e2e108b | 45 | #include "snprintf.h" |
090089c4 | 46 | |
47 | /* | |
48 | * RFC 1738 defines that these characters should be escaped, as well | |
49 | * any non-US-ASCII character or anything between 0x00 - 0x1F. | |
50 | */ | |
30a4f2a8 | 51 | static char rfc1738_unsafe_chars[] = |
090089c4 | 52 | { |
53 | (char) 0x3C, /* < */ | |
54 | (char) 0x3E, /* > */ | |
55 | (char) 0x22, /* " */ | |
56 | (char) 0x23, /* # */ | |
9bc73deb | 57 | #if 0 /* done in code */ |
090089c4 | 58 | (char) 0x25, /* % */ |
9bc73deb | 59 | #endif |
090089c4 | 60 | (char) 0x7B, /* { */ |
61 | (char) 0x7D, /* } */ | |
62 | (char) 0x7C, /* | */ | |
63 | (char) 0x5C, /* \ */ | |
64 | (char) 0x5E, /* ^ */ | |
65 | (char) 0x7E, /* ~ */ | |
66 | (char) 0x5B, /* [ */ | |
67 | (char) 0x5D, /* ] */ | |
68 | (char) 0x60, /* ` */ | |
69 | (char) 0x27, /* ' */ | |
70 | (char) 0x20 /* space */ | |
71 | }; | |
72 | ||
9bc73deb | 73 | static char rfc1738_reserved_chars[] = |
74 | { | |
75 | (char) 0x3b, /* ; */ | |
76 | (char) 0x2f, /* / */ | |
77 | (char) 0x3f, /* ? */ | |
78 | (char) 0x3a, /* : */ | |
79 | (char) 0x40, /* @ */ | |
80 | (char) 0x3d, /* = */ | |
81 | (char) 0x26 /* & */ | |
82 | }; | |
83 | ||
090089c4 | 84 | /* |
85 | * rfc1738_escape - Returns a static buffer contains the RFC 1738 | |
86 | * compliant, escaped version of the given url. | |
87 | */ | |
9bc73deb | 88 | static char * |
89 | rfc1738_do_escape(const char *url, int encode_reserved) | |
090089c4 | 90 | { |
3fdadc70 | 91 | static char *buf; |
92 | static size_t bufsize = 0; | |
0ee4272b | 93 | const char *p; |
94 | char *q; | |
090089c4 | 95 | int i, do_escape; |
96 | ||
3fdadc70 | 97 | if (buf == NULL || strlen(url) * 3 > bufsize) { |
ccf2bc25 | 98 | xfree(buf); |
3fdadc70 | 99 | bufsize = strlen(url) * 3 + 1; |
ad8bc8eb | 100 | buf = xcalloc(bufsize, 1); |
3fdadc70 | 101 | } |
3fdadc70 | 102 | for (p = url, q = buf; *p != '\0'; p++, q++) { |
090089c4 | 103 | do_escape = 0; |
104 | ||
105 | /* RFC 1738 defines these chars as unsafe */ | |
106 | for (i = 0; i < sizeof(rfc1738_unsafe_chars); i++) { | |
107 | if (*p == rfc1738_unsafe_chars[i]) { | |
108 | do_escape = 1; | |
109 | break; | |
110 | } | |
111 | } | |
9bc73deb | 112 | /* Handle % separately */ |
113 | if (encode_reserved >= 0 && *p == '%') | |
114 | do_escape = 1; | |
115 | /* RFC 1738 defines these chars as reserved */ | |
116 | for (i = 0; i < sizeof(rfc1738_reserved_chars) && encode_reserved > 0; i++) { | |
117 | if (*p == rfc1738_reserved_chars[i]) { | |
118 | do_escape = 1; | |
119 | break; | |
120 | } | |
121 | } | |
090089c4 | 122 | /* RFC 1738 says any control chars (0x00-0x1F) are encoded */ |
86ee2017 | 123 | if ((unsigned char) *p <= (unsigned char) 0x1F) { |
090089c4 | 124 | do_escape = 1; |
125 | } | |
126 | /* RFC 1738 says 0x7f is encoded */ | |
127 | if (*p == (char) 0x7F) { | |
128 | do_escape = 1; | |
129 | } | |
130 | /* RFC 1738 says any non-US-ASCII are encoded */ | |
86ee2017 | 131 | if (((unsigned char) *p >= (unsigned char) 0x80) && |
132 | ((unsigned char) *p <= (unsigned char) 0xFF)) { | |
090089c4 | 133 | do_escape = 1; |
134 | } | |
135 | /* Do the triplet encoding, or just copy the char */ | |
93fc29fe | 136 | /* note: we do not need snprintf here as q is appropriately |
0e473d70 | 137 | * allocated - KA */ |
93fc29fe | 138 | |
090089c4 | 139 | if (do_escape == 1) { |
140 | (void) sprintf(q, "%%%02x", (unsigned char) *p); | |
141 | q += sizeof(char) * 2; | |
142 | } else { | |
143 | *q = *p; | |
144 | } | |
145 | } | |
146 | *q = '\0'; | |
147 | return (buf); | |
148 | } | |
149 | ||
9bc73deb | 150 | /* |
151 | * rfc1738_escape - Returns a static buffer that contains the RFC | |
152 | * 1738 compliant, escaped version of the given url. | |
153 | */ | |
154 | char * | |
155 | rfc1738_escape(const char *url) | |
156 | { | |
157 | return rfc1738_do_escape(url, 0); | |
158 | } | |
159 | ||
160 | /* | |
161 | * rfc1738_escape_unescaped - Returns a static buffer that contains | |
162 | * the RFC 1738 compliant, escaped version of the given url. | |
163 | */ | |
164 | char * | |
165 | rfc1738_escape_unescaped(const char *url) | |
166 | { | |
167 | return rfc1738_do_escape(url, -1); | |
168 | } | |
169 | ||
170 | /* | |
171 | * rfc1738_escape_part - Returns a static buffer that contains the | |
172 | * RFC 1738 compliant, escaped version of the given url segment. | |
173 | */ | |
174 | char * | |
175 | rfc1738_escape_part(const char *url) | |
176 | { | |
177 | return rfc1738_do_escape(url, 1); | |
178 | } | |
179 | ||
090089c4 | 180 | /* |
181 | * rfc1738_unescape() - Converts escaped characters (%xy numbers) in | |
182 | * given the string. %% is a %. %ab is the 8-bit hexadecimal number "ab" | |
183 | */ | |
b8d8561b | 184 | void |
185 | rfc1738_unescape(char *s) | |
090089c4 | 186 | { |
187 | char hexnum[3]; | |
188 | int i, j; /* i is write, j is read */ | |
189 | unsigned int x; | |
090089c4 | 190 | for (i = j = 0; s[j]; i++, j++) { |
191 | s[i] = s[j]; | |
a8b7f666 | 192 | if (s[i] != '%') |
193 | continue; | |
461eef68 | 194 | if (s[j + 1] == '%') { /* %% case */ |
a8b7f666 | 195 | j++; |
196 | continue; | |
197 | } | |
461eef68 | 198 | if (s[j + 1] && s[j + 2]) { |
199 | hexnum[0] = s[j + 1]; | |
200 | hexnum[1] = s[j + 2]; | |
a8b7f666 | 201 | hexnum[2] = '\0'; |
202 | if (1 == sscanf(hexnum, "%x", &x)) { | |
461eef68 | 203 | s[i] = (char) (0x0ff & x); |
a8b7f666 | 204 | j += 2; |
090089c4 | 205 | } |
206 | } | |
207 | } | |
208 | s[i] = '\0'; | |
209 | } |