diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2006-02-21 12:27:17 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2006-02-21 12:27:17 +0000 |
| commit | 5a8710676d58be3b3f3606993e802c6a075dbd89 (patch) | |
| tree | 634e1f7be2dffb00c4248323c1465f22aedb6f60 /src/HttpConnection.cc | |
| parent | 6d6f25b75c1557f2b92e51fe589aadc48a1fdb68 (diff) | |
Diffstat (limited to 'src/HttpConnection.cc')
| -rw-r--r-- | src/HttpConnection.cc | 88 |
1 files changed, 35 insertions, 53 deletions
diff --git a/src/HttpConnection.cc b/src/HttpConnection.cc index b22d9126..9f373d33 100644 --- a/src/HttpConnection.cc +++ b/src/HttpConnection.cc @@ -26,15 +26,16 @@ #include "Base64.h" #include "message.h" -HttpConnection::HttpConnection(int cuid, Socket* socket, const Option* op, Logger* logger):cuid(cuid), socket(socket),option(op),logger(logger) {} +HttpConnection::HttpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger): + cuid(cuid), socket(socket), req(req), option(op), logger(logger) {} -void HttpConnection::sendRequest(const Request* req, const Segment& segment) { - string request = createRequest(req, segment); +void HttpConnection::sendRequest(const Segment& segment) const { + string request = createRequest(segment); logger->info(MSG_SENDING_HTTP_REQUEST, cuid, request.c_str()); socket->writeData(request.c_str(), request.size()); } -void HttpConnection::sendProxyRequest(const Request* req) { +void HttpConnection::sendProxyRequest() const { string request = string("CONNECT ")+req->getHost()+":"+Util::llitos(req->getPort())+ string(" HTTP/1.1\r\n")+ "Host: "+getHost(req->getHost(), req->getPort())+"\r\n"; @@ -47,19 +48,19 @@ void HttpConnection::sendProxyRequest(const Request* req) { socket->writeData(request.c_str(), request.size()); } -string HttpConnection::getHost(const string& host, int port) { +string HttpConnection::getHost(const string& host, int port) const { return host+(port == 80 || port == 443 ? "" : ":"+Util::llitos(port)); } -string HttpConnection::createRequest(const Request* req, const Segment& segment) { +string HttpConnection::createRequest(const Segment& segment) const { string request = string("GET ")+ - // some servers cannot permit absolute URI as requet URI. - //req->getCurrentUrl()+ - (req->getDir() == "/" ? "/" : req->getDir()+"/")+req->getFile()+ + (req->getProtocol() == "ftp" ? + req->getCurrentUrl() : + ((req->getDir() == "/" ? "/" : req->getDir()+"/")+req->getFile()))+ string(" HTTP/1.1\r\n")+ "User-Agent: aria2\r\n"+ "Connection: close\r\n"+ - "Accept: */*\r\n"+ + "Accept: */*\r\n"+ /* */ "Host: "+getHost(req->getHost(), req->getPort())+"\r\n"+ "Pragma: no-cache\r\n"+ "Cache-Control: no-cache\r\n"; @@ -87,48 +88,31 @@ string HttpConnection::createRequest(const Request* req, const Segment& segment) } int HttpConnection::receiveResponse(HttpHeader& headers) { - string header; - char* buf = NULL; - try { - // read a line of the header - int bufSize = 256; - // TODO limit iteration count - while(1) { - bufSize += 256; - if(bufSize > 2048) { - throw new DlAbortEx(EX_INVALID_HEADER); - } - buf = new char[bufSize]; - int tbufSize = bufSize-1; - socket->peekData(buf, tbufSize); - if(tbufSize > 0) { - buf[tbufSize] = '\0'; - } - header = buf; - char* p; - if((p = strstr(buf, "\r\n")) == buf) { - throw new DlAbortEx(EX_NO_HEADER); - } - if((p = strstr(buf, "\r\n\r\n")) != NULL) { - *(p+4) = '\0'; - header = buf; - tbufSize = header.size(); - socket->readData(buf, tbufSize); - delete [] buf; - buf = NULL; - break; + char buf[512]; + while(socket->isReadable(0)) { + int size = sizeof(buf)-1; + socket->peekData(buf, size); + buf[size] = '\0'; + int hlenTemp = header.size(); + header += buf; + string::size_type p; + if((p = header.find("\r\n\r\n")) == string::npos) { + socket->readData(buf, size); + } else { + if(Util::endsWith(header, "\r\n\r\n")) { + socket->readData(buf, size); } else { - delete [] buf; - buf = NULL; + header.erase(p+4); + size = p+4-hlenTemp; + socket->readData(buf, size); } + break; } - } catch(Exception* e) { - if(buf != NULL) { - delete [] buf; - } - throw; } - // OK, i got all headers. + if(!Util::endsWith(header, "\r\n\r\n")) { + return 0; + } + // OK, we got all headers. logger->info(MSG_RECEIVE_RESPONSE, cuid, header.c_str()); string::size_type p, np; p = np = 0; @@ -145,19 +129,17 @@ int HttpConnection::receiveResponse(HttpHeader& headers) { p = np+2; pair<string, string> hp; Util::split(hp, line, ':'); - HttpHeader::value_type nh(hp.first, hp.second); - headers.insert(nh); + headers.put(hp.first, hp.second); } - // TODO rewrite this using strtoul return (int)strtol(status.c_str(), NULL, 10); } -bool HttpConnection::useProxy() { +bool HttpConnection::useProxy() const { return option->defined("http_proxy_enabled") && option->get("http_proxy_enabled") == "true"; } -bool HttpConnection::useProxyAuth() { +bool HttpConnection::useProxyAuth() const { return option->defined("http_proxy_auth_enabled") && option->get("http_proxy_auth_enabled") == "true"; } |
