HttpResponseCommand.cc 16 KB

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