HttpRequest.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "Request.h"
  37. #include "Segment.h"
  38. #include "Range.h"
  39. #include "Cookie.h"
  40. #include "CookieBox.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 <numeric>
  49. namespace aria2 {
  50. const std::string HttpRequest::USER_AGENT("aria2");
  51. HttpRequest::HttpRequest():entityLength(0),
  52. authEnabled(false),
  53. proxyEnabled(false),
  54. proxyAuthEnabled(false),
  55. userAgent(USER_AGENT)
  56. {}
  57. SharedHandle<Segment> HttpRequest::getSegment() const
  58. {
  59. return segment;
  60. }
  61. void HttpRequest::setSegment(const SharedHandle<Segment>& segment)
  62. {
  63. this->segment = segment;
  64. }
  65. void HttpRequest::setRequest(const SharedHandle<Request>& request)
  66. {
  67. this->request = request;
  68. }
  69. SharedHandle<Request> HttpRequest::getRequest() const
  70. {
  71. return request;
  72. }
  73. off_t HttpRequest::getStartByte() const
  74. {
  75. if(segment.isNull()) {
  76. return 0;
  77. } else {
  78. return segment->getPositionToWrite();
  79. }
  80. }
  81. off_t HttpRequest::getEndByte() const
  82. {
  83. if(segment.isNull() || request.isNull()) {
  84. return 0;
  85. } else {
  86. if(request->isPipeliningEnabled()) {
  87. return segment->getPosition()+segment->getLength()-1;
  88. } else {
  89. return 0;
  90. }
  91. }
  92. }
  93. RangeHandle HttpRequest::getRange() const
  94. {
  95. // content-length is always 0
  96. if(segment.isNull()) {
  97. return SharedHandle<Range>(new Range());
  98. } else {
  99. return SharedHandle<Range>(new Range(getStartByte(), getEndByte(), entityLength));
  100. }
  101. }
  102. bool HttpRequest::isRangeSatisfied(const RangeHandle& range) const
  103. {
  104. if(segment.isNull()) {
  105. return true;
  106. }
  107. if((getStartByte() == range->getStartByte()) &&
  108. ((getEndByte() == 0) ||
  109. ((getEndByte() > 0) && (getEndByte() == range->getEndByte()))) &&
  110. ((entityLength == 0) ||
  111. ((entityLength > 0) && (entityLength == range->getEntityLength())))) {
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. }
  117. std::string HttpRequest::getHostText(const std::string& host, uint16_t port) const
  118. {
  119. return host+(port == 80 || port == 443 ? "" : ":"+Util::uitos(port));
  120. }
  121. std::string HttpRequest::createRequest() const
  122. {
  123. std::string requestLine = "GET ";
  124. if(getProtocol() == Request::PROTO_FTP || proxyEnabled) {
  125. requestLine += getCurrentURI();
  126. } else {
  127. if(getDir() == A2STR::SLASH_C) {
  128. requestLine += getDir();
  129. } else {
  130. requestLine += getDir()+A2STR::SLASH_C;
  131. }
  132. requestLine += getFile()+getQuery();
  133. }
  134. requestLine +=
  135. std::string(" HTTP/1.1\r\n")+
  136. "User-Agent: "+userAgent+"\r\n";
  137. requestLine += "Accept: */*"; /* */
  138. for(std::deque<std::string>::const_iterator i = _acceptTypes.begin();
  139. i != _acceptTypes.end(); ++i) {
  140. requestLine += ","+(*i);
  141. }
  142. requestLine += "\r\n";
  143. requestLine +=
  144. "Host: "+getHostText(getHost(), getPort())+"\r\n"+
  145. "Pragma: no-cache\r\n"+
  146. "Cache-Control: no-cache\r\n";
  147. if(!request->isKeepAliveEnabled() && !request->isPipeliningEnabled()) {
  148. requestLine += "Connection: close\r\n";
  149. }
  150. if(!segment.isNull() && segment->getLength() > 0 &&
  151. (request->isPipeliningEnabled() || getStartByte() > 0)) {
  152. requestLine += "Range: bytes="+Util::itos(getStartByte());
  153. requestLine += "-";
  154. if(request->isPipeliningEnabled()) {
  155. requestLine += Util::itos(getEndByte());
  156. }
  157. requestLine += "\r\n";
  158. }
  159. if(proxyEnabled) {
  160. if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  161. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  162. } else {
  163. requestLine += "Proxy-Connection: close\r\n";
  164. }
  165. }
  166. if(proxyEnabled && proxyAuthEnabled) {
  167. requestLine += getProxyAuthString();
  168. }
  169. if(authEnabled) {
  170. requestLine += "Authorization: Basic "+
  171. Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfig(request)->getAuthText())+"\r\n";
  172. }
  173. if(getPreviousURI().size()) {
  174. requestLine += "Referer: "+getPreviousURI()+"\r\n";
  175. }
  176. std::string cookiesValue;
  177. Cookies cookies = request->cookieBox->criteriaFind(getHost(),
  178. getDir(),
  179. time(0),
  180. getProtocol() == Request::PROTO_HTTPS ?
  181. true : false);
  182. for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
  183. cookiesValue += (*itr).toString()+";";
  184. }
  185. if(cookiesValue.size()) {
  186. requestLine += std::string("Cookie: ")+cookiesValue+"\r\n";
  187. }
  188. // append additional headers given by user.
  189. for(std::deque<std::string>::const_iterator i = _headers.begin();
  190. i != _headers.end(); ++i) {
  191. requestLine += (*i)+"\r\n";
  192. }
  193. requestLine += "\r\n";
  194. return requestLine;
  195. }
  196. std::string HttpRequest::createProxyRequest() const
  197. {
  198. std::string requestLine =
  199. std::string("CONNECT ")+getHost()+":"+Util::uitos(getPort())+
  200. std::string(" HTTP/1.1\r\n")+
  201. "User-Agent: "+userAgent+"\r\n"+
  202. "Host: "+getHost()+":"+Util::uitos(getPort())+"\r\n";
  203. if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  204. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  205. }else {
  206. requestLine += "Proxy-Connection: close\r\n";
  207. }
  208. if(proxyAuthEnabled) {
  209. requestLine += getProxyAuthString();
  210. }
  211. requestLine += "\r\n";
  212. return requestLine;
  213. }
  214. std::string HttpRequest::getProxyAuthString() const {
  215. return "Proxy-Authorization: Basic "+
  216. Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfigForHttpProxy(request)->getAuthText())+"\r\n";
  217. }
  218. void HttpRequest::addHeader(const std::string& headersString)
  219. {
  220. std::deque<std::string> headers;
  221. Util::slice(headers, headersString, '\n', true);
  222. _headers.insert(_headers.end(), headers.begin(), headers.end());
  223. }
  224. void HttpRequest::addAcceptType(const std::string& type)
  225. {
  226. _acceptTypes.push_back(type);
  227. }
  228. void HttpRequest::configure(const Option* option)
  229. {
  230. authEnabled = option->getAsBool(PREF_HTTP_AUTH_ENABLED);
  231. proxyEnabled =
  232. option->getAsBool(PREF_HTTP_PROXY_ENABLED) &&
  233. option->get(PREF_HTTP_PROXY_METHOD) == V_GET;
  234. proxyAuthEnabled = option->getAsBool(PREF_HTTP_PROXY_AUTH_ENABLED);
  235. }
  236. const std::string& HttpRequest::getPreviousURI() const
  237. {
  238. return request->getPreviousUrl();
  239. }
  240. const std::string& HttpRequest::getHost() const
  241. {
  242. return request->getHost();
  243. }
  244. uint16_t HttpRequest::getPort() const
  245. {
  246. return request->getPort();
  247. }
  248. const std::string& HttpRequest::getMethod() const
  249. {
  250. return request->getMethod();
  251. }
  252. const std::string& HttpRequest::getProtocol() const
  253. {
  254. return request->getProtocol();
  255. }
  256. const std::string& HttpRequest::getCurrentURI() const
  257. {
  258. return request->getCurrentUrl();
  259. }
  260. const std::string& HttpRequest::getDir() const
  261. {
  262. return request->getDir();
  263. }
  264. const std::string& HttpRequest::getFile() const
  265. {
  266. return request->getFile();
  267. }
  268. const std::string& HttpRequest::getQuery() const
  269. {
  270. return request->getQuery();
  271. }
  272. } // namespace aria2