diff options
Diffstat (limited to 'lib/httpserver/S3Client.cpp')
-rw-r--r-- | lib/httpserver/S3Client.cpp | 83 |
1 files changed, 66 insertions, 17 deletions
diff --git a/lib/httpserver/S3Client.cpp b/lib/httpserver/S3Client.cpp index cd5988d5..21814066 100644 --- a/lib/httpserver/S3Client.cpp +++ b/lib/httpserver/S3Client.cpp @@ -34,7 +34,7 @@ // Name: S3Client::GetObject(const std::string& rObjectURI) // Purpose: Retrieve the object with the specified URI (key) // from your S3 bucket. -// Created: 09/01/09 +// Created: 09/01/2009 // // -------------------------------------------------------------------------- @@ -46,12 +46,29 @@ HTTPResponse S3Client::GetObject(const std::string& rObjectURI) // -------------------------------------------------------------------------- // // Function +// Name: S3Client::HeadObject(const std::string& rObjectURI) +// Purpose: Retrieve the metadata for the object with the +// specified URI (key) from your S3 bucket. +// Created: 03/08/2015 +// +// -------------------------------------------------------------------------- + +HTTPResponse S3Client::HeadObject(const std::string& rObjectURI) +{ + return FinishAndSendRequest(HTTPRequest::Method_HEAD, rObjectURI); +} + + +HTTPResponse HeadObject(const std::string& rObjectURI); +// -------------------------------------------------------------------------- +// +// Function // Name: S3Client::PutObject(const std::string& rObjectURI, // IOStream& rStreamToSend, const char* pContentType) // Purpose: Upload the stream to S3, creating or overwriting the // object with the specified URI (key) in your S3 // bucket. -// Created: 09/01/09 +// Created: 09/01/2009 // // -------------------------------------------------------------------------- @@ -77,7 +94,7 @@ HTTPResponse S3Client::PutObject(const std::string& rObjectURI, // connection to the server if necessary, which may // throw a ConnectionException. Returns the HTTP // response returned by S3, which may be a 500 error. -// Created: 09/01/09 +// Created: 09/01/2009 // // -------------------------------------------------------------------------- @@ -113,7 +130,7 @@ HTTPResponse S3Client::FinishAndSendRequest(HTTPRequest::Method Method, { request.AddHeader("Content-Type", pStreamContentType); } - + std::string s3suffix = ".s3.amazonaws.com"; std::string bucket; if (mHostName.size() > s3suffix.size()) @@ -126,18 +143,18 @@ HTTPResponse S3Client::FinishAndSendRequest(HTTPRequest::Method Method, s3suffix.size()); } } - + std::ostringstream data; data << request.GetVerb() << "\n"; data << "\n"; /* Content-MD5 */ data << request.GetContentType() << "\n"; data << date.str() << "\n"; - + if (! bucket.empty()) { data << "/" << bucket; } - + data << request.GetRequestURI(); std::string data_string = data.str(); @@ -148,7 +165,7 @@ HTTPResponse S3Client::FinishAndSendRequest(HTTPRequest::Method Method, (const unsigned char*)data_string.c_str(), data_string.size(), digest_buffer, &digest_size); std::string digest((const char *)digest_buffer, digest_size); - + base64::encoder encoder; std::string auth_code = "AWS " + mAccessKey + ":" + encoder.encode(digest); @@ -200,6 +217,7 @@ HTTPResponse S3Client::FinishAndSendRequest(HTTPRequest::Method Method, } else { + BOX_TRACE("S3Client: " << mHostName << " ! " << ce.what()); throw; } } @@ -218,26 +236,57 @@ HTTPResponse S3Client::FinishAndSendRequest(HTTPRequest::Method Method, // necessary, which may throw a ConnectionException. // Returns the HTTP response returned by S3, which may // be a 500 error. -// Created: 09/01/09 +// Created: 09/01/2009 // // -------------------------------------------------------------------------- HTTPResponse S3Client::SendRequest(HTTPRequest& rRequest, IOStream* pStreamToSend, const char* pStreamContentType) -{ +{ HTTPResponse response; - + if (pStreamToSend) { - rRequest.SendWithStream(*mapClientSocket, - 30000 /* milliseconds */, + rRequest.SendWithStream(*mapClientSocket, mNetworkTimeout, pStreamToSend, response); } else { - rRequest.Send(*mapClientSocket, 30000 /* milliseconds */); - response.Receive(*mapClientSocket, 30000 /* milliseconds */); + rRequest.Send(*mapClientSocket, mNetworkTimeout); + response.Receive(*mapClientSocket, mNetworkTimeout); + } + + if(!response.IsKeepAlive()) + { + BOX_TRACE("Server will close the connection, closing our end too."); + mapClientSocket.reset(); + } + else + { + BOX_TRACE("Server will keep the connection open for more requests."); } - + return response; -} +} + +// -------------------------------------------------------------------------- +// +// Function +// Name: S3Client::CheckResponse(HTTPResponse&, +// std::string& message) +// Purpose: Check the status code of an Amazon S3 response, and +// throw an exception with a useful message (including +// the supplied message) if it's not a 200 OK response. +// Created: 26/07/2015 +// +// -------------------------------------------------------------------------- + +void S3Client::CheckResponse(const HTTPResponse& response, const std::string& message) const +{ + if(response.GetResponseCode() != HTTPResponse::Code_OK) + { + THROW_EXCEPTION_MESSAGE(HTTPException, RequestFailedUnexpectedly, + message); + } +} + |