HttpResponse.cc 8.7 KB

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