HttpResponseCommand.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "DlAbortEx.h"
  37. #include "DlRetryEx.h"
  38. #include "HttpDownloadCommand.h"
  39. #include "message.h"
  40. #include "Util.h"
  41. #include "prefs.h"
  42. #include "File.h"
  43. #include <sys/types.h>
  44. #include <unistd.h>
  45. HttpResponseCommand::HttpResponseCommand(int32_t cuid,
  46. const RequestHandle& req,
  47. const HttpConnectionHandle& httpConnection,
  48. DownloadEngine* e,
  49. const SocketHandle& s)
  50. :AbstractCommand(cuid, req, e, s),
  51. httpConnection(httpConnection) {}
  52. HttpResponseCommand::~HttpResponseCommand() {}
  53. bool HttpResponseCommand::executeInternal()
  54. {
  55. HttpRequestHandle httpRequest = httpConnection->getFirstHttpRequest();
  56. if(!(httpRequest->getSegment() == segment)) {
  57. logger->info(MSG_SEGMENT_CHANGED, cuid);
  58. return prepareForRetry(0);
  59. }
  60. HttpResponseHandle httpResponse = httpConnection->receiveResponse();
  61. if(httpResponse.isNull()) {
  62. // The server has not responded to our request yet.
  63. e->commands.push_back(this);
  64. return false;
  65. }
  66. // check HTTP status number
  67. httpResponse->validateResponse();
  68. httpResponse->retrieveCookie();
  69. // check whether the server supports persistent connections.
  70. /*
  71. if(Util::toLower(headers.getFirst("Connection")).find("close") != string::npos) {
  72. req->setKeepAlive(false);
  73. }
  74. */
  75. // check whether Location header exists. If it does, update request object
  76. // with redirected URL.
  77. // then establish a connection to the new host and port
  78. if(httpResponse->isRedirect()) {
  79. httpResponse->processRedirect();
  80. logger->info(MSG_REDIRECT, cuid, httpResponse->getRedirectURI().c_str());
  81. e->noWait = true;
  82. return prepareForRetry(0);
  83. }
  84. httpResponse->validateFilename(e->segmentMan->filename);
  85. if(e->segmentMan->downloadStarted) {
  86. createHttpDownloadCommand(httpResponse);
  87. return true;
  88. } else {
  89. if(httpResponse->isTransferEncodingSpecified()) {
  90. return handleOtherEncoding(httpResponse);
  91. } else {
  92. return handleDefaultEncoding(httpResponse);
  93. }
  94. }
  95. }
  96. bool HttpResponseCommand::handleDefaultEncoding(const HttpResponseHandle& httpResponse)
  97. {
  98. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  99. // TODO quick and dirty way
  100. if(httpRequest->getRequest()->isTorrent) {
  101. return doTorrentStuff(httpResponse);
  102. }
  103. int64_t size = httpResponse->getEntityLength();
  104. if(size == INT64_MAX || size < 0) {
  105. throw new DlAbortEx(EX_TOO_LARGE_FILE, size);
  106. }
  107. e->segmentMan->isSplittable = !(size == 0);
  108. e->segmentMan->filename = httpResponse->determinFilename();
  109. e->segmentMan->downloadStarted = true;
  110. e->segmentMan->totalSize = size;
  111. // quick hack for method 'head'
  112. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  113. // TODO because we don't want segment file to be saved.
  114. e->segmentMan->isSplittable = false;
  115. return true;
  116. }
  117. bool segFileExists = e->segmentMan->segmentFileExists();
  118. if(segFileExists) {
  119. e->segmentMan->load();
  120. e->segmentMan->diskWriter->openExistingFile(e->segmentMan->getFilePath());
  121. // send request again to the server with Range header
  122. return prepareForRetry(0);
  123. } else {
  124. e->segmentMan->initBitfield(e->option->getAsInt(PREF_SEGMENT_SIZE),
  125. e->segmentMan->totalSize);
  126. e->segmentMan->diskWriter->initAndOpenFile(e->segmentMan->getFilePath(),
  127. size);
  128. return prepareForRetry(0);
  129. }
  130. }
  131. bool HttpResponseCommand::handleOtherEncoding(const HttpResponseHandle& httpResponse) {
  132. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  133. // we ignore content-length when transfer-encoding is set
  134. e->segmentMan->downloadStarted = true;
  135. e->segmentMan->isSplittable = false;
  136. e->segmentMan->filename = httpResponse->determinFilename();
  137. e->segmentMan->totalSize = 0;
  138. // quick hack for method 'head'
  139. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  140. return true;
  141. }
  142. // disable keep-alive
  143. req->setKeepAlive(false);
  144. segment = e->segmentMan->getSegment(cuid);
  145. e->segmentMan->diskWriter->initAndOpenFile(e->segmentMan->getFilePath());
  146. createHttpDownloadCommand(httpResponse);
  147. return true;
  148. }
  149. void HttpResponseCommand::createHttpDownloadCommand(const HttpResponseHandle& httpResponse)
  150. {
  151. TransferEncodingHandle enc = 0;
  152. if(httpResponse->isTransferEncodingSpecified()) {
  153. enc = httpResponse->getTransferDecoder();
  154. if(enc.isNull()) {
  155. throw new DlAbortEx(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
  156. httpResponse->getTransferEncoding().c_str());
  157. }
  158. enc->init();
  159. }
  160. HttpDownloadCommand* command = new HttpDownloadCommand(cuid, req, e, socket);
  161. command->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
  162. command->setStartupIdleTime(e->option->getAsInt(PREF_STARTUP_IDLE_TIME));
  163. command->setLowestDownloadSpeedLimit(e->option->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  164. command->setTransferDecoder(enc);
  165. e->commands.push_back(command);
  166. }
  167. bool HttpResponseCommand::doTorrentStuff(const HttpResponseHandle& httpResponse)
  168. {
  169. int64_t size = httpResponse->getEntityLength();
  170. e->segmentMan->totalSize = size;
  171. if(size > 0) {
  172. e->segmentMan->initBitfield(e->option->getAsInt(PREF_SEGMENT_SIZE),
  173. e->segmentMan->totalSize);
  174. }
  175. // disable keep-alive
  176. httpResponse->getHttpRequest()->getRequest()->setKeepAlive(false);
  177. e->segmentMan->isSplittable = false;
  178. e->segmentMan->downloadStarted = true;
  179. e->segmentMan->diskWriter->initAndOpenFile("/tmp/aria2"+Util::itos((int32_t)getpid()));
  180. createHttpDownloadCommand(httpResponse);
  181. return true;
  182. }