Optimized Request and Response classes.

This commit is contained in:
Karutoh 2025-06-01 22:16:32 -07:00
parent 135f855309
commit 73f7ec1a8d
20 changed files with 877 additions and 231 deletions

View File

@ -201,6 +201,8 @@ set(EHS_SOURCES
src/io/socket/ehc/NetUtils.cpp src/io/socket/ehc/NetUtils.cpp
include/ehs/io/socket/BaseICMP.h src/io/socket/BaseICMP.cpp include/ehs/io/socket/BaseICMP.h src/io/socket/BaseICMP.cpp
include/ehs/io/socket/ICMP.h include/ehs/io/socket/ICMP.h
src/io/socket/QueryVar.cpp include/ehs/io/socket/QueryVar.h
src/io/socket/HeaderVar.cpp include/ehs/io/socket/HeaderVar.h
) )
if (IS_OS_WINDOWS) if (IS_OS_WINDOWS)

View File

@ -24,9 +24,9 @@ namespace ehs
bool connected; bool connected;
public: public:
static const UInt_16 HTTPS_Port = 443; static constexpr UInt_16 HTTPS_Port = 443;
static const UInt_16 HTTP_Port = 80; static constexpr UInt_16 HTTP_Port = 80;
static const UInt_16 MaxHeaderSize = 8192; static constexpr UInt_16 MaxHeaderSize = 8192;
virtual ~BaseTCP() = default; virtual ~BaseTCP() = default;
@ -55,7 +55,7 @@ namespace ehs
/// @param [in] address The ip address to bind to. /// @param [in] address The ip address to bind to.
/// @param [in] port The port to bind to. /// @param [in] port The port to bind to.
/// @note Used for servers. /// @note Used for servers.
virtual void Bind(const Str_8& address, UInt_16 port) = 0; virtual void Bind(Str_8 address, const UInt_16 &port) = 0;
/// Listens for new incoming connections. /// Listens for new incoming connections.
/// @note Used for servers. /// @note Used for servers.
@ -63,13 +63,13 @@ namespace ehs
/// Accepts the new incoming connection. /// Accepts the new incoming connection.
/// @note Used for servers. /// @note Used for servers.
virtual BaseTCP* Accept() = 0; virtual BaseTCP *Accept() = 0;
/// Connects to a server at the specified address and port. /// Connects to a server at the specified address and port.
/// @param [in] address The ip address to connect to. /// @param [in] address The ip address to connect to.
/// @param [in] port The port to connect to. /// @param [in] port The port to connect to.
/// @note Used for clients. /// @note Used for clients.
virtual void Connect(const Str_8& address, UInt_16 port) = 0; virtual void Connect(Str_8 address, const UInt_16 &port) = 0;
/// Sends data to the connected endpoint. /// Sends data to the connected endpoint.
/// @param [in] buffer The data to send to the endpoint. /// @param [in] buffer The data to send to the endpoint.

View File

@ -0,0 +1,45 @@
#pragma once
#include "ehs/Str.h"
namespace ehs
{
class HeaderVar
{
private:
UInt_64 id;
Str_8 name;
Str_8 value;
public:
HeaderVar();
HeaderVar(Str_8 name, Str_8 value);
HeaderVar(HeaderVar &&other) noexcept;
HeaderVar(const HeaderVar &other);
HeaderVar &operator=(HeaderVar &&other) noexcept;
HeaderVar &operator=(const HeaderVar &other);
bool operator==(const HeaderVar &other) const;
bool operator!=(const HeaderVar &other) const;
bool operator==(const UInt_64 &otherId) const;
bool operator!=(const UInt_64 &otherId) const;
UInt_64 GetId() const;
Str_8 GetName() const;
Str_8 GetValue() const;
void SetValue(Str_8 value);
Str_8 ToStr() const;
};
}

View File

@ -0,0 +1,45 @@
#pragma once
#include "ehs/Str.h"
namespace ehs
{
class QueryVar
{
private:
UInt_64 id;
Str_8 name;
Str_8 value;
public:
QueryVar();
QueryVar(Str_8 name, Str_8 value);
QueryVar(QueryVar &&other) noexcept;
QueryVar(const QueryVar &other);
QueryVar &operator=(QueryVar &&other) noexcept;
QueryVar &operator=(const QueryVar &other);
bool operator==(const QueryVar &other) const;
bool operator!=(const QueryVar &other) const;
bool operator==(const UInt_64 &otherId) const;
bool operator!=(const UInt_64 &otherId) const;
UInt_64 GetId() const;
Str_8 GetName() const;
Str_8 GetValue() const;
void SetValue(Str_8 value);
Str_8 ToStr() const;
};
}

View File

@ -5,6 +5,8 @@
#include "ehs/Str.h" #include "ehs/Str.h"
#include "ehs/json/Json.h" #include "ehs/json/Json.h"
#include "Socket.h" #include "Socket.h"
#include "QueryVar.h"
#include "HeaderVar.h"
namespace ehs namespace ehs
{ {
@ -21,8 +23,8 @@ namespace ehs
private: private:
Verb verb; Verb verb;
Str_8 rsrc; Str_8 rsrc;
Vector<Str_8> queries; Vector<QueryVar> queries;
Vector<Str_8> header; Vector<HeaderVar> header;
ContentType cType; ContentType cType;
Str_8 body; Str_8 body;
@ -33,25 +35,34 @@ namespace ehs
/// Initializes this request with a given verb and URI resource. /// Initializes this request with a given verb and URI resource.
/// @param [in] verb The type of request to make. /// @param [in] verb The type of request to make.
/// @param [in] rsrc The URI endpoint to make the request at. /// @param [in] rsrc The URI endpoint to make the request at.
Request(const Verb verb, const Str_8& rsrc); Request(const Verb &verb, Str_8 rsrc);
/// Initializes this request with the raw request data. /// Initializes this request with the raw request data.
/// @param [in] data The C-style string of the request. /// @param [in] data The C-style string of the request.
/// @param [in] size The size of the given C-style string. /// @param [in] size The size of the given C-style string.
Request(const char* data, const UInt_64 size); Request(const char *data, const UInt_64 &size);
/// Initializes this request with the raw request data. /// Initializes this request with the raw request data.
/// @param [in] data The string of the request. /// @param [in] data The string of the request.
Request(const Str_8& data); Request(const Str_8 &data);
/// Moves members from another object of the same type.
/// @param [in] other The object to move from.
Request(Request &&other) noexcept;
/// Copies members from another object of the same type. /// Copies members from another object of the same type.
/// @param [in] req The object to copy from. /// @param [in] other The object to copy from.
Request(const Request& req) = default; Request(const Request &other);
/// Moves members from another object of the same type.
/// @param [in] other The object to move from.
/// @returns The request that has been assigned to.
Request& operator=(Request &&other) noexcept;
/// Copies members from another object of the same type. /// Copies members from another object of the same type.
/// @param [in] req The object to copy from. /// @param [in] other The object to copy from.
/// @returns The request that has been assigned to. /// @returns The request that has been assigned to.
Request& operator=(const Request& req); Request& operator=(const Request &other);
/// Retrieves the verb for the request. /// Retrieves the verb for the request.
/// @returns The result. /// @returns The result.
@ -59,7 +70,7 @@ namespace ehs
/// Sets the content type for the body. /// Sets the content type for the body.
/// @param [in] cType The content type to use. /// @param [in] cType The content type to use.
void SetContentType(const ContentType cType); void SetContentType(const ContentType &cType);
/// Retrieves the content type for the body. /// Retrieves the content type for the body.
/// @returns The result. /// @returns The result.
@ -67,75 +78,145 @@ namespace ehs
/// Sets the URI resource. /// Sets the URI resource.
/// @param [in] rsrc The resource. /// @param [in] rsrc The resource.
void SetResource(const Str_8& rsrc); void SetResource(Str_8 rsrc);
/// Retrieves the URI resource. /// Retrieves the URI resource.
/// @returns The result. /// @returns The result.
Str_8 GetResource() const; Str_8 GetResource() const;
/// Adds a query variable to the URI. /// Checks whether a header variable exists using the id.
/// @param [in] var The variable identifier. /// @param [in] id The variable id to look for.
/// @param [in] value The value of the variable. /// @returns True if the variable exists. False otherwise.
void AddQuery(const Str_8& var, const Str_8& value); bool HasQueryVar(const UInt_64 &id) const;
/// Retrieves a query variable from the URI. /// Checks whether a header variable exists using the name.
/// @param [in] var The variable identifier to look for. /// @param [in] name The variable name to look for.
/// @returns The value of the query variable. Empty if it was not found. /// @returns True if the variable exists. False otherwise.
Str_8 GetQuery(const Str_8& var); bool HasQueryVar(const Str_8 &name) const;
/// Adds a header variable.
/// @param [in] var The variable to add to the header.
/// @returns False if the given variable name already exists. True otherwise.
bool AddQueryVar(QueryVar var);
/// Removes a header variable using the id.
/// @param [in] id The variable id to look for.
/// @returns True if the variable was found. False otherwise.
bool RemoveQueryVar(const UInt_64 &id);
/// Removes a header variable using the name.
/// @param [in] name The variable name to look for.
/// @returns True if the variable was found. False otherwise.
bool RemoveQueryVar(const Str_8 &name);
/// Retrieves a header variable using the id.
/// @param [in] id The variable id to look for.
/// @returns The variable object if found. Nullptr otherwise.
QueryVar *GetQueryVar(const UInt_64 &id) const;
/// Retrieves a header variable using the name.
/// @param [in] name The variable name to look for.
/// @returns The variable object if found. Nullptr otherwise.
QueryVar *GetQueryVar(const Str_8& name) const;
/// Retrieves a header variable value using the id.
/// @param [in] id The variable id to look for.
/// @returns The variable's value if found. Empty otherwise.
Str_8 GetQueryValue(const UInt_64 &id) const;
/// Retrieves a header variable value using the name.
/// @param [in] name The variable name to look for.
/// @returns The variable's value if found. Empty otherwise.
Str_8 GetQueryValue(const Str_8& name) const;
/// Retrieves all the query variables from the URI in a vector object. /// Retrieves all the query variables from the URI in a vector object.
/// @returns The result. /// @returns The result.
Vector<Str_8> GetQueries() const; Vector<QueryVar> GetQueries() const;
/// A helper method to automatically add the required header variables for basic authentication. /// A helper method to automatically add the required header variables for basic authentication.
/// @param [in] id The username or id. /// @param [in] id The username or id.
/// @param [in] secret The secret given by an API. /// @param [in] secret The secret given by an API.
void BasicAuth(const Str_8& id, const Str_8& secret); void BasicAuth(const Str_8 &id, const Str_8 &secret);
/// A helper method to automatically add the required header variables for bearer authentication. /// A helper method to automatically add the required header variables for bearer authentication.
/// @param [in] token The token given by an API. /// @param [in] token The token given by an API.
void BearerAuth(const Str_8& token); void BearerAuth(const Str_8 &token);
/// A helper method to automatically add the required header variables for bearer authentication. /// A helper method to automatically add the required header variables for bearer authentication.
/// @param [in] token The token given by an API. /// @param [in] token The token given by an API.
/// @param [in] clientId The client id given by an API. /// @param [in] clientId The client id given by an API.
void BearerAuth(const Str_8& token, const Str_8& clientId); void BearerAuth(const Str_8 &token, const Str_8 &clientId);
/// A helper method to automatically add the required header variables for bot authentication. /// A helper method to automatically add the required header variables for bot authentication.
/// @param [in] token The token given by an API. /// @param [in] token The token given by an API.
void BotAuth(const Str_8& token); void BotAuth(const Str_8 &token);
/// Checks whether a header variable exists using the id.
/// @param [in] id The variable id to look for.
/// @returns True if the variable exists. False otherwise.
bool HasHeaderVar(const UInt_64 &id) const;
/// Checks whether a header variable exists using the name.
/// @param [in] name The variable name to look for.
/// @returns True if the variable exists. False otherwise.
bool HasHeaderVar(const Str_8 &name) const;
/// Adds a header variable. /// Adds a header variable.
/// @param [in] var The variable identifier. /// @param [in] var The variable to add to the header.
/// @param [in] value The value of the variable. /// @returns False if the given variable name already exists. True otherwise.
void AddToHeader(const Str_8& var, const Str_8& value); bool AddHeaderVar(HeaderVar var);
/// Retrieves a header variable. /// Removes a header variable using the id.
/// @param [in] var The variable identifier to look for. /// @param [in] id The variable id to look for.
/// @returns The value of the header variable. Empty if it was not found. /// @returns True if the variable was found. False otherwise.
Str_8 GetHeader(const Str_8& var) const; bool RemoveHeaderVar(const UInt_64 &id);
/// Removes a header variable using the name.
/// @param [in] name The variable name to look for.
/// @returns True if the variable was found. False otherwise.
bool RemoveHeaderVar(const Str_8 &name);
/// Retrieves a header variable using the id.
/// @param [in] id The variable id to look for.
/// @returns The variable object if found. Nullptr otherwise.
HeaderVar *GetHeaderVar(const UInt_64 &id) const;
/// Retrieves a header variable using the name.
/// @param [in] name The variable name to look for.
/// @returns The variable object if found. Nullptr otherwise.
HeaderVar *GetHeaderVar(const Str_8& name) const;
/// Retrieves a header variable value using the id.
/// @param [in] id The variable id to look for.
/// @returns The variable's value if found. Empty otherwise.
Str_8 GetHeaderValue(const UInt_64 &id) const;
/// Retrieves a header variable value using the name.
/// @param [in] name The variable name to look for.
/// @returns The variable's value if found. Empty otherwise.
Str_8 GetHeaderValue(const Str_8& name) const;
/// Retrieves all the header variables in a vector object. /// Retrieves all the header variables in a vector object.
/// @returns The result. /// @returns The result.
Vector<Str_8> GetHeader() const; Vector<HeaderVar> GetHeader() const;
/// Adds a body variable. /// Adds a body variable.
/// @param [in] var The variable identifier. /// @param [in] var The variable identifier.
/// @param [in] value The value of the variable. /// @param [in] value The value of the variable.
void AddToBody(const Str_8& var, const Str_8& value); void AddToBody(const Str_8 &var, const Str_8 &value);
/// Adds a value to the body. /// Adds a value to the body.
/// @param [in] data The value to add. /// @param [in] data The value to add.
void AddToBody(const Str_8& data); void AddToBody(const Str_8 &data);
/// Sets the entire body. /// Sets the entire body.
/// @param [in] body The body to use. /// @param [in] body The body to use.
void SetBody(const Str_8& body); void SetBody(const Str_8 &body);
/// Retrieves a body variable. /// Retrieves a body variable.
/// @param [in] var The variable identifier to look for. /// @param [in] var The variable identifier to look for.
/// @returns The value of the body variable. Empty if it was not found. /// @returns The value of the body variable. Empty if it was not found.
Str_8 GetVar(const Str_8& var) const; Str_8 GetVar(const Str_8 &var) const;
/// Retrieves the entire body. /// Retrieves the entire body.
/// @returns The result. /// @returns The result.
@ -152,13 +233,13 @@ namespace ehs
bool IsValid() const; bool IsValid() const;
private: private:
static Str_8 VerbToStr(const Verb verb); static Str_8 VerbToStr(const Verb &verb);
static Str_8 ContentTypeToStr(const ContentType cType); static Str_8 ContentTypeToStr(const ContentType &cType);
static ContentType StrToContentType(const Str_8& value); static ContentType StrToContentType(const Str_8 &value);
void ReadData(const Str_8& data); void ReadData(const Str_8 &data);
}; };
} }

View File

@ -5,6 +5,7 @@
#include "ehs/Str.h" #include "ehs/Str.h"
#include "ehs/json/Json.h" #include "ehs/json/Json.h"
#include "Socket.h" #include "Socket.h"
#include "HeaderVar.h"
namespace ehs namespace ehs
{ {
@ -14,7 +15,7 @@ namespace ehs
UInt_32 code; UInt_32 code;
Str_8 server; Str_8 server;
ContentType cType; ContentType cType;
Vector<Str_8> header; Vector<HeaderVar> header;
Str_8 body; Str_8 body;
public: public:
@ -24,29 +25,38 @@ namespace ehs
/// Initializes this response with a given code and server identifier. /// Initializes this response with a given code and server identifier.
/// @param [in] code The code to give. /// @param [in] code The code to give.
/// @param [in] server The server identifier. /// @param [in] server The server identifier.
Response(const UInt_32 code, const Str_8& server); Response(const UInt_32 &code, Str_8 server);
/// Initializes this response with the raw response data. /// Initializes this response with the raw response data.
/// @param [in] data The C-style string of the response. /// @param [in] data The C-style string of the response.
/// @param [in] size The size of the given C-style string. /// @param [in] size The size of the given C-style string.
Response(const char* data, const UInt_64 size); Response(const char *data, const UInt_64 &size);
/// Initializes this response with the raw response data. /// Initializes this response with the raw response data.
/// @param [in] data The string of the response. /// @param [in] data The string of the response.
Response(const Str_8& data); Response(const Str_8 &data);
/// Moves members from another object of the same type.
/// @param [in] other The object to move from.
Response(Response &&other) noexcept;
/// Copies members from another object of the same type. /// Copies members from another object of the same type.
/// @param [in] res The object to copy from. /// @param [in] other The object to copy from.
Response(const Response& res) = default; Response(const Response &other);
/// Moves members from another object of the same type.
/// @param [in] other The object to move from.
/// @returns The response that has been assigned to.
Response &operator=(Response &&other) noexcept;
/// Copies members from another object of the same type. /// Copies members from another object of the same type.
/// @param [in] res The object to copy from. /// @param [in] res The object to copy from.
/// @returns The response that has been assigned to. /// @returns The response that has been assigned to.
Response& operator=(const Response& res); Response& operator=(const Response &other);
/// Sets the response code to send to the endpoint. /// Sets the response code to send to the endpoint.
/// @param [in] code The code for success, error or info. /// @param [in] code The code for success, error or info.
void SetCode(const UInt_32 code); void SetCode(const UInt_32 &code);
/// Retrieves the response code. /// Retrieves the response code.
/// @returns The result. /// @returns The result.
@ -54,7 +64,7 @@ namespace ehs
/// Sets the server identifier. /// Sets the server identifier.
/// @param [in] server The server identifier to use. /// @param [in] server The server identifier to use.
void SetServer(const Str_8& server); void SetServer(Str_8 server);
/// Retrieves the server identifier. /// Retrieves the server identifier.
/// @returns The result. /// @returns The result.
@ -62,25 +72,60 @@ namespace ehs
/// Sets the content type for the body. /// Sets the content type for the body.
/// @param [in] cType The content type to use. /// @param [in] cType The content type to use.
void SetContentType(const ContentType cType); void SetContentType(const ContentType &cType);
/// Retrieves the content type for the body. /// Retrieves the content type for the body.
/// @returns The result. /// @returns The result.
ContentType GetContentType() const; ContentType GetContentType() const;
/// Adds a header variable. /// Checks whether a header variable exists using the id.
/// @param [in] var The variable identifier. /// @param [in] id The variable id to look for.
/// @param [in] value The value of the variable. /// @returns True if the variable exists. False otherwise.
void AddToHeader(const Str_8& var, const Str_8& value); bool HasHeaderVar(const UInt_64 &id) const;
/// Retrieves a header variable. /// Checks whether a header variable exists using the name.
/// @param [in] var The variable identifier to look for. /// @param [in] name The variable name to look for.
/// @returns The value of the header variable. Empty if it was not found. /// @returns True if the variable exists. False otherwise.
Str_8 GetHeader(const Str_8& var) const; bool HasHeaderVar(const Str_8 &name) const;
/// Adds a header variable.
/// @param [in] var The variable to add to the header.
/// @returns False if the given variable name already exists. True otherwise.
bool AddHeaderVar(HeaderVar var);
/// Removes a header variable using the id.
/// @param [in] id The variable id to look for.
/// @returns True if the variable was found. False otherwise.
bool RemoveHeaderVar(const UInt_64 &id);
/// Removes a header variable using the name.
/// @param [in] name The variable name to look for.
/// @returns True if the variable was found. False otherwise.
bool RemoveHeaderVar(const Str_8 &name);
/// Retrieves a header variable using the id.
/// @param [in] id The variable id to look for.
/// @returns The variable object if found. Nullptr otherwise.
HeaderVar *GetHeaderVar(const UInt_64 &id) const;
/// Retrieves a header variable using the name.
/// @param [in] name The variable name to look for.
/// @returns The variable object if found. Nullptr otherwise.
HeaderVar *GetHeaderVar(const Str_8& name) const;
/// Retrieves a header variable value using the id.
/// @param [in] id The variable id to look for.
/// @returns The variable's value if found. Empty otherwise.
Str_8 GetHeaderValue(const UInt_64 &id) const;
/// Retrieves a header variable value using the name.
/// @param [in] name The variable name to look for.
/// @returns The variable's value if found. Empty otherwise.
Str_8 GetHeaderValue(const Str_8& name) const;
/// Retrieves all the header variables in a vector object. /// Retrieves all the header variables in a vector object.
/// @returns The result. /// @returns The result.
Vector<Str_8> GetHeader() const; Vector<HeaderVar> GetHeader() const;
/// Adds a body variable. /// Adds a body variable.
/// @param [in] var The variable identifier. /// @param [in] var The variable identifier.
@ -115,13 +160,13 @@ namespace ehs
bool IsValid() const; bool IsValid() const;
private: private:
static Str_8 CodeToStr(const UInt_32 code); static Str_8 CodeToStr(const UInt_32 &code);
static Str_8 ContentTypeToStr(const ContentType cType); static Str_8 ContentTypeToStr(const ContentType &cType);
static ContentType StrToContentType(const Str_8& value); static ContentType StrToContentType(const Str_8 &value);
void ReadData(const Str_8& data); void ReadData(const Str_8 &data);
}; };
} }

View File

@ -42,17 +42,15 @@ namespace ehs
void Release() override; void Release() override;
void Bind(const Str_8& address, unsigned short port) override;
void Listen() override; void Listen() override;
SSL* Accept() override; SSL* Accept() override;
void Connect(const Str_8& address, const UInt_16 port) override; void Connect(Str_8 address, const UInt_16 &port) override;
UInt_64 Send(const Byte* const buffer, const UInt_32 size) override; UInt_64 Send(const Byte* buffer, const UInt_32 size) override;
UInt_64 Receive(Byte* const buffer, const UInt_32 size) override; UInt_64 Receive(Byte* buffer, const UInt_32 size) override;
void UseCertificate(const Char_8* data, const UInt_32 &size); void UseCertificate(const Char_8* data, const UInt_32 &size);

View File

@ -40,6 +40,7 @@ namespace ehs
APP_XML, APP_XML,
TEXT_PLAIN, TEXT_PLAIN,
TEXT_HTML, TEXT_HTML,
TEXT_CSS,
TEXT_XML, TEXT_XML,
IMG_X_ICON, IMG_X_ICON,
NONE NONE

View File

@ -46,7 +46,7 @@ namespace ehs
/// @param [in] address The local IPv4 or IPv6 address to bind to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device. /// @param [in] address The local IPv4 or IPv6 address to bind to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param [in] port The port to bind to. /// @param [in] port The port to bind to.
/// @note Requires the port given to be forwarded if this is called. /// @note Requires the port given to be forwarded if this is called.
void Bind(const Str_8& address, UInt_16 port) override; void Bind(Str_8 address, const UInt_16 &port) override;
/// Listens for incoming connections. Used for servers or PtP. /// Listens for incoming connections. Used for servers or PtP.
void Listen() override; void Listen() override;
@ -58,7 +58,7 @@ namespace ehs
/// Connects to a TCP Socket that listens for incoming connections. Used for clients or PtP. /// Connects to a TCP Socket that listens for incoming connections. Used for clients or PtP.
/// @param address The address of the listening TCP socket. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device. /// @param address The address of the listening TCP socket. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param port The port of the listening TCP socket. /// @param port The port of the listening TCP socket.
void Connect(const Str_8& address, UInt_16 port) override; void Connect(Str_8 address, const UInt_16 &port) override;
/// Sends data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking. /// Sends data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking.
/// @param [in] buffer The C-style array to send. /// @param [in] buffer The C-style array to send.
@ -89,12 +89,12 @@ namespace ehs
bool IsValid() const override; bool IsValid() const override;
private: private:
void Bind_v6(const Str_8& address, UInt_16 port); void Bind_v6(const Str_8 &address, const UInt_16 &port) const;
void Bind_v4(const Str_8& address, UInt_16 port); void Bind_v4(const Str_8 &address, const UInt_16 &port) const;
void Connect_v6(const Str_8& address, UInt_16 port); void Connect_v6(const Str_8 &address, const UInt_16 &port);
void Connect_v4(const Str_8& address, UInt_16 port); void Connect_v4(const Str_8 &address, const UInt_16 &port);
}; };
} }

View File

@ -46,7 +46,7 @@ namespace ehs
/// @param [in] address The local IPv4 or IPv6 address to bind to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device. /// @param [in] address The local IPv4 or IPv6 address to bind to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param [in] port The port to bind to. /// @param [in] port The port to bind to.
/// @note Requires the port given to be forwarded if this is called. /// @note Requires the port given to be forwarded if this is called.
void Bind(const Str_8& address, UInt_16 port) override; void Bind(Str_8 address, const UInt_16 &port) override;
/// Listens for incoming connections. Used for servers or PtP. /// Listens for incoming connections. Used for servers or PtP.
void Listen() override; void Listen() override;
@ -58,7 +58,7 @@ namespace ehs
/// Connects to a TCP Socket that listens for incoming connections. Used for clients or PtP. /// Connects to a TCP Socket that listens for incoming connections. Used for clients or PtP.
/// @param address The address of the listening TCP socket. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device. /// @param address The address of the listening TCP socket. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param port The port of the listening TCP socket. /// @param port The port of the listening TCP socket.
void Connect(const Str_8& address, UInt_16 port) override; void Connect(Str_8 address, const UInt_16 &port) override;
/// Sends data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking. /// Sends data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking.
/// @param [in] buffer The C-style array to send. /// @param [in] buffer The C-style array to send.
@ -89,9 +89,9 @@ namespace ehs
bool IsValid() const override; bool IsValid() const override;
private: private:
void Bind_v6(const Str_8& address, UInt_16 port); void Bind_v6(const Str_8& address, const UInt_16 &port);
void Bind_v4(const Str_8& address, UInt_16 port); void Bind_v4(const Str_8& address, const UInt_16 &port);
void Connect_v6(const Str_8& address, UInt_16 port); void Connect_v6(const Str_8& address, UInt_16 port);

View File

@ -108,7 +108,7 @@ namespace ehs
if (!IsValid()) if (!IsValid())
return; return;
req.AddToHeader("Host", remoteHostName); req.AddHeaderVar({"Host", remoteHostName});
SendStr(req.FormResult()); SendStr(req.FormResult());
} }
@ -124,10 +124,10 @@ namespace ehs
Response response(header); Response response(header);
Str_8 encoding = response.GetHeader("Transfer-Encoding"); Str_8 encoding = response.GetHeaderValue("Transfer-Encoding");
if (!encoding.Size()) if (!encoding.Size())
{ {
int bodySize = response.GetHeader("content-length").ToDecimal<int>(); int bodySize = response.GetHeaderValue("content-length").ToDecimal<int>();
if (!bodySize) if (!bodySize)
return response; return response;
@ -163,10 +163,10 @@ namespace ehs
if (request.GetVerb() == Verb::GET) if (request.GetVerb() == Verb::GET)
return request; return request;
Str_8 encoding = request.GetHeader("Transfer-Encoding"); Str_8 encoding = request.GetHeaderValue("Transfer-Encoding");
if (!encoding.Size()) if (!encoding.Size())
{ {
int bodySize = request.GetHeader("Content-Length").ToDecimal<int>(); int bodySize = request.GetHeaderValue("Content-Length").ToDecimal<int>();
if (!bodySize) if (!bodySize)
return request; return request;

View File

@ -0,0 +1,96 @@
#include "ehs/io/socket/HeaderVar.h"
namespace ehs
{
HeaderVar::HeaderVar()
: id(0)
{
}
HeaderVar::HeaderVar(Str_8 name, Str_8 value)
: id(name.Hash_64()), name((Str_8 &&)name), value((Str_8 &&)value)
{
}
HeaderVar::HeaderVar(HeaderVar&& other) noexcept
: id(other.id), name((Str_8 &&)other.name), value((Str_8 &&)other.value)
{
other.id = 0;
}
HeaderVar::HeaderVar(const HeaderVar& other)
: id(other.id), name(other.name), value(other.value)
{
}
HeaderVar& HeaderVar::operator=(HeaderVar&& other) noexcept
{
if (this == &other)
return *this;
id = other.id;
name = (Str_8 &&)other.name;
value = (Str_8 &&)other.value;
other.id = 0;
return *this;
}
HeaderVar& HeaderVar::operator=(const HeaderVar& other)
{
if (this == &other)
return *this;
id = other.id;
name = other.name;
value = other.value;
return *this;
}
bool HeaderVar::operator==(const HeaderVar& other) const
{
return id == other.id;
}
bool HeaderVar::operator!=(const HeaderVar& other) const
{
return id != other.id;
}
bool HeaderVar::operator==(const UInt_64& otherId) const
{
return id == otherId;
}
bool HeaderVar::operator!=(const UInt_64& otherId) const
{
return id != otherId;
}
UInt_64 HeaderVar::GetId() const
{
return id;
}
Str_8 HeaderVar::GetName() const
{
return name;
}
Str_8 HeaderVar::GetValue() const
{
return value;
}
void HeaderVar::SetValue(Str_8 value)
{
this->value = (Str_8 &&)value;
}
Str_8 HeaderVar::ToStr() const
{
return name + ": " + value;
}
}

View File

@ -0,0 +1,96 @@
#include "ehs/io/socket/QueryVar.h"
namespace ehs
{
QueryVar::QueryVar()
: id(0)
{
}
QueryVar::QueryVar(Str_8 name, Str_8 value)
: id(name.Hash_64()), name((Str_8 &&)name), value((Str_8 &&)value)
{
}
QueryVar::QueryVar(QueryVar&& other) noexcept
: id(other.id), name((Str_8 &&)other.name), value((Str_8 &&)other.value)
{
other.id = 0;
}
QueryVar::QueryVar(const QueryVar& other)
: id(other.id), name(other.name), value(other.value)
{
}
QueryVar& QueryVar::operator=(QueryVar&& other) noexcept
{
if (this == &other)
return *this;
id = other.id;
name = (Str_8 &&)other.name;
value = (Str_8 &&)other.value;
other.id = 0;
return *this;
}
QueryVar& QueryVar::operator=(const QueryVar& other)
{
if (this == &other)
return *this;
id = other.id;
name = other.name;
value = other.value;
return *this;
}
bool QueryVar::operator==(const QueryVar& other) const
{
return id == other.id;
}
bool QueryVar::operator!=(const QueryVar& other) const
{
return id != other.id;
}
bool QueryVar::operator==(const UInt_64& otherId) const
{
return id == otherId;
}
bool QueryVar::operator!=(const UInt_64& otherId) const
{
return id != otherId;
}
UInt_64 QueryVar::GetId() const
{
return id;
}
Str_8 QueryVar::GetName() const
{
return name;
}
Str_8 QueryVar::GetValue() const
{
return value;
}
void QueryVar::SetValue(Str_8 value)
{
this->value = (Str_8 &&)value;
}
Str_8 QueryVar::ToStr() const
{
return name + "=" + value;
}
}

View File

@ -4,38 +4,70 @@
namespace ehs namespace ehs
{ {
Request::Request() Request::Request()
: verb(Verb::GET), cType(ContentType::NONE) : verb(Verb::POST), cType(ContentType::NONE)
{ {
} }
Request::Request(const Verb verb, const Str_8& rsrc) Request::Request(const Verb &verb, Str_8 rsrc)
: verb(verb), rsrc(rsrc), cType(ContentType::NONE) : verb(verb), rsrc((Str_8 &&)rsrc), cType(ContentType::NONE)
{ {
} }
Request::Request(const char* data, const UInt_64 size) Request::Request(const char *data, const UInt_64 &size)
: verb(Verb::POST), cType(ContentType::NONE) : verb(Verb::POST), cType(ContentType::NONE)
{ {
ReadData(Str_8(data, size)); ReadData(Str_8(data, size));
} }
Request::Request(const Str_8& data) Request::Request(const Str_8 &data)
: verb(Verb::POST), cType(ContentType::NONE) : verb(Verb::POST), cType(ContentType::NONE)
{ {
ReadData(data); ReadData(data);
} }
Request& Request::operator=(const Request &req) Request::Request(Request&& other) noexcept
: verb(other.verb), rsrc((Str_8 &&)other.rsrc), queries((Vector<QueryVar> &&)other.queries),
header((Vector<HeaderVar> &&)other.header), cType(other.cType), body((Str_8 &&)other.body)
{ {
if (this == &req) other.verb = Verb::POST;
other.cType = ContentType::NONE;
}
Request::Request(const Request& other)
: verb(other.verb), rsrc(other.rsrc), queries(other.queries), header(other.header), cType(other.cType),
body(other.body)
{
}
Request& Request::operator=(Request&& other) noexcept
{
if (this == &other)
return *this;
verb = other.verb;
rsrc = (Str_8 &&)other.rsrc;
queries = (Vector<QueryVar> &&)other.queries;
header = (Vector<HeaderVar> &&)other.header;
cType = other.cType;
body = (Str_8 &&)other.body;
other.verb = Verb::POST;
other.cType = ContentType::NONE;
return* this;
}
Request& Request::operator=(const Request &other)
{
if (this == &other)
return *this; return *this;
verb = req.verb; verb = other.verb;
rsrc = req.rsrc; rsrc = other.rsrc;
queries = req.queries; queries = other.queries;
header = req.header; header = other.header;
cType = req.cType; cType = other.cType;
body = req.body; body = other.body;
return* this; return* this;
} }
@ -45,7 +77,7 @@ namespace ehs
return verb; return verb;
} }
void Request::SetContentType(const ContentType cType) void Request::SetContentType(const ContentType &cType)
{ {
if (body.Size()) if (body.Size())
body.Resize(0); body.Resize(0);
@ -58,9 +90,9 @@ namespace ehs
return cType; return cType;
} }
void Request::SetResource(const Str_8& rsrc) void Request::SetResource(Str_8 rsrc)
{ {
this->rsrc = rsrc; this->rsrc = (Str_8 &&)rsrc;
} }
Str_8 Request::GetResource() const Str_8 Request::GetResource() const
@ -68,69 +100,179 @@ namespace ehs
return rsrc; return rsrc;
} }
void Request::AddQuery(const Str_8& var, const Str_8& value) bool Request::HasQueryVar(const UInt_64& id) const
{ {
queries.Push(var + "=" + value); for (UInt_64 i = 0; i < queries.Size(); ++i)
if (queries[i] == id)
return true;
return false;
} }
Str_8 Request::GetQuery(const Str_8& var) bool Request::HasQueryVar(const Str_8& name) const
{
return HasQueryVar(name.Hash_64());
}
bool Request::AddQueryVar(QueryVar var)
{
if (HasQueryVar(var.GetId()))
return false;
queries.Push((QueryVar &&)var);
return true;
}
bool Request::RemoveQueryVar(const UInt_64& id)
{ {
for (UInt_64 i = 0; i < queries.Size(); ++i) for (UInt_64 i = 0; i < queries.Size(); ++i)
{ {
Vector<Str_8> data = queries[i].Split("="); if (queries[i] != id)
continue;
if (data[0] == var) queries.Swap(i, queries.End());
return data[1]; queries.Pop();
return true;
} }
return ""; return false;
} }
Vector<Str_8> Request::GetQueries() const bool Request::RemoveQueryVar(const Str_8& name)
{
return RemoveQueryVar(name.Hash_64());
}
QueryVar *Request::GetQueryVar(const UInt_64& id) const
{
for (UInt_64 i = 0; i < queries.Size(); ++i)
if (queries[i] == id)
return &queries[i];
return nullptr;
}
QueryVar *Request::GetQueryVar(const Str_8& name) const
{
return GetQueryVar(name.Hash_64());
}
Str_8 Request::GetQueryValue(const UInt_64& id) const
{
for (UInt_64 i = 0; i < queries.Size(); ++i)
if (queries[i] == id)
return queries[i].GetValue();
return {};
}
Str_8 Request::GetQueryValue(const Str_8& name) const
{
return GetQueryValue(name.Hash_64());
}
Vector<QueryVar> Request::GetQueries() const
{ {
return queries; return queries;
} }
void Request::BasicAuth(const Str_8& id, const Str_8& secret) void Request::BasicAuth(const Str_8& id, const Str_8& secret)
{ {
AddToHeader("Authorization", Str_8("Basic ") + Base64::Encode(id + ":" + secret)); AddHeaderVar({"Authorization", Str_8("Basic ") + Base64::Encode(id + ":" + secret)});
} }
void Request::BearerAuth(const Str_8& token) void Request::BearerAuth(const Str_8& token)
{ {
AddToHeader("Authorization", "Bearer " + token); AddHeaderVar({"Authorization", "Bearer " + token});
} }
void Request::BearerAuth(const Str_8& token, const Str_8& clientId) void Request::BearerAuth(const Str_8& token, const Str_8& clientId)
{ {
AddToHeader("Authorization", "Bearer " + token); AddHeaderVar({"Authorization", "Bearer " + token});
AddToHeader("Client-Id", clientId); AddHeaderVar({"Client-Id", clientId});
} }
void Request::BotAuth(const Str_8& token) void Request::BotAuth(const Str_8& token)
{ {
AddToHeader("Authorization", "Bot " + token); AddHeaderVar({"Authorization", "Bot " + token});
} }
void Request::AddToHeader(const Str_8& var, const Str_8& value) bool Request::HasHeaderVar(const UInt_64& id) const
{ {
header.Push(var + ": " + value); for (UInt_64 i = 0; i < header.Size(); ++i)
if (header[i] == id)
return true;
return false;
} }
Str_8 Request::GetHeader(const Str_8& var) const bool Request::HasHeaderVar(const Str_8& name) const
{
return HasHeaderVar(name.Hash_64());
}
bool Request::AddHeaderVar(HeaderVar var)
{
if (HasHeaderVar(var.GetId()))
return false;
header.Push((HeaderVar &&)var);
return true;
}
bool Request::RemoveHeaderVar(const UInt_64 &id)
{ {
for (UInt_64 i = 0; i < header.Size(); ++i) for (UInt_64 i = 0; i < header.Size(); ++i)
{ {
Vector<Str_8> data = header[i].Split(": "); if (header[i] != id)
continue;
if (data[0] == var) header.Swap(i, header.End());
return data[1]; header.Pop();
return true;
} }
return ""; return false;
} }
Vector<Str_8> Request::GetHeader() const bool Request::RemoveHeaderVar(const Str_8 &name)
{
return RemoveHeaderVar(name.Hash_64());
}
HeaderVar *Request::GetHeaderVar(const UInt_64 &id) const
{
for (UInt_64 i = 0; i < header.Size(); ++i)
if (header[i] == id)
return &header[i];
return nullptr;
}
HeaderVar *Request::GetHeaderVar(const Str_8 &name) const
{
return GetHeaderVar(name.Hash_64());
}
Str_8 Request::GetHeaderValue(const UInt_64 &id) const
{
for (UInt_64 i = 0; i < header.Size(); ++i)
if (header[i] == id)
return header[i].GetValue();
return {};
}
Str_8 Request::GetHeaderValue(const Str_8 &name) const
{
return GetHeaderValue(name.Hash_64());
}
Vector<HeaderVar> Request::GetHeader() const
{ {
return header; return header;
} }
@ -200,16 +342,20 @@ namespace ehs
Str_8 result = VerbToStr(verb) + " " + rsrc; Str_8 result = VerbToStr(verb) + " " + rsrc;
if (queries.Size()) if (queries.Size())
result += "?" + queries[0]; result += "?" + queries[0].ToStr();
for (UInt_64 i = 1; i < queries.Size(); ++i) for (UInt_64 i = 1; i < queries.Size(); ++i)
result += "&" + queries[i]; result += "&" + queries[i].ToStr();
result += " HTTP/1.1\r\n"; result += " HTTP/1.1\r\n";
for (UInt_64 i = 0; i < header.Size(); ++i) for (UInt_64 i = 0; i < header.Size(); ++i)
{ {
result += header[i] + "\r\n"; if (header[i] == 11245195881701120957ull || header[i] == 6556302699635904946ull ||
header[i] == 13347276190653935221ull) // Content-Length, Server, and Content-Type in order from left to right.
continue;
result += header[i].ToStr() + "\r\n";
} }
result += "Content-Type: " + ContentTypeToStr(cType) + "\r\n"; result += "Content-Type: " + ContentTypeToStr(cType) + "\r\n";
@ -227,7 +373,7 @@ namespace ehs
return rsrc.Size() || queries.Size() || header.Size() || body.Size(); return rsrc.Size() || queries.Size() || header.Size() || body.Size();
} }
Str_8 Request::VerbToStr(const Verb verb) Str_8 Request::VerbToStr(const Verb &verb)
{ {
switch (verb) switch (verb)
{ {
@ -244,7 +390,7 @@ namespace ehs
} }
} }
Str_8 Request::ContentTypeToStr(const ContentType cType) Str_8 Request::ContentTypeToStr(const ContentType &cType)
{ {
switch (cType) switch (cType)
{ {
@ -262,6 +408,8 @@ namespace ehs
return "text/plain"; return "text/plain";
case ContentType::TEXT_HTML: case ContentType::TEXT_HTML:
return "text/html"; return "text/html";
case ContentType::TEXT_CSS:
return "text/css";
case ContentType::TEXT_XML: case ContentType::TEXT_XML:
return "text/xml"; return "text/xml";
case ContentType::IMG_X_ICON: case ContentType::IMG_X_ICON:
@ -271,7 +419,7 @@ namespace ehs
} }
} }
ContentType Request::StrToContentType(const Str_8& value) ContentType Request::StrToContentType(const Str_8 &value)
{ {
if (value == "multipart/form-data") if (value == "multipart/form-data")
return ContentType::APP_MULTIPART_FORMDATA; return ContentType::APP_MULTIPART_FORMDATA;
@ -287,6 +435,8 @@ namespace ehs
return ContentType::TEXT_PLAIN; return ContentType::TEXT_PLAIN;
else if (value == "text/html") else if (value == "text/html")
return ContentType::TEXT_HTML; return ContentType::TEXT_HTML;
else if (value == "text/css")
return ContentType::TEXT_CSS;
else if (value == "text/xml") else if (value == "text/xml")
return ContentType::TEXT_XML; return ContentType::TEXT_XML;
else if (value == "image/x-icon") else if (value == "image/x-icon")
@ -295,7 +445,7 @@ namespace ehs
return ContentType::NONE; return ContentType::NONE;
} }
void Request::ReadData(const Str_8& data) void Request::ReadData(const Str_8 &data)
{ {
Vector<Str_8> lines = data.Split("\r\n"); Vector<Str_8> lines = data.Split("\r\n");
Vector<Str_8> meta = lines[0].Split(" "); Vector<Str_8> meta = lines[0].Split(" ");
@ -312,7 +462,14 @@ namespace ehs
{ {
rsrc = meta[1].Sub(0, queryIndex); rsrc = meta[1].Sub(0, queryIndex);
cType = ContentType::APP_FORMURLENCODED; cType = ContentType::APP_FORMURLENCODED;
queries = meta[1].Sub(queryIndex + 1).Split("&");
Vector<Str_8> strQueries = meta[1].Sub(queryIndex + 1).Split("&");
for (UInt_64 i = 0; i < strQueries.Size(); ++i)
{
const Vector<Str_8> queryVar = strQueries[i].Split("=");
queries.Push({queryVar[0], queryVar[1]});
}
} }
else else
{ {
@ -335,7 +492,9 @@ namespace ehs
if (var[0] == "Content-Length") if (var[0] == "Content-Length")
continue; continue;
header.Push(lines[i]); Vector<Str_8> headerVar = lines[i].Split(": ");
header.Push({headerVar[0], headerVar[1]});
} }
} }
} }

View File

@ -2,43 +2,73 @@
namespace ehs namespace ehs
{ {
Response::Response(const UInt_32 code, const Str_8& server)
: code(code), server(server), cType(ContentType::NONE)
{
}
Response::Response(const char* data, const UInt_64 size)
: code(0), cType(ContentType::NONE)
{
ReadData(Str_8(data, size));
}
Response::Response(const Str_8& data)
: code(0), cType(ContentType::NONE)
{
ReadData(data);
}
Response::Response() Response::Response()
: code(0), cType(ContentType::NONE) : code(0), cType(ContentType::NONE)
{ {
} }
Response& Response::operator=(const Response& res) Response::Response(const UInt_32 &code, Str_8 server)
: code(code), server((Str_8 &&)server), cType(ContentType::NONE)
{ {
if (this == &res) }
return *this;
code = res.code; Response::Response(const char *data, const UInt_64 &size)
server = res.server; : code(0), cType(ContentType::NONE)
cType = res.cType; {
header = res.header; ReadData(Str_8(data, size));
body = res.body; }
Response::Response(const Str_8 &data)
: code(0), cType(ContentType::NONE)
{
ReadData(data);
}
Response::Response(Response&& other) noexcept
: code(other.code), server((Str_8 &&)other.server), cType(other.cType), header((Vector<HeaderVar> &&)other.header),
body((Str_8 &&)other.body)
{
other.code = 0;
other.cType = ContentType::NONE;
}
Response::Response(const Response &other)
: code(other.code), server(other.server), cType(other.cType), header(other.header), body(other.body)
{
}
Response& Response::operator=(Response&& other) noexcept
{
if (this == &other)
return *this;
code = other.code;
server = (Str_8 &&)other.server;
cType = other.cType;
header = (Vector<HeaderVar> &&)other.header;
body = (Str_8 &&)other.body;
other.code = 0;
other.cType = ContentType::NONE;
return *this; return *this;
} }
void Response::SetCode(const UInt_32 code) Response& Response::operator=(const Response& other)
{
if (this == &other)
return *this;
code = other.code;
server = other.server;
cType = other.cType;
header = other.header;
body = other.body;
return *this;
}
void Response::SetCode(const UInt_32 &code)
{ {
this->code = code; this->code = code;
} }
@ -48,9 +78,9 @@ namespace ehs
return code; return code;
} }
void Response::SetServer(const Str_8& server) void Response::SetServer(Str_8 server)
{ {
this->server = server; this->server = (Str_8 &&)server;
} }
Str_8 Response::GetServer() const Str_8 Response::GetServer() const
@ -58,7 +88,7 @@ namespace ehs
return server; return server;
} }
void Response::SetContentType(const ContentType cType) void Response::SetContentType(const ContentType &cType)
{ {
this->cType = cType; this->cType = cType;
} }
@ -68,27 +98,80 @@ namespace ehs
return cType; return cType;
} }
void Response::AddToHeader(const Str_8& var, const Str_8& value) bool Response::HasHeaderVar(const UInt_64& id) const
{ {
header.Push(var + ": " + value); for (UInt_64 i = 0; i < header.Size(); ++i)
if (header[i] == id)
return true;
return false;
} }
Str_8 Response::GetHeader(const Str_8& var) const bool Response::HasHeaderVar(const Str_8& name) const
{ {
Str_8 lIdentity = var.GetLower(); return HasHeaderVar(name.Hash_64());
}
bool Response::AddHeaderVar(HeaderVar var)
{
if (HasHeaderVar(var.GetId()))
return false;
header.Push((HeaderVar &&)var);
return true;
}
bool Response::RemoveHeaderVar(const UInt_64& id)
{
for (UInt_64 i = 0; i < header.Size(); ++i) for (UInt_64 i = 0; i < header.Size(); ++i)
{ {
Vector<Str_8> data = header[i].Split(": "); if (header[i] != id)
continue;
if (data[0].GetLower() == lIdentity) header.Swap(i, header.End());
return data[1]; header.Pop();
return true;
} }
return ""; return false;
} }
Vector<Str_8> Response::GetHeader() const bool Response::RemoveHeaderVar(const Str_8& name)
{
return RemoveHeaderVar(name.Hash_64());
}
HeaderVar* Response::GetHeaderVar(const UInt_64 &id) const
{
for (UInt_64 i = 0; i < header.Size(); ++i)
if (header[i] == id)
return &header[i];
return nullptr;
}
HeaderVar *Response::GetHeaderVar(const Str_8 &name) const
{
return GetHeaderVar(name.Hash_64());
}
Str_8 Response::GetHeaderValue(const UInt_64& id) const
{
for (UInt_64 i = 0; i < header.Size(); ++i)
if (header[i] == id)
return header[i].GetValue();
return {};
}
Str_8 Response::GetHeaderValue(const Str_8& name) const
{
return GetHeaderValue(name.Hash_64());
}
Vector<HeaderVar> Response::GetHeader() const
{ {
return header; return header;
} }
@ -159,14 +242,11 @@ namespace ehs
for (UInt_64 i = 0; i < header.Size(); ++i) for (UInt_64 i = 0; i < header.Size(); ++i)
{ {
if (header[i].Find("Content-Length", nullptr, SearchPattern::LEFT_RIGHT, IndexResult::ENDING)) if (header[i] == 11245195881701120957ull || header[i] == 6556302699635904946ull ||
continue; header[i] == 13347276190653935221ull) // Content-Length, Server, and Content-Type in order from left to right.
else if (header[i].Find("Server", nullptr, SearchPattern::LEFT_RIGHT, IndexResult::ENDING))
continue;
else if (header[i].Find("Content-Type", nullptr, SearchPattern::LEFT_RIGHT, IndexResult::ENDING))
continue; continue;
result += header[i] + "\r\n"; result += header[i].ToStr() + "\r\n";
} }
result += "Content-Type: " + ContentTypeToStr(cType) + "\r\nContent-Length: " + Str_8::FromNum(body.Size()) + "\r\n\r\n" + body; result += "Content-Type: " + ContentTypeToStr(cType) + "\r\nContent-Length: " + Str_8::FromNum(body.Size()) + "\r\n\r\n" + body;
@ -179,7 +259,7 @@ namespace ehs
return server.Size() || header.Size() || body.Size(); return server.Size() || header.Size() || body.Size();
} }
Str_8 Response::CodeToStr(const UInt_32 code) Str_8 Response::CodeToStr(const UInt_32 &code)
{ {
if (code == 100) if (code == 100)
return "Continue"; return "Continue";
@ -323,7 +403,7 @@ namespace ehs
return "Unused Status Code"; return "Unused Status Code";
} }
Str_8 Response::ContentTypeToStr(const ContentType cType) Str_8 Response::ContentTypeToStr(const ContentType &cType)
{ {
switch (cType) switch (cType)
{ {
@ -341,6 +421,8 @@ namespace ehs
return "text/plain"; return "text/plain";
case ContentType::TEXT_HTML: case ContentType::TEXT_HTML:
return "text/html"; return "text/html";
case ContentType::TEXT_CSS:
return "text/css";
case ContentType::TEXT_XML: case ContentType::TEXT_XML:
return "text/xml"; return "text/xml";
case ContentType::IMG_X_ICON: case ContentType::IMG_X_ICON:
@ -350,7 +432,7 @@ namespace ehs
} }
} }
ContentType Response::StrToContentType(const Str_8& value) ContentType Response::StrToContentType(const Str_8 &value)
{ {
if (value == "multipart/form-data") if (value == "multipart/form-data")
return ContentType::APP_MULTIPART_FORMDATA; return ContentType::APP_MULTIPART_FORMDATA;
@ -366,6 +448,8 @@ namespace ehs
return ContentType::TEXT_PLAIN; return ContentType::TEXT_PLAIN;
else if (value == "text/html") else if (value == "text/html")
return ContentType::TEXT_HTML; return ContentType::TEXT_HTML;
else if (value == "text/css")
return ContentType::TEXT_CSS;
else if (value == "text/xml") else if (value == "text/xml")
return ContentType::TEXT_XML; return ContentType::TEXT_XML;
else if (value == "image/x-icon") else if (value == "image/x-icon")
@ -374,7 +458,7 @@ namespace ehs
return ContentType::NONE; return ContentType::NONE;
} }
void Response::ReadData(const Str_8& data) void Response::ReadData(const Str_8 &data)
{ {
Vector<Str_8> lines = data.Split("\r\n"); Vector<Str_8> lines = data.Split("\r\n");
Vector<Str_8> meta = lines[0].Split(" "); Vector<Str_8> meta = lines[0].Split(" ");
@ -388,12 +472,12 @@ namespace ehs
Vector<Str_8> var = lines[i].Split(": "); Vector<Str_8> var = lines[i].Split(": ");
if (var[0].GetLower() == "server") if (var[0] == "Server")
{ {
server = var[1]; server = var[1];
continue; continue;
} }
else if (var[0].GetLower() == "content-type") else if (var[0] == "Content-Type")
{ {
Vector<Str_8> ctData = var[1].Split(";"); Vector<Str_8> ctData = var[1].Split(";");
@ -401,7 +485,9 @@ namespace ehs
continue; continue;
} }
header.Push(lines[i]); Vector<Str_8> headerVar = lines[i].Split(": ");
header.Push({headerVar[0], headerVar[1]});
} }
} }
} }

View File

@ -147,14 +147,6 @@ namespace ehs
} }
} }
void SSL::Bind(const Str_8& address, unsigned short port)
{
if (bound)
return;
TCP::Bind(address, port);
}
void SSL::Listen() void SSL::Listen()
{ {
sslHdl = SSL_new(ctx); sslHdl = SSL_new(ctx);
@ -200,7 +192,7 @@ namespace ehs
return client; return client;
} }
void SSL::Connect(const Str_8& address, const UInt_16 port) void SSL::Connect(Str_8 address, const UInt_16 &port)
{ {
TCP::Connect(address, port); TCP::Connect(address, port);

View File

@ -114,7 +114,7 @@ namespace ehs
hdl = EHS_INVALID_SOCKET; hdl = EHS_INVALID_SOCKET;
} }
void TCP::Bind(const Str_8& address, UInt_16 port) void TCP::Bind(Str_8 address, const UInt_16 &port)
{ {
if (!IsValid() || bound || connection) if (!IsValid() || bound || connection)
return; return;
@ -124,7 +124,7 @@ namespace ehs
else if (ip == IP::V4) else if (ip == IP::V4)
Bind_v4(address, port); Bind_v4(address, port);
this->localAddr = address; this->localAddr = (Str_8 &&)address;
this->localPort = port; this->localPort = port;
bound = true; bound = true;
@ -207,26 +207,26 @@ namespace ehs
return client; return client;
} }
void TCP::Connect(const Str_8& address, const UInt_16 port) void TCP::Connect(Str_8 address, const UInt_16 &port)
{ {
if (connection || !IsValid() || listening) if (connection || !IsValid() || listening)
return; return;
remoteHostName = address; remoteHostName = (Str_8 &&)address;
remotePort = port; remotePort = port;
if (ip == IP::V6) if (ip == IP::V6)
{ {
if (IsIPv6Only()) if (IsIPv6Only())
remoteAddr = DNS::Resolve(IP::V6, address); remoteAddr = DNS::Resolve(IP::V6, remoteHostName);
else else
remoteAddr = DNS::Resolve(address); remoteAddr = DNS::Resolve(remoteHostName);
Connect_v6(remoteAddr, port); Connect_v6(remoteAddr, port);
} }
else if (ip == IP::V4) else if (ip == IP::V4)
{ {
remoteAddr = DNS::Resolve(IP::V4, address); remoteAddr = DNS::Resolve(IP::V4, remoteHostName);
Connect_v4(remoteAddr, port); Connect_v4(remoteAddr, port);
} }
@ -408,7 +408,7 @@ namespace ehs
return hdl != EHS_INVALID_SOCKET; return hdl != EHS_INVALID_SOCKET;
} }
void TCP::Bind_v6(const Str_8& address, UInt_16 port) void TCP::Bind_v6(const Str_8& address, const UInt_16 &port) const
{ {
sockaddr_in6 result = {}; sockaddr_in6 result = {};
result.sin6_family = AF_INET6; result.sin6_family = AF_INET6;
@ -444,7 +444,7 @@ namespace ehs
} }
} }
void TCP::Bind_v4(const Str_8& address, UInt_16 port) void TCP::Bind_v4(const Str_8& address, const UInt_16 &port) const
{ {
sockaddr_in result = {}; sockaddr_in result = {};
result.sin_family = AF_INET; result.sin_family = AF_INET;
@ -479,7 +479,7 @@ namespace ehs
} }
} }
void TCP::Connect_v6(const Str_8& address, UInt_16 port) void TCP::Connect_v6(const Str_8 &address, const UInt_16 &port)
{ {
sockaddr_in6 result = {}; sockaddr_in6 result = {};
result.sin6_family = AF_INET6; result.sin6_family = AF_INET6;
@ -516,7 +516,7 @@ namespace ehs
} }
} }
void TCP::Connect_v4(const Str_8& address, UInt_16 port) void TCP::Connect_v4(const Str_8 &address, const UInt_16 &port)
{ {
sockaddr_in result = {}; sockaddr_in result = {};
result.sin_family = AF_INET; result.sin_family = AF_INET;

View File

@ -137,7 +137,7 @@ namespace ehs
connected = false; connected = false;
} }
void TCP::Bind(const Str_8& address, UInt_16 port) void TCP::Bind(Str_8 address, const UInt_16 &port)
{ {
if (!IsValid() || bound || connection) if (!IsValid() || bound || connection)
return; return;
@ -147,7 +147,7 @@ namespace ehs
else if (ip == IP::V4) else if (ip == IP::V4)
Bind_v4(address, port); Bind_v4(address, port);
this->localAddr = address; this->localAddr = (Str_8 &&)address;
this->localPort = port; this->localPort = port;
bound = true; bound = true;
@ -236,26 +236,26 @@ namespace ehs
return client; return client;
} }
void TCP::Connect(const Str_8& address, const UInt_16 port) void TCP::Connect(Str_8 address, const UInt_16 &port)
{ {
if (connection || !IsValid() || listening) if (connection || !IsValid() || listening)
return; return;
remoteHostName = address; remoteHostName = (Str_8 &&)address;
remotePort = port; remotePort = port;
if (ip == IP::V6) if (ip == IP::V6)
{ {
if (IsIPv6Only()) if (IsIPv6Only())
remoteAddr = DNS::Resolve(IP::V6, address); remoteAddr = DNS::Resolve(IP::V6, remoteHostName);
else else
remoteAddr = DNS::Resolve(address); remoteAddr = DNS::Resolve(remoteHostName);
Connect_v6(remoteAddr, port); Connect_v6(remoteAddr, port);
} }
else if (ip == IP::V4) else if (ip == IP::V4)
{ {
remoteAddr = DNS::Resolve(IP::V4, address); remoteAddr = DNS::Resolve(IP::V4, remoteHostName);
Connect_v4(remoteAddr, port); Connect_v4(remoteAddr, port);
} }

View File

@ -81,7 +81,7 @@ namespace ehs
authReq.SetContentType(ContentType::APP_FORMURLENCODED); authReq.SetContentType(ContentType::APP_FORMURLENCODED);
authReq.BasicAuth(clientId, secret); authReq.BasicAuth(clientId, secret);
authReq.AddToBody("grant_type", "authorization_code"); authReq.AddToBody("grant_type", "authorization_code");
authReq.AddToBody("code", cbReq.GetQuery("code")); authReq.AddToBody("code", cbReq.GetQueryValue("code"));
authReq.AddToBody("redirect_uri", rURI); authReq.AddToBody("redirect_uri", rURI);
accounts.SendReq(authReq); accounts.SendReq(authReq);
@ -134,7 +134,7 @@ namespace ehs
StartConnection(); StartConnection();
Request req(Verb::PUT, "/v1/me/player/volume"); Request req(Verb::PUT, "/v1/me/player/volume");
req.AddQuery("volume_percent", Str_8::FromNum(level)); req.AddQueryVar({"volume_percent", Str_8::FromNum(level)});
req.BearerAuth(token); req.BearerAuth(token);
client.SendReq(req); client.SendReq(req);
@ -206,7 +206,7 @@ namespace ehs
} }
Request req(Verb::PUT, "/v1/me/player/repeat"); Request req(Verb::PUT, "/v1/me/player/repeat");
req.AddQuery("state", result); req.AddQueryVar({"state", result});
req.BearerAuth(token); req.BearerAuth(token);
client.SendReq(req); client.SendReq(req);
@ -226,7 +226,7 @@ namespace ehs
StartConnection(); StartConnection();
Request req(Verb::PUT, "/v1/me/player/repeat"); Request req(Verb::PUT, "/v1/me/player/repeat");
req.AddQuery("state", state ? "true" : "false"); req.AddQueryVar({"state", state ? "true" : "false"});
req.BearerAuth(token); req.BearerAuth(token);
client.SendReq(req); client.SendReq(req);
@ -258,10 +258,10 @@ namespace ehs
q += "+track%3A" + name.ReplaceAll(" ", "+"); q += "+track%3A" + name.ReplaceAll(" ", "+");
req.AddQuery("q", q); req.AddQueryVar({"q", q});
req.AddQuery("type", "track"); req.AddQueryVar({"type", "track"});
req.AddQuery("limit", "1"); req.AddQueryVar({"limit", "1"});
req.AddQuery("offset", "0"); req.AddQueryVar({"offset", "0"});
req.BearerAuth(token); req.BearerAuth(token);
client.SendReq(req); client.SendReq(req);
@ -454,7 +454,7 @@ namespace ehs
StartConnection(); StartConnection();
Request req(Verb::POST, "/v1/me/player/queue"); Request req(Verb::POST, "/v1/me/player/queue");
req.AddQuery("uri", "spotify:track:" + id); req.AddQueryVar({"uri", "spotify:track:" + id});
req.BearerAuth(token); req.BearerAuth(token);
client.SendReq(req); client.SendReq(req);
@ -505,8 +505,8 @@ namespace ehs
StartConnection(); StartConnection();
Request req(Verb::POST, "/v1/playlists/" + playlistId + "/tracks"); Request req(Verb::POST, "/v1/playlists/" + playlistId + "/tracks");
req.AddQuery("position", Str_8::FromNum(pos)); req.AddQueryVar({"position", Str_8::FromNum(pos)});
req.AddQuery("uris", "spotify:track:" + trackId); req.AddQueryVar({"uris", "spotify:track:" + trackId});
req.BearerAuth(token); req.BearerAuth(token);
client.SendReq(req); client.SendReq(req);
@ -564,7 +564,7 @@ namespace ehs
StartConnection(); StartConnection();
Request req(Verb::PUT, "/v1/me/player/seek"); Request req(Verb::PUT, "/v1/me/player/seek");
req.AddQuery("position_ms", Str_8::FromNum(pos)); req.AddQueryVar({"position_ms", Str_8::FromNum(pos)});
req.BearerAuth(token); req.BearerAuth(token);
client.SendReq(req); client.SendReq(req);

View File

@ -78,7 +78,7 @@ namespace ehs
authReq.SetContentType(ContentType::APP_FORMURLENCODED); authReq.SetContentType(ContentType::APP_FORMURLENCODED);
authReq.AddToBody("client_id", clientId); authReq.AddToBody("client_id", clientId);
authReq.AddToBody("client_secret", secret); authReq.AddToBody("client_secret", secret);
authReq.AddToBody("code", cbReq.GetQuery("code")); authReq.AddToBody("code", cbReq.GetQueryValue("code"));
authReq.AddToBody("grant_type", "authorization_code"); authReq.AddToBody("grant_type", "authorization_code");
authReq.AddToBody("redirect_uri", redURI); authReq.AddToBody("redirect_uri", redURI);