2022-02-20 12:40:20 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2022 Haiku Inc. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _B_HTTP_REQUEST_H_
|
|
|
|
#define _B_HTTP_REQUEST_H_
|
|
|
|
|
2022-02-25 11:41:14 +03:00
|
|
|
#include <memory>
|
2022-06-23 23:13:35 +03:00
|
|
|
#include <optional>
|
2022-02-20 12:40:20 +03:00
|
|
|
#include <string_view>
|
|
|
|
#include <variant>
|
|
|
|
|
|
|
|
#include <ErrorsExt.h>
|
|
|
|
#include <String.h>
|
|
|
|
|
2022-03-30 09:38:57 +03:00
|
|
|
class BDataIO;
|
|
|
|
class BMallocIO;
|
2022-02-25 11:41:14 +03:00
|
|
|
class BUrl;
|
|
|
|
|
2022-02-20 12:40:20 +03:00
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
namespace Network {
|
|
|
|
|
|
|
|
|
2022-05-15 18:43:07 +03:00
|
|
|
class BHttpFields;
|
2022-08-07 08:47:15 +03:00
|
|
|
class HttpBuffer;
|
|
|
|
class HttpSerializer;
|
2022-05-15 18:43:07 +03:00
|
|
|
|
|
|
|
|
2022-10-30 00:45:39 +03:00
|
|
|
class BHttpMethod
|
|
|
|
{
|
2022-02-20 12:40:20 +03:00
|
|
|
public:
|
|
|
|
// Constants for default methods in RFC 7230 section 4.2
|
2022-10-30 00:45:39 +03:00
|
|
|
enum Verb { Get, Head, Post, Put, Delete, Connect, Options, Trace };
|
2022-02-20 12:40:20 +03:00
|
|
|
|
|
|
|
// Error type when constructing with a custom method
|
2022-10-30 00:45:39 +03:00
|
|
|
class InvalidMethod;
|
2022-02-20 12:40:20 +03:00
|
|
|
|
|
|
|
// Constructors & Destructor
|
|
|
|
BHttpMethod(Verb verb) noexcept;
|
|
|
|
BHttpMethod(const std::string_view& method);
|
|
|
|
BHttpMethod(const BHttpMethod& other);
|
|
|
|
BHttpMethod(BHttpMethod&& other) noexcept;
|
|
|
|
~BHttpMethod();
|
|
|
|
|
|
|
|
// Assignment operators
|
|
|
|
BHttpMethod& operator=(const BHttpMethod& other);
|
|
|
|
BHttpMethod& operator=(BHttpMethod&& other) noexcept;
|
|
|
|
|
2022-04-18 11:28:33 +03:00
|
|
|
// Comparison
|
|
|
|
bool operator==(const Verb& other) const noexcept;
|
2022-04-23 20:30:38 +03:00
|
|
|
bool operator!=(const Verb& other) const noexcept;
|
2022-04-18 11:28:33 +03:00
|
|
|
|
2022-02-20 12:40:20 +03:00
|
|
|
// Get the method as a string
|
2022-10-30 00:45:39 +03:00
|
|
|
const std::string_view Method() const noexcept;
|
2022-02-20 12:40:20 +03:00
|
|
|
|
|
|
|
private:
|
2022-10-30 00:45:39 +03:00
|
|
|
std::variant<Verb, BString> fMethod;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class BHttpMethod::InvalidMethod : public BError
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InvalidMethod(const char* origin, BString input);
|
|
|
|
|
|
|
|
virtual const char* Message() const noexcept override;
|
|
|
|
virtual BString DebugMessage() const override;
|
|
|
|
|
|
|
|
BString input;
|
2022-02-20 12:40:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-05-29 17:13:54 +03:00
|
|
|
struct BHttpAuthentication {
|
2022-10-30 00:45:39 +03:00
|
|
|
BString username;
|
|
|
|
BString password;
|
2022-05-29 17:13:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-10-30 00:45:39 +03:00
|
|
|
class BHttpRequest
|
|
|
|
{
|
2022-02-25 11:41:14 +03:00
|
|
|
public:
|
2022-06-23 23:13:35 +03:00
|
|
|
// Aggregate parameter types
|
2022-10-30 00:45:39 +03:00
|
|
|
struct Body;
|
2022-06-23 23:13:35 +03:00
|
|
|
|
2022-02-25 11:41:14 +03:00
|
|
|
// Constructors and Destructor
|
2022-10-30 00:45:39 +03:00
|
|
|
BHttpRequest();
|
|
|
|
BHttpRequest(const BUrl& url);
|
|
|
|
BHttpRequest(const BHttpRequest& other) = delete;
|
|
|
|
BHttpRequest(BHttpRequest&& other) noexcept;
|
|
|
|
~BHttpRequest();
|
2022-02-25 11:41:14 +03:00
|
|
|
|
|
|
|
// Assignment operators
|
2022-10-30 00:45:39 +03:00
|
|
|
BHttpRequest& operator=(const BHttpRequest& other) = delete;
|
|
|
|
BHttpRequest& operator=(BHttpRequest&&) noexcept;
|
2022-02-25 11:41:14 +03:00
|
|
|
|
|
|
|
// Access
|
2022-10-30 00:45:39 +03:00
|
|
|
bool IsEmpty() const noexcept;
|
|
|
|
const BHttpAuthentication* Authentication() const noexcept;
|
|
|
|
const BHttpFields& Fields() const noexcept;
|
|
|
|
uint8 MaxRedirections() const noexcept;
|
|
|
|
const BHttpMethod& Method() const noexcept;
|
|
|
|
const Body* RequestBody() const noexcept;
|
|
|
|
bool StopOnError() const noexcept;
|
|
|
|
bigtime_t Timeout() const noexcept;
|
|
|
|
const BUrl& Url() const noexcept;
|
2022-02-25 11:41:14 +03:00
|
|
|
|
|
|
|
// Named Setters
|
2022-10-30 00:45:39 +03:00
|
|
|
void SetAuthentication(const BHttpAuthentication& authentication);
|
|
|
|
void SetFields(const BHttpFields& fields);
|
|
|
|
void SetMaxRedirections(uint8 maxRedirections);
|
|
|
|
void SetMethod(const BHttpMethod& method);
|
|
|
|
void SetRequestBody(std::unique_ptr<BDataIO> input, BString mimeType,
|
|
|
|
std::optional<off_t> size);
|
|
|
|
void SetStopOnError(bool stopOnError);
|
|
|
|
void SetTimeout(bigtime_t timeout);
|
|
|
|
void SetUrl(const BUrl& url);
|
2022-02-25 11:41:14 +03:00
|
|
|
|
2022-06-23 23:13:35 +03:00
|
|
|
// Clearing Options
|
2022-10-30 00:45:39 +03:00
|
|
|
void ClearAuthentication() noexcept;
|
2022-06-23 23:13:35 +03:00
|
|
|
std::unique_ptr<BDataIO> ClearRequestBody() noexcept;
|
|
|
|
|
2022-03-30 09:38:57 +03:00
|
|
|
// Serialization
|
2022-10-30 00:45:39 +03:00
|
|
|
BString HeaderToString() const;
|
2022-04-23 20:30:38 +03:00
|
|
|
|
2022-02-25 11:41:14 +03:00
|
|
|
private:
|
2022-03-07 11:03:10 +03:00
|
|
|
friend class BHttpSession;
|
2022-08-07 08:47:15 +03:00
|
|
|
friend class HttpSerializer;
|
2022-03-07 11:03:10 +03:00
|
|
|
struct Data;
|
2022-06-23 23:13:35 +03:00
|
|
|
|
2022-10-30 00:45:39 +03:00
|
|
|
bool RewindBody() noexcept;
|
|
|
|
void SerializeHeaderTo(HttpBuffer& buffer) const;
|
|
|
|
|
|
|
|
std::unique_ptr<Data> fData;
|
|
|
|
};
|
|
|
|
|
2022-06-23 23:13:35 +03:00
|
|
|
|
2022-10-30 00:45:39 +03:00
|
|
|
struct BHttpRequest::Body {
|
|
|
|
std::unique_ptr<BDataIO> input;
|
|
|
|
BString mimeType;
|
|
|
|
std::optional<off_t> size;
|
|
|
|
std::optional<off_t> startPosition;
|
2022-02-25 11:41:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-02-20 12:40:20 +03:00
|
|
|
} // namespace Network
|
|
|
|
|
|
|
|
} // namespace BPrivate
|
|
|
|
|
2022-02-25 11:41:14 +03:00
|
|
|
#endif // _B_HTTP_REQUEST_H_
|