]>
Commit | Line | Data |
---|---|---|
5999b776 | 1 | |
cb69b4c7 | 2 | /* |
262a0e14 | 3 | * $Id$ |
cb69b4c7 | 4 | * |
123abbe1 | 5 | * DEBUG: section 57 HTTP Status-line |
cb69b4c7 | 6 | * AUTHOR: Alex Rousskov |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 9 | * ---------------------------------------------------------- |
cb69b4c7 | 10 | * |
2b6662ba | 11 | * Squid is the result of efforts by numerous individuals from |
12 | * the Internet community; see the CONTRIBUTORS file for full | |
13 | * details. Many organizations have provided support for Squid's | |
14 | * development; see the SPONSORS file for full details. Squid is | |
15 | * Copyrighted (C) 2001 by the Regents of the University of | |
16 | * California; see the COPYRIGHT file for full details. Squid | |
17 | * incorporates software developed and/or copyrighted by other | |
18 | * sources; see the CREDITS file for full details. | |
cb69b4c7 | 19 | * |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
26ac0430 | 24 | * |
cb69b4c7 | 25 | * This program is distributed in the hope that it will be useful, |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
26ac0430 | 29 | * |
cb69b4c7 | 30 | * You should have received a copy of the GNU General Public License |
31 | * along with this program; if not, write to the Free Software | |
cbdec147 | 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 33 | * |
cb69b4c7 | 34 | */ |
35 | ||
36 | #include "squid.h" | |
450e0c10 | 37 | #include "HttpStatusLine.h" |
cb69b4c7 | 38 | |
39 | /* local constants */ | |
ecf57684 | 40 | /* AYJ: see bug 2469 - RFC2616 confirms stating 'SP characters' plural! */ |
ccf44862 | 41 | const char *HttpStatusLineFormat = "HTTP/%d.%d %3d %s\r\n"; |
cb69b4c7 | 42 | |
cb69b4c7 | 43 | void |
2ac76861 | 44 | httpStatusLineInit(HttpStatusLine * sline) |
45 | { | |
450e0c10 | 46 | HttpVersion version; |
bffee5af | 47 | httpStatusLineSet(sline, version, HTTP_STATUS_NONE, NULL); |
cb69b4c7 | 48 | } |
49 | ||
50 | void | |
2ac76861 | 51 | httpStatusLineClean(HttpStatusLine * sline) |
52 | { | |
450e0c10 | 53 | HttpVersion version; |
ccf44862 | 54 | httpStatusLineSet(sline, version, HTTP_INTERNAL_SERVER_ERROR, NULL); |
cb69b4c7 | 55 | } |
56 | ||
adba4a64 | 57 | /* set values */ |
2ac76861 | 58 | void |
450e0c10 | 59 | httpStatusLineSet(HttpStatusLine * sline, HttpVersion version, http_status status, const char *reason) |
2ac76861 | 60 | { |
cb69b4c7 | 61 | assert(sline); |
62 | sline->version = version; | |
63 | sline->status = status; | |
64 | /* Note: no xstrdup for 'reason', assumes constant 'reasons' */ | |
65 | sline->reason = reason; | |
66 | } | |
67 | ||
cfdef7b5 | 68 | /** write HTTP version and status structures into a Packer buffer for output as HTTP status line. */ |
cb69b4c7 | 69 | void |
2ac76861 | 70 | httpStatusLinePackInto(const HttpStatusLine * sline, Packer * p) |
cb69b4c7 | 71 | { |
72 | assert(sline && p); | |
bf8fe701 | 73 | debugs(57, 9, "packing sline " << sline << " using " << p << ":"); |
bffee5af | 74 | debug(57, 9) (HttpStatusLineFormat, sline->version.major, |
62e76326 | 75 | sline->version.minor, sline->status, |
76 | sline->reason ? sline->reason : httpStatusString(sline->status)); | |
bffee5af | 77 | packerPrintf(p, HttpStatusLineFormat, sline->version.major, |
62e76326 | 78 | sline->version.minor, sline->status, httpStatusLineReason(sline)); |
cb69b4c7 | 79 | } |
80 | ||
a83bdfdc | 81 | /* |
82 | * Parse character string into 'sline'. Note 'end' currently unused, | |
83 | * so NULL-termination assumed. | |
84 | */ | |
cb69b4c7 | 85 | int |
30abd221 | 86 | httpStatusLineParse(HttpStatusLine * sline, const String &protoPrefix, const char *start, const char *end) |
2ac76861 | 87 | { |
cb69b4c7 | 88 | assert(sline); |
2ac76861 | 89 | sline->status = HTTP_INVALID_HEADER; /* Squid header parsing error */ |
62e76326 | 90 | |
a83bdfdc | 91 | // XXX: HttpMsg::parse() has a similar check but is using |
8596962e | 92 | // casesensitive comparison (which is required by HTTP errata?) |
93 | ||
30abd221 | 94 | if (protoPrefix.caseCmp(start, protoPrefix.size()) != 0) |
62e76326 | 95 | return 0; |
96 | ||
8596962e | 97 | start += protoPrefix.size(); |
62e76326 | 98 | |
b6a2f15e | 99 | if (!xisdigit(*start)) |
62e76326 | 100 | return 0; |
101 | ||
bffee5af | 102 | if (sscanf(start, "%d.%d", &sline->version.major, &sline->version.minor) != 2) { |
bf8fe701 | 103 | debugs(57, 7, "httpStatusLineParse: Invalid HTTP identifier."); |
ccf44862 | 104 | } |
62e76326 | 105 | |
cb69b4c7 | 106 | if (!(start = strchr(start, ' '))) |
62e76326 | 107 | return 0; |
108 | ||
6437ac71 | 109 | sline->status = (http_status) atoi(++start); |
62e76326 | 110 | |
cb69b4c7 | 111 | /* we ignore 'reason-phrase' */ |
a83bdfdc | 112 | /* Should assert start < end ? */ |
2ac76861 | 113 | return 1; /* success */ |
cb69b4c7 | 114 | } |
115 | ||
5071f997 | 116 | const char * |
117 | httpStatusLineReason(const HttpStatusLine * sline) | |
118 | { | |
119 | assert(sline); | |
120 | return sline->reason ? sline->reason : httpStatusString(sline->status); | |
121 | } | |
122 | ||
910169e5 | 123 | const char * |
cb69b4c7 | 124 | httpStatusString(http_status status) |
125 | { | |
126 | /* why not to return matching string instead of using "p" ? @?@ */ | |
127 | const char *p = NULL; | |
62e76326 | 128 | |
cb69b4c7 | 129 | switch (status) { |
62e76326 | 130 | |
cb69b4c7 | 131 | case 0: |
62e76326 | 132 | p = "Init"; /* we init .status with code 0 */ |
133 | break; | |
134 | ||
65d802fb | 135 | case HTTP_CONTINUE: |
62e76326 | 136 | p = "Continue"; |
137 | break; | |
138 | ||
65d802fb | 139 | case HTTP_SWITCHING_PROTOCOLS: |
62e76326 | 140 | p = "Switching Protocols"; |
141 | break; | |
142 | ||
65d802fb | 143 | case HTTP_OK: |
62e76326 | 144 | p = "OK"; |
145 | break; | |
146 | ||
65d802fb | 147 | case HTTP_CREATED: |
62e76326 | 148 | p = "Created"; |
149 | break; | |
150 | ||
65d802fb | 151 | case HTTP_ACCEPTED: |
62e76326 | 152 | p = "Accepted"; |
153 | break; | |
154 | ||
65d802fb | 155 | case HTTP_NON_AUTHORITATIVE_INFORMATION: |
62e76326 | 156 | p = "Non-Authoritative Information"; |
157 | break; | |
158 | ||
65d802fb | 159 | case HTTP_NO_CONTENT: |
62e76326 | 160 | p = "No Content"; |
161 | break; | |
162 | ||
65d802fb | 163 | case HTTP_RESET_CONTENT: |
62e76326 | 164 | p = "Reset Content"; |
165 | break; | |
166 | ||
65d802fb | 167 | case HTTP_PARTIAL_CONTENT: |
62e76326 | 168 | p = "Partial Content"; |
169 | break; | |
170 | ||
65d802fb | 171 | case HTTP_MULTIPLE_CHOICES: |
62e76326 | 172 | p = "Multiple Choices"; |
173 | break; | |
174 | ||
65d802fb | 175 | case HTTP_MOVED_PERMANENTLY: |
62e76326 | 176 | p = "Moved Permanently"; |
177 | break; | |
178 | ||
65d802fb | 179 | case HTTP_MOVED_TEMPORARILY: |
62e76326 | 180 | p = "Moved Temporarily"; |
181 | break; | |
182 | ||
65d802fb | 183 | case HTTP_SEE_OTHER: |
62e76326 | 184 | p = "See Other"; |
185 | break; | |
186 | ||
65d802fb | 187 | case HTTP_NOT_MODIFIED: |
62e76326 | 188 | p = "Not Modified"; |
189 | break; | |
190 | ||
65d802fb | 191 | case HTTP_USE_PROXY: |
62e76326 | 192 | p = "Use Proxy"; |
193 | break; | |
194 | ||
6bae99ed | 195 | case HTTP_TEMPORARY_REDIRECT: |
62e76326 | 196 | p = "Temporary Redirect"; |
197 | break; | |
198 | ||
65d802fb | 199 | case HTTP_BAD_REQUEST: |
62e76326 | 200 | p = "Bad Request"; |
201 | break; | |
202 | ||
65d802fb | 203 | case HTTP_UNAUTHORIZED: |
62e76326 | 204 | p = "Unauthorized"; |
205 | break; | |
206 | ||
65d802fb | 207 | case HTTP_PAYMENT_REQUIRED: |
62e76326 | 208 | p = "Payment Required"; |
209 | break; | |
210 | ||
65d802fb | 211 | case HTTP_FORBIDDEN: |
62e76326 | 212 | p = "Forbidden"; |
213 | break; | |
214 | ||
65d802fb | 215 | case HTTP_NOT_FOUND: |
62e76326 | 216 | p = "Not Found"; |
217 | break; | |
218 | ||
65d802fb | 219 | case HTTP_METHOD_NOT_ALLOWED: |
62e76326 | 220 | p = "Method Not Allowed"; |
221 | break; | |
222 | ||
65d802fb | 223 | case HTTP_NOT_ACCEPTABLE: |
62e76326 | 224 | p = "Not Acceptable"; |
225 | break; | |
226 | ||
65d802fb | 227 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
62e76326 | 228 | p = "Proxy Authentication Required"; |
229 | break; | |
230 | ||
65d802fb | 231 | case HTTP_REQUEST_TIMEOUT: |
62e76326 | 232 | p = "Request Time-out"; |
233 | break; | |
234 | ||
65d802fb | 235 | case HTTP_CONFLICT: |
62e76326 | 236 | p = "Conflict"; |
237 | break; | |
238 | ||
65d802fb | 239 | case HTTP_GONE: |
62e76326 | 240 | p = "Gone"; |
241 | break; | |
242 | ||
65d802fb | 243 | case HTTP_LENGTH_REQUIRED: |
62e76326 | 244 | p = "Length Required"; |
245 | break; | |
246 | ||
65d802fb | 247 | case HTTP_PRECONDITION_FAILED: |
62e76326 | 248 | p = "Precondition Failed"; |
249 | break; | |
250 | ||
65d802fb | 251 | case HTTP_REQUEST_ENTITY_TOO_LARGE: |
62e76326 | 252 | p = "Request Entity Too Large"; |
253 | break; | |
254 | ||
65d802fb | 255 | case HTTP_REQUEST_URI_TOO_LARGE: |
62e76326 | 256 | p = "Request-URI Too Large"; |
257 | break; | |
258 | ||
65d802fb | 259 | case HTTP_UNSUPPORTED_MEDIA_TYPE: |
62e76326 | 260 | p = "Unsupported Media Type"; |
261 | break; | |
262 | ||
9e8942b0 | 263 | case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE: |
264 | p = "Requested Range Not Satisfiable"; | |
265 | break; | |
266 | ||
267 | case HTTP_EXPECTATION_FAILED: | |
268 | p = "Expectation Failed"; | |
269 | break; | |
270 | ||
65d802fb | 271 | case HTTP_INTERNAL_SERVER_ERROR: |
62e76326 | 272 | p = "Internal Server Error"; |
273 | break; | |
274 | ||
65d802fb | 275 | case HTTP_NOT_IMPLEMENTED: |
62e76326 | 276 | p = "Not Implemented"; |
277 | break; | |
278 | ||
65d802fb | 279 | case HTTP_BAD_GATEWAY: |
62e76326 | 280 | p = "Bad Gateway"; |
281 | break; | |
282 | ||
65d802fb | 283 | case HTTP_SERVICE_UNAVAILABLE: |
62e76326 | 284 | p = "Service Unavailable"; |
285 | break; | |
286 | ||
65d802fb | 287 | case HTTP_GATEWAY_TIMEOUT: |
62e76326 | 288 | p = "Gateway Time-out"; |
289 | break; | |
290 | ||
65d802fb | 291 | case HTTP_HTTP_VERSION_NOT_SUPPORTED: |
62e76326 | 292 | p = "HTTP Version not supported"; |
293 | break; | |
294 | ||
cb69b4c7 | 295 | default: |
62e76326 | 296 | p = "Unknown"; |
bf8fe701 | 297 | debugs(57, 3, "Unknown HTTP status code: " << status); |
62e76326 | 298 | break; |
cb69b4c7 | 299 | } |
62e76326 | 300 | |
cb69b4c7 | 301 | return p; |
302 | } |