diff --git a/CMakeLists.txt b/CMakeLists.txt index 8957e8e..7d710b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -201,6 +201,8 @@ set(EHS_SOURCES src/io/socket/ehc/NetUtils.cpp include/ehs/io/socket/BaseICMP.h src/io/socket/BaseICMP.cpp 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) diff --git a/include/ehs/io/socket/BaseTCP.h b/include/ehs/io/socket/BaseTCP.h index 4010bcb..3908ac5 100644 --- a/include/ehs/io/socket/BaseTCP.h +++ b/include/ehs/io/socket/BaseTCP.h @@ -24,9 +24,9 @@ namespace ehs bool connected; public: - static const UInt_16 HTTPS_Port = 443; - static const UInt_16 HTTP_Port = 80; - static const UInt_16 MaxHeaderSize = 8192; + static constexpr UInt_16 HTTPS_Port = 443; + static constexpr UInt_16 HTTP_Port = 80; + static constexpr UInt_16 MaxHeaderSize = 8192; virtual ~BaseTCP() = default; @@ -55,7 +55,7 @@ namespace ehs /// @param [in] address The ip address to bind to. /// @param [in] port The port to bind to. /// @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. /// @note Used for servers. @@ -63,13 +63,13 @@ namespace ehs /// Accepts the new incoming connection. /// @note Used for servers. - virtual BaseTCP* Accept() = 0; + virtual BaseTCP *Accept() = 0; /// Connects to a server at the specified address and port. /// @param [in] address The ip address to connect to. /// @param [in] port The port to connect to. /// @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. /// @param [in] buffer The data to send to the endpoint. diff --git a/include/ehs/io/socket/HeaderVar.h b/include/ehs/io/socket/HeaderVar.h new file mode 100644 index 0000000..ade976a --- /dev/null +++ b/include/ehs/io/socket/HeaderVar.h @@ -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; + }; +} diff --git a/include/ehs/io/socket/QueryVar.h b/include/ehs/io/socket/QueryVar.h new file mode 100644 index 0000000..a420097 --- /dev/null +++ b/include/ehs/io/socket/QueryVar.h @@ -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; + }; +} \ No newline at end of file diff --git a/include/ehs/io/socket/Request.h b/include/ehs/io/socket/Request.h index f97e10a..902a6ed 100644 --- a/include/ehs/io/socket/Request.h +++ b/include/ehs/io/socket/Request.h @@ -5,6 +5,8 @@ #include "ehs/Str.h" #include "ehs/json/Json.h" #include "Socket.h" +#include "QueryVar.h" +#include "HeaderVar.h" namespace ehs { @@ -21,8 +23,8 @@ namespace ehs private: Verb verb; Str_8 rsrc; - Vector queries; - Vector header; + Vector queries; + Vector header; ContentType cType; Str_8 body; @@ -33,25 +35,34 @@ namespace ehs /// Initializes this request with a given verb and URI resource. /// @param [in] verb The type of request to make. /// @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. /// @param [in] data The C-style string of the request. /// @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. /// @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. - /// @param [in] req The object to copy from. - Request(const Request& req) = default; + /// @param [in] other The object to copy from. + 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. - /// @param [in] req The object to copy from. + /// @param [in] other The object to copy from. /// @returns The request that has been assigned to. - Request& operator=(const Request& req); + Request& operator=(const Request &other); /// Retrieves the verb for the request. /// @returns The result. @@ -59,7 +70,7 @@ namespace ehs /// Sets the content type for the body. /// @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. /// @returns The result. @@ -67,75 +78,145 @@ namespace ehs /// Sets the URI resource. /// @param [in] rsrc The resource. - void SetResource(const Str_8& rsrc); + void SetResource(Str_8 rsrc); /// Retrieves the URI resource. /// @returns The result. Str_8 GetResource() const; - /// Adds a query variable to the URI. - /// @param [in] var The variable identifier. - /// @param [in] value The value of the variable. - void AddQuery(const Str_8& var, const Str_8& value); + /// 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 HasQueryVar(const UInt_64 &id) const; - /// Retrieves a query variable from the URI. - /// @param [in] var The variable identifier to look for. - /// @returns The value of the query variable. Empty if it was not found. - Str_8 GetQuery(const Str_8& var); + /// 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 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. /// @returns The result. - Vector GetQueries() const; + Vector GetQueries() const; /// A helper method to automatically add the required header variables for basic authentication. /// @param [in] id The username or id. /// @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. /// @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. /// @param [in] token The token 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. /// @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. - /// @param [in] var The variable identifier. - /// @param [in] value The value of the variable. - void AddToHeader(const Str_8& var, const Str_8& value); + /// @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); - /// Retrieves a header variable. - /// @param [in] var The variable identifier to look for. - /// @returns The value of the header variable. Empty if it was not found. - Str_8 GetHeader(const Str_8& var) const; + /// 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. /// @returns The result. - Vector GetHeader() const; + Vector GetHeader() const; /// Adds a body variable. /// @param [in] var The variable identifier. /// @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. /// @param [in] data The value to add. - void AddToBody(const Str_8& data); + void AddToBody(const Str_8 &data); /// Sets the entire body. /// @param [in] body The body to use. - void SetBody(const Str_8& body); + void SetBody(const Str_8 &body); /// Retrieves a body variable. /// @param [in] var The variable identifier to look for. /// @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. /// @returns The result. @@ -152,13 +233,13 @@ namespace ehs bool IsValid() const; 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); }; } \ No newline at end of file diff --git a/include/ehs/io/socket/Response.h b/include/ehs/io/socket/Response.h index 5735ba4..d76ea1e 100644 --- a/include/ehs/io/socket/Response.h +++ b/include/ehs/io/socket/Response.h @@ -5,6 +5,7 @@ #include "ehs/Str.h" #include "ehs/json/Json.h" #include "Socket.h" +#include "HeaderVar.h" namespace ehs { @@ -14,7 +15,7 @@ namespace ehs UInt_32 code; Str_8 server; ContentType cType; - Vector header; + Vector header; Str_8 body; public: @@ -24,29 +25,38 @@ namespace ehs /// Initializes this response with a given code and server identifier. /// @param [in] code The code to give. /// @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. /// @param [in] data The C-style string of the response. /// @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. /// @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. - /// @param [in] res The object to copy from. - Response(const Response& res) = default; + /// @param [in] other The object to copy from. + 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. /// @param [in] res The object to copy from. /// @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. /// @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. /// @returns The result. @@ -54,7 +64,7 @@ namespace ehs /// Sets the server identifier. /// @param [in] server The server identifier to use. - void SetServer(const Str_8& server); + void SetServer(Str_8 server); /// Retrieves the server identifier. /// @returns The result. @@ -62,25 +72,60 @@ namespace ehs /// Sets the content type for the body. /// @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. /// @returns The result. ContentType GetContentType() const; - /// Adds a header variable. - /// @param [in] var The variable identifier. - /// @param [in] value The value of the variable. - void AddToHeader(const Str_8& var, const Str_8& value); + /// 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; - /// Retrieves a header variable. - /// @param [in] var The variable identifier to look for. - /// @returns The value of the header variable. Empty if it was not found. - Str_8 GetHeader(const Str_8& var) 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. + /// @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. /// @returns The result. - Vector GetHeader() const; + Vector GetHeader() const; /// Adds a body variable. /// @param [in] var The variable identifier. @@ -115,13 +160,13 @@ namespace ehs bool IsValid() const; 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); }; } \ No newline at end of file diff --git a/include/ehs/io/socket/SSL.h b/include/ehs/io/socket/SSL.h index fa7e87d..bae197d 100644 --- a/include/ehs/io/socket/SSL.h +++ b/include/ehs/io/socket/SSL.h @@ -42,17 +42,15 @@ namespace ehs void Release() override; - void Bind(const Str_8& address, unsigned short port) override; - void Listen() 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); diff --git a/include/ehs/io/socket/Socket.h b/include/ehs/io/socket/Socket.h index 5051ece..7d2d8e3 100644 --- a/include/ehs/io/socket/Socket.h +++ b/include/ehs/io/socket/Socket.h @@ -40,6 +40,7 @@ namespace ehs APP_XML, TEXT_PLAIN, TEXT_HTML, + TEXT_CSS, TEXT_XML, IMG_X_ICON, NONE diff --git a/include/ehs/io/socket/TCP_BSD.h b/include/ehs/io/socket/TCP_BSD.h index 605259d..81f4de7 100644 --- a/include/ehs/io/socket/TCP_BSD.h +++ b/include/ehs/io/socket/TCP_BSD.h @@ -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] port The port to bind to. /// @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. void Listen() override; @@ -58,7 +58,7 @@ namespace ehs /// 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 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. /// @param [in] buffer The C-style array to send. @@ -89,12 +89,12 @@ namespace ehs bool IsValid() const override; 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); }; } \ No newline at end of file diff --git a/include/ehs/io/socket/TCP_W32.h b/include/ehs/io/socket/TCP_W32.h index 605259d..94594da 100644 --- a/include/ehs/io/socket/TCP_W32.h +++ b/include/ehs/io/socket/TCP_W32.h @@ -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] port The port to bind to. /// @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. void Listen() override; @@ -58,7 +58,7 @@ namespace ehs /// 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 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. /// @param [in] buffer The C-style array to send. @@ -89,9 +89,9 @@ namespace ehs bool IsValid() const override; 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); diff --git a/src/io/socket/BaseTCP.cpp b/src/io/socket/BaseTCP.cpp index 61320e3..2866781 100644 --- a/src/io/socket/BaseTCP.cpp +++ b/src/io/socket/BaseTCP.cpp @@ -108,7 +108,7 @@ namespace ehs if (!IsValid()) return; - req.AddToHeader("Host", remoteHostName); + req.AddHeaderVar({"Host", remoteHostName}); SendStr(req.FormResult()); } @@ -124,10 +124,10 @@ namespace ehs Response response(header); - Str_8 encoding = response.GetHeader("Transfer-Encoding"); + Str_8 encoding = response.GetHeaderValue("Transfer-Encoding"); if (!encoding.Size()) { - int bodySize = response.GetHeader("content-length").ToDecimal(); + int bodySize = response.GetHeaderValue("content-length").ToDecimal(); if (!bodySize) return response; @@ -163,10 +163,10 @@ namespace ehs if (request.GetVerb() == Verb::GET) return request; - Str_8 encoding = request.GetHeader("Transfer-Encoding"); + Str_8 encoding = request.GetHeaderValue("Transfer-Encoding"); if (!encoding.Size()) { - int bodySize = request.GetHeader("Content-Length").ToDecimal(); + int bodySize = request.GetHeaderValue("Content-Length").ToDecimal(); if (!bodySize) return request; diff --git a/src/io/socket/HeaderVar.cpp b/src/io/socket/HeaderVar.cpp new file mode 100644 index 0000000..732bc82 --- /dev/null +++ b/src/io/socket/HeaderVar.cpp @@ -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; + } +} diff --git a/src/io/socket/QueryVar.cpp b/src/io/socket/QueryVar.cpp new file mode 100644 index 0000000..7947b79 --- /dev/null +++ b/src/io/socket/QueryVar.cpp @@ -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; + } +} diff --git a/src/io/socket/Request.cpp b/src/io/socket/Request.cpp index ea976d4..6949a25 100644 --- a/src/io/socket/Request.cpp +++ b/src/io/socket/Request.cpp @@ -4,38 +4,70 @@ namespace ehs { Request::Request() - : verb(Verb::GET), cType(ContentType::NONE) + : verb(Verb::POST), cType(ContentType::NONE) { } - Request::Request(const Verb verb, const Str_8& rsrc) - : verb(verb), rsrc(rsrc), cType(ContentType::NONE) + Request::Request(const Verb &verb, Str_8 rsrc) + : 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) { ReadData(Str_8(data, size)); } - Request::Request(const Str_8& data) + Request::Request(const Str_8 &data) : verb(Verb::POST), cType(ContentType::NONE) { ReadData(data); } - - Request& Request::operator=(const Request &req) + + Request::Request(Request&& other) noexcept + : verb(other.verb), rsrc((Str_8 &&)other.rsrc), queries((Vector &&)other.queries), + header((Vector &&)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 &&)other.queries; + header = (Vector &&)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; - verb = req.verb; - rsrc = req.rsrc; - queries = req.queries; - header = req.header; - cType = req.cType; - body = req.body; + verb = other.verb; + rsrc = other.rsrc; + queries = other.queries; + header = other.header; + cType = other.cType; + body = other.body; return* this; } @@ -45,7 +77,7 @@ namespace ehs return verb; } - void Request::SetContentType(const ContentType cType) + void Request::SetContentType(const ContentType &cType) { if (body.Size()) body.Resize(0); @@ -58,9 +90,9 @@ namespace ehs 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 @@ -68,69 +100,179 @@ namespace ehs 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) { - Vector data = queries[i].Split("="); + if (queries[i] != id) + continue; - if (data[0] == var) - return data[1]; + queries.Swap(i, queries.End()); + queries.Pop(); + + return true; } - return ""; + return false; } - Vector 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 Request::GetQueries() const { return queries; } 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) { - AddToHeader("Authorization", "Bearer " + token); + AddHeaderVar({"Authorization", "Bearer " + token}); } void Request::BearerAuth(const Str_8& token, const Str_8& clientId) { - AddToHeader("Authorization", "Bearer " + token); - AddToHeader("Client-Id", clientId); + AddHeaderVar({"Authorization", "Bearer " + token}); + AddHeaderVar({"Client-Id", clientId}); } void Request::BotAuth(const Str_8& token) { - AddToHeader("Authorization", "Bot " + token); - } - - void Request::AddToHeader(const Str_8& var, const Str_8& value) - { - header.Push(var + ": " + value); + AddHeaderVar({"Authorization", "Bot " + token}); } - Str_8 Request::GetHeader(const Str_8& var) const + bool Request::HasHeaderVar(const UInt_64& id) const + { + for (UInt_64 i = 0; i < header.Size(); ++i) + if (header[i] == id) + return true; + + return false; + } + + 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) { - Vector data = header[i].Split(": "); + if (header[i] != id) + continue; - if (data[0] == var) - return data[1]; + header.Swap(i, header.End()); + header.Pop(); + + return true; } - return ""; + return false; } - - Vector 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 Request::GetHeader() const { return header; } @@ -200,16 +342,20 @@ namespace ehs Str_8 result = VerbToStr(verb) + " " + rsrc; if (queries.Size()) - result += "?" + queries[0]; + result += "?" + queries[0].ToStr(); for (UInt_64 i = 1; i < queries.Size(); ++i) - result += "&" + queries[i]; + result += "&" + queries[i].ToStr(); result += " HTTP/1.1\r\n"; 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"; @@ -227,7 +373,7 @@ namespace ehs 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) { @@ -244,7 +390,7 @@ namespace ehs } } - Str_8 Request::ContentTypeToStr(const ContentType cType) + Str_8 Request::ContentTypeToStr(const ContentType &cType) { switch (cType) { @@ -262,6 +408,8 @@ namespace ehs return "text/plain"; case ContentType::TEXT_HTML: return "text/html"; + case ContentType::TEXT_CSS: + return "text/css"; case ContentType::TEXT_XML: return "text/xml"; 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") return ContentType::APP_MULTIPART_FORMDATA; @@ -287,6 +435,8 @@ namespace ehs return ContentType::TEXT_PLAIN; else if (value == "text/html") return ContentType::TEXT_HTML; + else if (value == "text/css") + return ContentType::TEXT_CSS; else if (value == "text/xml") return ContentType::TEXT_XML; else if (value == "image/x-icon") @@ -295,7 +445,7 @@ namespace ehs return ContentType::NONE; } - void Request::ReadData(const Str_8& data) + void Request::ReadData(const Str_8 &data) { Vector lines = data.Split("\r\n"); Vector meta = lines[0].Split(" "); @@ -312,7 +462,14 @@ namespace ehs { rsrc = meta[1].Sub(0, queryIndex); cType = ContentType::APP_FORMURLENCODED; - queries = meta[1].Sub(queryIndex + 1).Split("&"); + + Vector strQueries = meta[1].Sub(queryIndex + 1).Split("&"); + for (UInt_64 i = 0; i < strQueries.Size(); ++i) + { + const Vector queryVar = strQueries[i].Split("="); + + queries.Push({queryVar[0], queryVar[1]}); + } } else { @@ -335,7 +492,9 @@ namespace ehs if (var[0] == "Content-Length") continue; - header.Push(lines[i]); + Vector headerVar = lines[i].Split(": "); + + header.Push({headerVar[0], headerVar[1]}); } } } \ No newline at end of file diff --git a/src/io/socket/Response.cpp b/src/io/socket/Response.cpp index eeafc69..bf7bc3f 100644 --- a/src/io/socket/Response.cpp +++ b/src/io/socket/Response.cpp @@ -2,43 +2,73 @@ 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() : 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; - server = res.server; - cType = res.cType; - header = res.header; - body = res.body; + 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&& other) noexcept + : code(other.code), server((Str_8 &&)other.server), cType(other.cType), header((Vector &&)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 &&)other.header; + body = (Str_8 &&)other.body; + + other.code = 0; + other.cType = ContentType::NONE; 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; } @@ -48,9 +78,9 @@ namespace ehs 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 @@ -58,7 +88,7 @@ namespace ehs return server; } - void Response::SetContentType(const ContentType cType) + void Response::SetContentType(const ContentType &cType) { this->cType = cType; } @@ -68,27 +98,80 @@ namespace ehs 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) { - Vector data = header[i].Split(": "); + if (header[i] != id) + continue; - if (data[0].GetLower() == lIdentity) - return data[1]; + header.Swap(i, header.End()); + header.Pop(); + + return true; } - return ""; + return false; } - Vector 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 Response::GetHeader() const { return header; } @@ -159,14 +242,11 @@ namespace ehs for (UInt_64 i = 0; i < header.Size(); ++i) { - if (header[i].Find("Content-Length", nullptr, SearchPattern::LEFT_RIGHT, IndexResult::ENDING)) - continue; - 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)) + 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] + "\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; @@ -179,7 +259,7 @@ namespace ehs 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) return "Continue"; @@ -323,7 +403,7 @@ namespace ehs return "Unused Status Code"; } - Str_8 Response::ContentTypeToStr(const ContentType cType) + Str_8 Response::ContentTypeToStr(const ContentType &cType) { switch (cType) { @@ -341,6 +421,8 @@ namespace ehs return "text/plain"; case ContentType::TEXT_HTML: return "text/html"; + case ContentType::TEXT_CSS: + return "text/css"; case ContentType::TEXT_XML: return "text/xml"; 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") return ContentType::APP_MULTIPART_FORMDATA; @@ -366,6 +448,8 @@ namespace ehs return ContentType::TEXT_PLAIN; else if (value == "text/html") return ContentType::TEXT_HTML; + else if (value == "text/css") + return ContentType::TEXT_CSS; else if (value == "text/xml") return ContentType::TEXT_XML; else if (value == "image/x-icon") @@ -374,7 +458,7 @@ namespace ehs return ContentType::NONE; } - void Response::ReadData(const Str_8& data) + void Response::ReadData(const Str_8 &data) { Vector lines = data.Split("\r\n"); Vector meta = lines[0].Split(" "); @@ -388,12 +472,12 @@ namespace ehs Vector var = lines[i].Split(": "); - if (var[0].GetLower() == "server") + if (var[0] == "Server") { server = var[1]; continue; } - else if (var[0].GetLower() == "content-type") + else if (var[0] == "Content-Type") { Vector ctData = var[1].Split(";"); @@ -401,7 +485,9 @@ namespace ehs continue; } - header.Push(lines[i]); + Vector headerVar = lines[i].Split(": "); + + header.Push({headerVar[0], headerVar[1]}); } } } \ No newline at end of file diff --git a/src/io/socket/SSL.cpp b/src/io/socket/SSL.cpp index b6e1a6a..3e57eac 100644 --- a/src/io/socket/SSL.cpp +++ b/src/io/socket/SSL.cpp @@ -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() { sslHdl = SSL_new(ctx); @@ -200,7 +192,7 @@ namespace ehs 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); diff --git a/src/io/socket/TCP_BSD.cpp b/src/io/socket/TCP_BSD.cpp index 2c43c15..b68ad29 100644 --- a/src/io/socket/TCP_BSD.cpp +++ b/src/io/socket/TCP_BSD.cpp @@ -114,7 +114,7 @@ namespace ehs 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) return; @@ -124,7 +124,7 @@ namespace ehs else if (ip == IP::V4) Bind_v4(address, port); - this->localAddr = address; + this->localAddr = (Str_8 &&)address; this->localPort = port; bound = true; @@ -207,26 +207,26 @@ namespace ehs 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) return; - remoteHostName = address; + remoteHostName = (Str_8 &&)address; remotePort = port; if (ip == IP::V6) { if (IsIPv6Only()) - remoteAddr = DNS::Resolve(IP::V6, address); + remoteAddr = DNS::Resolve(IP::V6, remoteHostName); else - remoteAddr = DNS::Resolve(address); + remoteAddr = DNS::Resolve(remoteHostName); Connect_v6(remoteAddr, port); } else if (ip == IP::V4) { - remoteAddr = DNS::Resolve(IP::V4, address); + remoteAddr = DNS::Resolve(IP::V4, remoteHostName); Connect_v4(remoteAddr, port); } @@ -408,7 +408,7 @@ namespace ehs 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 = {}; 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 = {}; 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 = {}; 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 = {}; result.sin_family = AF_INET; diff --git a/src/io/socket/TCP_W32.cpp b/src/io/socket/TCP_W32.cpp index 2f3ec99..7b19d51 100644 --- a/src/io/socket/TCP_W32.cpp +++ b/src/io/socket/TCP_W32.cpp @@ -137,7 +137,7 @@ namespace ehs 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) return; @@ -147,7 +147,7 @@ namespace ehs else if (ip == IP::V4) Bind_v4(address, port); - this->localAddr = address; + this->localAddr = (Str_8 &&)address; this->localPort = port; bound = true; @@ -236,26 +236,26 @@ namespace ehs 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) return; - remoteHostName = address; + remoteHostName = (Str_8 &&)address; remotePort = port; if (ip == IP::V6) { if (IsIPv6Only()) - remoteAddr = DNS::Resolve(IP::V6, address); + remoteAddr = DNS::Resolve(IP::V6, remoteHostName); else - remoteAddr = DNS::Resolve(address); + remoteAddr = DNS::Resolve(remoteHostName); Connect_v6(remoteAddr, port); } else if (ip == IP::V4) { - remoteAddr = DNS::Resolve(IP::V4, address); + remoteAddr = DNS::Resolve(IP::V4, remoteHostName); Connect_v4(remoteAddr, port); } diff --git a/src/io/socket/rest/Spotify.cpp b/src/io/socket/rest/Spotify.cpp index 479aded..492efb4 100644 --- a/src/io/socket/rest/Spotify.cpp +++ b/src/io/socket/rest/Spotify.cpp @@ -81,7 +81,7 @@ namespace ehs authReq.SetContentType(ContentType::APP_FORMURLENCODED); authReq.BasicAuth(clientId, secret); authReq.AddToBody("grant_type", "authorization_code"); - authReq.AddToBody("code", cbReq.GetQuery("code")); + authReq.AddToBody("code", cbReq.GetQueryValue("code")); authReq.AddToBody("redirect_uri", rURI); accounts.SendReq(authReq); @@ -134,7 +134,7 @@ namespace ehs StartConnection(); 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); client.SendReq(req); @@ -206,7 +206,7 @@ namespace ehs } Request req(Verb::PUT, "/v1/me/player/repeat"); - req.AddQuery("state", result); + req.AddQueryVar({"state", result}); req.BearerAuth(token); client.SendReq(req); @@ -226,7 +226,7 @@ namespace ehs StartConnection(); Request req(Verb::PUT, "/v1/me/player/repeat"); - req.AddQuery("state", state ? "true" : "false"); + req.AddQueryVar({"state", state ? "true" : "false"}); req.BearerAuth(token); client.SendReq(req); @@ -258,10 +258,10 @@ namespace ehs q += "+track%3A" + name.ReplaceAll(" ", "+"); - req.AddQuery("q", q); - req.AddQuery("type", "track"); - req.AddQuery("limit", "1"); - req.AddQuery("offset", "0"); + req.AddQueryVar({"q", q}); + req.AddQueryVar({"type", "track"}); + req.AddQueryVar({"limit", "1"}); + req.AddQueryVar({"offset", "0"}); req.BearerAuth(token); client.SendReq(req); @@ -454,7 +454,7 @@ namespace ehs StartConnection(); Request req(Verb::POST, "/v1/me/player/queue"); - req.AddQuery("uri", "spotify:track:" + id); + req.AddQueryVar({"uri", "spotify:track:" + id}); req.BearerAuth(token); client.SendReq(req); @@ -505,8 +505,8 @@ namespace ehs StartConnection(); Request req(Verb::POST, "/v1/playlists/" + playlistId + "/tracks"); - req.AddQuery("position", Str_8::FromNum(pos)); - req.AddQuery("uris", "spotify:track:" + trackId); + req.AddQueryVar({"position", Str_8::FromNum(pos)}); + req.AddQueryVar({"uris", "spotify:track:" + trackId}); req.BearerAuth(token); client.SendReq(req); @@ -564,7 +564,7 @@ namespace ehs StartConnection(); 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); client.SendReq(req); diff --git a/src/io/socket/rest/Twitch.cpp b/src/io/socket/rest/Twitch.cpp index e29a112..1d84f1c 100644 --- a/src/io/socket/rest/Twitch.cpp +++ b/src/io/socket/rest/Twitch.cpp @@ -78,7 +78,7 @@ namespace ehs authReq.SetContentType(ContentType::APP_FORMURLENCODED); authReq.AddToBody("client_id", clientId); 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("redirect_uri", redURI);