From b18e62dba74f75f89aab830dd961af231da06119 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 5 Feb 2014 21:20:09 +0900 Subject: [PATCH] Treat 30X response without Location header field as error This is required to make segmented download work. --- src/HttpResponse.cc | 4 ++++ src/HttpResponseCommand.cc | 9 ++++----- test/HttpResponseTest.cc | 9 ++++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index 27eb500f..dae61dc3 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -105,6 +105,10 @@ void HttpResponse::validateResponse() const case 303: // See Other case 307: // Temporary Redirect case 308: // Permanent Redirect + if (!httpHeader_->defined(HttpHeader::LOCATION)) { + throw DL_ABORT_EX2(fmt(EX_LOCATION_HEADER_REQUIRED, statusCode), + error_code::HTTP_PROTOCOL_ERROR); + } return; } if (statusCode >= 400) { diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index 622736d3..d6dbd7a9 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -231,11 +231,10 @@ bool HttpResponseCommand::executeInternal() #endif // ENABLE_MESSAGE_DIGEST } - if (statusCode == 404) { - grp->increaseAndValidateFileNotFoundCount(); - return skipResponseBody(std::move(httpResponse)); - } - if (statusCode >= 400 || statusCode == 304 || httpResponse->isRedirect()) { + if(statusCode >= 300) { + if(statusCode == 404) { + grp->increaseAndValidateFileNotFoundCount(); + } return skipResponseBody(std::move(httpResponse)); } diff --git a/test/HttpResponseTest.cc b/test/HttpResponseTest.cc index eca1df08..968a1c60 100644 --- a/test/HttpResponseTest.cc +++ b/test/HttpResponseTest.cc @@ -362,7 +362,14 @@ void HttpResponseTest::testValidateResponse() httpResponse.setHttpHeader(make_unique()); httpResponse.getHttpHeader()->setStatusCode(301); - // It is fine without Location header + try { + httpResponse.validateResponse(); + CPPUNIT_FAIL("exception must be thrown."); + } catch(Exception& e) { + // success + } + + httpResponse.getHttpHeader()->put(HttpHeader::LOCATION, "http://a/b"); httpResponse.validateResponse(); httpResponse.getHttpHeader()->setStatusCode(201);