HttpConnection.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "Socket.h"
  51. #include "Option.h"
  52. #include "CookieStorage.h"
  53. #include "AuthConfigFactory.h"
  54. #include "AuthConfig.h"
  55. #include "a2functional.h"
  56. namespace aria2 {
  57. HttpRequestEntry::HttpRequestEntry
  58. (const SharedHandle<HttpRequest>& httpRequest):
  59. _httpRequest(httpRequest),
  60. _proc(new HttpHeaderProcessor()) {}
  61. HttpRequestEntry::~HttpRequestEntry() {}
  62. HttpConnection::HttpConnection(cuid_t cuid, const SocketHandle& socket):
  63. _cuid(cuid), _socket(socket),
  64. _socketBuffer(socket),
  65. _logger(LogFactory::getInstance())
  66. {}
  67. std::string HttpConnection::eraseConfidentialInfo(const std::string& request)
  68. {
  69. std::istringstream istr(request);
  70. std::string result;
  71. std::string line;
  72. while(getline(istr, line)) {
  73. static const std::string AUTH_HEADER("Authorization: Basic");
  74. static const std::string PROXY_AUTH_HEADER("Proxy-Authorization: Basic");
  75. if(util::startsWith(line, AUTH_HEADER)) {
  76. result += "Authorization: Basic ********\n";
  77. } else if(util::startsWith(line, PROXY_AUTH_HEADER)) {
  78. result += "Proxy-Authorization: Basic ********\n";
  79. } else {
  80. strappend(result, line, "\n");
  81. }
  82. }
  83. return result;
  84. }
  85. void HttpConnection::sendRequest(const SharedHandle<HttpRequest>& httpRequest)
  86. {
  87. std::string request = httpRequest->createRequest();
  88. if(_logger->info()) {
  89. _logger->info(MSG_SENDING_REQUEST,
  90. util::itos(_cuid).c_str(),
  91. eraseConfidentialInfo(request).c_str());
  92. }
  93. _socketBuffer.pushStr(request);
  94. _socketBuffer.send();
  95. SharedHandle<HttpRequestEntry> entry(new HttpRequestEntry(httpRequest));
  96. _outstandingHttpRequests.push_back(entry);
  97. }
  98. void HttpConnection::sendProxyRequest
  99. (const SharedHandle<HttpRequest>& httpRequest)
  100. {
  101. std::string request = httpRequest->createProxyRequest();
  102. if(_logger->info()) {
  103. _logger->info(MSG_SENDING_REQUEST,
  104. util::itos(_cuid).c_str(),
  105. eraseConfidentialInfo(request).c_str());
  106. }
  107. _socketBuffer.pushStr(request);
  108. _socketBuffer.send();
  109. SharedHandle<HttpRequestEntry> entry(new HttpRequestEntry(httpRequest));
  110. _outstandingHttpRequests.push_back(entry);
  111. }
  112. SharedHandle<HttpResponse> HttpConnection::receiveResponse()
  113. {
  114. if(_outstandingHttpRequests.empty()) {
  115. throw DL_ABORT_EX(EX_NO_HTTP_REQUEST_ENTRY_FOUND);
  116. }
  117. HttpRequestEntryHandle entry = _outstandingHttpRequests.front();
  118. HttpHeaderProcessorHandle proc = entry->getHttpHeaderProcessor();
  119. unsigned char buf[512];
  120. size_t size = sizeof(buf);
  121. _socket->peekData(buf, size);
  122. if(size == 0) {
  123. if(_socket->wantRead() || _socket->wantWrite()) {
  124. return SharedHandle<HttpResponse>();
  125. } else {
  126. throw DL_RETRY_EX(EX_INVALID_RESPONSE);
  127. }
  128. }
  129. proc->update(buf, size);
  130. if(!proc->eoh()) {
  131. _socket->readData(buf, size);
  132. return SharedHandle<HttpResponse>();
  133. }
  134. size_t putbackDataLength = proc->getPutBackDataLength();
  135. size -= putbackDataLength;
  136. _socket->readData(buf, size);
  137. if(_logger->info()) {
  138. _logger->info(MSG_RECEIVE_RESPONSE,
  139. util::itos(_cuid).c_str(), proc->getHeaderString().c_str());
  140. }
  141. SharedHandle<HttpHeader> httpHeader = proc->getHttpResponseHeader();
  142. SharedHandle<HttpResponse> httpResponse(new HttpResponse());
  143. httpResponse->setCuid(_cuid);
  144. httpResponse->setHttpHeader(httpHeader);
  145. httpResponse->setHttpRequest(entry->getHttpRequest());
  146. _outstandingHttpRequests.pop_front();
  147. return httpResponse;
  148. }
  149. bool HttpConnection::isIssued(const SharedHandle<Segment>& segment) const
  150. {
  151. for(HttpRequestEntries::const_iterator itr = _outstandingHttpRequests.begin(),
  152. eoi = _outstandingHttpRequests.end(); itr != eoi; ++itr) {
  153. SharedHandle<HttpRequest> httpRequest = (*itr)->getHttpRequest();
  154. if(httpRequest->getSegment() == segment) {
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. SharedHandle<HttpRequest> HttpConnection::getFirstHttpRequest() const
  161. {
  162. if(_outstandingHttpRequests.empty()) {
  163. return SharedHandle<HttpRequest>();
  164. } else {
  165. return _outstandingHttpRequests.front()->getHttpRequest();
  166. }
  167. }
  168. bool HttpConnection::sendBufferIsEmpty() const
  169. {
  170. return _socketBuffer.sendBufferIsEmpty();
  171. }
  172. void HttpConnection::sendPendingData()
  173. {
  174. _socketBuffer.send();
  175. }
  176. } // namespace aria2