Skip to content
Permalink
Browse files

Add function to determine if HTTP response code is OK.

OK responses are a range rather than a single value, so simplify the check with a function.
  • Loading branch information...
dwsteele committed May 24, 2019
1 parent 96770c5 commit 6c385dfa7a7f489159c06c5a31fd3cd7c77c39a8
Showing with 20 additions and 6 deletions.
  1. +17 −2 src/common/io/http/client.c
  2. +1 −1 src/common/io/http/client.h
  3. +1 −1 src/storage/s3/read.c
  4. +1 −2 src/storage/s3/storage.c
@@ -378,7 +378,7 @@ httpClientRequest(

// If all content should be returned from this function then read the buffer. Also read the reponse if there has
// been an error.
if (returnContent || httpClientResponseCode(this) != HTTP_RESPONSE_CODE_OK)
if (returnContent || !httpClientResponseCodeOk(this))
{
if (contentExists)
{
@@ -408,7 +408,7 @@ httpClientRequest(
if (this->closeOnContentEof && !contentExists)
tlsClientClose(this->tls);

// Retry when reponse code is 5xx. These errors generally represent a server error for a request that looks valid.
// Retry when response code is 5xx. These errors generally represent a server error for a request that looks valid.
// There are a few errors that might be permanently fatal but they are rare and it seems best not to try and pick
// and choose errors in this class to retry.
if (httpClientResponseCode(this) / 100 == HTTP_RESPONSE_CODE_RETRY_CLASS)
@@ -473,6 +473,21 @@ httpClientResponseCode(const HttpClient *this)
FUNCTION_TEST_RETURN(this->responseCode);
}

/***********************************************************************************************************************************
Is this response code OK, i.e. 2XX?
***********************************************************************************************************************************/
bool
httpClientResponseCodeOk(const HttpClient *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(HTTP_CLIENT, this);
FUNCTION_TEST_END();

ASSERT(this != NULL);

FUNCTION_TEST_RETURN(this->responseCode / 100 == 2);
}

/***********************************************************************************************************************************
Get the response headers
***********************************************************************************************************************************/
@@ -42,7 +42,6 @@ HTTP Constants
#define HTTP_HEADER_ETAG "etag"
STRING_DECLARE(HTTP_HEADER_ETAG_STR);

#define HTTP_RESPONSE_CODE_OK 200
#define HTTP_RESPONSE_CODE_FORBIDDEN 403
#define HTTP_RESPONSE_CODE_NOT_FOUND 404

@@ -66,6 +65,7 @@ IoRead *httpClientIoRead(const HttpClient *this);
unsigned int httpClientResponseCode(const HttpClient *this);
const HttpHeader *httpClientReponseHeader(const HttpClient *this);
const String *httpClientResponseMessage(const HttpClient *this);
bool httpClientResponseCodeOk(const HttpClient *this);

/***********************************************************************************************************************************
Destructor
@@ -58,7 +58,7 @@ storageReadS3Open(THIS_VOID)
// On success
this->httpClient = storageS3HttpClient(this->storage);

if (httpClientResponseCode(this->httpClient) == HTTP_RESPONSE_CODE_OK)
if (httpClientResponseCodeOk(this->httpClient))
result = true;

// Else error unless ignore missing
@@ -253,7 +253,6 @@ storageS3Request(
// Calculate content-md5 header if there is content
if (body != NULL)
{

char md5Hash[HASH_TYPE_MD5_SIZE_HEX];
encodeToStr(encodeBase64, bufPtr(cryptoHashOne(HASH_TYPE_MD5_STR, body)), HASH_TYPE_M5_SIZE, md5Hash);
httpHeaderAdd(requestHeader, HTTP_HEADER_CONTENT_MD5_STR, STR(md5Hash));
@@ -270,7 +269,7 @@ storageS3Request(
Buffer *response = httpClientRequest(this->httpClient, verb, uri, query, requestHeader, body, returnContent);

// Error if the request was not successful
if (httpClientResponseCode(this->httpClient) != HTTP_RESPONSE_CODE_OK &&
if (!httpClientResponseCodeOk(this->httpClient) &&
(!allowMissing || httpClientResponseCode(this->httpClient) != HTTP_RESPONSE_CODE_NOT_FOUND))
{
// General error message

0 comments on commit 6c385df

Please sign in to comment.
You can’t perform that action at this time.