2022-03-05 17:56:57 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2022 Haiku Inc. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _B_HTTP_RESULT_H_
|
|
|
|
#define _B_HTTP_RESULT_H_
|
|
|
|
|
|
|
|
#include <memory>
|
2022-09-24 12:30:00 +03:00
|
|
|
#include <optional>
|
2022-03-05 17:56:57 +03:00
|
|
|
|
|
|
|
#include <String.h>
|
|
|
|
|
2022-04-15 08:56:18 +03:00
|
|
|
class BDataIO;
|
|
|
|
|
2022-03-05 17:56:57 +03:00
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
namespace Network {
|
|
|
|
|
2022-04-15 08:56:18 +03:00
|
|
|
class BHttpFields;
|
2022-03-05 17:56:57 +03:00
|
|
|
struct HttpResultPrivate;
|
|
|
|
|
|
|
|
|
2022-10-30 00:45:39 +03:00
|
|
|
struct BHttpBody {
|
|
|
|
std::optional<BString> text;
|
2022-04-15 08:56:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-04-23 20:30:38 +03:00
|
|
|
enum class BHttpStatusClass : int16 {
|
2022-10-30 00:45:39 +03:00
|
|
|
Invalid = 000,
|
|
|
|
Informational = 100,
|
|
|
|
Success = 200,
|
|
|
|
Redirection = 300,
|
|
|
|
ClientError = 400,
|
|
|
|
ServerError = 500
|
2022-04-23 20:30:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-10-30 00:45:39 +03:00
|
|
|
struct BHttpStatus {
|
|
|
|
int16 code = 0;
|
|
|
|
BString text;
|
2022-04-23 20:30:38 +03:00
|
|
|
|
|
|
|
// Helpers
|
2022-10-30 00:45:39 +03:00
|
|
|
BHttpStatusClass StatusClass() const noexcept;
|
|
|
|
BHttpStatusCode StatusCode() const noexcept;
|
2022-03-05 17:56:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class BHttpResult
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Constructors and destructor
|
2022-10-30 00:45:39 +03:00
|
|
|
BHttpResult(const BHttpResult& other) = delete;
|
|
|
|
BHttpResult(BHttpResult&& other) noexcept;
|
|
|
|
~BHttpResult();
|
2022-03-05 17:56:57 +03:00
|
|
|
|
|
|
|
// Assignment operators
|
2022-10-30 00:45:39 +03:00
|
|
|
BHttpResult& operator=(const BHttpResult& other) = delete;
|
|
|
|
BHttpResult& operator=(BHttpResult&& other) noexcept;
|
2022-03-05 17:56:57 +03:00
|
|
|
|
|
|
|
// Blocking Access Functions
|
2022-10-30 00:45:39 +03:00
|
|
|
const BHttpStatus& Status() const;
|
|
|
|
const BHttpFields& Fields() const;
|
|
|
|
BHttpBody& Body() const;
|
2022-03-05 17:56:57 +03:00
|
|
|
|
|
|
|
// Check if data is available yet
|
2022-10-30 00:45:39 +03:00
|
|
|
bool HasStatus() const;
|
|
|
|
bool HasFields() const;
|
|
|
|
bool HasBody() const;
|
|
|
|
bool IsCompleted() const;
|
2022-03-05 17:56:57 +03:00
|
|
|
|
|
|
|
// Identity
|
2022-10-30 00:45:39 +03:00
|
|
|
int32 Identity() const;
|
2022-03-05 17:56:57 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class BHttpSession;
|
2022-10-30 00:45:39 +03:00
|
|
|
BHttpResult(std::shared_ptr<HttpResultPrivate> data);
|
|
|
|
std::shared_ptr<HttpResultPrivate> fData;
|
2022-03-05 17:56:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Network
|
|
|
|
|
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
#endif // _B_HTTP_RESPONSE_H_
|