HttpConnection.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "HttpConnection.h"
  23. #include "DlRetryEx.h"
  24. #include "Util.h"
  25. #include "Base64.h"
  26. #include "message.h"
  27. #include "prefs.h"
  28. #include "LogFactory.h"
  29. HttpConnection::HttpConnection(int cuid, const SocketHandle& socket,
  30. const Request* req, const Option* op):
  31. cuid(cuid), socket(socket), req(req), option(op), headerBufLength(0) {
  32. logger = LogFactory::getInstance();
  33. }
  34. void HttpConnection::sendRequest(const Segment& segment) const {
  35. string request = createRequest(segment);
  36. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  37. socket->writeData(request.c_str(), request.size());
  38. }
  39. void HttpConnection::sendProxyRequest() const {
  40. string request =
  41. string("CONNECT ")+req->getHost()+":"+Util::llitos(req->getPort())+
  42. string(" HTTP/1.1\r\n")+
  43. "User-Agent: "+USER_AGENT+"\r\n"+
  44. "Proxy-Connection: close\r\n"+
  45. "Host: "+getHost(req->getHost(), req->getPort())+"\r\n";
  46. if(useProxyAuth()) {
  47. request += getProxyAuthString();
  48. }
  49. request += "\r\n";
  50. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  51. socket->writeData(request.c_str(), request.size());
  52. }
  53. string HttpConnection::getProxyAuthString() const {
  54. return "Proxy-Authorization: Basic "+
  55. Base64::encode(option->get(PREF_HTTP_PROXY_USER)+":"+
  56. option->get(PREF_HTTP_PROXY_PASSWD))+"\r\n";
  57. }
  58. string HttpConnection::getHost(const string& host, int port) const {
  59. return host+(port == 80 || port == 443 ? "" : ":"+Util::llitos(port));
  60. }
  61. string HttpConnection::createRequest(const Segment& segment) const {
  62. string request = string("GET ")+
  63. (req->getProtocol() == "ftp" || useProxy() && useProxyGet() ?
  64. req->getCurrentUrl() :
  65. ((req->getDir() == "/" ? "/" : req->getDir()+"/")+req->getFile()))+
  66. string(" HTTP/1.1\r\n")+
  67. "User-Agent: "+USER_AGENT+"\r\n"+
  68. "Connection: close\r\n"+
  69. "Accept: */*\r\n"+ /* */
  70. "Host: "+getHost(req->getHost(), req->getPort())+"\r\n"+
  71. "Pragma: no-cache\r\n"+
  72. "Cache-Control: no-cache\r\n";
  73. if(segment.sp+segment.ds > 0) {
  74. request += "Range: bytes="+
  75. Util::llitos(segment.sp+segment.ds)+"-"+Util::llitos(segment.ep)+"\r\n";
  76. }
  77. if(useProxy() && useProxyAuth() && useProxyGet()) {
  78. request += "Proxy-Connection: close\r\n";
  79. request += getProxyAuthString();
  80. }
  81. if(option->get(PREF_HTTP_AUTH_ENABLED) == V_TRUE) {
  82. if(option->get(PREF_HTTP_AUTH_SCHEME) == V_BASIC) {
  83. request += "Authorization: Basic "+
  84. Base64::encode(option->get(PREF_HTTP_USER)+":"+
  85. option->get(PREF_HTTP_PASSWD))+"\r\n";
  86. }
  87. }
  88. if(req->getPreviousUrl().size()) {
  89. request += "Referer: "+req->getPreviousUrl()+"\r\n";
  90. }
  91. string cookiesValue;
  92. Cookies cookies = req->cookieBox->criteriaFind(req->getHost(), req->getDir(), req->getProtocol() == "https" ? true : false);
  93. for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
  94. cookiesValue += (*itr).toString()+";";
  95. }
  96. if(cookiesValue.size()) {
  97. request += string("Cookie: ")+cookiesValue+"\r\n";
  98. }
  99. request += "\r\n";
  100. return request;
  101. }
  102. int HttpConnection::findEndOfHeader(const char* buf, const char* substr, int bufLength) const {
  103. const char* p = buf;
  104. while(bufLength > p-buf && bufLength-(p-buf) >= (int)strlen(substr)) {
  105. if(memcmp(p, substr, strlen(substr)) == 0) {
  106. return p-buf;
  107. }
  108. p++;
  109. }
  110. return -1;
  111. }
  112. int HttpConnection::receiveResponse(HttpHeader& headers) {
  113. //char buf[512];
  114. string header;
  115. int delimiterSwitch = 0;
  116. char* delimiters[] = { "\r\n", "\n" };
  117. int size = HEADERBUF_SIZE-headerBufLength;
  118. if(size < 0) {
  119. // TODO too large header
  120. throw new DlRetryEx("too large header > 4096");
  121. }
  122. socket->peekData(headerBuf+headerBufLength, size);
  123. if(size == 0) {
  124. throw new DlRetryEx(EX_INVALID_RESPONSE);
  125. }
  126. //buf[size] = '\0';
  127. int hlenTemp = headerBufLength+size;
  128. //header += buf;
  129. //string::size_type p;
  130. int eohIndex;
  131. if((eohIndex = findEndOfHeader(headerBuf, "\r\n\r\n", hlenTemp)) == -1 &&
  132. (eohIndex = findEndOfHeader(headerBuf, "\n\n", hlenTemp)) == -1) {
  133. socket->readData(headerBuf+headerBufLength, size);
  134. headerBufLength += size;
  135. } else {
  136. if(eohIndex[headerBuf] == '\n') {
  137. // for crapping non-standard HTTP server
  138. delimiterSwitch = 1;
  139. } else {
  140. delimiterSwitch = 0;
  141. }
  142. headerBuf[eohIndex+strlen(delimiters[delimiterSwitch])*2] = '\0';
  143. header = headerBuf;
  144. size = eohIndex+strlen(delimiters[delimiterSwitch])*2-headerBufLength;
  145. socket->readData(headerBuf+headerBufLength, size);
  146. }
  147. if(!Util::endsWith(header, "\r\n\r\n") && !Util::endsWith(header, "\n\n")) {
  148. return 0;
  149. }
  150. // OK, we got all headers.
  151. logger->info(MSG_RECEIVE_RESPONSE, cuid, header.c_str());
  152. string::size_type p, np;
  153. p = np = 0;
  154. np = header.find(delimiters[delimiterSwitch], p);
  155. if(np == string::npos) {
  156. throw new DlRetryEx(EX_NO_STATUS_HEADER);
  157. }
  158. // check HTTP status value
  159. if(header.size() <= 12) {
  160. throw new DlRetryEx(EX_NO_STATUS_HEADER);
  161. }
  162. string status = header.substr(9, 3);
  163. p = np+2;
  164. // retreive status name-value pairs, then push these into map
  165. while((np = header.find(delimiters[delimiterSwitch], p)) != string::npos && np != p) {
  166. string line = header.substr(p, np-p);
  167. p = np+2;
  168. pair<string, string> hp;
  169. Util::split(hp, line, ':');
  170. headers.put(hp.first, hp.second);
  171. }
  172. return (int)strtol(status.c_str(), NULL, 10);
  173. }
  174. bool HttpConnection::useProxy() const {
  175. return option->get(PREF_HTTP_PROXY_ENABLED) == V_TRUE;
  176. }
  177. bool HttpConnection::useProxyAuth() const {
  178. return option->get(PREF_HTTP_PROXY_AUTH_ENABLED) == V_TRUE;
  179. }
  180. bool HttpConnection::useProxyGet() const {
  181. return option->get(PREF_HTTP_PROXY_METHOD) == V_GET;
  182. }