From 04246f0a633e44a9f59bf1f4cf280a950bde4cd4 Mon Sep 17 00:00:00 2001 From: James Turner Date: Tue, 7 Jan 2014 16:22:10 +0000 Subject: [PATCH] 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. --- simgear/io/HTTPClient.cxx | 18 ++++++------------ simgear/io/HTTPRequest.cxx | 4 ++-- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/simgear/io/HTTPClient.cxx b/simgear/io/HTTPClient.cxx index ecc63f84..e345c093 100644 --- a/simgear/io/HTTPClient.cxx +++ b/simgear/io/HTTPClient.cxx @@ -65,12 +65,6 @@ class Connection; typedef std::multimap ConnectionDict; typedef std::list RequestList; -static bool isFailureStatus(int httpStatus) -{ - int majorCode = httpStatus / 100; - return (majorCode != 2); -} - class Client::ClientPrivate { public: @@ -220,12 +214,12 @@ public: assert(state == STATE_WAITING_FOR_RESPONSE); activeRequest = sentRequests.front(); - activeRequest->responseStart(buffer); - if (isFailureStatus(activeRequest->responseCode())) { - handleError(EIO); - return; - } - + try { + activeRequest->responseStart(buffer); + } catch (sg_exception& e) { + handleError(EIO); + } + state = STATE_GETTING_HEADERS; buffer.clear(); if (activeRequest->responseCode() == 204) { diff --git a/simgear/io/HTTPRequest.cxx b/simgear/io/HTTPRequest.cxx index d51d9790..46c64d5d 100644 --- a/simgear/io/HTTPRequest.cxx +++ b/simgear/io/HTTPRequest.cxx @@ -4,6 +4,7 @@ #include #include #include +#include namespace simgear { @@ -115,8 +116,7 @@ void Request::responseStart(const std::string& r) const int maxSplit = 2; // HTTP/1.1 nnn reason-string string_list parts = strutils::split(r, NULL, maxSplit); if (parts.size() != 3) { - setFailure(400, "malformed HTTP response header"); - return; + throw sg_io_exception("bad HTTP response"); } _responseVersion = decodeHTTPVersion(parts[0]);