HttpResponse.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #include "prefs.h"
  55. #include "Option.h"
  56. #include "Checksum.h"
  57. #include "uri.h"
  58. #include "MetalinkHttpEntry.h"
  59. #include "base64.h"
  60. #include "array_fun.h"
  61. #include "MessageDigest.h"
  62. #ifdef HAVE_ZLIB
  63. #include "GZipDecodingStreamFilter.h"
  64. #endif // HAVE_ZLIB
  65. namespace aria2 {
  66. HttpResponse::HttpResponse() : cuid_{0} {}
  67. void HttpResponse::validateResponse() const
  68. {
  69. int statusCode = getStatusCode();
  70. switch (statusCode) {
  71. case 200: // OK
  72. case 206: // Partial Content
  73. if (!httpHeader_->defined(HttpHeader::TRANSFER_ENCODING)) {
  74. // compare the received range against the requested range
  75. auto responseRange = httpHeader_->getRange();
  76. if (!httpRequest_->isRangeSatisfied(responseRange)) {
  77. throw DL_ABORT_EX2(
  78. fmt(EX_INVALID_RANGE_HEADER, httpRequest_->getStartByte(),
  79. httpRequest_->getEndByte(), httpRequest_->getEntityLength(),
  80. responseRange.startByte, responseRange.endByte,
  81. responseRange.entityLength),
  82. error_code::CANNOT_RESUME);
  83. }
  84. }
  85. return;
  86. case 304: // Not Modified
  87. if (!httpRequest_->conditionalRequest()) {
  88. throw DL_ABORT_EX2("Got 304 without If-Modified-Since or If-None-Match",
  89. error_code::HTTP_PROTOCOL_ERROR);
  90. }
  91. return;
  92. case 300: // Multiple Choices
  93. case 301: // Moved Permanently
  94. case 302: // Found
  95. case 303: // See Other
  96. case 307: // Temporary Redirect
  97. case 308: // Permanent Redirect
  98. if (!httpHeader_->defined(HttpHeader::LOCATION)) {
  99. throw DL_ABORT_EX2(fmt(EX_LOCATION_HEADER_REQUIRED, statusCode),
  100. error_code::HTTP_PROTOCOL_ERROR);
  101. }
  102. return;
  103. }
  104. if (statusCode >= 400) {
  105. return;
  106. }
  107. throw DL_ABORT_EX2(fmt("Unexpected status %d", statusCode),
  108. error_code::HTTP_PROTOCOL_ERROR);
  109. }
  110. std::string HttpResponse::determineFilename(bool contentDispositionUTF8) const
  111. {
  112. std::string contentDisposition = util::getContentDispositionFilename(
  113. httpHeader_->find(HttpHeader::CONTENT_DISPOSITION),
  114. contentDispositionUTF8);
  115. if (contentDisposition.empty()) {
  116. auto file = httpRequest_->getFile();
  117. file = util::percentDecode(file.begin(), file.end());
  118. if (file.empty()) {
  119. return Request::DEFAULT_FILE;
  120. }
  121. return file;
  122. }
  123. A2_LOG_INFO(
  124. fmt(MSG_CONTENT_DISPOSITION_DETECTED, cuid_, contentDisposition.c_str()));
  125. return contentDisposition;
  126. }
  127. void HttpResponse::retrieveCookie()
  128. {
  129. Time now;
  130. auto r = httpHeader_->equalRange(HttpHeader::SET_COOKIE);
  131. for (; r.first != r.second; ++r.first) {
  132. httpRequest_->getCookieStorage()->parseAndStore(
  133. (*r.first).second, httpRequest_->getHost(), httpRequest_->getDir(),
  134. now.getTimeFromEpoch());
  135. }
  136. }
  137. bool HttpResponse::isRedirect() const
  138. {
  139. switch (getStatusCode()) {
  140. case 300: // Multiple Choices
  141. case 301: // Moved Permanently
  142. case 302: // Found
  143. case 303: // See Other
  144. case 307: // Temporary Redirect
  145. case 308: // Permanent Redirect
  146. return httpHeader_->defined(HttpHeader::LOCATION);
  147. }
  148. return false;
  149. }
  150. void HttpResponse::processRedirect()
  151. {
  152. const auto& req = httpRequest_->getRequest();
  153. if (!req->redirectUri(util::percentEncodeMini(getRedirectURI()))) {
  154. throw DL_RETRY_EX(fmt(
  155. "CUID#%" PRId64 " - Redirect to %s failed. It may not be a valid URI.",
  156. cuid_, req->getCurrentUri().c_str()));
  157. }
  158. A2_LOG_NOTICE(fmt(MSG_REDIRECT, cuid_,
  159. httpRequest_->getRequest()->getCurrentUri().c_str()));
  160. }
  161. const std::string& HttpResponse::getRedirectURI() const
  162. {
  163. return httpHeader_->find(HttpHeader::LOCATION);
  164. }
  165. bool HttpResponse::isTransferEncodingSpecified() const
  166. {
  167. return httpHeader_->defined(HttpHeader::TRANSFER_ENCODING);
  168. }
  169. const std::string& HttpResponse::getTransferEncoding() const
  170. {
  171. // TODO See TODO in getTransferEncodingStreamFilter()
  172. return httpHeader_->find(HttpHeader::TRANSFER_ENCODING);
  173. }
  174. std::unique_ptr<StreamFilter>
  175. HttpResponse::getTransferEncodingStreamFilter() const
  176. {
  177. // TODO Transfer-Encoding header field can contains multiple tokens. We should
  178. // parse the field and retrieve each token.
  179. if (isTransferEncodingSpecified()) {
  180. if (util::strieq(getTransferEncoding(), "chunked")) {
  181. return make_unique<ChunkedDecodingStreamFilter>();
  182. }
  183. }
  184. return nullptr;
  185. }
  186. bool HttpResponse::isContentEncodingSpecified() const
  187. {
  188. return httpHeader_->defined(HttpHeader::CONTENT_ENCODING);
  189. }
  190. const std::string& HttpResponse::getContentEncoding() const
  191. {
  192. return httpHeader_->find(HttpHeader::CONTENT_ENCODING);
  193. }
  194. std::unique_ptr<StreamFilter>
  195. HttpResponse::getContentEncodingStreamFilter() const
  196. {
  197. #ifdef HAVE_ZLIB
  198. if (util::strieq(getContentEncoding(), "gzip") ||
  199. util::strieq(getContentEncoding(), "deflate")) {
  200. return make_unique<GZipDecodingStreamFilter>();
  201. }
  202. #endif // HAVE_ZLIB
  203. return nullptr;
  204. }
  205. int64_t HttpResponse::getContentLength() const
  206. {
  207. if (!httpHeader_) {
  208. return 0;
  209. }
  210. return httpHeader_->getRange().getContentLength();
  211. }
  212. int64_t HttpResponse::getEntityLength() const
  213. {
  214. if (!httpHeader_) {
  215. return 0;
  216. }
  217. return httpHeader_->getRange().entityLength;
  218. }
  219. std::string HttpResponse::getContentType() const
  220. {
  221. if (!httpHeader_) {
  222. return A2STR::NIL;
  223. }
  224. const auto& ctype = httpHeader_->find(HttpHeader::CONTENT_TYPE);
  225. auto i = std::find(ctype.begin(), ctype.end(), ';');
  226. Scip p = util::stripIter(ctype.begin(), i);
  227. return std::string(p.first, p.second);
  228. }
  229. void HttpResponse::setHttpHeader(std::unique_ptr<HttpHeader> httpHeader)
  230. {
  231. httpHeader_ = std::move(httpHeader);
  232. }
  233. const std::unique_ptr<HttpHeader>& HttpResponse::getHttpHeader() const
  234. {
  235. return httpHeader_;
  236. }
  237. void HttpResponse::setHttpRequest(std::unique_ptr<HttpRequest> httpRequest)
  238. {
  239. httpRequest_ = std::move(httpRequest);
  240. }
  241. int HttpResponse::getStatusCode() const { return httpHeader_->getStatusCode(); }
  242. Time HttpResponse::getLastModifiedTime() const
  243. {
  244. return Time::parseHTTPDate(httpHeader_->find(HttpHeader::LAST_MODIFIED));
  245. }
  246. bool HttpResponse::supportsPersistentConnection() const
  247. {
  248. return httpHeader_->isKeepAlive();
  249. }
  250. namespace {
  251. bool parseMetalinkHttpLink(MetalinkHttpEntry& result, const std::string& s)
  252. {
  253. const auto first = std::find(s.begin(), s.end(), '<');
  254. if (first == s.end()) {
  255. return false;
  256. }
  257. auto last = std::find(first, s.end(), '>');
  258. if (last == s.end()) {
  259. return false;
  260. }
  261. auto p = util::stripIter(first + 1, last);
  262. if (p.first == p.second) {
  263. return false;
  264. }
  265. result.uri.assign(p.first, p.second);
  266. last = std::find(last, s.end(), ';');
  267. if (last != s.end()) {
  268. ++last;
  269. }
  270. bool ok = false;
  271. while (1) {
  272. std::string name, value;
  273. auto r = util::nextParam(name, value, last, s.end(), ';');
  274. last = r.first;
  275. if (!r.second) {
  276. break;
  277. }
  278. if (value.empty()) {
  279. if (name == "pref") {
  280. result.pref = true;
  281. }
  282. continue;
  283. }
  284. if (name == "rel") {
  285. if (value == "duplicate") {
  286. ok = true;
  287. }
  288. else {
  289. ok = false;
  290. }
  291. continue;
  292. }
  293. if (name == "pri") {
  294. int32_t priValue;
  295. if (util::parseIntNoThrow(priValue, value)) {
  296. if (1 <= priValue && priValue <= 999999) {
  297. result.pri = priValue;
  298. }
  299. }
  300. continue;
  301. }
  302. if (name == "geo") {
  303. util::lowercase(value);
  304. result.geo = value;
  305. continue;
  306. }
  307. }
  308. return ok;
  309. }
  310. } // namespace
  311. // Metalink/HTTP is defined by http://tools.ietf.org/html/rfc6249.
  312. // Link header field is defined by http://tools.ietf.org/html/rfc5988.
  313. void HttpResponse::getMetalinKHttpEntries(
  314. std::vector<MetalinkHttpEntry>& result,
  315. const std::shared_ptr<Option>& option) const
  316. {
  317. auto p = httpHeader_->equalRange(HttpHeader::LINK);
  318. for (; p.first != p.second; ++p.first) {
  319. MetalinkHttpEntry e;
  320. if (parseMetalinkHttpLink(e, (*p.first).second)) {
  321. result.push_back(e);
  322. }
  323. }
  324. if (!result.empty()) {
  325. std::vector<std::string> locs;
  326. if (option->defined(PREF_METALINK_LOCATION)) {
  327. const std::string& loc = option->get(PREF_METALINK_LOCATION);
  328. util::split(loc.begin(), loc.end(), std::back_inserter(locs), ',', true);
  329. for (auto& l : locs) {
  330. util::lowercase(l);
  331. }
  332. }
  333. for (auto& r : result) {
  334. if (std::find(locs.begin(), locs.end(), r.geo) != locs.end()) {
  335. r.pri -= 999999;
  336. }
  337. }
  338. }
  339. std::sort(result.begin(), result.end());
  340. }
  341. // Digest header field is defined by
  342. // http://tools.ietf.org/html/rfc3230.
  343. void HttpResponse::getDigest(std::vector<Checksum>& result) const
  344. {
  345. auto p = httpHeader_->equalRange(HttpHeader::DIGEST);
  346. for (; p.first != p.second; ++p.first) {
  347. const std::string& s = (*p.first).second;
  348. std::string::const_iterator itr = s.begin();
  349. while (1) {
  350. std::string hashType, digest;
  351. auto r = util::nextParam(hashType, digest, itr, s.end(), ',');
  352. itr = r.first;
  353. if (!r.second) {
  354. break;
  355. }
  356. util::lowercase(hashType);
  357. digest = base64::decode(digest.begin(), digest.end());
  358. if (!MessageDigest::supports(hashType) ||
  359. MessageDigest::getDigestLength(hashType) != digest.size()) {
  360. continue;
  361. }
  362. result.push_back(Checksum(hashType, digest));
  363. }
  364. }
  365. std::sort(result.begin(), result.end(), HashTypeStronger());
  366. std::vector<Checksum> temp;
  367. for (auto i = result.begin(), eoi = result.end(); i != eoi;) {
  368. bool ok = true;
  369. auto j = i + 1;
  370. for (; j != eoi; ++j) {
  371. if ((*i).getHashType() != (*j).getHashType()) {
  372. break;
  373. }
  374. if ((*i).getDigest() != (*j).getDigest()) {
  375. ok = false;
  376. }
  377. }
  378. if (ok) {
  379. temp.push_back(*i);
  380. }
  381. i = j;
  382. }
  383. std::swap(temp, result);
  384. }
  385. } // namespace aria2