HttpSkipResponseCommand.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "Decoder.h"
  41. #include "DlRetryEx.h"
  42. #include "Request.h"
  43. #include "DownloadEngine.h"
  44. #include "Logger.h"
  45. #include "HttpRequest.h"
  46. #include "Segment.h"
  47. #include "util.h"
  48. #include "StringFormat.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 "RequestGroupMan.h"
  58. #include "FileAllocationEntry.h"
  59. #include "CheckIntegrityEntry.h"
  60. #include "ServerStatMan.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. _totalLength(_httpResponse->getEntityLength()),
  75. _receivedBytes(0)
  76. {}
  77. HttpSkipResponseCommand::~HttpSkipResponseCommand() {}
  78. void HttpSkipResponseCommand::setTransferEncodingDecoder
  79. (const SharedHandle<Decoder>& decoder)
  80. {
  81. _transferEncodingDecoder = decoder;
  82. }
  83. bool HttpSkipResponseCommand::executeInternal()
  84. {
  85. if(getRequest()->getMethod() == Request::METHOD_HEAD ||
  86. (_totalLength == 0 && _transferEncodingDecoder.isNull())) {
  87. // If request method is HEAD or content-length header is present and
  88. // it's value is 0, then pool socket for reuse.
  89. // If content-length header is not present, then EOF is expected in the end.
  90. // In this case, the content is thrown away and socket cannot be pooled.
  91. if(getRequest()->getMethod() == Request::METHOD_HEAD ||
  92. _httpResponse->getHttpHeader()->defined(HttpHeader::CONTENT_LENGTH)) {
  93. poolConnection();
  94. }
  95. return processResponse();
  96. }
  97. const size_t BUFSIZE = 16*1024;
  98. unsigned char buf[BUFSIZE];
  99. size_t bufSize = BUFSIZE;
  100. try {
  101. getSocket()->readData(buf, bufSize);
  102. if(_transferEncodingDecoder.isNull()) {
  103. _receivedBytes += bufSize;
  104. } else {
  105. // _receivedBytes is not updated if transferEncoding is set.
  106. // The return value is safely ignored here.
  107. _transferEncodingDecoder->decode(buf, bufSize);
  108. }
  109. if(_totalLength != 0 && bufSize == 0 &&
  110. !getSocket()->wantRead() && !getSocket()->wantWrite()) {
  111. throw DL_RETRY_EX(EX_GOT_EOF);
  112. }
  113. } catch(RecoverableException& e) {
  114. if(getLogger()->debug()) {
  115. getLogger()->debug(EX_EXCEPTION_CAUGHT, e);
  116. }
  117. return processResponse();
  118. }
  119. bool finished = false;
  120. if(_transferEncodingDecoder.isNull()) {
  121. if(bufSize == 0) {
  122. if(!getSocket()->wantRead() && !getSocket()->wantWrite()) {
  123. return processResponse();
  124. }
  125. } else {
  126. finished = (_totalLength == _receivedBytes);
  127. }
  128. } else {
  129. finished = _transferEncodingDecoder->finished();
  130. }
  131. if(finished) {
  132. poolConnection();
  133. return processResponse();
  134. } else {
  135. setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite());
  136. getDownloadEngine()->addCommand(this);
  137. return false;
  138. }
  139. }
  140. void HttpSkipResponseCommand::poolConnection() const
  141. {
  142. if(getRequest()->supportsPersistentConnection()) {
  143. getDownloadEngine()->poolSocket
  144. (getRequest(), createProxyRequest(), getSocket());
  145. }
  146. }
  147. bool HttpSkipResponseCommand::processResponse()
  148. {
  149. if(_httpResponse->isRedirect()) {
  150. unsigned int rnum =
  151. _httpResponse->getHttpRequest()->getRequest()->getRedirectCount();
  152. if(rnum >= Request::MAX_REDIRECT) {
  153. throw DL_ABORT_EX
  154. (StringFormat("Too many redirects: count=%u", rnum).str());
  155. }
  156. _httpResponse->processRedirect();
  157. return prepareForRetry(0);
  158. } else if(_httpResponse->getResponseStatus() >= HttpHeader::S400) {
  159. if(_httpResponse->getResponseStatus() == HttpHeader::S401) {
  160. if(getOption()->getAsBool(PREF_HTTP_AUTH_CHALLENGE) &&
  161. !_httpResponse->getHttpRequest()->authenticationUsed() &&
  162. getDownloadEngine()->getAuthConfigFactory()->activateBasicCred
  163. (getRequest()->getHost(), getRequest()->getDir(), getOption().get())) {
  164. return prepareForRetry(0);
  165. } else {
  166. throw DL_ABORT_EX(EX_AUTH_FAILED);
  167. }
  168. }else if(_httpResponse->getResponseStatus() == HttpHeader::S404) {
  169. throw DL_ABORT_EX2(MSG_RESOURCE_NOT_FOUND,
  170. downloadresultcode::RESOURCE_NOT_FOUND);
  171. } else {
  172. throw DL_ABORT_EX
  173. (StringFormat
  174. (EX_BAD_STATUS,
  175. util::parseUInt(_httpResponse->getResponseStatus())).str());
  176. }
  177. } else {
  178. return prepareForRetry(0);
  179. }
  180. }
  181. void HttpSkipResponseCommand::disableSocketCheck()
  182. {
  183. disableReadCheckSocket();
  184. disableWriteCheckSocket();
  185. }
  186. } // namespace aria2