]>
Commit | Line | Data |
---|---|---|
bb849dbd RC |
1 | /* |
2 | * $Id$ | |
3 | * | |
4 | * DEBUG: | |
5 | * AUTHOR: Harvest Derived | |
6 | * | |
7 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
8 | * ---------------------------------------------------------- | |
9 | * | |
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. | |
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 | |
31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
32 | * | |
33 | */ | |
34 | ||
35 | #include <stdlib.h> | |
36 | #include <stdio.h> | |
37 | #include <string.h> | |
38 | ||
39 | #include "rfc1738.h" | |
40 | ||
41 | /* | |
42 | * RFC 1738 defines that these characters should be escaped, as well | |
43 | * any non-US-ASCII character or anything between 0x00 - 0x1F. | |
44 | */ | |
45 | static char rfc1738_unsafe_chars[] = { | |
46 | (char) 0x3C, /* < */ | |
47 | (char) 0x3E, /* > */ | |
48 | (char) 0x22, /* " */ | |
49 | (char) 0x23, /* # */ | |
50 | #if 0 /* done in code */ | |
51 | (char) 0x25, /* % */ | |
52 | #endif | |
53 | (char) 0x7B, /* { */ | |
54 | (char) 0x7D, /* } */ | |
55 | (char) 0x7C, /* | */ | |
56 | (char) 0x5C, /* \ */ | |
57 | (char) 0x5E, /* ^ */ | |
58 | (char) 0x7E, /* ~ */ | |
59 | (char) 0x5B, /* [ */ | |
60 | (char) 0x5D, /* ] */ | |
61 | (char) 0x60, /* ` */ | |
62 | (char) 0x27, /* ' */ | |
63 | (char) 0x20 /* space */ | |
64 | }; | |
65 | ||
66 | static char rfc1738_reserved_chars[] = { | |
67 | (char) 0x3b, /* ; */ | |
68 | (char) 0x2f, /* / */ | |
69 | (char) 0x3f, /* ? */ | |
70 | (char) 0x3a, /* : */ | |
71 | (char) 0x40, /* @ */ | |
72 | (char) 0x3d, /* = */ | |
73 | (char) 0x26 /* & */ | |
74 | }; | |
75 | ||
76 | /* | |
77 | * rfc1738_escape - Returns a static buffer contains the RFC 1738 | |
78 | * compliant, escaped version of the given url. | |
79 | */ | |
80 | static char * | |
81 | rfc1738_do_escape (const char *url, int encode_reserved) | |
82 | { | |
83 | static char *buf; | |
84 | static size_t bufsize = 0; | |
85 | const char *p; | |
86 | char *q; | |
87 | unsigned int i, do_escape; | |
88 | ||
89 | if (buf == NULL || strlen (url) * 3 > bufsize) | |
90 | { | |
5e0464a1 | 91 | delete[] buf; |
bb849dbd | 92 | bufsize = strlen (url) * 3 + 1; |
5e0464a1 | 93 | buf = new char [bufsize]; |
bb849dbd RC |
94 | } |
95 | for (p = url, q = buf; *p != '\0'; p++, q++) | |
96 | { | |
97 | do_escape = 0; | |
98 | ||
99 | /* RFC 1738 defines these chars as unsafe */ | |
100 | for (i = 0; i < sizeof (rfc1738_unsafe_chars); i++) | |
101 | { | |
102 | if (*p == rfc1738_unsafe_chars[i]) | |
103 | { | |
104 | do_escape = 1; | |
105 | break; | |
106 | } | |
107 | } | |
108 | /* Handle % separately */ | |
109 | if (encode_reserved >= 0 && *p == '%') | |
110 | do_escape = 1; | |
111 | /* RFC 1738 defines these chars as reserved */ | |
112 | for (i = 0; i < sizeof (rfc1738_reserved_chars) && encode_reserved > 0; | |
113 | i++) | |
114 | { | |
115 | if (*p == rfc1738_reserved_chars[i]) | |
116 | { | |
117 | do_escape = 1; | |
118 | break; | |
119 | } | |
120 | } | |
121 | /* RFC 1738 says any control chars (0x00-0x1F) are encoded */ | |
122 | if ((unsigned char) *p <= (unsigned char) 0x1F) | |
123 | { | |
124 | do_escape = 1; | |
125 | } | |
126 | /* RFC 1738 says 0x7f is encoded */ | |
127 | if (*p == (char) 0x7F) | |
128 | { | |
129 | do_escape = 1; | |
130 | } | |
131 | /* RFC 1738 says any non-US-ASCII are encoded */ | |
132 | if (((unsigned char) *p >= (unsigned char) 0x80) && | |
133 | ((unsigned char) *p <= (unsigned char) 0xFF)) | |
134 | { | |
135 | do_escape = 1; | |
136 | } | |
137 | /* Do the triplet encoding, or just copy the char */ | |
138 | /* note: we do not need snprintf here as q is appropriately | |
139 | * allocated - KA */ | |
140 | ||
141 | if (do_escape == 1) | |
142 | { | |
143 | (void) sprintf (q, "%%%02x", (unsigned char) *p); | |
144 | q += sizeof (char) * 2; | |
145 | } | |
146 | else | |
147 | { | |
148 | *q = *p; | |
149 | } | |
150 | } | |
151 | *q = '\0'; | |
152 | return (buf); | |
153 | } | |
154 | ||
155 | /* | |
156 | * rfc1738_escape - Returns a static buffer that contains the RFC | |
157 | * 1738 compliant, escaped version of the given url. | |
158 | */ | |
159 | char const * | |
160 | rfc1738_escape (const char *url) | |
161 | { | |
162 | return rfc1738_do_escape (url, 0); | |
163 | } | |
164 | ||
165 | /* | |
166 | * rfc1738_escape_unescaped - Returns a static buffer that contains | |
167 | * the RFC 1738 compliant, escaped version of the given url. | |
168 | */ | |
169 | char * | |
170 | rfc1738_escape_unescaped (const char *url) | |
171 | { | |
172 | return rfc1738_do_escape (url, -1); | |
173 | } | |
174 | ||
175 | /* | |
176 | * rfc1738_escape_part - Returns a static buffer that contains the | |
177 | * RFC 1738 compliant, escaped version of the given url segment. | |
178 | */ | |
179 | char const * | |
180 | rfc1738_escape_part (const char *url) | |
181 | { | |
182 | return rfc1738_do_escape (url, 1); | |
183 | } | |
3c054baf RC |
184 | String |
185 | rfc1738_escape_part (String const &url) | |
186 | { | |
187 | char const *t = rfc1738_do_escape (url.cstr_oneuse(), 1); | |
188 | return String (t); | |
189 | } | |
bb849dbd RC |
190 | |
191 | /* | |
192 | * rfc1738_unescape() - Converts escaped characters (%xy numbers) in | |
193 | * given the string. %% is a %. %ab is the 8-bit hexadecimal number "ab" | |
194 | */ | |
195 | void | |
196 | rfc1738_unescape (char *s) | |
197 | { | |
198 | char hexnum[3]; | |
199 | int i, j; /* i is write, j is read */ | |
200 | unsigned int x; | |
201 | for (i = j = 0; s[j]; i++, j++) | |
202 | { | |
203 | s[i] = s[j]; | |
204 | if (s[i] != '%') | |
205 | continue; | |
206 | if (s[j + 1] == '%') | |
207 | { /* %% case */ | |
208 | j++; | |
209 | continue; | |
210 | } | |
211 | if (s[j + 1] && s[j + 2]) | |
212 | { | |
213 | hexnum[0] = s[j + 1]; | |
214 | hexnum[1] = s[j + 2]; | |
215 | hexnum[2] = '\0'; | |
216 | if (1 == sscanf (hexnum, "%x", &x)) | |
217 | { | |
218 | s[i] = (char) (0x0ff & x); | |
219 | j += 2; | |
220 | } | |
221 | } | |
222 | } | |
223 | s[i] = '\0'; | |
224 | } |