HttpResponse.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "HttpResponse.h"
  36. #include "Request.h"
  37. #include "Segment.h"
  38. #include "HttpRequest.h"
  39. #include "HttpHeader.h"
  40. #include "Range.h"
  41. #include "LogFactory.h"
  42. #include "Logger.h"
  43. #include "Util.h"
  44. #include "message.h"
  45. #include "DlAbortEx.h"
  46. #include "DlRetryEx.h"
  47. #include "StringFormat.h"
  48. #include "A2STR.h"
  49. #include "Decoder.h"
  50. #include "ChunkedDecoder.h"
  51. #ifdef HAVE_LIBZ
  52. # include "GZipDecoder.h"
  53. #endif // HAVE_LIBZ
  54. #include "CookieStorage.h"
  55. #include <deque>
  56. namespace aria2 {
  57. HttpResponse::HttpResponse():cuid(0),
  58. logger(LogFactory::getInstance())
  59. {}
  60. HttpResponse::~HttpResponse() {}
  61. void HttpResponse::validateResponse() const
  62. {
  63. const std::string& status = getResponseStatus();
  64. if(status >= HttpHeader::S400) {
  65. return;
  66. }
  67. if(status >= HttpHeader::S300) {
  68. if(!httpHeader->defined(HttpHeader::LOCATION)) {
  69. throw DlAbortEx
  70. (StringFormat(EX_LOCATION_HEADER_REQUIRED,
  71. Util::parseUInt(status)).str());
  72. }
  73. } else if(!httpHeader->defined(HttpHeader::TRANSFER_ENCODING)) {
  74. // compare the received range against the requested range
  75. RangeHandle responseRange = httpHeader->getRange();
  76. if(!httpRequest->isRangeSatisfied(responseRange)) {
  77. throw DlAbortEx
  78. (StringFormat
  79. (EX_INVALID_RANGE_HEADER,
  80. Util::itos(httpRequest->getStartByte(), true).c_str(),
  81. Util::itos(httpRequest->getEndByte(), true).c_str(),
  82. Util::uitos(httpRequest->getEntityLength(), true).c_str(),
  83. Util::itos(responseRange->getStartByte(), true).c_str(),
  84. Util::itos(responseRange->getEndByte(), true).c_str(),
  85. Util::uitos(responseRange->getEntityLength(), true).c_str()).str());
  86. }
  87. }
  88. }
  89. std::string HttpResponse::determinFilename() const
  90. {
  91. std::string contentDisposition =
  92. Util::getContentDispositionFilename
  93. (httpHeader->getFirst(HttpHeader::CONTENT_DISPOSITION));
  94. if(contentDisposition.empty()) {
  95. return Util::urldecode(httpRequest->getFile());
  96. } else {
  97. logger->info(MSG_CONTENT_DISPOSITION_DETECTED,
  98. cuid, contentDisposition.c_str());
  99. return Util::urldecode(contentDisposition);
  100. }
  101. }
  102. void HttpResponse::retrieveCookie()
  103. {
  104. std::deque<std::string> v = httpHeader->get(HttpHeader::SET_COOKIE);
  105. for(std::deque<std::string>::const_iterator itr = v.begin(); itr != v.end();
  106. ++itr) {
  107. httpRequest->getCookieStorage()->parseAndStore(*itr,
  108. httpRequest->getHost(),
  109. httpRequest->getDir());
  110. }
  111. }
  112. bool HttpResponse::isRedirect() const
  113. {
  114. const std::string& status = getResponseStatus();
  115. return HttpHeader::S300 <= status && status < HttpHeader::S400 &&
  116. httpHeader->defined(HttpHeader::LOCATION);
  117. }
  118. void HttpResponse::processRedirect()
  119. {
  120. if(httpRequest->getRequest()->redirectUrl(getRedirectURI())) {
  121. logger->info(MSG_REDIRECT, cuid,
  122. httpRequest->getRequest()->getCurrentUrl().c_str());
  123. } else {
  124. throw DlRetryEx
  125. (StringFormat("CUID#%d - Redirect to %s failed. It may not be a valid"
  126. " URI.", cuid,
  127. httpRequest->getRequest()->getCurrentUrl().c_str()).str());
  128. }
  129. }
  130. std::string HttpResponse::getRedirectURI() const
  131. {
  132. return httpHeader->getFirst(HttpHeader::LOCATION);
  133. }
  134. bool HttpResponse::isTransferEncodingSpecified() const
  135. {
  136. return httpHeader->defined(HttpHeader::TRANSFER_ENCODING);
  137. }
  138. std::string HttpResponse::getTransferEncoding() const
  139. {
  140. // TODO See TODO in getTransferEncodingDecoder()
  141. return httpHeader->getFirst(HttpHeader::TRANSFER_ENCODING);
  142. }
  143. SharedHandle<Decoder> HttpResponse::getTransferEncodingDecoder() const
  144. {
  145. // TODO Transfer-Encoding header field can contains multiple tokens. We should
  146. // parse the field and retrieve each token.
  147. if(isTransferEncodingSpecified()) {
  148. if(getTransferEncoding() == HttpHeader::CHUNKED) {
  149. return SharedHandle<Decoder>(new ChunkedDecoder());
  150. }
  151. }
  152. return SharedHandle<Decoder>();
  153. }
  154. bool HttpResponse::isContentEncodingSpecified() const
  155. {
  156. return httpHeader->defined(HttpHeader::CONTENT_ENCODING);
  157. }
  158. const std::string& HttpResponse::getContentEncoding() const
  159. {
  160. return httpHeader->getFirst(HttpHeader::CONTENT_ENCODING);
  161. }
  162. SharedHandle<Decoder> HttpResponse::getContentEncodingDecoder() const
  163. {
  164. #ifdef HAVE_LIBZ
  165. if(getContentEncoding() == HttpHeader::GZIP ||
  166. getContentEncoding() == HttpHeader::DEFLATE) {
  167. return SharedHandle<Decoder>(new GZipDecoder());
  168. }
  169. #endif // HAVE_LIBZ
  170. return SharedHandle<Decoder>();
  171. }
  172. uint64_t HttpResponse::getContentLength() const
  173. {
  174. if(httpHeader.isNull()) {
  175. return 0;
  176. } else {
  177. return httpHeader->getRange()->getContentLength();
  178. }
  179. }
  180. uint64_t HttpResponse::getEntityLength() const
  181. {
  182. if(httpHeader.isNull()) {
  183. return 0;
  184. } else {
  185. return httpHeader->getRange()->getEntityLength();
  186. }
  187. }
  188. std::string HttpResponse::getContentType() const
  189. {
  190. if(httpHeader.isNull()) {
  191. return A2STR::NIL;
  192. } else {
  193. return httpHeader->getFirst(HttpHeader::CONTENT_TYPE);
  194. }
  195. }
  196. void HttpResponse::setHttpHeader(const SharedHandle<HttpHeader>& httpHeader)
  197. {
  198. this->httpHeader = httpHeader;
  199. }
  200. SharedHandle<HttpHeader> HttpResponse::getHttpHeader() const
  201. {
  202. return httpHeader;
  203. }
  204. void HttpResponse::setHttpRequest(const SharedHandle<HttpRequest>& httpRequest)
  205. {
  206. this->httpRequest = httpRequest;
  207. }
  208. SharedHandle<HttpRequest> HttpResponse::getHttpRequest() const
  209. {
  210. return httpRequest;
  211. }
  212. // TODO return std::string
  213. const std::string& HttpResponse::getResponseStatus() const
  214. {
  215. return httpHeader->getResponseStatus();
  216. }
  217. bool HttpResponse::hasRetryAfter() const
  218. {
  219. return httpHeader->defined(HttpHeader::RETRY_AFTER);
  220. }
  221. time_t HttpResponse::getRetryAfter() const
  222. {
  223. return httpHeader->getFirstAsUInt(HttpHeader::RETRY_AFTER);
  224. }
  225. Time HttpResponse::getLastModifiedTime() const
  226. {
  227. return Time::parseHTTPDate(httpHeader->getFirst(HttpHeader::LAST_MODIFIED));
  228. }
  229. } // namespace aria2