]>
Commit | Line | Data |
---|---|---|
5999b776 | 1 | |
cb69b4c7 | 2 | /* |
8596962e | 3 | * $Id: HttpStatusLine.cc,v 1.29 2005/09/12 23:28:57 wessels Exp $ |
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. | |
24 | * | |
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. | |
29 | * | |
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 */ | |
ccf44862 | 40 | const char *HttpStatusLineFormat = "HTTP/%d.%d %3d %s\r\n"; |
cb69b4c7 | 41 | |
cb69b4c7 | 42 | void |
2ac76861 | 43 | httpStatusLineInit(HttpStatusLine * sline) |
44 | { | |
450e0c10 | 45 | HttpVersion version; |
bffee5af | 46 | httpStatusLineSet(sline, version, HTTP_STATUS_NONE, NULL); |
cb69b4c7 | 47 | } |
48 | ||
49 | void | |
2ac76861 | 50 | httpStatusLineClean(HttpStatusLine * sline) |
51 | { | |
450e0c10 | 52 | HttpVersion version; |
ccf44862 | 53 | httpStatusLineSet(sline, version, HTTP_INTERNAL_SERVER_ERROR, NULL); |
cb69b4c7 | 54 | } |
55 | ||
adba4a64 | 56 | /* set values */ |
2ac76861 | 57 | void |
450e0c10 | 58 | httpStatusLineSet(HttpStatusLine * sline, HttpVersion version, http_status status, const char *reason) |
2ac76861 | 59 | { |
cb69b4c7 | 60 | assert(sline); |
61 | sline->version = version; | |
62 | sline->status = status; | |
63 | /* Note: no xstrdup for 'reason', assumes constant 'reasons' */ | |
64 | sline->reason = reason; | |
65 | } | |
66 | ||
eb139d08 | 67 | /* parse a 0-terminating buffer and fill internal structures; returns true on success */ |
cb69b4c7 | 68 | void |
2ac76861 | 69 | httpStatusLinePackInto(const HttpStatusLine * sline, Packer * p) |
cb69b4c7 | 70 | { |
71 | assert(sline && p); | |
399e85ea | 72 | debug(57, 9) ("packing sline %p using %p:\n", sline, p); |
bffee5af | 73 | debug(57, 9) (HttpStatusLineFormat, sline->version.major, |
62e76326 | 74 | sline->version.minor, sline->status, |
75 | sline->reason ? sline->reason : httpStatusString(sline->status)); | |
bffee5af | 76 | packerPrintf(p, HttpStatusLineFormat, sline->version.major, |
62e76326 | 77 | sline->version.minor, sline->status, httpStatusLineReason(sline)); |
cb69b4c7 | 78 | } |
79 | ||
adba4a64 | 80 | /* pack fields using Packer */ |
cb69b4c7 | 81 | int |
8596962e | 82 | httpStatusLineParse(HttpStatusLine * sline, const String &protoPrefix, const char *start, const char *end) |
2ac76861 | 83 | { |
cb69b4c7 | 84 | assert(sline); |
2ac76861 | 85 | sline->status = HTTP_INVALID_HEADER; /* Squid header parsing error */ |
62e76326 | 86 | |
8596962e | 87 | // XXX: HttpReply::parse() has a similar check but is using |
88 | // casesensitive comparison (which is required by HTTP errata?) | |
89 | ||
90 | if (protoPrefix.caseCmp(start, protoPrefix.size()) != 0) | |
62e76326 | 91 | return 0; |
92 | ||
8596962e | 93 | start += protoPrefix.size(); |
62e76326 | 94 | |
b6a2f15e | 95 | if (!xisdigit(*start)) |
62e76326 | 96 | return 0; |
97 | ||
bffee5af | 98 | if (sscanf(start, "%d.%d", &sline->version.major, &sline->version.minor) != 2) { |
62e76326 | 99 | debug(57, 7) ("httpStatusLineParse: Invalid HTTP identifier.\n"); |
ccf44862 | 100 | } |
62e76326 | 101 | |
cb69b4c7 | 102 | if (!(start = strchr(start, ' '))) |
62e76326 | 103 | return 0; |
104 | ||
6437ac71 | 105 | sline->status = (http_status) atoi(++start); |
62e76326 | 106 | |
cb69b4c7 | 107 | /* we ignore 'reason-phrase' */ |
2ac76861 | 108 | return 1; /* success */ |
cb69b4c7 | 109 | } |
110 | ||
5071f997 | 111 | const char * |
112 | httpStatusLineReason(const HttpStatusLine * sline) | |
113 | { | |
114 | assert(sline); | |
115 | return sline->reason ? sline->reason : httpStatusString(sline->status); | |
116 | } | |
117 | ||
910169e5 | 118 | const char * |
cb69b4c7 | 119 | httpStatusString(http_status status) |
120 | { | |
121 | /* why not to return matching string instead of using "p" ? @?@ */ | |
122 | const char *p = NULL; | |
62e76326 | 123 | |
cb69b4c7 | 124 | switch (status) { |
62e76326 | 125 | |
cb69b4c7 | 126 | case 0: |
62e76326 | 127 | p = "Init"; /* we init .status with code 0 */ |
128 | break; | |
129 | ||
65d802fb | 130 | case HTTP_CONTINUE: |
62e76326 | 131 | p = "Continue"; |
132 | break; | |
133 | ||
65d802fb | 134 | case HTTP_SWITCHING_PROTOCOLS: |
62e76326 | 135 | p = "Switching Protocols"; |
136 | break; | |
137 | ||
65d802fb | 138 | case HTTP_OK: |
62e76326 | 139 | p = "OK"; |
140 | break; | |
141 | ||
65d802fb | 142 | case HTTP_CREATED: |
62e76326 | 143 | p = "Created"; |
144 | break; | |
145 | ||
65d802fb | 146 | case HTTP_ACCEPTED: |
62e76326 | 147 | p = "Accepted"; |
148 | break; | |
149 | ||
65d802fb | 150 | case HTTP_NON_AUTHORITATIVE_INFORMATION: |
62e76326 | 151 | p = "Non-Authoritative Information"; |
152 | break; | |
153 | ||
65d802fb | 154 | case HTTP_NO_CONTENT: |
62e76326 | 155 | p = "No Content"; |
156 | break; | |
157 | ||
65d802fb | 158 | case HTTP_RESET_CONTENT: |
62e76326 | 159 | p = "Reset Content"; |
160 | break; | |
161 | ||
65d802fb | 162 | case HTTP_PARTIAL_CONTENT: |
62e76326 | 163 | p = "Partial Content"; |
164 | break; | |
165 | ||
65d802fb | 166 | case HTTP_MULTIPLE_CHOICES: |
62e76326 | 167 | p = "Multiple Choices"; |
168 | break; | |
169 | ||
65d802fb | 170 | case HTTP_MOVED_PERMANENTLY: |
62e76326 | 171 | p = "Moved Permanently"; |
172 | break; | |
173 | ||
65d802fb | 174 | case HTTP_MOVED_TEMPORARILY: |
62e76326 | 175 | p = "Moved Temporarily"; |
176 | break; | |
177 | ||
65d802fb | 178 | case HTTP_SEE_OTHER: |
62e76326 | 179 | p = "See Other"; |
180 | break; | |
181 | ||
65d802fb | 182 | case HTTP_NOT_MODIFIED: |
62e76326 | 183 | p = "Not Modified"; |
184 | break; | |
185 | ||
65d802fb | 186 | case HTTP_USE_PROXY: |
62e76326 | 187 | p = "Use Proxy"; |
188 | break; | |
189 | ||
6bae99ed | 190 | case HTTP_TEMPORARY_REDIRECT: |
62e76326 | 191 | p = "Temporary Redirect"; |
192 | break; | |
193 | ||
65d802fb | 194 | case HTTP_BAD_REQUEST: |
62e76326 | 195 | p = "Bad Request"; |
196 | break; | |
197 | ||
65d802fb | 198 | case HTTP_UNAUTHORIZED: |
62e76326 | 199 | p = "Unauthorized"; |
200 | break; | |
201 | ||
65d802fb | 202 | case HTTP_PAYMENT_REQUIRED: |
62e76326 | 203 | p = "Payment Required"; |
204 | break; | |
205 | ||
65d802fb | 206 | case HTTP_FORBIDDEN: |
62e76326 | 207 | p = "Forbidden"; |
208 | break; | |
209 | ||
65d802fb | 210 | case HTTP_NOT_FOUND: |
62e76326 | 211 | p = "Not Found"; |
212 | break; | |
213 | ||
65d802fb | 214 | case HTTP_METHOD_NOT_ALLOWED: |
62e76326 | 215 | p = "Method Not Allowed"; |
216 | break; | |
217 | ||
65d802fb | 218 | case HTTP_NOT_ACCEPTABLE: |
62e76326 | 219 | p = "Not Acceptable"; |
220 | break; | |
221 | ||
65d802fb | 222 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
62e76326 | 223 | p = "Proxy Authentication Required"; |
224 | break; | |
225 | ||
65d802fb | 226 | case HTTP_REQUEST_TIMEOUT: |
62e76326 | 227 | p = "Request Time-out"; |
228 | break; | |
229 | ||
65d802fb | 230 | case HTTP_CONFLICT: |
62e76326 | 231 | p = "Conflict"; |
232 | break; | |
233 | ||
65d802fb | 234 | case HTTP_GONE: |
62e76326 | 235 | p = "Gone"; |
236 | break; | |
237 | ||
65d802fb | 238 | case HTTP_LENGTH_REQUIRED: |
62e76326 | 239 | p = "Length Required"; |
240 | break; | |
241 | ||
65d802fb | 242 | case HTTP_PRECONDITION_FAILED: |
62e76326 | 243 | p = "Precondition Failed"; |
244 | break; | |
245 | ||
65d802fb | 246 | case HTTP_REQUEST_ENTITY_TOO_LARGE: |
62e76326 | 247 | p = "Request Entity Too Large"; |
248 | break; | |
249 | ||
65d802fb | 250 | case HTTP_REQUEST_URI_TOO_LARGE: |
62e76326 | 251 | p = "Request-URI Too Large"; |
252 | break; | |
253 | ||
65d802fb | 254 | case HTTP_UNSUPPORTED_MEDIA_TYPE: |
62e76326 | 255 | p = "Unsupported Media Type"; |
256 | break; | |
257 | ||
65d802fb | 258 | case HTTP_INTERNAL_SERVER_ERROR: |
62e76326 | 259 | p = "Internal Server Error"; |
260 | break; | |
261 | ||
65d802fb | 262 | case HTTP_NOT_IMPLEMENTED: |
62e76326 | 263 | p = "Not Implemented"; |
264 | break; | |
265 | ||
65d802fb | 266 | case HTTP_BAD_GATEWAY: |
62e76326 | 267 | p = "Bad Gateway"; |
268 | break; | |
269 | ||
65d802fb | 270 | case HTTP_SERVICE_UNAVAILABLE: |
62e76326 | 271 | p = "Service Unavailable"; |
272 | break; | |
273 | ||
65d802fb | 274 | case HTTP_GATEWAY_TIMEOUT: |
62e76326 | 275 | p = "Gateway Time-out"; |
276 | break; | |
277 | ||
65d802fb | 278 | case HTTP_HTTP_VERSION_NOT_SUPPORTED: |
62e76326 | 279 | p = "HTTP Version not supported"; |
280 | break; | |
281 | ||
cb69b4c7 | 282 | default: |
62e76326 | 283 | p = "Unknown"; |
284 | debug(57, 3) ("Unknown HTTP status code: %d\n", status); | |
285 | break; | |
cb69b4c7 | 286 | } |
62e76326 | 287 | |
cb69b4c7 | 288 | return p; |
289 | } |