HttpResponseCommand.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "ServerHost.h"
  41. #include "RequestGroupMan.h"
  42. #include "Request.h"
  43. #include "HttpRequest.h"
  44. #include "HttpResponse.h"
  45. #include "HttpConnection.h"
  46. #include "SegmentMan.h"
  47. #include "Segment.h"
  48. #include "HttpDownloadCommand.h"
  49. #include "DiskAdaptor.h"
  50. #include "PieceStorage.h"
  51. #include "DefaultBtProgressInfoFile.h"
  52. #include "DownloadFailureException.h"
  53. #include "DlAbortEx.h"
  54. #include "Util.h"
  55. #include "File.h"
  56. #include "Option.h"
  57. #include "Logger.h"
  58. #include "Socket.h"
  59. #include "message.h"
  60. #include "prefs.h"
  61. #include "StringFormat.h"
  62. #include "HttpSkipResponseCommand.h"
  63. #include "HttpHeader.h"
  64. #include "LogFactory.h"
  65. #include "CookieStorage.h"
  66. #include "AuthConfigFactory.h"
  67. #include "AuthConfig.h"
  68. #include "a2functional.h"
  69. #include "URISelector.h"
  70. namespace aria2 {
  71. static SharedHandle<Decoder> getTransferEncodingDecoder
  72. (const SharedHandle<HttpResponse>& httpResponse);
  73. static SharedHandle<Decoder> getContentEncodingDecoder
  74. (const SharedHandle<HttpResponse>& httpResponse);
  75. HttpResponseCommand::HttpResponseCommand
  76. (int32_t cuid,
  77. const RequestHandle& req,
  78. const SharedHandle<FileEntry>& fileEntry,
  79. RequestGroup* requestGroup,
  80. const HttpConnectionHandle& httpConnection,
  81. DownloadEngine* e,
  82. const SocketHandle& s)
  83. :AbstractCommand(cuid, req, fileEntry, requestGroup, e, s),
  84. httpConnection(httpConnection)
  85. {}
  86. HttpResponseCommand::~HttpResponseCommand() {}
  87. bool HttpResponseCommand::executeInternal()
  88. {
  89. HttpRequestHandle httpRequest = httpConnection->getFirstHttpRequest();
  90. HttpResponseHandle httpResponse = httpConnection->receiveResponse();
  91. if(httpResponse.isNull()) {
  92. // The server has not responded to our request yet.
  93. // For socket->wantRead() == true, setReadCheckSocket(socket) is already
  94. // done in the constructor.
  95. setWriteCheckSocketIf(socket, socket->wantWrite());
  96. e->commands.push_back(this);
  97. return false;
  98. }
  99. // check HTTP status number
  100. httpResponse->validateResponse();
  101. httpResponse->retrieveCookie();
  102. SharedHandle<HttpHeader> httpHeader = httpResponse->getHttpHeader();
  103. // Disable persistent connection if:
  104. // Connection: close is received or the remote server is not HTTP/1.1.
  105. // We don't care whether non-HTTP/1.1 server returns Connection: keep-alive.
  106. req->supportsPersistentConnection
  107. (httpResponse->supportsPersistentConnection());
  108. if(req->isPipeliningEnabled()) {
  109. req->setMaxPipelinedRequest(getOption()->getAsInt(PREF_MAX_HTTP_PIPELINING));
  110. }
  111. if(httpResponse->getResponseStatus() >= HttpHeader::S300) {
  112. if(httpResponse->getResponseStatus() == HttpHeader::S404) {
  113. _requestGroup->increaseAndValidateFileNotFoundCount();
  114. }
  115. return skipResponseBody(httpResponse);
  116. }
  117. if(!_requestGroup->isSingleHostMultiConnectionEnabled()) {
  118. // Query by hostname. Searching by CUID may returns NULL. In case
  119. // when resuming download, ServerHost is registered with CUID A.
  120. // Then if requested range is not equal to saved one,
  121. // StreamFileAllocationEntry is created with _nextCommand NULL.
  122. // This results creating new command CUID, say B and same URI. So
  123. // searching ServerHost by CUID B fails.
  124. SharedHandle<ServerHost> sv =
  125. _requestGroup->searchServerHost(req->getHost());
  126. if(!sv.isNull()) {
  127. _fileEntry->removeURIWhoseHostnameIs(sv->getHostname());
  128. }
  129. }
  130. if(_requestGroup->getPieceStorage().isNull()) {
  131. uint64_t totalLength = httpResponse->getEntityLength();
  132. _fileEntry->setLength(totalLength);
  133. // We assume that in this context
  134. // DownloadContext::getFileEntries().size() == 1
  135. if(getOption()->get(PREF_OUT).empty()) {
  136. _fileEntry->setPath
  137. (strconcat(getDownloadContext()->getDir(),
  138. "/", httpResponse->determinFilename()));
  139. }
  140. _fileEntry->setContentType(httpResponse->getContentType());
  141. _requestGroup->preDownloadProcessing();
  142. if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {
  143. throw DOWNLOAD_FAILURE_EXCEPTION
  144. (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
  145. _requestGroup->getFirstFilePath().c_str()).str());
  146. }
  147. // update last modified time
  148. updateLastModifiedTime(httpResponse->getLastModifiedTime());
  149. // If both transfer-encoding and total length is specified, we
  150. // assume we can do segmented downloading
  151. if(totalLength == 0 || shouldInflateContentEncoding(httpResponse)) {
  152. // we ignore content-length when inflate is required
  153. _fileEntry->setLength(0);
  154. if(req->getMethod() == Request::METHOD_GET &&
  155. (totalLength != 0 ||
  156. !httpResponse->getHttpHeader()->defined(HttpHeader::CONTENT_LENGTH))){
  157. // DownloadContext::knowsTotalLength() == true only when
  158. // server says the size of file is 0 explicitly.
  159. getDownloadContext()->markTotalLengthIsUnknown();
  160. }
  161. return handleOtherEncoding(httpResponse);
  162. } else {
  163. return handleDefaultEncoding(httpResponse);
  164. }
  165. } else {
  166. // validate totalsize
  167. _requestGroup->validateTotalLength(_fileEntry->getLength(),
  168. httpResponse->getEntityLength());
  169. // update last modified time
  170. updateLastModifiedTime(httpResponse->getLastModifiedTime());
  171. if(_requestGroup->getTotalLength() == 0) {
  172. // Since total length is unknown, the file size in previously
  173. // failed download could be larger than the size this time.
  174. // Also we can't resume in this case too. So truncate the file
  175. // anyway.
  176. _requestGroup->getPieceStorage()->getDiskAdaptor()->truncate(0);
  177. e->commands.push_back
  178. (createHttpDownloadCommand(httpResponse,
  179. getTransferEncodingDecoder(httpResponse),
  180. getContentEncodingDecoder(httpResponse)));
  181. } else {
  182. e->commands.push_back(createHttpDownloadCommand(httpResponse,
  183. getTransferEncodingDecoder(httpResponse)));
  184. }
  185. return true;
  186. }
  187. }
  188. void HttpResponseCommand::updateLastModifiedTime(const Time& lastModified)
  189. {
  190. if(getOption()->getAsBool(PREF_REMOTE_TIME)) {
  191. _requestGroup->updateLastModifiedTime(lastModified);
  192. }
  193. }
  194. static bool fileIsGzipped(const SharedHandle<HttpResponse>& httpResponse)
  195. {
  196. std::string filename =
  197. Util::toLower(httpResponse->getHttpRequest()->getRequest()->getFile());
  198. return Util::endsWith(filename, ".gz") || Util::endsWith(filename, ".tgz");
  199. }
  200. bool HttpResponseCommand::shouldInflateContentEncoding
  201. (const SharedHandle<HttpResponse>& httpResponse)
  202. {
  203. // Basically, on the fly inflation cannot be made with segment download,
  204. // because in each segment we don't know where the date should be written.
  205. // So turn off segmented downloading.
  206. // Meanwhile, Some server returns content-encoding: gzip for .tgz files.
  207. // I think those files should not be inflated by clients, because it is the
  208. // original format of those files. So I made filename ending ".gz" or ".tgz"
  209. // (case-insensitive) not inflated.
  210. return httpResponse->isContentEncodingSpecified() &&
  211. !fileIsGzipped(httpResponse);
  212. }
  213. bool HttpResponseCommand::handleDefaultEncoding(const HttpResponseHandle& httpResponse)
  214. {
  215. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  216. _requestGroup->adjustFilename
  217. (SharedHandle<BtProgressInfoFile>(new DefaultBtProgressInfoFile
  218. (_requestGroup->getDownloadContext(),
  219. SharedHandle<PieceStorage>(),
  220. getOption().get())));
  221. _requestGroup->initPieceStorage();
  222. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  223. onDryRunFileFound();
  224. return true;
  225. }
  226. BtProgressInfoFileHandle infoFile(new DefaultBtProgressInfoFile(_requestGroup->getDownloadContext(), _requestGroup->getPieceStorage(), getOption().get()));
  227. if(!infoFile->exists() && _requestGroup->downloadFinishedByFileLength()) {
  228. _requestGroup->getPieceStorage()->markAllPiecesDone();
  229. logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED,
  230. _requestGroup->getGID(),
  231. _requestGroup->getFirstFilePath().c_str());
  232. return true;
  233. }
  234. DownloadCommand* command = 0;
  235. try {
  236. _requestGroup->loadAndOpenFile(infoFile);
  237. File file(_requestGroup->getFirstFilePath());
  238. SegmentHandle segment = _requestGroup->getSegmentMan()->getSegment(cuid, 0);
  239. // pipelining requires implicit range specified. But the request for
  240. // this response most likely dones't contains range header. This means
  241. // we can't continue to use this socket because server sends all entity
  242. // body instead of a segment.
  243. // Therefore, we shutdown the socket here if pipelining is enabled.
  244. if(req->getMethod() == Request::METHOD_GET &&
  245. !segment.isNull() && segment->getPositionToWrite() == 0 &&
  246. !req->isPipeliningEnabled()) {
  247. command = createHttpDownloadCommand(httpResponse);
  248. } else {
  249. _requestGroup->getSegmentMan()->cancelSegment(cuid);
  250. _fileEntry->poolRequest(req);
  251. }
  252. prepareForNextAction(command);
  253. if(req->getMethod() == Request::METHOD_HEAD) {
  254. poolConnection();
  255. req->setMethod(Request::METHOD_GET);
  256. }
  257. } catch(Exception& e) {
  258. delete command;
  259. throw;
  260. }
  261. return true;
  262. }
  263. static SharedHandle<Decoder> getTransferEncodingDecoder
  264. (const SharedHandle<HttpResponse>& httpResponse)
  265. {
  266. SharedHandle<Decoder> decoder;
  267. if(httpResponse->isTransferEncodingSpecified()) {
  268. decoder = httpResponse->getTransferEncodingDecoder();
  269. if(decoder.isNull()) {
  270. throw DL_ABORT_EX
  271. (StringFormat(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
  272. httpResponse->getTransferEncoding().c_str()).str());
  273. }
  274. decoder->init();
  275. }
  276. return decoder;
  277. }
  278. static SharedHandle<Decoder> getContentEncodingDecoder
  279. (const SharedHandle<HttpResponse>& httpResponse)
  280. {
  281. SharedHandle<Decoder> decoder;
  282. if(httpResponse->isContentEncodingSpecified()) {
  283. decoder = httpResponse->getContentEncodingDecoder();
  284. if(decoder.isNull()) {
  285. LogFactory::getInstance()->info
  286. ("Content-Encoding %s is specified, but the current implementation"
  287. "doesn't support it. The decoding process is skipped and the"
  288. "downloaded content will be still encoded.",
  289. httpResponse->getContentEncoding().c_str());
  290. } else {
  291. decoder->init();
  292. }
  293. }
  294. return decoder;
  295. }
  296. bool HttpResponseCommand::handleOtherEncoding(const HttpResponseHandle& httpResponse) {
  297. // We assume that RequestGroup::getTotalLength() == 0 here
  298. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  299. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  300. _requestGroup->initPieceStorage();
  301. onDryRunFileFound();
  302. return true;
  303. }
  304. if(req->getMethod() == Request::METHOD_HEAD) {
  305. poolConnection();
  306. req->setMethod(Request::METHOD_GET);
  307. return prepareForRetry(0);
  308. }
  309. // For zero-length file, check existing file comparing its size
  310. if(_requestGroup->downloadFinishedByFileLength()) {
  311. _requestGroup->initPieceStorage();
  312. _requestGroup->getPieceStorage()->markAllPiecesDone();
  313. logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED,
  314. _requestGroup->getGID(),
  315. _requestGroup->getFirstFilePath().c_str());
  316. poolConnection();
  317. return true;
  318. }
  319. _requestGroup->shouldCancelDownloadForSafety();
  320. _requestGroup->initPieceStorage();
  321. _requestGroup->getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
  322. // In this context, knowsTotalLength() is true only when the file is
  323. // really zero-length.
  324. if(_requestGroup->getDownloadContext()->knowsTotalLength()) {
  325. poolConnection();
  326. return true;
  327. }
  328. e->commands.push_back
  329. (createHttpDownloadCommand(httpResponse,
  330. getTransferEncodingDecoder(httpResponse),
  331. getContentEncodingDecoder(httpResponse)));
  332. return true;
  333. }
  334. bool HttpResponseCommand::skipResponseBody
  335. (const SharedHandle<HttpResponse>& httpResponse)
  336. {
  337. SharedHandle<Decoder> decoder = getTransferEncodingDecoder(httpResponse);
  338. // We don't use Content-Encoding here because this response body is just
  339. // thrown away.
  340. HttpSkipResponseCommand* command = new HttpSkipResponseCommand
  341. (cuid, req, _fileEntry, _requestGroup, httpConnection, httpResponse, e, socket);
  342. command->setTransferEncodingDecoder(decoder);
  343. // If request method is HEAD or the response body is zero-length,
  344. // set command's status to real time so that avoid read check blocking
  345. if(req->getMethod() == Request::METHOD_HEAD ||
  346. (httpResponse->getEntityLength() == 0 &&
  347. !httpResponse->isTransferEncodingSpecified())) {
  348. command->setStatusRealtime();
  349. // If entity length == 0, then socket read/write check must be disabled.
  350. command->disableSocketCheck();
  351. e->setNoWait(true);
  352. }
  353. e->commands.push_back(command);
  354. return true;
  355. }
  356. HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand
  357. (const HttpResponseHandle& httpResponse,
  358. const SharedHandle<Decoder>& transferEncodingDecoder,
  359. const SharedHandle<Decoder>& contentEncodingDecoder)
  360. {
  361. HttpDownloadCommand* command =
  362. new HttpDownloadCommand(cuid, req, _fileEntry, _requestGroup,
  363. httpResponse, httpConnection, e, socket);
  364. command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME));
  365. command->setLowestDownloadSpeedLimit
  366. (getOption()->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  367. command->setTransferEncodingDecoder(transferEncodingDecoder);
  368. if(!contentEncodingDecoder.isNull()) {
  369. command->setContentEncodingDecoder(contentEncodingDecoder);
  370. // Since the compressed file's length are returned in the response header
  371. // and the decompressed file size is unknown at this point, disable file
  372. // allocation here.
  373. _requestGroup->setFileAllocationEnabled(false);
  374. }
  375. _requestGroup->getURISelector()->tuneDownloadCommand
  376. (_fileEntry->getRemainingUris(), command);
  377. return command;
  378. }
  379. void HttpResponseCommand::poolConnection()
  380. {
  381. if(req->supportsPersistentConnection()) {
  382. e->poolSocket(req, isProxyDefined(), socket);
  383. }
  384. }
  385. void HttpResponseCommand::onDryRunFileFound()
  386. {
  387. _requestGroup->getPieceStorage()->markAllPiecesDone();
  388. poolConnection();
  389. }
  390. } // namespace aria2