HttpResponse.cc 8.2 KB

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