HttpResponse.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <deque>
  37. #include "Request.h"
  38. #include "Segment.h"
  39. #include "HttpRequest.h"
  40. #include "HttpHeader.h"
  41. #include "Range.h"
  42. #include "LogFactory.h"
  43. #include "Logger.h"
  44. #include "Util.h"
  45. #include "message.h"
  46. #include "DlAbortEx.h"
  47. #include "DlRetryEx.h"
  48. #include "StringFormat.h"
  49. #include "A2STR.h"
  50. #include "Decoder.h"
  51. #include "ChunkedDecoder.h"
  52. #ifdef HAVE_LIBZ
  53. # include "GZipDecoder.h"
  54. #endif // HAVE_LIBZ
  55. #include "CookieStorage.h"
  56. #include "AuthConfigFactory.h"
  57. #include "AuthConfig.h"
  58. namespace aria2 {
  59. HttpResponse::HttpResponse():cuid(0),
  60. logger(LogFactory::getInstance())
  61. {}
  62. HttpResponse::~HttpResponse() {}
  63. void HttpResponse::validateResponse() const
  64. {
  65. const std::string& status = getResponseStatus();
  66. if(status >= HttpHeader::S400) {
  67. return;
  68. }
  69. if(status >= HttpHeader::S300) {
  70. if(!httpHeader->defined(HttpHeader::LOCATION)) {
  71. throw DlAbortEx
  72. (StringFormat(EX_LOCATION_HEADER_REQUIRED,
  73. Util::parseUInt(status)).str());
  74. }
  75. } else if(!httpHeader->defined(HttpHeader::TRANSFER_ENCODING)) {
  76. // compare the received range against the requested range
  77. RangeHandle responseRange = httpHeader->getRange();
  78. if(!httpRequest->isRangeSatisfied(responseRange)) {
  79. throw DlAbortEx
  80. (StringFormat
  81. (EX_INVALID_RANGE_HEADER,
  82. Util::itos(httpRequest->getStartByte(), true).c_str(),
  83. Util::itos(httpRequest->getEndByte(), true).c_str(),
  84. Util::uitos(httpRequest->getEntityLength(), true).c_str(),
  85. Util::itos(responseRange->getStartByte(), true).c_str(),
  86. Util::itos(responseRange->getEndByte(), true).c_str(),
  87. Util::uitos(responseRange->getEntityLength(), true).c_str()).str());
  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. return Util::urldecode(httpRequest->getFile());
  98. } else {
  99. logger->info(MSG_CONTENT_DISPOSITION_DETECTED,
  100. cuid, contentDisposition.c_str());
  101. return Util::urldecode(contentDisposition);
  102. }
  103. }
  104. void HttpResponse::retrieveCookie()
  105. {
  106. std::deque<std::string> v = httpHeader->get(HttpHeader::SET_COOKIE);
  107. for(std::deque<std::string>::const_iterator itr = v.begin(); itr != v.end();
  108. ++itr) {
  109. httpRequest->getCookieStorage()->parseAndStore(*itr,
  110. httpRequest->getHost(),
  111. httpRequest->getDir());
  112. }
  113. }
  114. bool HttpResponse::isRedirect() const
  115. {
  116. const std::string& status = getResponseStatus();
  117. return HttpHeader::S300 <= status && status < HttpHeader::S400 &&
  118. httpHeader->defined(HttpHeader::LOCATION);
  119. }
  120. void HttpResponse::processRedirect()
  121. {
  122. if(httpRequest->getRequest()->redirectUrl(getRedirectURI())) {
  123. logger->info(MSG_REDIRECT, cuid,
  124. httpRequest->getRequest()->getCurrentUrl().c_str());
  125. } else {
  126. throw DlRetryEx
  127. (StringFormat("CUID#%d - Redirect to %s failed. It may not be a valid"
  128. " URI.", cuid,
  129. httpRequest->getRequest()->getCurrentUrl().c_str()).str());
  130. }
  131. }
  132. std::string HttpResponse::getRedirectURI() const
  133. {
  134. return httpHeader->getFirst(HttpHeader::LOCATION);
  135. }
  136. bool HttpResponse::isTransferEncodingSpecified() const
  137. {
  138. return httpHeader->defined(HttpHeader::TRANSFER_ENCODING);
  139. }
  140. std::string HttpResponse::getTransferEncoding() const
  141. {
  142. // TODO See TODO in getTransferEncodingDecoder()
  143. return httpHeader->getFirst(HttpHeader::TRANSFER_ENCODING);
  144. }
  145. SharedHandle<Decoder> HttpResponse::getTransferEncodingDecoder() const
  146. {
  147. // TODO Transfer-Encoding header field can contains multiple tokens. We should
  148. // parse the field and retrieve each token.
  149. if(isTransferEncodingSpecified()) {
  150. if(getTransferEncoding() == HttpHeader::CHUNKED) {
  151. return SharedHandle<Decoder>(new ChunkedDecoder());
  152. }
  153. }
  154. return SharedHandle<Decoder>();
  155. }
  156. bool HttpResponse::isContentEncodingSpecified() const
  157. {
  158. return httpHeader->defined(HttpHeader::CONTENT_ENCODING);
  159. }
  160. const std::string& HttpResponse::getContentEncoding() const
  161. {
  162. return httpHeader->getFirst(HttpHeader::CONTENT_ENCODING);
  163. }
  164. SharedHandle<Decoder> HttpResponse::getContentEncodingDecoder() const
  165. {
  166. #ifdef HAVE_LIBZ
  167. if(getContentEncoding() == HttpHeader::GZIP ||
  168. getContentEncoding() == HttpHeader::DEFLATE) {
  169. return SharedHandle<Decoder>(new GZipDecoder());
  170. }
  171. #endif // HAVE_LIBZ
  172. return SharedHandle<Decoder>();
  173. }
  174. uint64_t HttpResponse::getContentLength() const
  175. {
  176. if(httpHeader.isNull()) {
  177. return 0;
  178. } else {
  179. return httpHeader->getRange()->getContentLength();
  180. }
  181. }
  182. uint64_t HttpResponse::getEntityLength() const
  183. {
  184. if(httpHeader.isNull()) {
  185. return 0;
  186. } else {
  187. return httpHeader->getRange()->getEntityLength();
  188. }
  189. }
  190. std::string HttpResponse::getContentType() const
  191. {
  192. if(httpHeader.isNull()) {
  193. return A2STR::NIL;
  194. } else {
  195. return
  196. Util::split(httpHeader->getFirst(HttpHeader::CONTENT_TYPE), ";").first;
  197. }
  198. }
  199. void HttpResponse::setHttpHeader(const SharedHandle<HttpHeader>& httpHeader)
  200. {
  201. this->httpHeader = httpHeader;
  202. }
  203. SharedHandle<HttpHeader> HttpResponse::getHttpHeader() const
  204. {
  205. return httpHeader;
  206. }
  207. void HttpResponse::setHttpRequest(const SharedHandle<HttpRequest>& httpRequest)
  208. {
  209. this->httpRequest = httpRequest;
  210. }
  211. SharedHandle<HttpRequest> HttpResponse::getHttpRequest() const
  212. {
  213. return httpRequest;
  214. }
  215. // TODO return std::string
  216. const std::string& HttpResponse::getResponseStatus() const
  217. {
  218. return httpHeader->getResponseStatus();
  219. }
  220. bool HttpResponse::hasRetryAfter() const
  221. {
  222. return httpHeader->defined(HttpHeader::RETRY_AFTER);
  223. }
  224. time_t HttpResponse::getRetryAfter() const
  225. {
  226. return httpHeader->getFirstAsUInt(HttpHeader::RETRY_AFTER);
  227. }
  228. Time HttpResponse::getLastModifiedTime() const
  229. {
  230. return Time::parseHTTPDate(httpHeader->getFirst(HttpHeader::LAST_MODIFIED));
  231. }
  232. bool HttpResponse::supportsPersistentConnection() const
  233. {
  234. std::string connection =
  235. Util::toLower(httpHeader->getFirst(HttpHeader::CONNECTION));
  236. std::string version = httpHeader->getVersion();
  237. return
  238. connection.find(HttpHeader::CLOSE) == std::string::npos &&
  239. (version == HttpHeader::HTTP_1_1 ||
  240. connection.find("keep-alive") != std::string::npos) &&
  241. (!httpRequest->isProxyRequestSet() ||
  242. Util::toLower(httpHeader->getFirst("Proxy-Connection")).find("keep-alive")
  243. != std::string::npos);
  244. }
  245. } // namespace aria2