@@ -305,8 +305,6 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource,
|
||||
Response response;
|
||||
using Error = Response::Error;
|
||||
|
||||
std::string stringResourceURL = " - Resource URL: " + resource.url;
|
||||
|
||||
if (error) {
|
||||
[MLNNativeNetworkManager.sharedManager errorLog:@"Requesting: %@ failed with error: %@", req.URL, error.debugDescription];
|
||||
|
||||
@@ -318,8 +316,7 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource,
|
||||
switch ([error code]) {
|
||||
case NSURLErrorBadServerResponse: // 5xx errors
|
||||
response.error = std::make_unique<Error>(
|
||||
Error::Reason::Server, std::string{ [[error localizedDescription] UTF8String]} + stringResourceURL);
|
||||
|
||||
Error::Reason::Server, [[error localizedDescription] UTF8String]);
|
||||
break;
|
||||
|
||||
case NSURLErrorNetworkConnectionLost:
|
||||
@@ -332,12 +329,12 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource,
|
||||
case NSURLErrorDataNotAllowed:
|
||||
case NSURLErrorTimedOut:
|
||||
response.error = std::make_unique<Error>(
|
||||
Error::Reason::Connection, [[error localizedDescription] UTF8String] + stringResourceURL);
|
||||
Error::Reason::Connection, [[error localizedDescription] UTF8String]);
|
||||
break;
|
||||
|
||||
default:
|
||||
response.error = std::make_unique<Error>(
|
||||
Error::Reason::Other, [[error localizedDescription] UTF8String] + stringResourceURL);
|
||||
Error::Reason::Other, [[error localizedDescription] UTF8String]);
|
||||
break;
|
||||
}
|
||||
} else if ([res isKindOfClass:[NSHTTPURLResponse class]]) {
|
||||
@@ -375,7 +372,7 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource,
|
||||
response.notModified = true;
|
||||
} else if (responseCode == 404) {
|
||||
response.error =
|
||||
std::make_unique<Error>(Error::Reason::NotFound, "HTTP status code 404" + stringResourceURL);
|
||||
std::make_unique<Error>(Error::Reason::NotFound, "HTTP status code 404");
|
||||
} else if (responseCode == 429) {
|
||||
// Get the standard header
|
||||
std::optional<std::string> retryAfter;
|
||||
@@ -391,22 +388,22 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource,
|
||||
xRateLimitReset = std::string([xReset UTF8String]);
|
||||
}
|
||||
|
||||
response.error = std::make_unique<Error>(Error::Reason::RateLimit, "HTTP status code 429" + stringResourceURL, http::parseRetryHeaders(retryAfter, xRateLimitReset));
|
||||
response.error = std::make_unique<Error>(Error::Reason::RateLimit, "HTTP status code 429", http::parseRetryHeaders(retryAfter, xRateLimitReset));
|
||||
} else if (responseCode >= 500 && responseCode < 600) {
|
||||
response.error =
|
||||
std::make_unique<Error>(Error::Reason::Server, std::string{ "HTTP status code " } +
|
||||
std::to_string(responseCode) + stringResourceURL);
|
||||
std::to_string(responseCode));
|
||||
} else {
|
||||
response.error =
|
||||
std::make_unique<Error>(Error::Reason::Other, std::string{ "HTTP status code " } +
|
||||
std::to_string(responseCode) + stringResourceURL);
|
||||
std::to_string(responseCode));
|
||||
}
|
||||
} else if ([url isFileURL]) {
|
||||
response.data = std::make_shared<std::string>((const char *)[data bytes], [data length]);
|
||||
} else {
|
||||
// This should never happen.
|
||||
response.error = std::make_unique<Error>(Error::Reason::Other,
|
||||
"Response class is not NSHTTPURLResponse" + stringResourceURL);
|
||||
"Response class is not NSHTTPURLResponse");
|
||||
}
|
||||
|
||||
shared->notify(response);
|
||||
@@ -433,4 +430,5 @@ void HTTPFileSource::setClientOptions(ClientOptions options) {
|
||||
ClientOptions HTTPFileSource::getClientOptions() {
|
||||
return impl->getClientOptions();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -62,13 +62,10 @@ TEST(HTTPFileSource, TEST_REQUIRES_SERVER(HTTP404)) {
|
||||
util::RunLoop loop;
|
||||
HTTPFileSource fs(ResourceOptions::Default(), ClientOptions());
|
||||
|
||||
std::string stringResourceURL = "http://127.0.0.1:3000/doesnotexist";
|
||||
std::string resourceURLInterpolation = " - Resource URL: " + stringResourceURL;
|
||||
|
||||
auto req = fs.request({Resource::Unknown, stringResourceURL}, [&](Response res) {
|
||||
auto req = fs.request({Resource::Unknown, "http://127.0.0.1:3000/doesnotexist"}, [&](Response res) {
|
||||
ASSERT_NE(nullptr, res.error);
|
||||
EXPECT_EQ(Response::Error::Reason::NotFound, res.error->reason);
|
||||
EXPECT_EQ("HTTP status code 404" + resourceURLInterpolation, res.error->message + resourceURLInterpolation);
|
||||
EXPECT_EQ("HTTP status code 404", res.error->message);
|
||||
EXPECT_FALSE(bool(res.data));
|
||||
EXPECT_FALSE(bool(res.expires));
|
||||
EXPECT_FALSE(res.mustRevalidate);
|
||||
@@ -138,13 +135,10 @@ TEST(HTTPFileSource, TEST_REQUIRES_SERVER(HTTP500)) {
|
||||
util::RunLoop loop;
|
||||
HTTPFileSource fs(ResourceOptions::Default(), ClientOptions());
|
||||
|
||||
std::string stringResourceURL = "http://127.0.0.1:3000/permanent-error";
|
||||
std::string resourceURLInterpolation = " - Resource URL: " + stringResourceURL;
|
||||
|
||||
auto req = fs.request({Resource::Unknown, stringResourceURL}, [&](Response res) {
|
||||
auto req = fs.request({Resource::Unknown, "http://127.0.0.1:3000/permanent-error"}, [&](Response res) {
|
||||
ASSERT_NE(nullptr, res.error);
|
||||
EXPECT_EQ(Response::Error::Reason::Server, res.error->reason);
|
||||
EXPECT_EQ("HTTP status code 500" + resourceURLInterpolation, res.error->message + resourceURLInterpolation);
|
||||
EXPECT_EQ("HTTP status code 500", res.error->message);
|
||||
EXPECT_FALSE(bool(res.data));
|
||||
EXPECT_FALSE(bool(res.expires));
|
||||
EXPECT_FALSE(res.mustRevalidate);
|
||||
|
||||
Reference in New Issue
Block a user