]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpStatusLine.cc
Updated copyright
[thirdparty/squid.git] / src / HttpStatusLine.cc
1
2 /*
3 * $Id: HttpStatusLine.cc,v 1.22 2001/01/12 00:37:14 wessels Exp $
4 *
5 * DEBUG: section 57 HTTP Status-line
6 * AUTHOR: Alex Rousskov
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
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
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37
38
39 /* local constants */
40 const char *HttpStatusLineFormat = "HTTP/%d.%d %3d %s\r\n";
41
42 void
43 httpStatusLineInit(HttpStatusLine * sline)
44 {
45 http_version_t version;
46 httpBuildVersion(&version, 0, 0);
47 httpStatusLineSet(sline, version, HTTP_STATUS_NONE, NULL);
48 }
49
50 void
51 httpStatusLineClean(HttpStatusLine * sline)
52 {
53 http_version_t version;
54 httpBuildVersion(&version, 0, 0);
55 httpStatusLineSet(sline, version, HTTP_INTERNAL_SERVER_ERROR, NULL);
56 }
57
58 /* set values */
59 void
60 httpStatusLineSet(HttpStatusLine * sline, http_version_t version, http_status status, const char *reason)
61 {
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
69 /* parse a 0-terminating buffer and fill internal structures; returns true on success */
70 void
71 httpStatusLinePackInto(const HttpStatusLine * sline, Packer * p)
72 {
73 assert(sline && p);
74 debug(57, 9) ("packing sline %p using %p:\n", sline, p);
75 debug(57, 9) (HttpStatusLineFormat, sline->version.major,
76 sline->version.minor, sline->status,
77 sline->reason ? sline->reason : httpStatusString(sline->status));
78 packerPrintf(p, HttpStatusLineFormat, sline->version.major,
79 sline->version.minor, sline->status, httpStatusLineReason(sline));
80 }
81
82 /* pack fields using Packer */
83 int
84 httpStatusLineParse(HttpStatusLine * sline, const char *start, const char *end)
85 {
86 assert(sline);
87 sline->status = HTTP_INVALID_HEADER; /* Squid header parsing error */
88 if (strncasecmp(start, "HTTP/", 5))
89 return 0;
90 start += 5;
91 if (!xisdigit(*start))
92 return 0;
93 if (sscanf(start, "%d.%d", &sline->version.major, &sline->version.minor) != 2) {
94 debug(57, 7) ("httpStatusLineParse: Invalid HTTP identifier.\n");
95 }
96 if (!(start = strchr(start, ' ')))
97 return 0;
98 sline->status = atoi(++start);
99 /* we ignore 'reason-phrase' */
100 return 1; /* success */
101 }
102
103 const char *
104 httpStatusLineReason(const HttpStatusLine * sline)
105 {
106 assert(sline);
107 return sline->reason ? sline->reason : httpStatusString(sline->status);
108 }
109
110 const char *
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:
117 p = "Init"; /* we init .status with code 0 */
118 break;
119 case HTTP_CONTINUE:
120 p = "Continue";
121 break;
122 case HTTP_SWITCHING_PROTOCOLS:
123 p = "Switching Protocols";
124 break;
125 case HTTP_OK:
126 p = "OK";
127 break;
128 case HTTP_CREATED:
129 p = "Created";
130 break;
131 case HTTP_ACCEPTED:
132 p = "Accepted";
133 break;
134 case HTTP_NON_AUTHORITATIVE_INFORMATION:
135 p = "Non-Authoritative Information";
136 break;
137 case HTTP_NO_CONTENT:
138 p = "No Content";
139 break;
140 case HTTP_RESET_CONTENT:
141 p = "Reset Content";
142 break;
143 case HTTP_PARTIAL_CONTENT:
144 p = "Partial Content";
145 break;
146 case HTTP_MULTIPLE_CHOICES:
147 p = "Multiple Choices";
148 break;
149 case HTTP_MOVED_PERMANENTLY:
150 p = "Moved Permanently";
151 break;
152 case HTTP_MOVED_TEMPORARILY:
153 p = "Moved Temporarily";
154 break;
155 case HTTP_SEE_OTHER:
156 p = "See Other";
157 break;
158 case HTTP_NOT_MODIFIED:
159 p = "Not Modified";
160 break;
161 case HTTP_USE_PROXY:
162 p = "Use Proxy";
163 break;
164 case HTTP_BAD_REQUEST:
165 p = "Bad Request";
166 break;
167 case HTTP_UNAUTHORIZED:
168 p = "Unauthorized";
169 break;
170 case HTTP_PAYMENT_REQUIRED:
171 p = "Payment Required";
172 break;
173 case HTTP_FORBIDDEN:
174 p = "Forbidden";
175 break;
176 case HTTP_NOT_FOUND:
177 p = "Not Found";
178 break;
179 case HTTP_METHOD_NOT_ALLOWED:
180 p = "Method Not Allowed";
181 break;
182 case HTTP_NOT_ACCEPTABLE:
183 p = "Not Acceptable";
184 break;
185 case HTTP_PROXY_AUTHENTICATION_REQUIRED:
186 p = "Proxy Authentication Required";
187 break;
188 case HTTP_REQUEST_TIMEOUT:
189 p = "Request Time-out";
190 break;
191 case HTTP_CONFLICT:
192 p = "Conflict";
193 break;
194 case HTTP_GONE:
195 p = "Gone";
196 break;
197 case HTTP_LENGTH_REQUIRED:
198 p = "Length Required";
199 break;
200 case HTTP_PRECONDITION_FAILED:
201 p = "Precondition Failed";
202 break;
203 case HTTP_REQUEST_ENTITY_TOO_LARGE:
204 p = "Request Entity Too Large";
205 break;
206 case HTTP_REQUEST_URI_TOO_LARGE:
207 p = "Request-URI Too Large";
208 break;
209 case HTTP_UNSUPPORTED_MEDIA_TYPE:
210 p = "Unsupported Media Type";
211 break;
212 case HTTP_INTERNAL_SERVER_ERROR:
213 p = "Internal Server Error";
214 break;
215 case HTTP_NOT_IMPLEMENTED:
216 p = "Not Implemented";
217 break;
218 case HTTP_BAD_GATEWAY:
219 p = "Bad Gateway";
220 break;
221 case HTTP_SERVICE_UNAVAILABLE:
222 p = "Service Unavailable";
223 break;
224 case HTTP_GATEWAY_TIMEOUT:
225 p = "Gateway Time-out";
226 break;
227 case HTTP_HTTP_VERSION_NOT_SUPPORTED:
228 p = "HTTP Version not supported";
229 break;
230 default:
231 p = "Unknown";
232 debug(57, 3) ("Unknown HTTP status code: %d\n", status);
233 break;
234 }
235 return p;
236 }