HttpConnection.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <sstream>
  37. #include "util.h"
  38. #include "message.h"
  39. #include "prefs.h"
  40. #include "LogFactory.h"
  41. #include "DlRetryEx.h"
  42. #include "DlAbortEx.h"
  43. #include "Request.h"
  44. #include "Segment.h"
  45. #include "HttpRequest.h"
  46. #include "HttpResponse.h"
  47. #include "HttpHeaderProcessor.h"
  48. #include "HttpHeader.h"
  49. #include "Logger.h"
  50. #include "SocketCore.h"
  51. #include "Option.h"
  52. #include "CookieStorage.h"
  53. #include "AuthConfigFactory.h"
  54. #include "AuthConfig.h"
  55. #include "a2functional.h"
  56. #include "fmt.h"
  57. #include "SocketRecvBuffer.h"
  58. #include "array_fun.h"
  59. namespace aria2 {
  60. HttpRequestEntry::HttpRequestEntry(std::unique_ptr<HttpRequest> httpRequest)
  61. : httpRequest_{std::move(httpRequest)},
  62. proc_{
  63. make_unique<HttpHeaderProcessor>(HttpHeaderProcessor::CLIENT_PARSER)}
  64. {
  65. }
  66. void HttpRequestEntry::resetHttpHeaderProcessor()
  67. {
  68. proc_ = make_unique<HttpHeaderProcessor>(HttpHeaderProcessor::CLIENT_PARSER);
  69. }
  70. std::unique_ptr<HttpRequest> HttpRequestEntry::popHttpRequest()
  71. {
  72. return std::move(httpRequest_);
  73. }
  74. const std::unique_ptr<HttpHeaderProcessor>&
  75. HttpRequestEntry::getHttpHeaderProcessor() const
  76. {
  77. return proc_;
  78. }
  79. HttpConnection::HttpConnection(
  80. cuid_t cuid, const std::shared_ptr<SocketCore>& socket,
  81. const std::shared_ptr<SocketRecvBuffer>& socketRecvBuffer)
  82. : cuid_(cuid),
  83. socket_(socket),
  84. socketRecvBuffer_(socketRecvBuffer),
  85. socketBuffer_(socket)
  86. {
  87. }
  88. HttpConnection::~HttpConnection() = default;
  89. std::string HttpConnection::eraseConfidentialInfo(const std::string& request)
  90. {
  91. std::istringstream istr(request);
  92. std::string result;
  93. std::string line;
  94. while (getline(istr, line)) {
  95. if (util::startsWith(line, "Authorization: Basic")) {
  96. result += "Authorization: Basic ********\n";
  97. }
  98. else if (util::startsWith(line, "Proxy-Authorization: Basic")) {
  99. result += "Proxy-Authorization: Basic ********\n";
  100. }
  101. else {
  102. result += line;
  103. result += "\n";
  104. }
  105. }
  106. return result;
  107. }
  108. void HttpConnection::sendRequest(std::unique_ptr<HttpRequest> httpRequest,
  109. std::string request)
  110. {
  111. A2_LOG_INFO(
  112. fmt(MSG_SENDING_REQUEST, cuid_, eraseConfidentialInfo(request).c_str()));
  113. socketBuffer_.pushStr(std::move(request));
  114. socketBuffer_.send();
  115. outstandingHttpRequests_.push_back(
  116. make_unique<HttpRequestEntry>(std::move(httpRequest)));
  117. }
  118. void HttpConnection::sendRequest(std::unique_ptr<HttpRequest> httpRequest)
  119. {
  120. auto req = httpRequest->createRequest();
  121. sendRequest(std::move(httpRequest), std::move(req));
  122. }
  123. void HttpConnection::sendProxyRequest(std::unique_ptr<HttpRequest> httpRequest)
  124. {
  125. auto req = httpRequest->createProxyRequest();
  126. sendRequest(std::move(httpRequest), std::move(req));
  127. }
  128. std::unique_ptr<HttpResponse> HttpConnection::receiveResponse()
  129. {
  130. if (outstandingHttpRequests_.empty()) {
  131. throw DL_ABORT_EX(EX_NO_HTTP_REQUEST_ENTRY_FOUND);
  132. }
  133. if (socketRecvBuffer_->bufferEmpty()) {
  134. if (socketRecvBuffer_->recv() == 0 && !socket_->wantRead() &&
  135. !socket_->wantWrite()) {
  136. throw DL_RETRY_EX(EX_GOT_EOF);
  137. }
  138. }
  139. const auto& proc = outstandingHttpRequests_.front()->getHttpHeaderProcessor();
  140. if (proc->parse(socketRecvBuffer_->getBuffer(),
  141. socketRecvBuffer_->getBufferLength())) {
  142. A2_LOG_INFO(
  143. fmt(MSG_RECEIVE_RESPONSE, cuid_, proc->getHeaderString().c_str()));
  144. auto result = proc->getResult();
  145. if (result->getStatusCode() / 100 == 1) {
  146. socketRecvBuffer_->drain(proc->getLastBytesProcessed());
  147. outstandingHttpRequests_.front()->resetHttpHeaderProcessor();
  148. return nullptr;
  149. }
  150. auto httpResponse = make_unique<HttpResponse>();
  151. httpResponse->setCuid(cuid_);
  152. httpResponse->setHttpHeader(std::move(result));
  153. httpResponse->setHttpRequest(
  154. outstandingHttpRequests_.front()->popHttpRequest());
  155. socketRecvBuffer_->drain(proc->getLastBytesProcessed());
  156. outstandingHttpRequests_.pop_front();
  157. return httpResponse;
  158. }
  159. socketRecvBuffer_->drain(proc->getLastBytesProcessed());
  160. return nullptr;
  161. }
  162. bool HttpConnection::isIssued(const std::shared_ptr<Segment>& segment) const
  163. {
  164. for (const auto& entry : outstandingHttpRequests_) {
  165. if (*entry->getHttpRequest()->getSegment() == *segment) {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. bool HttpConnection::sendBufferIsEmpty() const
  172. {
  173. return socketBuffer_.sendBufferIsEmpty();
  174. }
  175. void HttpConnection::sendPendingData() { socketBuffer_.send(); }
  176. } // namespace aria2