HTTP: tweak response parsing.

Signal a parse failure via an exception, instead of via a status
code, since this is hard to disambiguate from a valid server-side
status code.
This commit is contained in:
James Turner 2014-01-07 16:22:10 +00:00
parent 61df58d651
commit 04246f0a63
2 changed files with 8 additions and 14 deletions

View File

@ -65,12 +65,6 @@ class Connection;
typedef std::multimap<std::string, Connection*> ConnectionDict; typedef std::multimap<std::string, Connection*> ConnectionDict;
typedef std::list<Request_ptr> RequestList; typedef std::list<Request_ptr> RequestList;
static bool isFailureStatus(int httpStatus)
{
int majorCode = httpStatus / 100;
return (majorCode != 2);
}
class Client::ClientPrivate class Client::ClientPrivate
{ {
public: public:
@ -220,12 +214,12 @@ public:
assert(state == STATE_WAITING_FOR_RESPONSE); assert(state == STATE_WAITING_FOR_RESPONSE);
activeRequest = sentRequests.front(); activeRequest = sentRequests.front();
activeRequest->responseStart(buffer); try {
if (isFailureStatus(activeRequest->responseCode())) { activeRequest->responseStart(buffer);
handleError(EIO); } catch (sg_exception& e) {
return; handleError(EIO);
} }
state = STATE_GETTING_HEADERS; state = STATE_GETTING_HEADERS;
buffer.clear(); buffer.clear();
if (activeRequest->responseCode() == 204) { if (activeRequest->responseCode() == 204) {

View File

@ -4,6 +4,7 @@
#include <simgear/debug/logstream.hxx> #include <simgear/debug/logstream.hxx>
#include <simgear/misc/strutils.hxx> #include <simgear/misc/strutils.hxx>
#include <simgear/props/props_io.hxx> #include <simgear/props/props_io.hxx>
#include <simgear/structure/exception.hxx>
namespace simgear namespace simgear
{ {
@ -115,8 +116,7 @@ void Request::responseStart(const std::string& r)
const int maxSplit = 2; // HTTP/1.1 nnn reason-string const int maxSplit = 2; // HTTP/1.1 nnn reason-string
string_list parts = strutils::split(r, NULL, maxSplit); string_list parts = strutils::split(r, NULL, maxSplit);
if (parts.size() != 3) { if (parts.size() != 3) {
setFailure(400, "malformed HTTP response header"); throw sg_io_exception("bad HTTP response");
return;
} }
_responseVersion = decodeHTTPVersion(parts[0]); _responseVersion = decodeHTTPVersion(parts[0]);