HttpResponse.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "fmt.h"
  48. #include "A2STR.h"
  49. #include "CookieStorage.h"
  50. #include "AuthConfigFactory.h"
  51. #include "AuthConfig.h"
  52. #include "ChunkedDecodingStreamFilter.h"
  53. #include "error_code.h"
  54. #ifdef HAVE_ZLIB
  55. # include "GZipDecodingStreamFilter.h"
  56. #endif // HAVE_ZLIB
  57. namespace aria2 {
  58. HttpResponse::HttpResponse()
  59. : cuid_(0)
  60. {}
  61. HttpResponse::~HttpResponse() {}
  62. void HttpResponse::validateResponse() const
  63. {
  64. int statusCode = getStatusCode();
  65. if(statusCode >= 400) {
  66. return;
  67. }
  68. if(statusCode == 304) {
  69. if(!httpRequest_->conditionalRequest()) {
  70. throw DL_ABORT_EX2("Got 304 without If-Modified-Since or If-None-Match",
  71. error_code::HTTP_PROTOCOL_ERROR);
  72. }
  73. } else if(statusCode == 301 ||
  74. statusCode == 302 ||
  75. statusCode == 303 ||
  76. statusCode == 307) {
  77. if(!httpHeader_->defined(HttpHeader::LOCATION)) {
  78. throw DL_ABORT_EX2(fmt(EX_LOCATION_HEADER_REQUIRED, statusCode),
  79. error_code::HTTP_PROTOCOL_ERROR);
  80. }
  81. return;
  82. } else if(statusCode == 200 || statusCode == 206) {
  83. if(!httpHeader_->defined(HttpHeader::TRANSFER_ENCODING)) {
  84. // compare the received range against the requested range
  85. RangeHandle responseRange = httpHeader_->getRange();
  86. if(!httpRequest_->isRangeSatisfied(responseRange)) {
  87. throw DL_ABORT_EX2
  88. (fmt(EX_INVALID_RANGE_HEADER,
  89. util::itos(httpRequest_->getStartByte(), true).c_str(),
  90. util::itos(httpRequest_->getEndByte(), true).c_str(),
  91. util::uitos(httpRequest_->getEntityLength(), true).c_str(),
  92. util::itos(responseRange->getStartByte(), true).c_str(),
  93. util::itos(responseRange->getEndByte(), true).c_str(),
  94. util::uitos(responseRange->getEntityLength(), true).c_str()),
  95. error_code::CANNOT_RESUME);
  96. }
  97. }
  98. } else {
  99. throw DL_ABORT_EX2(fmt("Unexpected status %d", statusCode),
  100. error_code::HTTP_PROTOCOL_ERROR);
  101. }
  102. }
  103. std::string HttpResponse::determinFilename() const
  104. {
  105. std::string contentDisposition =
  106. util::getContentDispositionFilename
  107. (httpHeader_->getFirst(HttpHeader::CONTENT_DISPOSITION));
  108. if(contentDisposition.empty()) {
  109. std::string file = util::percentDecode(httpRequest_->getFile());
  110. if(file.empty()) {
  111. return "index.html";
  112. } else {
  113. return file;
  114. }
  115. } else {
  116. A2_LOG_INFO(fmt(MSG_CONTENT_DISPOSITION_DETECTED,
  117. cuid_,
  118. contentDisposition.c_str()));
  119. return contentDisposition;
  120. }
  121. }
  122. void HttpResponse::retrieveCookie()
  123. {
  124. Time now;
  125. std::vector<std::string> v = httpHeader_->get(HttpHeader::SET_COOKIE);
  126. for(std::vector<std::string>::const_iterator itr = v.begin(), eoi = v.end();
  127. itr != eoi; ++itr) {
  128. httpRequest_->getCookieStorage()->parseAndStore
  129. (*itr, httpRequest_->getHost(), httpRequest_->getDir(), now.getTime());
  130. }
  131. }
  132. bool HttpResponse::isRedirect() const
  133. {
  134. int statusCode = getStatusCode();
  135. return (301 == statusCode ||
  136. 302 == statusCode ||
  137. 303 == statusCode ||
  138. 307 == statusCode) &&
  139. httpHeader_->defined(HttpHeader::LOCATION);
  140. }
  141. void HttpResponse::processRedirect()
  142. {
  143. if(httpRequest_->getRequest()->redirectUri(getRedirectURI())) {
  144. A2_LOG_INFO(fmt(MSG_REDIRECT,
  145. cuid_,
  146. httpRequest_->getRequest()->getCurrentUri().c_str()));
  147. } else {
  148. throw DL_RETRY_EX
  149. (fmt("CUID#%lld - Redirect to %s failed. It may not be a valid URI.",
  150. cuid_,
  151. httpRequest_->getRequest()->getCurrentUri().c_str()));
  152. }
  153. }
  154. std::string HttpResponse::getRedirectURI() const
  155. {
  156. return httpHeader_->getFirst(HttpHeader::LOCATION);
  157. }
  158. bool HttpResponse::isTransferEncodingSpecified() const
  159. {
  160. return httpHeader_->defined(HttpHeader::TRANSFER_ENCODING);
  161. }
  162. std::string HttpResponse::getTransferEncoding() const
  163. {
  164. // TODO See TODO in getTransferEncodingStreamFilter()
  165. return httpHeader_->getFirst(HttpHeader::TRANSFER_ENCODING);
  166. }
  167. SharedHandle<StreamFilter> HttpResponse::getTransferEncodingStreamFilter() const
  168. {
  169. SharedHandle<StreamFilter> filter;
  170. // TODO Transfer-Encoding header field can contains multiple tokens. We should
  171. // parse the field and retrieve each token.
  172. if(isTransferEncodingSpecified()) {
  173. if(getTransferEncoding() == HttpHeader::CHUNKED) {
  174. filter.reset(new ChunkedDecodingStreamFilter());
  175. }
  176. }
  177. return filter;
  178. }
  179. bool HttpResponse::isContentEncodingSpecified() const
  180. {
  181. return httpHeader_->defined(HttpHeader::CONTENT_ENCODING);
  182. }
  183. const std::string& HttpResponse::getContentEncoding() const
  184. {
  185. return httpHeader_->getFirst(HttpHeader::CONTENT_ENCODING);
  186. }
  187. SharedHandle<StreamFilter> HttpResponse::getContentEncodingStreamFilter() const
  188. {
  189. SharedHandle<StreamFilter> filter;
  190. #ifdef HAVE_ZLIB
  191. if(getContentEncoding() == HttpHeader::GZIP ||
  192. getContentEncoding() == HttpHeader::DEFLATE) {
  193. filter.reset(new GZipDecodingStreamFilter());
  194. }
  195. #endif // HAVE_ZLIB
  196. return filter;
  197. }
  198. uint64_t HttpResponse::getContentLength() const
  199. {
  200. if(!httpHeader_) {
  201. return 0;
  202. } else {
  203. return httpHeader_->getRange()->getContentLength();
  204. }
  205. }
  206. uint64_t HttpResponse::getEntityLength() const
  207. {
  208. if(!httpHeader_) {
  209. return 0;
  210. } else {
  211. return httpHeader_->getRange()->getEntityLength();
  212. }
  213. }
  214. std::string HttpResponse::getContentType() const
  215. {
  216. if(!httpHeader_) {
  217. return A2STR::NIL;
  218. } else {
  219. std::pair<std::string, std::string> p;
  220. util::divide(p, httpHeader_->getFirst(HttpHeader::CONTENT_TYPE), ';');
  221. return p.first;
  222. }
  223. }
  224. void HttpResponse::setHttpHeader(const SharedHandle<HttpHeader>& httpHeader)
  225. {
  226. httpHeader_ = httpHeader;
  227. }
  228. void HttpResponse::setHttpRequest(const SharedHandle<HttpRequest>& httpRequest)
  229. {
  230. httpRequest_ = httpRequest;
  231. }
  232. int HttpResponse::getStatusCode() const
  233. {
  234. return httpHeader_->getStatusCode();
  235. }
  236. bool HttpResponse::hasRetryAfter() const
  237. {
  238. return httpHeader_->defined(HttpHeader::RETRY_AFTER);
  239. }
  240. time_t HttpResponse::getRetryAfter() const
  241. {
  242. return httpHeader_->getFirstAsUInt(HttpHeader::RETRY_AFTER);
  243. }
  244. Time HttpResponse::getLastModifiedTime() const
  245. {
  246. return Time::parseHTTPDate(httpHeader_->getFirst(HttpHeader::LAST_MODIFIED));
  247. }
  248. bool HttpResponse::supportsPersistentConnection() const
  249. {
  250. std::string connection =
  251. util::toLower(httpHeader_->getFirst(HttpHeader::CONNECTION));
  252. std::string version = httpHeader_->getVersion();
  253. return
  254. connection.find(HttpHeader::CLOSE) == std::string::npos &&
  255. (version == HttpHeader::HTTP_1_1 ||
  256. connection.find("keep-alive") != std::string::npos) &&
  257. (!httpRequest_->isProxyRequestSet() ||
  258. util::toLower(httpHeader_->getFirst("Proxy-Connection")).find("keep-alive")
  259. != std::string::npos);
  260. }
  261. } // namespace aria2