HttpResponseCommand.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "HttpInitiateConnectionCommand.h"
  40. #include "message.h"
  41. #include "Util.h"
  42. #include "prefs.h"
  43. #include <sys/types.h>
  44. #include <unistd.h>
  45. HttpResponseCommand::HttpResponseCommand(int cuid, Request* req,
  46. DownloadEngine* e,
  47. const SocketHandle& s)
  48. :AbstractCommand(cuid, req, e, s) {
  49. http = new HttpConnection(cuid, socket, req, e->option);
  50. }
  51. HttpResponseCommand::~HttpResponseCommand() {
  52. delete http;
  53. }
  54. bool HttpResponseCommand::executeInternal(Segment& segment) {
  55. if(req->segment != segment) {
  56. logger->info(MSG_SEGMENT_CHANGED, cuid);
  57. return prepareForRetry(0);
  58. }
  59. HttpHeader headers;
  60. int status = http->receiveResponse(headers);
  61. if(status == 0) {
  62. // didn't receive header fully
  63. e->commands.push_back(this);
  64. return false;
  65. }
  66. // check HTTP status number
  67. checkResponse(status, segment);
  68. retrieveCookie(headers);
  69. // check whether the server supports persistent connections.
  70. if(Util::toLower(headers.getFirst("Connection")).find("close") != string::npos) {
  71. req->setKeepAlive(false);
  72. }
  73. // check whether Location header exists. If it does, update request object
  74. // with redirected URL.
  75. // then establish a connection to the new host and port
  76. if(headers.defined("Location")) {
  77. return handleRedirect(headers.getFirst("Location"), headers);
  78. }
  79. if(!e->segmentMan->downloadStarted) {
  80. string transferEncoding;
  81. if(headers.defined("Transfer-Encoding")) {
  82. return handleOtherEncoding(headers.getFirst("Transfer-Encoding"),
  83. headers);
  84. } else {
  85. return handleDefaultEncoding(headers);
  86. }
  87. } else {
  88. if(determinFilename(headers) != e->segmentMan->filename) {
  89. throw new DlAbortEx(EX_FILENAME_MISMATCH, req->getFile().c_str(), e->segmentMan->filename.c_str());
  90. }
  91. createHttpDownloadCommand();
  92. return true;
  93. }
  94. }
  95. void HttpResponseCommand::checkResponse(int status, const Segment& segment) {
  96. if(status == 401) {
  97. throw new DlAbortEx(EX_AUTH_FAILED);
  98. }
  99. if(!(300 <= status && status < 400 ||
  100. (segment.getPosition()+segment.writtenLength == 0 && (status == 200 || status == 206)) ||
  101. (segment.getPosition()+segment.writtenLength > 0 && status == 206))) {
  102. throw new DlRetryEx(EX_BAD_STATUS, status);
  103. }
  104. }
  105. bool HttpResponseCommand::handleRedirect(const string& url, const HttpHeader& headers) {
  106. req->redirectUrl(url);
  107. logger->info(MSG_REDIRECT, cuid, url.c_str());
  108. e->noWait = true;
  109. return prepareForRetry(0);
  110. }
  111. string HttpResponseCommand::determinFilename(const HttpHeader& headers) {
  112. string contentDisposition =
  113. Util::getContentDispositionFilename(headers.getFirst("Content-Disposition"));
  114. if(contentDisposition.empty()) {
  115. return Util::urldecode(req->getFile());
  116. } else {
  117. logger->info("CUID#%d - Content-Disposition Detected. Use %s as filename",
  118. cuid, contentDisposition.c_str());
  119. return Util::urldecode(contentDisposition);
  120. }
  121. }
  122. bool HttpResponseCommand::handleDefaultEncoding(const HttpHeader& headers) {
  123. // TODO quick and dirty way
  124. if(req->isTorrent) {
  125. long long int size = headers.getFirstAsLLInt("Content-Length");
  126. e->segmentMan->totalSize = size;
  127. if(size > 0) {
  128. e->segmentMan->initBitfield(e->option->getAsInt(PREF_SEGMENT_SIZE),
  129. e->segmentMan->totalSize);
  130. }
  131. // disable keep-alive
  132. req->setKeepAlive(false);
  133. e->segmentMan->isSplittable = false;
  134. e->segmentMan->downloadStarted = true;
  135. e->segmentMan->diskWriter->initAndOpenFile("/tmp/aria2"+Util::itos(getpid()));
  136. createHttpDownloadCommand();
  137. return true;
  138. }
  139. long long int size = headers.getFirstAsLLInt("Content-Length");
  140. if(size == LONG_LONG_MAX || size < 0) {
  141. throw new DlAbortEx(EX_TOO_LARGE_FILE, size);
  142. }
  143. e->segmentMan->isSplittable = !(size == 0);
  144. e->segmentMan->filename = determinFilename(headers);
  145. bool segFileExists = e->segmentMan->segmentFileExists();
  146. e->segmentMan->downloadStarted = true;
  147. if(segFileExists) {
  148. e->segmentMan->load();
  149. e->segmentMan->diskWriter->openExistingFile(e->segmentMan->getFilePath());
  150. // send request again to the server with Range header
  151. return prepareForRetry(0);
  152. } else {
  153. e->segmentMan->totalSize = size;
  154. e->segmentMan->initBitfield(e->option->getAsInt(PREF_SEGMENT_SIZE),
  155. e->segmentMan->totalSize);
  156. e->segmentMan->diskWriter->initAndOpenFile(e->segmentMan->getFilePath());
  157. return prepareForRetry(0);
  158. }
  159. }
  160. bool HttpResponseCommand::handleOtherEncoding(const string& transferEncoding, const HttpHeader& headers) {
  161. // we ignore content-length when transfer-encoding is set
  162. e->segmentMan->downloadStarted = true;
  163. e->segmentMan->isSplittable = false;
  164. e->segmentMan->filename = determinFilename(headers);
  165. e->segmentMan->totalSize = 0;
  166. // disable keep-alive
  167. req->setKeepAlive(false);
  168. Segment segment;
  169. e->segmentMan->getSegment(segment, cuid);
  170. e->segmentMan->diskWriter->initAndOpenFile(e->segmentMan->getFilePath());
  171. createHttpDownloadCommand(transferEncoding);
  172. return true;
  173. }
  174. void HttpResponseCommand::createHttpDownloadCommand(const string& transferEncoding) {
  175. HttpDownloadCommand* command = new HttpDownloadCommand(cuid, req, e, socket);
  176. TransferEncoding* enc = NULL;
  177. if(transferEncoding.size() && (enc = command->getTransferEncoding(transferEncoding)) == NULL) {
  178. delete(command);
  179. throw new DlAbortEx(EX_TRANSFER_ENCODING_NOT_SUPPORTED, transferEncoding.c_str());
  180. } else {
  181. if(enc != NULL) {
  182. command->transferEncoding = transferEncoding;
  183. enc->init();
  184. }
  185. e->commands.push_back(command);
  186. }
  187. }
  188. void HttpResponseCommand::retrieveCookie(const HttpHeader& headers) {
  189. Strings v = headers.get("Set-Cookie");
  190. for(Strings::const_iterator itr = v.begin(); itr != v.end(); itr++) {
  191. Cookie c;
  192. req->cookieBox->parse(c, *itr);
  193. req->cookieBox->add(c);
  194. }
  195. }