HttpResponse.cc 11 KB

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