STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Static Public Member Functions | List of all members
stt::data::HttpStringUtil Class Reference

Responsible for HTTP string and URL parsing Including functions to extract parameters, IP, port, request header fields, etc., from URLs or request messages. More...

#include <sttnet_English.h>

Static Public Member Functions

static size_t get_split_str (const std::string_view &ori_str, std::string_view &str, const std::string_view &a, const std::string_view &b, const size_t &pos=0)
 Extract a substring between two markers from the original string. More...
 
static std::string_view & get_value_str (const std::string_view &ori_str, std::string_view &str, const std::string &name)
 Extract the value of a specified key from the URL query parameters. More...
 
static std::string_view & get_value_header (const std::string_view &ori_str, std::string_view &str, const std::string &name)
 Extract the value of a specified field from the HTTP request header. More...
 
static std::string_view & get_location_str (const std::string_view &ori_str, std::string_view &str)
 Extract the path and query part from the URL. More...
 
static std::string_view & getLocPara (const std::string_view &url, std::string_view &locPara)
 Extract the path part of the URL (excluding the query). More...
 
static std::string_view & getPara (const std::string_view &url, std::string_view &para)
 Get the query parameter string in the URL (including ?). More...
 
static size_t get_split_str (const std::string_view &ori_str, std::string &str, const std::string_view &a, const std::string_view &b, const size_t &pos=0)
 Extract a substring between two markers from the original string. More...
 
static std::string & get_value_str (const std::string &ori_str, std::string &str, const std::string &name)
 Extract the value of a specified key from the URL query parameters. More...
 
static std::string & get_value_header (const std::string &ori_str, std::string &str, const std::string &name)
 Extract the value of a specified field from the HTTP request header. More...
 
static std::string & get_location_str (const std::string &ori_str, std::string &str)
 Extract the path and query part from the URL. More...
 
static std::string & getLocPara (const std::string &url, std::string &locPara)
 Extract the path part of the URL (excluding the query). More...
 
static std::string & getPara (const std::string &url, std::string &para)
 Get the query parameter string in the URL (including ?). More...
 
static std::string & getIP (const std::string &url, std::string &IP)
 Extract the host IP or domain name from the URL. More...
 
static int & getPort (const std::string &url, int &port)
 Extract the port number from the URL. More...
 
static std::string createHeader (const std::string &first, const std::string &second)
 Create an HTTP request header field string. More...
 
template<class... Args>
static std::string createHeader (const std::string &first, const std::string &second, Args...args)
 Recursively construct multiple HTTP request header fields. More...
 

Detailed Description

Responsible for HTTP string and URL parsing Including functions to extract parameters, IP, port, request header fields, etc., from URLs or request messages.

Member Function Documentation

static std::string stt::data::HttpStringUtil::createHeader ( const std::string &  first,
const std::string &  second 
)
static

Create an HTTP request header field string.

This function constructs a string in the format field name: field value\r\n.

Parameters
firstThe first field name.
secondThe first field value.
Returns
The constructed single HTTP request header string.
template<class... Args>
static std::string stt::data::HttpStringUtil::createHeader ( const std::string &  first,
const std::string &  second,
Args...  args 
)
inlinestatic

Recursively construct multiple HTTP request header fields.

Supports the construction of multiple field names and values, used as:

std::string headers = createHeader("Host", "example.com", "Connection", "keep-alive,"Content-Type","charset=UTF-8");

Finally generated:

Host: example.com\r\n
Connection: keep-alive\r\n
Content-Type: charset=UTF-8\r\n
Parameters
ArgsThe remaining parameters, which need to be passed in pairs of (field name, field value).
firstThe current field name.
secondThe current field value.
argsSubsequent field names and values (must be even in number).
Returns
The constructed complete HTTP request header string.
static std::string_view& stt::data::HttpStringUtil::get_location_str ( const std::string_view &  ori_str,
std::string_view &  str 
)
static

Extract the path and query part from the URL.

For example, extract /path from http://abc.com/path?query=123 or from /path?query=123.

Parameters
ori_strOriginal URL.
strReturn the path part.
Returns
Reference to the result string.
static std::string& stt::data::HttpStringUtil::get_location_str ( const std::string &  ori_str,
std::string &  str 
)
static

Extract the path and query part from the URL.

For example, extract /path from http://abc.com/path?query=123 or from /path?query=123.

Parameters
ori_strOriginal URL.
strReturn the path part.
Returns
Reference to the result string.
static size_t stt::data::HttpStringUtil::get_split_str ( const std::string_view &  ori_str,
std::string_view &  str,
const std::string_view &  a,
const std::string_view &  b,
const size_t &  pos = 0 
)
static

Extract a substring between two markers from the original string.

Extracts the content from a to b (excluding a and b), and a starting search position can be specified. If a or b is an empty string, it means from the beginning or to the end, respectively. If a is not found, it defaults to starting from the beginning. If b is not found, it defaults to the end.

Parameters
ori_strOriginal string.
strString to store the extraction result.
aStart marker string.
bEnd marker string.
posStarting search position.
Returns
Returns the position of b in ori_str (may return string::npos if b is not found or b is "")
Note
If not found, the result string is ""
static size_t stt::data::HttpStringUtil::get_split_str ( const std::string_view &  ori_str,
std::string &  str,
const std::string_view &  a,
const std::string_view &  b,
const size_t &  pos = 0 
)
static

Extract a substring between two markers from the original string.

Extracts the content from a to b (excluding a and b), and a starting search position can be specified. If a or b is an empty string, it means from the beginning or to the end, respectively. If a is not found, it defaults to starting from the beginning. If b is not found, it defaults to the end.

Parameters
ori_strOriginal string.
strString to store the extraction result.
aStart marker string.
bEnd marker string.
posStarting search position.
Returns
Returns the position of b in ori_str (may return string::npos if b is not found or b is "")
Note
If not found, the result string is ""
static std::string_view& stt::data::HttpStringUtil::get_value_header ( const std::string_view &  ori_str,
std::string_view &  str,
const std::string &  name 
)
static

Extract the value of a specified field from the HTTP request header.

Parameters
ori_strOriginal HTTP request header string.
strExtraction result.
nameRequest header field name (e.g., "Host").
Returns
Reference to the result string.
static std::string& stt::data::HttpStringUtil::get_value_header ( const std::string &  ori_str,
std::string &  str,
const std::string &  name 
)
static

Extract the value of a specified field from the HTTP request header.

Parameters
ori_strOriginal HTTP request header string.
strExtraction result.
nameRequest header field name (e.g., "Host").
Returns
Reference to the result string.
static std::string_view& stt::data::HttpStringUtil::get_value_str ( const std::string_view &  ori_str,
std::string_view &  str,
const std::string &  name 
)
static

Extract the value of a specified key from the URL query parameters.

Note
The URL does not need to be complete. For example, extracting the value of id from ?id=123&name=abc is the same as extracting from http://xxxx/?id=123&name=abc.
Parameters
ori_strOriginal URL string.
strString to store the extraction result.
nameParameter name (key).
Returns
Reference to the result string.
static std::string& stt::data::HttpStringUtil::get_value_str ( const std::string &  ori_str,
std::string &  str,
const std::string &  name 
)
static

Extract the value of a specified key from the URL query parameters.

Note
The URL does not need to be complete. For example, extracting the value of id from ?id=123&name=abc is the same as extracting from http://xxxx/?id=123&name=abc.
Parameters
ori_strOriginal URL string.
strString to store the extraction result.
nameParameter name (key).
Returns
Reference to the result string.
static std::string& stt::data::HttpStringUtil::getIP ( const std::string &  url,
std::string &  IP 
)
static

Extract the host IP or domain name from the URL.

For example, extract 127.0.0.1 from http://127.0.0.1:8080/.

Warning
The URL must be complete and explicitly specify the port
Parameters
urlComplete URL.
IPStore the extracted IP or domain name.
Returns
Reference to the result string.
static std::string_view& stt::data::HttpStringUtil::getLocPara ( const std::string_view &  url,
std::string_view &  locPara 
)
static

Extract the path part of the URL (excluding the query).

Similar to get_location_str, but retains all content after the path (such as parameters).

Parameters
URL.
locParaReturn the path+parameter part.
Returns
Reference to the result string.
static std::string& stt::data::HttpStringUtil::getLocPara ( const std::string &  url,
std::string &  locPara 
)
static

Extract the path part of the URL (excluding the query).

Similar to get_location_str, but retains all content after the path (such as parameters).

Parameters
URL.
locParaReturn the path+parameter part.
Returns
Reference to the result string.
static std::string_view& stt::data::HttpStringUtil::getPara ( const std::string_view &  url,
std::string_view &  para 
)
static

Get the query parameter string in the URL (including ?).

Note
It can be a complete URL or an incomplete one, such as /path?id=123&name=abc
Parameters
URL.
paraReturn the parameter part (in the form of "?id=123&name=abc").
Returns
Reference to the result string.
static std::string& stt::data::HttpStringUtil::getPara ( const std::string &  url,
std::string &  para 
)
static

Get the query parameter string in the URL (including ?).

Note
It can be a complete URL or an incomplete one, such as /path?id=123&name=abc
Parameters
URL.
paraReturn the parameter part (in the form of "?id=123&name=abc").
Returns
Reference to the result string.
static int& stt::data::HttpStringUtil::getPort ( const std::string &  url,
int &  port 
)
static

Extract the port number from the URL.

For example, extract 8080 from http://127.0.0.1:8080/.

Warning
Must be a complete URL, and must explicitly specify the port and path, even if the path is empty, write a /
Parameters
urlComplete URL.
portStore the parsed port number.
Returns
Reference to the port number.

The documentation for this class was generated from the following file: