HTTPResponse.cpp 15 KB

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