HttpResponseCommand.cc 19 KB

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