HttpSkipResponseCommand.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "HttpSkipResponseCommand.h"
  36. #include "HttpConnection.h"
  37. #include "HttpResponse.h"
  38. #include "message.h"
  39. #include "SocketCore.h"
  40. #include "DlRetryEx.h"
  41. #include "Request.h"
  42. #include "DownloadEngine.h"
  43. #include "Logger.h"
  44. #include "LogFactory.h"
  45. #include "HttpRequest.h"
  46. #include "Segment.h"
  47. #include "util.h"
  48. #include "fmt.h"
  49. #include "DlAbortEx.h"
  50. #include "HttpHeader.h"
  51. #include "prefs.h"
  52. #include "Option.h"
  53. #include "CookieStorage.h"
  54. #include "AuthConfigFactory.h"
  55. #include "AuthConfig.h"
  56. #include "DownloadContext.h"
  57. #include "StreamFilter.h"
  58. #include "BinaryStream.h"
  59. #include "NullSinkStreamFilter.h"
  60. #include "SinkStreamFilter.h"
  61. namespace aria2 {
  62. HttpSkipResponseCommand::HttpSkipResponseCommand
  63. (cuid_t cuid,
  64. const SharedHandle<Request>& req,
  65. const SharedHandle<FileEntry>& fileEntry,
  66. RequestGroup* requestGroup,
  67. const SharedHandle<HttpConnection>& httpConnection,
  68. const SharedHandle<HttpResponse>& httpResponse,
  69. DownloadEngine* e,
  70. const SharedHandle<SocketCore>& s)
  71. : AbstractCommand(cuid, req, fileEntry, requestGroup, e, s),
  72. httpConnection_(httpConnection),
  73. httpResponse_(httpResponse),
  74. streamFilter_(new NullSinkStreamFilter()),
  75. sinkFilterOnly_(true),
  76. totalLength_(httpResponse_->getEntityLength()),
  77. receivedBytes_(0)
  78. {}
  79. HttpSkipResponseCommand::~HttpSkipResponseCommand() {}
  80. void HttpSkipResponseCommand::installStreamFilter
  81. (const SharedHandle<StreamFilter>& streamFilter)
  82. {
  83. if(!streamFilter) {
  84. return;
  85. }
  86. streamFilter->installDelegate(streamFilter_);
  87. streamFilter_ = streamFilter;
  88. sinkFilterOnly_ =
  89. util::endsWith(streamFilter_->getName(), SinkStreamFilter::NAME);
  90. }
  91. bool HttpSkipResponseCommand::executeInternal()
  92. {
  93. if(getRequest()->getMethod() == Request::METHOD_HEAD ||
  94. (totalLength_ == 0 && sinkFilterOnly_)) {
  95. // If request method is HEAD or content-length header is present and
  96. // it's value is 0, then pool socket for reuse.
  97. // If content-length header is not present, then EOF is expected in the end.
  98. // In this case, the content is thrown away and socket cannot be pooled.
  99. if(getRequest()->getMethod() == Request::METHOD_HEAD ||
  100. httpResponse_->getHttpHeader()->defined(HttpHeader::CONTENT_LENGTH)) {
  101. poolConnection();
  102. }
  103. return processResponse();
  104. }
  105. const size_t BUFSIZE = 16*1024;
  106. unsigned char buf[BUFSIZE];
  107. size_t bufSize;
  108. if(sinkFilterOnly_ && totalLength_ > 0) {
  109. bufSize = totalLength_-receivedBytes_;
  110. } else {
  111. bufSize = BUFSIZE;
  112. }
  113. try {
  114. if(sinkFilterOnly_) {
  115. getSocket()->readData(buf, bufSize);
  116. receivedBytes_ += bufSize;
  117. } else {
  118. getSocket()->peekData(buf, bufSize);
  119. // receivedBytes_ is not updated if transferEncoding is set.
  120. // The return value is safely ignored here.
  121. streamFilter_->transform(SharedHandle<BinaryStream>(),
  122. SharedHandle<Segment>(),
  123. buf, bufSize);
  124. bufSize = streamFilter_->getBytesProcessed();
  125. getSocket()->readData(buf, bufSize);
  126. }
  127. if(totalLength_ != 0 && bufSize == 0 &&
  128. !getSocket()->wantRead() && !getSocket()->wantWrite()) {
  129. throw DL_RETRY_EX(EX_GOT_EOF);
  130. }
  131. } catch(RecoverableException& e) {
  132. A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, e)
  133. return processResponse();
  134. }
  135. bool finished = false;
  136. if(sinkFilterOnly_) {
  137. if(bufSize == 0) {
  138. if(!getSocket()->wantRead() && !getSocket()->wantWrite()) {
  139. return processResponse();
  140. }
  141. } else {
  142. finished = (totalLength_ == receivedBytes_);
  143. }
  144. } else {
  145. finished = streamFilter_->finished();
  146. }
  147. if(finished) {
  148. poolConnection();
  149. return processResponse();
  150. } else {
  151. setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite());
  152. getDownloadEngine()->addCommand(this);
  153. return false;
  154. }
  155. }
  156. void HttpSkipResponseCommand::poolConnection() const
  157. {
  158. if(getRequest()->supportsPersistentConnection()) {
  159. getDownloadEngine()->poolSocket
  160. (getRequest(), createProxyRequest(), getSocket());
  161. }
  162. }
  163. bool HttpSkipResponseCommand::processResponse()
  164. {
  165. int statusCode;
  166. if(httpResponse_->isRedirect()) {
  167. unsigned int rnum =
  168. httpResponse_->getHttpRequest()->getRequest()->getRedirectCount();
  169. if(rnum >= Request::MAX_REDIRECT) {
  170. throw DL_ABORT_EX(fmt("Too many redirects: count=%u", rnum));
  171. }
  172. httpResponse_->processRedirect();
  173. return prepareForRetry(0);
  174. } else if((statusCode = httpResponse_->getStatusCode()) >= 400) {
  175. if(statusCode == 401) {
  176. if(getOption()->getAsBool(PREF_HTTP_AUTH_CHALLENGE) &&
  177. !httpResponse_->getHttpRequest()->authenticationUsed() &&
  178. getDownloadEngine()->getAuthConfigFactory()->activateBasicCred
  179. (getRequest()->getHost(), getRequest()->getDir(), getOption().get())) {
  180. return prepareForRetry(0);
  181. } else {
  182. throw DL_ABORT_EX(EX_AUTH_FAILED);
  183. }
  184. } else if(statusCode == 404) {
  185. throw DL_ABORT_EX2(MSG_RESOURCE_NOT_FOUND,
  186. downloadresultcode::RESOURCE_NOT_FOUND);
  187. } else {
  188. throw DL_ABORT_EX(fmt(EX_BAD_STATUS, statusCode));
  189. }
  190. } else {
  191. return prepareForRetry(0);
  192. }
  193. }
  194. void HttpSkipResponseCommand::disableSocketCheck()
  195. {
  196. disableReadCheckSocket();
  197. disableWriteCheckSocket();
  198. }
  199. } // namespace aria2