1//
2// HTTPResponse.cpp
3//
4// Library: Net
5// Package: HTTP
6// Module: HTTPResponse
7//
8// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Net/HTTPResponse.h"
16#include "Poco/Net/NetException.h"
17#include "Poco/NumberFormatter.h"
18#include "Poco/NumberParser.h"
19#include "Poco/DateTime.h"
20#include "Poco/DateTimeFormatter.h"
21#include "Poco/DateTimeFormat.h"
22#include "Poco/DateTimeParser.h"
23#include "Poco/Ascii.h"
24#include "Poco/String.h"
25
26
27using Poco::DateTime;
28using Poco::NumberFormatter;
29using Poco::NumberParser;
30using Poco::DateTimeFormatter;
31using Poco::DateTimeFormat;
32using Poco::DateTimeParser;
33
34
35namespace Poco {
36namespace Net {
37
38
39const std::string HTTPResponse::HTTP_REASON_CONTINUE = "Continue";
40const std::string HTTPResponse::HTTP_REASON_SWITCHING_PROTOCOLS = "Switching Protocols";
41const std::string HTTPResponse::HTTP_REASON_PROCESSING = "Processing";
42const std::string HTTPResponse::HTTP_REASON_OK = "OK";
43const std::string HTTPResponse::HTTP_REASON_CREATED = "Created";
44const std::string HTTPResponse::HTTP_REASON_ACCEPTED = "Accepted";
45const std::string HTTPResponse::HTTP_REASON_NONAUTHORITATIVE = "Non-Authoritative Information";
46const std::string HTTPResponse::HTTP_REASON_NO_CONTENT = "No Content";
47const std::string HTTPResponse::HTTP_REASON_RESET_CONTENT = "Reset Content";
48const std::string HTTPResponse::HTTP_REASON_PARTIAL_CONTENT = "Partial Content";
49const std::string HTTPResponse::HTTP_REASON_MULTI_STATUS = "Multi Status";
50const std::string HTTPResponse::HTTP_REASON_ALREADY_REPORTED = "Already Reported";
51const std::string HTTPResponse::HTTP_REASON_IM_USED = "IM Used";
52const std::string HTTPResponse::HTTP_REASON_MULTIPLE_CHOICES = "Multiple Choices";
53const std::string HTTPResponse::HTTP_REASON_MOVED_PERMANENTLY = "Moved Permanently";
54const std::string HTTPResponse::HTTP_REASON_FOUND = "Found";
55const std::string HTTPResponse::HTTP_REASON_SEE_OTHER = "See Other";
56const std::string HTTPResponse::HTTP_REASON_NOT_MODIFIED = "Not Modified";
57const std::string HTTPResponse::HTTP_REASON_USE_PROXY = "Use Proxy";
58const std::string HTTPResponse::HTTP_REASON_TEMPORARY_REDIRECT = "Temporary Redirect";
59const std::string HTTPResponse::HTTP_REASON_PERMANENT_REDIRECT = "Permanent Redirect";
60const std::string HTTPResponse::HTTP_REASON_BAD_REQUEST = "Bad Request";
61const std::string HTTPResponse::HTTP_REASON_UNAUTHORIZED = "Unauthorized";
62const std::string HTTPResponse::HTTP_REASON_PAYMENT_REQUIRED = "Payment Required";
63const std::string HTTPResponse::HTTP_REASON_FORBIDDEN = "Forbidden";
64const std::string HTTPResponse::HTTP_REASON_NOT_FOUND = "Not Found";
65const std::string HTTPResponse::HTTP_REASON_METHOD_NOT_ALLOWED = "Method Not Allowed";
66const std::string HTTPResponse::HTTP_REASON_NOT_ACCEPTABLE = "Not Acceptable";
67const std::string HTTPResponse::HTTP_REASON_PROXY_AUTHENTICATION_REQUIRED = "Proxy Authentication Required";
68const std::string HTTPResponse::HTTP_REASON_REQUEST_TIMEOUT = "Request Time-out";
69const std::string HTTPResponse::HTTP_REASON_CONFLICT = "Conflict";
70const std::string HTTPResponse::HTTP_REASON_GONE = "Gone";
71const std::string HTTPResponse::HTTP_REASON_LENGTH_REQUIRED = "Length Required";
72const std::string HTTPResponse::HTTP_REASON_PRECONDITION_FAILED = "Precondition Failed";
73const std::string HTTPResponse::HTTP_REASON_REQUEST_ENTITY_TOO_LARGE = "Request Entity Too Large";
74const std::string HTTPResponse::HTTP_REASON_REQUEST_URI_TOO_LONG = "Request-URI Too Large";
75const std::string HTTPResponse::HTTP_REASON_UNSUPPORTED_MEDIA_TYPE = "Unsupported Media Type";
76const std::string HTTPResponse::HTTP_REASON_REQUESTED_RANGE_NOT_SATISFIABLE = "Requested Range Not Satisfiable";
77const std::string HTTPResponse::HTTP_REASON_EXPECTATION_FAILED = "Expectation Failed";
78const std::string HTTPResponse::HTTP_REASON_IM_A_TEAPOT = "I'm a Teapot";
79const std::string HTTPResponse::HTTP_REASON_ENCHANCE_YOUR_CALM = "Enchance Your Calm";
80const std::string HTTPResponse::HTTP_REASON_MISDIRECTED_REQUEST = "Misdirected Request";
81const std::string HTTPResponse::HTTP_REASON_UNPROCESSABLE_ENTITY = "Unprocessable Entity";
82const std::string HTTPResponse::HTTP_REASON_LOCKED = "Locked";
83const std::string HTTPResponse::HTTP_REASON_FAILED_DEPENDENCY = "Failed Dependency";
84const std::string HTTPResponse::HTTP_REASON_UPGRADE_REQUIRED = "Upgrade Required";
85const std::string HTTPResponse::HTTP_REASON_PRECONDITION_REQUIRED = "Precondition Required";
86const std::string HTTPResponse::HTTP_REASON_TOO_MANY_REQUESTS = "Too Many Requests";
87const std::string HTTPResponse::HTTP_REASON_REQUEST_HEADER_FIELDS_TOO_LARGE = "Request Header Fields Too Large";
88const std::string HTTPResponse::HTTP_REASON_UNAVAILABLE_FOR_LEGAL_REASONS = "Unavailable For Legal Reasons";
89const std::string HTTPResponse::HTTP_REASON_INTERNAL_SERVER_ERROR = "Internal Server Error";
90const std::string HTTPResponse::HTTP_REASON_NOT_IMPLEMENTED = "Not Implemented";
91const std::string HTTPResponse::HTTP_REASON_BAD_GATEWAY = "Bad Gateway";
92const std::string HTTPResponse::HTTP_REASON_SERVICE_UNAVAILABLE = "Service Unavailable";
93const std::string HTTPResponse::HTTP_REASON_GATEWAY_TIMEOUT = "Gateway Time-Out";
94const std::string HTTPResponse::HTTP_REASON_VERSION_NOT_SUPPORTED = "HTTP Version Not Supported";
95const std::string HTTPResponse::HTTP_REASON_VARIANT_ALSO_NEGOTIATES = "Variant Also Negotiates";
96const std::string HTTPResponse::HTTP_REASON_INSUFFICIENT_STORAGE = "Insufficient Storage";
97const std::string HTTPResponse::HTTP_REASON_LOOP_DETECTED = "Loop Detected";
98const std::string HTTPResponse::HTTP_REASON_NOT_EXTENDED = "Not Extended";
99const std::string HTTPResponse::HTTP_REASON_NETWORK_AUTHENTICATION_REQUIRED = "Network Authentication Required";
100const std::string HTTPResponse::HTTP_REASON_UNKNOWN = "???";
101const std::string HTTPResponse::DATE = "Date";
102const std::string HTTPResponse::SET_COOKIE = "Set-Cookie";
103
104
105HTTPResponse::HTTPResponse():
106 _status(HTTP_OK),
107 _reason(getReasonForStatus(HTTP_OK))
108{
109}
110
111
112HTTPResponse::HTTPResponse(HTTPStatus status, const std::string& reason):
113 _status(status),
114 _reason(reason)
115{
116}
117
118
119
120HTTPResponse::HTTPResponse(const std::string& version, HTTPStatus status, const std::string& reason):
121 HTTPMessage(version),
122 _status(status),
123 _reason(reason)
124{
125}
126
127
128HTTPResponse::HTTPResponse(HTTPStatus status):
129 _status(status),
130 _reason(getReasonForStatus(status))
131{
132}
133
134
135HTTPResponse::HTTPResponse(const std::string& version, HTTPStatus status):
136 HTTPMessage(version),
137 _status(status),
138 _reason(getReasonForStatus(status))
139{
140}
141
142
143HTTPResponse::~HTTPResponse()
144{
145}
146
147
148void HTTPResponse::setStatus(HTTPStatus status)
149{
150 _status = status;
151}
152
153
154void HTTPResponse::setStatus(const std::string& status)
155{
156 setStatus((HTTPStatus) NumberParser::parse(status));
157}
158
159
160void HTTPResponse::setReason(const std::string& reason)
161{
162 _reason = reason;
163}
164
165
166void HTTPResponse::setStatusAndReason(HTTPStatus status, const std::string& reason)
167{
168 _status = status;
169 _reason = reason;
170}
171
172
173void HTTPResponse::setStatusAndReason(HTTPStatus status)
174{
175 setStatusAndReason(status, getReasonForStatus(status));
176}
177
178
179void HTTPResponse::setDate(const Poco::Timestamp& dateTime)
180{
181 set(DATE, DateTimeFormatter::format(dateTime, DateTimeFormat::HTTP_FORMAT));
182}
183
184
185Poco::Timestamp HTTPResponse::getDate() const
186{
187 const std::string& dateTime = get(DATE);
188 int tzd;
189 return DateTimeParser::parse(dateTime, tzd).timestamp();
190}
191
192
193void HTTPResponse::addCookie(const HTTPCookie& cookie)
194{
195 add(SET_COOKIE, cookie.toString());
196}
197
198
199void HTTPResponse::getCookies(std::vector<HTTPCookie>& cookies) const
200{
201 cookies.clear();
202 NameValueCollection::ConstIterator it = find(SET_COOKIE);
203 while (it != end() && Poco::icompare(it->first, SET_COOKIE) == 0)
204 {
205 NameValueCollection nvc;
206 splitParameters(it->second.begin(), it->second.end(), nvc);
207 cookies.push_back(HTTPCookie(nvc));
208 ++it;
209 }
210}
211
212
213void HTTPResponse::write(std::ostream& ostr) const
214{
215 beginWrite(ostr);
216 ostr << "\r\n";
217}
218
219
220void HTTPResponse::beginWrite(std::ostream& ostr) const
221{
222 ostr << getVersion() << " " << static_cast<int>(_status) << " " << _reason << "\r\n";
223 HTTPMessage::write(ostr);
224}
225
226
227void HTTPResponse::read(std::istream& istr)
228{
229 static const int eof = std::char_traits<char>::eof();
230
231 std::string version;
232 std::string status;
233 std::string reason;
234
235 int ch = istr.get();
236 if (istr.bad()) throw NetException("Error reading HTTP response header");
237 if (ch == eof) throw NoMessageException();
238 while (Poco::Ascii::isSpace(ch)) ch = istr.get();
239 if (ch == eof) throw MessageException("No HTTP response header");
240 while (!Poco::Ascii::isSpace(ch) && ch != eof && version.length() < MAX_VERSION_LENGTH) { version += (char) ch; ch = istr.get(); }
241 if (!Poco::Ascii::isSpace(ch)) throw MessageException("Invalid HTTP version string");
242 while (Poco::Ascii::isSpace(ch)) ch = istr.get();
243 while (!Poco::Ascii::isSpace(ch) && ch != eof && status.length() < MAX_STATUS_LENGTH) { status += (char) ch; ch = istr.get(); }
244 if (!Poco::Ascii::isSpace(ch)) throw MessageException("Invalid HTTP status code");
245 while (Poco::Ascii::isSpace(ch) && ch != '\r' && ch != '\n' && ch != eof) ch = istr.get();
246 while (ch != '\r' && ch != '\n' && ch != eof && reason.length() < MAX_REASON_LENGTH) { reason += (char) ch; ch = istr.get(); }
247 if (!Poco::Ascii::isSpace(ch)) throw MessageException("HTTP reason string too long");
248 if (ch == '\r') ch = istr.get();
249 if (ch != '\n') throw MessageException("Unterminated HTTP response line");
250
251 HTTPMessage::read(istr);
252 ch = istr.get();
253 while (ch != '\n' && ch != eof) { ch = istr.get(); }
254 setVersion(version);
255 setStatus(status);
256 setReason(reason);
257}
258
259
260const std::string& HTTPResponse::getReasonForStatus(HTTPStatus status)
261{
262 switch (status)
263 {
264 case HTTP_CONTINUE:
265 return HTTP_REASON_CONTINUE;
266 case HTTP_SWITCHING_PROTOCOLS:
267 return HTTP_REASON_SWITCHING_PROTOCOLS;
268 case HTTP_PROCESSING:
269 return HTTP_REASON_PROCESSING;
270 case HTTP_OK:
271 return HTTP_REASON_OK;
272 case HTTP_CREATED:
273 return HTTP_REASON_CREATED;
274 case HTTP_ACCEPTED:
275 return HTTP_REASON_ACCEPTED;
276 case HTTP_NONAUTHORITATIVE:
277 return HTTP_REASON_NONAUTHORITATIVE;
278 case HTTP_NO_CONTENT:
279 return HTTP_REASON_NO_CONTENT;
280 case HTTP_RESET_CONTENT:
281 return HTTP_REASON_RESET_CONTENT;
282 case HTTP_PARTIAL_CONTENT:
283 return HTTP_REASON_PARTIAL_CONTENT;
284 case HTTP_MULTI_STATUS:
285 return HTTP_REASON_MULTI_STATUS;
286 case HTTP_ALREADY_REPORTED:
287 return HTTP_REASON_ALREADY_REPORTED;
288 case HTTP_IM_USED:
289 return HTTP_REASON_IM_USED;
290 case HTTP_MULTIPLE_CHOICES:
291 return HTTP_REASON_MULTIPLE_CHOICES;
292 case HTTP_MOVED_PERMANENTLY:
293 return HTTP_REASON_MOVED_PERMANENTLY;
294 case HTTP_FOUND:
295 return HTTP_REASON_FOUND;
296 case HTTP_SEE_OTHER:
297 return HTTP_REASON_SEE_OTHER;
298 case HTTP_NOT_MODIFIED:
299 return HTTP_REASON_NOT_MODIFIED;
300 case HTTP_USE_PROXY:
301 return HTTP_REASON_USE_PROXY;
302 case HTTP_TEMPORARY_REDIRECT:
303 return HTTP_REASON_TEMPORARY_REDIRECT;
304 case HTTP_BAD_REQUEST:
305 return HTTP_REASON_BAD_REQUEST;
306 case HTTP_UNAUTHORIZED:
307 return HTTP_REASON_UNAUTHORIZED;
308 case HTTP_PAYMENT_REQUIRED:
309 return HTTP_REASON_PAYMENT_REQUIRED;
310 case HTTP_FORBIDDEN:
311 return HTTP_REASON_FORBIDDEN;
312 case HTTP_NOT_FOUND:
313 return HTTP_REASON_NOT_FOUND;
314 case HTTP_METHOD_NOT_ALLOWED:
315 return HTTP_REASON_METHOD_NOT_ALLOWED;
316 case HTTP_NOT_ACCEPTABLE:
317 return HTTP_REASON_NOT_ACCEPTABLE;
318 case HTTP_PROXY_AUTHENTICATION_REQUIRED:
319 return HTTP_REASON_PROXY_AUTHENTICATION_REQUIRED;
320 case HTTP_REQUEST_TIMEOUT:
321 return HTTP_REASON_REQUEST_TIMEOUT;
322 case HTTP_CONFLICT:
323 return HTTP_REASON_CONFLICT;
324 case HTTP_GONE:
325 return HTTP_REASON_GONE;
326 case HTTP_LENGTH_REQUIRED:
327 return HTTP_REASON_LENGTH_REQUIRED;
328 case HTTP_PRECONDITION_FAILED:
329 return HTTP_REASON_PRECONDITION_FAILED;
330 case HTTP_REQUEST_ENTITY_TOO_LARGE:
331 return HTTP_REASON_REQUEST_ENTITY_TOO_LARGE;
332 case HTTP_REQUEST_URI_TOO_LONG:
333 return HTTP_REASON_REQUEST_URI_TOO_LONG;
334 case HTTP_UNSUPPORTED_MEDIA_TYPE:
335 return HTTP_REASON_UNSUPPORTED_MEDIA_TYPE;
336 case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE:
337 return HTTP_REASON_REQUESTED_RANGE_NOT_SATISFIABLE;
338 case HTTP_EXPECTATION_FAILED:
339 return HTTP_REASON_EXPECTATION_FAILED;
340 case HTTP_IM_A_TEAPOT:
341 return HTTP_REASON_IM_A_TEAPOT;
342 case HTTP_ENCHANCE_YOUR_CALM:
343 return HTTP_REASON_ENCHANCE_YOUR_CALM;
344 case HTTP_MISDIRECTED_REQUEST:
345 return HTTP_REASON_MISDIRECTED_REQUEST;
346 case HTTP_UNPROCESSABLE_ENTITY:
347 return HTTP_REASON_UNPROCESSABLE_ENTITY;
348 case HTTP_LOCKED:
349 return HTTP_REASON_LOCKED;
350 case HTTP_FAILED_DEPENDENCY:
351 return HTTP_REASON_FAILED_DEPENDENCY;
352 case HTTP_UPGRADE_REQUIRED:
353 return HTTP_REASON_UPGRADE_REQUIRED;
354 case HTTP_PRECONDITION_REQUIRED:
355 return HTTP_REASON_PRECONDITION_REQUIRED;
356 case HTTP_TOO_MANY_REQUESTS:
357 return HTTP_REASON_TOO_MANY_REQUESTS;
358 case HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE:
359 return HTTP_REASON_REQUEST_HEADER_FIELDS_TOO_LARGE;
360 case HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
361 return HTTP_REASON_UNAVAILABLE_FOR_LEGAL_REASONS;
362 case HTTP_INTERNAL_SERVER_ERROR:
363 return HTTP_REASON_INTERNAL_SERVER_ERROR;
364 case HTTP_NOT_IMPLEMENTED:
365 return HTTP_REASON_NOT_IMPLEMENTED;
366 case HTTP_BAD_GATEWAY:
367 return HTTP_REASON_BAD_GATEWAY;
368 case HTTP_SERVICE_UNAVAILABLE:
369 return HTTP_REASON_SERVICE_UNAVAILABLE;
370 case HTTP_GATEWAY_TIMEOUT:
371 return HTTP_REASON_GATEWAY_TIMEOUT;
372 case HTTP_VERSION_NOT_SUPPORTED:
373 return HTTP_REASON_VERSION_NOT_SUPPORTED;
374 case HTTP_VARIANT_ALSO_NEGOTIATES:
375 return HTTP_REASON_VARIANT_ALSO_NEGOTIATES;
376 case HTTP_INSUFFICIENT_STORAGE:
377 return HTTP_REASON_INSUFFICIENT_STORAGE;
378 case HTTP_LOOP_DETECTED:
379 return HTTP_REASON_LOOP_DETECTED;
380 case HTTP_NOT_EXTENDED:
381 return HTTP_REASON_NOT_EXTENDED;
382 case HTTP_NETWORK_AUTHENTICATION_REQUIRED:
383 return HTTP_REASON_NETWORK_AUTHENTICATION_REQUIRED;
384 default:
385 return HTTP_REASON_UNKNOWN;
386 }
387}
388
389
390} } // namespace Poco::Net
391