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