HttpConnection.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "HttpConnection.h"
  36. #include "Util.h"
  37. #include "message.h"
  38. #include "prefs.h"
  39. #include "LogFactory.h"
  40. #include "DlRetryEx.h"
  41. #include "DlAbortEx.h"
  42. #include "Request.h"
  43. #include "Segment.h"
  44. #include "HttpRequest.h"
  45. #include "HttpResponse.h"
  46. #include "HttpHeaderProcessor.h"
  47. #include "HttpHeader.h"
  48. #include "Logger.h"
  49. #include "Socket.h"
  50. #include <sstream>
  51. namespace aria2 {
  52. HttpRequestEntry::HttpRequestEntry(const HttpRequestHandle& httpRequest,
  53. const HttpHeaderProcessorHandle& proc):
  54. _httpRequest(httpRequest),
  55. _proc(proc) {}
  56. HttpRequestEntry::~HttpRequestEntry() {}
  57. HttpRequestHandle HttpRequestEntry::getHttpRequest() const
  58. {
  59. return _httpRequest;
  60. }
  61. HttpHeaderProcessorHandle HttpRequestEntry::getHttpHeaderProcessor() const
  62. {
  63. return _proc;
  64. }
  65. HttpConnection::HttpConnection(int32_t cuid,
  66. const SocketHandle& socket,
  67. const Option* op):
  68. cuid(cuid), socket(socket), option(op), logger(LogFactory::getInstance())
  69. {}
  70. std::string HttpConnection::eraseConfidentialInfo(const std::string& request)
  71. {
  72. std::istringstream istr(request);
  73. std::ostringstream ostr;
  74. std::string line;
  75. while(getline(istr, line)) {
  76. if(Util::startsWith(line, "Authorization: Basic")) {
  77. ostr << "Authorization: Basic ********\n";
  78. } else if(Util::startsWith(line, "Proxy-Authorization: Basic")) {
  79. ostr << "Proxy-Authorization: Basic ********\n";
  80. } else {
  81. ostr << line << "\n";
  82. }
  83. }
  84. return ostr.str();
  85. }
  86. void HttpConnection::sendRequest(const HttpRequestHandle& httpRequest)
  87. {
  88. std::string request = httpRequest->createRequest();
  89. logger->info(MSG_SENDING_REQUEST, cuid, eraseConfidentialInfo(request).c_str());
  90. socket->writeData(request.c_str(), request.size());
  91. outstandingHttpRequests.push_back(new HttpRequestEntry(httpRequest,
  92. new HttpHeaderProcessor()));
  93. }
  94. void HttpConnection::sendProxyRequest(const HttpRequestHandle& httpRequest)
  95. {
  96. std::string request = httpRequest->createProxyRequest();
  97. logger->info(MSG_SENDING_REQUEST, cuid, eraseConfidentialInfo(request).c_str());
  98. socket->writeData(request.c_str(), request.size());
  99. outstandingHttpRequests.push_back(new HttpRequestEntry(httpRequest,
  100. new HttpHeaderProcessor()));
  101. }
  102. HttpResponseHandle HttpConnection::receiveResponse()
  103. {
  104. if(outstandingHttpRequests.size() == 0) {
  105. throw new DlAbortEx(EX_NO_HTTP_REQUEST_ENTRY_FOUND);
  106. }
  107. HttpRequestEntryHandle entry = outstandingHttpRequests.front();
  108. HttpHeaderProcessorHandle proc = entry->getHttpHeaderProcessor();
  109. char buf[512];
  110. int32_t size = sizeof(buf);
  111. socket->peekData(buf, size);
  112. if(size == 0) {
  113. throw new DlRetryEx(EX_INVALID_RESPONSE);
  114. }
  115. proc->update(buf, size);
  116. if(!proc->eoh()) {
  117. socket->readData(buf, size);
  118. return 0;
  119. }
  120. int32_t putbackDataLength = proc->getPutBackDataLength();
  121. size -= putbackDataLength;
  122. socket->readData(buf, size);
  123. // OK, we got all headers.
  124. logger->info(MSG_RECEIVE_RESPONSE, cuid, proc->getHeaderString().c_str());
  125. std::pair<std::string, HttpHeaderHandle> httpStatusHeader = proc->getHttpStatusHeader();
  126. if(Util::toLower(httpStatusHeader.second->getFirst("Connection")).find("close") != std::string::npos) {
  127. entry->getHttpRequest()->getRequest()->setKeepAlive(false);
  128. }
  129. HttpResponseHandle httpResponse = new HttpResponse();
  130. httpResponse->setCuid(cuid);
  131. httpResponse->setStatus(Util::parseInt(httpStatusHeader.first));
  132. httpResponse->setHttpHeader(httpStatusHeader.second);
  133. httpResponse->setHttpRequest(entry->getHttpRequest());
  134. outstandingHttpRequests.pop_front();
  135. return httpResponse;
  136. }
  137. bool HttpConnection::isIssued(const SegmentHandle& segment) const
  138. {
  139. for(HttpRequestEntries::const_iterator itr = outstandingHttpRequests.begin();
  140. itr != outstandingHttpRequests.end(); ++itr) {
  141. HttpRequestHandle httpRequest = (*itr)->getHttpRequest();
  142. if(httpRequest->getSegment() == segment) {
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. } // namespace aria2