HttpResponseCommand.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 "HttpResponseCommand.h"
  36. #include "DownloadEngine.h"
  37. #include "DownloadContext.h"
  38. #include "FileEntry.h"
  39. #include "RequestGroup.h"
  40. #include "RequestGroupMan.h"
  41. #include "Request.h"
  42. #include "HttpRequest.h"
  43. #include "HttpResponse.h"
  44. #include "HttpConnection.h"
  45. #include "SegmentMan.h"
  46. #include "Segment.h"
  47. #include "HttpDownloadCommand.h"
  48. #include "DiskAdaptor.h"
  49. #include "PieceStorage.h"
  50. #include "DefaultBtProgressInfoFile.h"
  51. #include "DownloadFailureException.h"
  52. #include "DlAbortEx.h"
  53. #include "util.h"
  54. #include "File.h"
  55. #include "Option.h"
  56. #include "Logger.h"
  57. #include "Socket.h"
  58. #include "message.h"
  59. #include "prefs.h"
  60. #include "fmt.h"
  61. #include "HttpSkipResponseCommand.h"
  62. #include "HttpHeader.h"
  63. #include "LogFactory.h"
  64. #include "CookieStorage.h"
  65. #include "AuthConfigFactory.h"
  66. #include "AuthConfig.h"
  67. #include "a2functional.h"
  68. #include "URISelector.h"
  69. #include "CheckIntegrityEntry.h"
  70. #include "StreamFilter.h"
  71. #include "SinkStreamFilter.h"
  72. #include "ChunkedDecodingStreamFilter.h"
  73. #include "uri.h"
  74. #include "SocketRecvBuffer.h"
  75. #ifdef HAVE_LIBZ
  76. # include "GZipDecodingStreamFilter.h"
  77. #endif // HAVE_LIBZ
  78. namespace aria2 {
  79. namespace {
  80. SharedHandle<StreamFilter> getTransferEncodingStreamFilter
  81. (const SharedHandle<HttpResponse>& httpResponse,
  82. const SharedHandle<StreamFilter>& delegate = SharedHandle<StreamFilter>())
  83. {
  84. SharedHandle<StreamFilter> filter;
  85. if(httpResponse->isTransferEncodingSpecified()) {
  86. filter = httpResponse->getTransferEncodingStreamFilter();
  87. if(!filter) {
  88. throw DL_ABORT_EX
  89. (fmt(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
  90. httpResponse->getTransferEncoding().c_str()));
  91. }
  92. filter->init();
  93. filter->installDelegate(delegate);
  94. }
  95. if(!filter) {
  96. filter = delegate;
  97. }
  98. return filter;
  99. }
  100. } // namespace
  101. namespace {
  102. SharedHandle<StreamFilter> getContentEncodingStreamFilter
  103. (const SharedHandle<HttpResponse>& httpResponse,
  104. const SharedHandle<StreamFilter>& delegate = SharedHandle<StreamFilter>())
  105. {
  106. SharedHandle<StreamFilter> filter;
  107. if(httpResponse->isContentEncodingSpecified()) {
  108. filter = httpResponse->getContentEncodingStreamFilter();
  109. if(!filter) {
  110. A2_LOG_INFO
  111. (fmt("Content-Encoding %s is specified, but the current implementation"
  112. "doesn't support it. The decoding process is skipped and the"
  113. "downloaded content will be still encoded.",
  114. httpResponse->getContentEncoding().c_str()));
  115. } else {
  116. filter->init();
  117. filter->installDelegate(delegate);
  118. }
  119. }
  120. if(!filter) {
  121. filter = delegate;
  122. }
  123. return filter;
  124. }
  125. } // namespace
  126. HttpResponseCommand::HttpResponseCommand
  127. (cuid_t cuid,
  128. const SharedHandle<Request>& req,
  129. const SharedHandle<FileEntry>& fileEntry,
  130. RequestGroup* requestGroup,
  131. const HttpConnectionHandle& httpConnection,
  132. DownloadEngine* e,
  133. const SocketHandle& s)
  134. : AbstractCommand(cuid, req, fileEntry, requestGroup, e, s,
  135. httpConnection->getSocketRecvBuffer()),
  136. httpConnection_(httpConnection)
  137. {
  138. checkSocketRecvBuffer();
  139. }
  140. HttpResponseCommand::~HttpResponseCommand() {}
  141. bool HttpResponseCommand::executeInternal()
  142. {
  143. SharedHandle<HttpRequest> httpRequest =httpConnection_->getFirstHttpRequest();
  144. SharedHandle<HttpResponse> httpResponse = httpConnection_->receiveResponse();
  145. if(!httpResponse) {
  146. // The server has not responded to our request yet.
  147. // For socket->wantRead() == true, setReadCheckSocket(socket) is already
  148. // done in the constructor.
  149. setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite());
  150. getDownloadEngine()->addCommand(this);
  151. return false;
  152. }
  153. // check HTTP status number
  154. httpResponse->validateResponse();
  155. httpResponse->retrieveCookie();
  156. SharedHandle<HttpHeader> httpHeader = httpResponse->getHttpHeader();
  157. // Disable persistent connection if:
  158. // Connection: close is received or the remote server is not HTTP/1.1.
  159. // We don't care whether non-HTTP/1.1 server returns Connection: keep-alive.
  160. getRequest()->supportsPersistentConnection
  161. (httpResponse->supportsPersistentConnection());
  162. if(getRequest()->isPipeliningEnabled()) {
  163. getRequest()->setMaxPipelinedRequest
  164. (getOption()->getAsInt(PREF_MAX_HTTP_PIPELINING));
  165. } else {
  166. getRequest()->setMaxPipelinedRequest(1);
  167. }
  168. int statusCode = httpResponse->getStatusCode();
  169. if(!httpResponse->getHttpRequest()->getIfModifiedSinceHeader().empty()) {
  170. if(statusCode == 304) {
  171. uint64_t totalLength = httpResponse->getEntityLength();
  172. getFileEntry()->setLength(totalLength);
  173. getRequestGroup()->initPieceStorage();
  174. getPieceStorage()->markAllPiecesDone();
  175. // Just set checksum verification done.
  176. getDownloadContext()->setChecksumVerified(true);
  177. A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
  178. util::itos(getRequestGroup()->getGID()).c_str(),
  179. getRequestGroup()->getFirstFilePath().c_str()));
  180. poolConnection();
  181. getFileEntry()->poolRequest(getRequest());
  182. return true;
  183. } else if(statusCode == 200 || statusCode == 206) {
  184. // Remote file is newer than local file. We allow overwrite.
  185. getOption()->put(PREF_ALLOW_OVERWRITE, A2_V_TRUE);
  186. }
  187. }
  188. if(statusCode != 304 && statusCode >= 300) {
  189. if(statusCode == 404) {
  190. getRequestGroup()->increaseAndValidateFileNotFoundCount();
  191. }
  192. return skipResponseBody(httpResponse);
  193. }
  194. if(getFileEntry()->isUniqueProtocol()) {
  195. // Redirection should be considered here. We need to parse
  196. // original URI to get hostname.
  197. uri::UriStruct us;
  198. if(uri::parse(us, getRequest()->getUri())) {
  199. getFileEntry()->removeURIWhoseHostnameIs(us.host);
  200. }
  201. }
  202. if(!getPieceStorage()) {
  203. uint64_t totalLength = httpResponse->getEntityLength();
  204. getFileEntry()->setLength(totalLength);
  205. if(getFileEntry()->getPath().empty()) {
  206. getFileEntry()->setPath
  207. (util::createSafePath
  208. (getOption()->get(PREF_DIR), httpResponse->determinFilename()));
  209. }
  210. getFileEntry()->setContentType(httpResponse->getContentType());
  211. getRequestGroup()->preDownloadProcessing();
  212. if(getDownloadEngine()->getRequestGroupMan()->
  213. isSameFileBeingDownloaded(getRequestGroup())) {
  214. throw DOWNLOAD_FAILURE_EXCEPTION2
  215. (fmt(EX_DUPLICATE_FILE_DOWNLOAD,
  216. getRequestGroup()->getFirstFilePath().c_str()),
  217. error_code::DUPLICATE_DOWNLOAD);
  218. }
  219. // update last modified time
  220. updateLastModifiedTime(httpResponse->getLastModifiedTime());
  221. // If both transfer-encoding and total length is specified, we
  222. // assume we can do segmented downloading
  223. if(totalLength == 0 || shouldInflateContentEncoding(httpResponse)) {
  224. // we ignore content-length when inflate is required
  225. getFileEntry()->setLength(0);
  226. if(getRequest()->getMethod() == Request::METHOD_GET &&
  227. (totalLength != 0 ||
  228. !httpResponse->getHttpHeader()->defined(HttpHeader::CONTENT_LENGTH))){
  229. // DownloadContext::knowsTotalLength() == true only when
  230. // server says the size of file is 0 explicitly.
  231. getDownloadContext()->markTotalLengthIsUnknown();
  232. }
  233. return handleOtherEncoding(httpResponse);
  234. } else {
  235. return handleDefaultEncoding(httpResponse);
  236. }
  237. } else {
  238. // validate totalsize
  239. getRequestGroup()->validateTotalLength(getFileEntry()->getLength(),
  240. httpResponse->getEntityLength());
  241. // update last modified time
  242. updateLastModifiedTime(httpResponse->getLastModifiedTime());
  243. if(getRequestGroup()->getTotalLength() == 0) {
  244. // Since total length is unknown, the file size in previously
  245. // failed download could be larger than the size this time.
  246. // Also we can't resume in this case too. So truncate the file
  247. // anyway.
  248. getPieceStorage()->getDiskAdaptor()->truncate(0);
  249. getDownloadEngine()->addCommand
  250. (createHttpDownloadCommand
  251. (httpResponse,
  252. getTransferEncodingStreamFilter
  253. (httpResponse,
  254. getContentEncodingStreamFilter(httpResponse))));
  255. } else {
  256. getDownloadEngine()->addCommand
  257. (createHttpDownloadCommand
  258. (httpResponse,
  259. getTransferEncodingStreamFilter(httpResponse)));
  260. }
  261. return true;
  262. }
  263. }
  264. void HttpResponseCommand::updateLastModifiedTime(const Time& lastModified)
  265. {
  266. if(getOption()->getAsBool(PREF_REMOTE_TIME)) {
  267. getRequestGroup()->updateLastModifiedTime(lastModified);
  268. }
  269. }
  270. bool HttpResponseCommand::shouldInflateContentEncoding
  271. (const SharedHandle<HttpResponse>& httpResponse)
  272. {
  273. // Basically, on the fly inflation cannot be made with segment
  274. // download, because in each segment we don't know where the date
  275. // should be written. So turn off segmented downloading.
  276. // Meanwhile, Some server returns content-encoding: gzip for .tgz
  277. // files. I think those files should not be inflated by clients,
  278. // because it is the original format of those files. Current
  279. // implementation just inflates these files nonetheless.
  280. const std::string& ce = httpResponse->getContentEncoding();
  281. return httpResponse->getHttpRequest()->acceptGZip() &&
  282. (ce == "gzip" || ce == "deflate");
  283. }
  284. bool HttpResponseCommand::handleDefaultEncoding
  285. (const SharedHandle<HttpResponse>& httpResponse)
  286. {
  287. SharedHandle<HttpRequest> httpRequest = httpResponse->getHttpRequest();
  288. SharedHandle<BtProgressInfoFile> progressInfoFile
  289. (new DefaultBtProgressInfoFile
  290. (getDownloadContext(), SharedHandle<PieceStorage>(), getOption().get()));
  291. getRequestGroup()->adjustFilename(progressInfoFile);
  292. getRequestGroup()->initPieceStorage();
  293. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  294. onDryRunFileFound();
  295. return true;
  296. }
  297. SharedHandle<CheckIntegrityEntry> checkEntry =
  298. getRequestGroup()->createCheckIntegrityEntry();
  299. if(!checkEntry) {
  300. return true;
  301. }
  302. File file(getRequestGroup()->getFirstFilePath());
  303. // We have to make sure that command that has Request object must
  304. // have segment after PieceStorage is initialized. See
  305. // AbstractCommand::execute()
  306. SharedHandle<Segment> segment =
  307. getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
  308. // pipelining requires implicit range specified. But the request for
  309. // this response most likely dones't contains range header. This means
  310. // we can't continue to use this socket because server sends all entity
  311. // body instead of a segment.
  312. // Therefore, we shutdown the socket here if pipelining is enabled.
  313. DownloadCommand* command = 0;
  314. if(getRequest()->getMethod() == Request::METHOD_GET &&
  315. segment && segment->getPositionToWrite() == 0 &&
  316. !getRequest()->isPipeliningEnabled()) {
  317. command = createHttpDownloadCommand
  318. (httpResponse,
  319. getTransferEncodingStreamFilter(httpResponse));
  320. } else {
  321. getSegmentMan()->cancelSegment(getCuid());
  322. getFileEntry()->poolRequest(getRequest());
  323. }
  324. // After command is passed to prepareForNextAction(), it is managed
  325. // by CheckIntegrityEntry.
  326. checkEntry->pushNextCommand(command);
  327. command = 0;
  328. prepareForNextAction(checkEntry);
  329. if(getRequest()->getMethod() == Request::METHOD_HEAD) {
  330. poolConnection();
  331. getRequest()->setMethod(Request::METHOD_GET);
  332. }
  333. return true;
  334. }
  335. bool HttpResponseCommand::handleOtherEncoding
  336. (const SharedHandle<HttpResponse>& httpResponse) {
  337. // We assume that RequestGroup::getTotalLength() == 0 here
  338. SharedHandle<HttpRequest> httpRequest = httpResponse->getHttpRequest();
  339. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  340. getRequestGroup()->initPieceStorage();
  341. onDryRunFileFound();
  342. return true;
  343. }
  344. if(getRequest()->getMethod() == Request::METHOD_HEAD) {
  345. poolConnection();
  346. getRequest()->setMethod(Request::METHOD_GET);
  347. return prepareForRetry(0);
  348. }
  349. // In this context, knowsTotalLength() is true only when the file is
  350. // really zero-length.
  351. SharedHandle<StreamFilter> streamFilter =
  352. getTransferEncodingStreamFilter
  353. (httpResponse,
  354. getContentEncodingStreamFilter(httpResponse));
  355. // If chunked transfer-encoding is specified, we have to read end of
  356. // chunk markers(0\r\n\r\n, for example).
  357. bool chunkedUsed = streamFilter &&
  358. streamFilter->getName() == ChunkedDecodingStreamFilter::NAME;
  359. // For zero-length file, check existing file comparing its size
  360. if(!chunkedUsed && getDownloadContext()->knowsTotalLength() &&
  361. getRequestGroup()->downloadFinishedByFileLength()) {
  362. // TODO If metalink file does not contain size and it contains
  363. // hash and file is not zero length, but remote server says the
  364. // file size is 0, no hash check is performed in the current
  365. // implementation. See also
  366. // FtpNegotiationCommand::onFileSizeDetermined()
  367. getRequestGroup()->initPieceStorage();
  368. getPieceStorage()->markAllPiecesDone();
  369. getDownloadContext()->setChecksumVerified(true);
  370. A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
  371. util::itos(getRequestGroup()->getGID()).c_str(),
  372. getRequestGroup()->getFirstFilePath().c_str()));
  373. poolConnection();
  374. return true;
  375. }
  376. getRequestGroup()->shouldCancelDownloadForSafety();
  377. getRequestGroup()->initPieceStorage();
  378. getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
  379. // Local file size becomes zero when DiskAdaptor::initAndOpenFile()
  380. // is called. So zero-length file is complete if chunked encoding is
  381. // not used.
  382. if(!chunkedUsed && getDownloadContext()->knowsTotalLength()) {
  383. getRequestGroup()->getPieceStorage()->markAllPiecesDone();
  384. poolConnection();
  385. return true;
  386. }
  387. // We have to make sure that command that has Request object must
  388. // have segment after PieceStorage is initialized. See
  389. // AbstractCommand::execute()
  390. getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
  391. getDownloadEngine()->addCommand
  392. (createHttpDownloadCommand(httpResponse, streamFilter));
  393. return true;
  394. }
  395. bool HttpResponseCommand::skipResponseBody
  396. (const SharedHandle<HttpResponse>& httpResponse)
  397. {
  398. SharedHandle<StreamFilter> filter =
  399. getTransferEncodingStreamFilter(httpResponse);
  400. // We don't use Content-Encoding here because this response body is just
  401. // thrown away.
  402. HttpSkipResponseCommand* command = new HttpSkipResponseCommand
  403. (getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  404. httpConnection_, httpResponse,
  405. getDownloadEngine(), getSocket());
  406. command->installStreamFilter(filter);
  407. // If request method is HEAD or the response body is zero-length,
  408. // set command's status to real time so that avoid read check blocking
  409. if(getRequest()->getMethod() == Request::METHOD_HEAD ||
  410. (httpResponse->getEntityLength() == 0 &&
  411. !httpResponse->isTransferEncodingSpecified())) {
  412. command->setStatusRealtime();
  413. // If entity length == 0, then socket read/write check must be disabled.
  414. command->disableSocketCheck();
  415. getDownloadEngine()->setNoWait(true);
  416. }
  417. getDownloadEngine()->addCommand(command);
  418. return true;
  419. }
  420. namespace {
  421. bool decideFileAllocation
  422. (const SharedHandle<StreamFilter>& filter)
  423. {
  424. #ifdef HAVE_LIBZ
  425. for(SharedHandle<StreamFilter> f = filter; f; f = f->getDelegate()){
  426. // Since the compressed file's length are returned in the response header
  427. // and the decompressed file size is unknown at this point, disable file
  428. // allocation here.
  429. if(f->getName() == GZipDecodingStreamFilter::NAME) {
  430. return false;
  431. }
  432. }
  433. #endif // HAVE_LIBZ
  434. return true;
  435. }
  436. } // namespace
  437. HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand
  438. (const SharedHandle<HttpResponse>& httpResponse,
  439. const SharedHandle<StreamFilter>& filter)
  440. {
  441. HttpDownloadCommand* command =
  442. new HttpDownloadCommand(getCuid(), getRequest(), getFileEntry(),
  443. getRequestGroup(),
  444. httpResponse, httpConnection_,
  445. getDownloadEngine(), getSocket());
  446. command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME));
  447. command->setLowestDownloadSpeedLimit
  448. (getOption()->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  449. command->installStreamFilter(filter);
  450. if(getRequestGroup()->isFileAllocationEnabled() &&
  451. !decideFileAllocation(filter)) {
  452. getRequestGroup()->setFileAllocationEnabled(false);
  453. }
  454. getRequestGroup()->getURISelector()->tuneDownloadCommand
  455. (getFileEntry()->getRemainingUris(), command);
  456. return command;
  457. }
  458. void HttpResponseCommand::poolConnection()
  459. {
  460. if(getRequest()->supportsPersistentConnection()) {
  461. getDownloadEngine()->poolSocket(getRequest(), createProxyRequest(),
  462. getSocket());
  463. }
  464. }
  465. void HttpResponseCommand::onDryRunFileFound()
  466. {
  467. getPieceStorage()->markAllPiecesDone();
  468. getDownloadContext()->setChecksumVerified(true);
  469. poolConnection();
  470. }
  471. } // namespace aria2