]>
Commit | Line | Data |
---|---|---|
5999b776 | 1 | |
cb69b4c7 | 2 | /* |
cbdec147 | 3 | * $Id: HttpStatusLine.cc,v 1.14 1998/07/20 17:19:09 wessels Exp $ |
cb69b4c7 | 4 | * |
123abbe1 | 5 | * DEBUG: section 57 HTTP Status-line |
cb69b4c7 | 6 | * AUTHOR: Alex Rousskov |
7 | * | |
8 | * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ | |
9 | * -------------------------------------------------------- | |
10 | * | |
11 | * Squid is the result of efforts by numerous individuals from the | |
12 | * Internet community. Development is led by Duane Wessels of the | |
13 | * National Laboratory for Applied Network Research and funded by | |
14 | * the National Science Foundation. | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License as published by | |
18 | * the Free Software Foundation; either version 2 of the License, or | |
19 | * (at your option) any later version. | |
20 | * | |
21 | * This program is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
25 | * | |
26 | * You should have received a copy of the GNU General Public License | |
27 | * along with this program; if not, write to the Free Software | |
cbdec147 | 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
cb69b4c7 | 29 | * |
30 | */ | |
31 | ||
32 | #include "squid.h" | |
33 | ||
34 | ||
35 | /* local constants */ | |
36 | const char *HttpStatusLineFormat = "HTTP/%3.1f %3d %s\r\n"; | |
37 | ||
cb69b4c7 | 38 | void |
2ac76861 | 39 | httpStatusLineInit(HttpStatusLine * sline) |
40 | { | |
cb69b4c7 | 41 | httpStatusLineSet(sline, 0.0, 0, NULL); |
42 | } | |
43 | ||
44 | void | |
2ac76861 | 45 | httpStatusLineClean(HttpStatusLine * sline) |
46 | { | |
cb69b4c7 | 47 | httpStatusLineSet(sline, 0.0, 500, NULL); |
48 | } | |
49 | ||
adba4a64 | 50 | /* set values */ |
2ac76861 | 51 | void |
52 | httpStatusLineSet(HttpStatusLine * sline, double version, http_status status, const char *reason) | |
53 | { | |
cb69b4c7 | 54 | assert(sline); |
55 | sline->version = version; | |
56 | sline->status = status; | |
57 | /* Note: no xstrdup for 'reason', assumes constant 'reasons' */ | |
58 | sline->reason = reason; | |
59 | } | |
60 | ||
eb139d08 | 61 | /* parse a 0-terminating buffer and fill internal structures; returns true on success */ |
cb69b4c7 | 62 | void |
2ac76861 | 63 | httpStatusLinePackInto(const HttpStatusLine * sline, Packer * p) |
cb69b4c7 | 64 | { |
65 | assert(sline && p); | |
399e85ea | 66 | debug(57, 9) ("packing sline %p using %p:\n", sline, p); |
67 | debug(57, 9) (HttpStatusLineFormat, sline->version, sline->status, | |
cb69b4c7 | 68 | sline->reason ? sline->reason : httpStatusString(sline->status)); |
69 | packerPrintf(p, HttpStatusLineFormat, | |
5071f997 | 70 | sline->version, sline->status, httpStatusLineReason(sline)); |
cb69b4c7 | 71 | } |
72 | ||
adba4a64 | 73 | /* pack fields using Packer */ |
cb69b4c7 | 74 | int |
2ac76861 | 75 | httpStatusLineParse(HttpStatusLine * sline, const char *start, const char *end) |
76 | { | |
cb69b4c7 | 77 | assert(sline); |
2ac76861 | 78 | sline->status = HTTP_INVALID_HEADER; /* Squid header parsing error */ |
cb69b4c7 | 79 | if (strncasecmp(start, "HTTP/", 5)) |
80 | return 0; | |
81 | start += 5; | |
82 | if (!isdigit(*start)) | |
83 | return 0; | |
84 | sline->version = atof(start); | |
85 | if (!(start = strchr(start, ' '))) | |
86 | return 0; | |
87 | sline->status = atoi(++start); | |
88 | /* we ignore 'reason-phrase' */ | |
2ac76861 | 89 | return 1; /* success */ |
cb69b4c7 | 90 | } |
91 | ||
5071f997 | 92 | const char * |
93 | httpStatusLineReason(const HttpStatusLine * sline) | |
94 | { | |
95 | assert(sline); | |
96 | return sline->reason ? sline->reason : httpStatusString(sline->status); | |
97 | } | |
98 | ||
910169e5 | 99 | const char * |
cb69b4c7 | 100 | httpStatusString(http_status status) |
101 | { | |
102 | /* why not to return matching string instead of using "p" ? @?@ */ | |
103 | const char *p = NULL; | |
104 | switch (status) { | |
105 | case 0: | |
2ac76861 | 106 | p = "Init"; /* we init .status with code 0 */ |
cb69b4c7 | 107 | break; |
65d802fb | 108 | case HTTP_CONTINUE: |
cb69b4c7 | 109 | p = "Continue"; |
110 | break; | |
65d802fb | 111 | case HTTP_SWITCHING_PROTOCOLS: |
cb69b4c7 | 112 | p = "Switching Protocols"; |
113 | break; | |
65d802fb | 114 | case HTTP_OK: |
cb69b4c7 | 115 | p = "OK"; |
116 | break; | |
65d802fb | 117 | case HTTP_CREATED: |
cb69b4c7 | 118 | p = "Created"; |
119 | break; | |
65d802fb | 120 | case HTTP_ACCEPTED: |
cb69b4c7 | 121 | p = "Accepted"; |
122 | break; | |
65d802fb | 123 | case HTTP_NON_AUTHORITATIVE_INFORMATION: |
cb69b4c7 | 124 | p = "Non-Authoritative Information"; |
125 | break; | |
65d802fb | 126 | case HTTP_NO_CONTENT: |
cb69b4c7 | 127 | p = "No Content"; |
128 | break; | |
65d802fb | 129 | case HTTP_RESET_CONTENT: |
cb69b4c7 | 130 | p = "Reset Content"; |
131 | break; | |
65d802fb | 132 | case HTTP_PARTIAL_CONTENT: |
cb69b4c7 | 133 | p = "Partial Content"; |
134 | break; | |
65d802fb | 135 | case HTTP_MULTIPLE_CHOICES: |
cb69b4c7 | 136 | p = "Multiple Choices"; |
137 | break; | |
65d802fb | 138 | case HTTP_MOVED_PERMANENTLY: |
cb69b4c7 | 139 | p = "Moved Permanently"; |
140 | break; | |
65d802fb | 141 | case HTTP_MOVED_TEMPORARILY: |
cb69b4c7 | 142 | p = "Moved Temporarily"; |
143 | break; | |
65d802fb | 144 | case HTTP_SEE_OTHER: |
cb69b4c7 | 145 | p = "See Other"; |
146 | break; | |
65d802fb | 147 | case HTTP_NOT_MODIFIED: |
cb69b4c7 | 148 | p = "Not Modified"; |
149 | break; | |
65d802fb | 150 | case HTTP_USE_PROXY: |
cb69b4c7 | 151 | p = "Use Proxy"; |
152 | break; | |
65d802fb | 153 | case HTTP_BAD_REQUEST: |
cb69b4c7 | 154 | p = "Bad Request"; |
155 | break; | |
65d802fb | 156 | case HTTP_UNAUTHORIZED: |
cb69b4c7 | 157 | p = "Unauthorized"; |
158 | break; | |
65d802fb | 159 | case HTTP_PAYMENT_REQUIRED: |
cb69b4c7 | 160 | p = "Payment Required"; |
161 | break; | |
65d802fb | 162 | case HTTP_FORBIDDEN: |
cb69b4c7 | 163 | p = "Forbidden"; |
164 | break; | |
65d802fb | 165 | case HTTP_NOT_FOUND: |
cb69b4c7 | 166 | p = "Not Found"; |
167 | break; | |
65d802fb | 168 | case HTTP_METHOD_NOT_ALLOWED: |
cb69b4c7 | 169 | p = "Method Not Allowed"; |
170 | break; | |
65d802fb | 171 | case HTTP_NOT_ACCEPTABLE: |
cb69b4c7 | 172 | p = "Not Acceptable"; |
173 | break; | |
65d802fb | 174 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
cb69b4c7 | 175 | p = "Proxy Authentication Required"; |
176 | break; | |
65d802fb | 177 | case HTTP_REQUEST_TIMEOUT: |
cb69b4c7 | 178 | p = "Request Time-out"; |
179 | break; | |
65d802fb | 180 | case HTTP_CONFLICT: |
cb69b4c7 | 181 | p = "Conflict"; |
182 | break; | |
65d802fb | 183 | case HTTP_GONE: |
cb69b4c7 | 184 | p = "Gone"; |
185 | break; | |
65d802fb | 186 | case HTTP_LENGTH_REQUIRED: |
cb69b4c7 | 187 | p = "Length Required"; |
188 | break; | |
65d802fb | 189 | case HTTP_PRECONDITION_FAILED: |
cb69b4c7 | 190 | p = "Precondition Failed"; |
191 | break; | |
65d802fb | 192 | case HTTP_REQUEST_ENTITY_TOO_LARGE: |
cb69b4c7 | 193 | p = "Request Entity Too Large"; |
194 | break; | |
65d802fb | 195 | case HTTP_REQUEST_URI_TOO_LARGE: |
cb69b4c7 | 196 | p = "Request-URI Too Large"; |
197 | break; | |
65d802fb | 198 | case HTTP_UNSUPPORTED_MEDIA_TYPE: |
cb69b4c7 | 199 | p = "Unsupported Media Type"; |
200 | break; | |
65d802fb | 201 | case HTTP_INTERNAL_SERVER_ERROR: |
cb69b4c7 | 202 | p = "Internal Server Error"; |
203 | break; | |
65d802fb | 204 | case HTTP_NOT_IMPLEMENTED: |
cb69b4c7 | 205 | p = "Not Implemented"; |
206 | break; | |
65d802fb | 207 | case HTTP_BAD_GATEWAY: |
cb69b4c7 | 208 | p = "Bad Gateway"; |
209 | break; | |
65d802fb | 210 | case HTTP_SERVICE_UNAVAILABLE: |
cb69b4c7 | 211 | p = "Service Unavailable"; |
212 | break; | |
65d802fb | 213 | case HTTP_GATEWAY_TIMEOUT: |
cb69b4c7 | 214 | p = "Gateway Time-out"; |
215 | break; | |
65d802fb | 216 | case HTTP_HTTP_VERSION_NOT_SUPPORTED: |
cb69b4c7 | 217 | p = "HTTP Version not supported"; |
218 | break; | |
219 | default: | |
220 | p = "Unknown"; | |
20198c7a | 221 | debug(57, 0) ("Unknown HTTP status code: %d\n", status); |
cb69b4c7 | 222 | break; |
223 | } | |
224 | return p; | |
225 | } |