HttpRequest.cc 9.5 KB

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