summaryrefslogtreecommitdiff
path: root/src/HttpResponseCommand.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-02-21 12:27:17 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-02-21 12:27:17 +0000
commit5a8710676d58be3b3f3606993e802c6a075dbd89 (patch)
tree634e1f7be2dffb00c4248323c1465f22aedb6f60 /src/HttpResponseCommand.cc
parent6d6f25b75c1557f2b92e51fe589aadc48a1fdb68 (diff)
Diffstat (limited to 'src/HttpResponseCommand.cc')
-rw-r--r--src/HttpResponseCommand.cc50
1 files changed, 27 insertions, 23 deletions
diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc
index 22c7fa71..1fd273bb 100644
--- a/src/HttpResponseCommand.cc
+++ b/src/HttpResponseCommand.cc
@@ -28,10 +28,12 @@
HttpResponseCommand::HttpResponseCommand(int cuid, Request* req, DownloadEngine* e, Socket* s):
AbstractCommand(cuid, req, e, s) {
- AbstractCommand::checkSocketIsReadable = true;
+ http = new HttpConnection(cuid, socket, req, e->option, e->logger);
}
-HttpResponseCommand::~HttpResponseCommand() {}
+HttpResponseCommand::~HttpResponseCommand() {
+ delete http;
+}
bool HttpResponseCommand::executeInternal(Segment seg) {
if(SEGMENT_EQUAL(req->seg, seg) == false) {
@@ -39,26 +41,30 @@ bool HttpResponseCommand::executeInternal(Segment seg) {
return prepareForRetry(0);
}
HttpHeader headers;
- HttpConnection httpConnection(cuid, socket, e->option, e->logger);
- int status = httpConnection.receiveResponse(headers);
+ int status = http->receiveResponse(headers);
+ if(status == 0) {
+ // didn't receive header fully
+ e->commands.push(this);
+ return false;
+ }
// check HTTP status number
checkResponse(status, seg);
retrieveCookie(headers);
// check whether Location header exists. If it does, update request object
// with redirected URL.
// then establish a connection to the new host and port
- if(headers.count("Location")) {
- return handleRedirect((*headers.find("Location")).second, headers);
+ if(headers.defined("Location")) {
+ return handleRedirect(headers.getFirst("Location"), headers);
}
if(!e->segmentMan->downloadStarted) {
string transferEncoding;
- headers.find("Transfer-Encoding");
- if(headers.count("Transfer-Encoding")) {
- return handleOtherEncoding((*headers.find("Transfer-Encoding")).second, headers);
+ if(headers.defined("Transfer-Encoding")) {
+ return handleOtherEncoding(headers.getFirst("Transfer-Encoding"), headers);
} else {
return handleDefaultEncoding(headers);
}
} else {
+ // TODO we must check headers["size"] == e->segmentMan->totalSize here
if(req->getFile() != e->segmentMan->filename) {
throw new DlAbortEx(EX_FILENAME_MISMATCH, req->getFile().c_str(), e->segmentMan->filename.c_str());
}
@@ -83,28 +89,25 @@ bool HttpResponseCommand::handleRedirect(string url, const HttpHeader& headers)
}
bool HttpResponseCommand::handleDefaultEncoding(const HttpHeader& headers) {
- long long int size;
- if(headers.count("Content-Length") == 0) {
- size = 0;
- } else {
- size = STRTOLL((*headers.find("Content-Length")).second.c_str());
- if(size == LONG_LONG_MAX || size == LONG_LONG_MIN) {
- throw new DlAbortEx(EX_TOO_LARGE_FILE, size);
- }
+ long long int size = headers.getFirstAsLLInt("Content-Length");
+ if(size == LONG_LONG_MAX || size == LONG_LONG_MIN || size < 0) {
+ throw new DlAbortEx(EX_TOO_LARGE_FILE, size);
}
e->segmentMan->isSplittable = !(size == 0);
e->segmentMan->filename = req->getFile();
- e->segmentMan->totalSize = size;
bool segFileExists = e->segmentMan->segmentFileExists();
e->segmentMan->downloadStarted = true;
if(segFileExists) {
- e->segmentMan->load();
+
e->diskWriter->openExistingFile(e->segmentMan->getFilePath());
+ // we must check headers["size"] == e->segmentMan->totalSize here
+ if(e->segmentMan->totalSize != size) {
+ return new DlAbortEx(EX_SIZE_MISMATCH, e->segmentMan->totalSize, size);
+ }
// send request again to the server with Range header
return prepareForRetry(0);
} else {
- Segment seg;
- e->segmentMan->getSegment(seg, cuid);
+ e->segmentMan->totalSize = size;
e->diskWriter->initAndOpenFile(e->segmentMan->getFilePath());
createHttpDownloadCommand();
return true;
@@ -140,9 +143,10 @@ void HttpResponseCommand::createHttpDownloadCommand(string transferEncoding) {
}
void HttpResponseCommand::retrieveCookie(const HttpHeader& headers) {
- for(HttpHeader::const_iterator itr = headers.find("Set-Cookie"); itr != headers.end(); itr++) {
+ vector<string> v = headers.get("Set-Cookie");
+ for(vector<string>::const_iterator itr = v.begin(); itr != v.end(); itr++) {
Cookie c;
- req->cookieBox->parse(c, (*itr).second);
+ req->cookieBox->parse(c, *itr);
req->cookieBox->add(c);
}
}