HttpSkipResponseCommand.cc 4.9 KB

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