HttpRequest.cc 9.5 KB

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