HttpResponse.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "CookieStorage.h"
  50. #include "AuthConfigFactory.h"
  51. #include "AuthConfig.h"
  52. #include "ChunkedDecodingStreamFilter.h"
  53. #ifdef HAVE_LIBZ
  54. # include "GZipDecodingStreamFilter.h"
  55. #endif // HAVE_LIBZ
  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::S304) {
  68. if(httpRequest_->getIfModifiedSinceHeader().empty()) {
  69. throw DL_ABORT_EX("Got 304 without If-Modified-Since");
  70. }
  71. } else if(status == HttpHeader::S301 ||
  72. status == HttpHeader::S302 ||
  73. status == HttpHeader::S303 ||
  74. status == HttpHeader::S307) {
  75. if(!httpHeader_->defined(HttpHeader::LOCATION)) {
  76. throw DL_ABORT_EX
  77. (StringFormat(EX_LOCATION_HEADER_REQUIRED,
  78. util::parseUInt(status)).str());
  79. }
  80. return;
  81. } else if(status == HttpHeader::S200 ||
  82. status == HttpHeader::S206) {
  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. (StringFormat
  89. (EX_INVALID_RANGE_HEADER,
  90. util::itos(httpRequest_->getStartByte(), true).c_str(),
  91. util::itos(httpRequest_->getEndByte(), true).c_str(),
  92. util::uitos(httpRequest_->getEntityLength(), true).c_str(),
  93. util::itos(responseRange->getStartByte(), true).c_str(),
  94. util::itos(responseRange->getEndByte(), true).c_str(),
  95. util::uitos(responseRange->getEntityLength(), true).c_str()).str(),
  96. downloadresultcode::CANNOT_RESUME);
  97. }
  98. }
  99. } else {
  100. throw DL_ABORT_EX
  101. (StringFormat("Unexpected status %s", status.c_str()).str());
  102. }
  103. }
  104. std::string HttpResponse::determinFilename() const
  105. {
  106. std::string contentDisposition =
  107. util::getContentDispositionFilename
  108. (httpHeader_->getFirst(HttpHeader::CONTENT_DISPOSITION));
  109. if(contentDisposition.empty()) {
  110. std::string file = util::percentDecode(httpRequest_->getFile());
  111. if(file.empty()) {
  112. return "index.html";
  113. } else {
  114. return file;
  115. }
  116. } else {
  117. if(logger_->info()) {
  118. logger_->info(MSG_CONTENT_DISPOSITION_DETECTED,
  119. util::itos(cuid_).c_str(), contentDisposition.c_str());
  120. }
  121. return contentDisposition;
  122. }
  123. }
  124. void HttpResponse::retrieveCookie()
  125. {
  126. Time now;
  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
  131. (*itr, httpRequest_->getHost(), httpRequest_->getDir(), now.getTime());
  132. }
  133. }
  134. bool HttpResponse::isRedirect() const
  135. {
  136. const std::string& status = getResponseStatus();
  137. return (HttpHeader::S301 == status ||
  138. HttpHeader::S302 == status ||
  139. HttpHeader::S303 == status ||
  140. HttpHeader::S307 == status) &&
  141. httpHeader_->defined(HttpHeader::LOCATION);
  142. }
  143. void HttpResponse::processRedirect()
  144. {
  145. if(httpRequest_->getRequest()->redirectUri(getRedirectURI())) {
  146. if(logger_->info()) {
  147. logger_->info(MSG_REDIRECT, util::itos(cuid_).c_str(),
  148. httpRequest_->getRequest()->getCurrentUri().c_str());
  149. }
  150. } else {
  151. throw DL_RETRY_EX
  152. (StringFormat("CUID#%s - Redirect to %s failed. It may not be a valid"
  153. " URI.", util::itos(cuid_).c_str(),
  154. httpRequest_->getRequest()->getCurrentUri().c_str()).str());
  155. }
  156. }
  157. std::string HttpResponse::getRedirectURI() const
  158. {
  159. return httpHeader_->getFirst(HttpHeader::LOCATION);
  160. }
  161. bool HttpResponse::isTransferEncodingSpecified() const
  162. {
  163. return httpHeader_->defined(HttpHeader::TRANSFER_ENCODING);
  164. }
  165. std::string HttpResponse::getTransferEncoding() const
  166. {
  167. // TODO See TODO in getTransferEncodingStreamFilter()
  168. return httpHeader_->getFirst(HttpHeader::TRANSFER_ENCODING);
  169. }
  170. SharedHandle<StreamFilter> HttpResponse::getTransferEncodingStreamFilter() const
  171. {
  172. SharedHandle<StreamFilter> filter;
  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. filter.reset(new ChunkedDecodingStreamFilter());
  178. }
  179. }
  180. return filter;
  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<StreamFilter> HttpResponse::getContentEncodingStreamFilter() const
  191. {
  192. SharedHandle<StreamFilter> filter;
  193. #ifdef HAVE_LIBZ
  194. if(getContentEncoding() == HttpHeader::GZIP ||
  195. getContentEncoding() == HttpHeader::DEFLATE) {
  196. filter.reset(new GZipDecodingStreamFilter());
  197. }
  198. #endif // HAVE_LIBZ
  199. return filter;
  200. }
  201. uint64_t HttpResponse::getContentLength() const
  202. {
  203. if(!httpHeader_) {
  204. return 0;
  205. } else {
  206. return httpHeader_->getRange()->getContentLength();
  207. }
  208. }
  209. uint64_t HttpResponse::getEntityLength() const
  210. {
  211. if(!httpHeader_) {
  212. return 0;
  213. } else {
  214. return httpHeader_->getRange()->getEntityLength();
  215. }
  216. }
  217. std::string HttpResponse::getContentType() const
  218. {
  219. if(!httpHeader_) {
  220. return A2STR::NIL;
  221. } else {
  222. std::pair<std::string, std::string> p;
  223. util::divide(p, httpHeader_->getFirst(HttpHeader::CONTENT_TYPE), ';');
  224. return p.first;
  225. }
  226. }
  227. void HttpResponse::setHttpHeader(const SharedHandle<HttpHeader>& httpHeader)
  228. {
  229. httpHeader_ = httpHeader;
  230. }
  231. void HttpResponse::setHttpRequest(const SharedHandle<HttpRequest>& httpRequest)
  232. {
  233. httpRequest_ = httpRequest;
  234. }
  235. // TODO return std::string
  236. const std::string& HttpResponse::getResponseStatus() const
  237. {
  238. return httpHeader_->getResponseStatus();
  239. }
  240. bool HttpResponse::hasRetryAfter() const
  241. {
  242. return httpHeader_->defined(HttpHeader::RETRY_AFTER);
  243. }
  244. time_t HttpResponse::getRetryAfter() const
  245. {
  246. return httpHeader_->getFirstAsUInt(HttpHeader::RETRY_AFTER);
  247. }
  248. Time HttpResponse::getLastModifiedTime() const
  249. {
  250. return Time::parseHTTPDate(httpHeader_->getFirst(HttpHeader::LAST_MODIFIED));
  251. }
  252. bool HttpResponse::supportsPersistentConnection() const
  253. {
  254. std::string connection =
  255. util::toLower(httpHeader_->getFirst(HttpHeader::CONNECTION));
  256. std::string version = httpHeader_->getVersion();
  257. return
  258. connection.find(HttpHeader::CLOSE) == std::string::npos &&
  259. (version == HttpHeader::HTTP_1_1 ||
  260. connection.find("keep-alive") != std::string::npos) &&
  261. (!httpRequest_->isProxyRequestSet() ||
  262. util::toLower(httpHeader_->getFirst("Proxy-Connection")).find("keep-alive")
  263. != std::string::npos);
  264. }
  265. } // namespace aria2