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(const HTTPResponse& other):
144 HTTPMessage(other),
145 _status(other._status),
146 _reason(other._reason)
147{
148}
149
150
151HTTPResponse::~HTTPResponse()
152{
153}
154
155
156HTTPResponse& HTTPResponse::operator = (const HTTPResponse& other)
157{
158 if (this != &other)
159 {
160 HTTPMessage::operator = (other);
161 _status = other._status;
162 _reason = other._reason;
163 }
164 return *this;
165}
166
167
168void HTTPResponse::setStatus(HTTPStatus status)
169{
170 _status = status;
171}
172
173
174void HTTPResponse::setStatus(const std::string& status)
175{
176 setStatus((HTTPStatus) NumberParser::parse(status));
177}
178
179
180void HTTPResponse::setReason(const std::string& reason)
181{
182 _reason = reason;
183}
184
185
186void HTTPResponse::setStatusAndReason(HTTPStatus status, const std::string& reason)
187{
188 _status = status;
189 _reason = reason;
190}
191
192
193void HTTPResponse::setStatusAndReason(HTTPStatus status)
194{
195 setStatusAndReason(status, getReasonForStatus(status));
196}
197
198
199void HTTPResponse::setDate(const Poco::Timestamp& dateTime)
200{
201 set(DATE, DateTimeFormatter::format(dateTime, DateTimeFormat::HTTP_FORMAT));
202}
203
204
205Poco::Timestamp HTTPResponse::getDate() const
206{
207 const std::string& dateTime = get(DATE);
208 int tzd;
209 return DateTimeParser::parse(dateTime, tzd).timestamp();
210}
211
212
213void HTTPResponse::addCookie(const HTTPCookie& cookie)
214{
215 add(SET_COOKIE, cookie.toString());
216}
217
218
219void HTTPResponse::getCookies(std::vector<HTTPCookie>& cookies) const
220{
221 cookies.clear();
222 NameValueCollection::ConstIterator it = find(SET_COOKIE);
223 while (it != end() && Poco::icompare(it->first, SET_COOKIE) == 0)
224 {
225 NameValueCollection nvc;
226 splitParameters(it->second.begin(), it->second.end(), nvc);
227 cookies.push_back(HTTPCookie(nvc));
228 ++it;
229 }
230}
231
232
233void HTTPResponse::write(std::ostream& ostr) const
234{
235 ostr << getVersion() << " " << static_cast<int>(_status) << " " << _reason << "\r\n";
236 HTTPMessage::write(ostr);
237 ostr << "\r\n";
238}
239
240
241void HTTPResponse::read(std::istream& istr)
242{
243 static const int eof = std::char_traits<char>::eof();
244
245 std::string version;
246 std::string status;
247 std::string reason;
248
249 int ch = istr.get();
250 if (istr.bad()) throw NetException("Error reading HTTP response header");
251 if (ch == eof) throw NoMessageException();
252 while (Poco::Ascii::isSpace(ch)) ch = istr.get();
253 if (ch == eof) throw MessageException("No HTTP response header");
254 while (!Poco::Ascii::isSpace(ch) && ch != eof && version.length() < MAX_VERSION_LENGTH) { version += (char) ch; ch = istr.get(); }
255 if (!Poco::Ascii::isSpace(ch)) throw MessageException("Invalid HTTP version string");
256 while (Poco::Ascii::isSpace(ch)) ch = istr.get();
257 while (!Poco::Ascii::isSpace(ch) && ch != eof && status.length() < MAX_STATUS_LENGTH) { status += (char) ch; ch = istr.get(); }
258 if (!Poco::Ascii::isSpace(ch)) throw MessageException("Invalid HTTP status code");
259 while (Poco::Ascii::isSpace(ch) && ch != '\r' && ch != '\n' && ch != eof) ch = istr.get();
260 while (ch != '\r' && ch != '\n' && ch != eof && reason.length() < MAX_REASON_LENGTH) { reason += (char) ch; ch = istr.get(); }
261 if (!Poco::Ascii::isSpace(ch)) throw MessageException("HTTP reason string too long");
262 if (ch == '\r') ch = istr.get();
263 if (ch != '\n') throw MessageException("Unterminated HTTP response line");
264
265 HTTPMessage::read(istr);
266 ch = istr.get();
267 while (ch != '\n' && ch != eof) { ch = istr.get(); }
268 setVersion(version);
269 setStatus(status);
270 setReason(reason);
271}
272
273
274const std::string& HTTPResponse::getReasonForStatus(HTTPStatus status)
275{
276 switch (status)
277 {
278 case HTTP_CONTINUE:
279 return HTTP_REASON_CONTINUE;
280 case HTTP_SWITCHING_PROTOCOLS:
281 return HTTP_REASON_SWITCHING_PROTOCOLS;
282 case HTTP_PROCESSING:
283 return HTTP_REASON_PROCESSING;
284 case HTTP_OK:
285 return HTTP_REASON_OK;
286 case HTTP_CREATED:
287 return HTTP_REASON_CREATED;
288 case HTTP_ACCEPTED:
289 return HTTP_REASON_ACCEPTED;
290 case HTTP_NONAUTHORITATIVE:
291 return HTTP_REASON_NONAUTHORITATIVE;
292 case HTTP_NO_CONTENT:
293 return HTTP_REASON_NO_CONTENT;
294 case HTTP_RESET_CONTENT:
295 return HTTP_REASON_RESET_CONTENT;
296 case HTTP_PARTIAL_CONTENT:
297 return HTTP_REASON_PARTIAL_CONTENT;
298 case HTTP_MULTI_STATUS:
299 return HTTP_REASON_MULTI_STATUS;
300 case HTTP_ALREADY_REPORTED:
301 return HTTP_REASON_ALREADY_REPORTED;
302 case HTTP_IM_USED:
303 return HTTP_REASON_IM_USED;
304 case HTTP_MULTIPLE_CHOICES:
305 return HTTP_REASON_MULTIPLE_CHOICES;
306 case HTTP_MOVED_PERMANENTLY:
307 return HTTP_REASON_MOVED_PERMANENTLY;
308 case HTTP_FOUND:
309 return HTTP_REASON_FOUND;
310 case HTTP_SEE_OTHER:
311 return HTTP_REASON_SEE_OTHER;
312 case HTTP_NOT_MODIFIED:
313 return HTTP_REASON_NOT_MODIFIED;
314 case HTTP_USE_PROXY:
315 return HTTP_REASON_USE_PROXY;
316 case HTTP_TEMPORARY_REDIRECT:
317 return HTTP_REASON_TEMPORARY_REDIRECT;
318 case HTTP_BAD_REQUEST:
319 return HTTP_REASON_BAD_REQUEST;
320 case HTTP_UNAUTHORIZED:
321 return HTTP_REASON_UNAUTHORIZED;
322 case HTTP_PAYMENT_REQUIRED:
323 return HTTP_REASON_PAYMENT_REQUIRED;
324 case HTTP_FORBIDDEN:
325 return HTTP_REASON_FORBIDDEN;
326 case HTTP_NOT_FOUND:
327 return HTTP_REASON_NOT_FOUND;
328 case HTTP_METHOD_NOT_ALLOWED:
329 return HTTP_REASON_METHOD_NOT_ALLOWED;
330 case HTTP_NOT_ACCEPTABLE:
331 return HTTP_REASON_NOT_ACCEPTABLE;
332 case HTTP_PROXY_AUTHENTICATION_REQUIRED:
333 return HTTP_REASON_PROXY_AUTHENTICATION_REQUIRED;
334 case HTTP_REQUEST_TIMEOUT:
335 return HTTP_REASON_REQUEST_TIMEOUT;
336 case HTTP_CONFLICT:
337 return HTTP_REASON_CONFLICT;
338 case HTTP_GONE:
339 return HTTP_REASON_GONE;
340 case HTTP_LENGTH_REQUIRED:
341 return HTTP_REASON_LENGTH_REQUIRED;
342 case HTTP_PRECONDITION_FAILED:
343 return HTTP_REASON_PRECONDITION_FAILED;
344 case HTTP_REQUEST_ENTITY_TOO_LARGE:
345 return HTTP_REASON_REQUEST_ENTITY_TOO_LARGE;
346 case HTTP_REQUEST_URI_TOO_LONG:
347 return HTTP_REASON_REQUEST_URI_TOO_LONG;
348 case HTTP_UNSUPPORTED_MEDIA_TYPE:
349 return HTTP_REASON_UNSUPPORTED_MEDIA_TYPE;
350 case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE:
351 return HTTP_REASON_REQUESTED_RANGE_NOT_SATISFIABLE;
352 case HTTP_EXPECTATION_FAILED:
353 return HTTP_REASON_EXPECTATION_FAILED;
354 case HTTP_IM_A_TEAPOT:
355 return HTTP_REASON_IM_A_TEAPOT;
356 case HTTP_ENCHANCE_YOUR_CALM:
357 return HTTP_REASON_ENCHANCE_YOUR_CALM;
358 case HTTP_MISDIRECTED_REQUEST:
359 return HTTP_REASON_MISDIRECTED_REQUEST;
360 case HTTP_UNPROCESSABLE_ENTITY:
361 return HTTP_REASON_UNPROCESSABLE_ENTITY;
362 case HTTP_LOCKED:
363 return HTTP_REASON_LOCKED;
364 case HTTP_FAILED_DEPENDENCY:
365 return HTTP_REASON_FAILED_DEPENDENCY;
366 case HTTP_UPGRADE_REQUIRED:
367 return HTTP_REASON_UPGRADE_REQUIRED;
368 case HTTP_PRECONDITION_REQUIRED:
369 return HTTP_REASON_PRECONDITION_REQUIRED;
370 case HTTP_TOO_MANY_REQUESTS:
371 return HTTP_REASON_TOO_MANY_REQUESTS;
372 case HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE:
373 return HTTP_REASON_REQUEST_HEADER_FIELDS_TOO_LARGE;
374 case HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
375 return HTTP_REASON_UNAVAILABLE_FOR_LEGAL_REASONS;
376 case HTTP_INTERNAL_SERVER_ERROR:
377 return HTTP_REASON_INTERNAL_SERVER_ERROR;
378 case HTTP_NOT_IMPLEMENTED:
379 return HTTP_REASON_NOT_IMPLEMENTED;
380 case HTTP_BAD_GATEWAY:
381 return HTTP_REASON_BAD_GATEWAY;
382 case HTTP_SERVICE_UNAVAILABLE:
383 return HTTP_REASON_SERVICE_UNAVAILABLE;
384 case HTTP_GATEWAY_TIMEOUT:
385 return HTTP_REASON_GATEWAY_TIMEOUT;
386 case HTTP_VERSION_NOT_SUPPORTED:
387 return HTTP_REASON_VERSION_NOT_SUPPORTED;
388 case HTTP_VARIANT_ALSO_NEGOTIATES:
389 return HTTP_REASON_VARIANT_ALSO_NEGOTIATES;
390 case HTTP_INSUFFICIENT_STORAGE:
391 return HTTP_REASON_INSUFFICIENT_STORAGE;
392 case HTTP_LOOP_DETECTED:
393 return HTTP_REASON_LOOP_DETECTED;
394 case HTTP_NOT_EXTENDED:
395 return HTTP_REASON_NOT_EXTENDED;
396 case HTTP_NETWORK_AUTHENTICATION_REQUIRED:
397 return HTTP_REASON_NETWORK_AUTHENTICATION_REQUIRED;
398 default:
399 return HTTP_REASON_UNKNOWN;
400 }
401}
402
403
404} } // namespace Poco::Net
405