HttpConnection.cc 5.8 KB

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