NetServices: Implement BHttpStatusCode, BHttpStatusClass and Redirects

The user of the API can set whether redirects should be followed, and if so,
how many. This is part of the BHttpRequest API. The BHttpSession then follows
those instructions, and executes the maximum number of redirects the user
would like to follow.

As part of this commit, the BHttpStatusClass and BHttpStatusCodes helper enums
have been added, to give a friendlier access to HTTP status codes and status
classes.

Change-Id: Ic8c9e3fda158e2cce549c8f1d360951f7ac83311
This commit is contained in:
Niels Sascha Reedijk
2022-04-23 18:30:38 +01:00
parent 59c359e5a9
commit 13bfff7be3
10 changed files with 459 additions and 42 deletions
+25 -15
View File
@@ -61,6 +61,7 @@ public:
// Comparison
bool operator==(const Verb& other) const noexcept;
bool operator!=(const Verb& other) const noexcept;
// Get the method as a string
const std::string_view Method() const noexcept;
@@ -70,35 +71,44 @@ private:
};
struct BHttpRedirectOptions {
bool followRedirect = true;
uint8 maxRedirections = 8;
};
class BHttpRequest {
public:
// Constructors and Destructor
BHttpRequest();
BHttpRequest(const BUrl& url);
BHttpRequest(const BHttpRequest& other) = delete;
BHttpRequest(BHttpRequest&& other) noexcept;
~BHttpRequest();
BHttpRequest();
BHttpRequest(const BUrl& url);
BHttpRequest(const BHttpRequest& other) = delete;
BHttpRequest(BHttpRequest&& other) noexcept;
~BHttpRequest();
// Assignment operators
BHttpRequest& operator=(const BHttpRequest& other) = delete;
BHttpRequest& operator=(BHttpRequest&&) noexcept;
BHttpRequest& operator=(const BHttpRequest& other) = delete;
BHttpRequest& operator=(BHttpRequest&&) noexcept;
// Access
bool IsEmpty() const noexcept;
const BHttpMethod& Method() const noexcept;
const BUrl& Url() const noexcept;
bool IsEmpty() const noexcept;
const BHttpMethod& Method() const noexcept;
const BHttpRedirectOptions& Redirect() const noexcept;
const BUrl& Url() const noexcept;
// Named Setters
void SetMethod(const BHttpMethod& method);
void SetUrl(const BUrl& url);
void SetMethod(const BHttpMethod& method);
void SetRedirect(const BHttpRedirectOptions& redirectOptions);
void SetUrl(const BUrl& url);
// Serialization
ssize_t SerializeHeaderTo(BDataIO* target) const;
BString HeaderToString() const;
ssize_t SerializeHeaderTo(BDataIO* target) const;
BString HeaderToString() const;
private:
friend class BHttpSession;
struct Data;
std::unique_ptr<Data> fData;
std::unique_ptr<Data> fData;
};
+71 -2
View File
@@ -28,10 +28,79 @@ struct BHttpBody
};
enum class BHttpStatusClass : int16 {
Invalid = 000,
Informational = 100,
Success = 200,
Redirection = 300,
ClientError = 400,
ServerError = 500
};
enum class BHttpStatusCode : int16 {
Unknown = 0,
// Informational status codes
Continue = 100,
SwitchingProtocols,
// Success status codes
Ok = 200,
Created,
Accepted,
NonAuthoritativeInformation,
NoContent,
ResetContent,
PartialContent,
// Redirection status codes
MultipleChoice = 300,
MovedPermanently,
Found,
SeeOther,
NotModified,
UseProxy,
TemporaryRedirect = 307,
PermanentRedirect,
// Client error status codes
BadRequest = 400,
Unauthorized,
PaymentRequired,
Forbidden,
NotFound,
MethodNotAllowed,
NotAcceptable,
ProxyAuthenticationRequired,
RequestTimeout,
Conflict,
Gone,
LengthRequired,
PreconditionFailed,
RequestEntityTooLarge,
RequestUriTooLarge,
UnsupportedMediaType,
RequestedRangeNotSatisfiable,
ExpectationFailed,
// Server error status codes
InternalServerError = 500,
NotImplemented,
BadGateway,
ServiceUnavailable,
GatewayTimeout,
};
struct BHttpStatus
{
int16 code = 0;
BString text;
int16 code = 0;
BString text;
// Helpers
BHttpStatusClass StatusClass() const noexcept;
BHttpStatusCode StatusCode() const noexcept;
};
@@ -39,6 +39,7 @@ public:
BMessenger observer = BMessenger());
private:
struct Redirect;
class Request;
class Impl;
std::shared_ptr<Impl> fImpl;