HttpRequest.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 "HttpRequest.h"
  36. #include <cassert>
  37. #include <numeric>
  38. #include "Segment.h"
  39. #include "Range.h"
  40. #include "CookieStorage.h"
  41. #include "Option.h"
  42. #include "Util.h"
  43. #include "Base64.h"
  44. #include "prefs.h"
  45. #include "AuthConfigFactory.h"
  46. #include "AuthConfig.h"
  47. #include "a2functional.h"
  48. #include "TimeA2.h"
  49. namespace aria2 {
  50. const std::string HttpRequest::USER_AGENT("aria2");
  51. HttpRequest::HttpRequest():entityLength(0),
  52. _contentEncodingEnabled(true),
  53. userAgent(USER_AGENT)
  54. {}
  55. void HttpRequest::setSegment(const SharedHandle<Segment>& segment)
  56. {
  57. this->segment = segment;
  58. }
  59. void HttpRequest::setRequest(const SharedHandle<Request>& request)
  60. {
  61. this->request = request;
  62. }
  63. off_t HttpRequest::getStartByte() const
  64. {
  65. if(segment.isNull()) {
  66. return 0;
  67. } else {
  68. return _fileEntry->gtoloff(segment->getPositionToWrite());
  69. }
  70. }
  71. off_t HttpRequest::getEndByte() const
  72. {
  73. if(segment.isNull() || request.isNull()) {
  74. return 0;
  75. } else {
  76. if(request->isPipeliningEnabled()) {
  77. return _fileEntry->gtoloff(segment->getPosition()+segment->getLength()-1);
  78. } else {
  79. return 0;
  80. }
  81. }
  82. }
  83. RangeHandle HttpRequest::getRange() const
  84. {
  85. // content-length is always 0
  86. if(segment.isNull()) {
  87. return SharedHandle<Range>(new Range());
  88. } else {
  89. return SharedHandle<Range>(new Range(getStartByte(), getEndByte(), entityLength));
  90. }
  91. }
  92. bool HttpRequest::isRangeSatisfied(const RangeHandle& range) const
  93. {
  94. if(segment.isNull()) {
  95. return true;
  96. }
  97. if((getStartByte() == range->getStartByte()) &&
  98. ((getEndByte() == 0) ||
  99. ((getEndByte() > 0) && (getEndByte() == range->getEndByte()))) &&
  100. ((entityLength == 0) ||
  101. ((entityLength > 0) && (entityLength == range->getEntityLength())))) {
  102. return true;
  103. } else {
  104. return false;
  105. }
  106. }
  107. std::string HttpRequest::getHostText(const std::string& host, uint16_t port) const
  108. {
  109. std::string hosttext = host;
  110. if(!(port == 80 || port == 443)) {
  111. strappend(hosttext, ":", Util::uitos(port));
  112. }
  113. return hosttext;
  114. }
  115. std::string HttpRequest::createRequest()
  116. {
  117. _authConfig = _authConfigFactory->createAuthConfig(request);
  118. std::string requestLine = request->getMethod();
  119. requestLine += " ";
  120. if(!_proxyRequest.isNull()) {
  121. if(getProtocol() == Request::PROTO_FTP &&
  122. request->getUsername().empty() && !_authConfig.isNull()) {
  123. // Insert user into URI, like ftp://USER@host/
  124. std::string uri = getCurrentURI();
  125. assert(uri.size() >= 6);
  126. uri.insert(6, Util::urlencode(_authConfig->getUser())+"@");
  127. requestLine += uri;
  128. } else {
  129. requestLine += getCurrentURI();
  130. }
  131. } else {
  132. if(getDir() == A2STR::SLASH_C) {
  133. requestLine += getDir();
  134. } else {
  135. requestLine += getDir();
  136. requestLine += A2STR::SLASH_C;
  137. }
  138. requestLine += getFile();
  139. requestLine += getQuery();
  140. }
  141. requestLine += " HTTP/1.1\r\n";
  142. strappend(requestLine, "User-Agent: ", userAgent, "\r\n");
  143. requestLine += "Accept: */*"; /* */
  144. for(std::deque<std::string>::const_iterator i = _acceptTypes.begin();
  145. i != _acceptTypes.end(); ++i) {
  146. strappend(requestLine, ",", (*i));
  147. }
  148. requestLine += "\r\n";
  149. if(_contentEncodingEnabled) {
  150. std::string acceptableEncodings;
  151. #ifdef HAVE_LIBZ
  152. acceptableEncodings += "deflate, gzip";
  153. #endif // HAVE_LIBZ
  154. if(!acceptableEncodings.empty()) {
  155. strappend(requestLine, "Accept-Encoding: ", acceptableEncodings, "\r\n");
  156. }
  157. }
  158. strappend(requestLine, "Host: ", getHostText(getHost(), getPort()), "\r\n");
  159. requestLine += "Pragma: no-cache\r\n";
  160. requestLine += "Cache-Control: no-cache\r\n";
  161. if(!request->isKeepAliveEnabled() && !request->isPipeliningEnabled()) {
  162. requestLine += "Connection: close\r\n";
  163. }
  164. if(!segment.isNull() && segment->getLength() > 0 &&
  165. (request->isPipeliningEnabled() || getStartByte() > 0)) {
  166. requestLine += "Range: bytes=";
  167. requestLine += Util::itos(getStartByte());
  168. requestLine += "-";
  169. if(request->isPipeliningEnabled()) {
  170. requestLine += Util::itos(getEndByte());
  171. }
  172. requestLine += "\r\n";
  173. }
  174. if(!_proxyRequest.isNull()) {
  175. if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  176. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  177. } else {
  178. requestLine += "Proxy-Connection: close\r\n";
  179. }
  180. }
  181. if(!_proxyRequest.isNull() && !_proxyRequest->getUsername().empty()) {
  182. requestLine += getProxyAuthString();
  183. }
  184. if(!_authConfig.isNull()) {
  185. strappend(requestLine, "Authorization: Basic ",
  186. Base64::encode(_authConfig->getAuthText()), "\r\n");
  187. }
  188. if(getPreviousURI().size()) {
  189. strappend(requestLine, "Referer: ", getPreviousURI(), "\r\n");
  190. }
  191. if(!_cookieStorage.isNull()) {
  192. std::string cookiesValue;
  193. std::deque<Cookie> cookies =
  194. _cookieStorage->criteriaFind(getHost(),
  195. getDir(),
  196. Time().getTime(),
  197. getProtocol() == Request::PROTO_HTTPS ?
  198. true : false);
  199. for(std::deque<Cookie>::const_iterator itr = cookies.begin();
  200. itr != cookies.end(); ++itr) {
  201. strappend(cookiesValue, (*itr).toString(), ";");
  202. }
  203. if(!cookiesValue.empty()) {
  204. strappend(requestLine, "Cookie: ", cookiesValue, "\r\n");
  205. }
  206. }
  207. // append additional headers given by user.
  208. for(std::deque<std::string>::const_iterator i = _headers.begin();
  209. i != _headers.end(); ++i) {
  210. strappend(requestLine, (*i), "\r\n");
  211. }
  212. requestLine += "\r\n";
  213. return requestLine;
  214. }
  215. std::string HttpRequest::createProxyRequest() const
  216. {
  217. assert(!_proxyRequest.isNull());
  218. std::string hostport = getHost();
  219. strappend(hostport, ":", Util::uitos(getPort()));
  220. std::string requestLine = "CONNECT ";
  221. strappend(requestLine, hostport, " HTTP/1.1\r\n");
  222. strappend(requestLine, "User-Agent: ", userAgent, "\r\n");
  223. strappend(requestLine, "Host: ", hostport, "\r\n");
  224. // TODO Is "Proxy-Connection" needed here?
  225. // if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  226. // requestLine += "Proxy-Connection: Keep-Alive\r\n";
  227. // }else {
  228. // requestLine += "Proxy-Connection: close\r\n";
  229. // }
  230. if(!_proxyRequest->getUsername().empty()) {
  231. requestLine += getProxyAuthString();
  232. }
  233. requestLine += "\r\n";
  234. return requestLine;
  235. }
  236. std::string HttpRequest::getProxyAuthString() const
  237. {
  238. return strconcat("Proxy-Authorization: Basic ",
  239. Base64::encode(strconcat(_proxyRequest->getUsername(),
  240. ":",
  241. _proxyRequest->getPassword())),
  242. "\r\n");
  243. }
  244. void HttpRequest::enableContentEncoding()
  245. {
  246. _contentEncodingEnabled = true;
  247. }
  248. void HttpRequest::disableContentEncoding()
  249. {
  250. _contentEncodingEnabled = false;
  251. }
  252. void HttpRequest::addHeader(const std::string& headersString)
  253. {
  254. std::deque<std::string> headers;
  255. Util::slice(headers, headersString, '\n', true);
  256. _headers.insert(_headers.end(), headers.begin(), headers.end());
  257. }
  258. void HttpRequest::addAcceptType(const std::string& type)
  259. {
  260. _acceptTypes.push_back(type);
  261. }
  262. void HttpRequest::setCookieStorage
  263. (const SharedHandle<CookieStorage>& cookieStorage)
  264. {
  265. _cookieStorage = cookieStorage;
  266. }
  267. void HttpRequest::setAuthConfigFactory
  268. (const SharedHandle<AuthConfigFactory>& factory)
  269. {
  270. _authConfigFactory = factory;
  271. }
  272. void HttpRequest::setProxyRequest(const SharedHandle<Request>& proxyRequest)
  273. {
  274. _proxyRequest = proxyRequest;
  275. }
  276. bool HttpRequest::isProxyRequestSet() const
  277. {
  278. return !_proxyRequest.isNull();
  279. }
  280. bool HttpRequest::authenticationUsed() const
  281. {
  282. return !_authConfig.isNull();
  283. }
  284. const SharedHandle<AuthConfig>& HttpRequest::getAuthConfig() const
  285. {
  286. return _authConfig;
  287. }
  288. } // namespace aria2