]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpStatusLine.cc
account for no content-length (<0)
[thirdparty/squid.git] / src / HttpStatusLine.cc
CommitLineData
5999b776 1
cb69b4c7 2/*
eb139d08 3 * $Id: HttpStatusLine.cc,v 1.12 1998/06/03 15:52:16 rousskov 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
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 */
31
32#include "squid.h"
33
34
35/* local constants */
36const char *HttpStatusLineFormat = "HTTP/%3.1f %3d %s\r\n";
37
38/* local routines */
39static const char *httpStatusString(http_status status);
40
adba4a64 41
42
cb69b4c7 43void
2ac76861 44httpStatusLineInit(HttpStatusLine * sline)
45{
cb69b4c7 46 httpStatusLineSet(sline, 0.0, 0, NULL);
47}
48
49void
2ac76861 50httpStatusLineClean(HttpStatusLine * sline)
51{
cb69b4c7 52 httpStatusLineSet(sline, 0.0, 500, NULL);
53}
54
adba4a64 55/* set values */
2ac76861 56void
57httpStatusLineSet(HttpStatusLine * sline, double version, http_status status, const char *reason)
58{
cb69b4c7 59 assert(sline);
60 sline->version = version;
61 sline->status = status;
62 /* Note: no xstrdup for 'reason', assumes constant 'reasons' */
63 sline->reason = reason;
64}
65
eb139d08 66/* parse a 0-terminating buffer and fill internal structures; returns true on success */
cb69b4c7 67void
2ac76861 68httpStatusLinePackInto(const HttpStatusLine * sline, Packer * p)
cb69b4c7 69{
70 assert(sline && p);
399e85ea 71 debug(57, 9) ("packing sline %p using %p:\n", sline, p);
72 debug(57, 9) (HttpStatusLineFormat, sline->version, sline->status,
cb69b4c7 73 sline->reason ? sline->reason : httpStatusString(sline->status));
74 packerPrintf(p, HttpStatusLineFormat,
5071f997 75 sline->version, sline->status, httpStatusLineReason(sline));
cb69b4c7 76}
77
adba4a64 78/* pack fields using Packer */
cb69b4c7 79int
2ac76861 80httpStatusLineParse(HttpStatusLine * sline, const char *start, const char *end)
81{
cb69b4c7 82 assert(sline);
2ac76861 83 sline->status = HTTP_INVALID_HEADER; /* Squid header parsing error */
cb69b4c7 84 if (strncasecmp(start, "HTTP/", 5))
85 return 0;
86 start += 5;
87 if (!isdigit(*start))
88 return 0;
89 sline->version = atof(start);
90 if (!(start = strchr(start, ' ')))
91 return 0;
92 sline->status = atoi(++start);
93 /* we ignore 'reason-phrase' */
2ac76861 94 return 1; /* success */
cb69b4c7 95}
96
5071f997 97const char *
98httpStatusLineReason(const HttpStatusLine * sline)
99{
100 assert(sline);
101 return sline->reason ? sline->reason : httpStatusString(sline->status);
102}
103
cb69b4c7 104static const char *
105httpStatusString(http_status status)
106{
107 /* why not to return matching string instead of using "p" ? @?@ */
108 const char *p = NULL;
109 switch (status) {
110 case 0:
2ac76861 111 p = "Init"; /* we init .status with code 0 */
cb69b4c7 112 break;
65d802fb 113 case HTTP_CONTINUE:
cb69b4c7 114 p = "Continue";
115 break;
65d802fb 116 case HTTP_SWITCHING_PROTOCOLS:
cb69b4c7 117 p = "Switching Protocols";
118 break;
65d802fb 119 case HTTP_OK:
cb69b4c7 120 p = "OK";
121 break;
65d802fb 122 case HTTP_CREATED:
cb69b4c7 123 p = "Created";
124 break;
65d802fb 125 case HTTP_ACCEPTED:
cb69b4c7 126 p = "Accepted";
127 break;
65d802fb 128 case HTTP_NON_AUTHORITATIVE_INFORMATION:
cb69b4c7 129 p = "Non-Authoritative Information";
130 break;
65d802fb 131 case HTTP_NO_CONTENT:
cb69b4c7 132 p = "No Content";
133 break;
65d802fb 134 case HTTP_RESET_CONTENT:
cb69b4c7 135 p = "Reset Content";
136 break;
65d802fb 137 case HTTP_PARTIAL_CONTENT:
cb69b4c7 138 p = "Partial Content";
139 break;
65d802fb 140 case HTTP_MULTIPLE_CHOICES:
cb69b4c7 141 p = "Multiple Choices";
142 break;
65d802fb 143 case HTTP_MOVED_PERMANENTLY:
cb69b4c7 144 p = "Moved Permanently";
145 break;
65d802fb 146 case HTTP_MOVED_TEMPORARILY:
cb69b4c7 147 p = "Moved Temporarily";
148 break;
65d802fb 149 case HTTP_SEE_OTHER:
cb69b4c7 150 p = "See Other";
151 break;
65d802fb 152 case HTTP_NOT_MODIFIED:
cb69b4c7 153 p = "Not Modified";
154 break;
65d802fb 155 case HTTP_USE_PROXY:
cb69b4c7 156 p = "Use Proxy";
157 break;
65d802fb 158 case HTTP_BAD_REQUEST:
cb69b4c7 159 p = "Bad Request";
160 break;
65d802fb 161 case HTTP_UNAUTHORIZED:
cb69b4c7 162 p = "Unauthorized";
163 break;
65d802fb 164 case HTTP_PAYMENT_REQUIRED:
cb69b4c7 165 p = "Payment Required";
166 break;
65d802fb 167 case HTTP_FORBIDDEN:
cb69b4c7 168 p = "Forbidden";
169 break;
65d802fb 170 case HTTP_NOT_FOUND:
cb69b4c7 171 p = "Not Found";
172 break;
65d802fb 173 case HTTP_METHOD_NOT_ALLOWED:
cb69b4c7 174 p = "Method Not Allowed";
175 break;
65d802fb 176 case HTTP_NOT_ACCEPTABLE:
cb69b4c7 177 p = "Not Acceptable";
178 break;
65d802fb 179 case HTTP_PROXY_AUTHENTICATION_REQUIRED:
cb69b4c7 180 p = "Proxy Authentication Required";
181 break;
65d802fb 182 case HTTP_REQUEST_TIMEOUT:
cb69b4c7 183 p = "Request Time-out";
184 break;
65d802fb 185 case HTTP_CONFLICT:
cb69b4c7 186 p = "Conflict";
187 break;
65d802fb 188 case HTTP_GONE:
cb69b4c7 189 p = "Gone";
190 break;
65d802fb 191 case HTTP_LENGTH_REQUIRED:
cb69b4c7 192 p = "Length Required";
193 break;
65d802fb 194 case HTTP_PRECONDITION_FAILED:
cb69b4c7 195 p = "Precondition Failed";
196 break;
65d802fb 197 case HTTP_REQUEST_ENTITY_TOO_LARGE:
cb69b4c7 198 p = "Request Entity Too Large";
199 break;
65d802fb 200 case HTTP_REQUEST_URI_TOO_LARGE:
cb69b4c7 201 p = "Request-URI Too Large";
202 break;
65d802fb 203 case HTTP_UNSUPPORTED_MEDIA_TYPE:
cb69b4c7 204 p = "Unsupported Media Type";
205 break;
65d802fb 206 case HTTP_INTERNAL_SERVER_ERROR:
cb69b4c7 207 p = "Internal Server Error";
208 break;
65d802fb 209 case HTTP_NOT_IMPLEMENTED:
cb69b4c7 210 p = "Not Implemented";
211 break;
65d802fb 212 case HTTP_BAD_GATEWAY:
cb69b4c7 213 p = "Bad Gateway";
214 break;
65d802fb 215 case HTTP_SERVICE_UNAVAILABLE:
cb69b4c7 216 p = "Service Unavailable";
217 break;
65d802fb 218 case HTTP_GATEWAY_TIMEOUT:
cb69b4c7 219 p = "Gateway Time-out";
220 break;
65d802fb 221 case HTTP_HTTP_VERSION_NOT_SUPPORTED:
cb69b4c7 222 p = "HTTP Version not supported";
223 break;
224 default:
225 p = "Unknown";
20198c7a 226 debug(57, 0) ("Unknown HTTP status code: %d\n", status);
cb69b4c7 227 break;
228 }
229 return p;
230}