HttpRequest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "Util.h"
  37. #include "Base64.h"
  38. #include "prefs.h"
  39. RangeHandle HttpRequest::getRange() const
  40. {
  41. // content-length is always 0
  42. if(segment.isNull()) {
  43. return new Range(0, 0, 0);
  44. } else {
  45. return new Range(getStartByte(), getEndByte(), entityLength);
  46. }
  47. }
  48. bool HttpRequest::isRangeSatisfied(const RangeHandle& range) const
  49. {
  50. if(segment.isNull()) {
  51. return true;
  52. }
  53. if(getStartByte() == range->getStartByte() &&
  54. (getEndByte() == 0 ||
  55. getEndByte() > 0 && getEndByte() == range->getEndByte()) &&
  56. (entityLength == 0 ||
  57. entityLength > 0 && entityLength == range->getEntityLength())) {
  58. return true;
  59. } else {
  60. return false;
  61. }
  62. }
  63. string HttpRequest::getHostText(const string& host, int32_t port) const
  64. {
  65. return host+(port == 80 || port == 443 ? "" : ":"+Util::llitos(port));
  66. }
  67. string HttpRequest::createRequest() const
  68. {
  69. string requestLine = "GET ";
  70. if(getProtocol() == "ftp" || proxyEnabled) {
  71. requestLine += getCurrentURI();
  72. } else {
  73. if(getDir() == "/") {
  74. requestLine += getDir();
  75. } else {
  76. requestLine += getDir()+"/";
  77. }
  78. requestLine += getFile();
  79. }
  80. requestLine +=
  81. string(" HTTP/1.1\r\n")+
  82. "User-Agent: "+userAgent+"\r\n"+
  83. "Accept: */*\r\n"+ /* */
  84. "Host: "+getHostText(getHost(), getPort())+"\r\n"+
  85. "Pragma: no-cache\r\n"+
  86. "Cache-Control: no-cache\r\n";
  87. if(!request->isKeepAlive()) {
  88. requestLine += "Connection: close\r\n";
  89. }
  90. if(!segment.isNull() && segment->getLength() > 0 &&
  91. (request->isKeepAlive() || getStartByte() > 0)) {
  92. requestLine += "Range: bytes="+Util::llitos(getStartByte());
  93. requestLine += "-";
  94. if(request->isKeepAlive()) {
  95. requestLine += Util::llitos(getEndByte());
  96. }
  97. requestLine += "\r\n";
  98. }
  99. if(proxyEnabled) {
  100. requestLine += "Proxy-Connection: close\r\n";
  101. }
  102. if(proxyEnabled && proxyAuthEnabled) {
  103. requestLine += getProxyAuthString();
  104. }
  105. if(authEnabled) {
  106. requestLine += "Authorization: Basic "+
  107. Base64::encode(request->resolveHttpAuthConfig()->getAuthText())+"\r\n";
  108. }
  109. if(getPreviousURI().size()) {
  110. requestLine += "Referer: "+getPreviousURI()+"\r\n";
  111. }
  112. string cookiesValue;
  113. Cookies cookies = request->cookieBox->criteriaFind(getHost(),
  114. getDir(),
  115. time(0),
  116. getProtocol() == "https" ?
  117. true : false);
  118. for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
  119. cookiesValue += (*itr).toString()+";";
  120. }
  121. if(cookiesValue.size()) {
  122. requestLine += string("Cookie: ")+cookiesValue+"\r\n";
  123. }
  124. requestLine += "\r\n";
  125. return requestLine;
  126. }
  127. string HttpRequest::createProxyRequest() const
  128. {
  129. string requestLine =
  130. string("CONNECT ")+getHost()+":"+Util::itos(getPort())+
  131. string(" HTTP/1.1\r\n")+
  132. "User-Agent: "+Util::urlencode(userAgent)+"\r\n"+
  133. "Proxy-Connection: close\r\n"+
  134. "Host: "+getHost()+":"+Util::itos(getPort())+"\r\n";
  135. if(proxyAuthEnabled) {
  136. requestLine += getProxyAuthString();
  137. }
  138. requestLine += "\r\n";
  139. return requestLine;
  140. }
  141. string HttpRequest::getProxyAuthString() const {
  142. return "Proxy-Authorization: Basic "+
  143. Base64::encode(request->resolveHttpProxyAuthConfig()->getAuthText())+"\r\n";
  144. }
  145. void HttpRequest::configure(const Option* option)
  146. {
  147. authEnabled = option->get(PREF_HTTP_AUTH_ENABLED) == V_TRUE;
  148. proxyEnabled =
  149. option->get(PREF_HTTP_PROXY_ENABLED) == V_TRUE &&
  150. option->get(PREF_HTTP_PROXY_METHOD) == V_GET;
  151. proxyAuthEnabled = option->get(PREF_HTTP_PROXY_AUTH_ENABLED) == V_TRUE;
  152. }