HttpSkipResponseCommand.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. namespace aria2 {
  54. HttpSkipResponseCommand::HttpSkipResponseCommand
  55. (int cuid,
  56. const SharedHandle<Request>& req,
  57. RequestGroup* requestGroup,
  58. const SharedHandle<HttpConnection>& httpConnection,
  59. const SharedHandle<HttpResponse>& httpResponse,
  60. DownloadEngine* e,
  61. const SharedHandle<SocketCore>& s):
  62. AbstractCommand(cuid, req, requestGroup, e, s),
  63. _httpConnection(httpConnection),
  64. _httpResponse(httpResponse),
  65. _totalLength(_httpResponse->getEntityLength()),
  66. _receivedBytes(0)
  67. {}
  68. HttpSkipResponseCommand::~HttpSkipResponseCommand() {}
  69. void HttpSkipResponseCommand::setTransferEncodingDecoder
  70. (const SharedHandle<Decoder>& decoder)
  71. {
  72. _transferEncodingDecoder = decoder;
  73. }
  74. bool HttpSkipResponseCommand::executeInternal()
  75. {
  76. if(_totalLength == 0 && _transferEncodingDecoder.isNull()) {
  77. return processResponse();
  78. }
  79. const size_t BUFSIZE = 16*1024;
  80. unsigned char buf[BUFSIZE];
  81. size_t bufSize = BUFSIZE;
  82. try {
  83. socket->readData(buf, bufSize);
  84. if(_transferEncodingDecoder.isNull()) {
  85. _receivedBytes += bufSize;
  86. } else {
  87. // _receivedBytes is not updated if transferEncoding is set.
  88. // The return value is safely ignored here.
  89. _transferEncodingDecoder->decode(buf, bufSize);
  90. }
  91. if(_totalLength != 0 && bufSize == 0) {
  92. throw DlRetryEx(EX_GOT_EOF);
  93. }
  94. } catch(RecoverableException& e) {
  95. logger->debug(EX_EXCEPTION_CAUGHT, e);
  96. return processResponse();
  97. }
  98. if(bufSize == 0) {
  99. // Since this method is called by DownloadEngine only when the socket is
  100. // readable, bufSize == 0 means server shutdown the connection.
  101. // So socket cannot be reused in this case.
  102. return prepareForRetry(0);
  103. } else if((!_transferEncodingDecoder.isNull() &&
  104. _transferEncodingDecoder->finished())
  105. || (_transferEncodingDecoder.isNull() &&
  106. _totalLength == _receivedBytes)) {
  107. if(!e->option->getAsBool(PREF_HTTP_PROXY_ENABLED) &&
  108. req->supportsPersistentConnection()) {
  109. std::pair<std::string, uint16_t> peerInfo;
  110. socket->getPeerInfo(peerInfo);
  111. e->poolSocket(peerInfo.first, peerInfo.second, socket);
  112. }
  113. return processResponse();
  114. } else {
  115. e->commands.push_back(this);
  116. return false;
  117. }
  118. }
  119. bool HttpSkipResponseCommand::processResponse()
  120. {
  121. if(_httpResponse->isRedirect()) {
  122. unsigned int rnum =
  123. _httpResponse->getHttpRequest()->getRequest()->getRedirectCount();
  124. if(rnum >= Request::MAX_REDIRECT) {
  125. throw DlAbortEx(StringFormat("Too many redirects: count=%u", rnum).str());
  126. }
  127. _httpResponse->processRedirect();
  128. logger->info(MSG_REDIRECT, cuid, _httpResponse->getRedirectURI().c_str());
  129. return prepareForRetry(0);
  130. } else if(_httpResponse->hasRetryAfter()) {
  131. return prepareForRetry(_httpResponse->getRetryAfter());
  132. } else if(_httpResponse->getResponseStatus() >= HttpHeader::S400) {
  133. if(_httpResponse->getResponseStatus() == HttpHeader::S401) {
  134. throw DlAbortEx(EX_AUTH_FAILED);
  135. }else if(_httpResponse->getResponseStatus() == HttpHeader::S404) {
  136. throw DlAbortEx(MSG_RESOURCE_NOT_FOUND);
  137. } else {
  138. throw DlAbortEx(StringFormat(EX_BAD_STATUS, Util::parseUInt(_httpResponse->getResponseStatus())).str());
  139. }
  140. } else {
  141. return prepareForRetry(0);
  142. }
  143. }
  144. } // namespace aria2