STTNet 0.7.0
Loading...
Searching...
No Matches
stt::data::HttpStringUtil Class Reference

负责Http字符串和URL解析 包括从 URL 或请求报文中提取参数、IP、端口、请求头字段等功能。 More...

#include <sttnet.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)
 从原始字符串中提取两个标记之间的子串。
 
static std::string_view & get_value_str (const std::string_view &ori_str, std::string_view &str, const std::string &name)
 从 URL 查询参数中提取指定 key 的值。
 
static std::string_view & get_value_header (const std::string_view &ori_str, std::string_view &str, const std::string &name)
 从 HTTP 请求头中提取指定字段的值。
 
static std::string_view & get_location_str (const std::string_view &ori_str, std::string_view &str)
 提取 URL 中 path 和 query 部分。
 
static std::string_view & getLocPara (const std::string_view &url, std::string_view &locPara)
 提取 URL 的 path 部分(不含 query)。
 
static std::string_view & getPara (const std::string_view &url, std::string_view &para)
 获取 URL 中的查询参数字符串(包括 ?)。
 
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)
 从原始字符串中提取两个标记之间的子串。
 
static std::string & get_value_str (const std::string &ori_str, std::string &str, const std::string &name)
 从 URL 查询参数中提取指定 key 的值。
 
static std::string & get_value_header (const std::string &ori_str, std::string &str, const std::string &name)
 从 HTTP 请求头中提取指定字段的值。
 
static std::string & get_location_str (const std::string &ori_str, std::string &str)
 提取 URL 中 path 和 query 部分。
 
static std::string & getLocPara (const std::string &url, std::string &locPara)
 提取 URL 的 path 部分(不含 query)。
 
static std::string & getPara (const std::string &url, std::string &para)
 获取 URL 中的查询参数字符串(包括 ?)。
 
static std::string & getIP (const std::string &url, std::string &IP)
 从 URL 中提取主机 IP 或域名。
 
static int & getPort (const std::string &url, int &port)
 从 URL 中提取端口号。
 
static std::string createHeader (const std::string &first, const std::string &second)
 创建一个 HTTP 请求头字段字符串。
 
template<class... Args>
static std::string createHeader (const std::string &first, const std::string &second, Args... args)
 递归构造多个 HTTP 请求头字段。
 

Detailed Description

Lightweight HTTP-string and URL extraction helpers.

Boundaries:
  • These functions are not a complete URI/HTTP parser and do not URL-decode.
  • Query keys are matched on field boundaries, so id does not match userid.
  • Header-name matching is case-insensitive.
  • The string_view overloads return non-owning views that must not outlive the input string.

Member Function Documentation

◆ createHeader() [1/2]

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

创建一个 HTTP 请求头字段字符串。

该函数构造格式为 字段名: 字段值\r\n 的字符串。

Parameters
first第一个字段名。
second第一个字段值。
Returns
构造的单条 HTTP 请求头字符串。

◆ createHeader() [2/2]

template<class... Args>
static std::string stt::data::HttpStringUtil::createHeader ( const std::string & first,
const std::string & second,
Args... args )
inlinestatic

递归构造多个 HTTP 请求头字段。

支持多个字段名和值的构造,用法为:

std::string headers = createHeader("Host", "example.com", "Connection", "keep-alive,"Content-Type","charset=UTF-8");
static std::string createHeader(const std::string &first, const std::string &second)
创建一个 HTTP 请求头字段字符串。

最终生成:

Host: example.com\r\n
Connection: keep-alive\r\n
Content-Type: charset=UTF-8\r\n
Template Parameters
Args其余参数类型,需以 (字段名, 字段值) 成对传入。
Parameters
first当前字段名。
second当前字段值。
args后续的字段名和值(必须为偶数个)。
Returns
构造的完整 HTTP 请求头字符串。

◆ get_location_str() [1/2]

static std::string & stt::data::HttpStringUtil::get_location_str ( const std::string & ori_str,
std::string & str )
static

提取 URL 中 path 和 query 部分。

例如从 http://abc.com/path?query=123 或者从/path?query=123 提取 /path

Parameters
ori_str原始 URL。
str返回 path 部分。
Returns
引用,指向结果字符串。

◆ get_location_str() [2/2]

static std::string_view & stt::data::HttpStringUtil::get_location_str ( const std::string_view & ori_str,
std::string_view & str )
static

提取 URL 中 path 和 query 部分。

例如从 http://abc.com/path?query=123 或者从/path?query=123 提取 /path

Parameters
ori_str原始 URL。
str返回 path 部分。
Returns
引用,指向结果字符串。

◆ get_split_str() [1/2]

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

从原始字符串中提取两个标记之间的子串。

提取从 a 到 b 之间的内容(不包含 a 和 b),可指定起始搜索位置。 若 a 或 b 为空字符串,则分别表示从头或到尾。 若a找不到,则默认从头开始 若b找不到,则默认到尾

Parameters
ori_str原始字符串。
str存储提取结果的字符串。
a起始标记字符串。
b终止标记字符串。
pos搜索起始位置。
Returns
返回b在ori_str中的位置(可能返回string::npos,如果b找不到或者b为"")
Note
如果找不到,结果字符串为""

◆ get_split_str() [2/2]

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

从原始字符串中提取两个标记之间的子串。

提取从 a 到 b 之间的内容(不包含 a 和 b),可指定起始搜索位置。 若 a 或 b 为空字符串,则分别表示从头或到尾。 若a找不到,则默认从头开始 若b找不到,则默认到尾

Parameters
ori_str原始字符串。
str存储提取结果的字符串。
a起始标记字符串。
b终止标记字符串。
pos搜索起始位置。
Returns
返回b在ori_str中的位置(可能返回string::npos,如果b找不到或者b为"")
Note
如果找不到,结果字符串为""

◆ get_value_header() [1/2]

static std::string & stt::data::HttpStringUtil::get_value_header ( const std::string & ori_str,
std::string & str,
const std::string & name )
static

从 HTTP 请求头中提取指定字段的值。

Parameters
ori_str原始 HTTP 请求头字符串。
str提取结果。
name请求头字段名(如 "Host")。
Returns
引用,指向结果字符串。

◆ get_value_header() [2/2]

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

从 HTTP 请求头中提取指定字段的值。

Parameters
ori_str原始 HTTP 请求头字符串。
str提取结果。
name请求头字段名(如 "Host")。
Returns
引用,指向结果字符串。

◆ get_value_str() [1/2]

static std::string & stt::data::HttpStringUtil::get_value_str ( const std::string & ori_str,
std::string & str,
const std::string & name )
static

从 URL 查询参数中提取指定 key 的值。

Note
url不需要完整也行 例如从 ?id=123&name=abc提取 id 的值和从http://xxxx/?id=123&name=abc提取 id 的值是一样的。
Parameters
ori_str原始 URL 字符串。
str存储提取结果的字符串。
name参数名(key)。
Returns
引用,指向结果字符串。

◆ get_value_str() [2/2]

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

从 URL 查询参数中提取指定 key 的值。

Note
url不需要完整也行 例如从 ?id=123&name=abc提取 id 的值和从http://xxxx/?id=123&name=abc提取 id 的值是一样的。
Parameters
ori_str原始 URL 字符串。
str存储提取结果的字符串。
name参数名(key)。
Returns
引用,指向结果字符串。

◆ getIP()

static std::string & stt::data::HttpStringUtil::getIP ( const std::string & url,
std::string & IP )
static

从 URL 中提取主机 IP 或域名。

例如从 http://127.0.0.1:8080/ 提取 127.0.0.1

Warning
url必须完整而且显式指明端口
Parameters
url完整 URL。
IP存储提取的 IP 或域名。
Returns
引用,指向结果字符串。

◆ getLocPara() [1/2]

static std::string & stt::data::HttpStringUtil::getLocPara ( const std::string & url,
std::string & locPara )
static

提取 URL 的 path 部分(不含 query)。

get_location_str 类似,但保留 path 之后的所有内容(如参数)。

Parameters
urlURL。
locPara返回 path+参数部分。
Returns
引用,指向结果字符串。

◆ getLocPara() [2/2]

static std::string_view & stt::data::HttpStringUtil::getLocPara ( const std::string_view & url,
std::string_view & locPara )
static

提取 URL 的 path 部分(不含 query)。

get_location_str 类似,但保留 path 之后的所有内容(如参数)。

Parameters
urlURL。
locPara返回 path+参数部分。
Returns
引用,指向结果字符串。

◆ getPara() [1/2]

static std::string & stt::data::HttpStringUtil::getPara ( const std::string & url,
std::string & para )
static

获取 URL 中的查询参数字符串(包括 ?)。

Note
可以是完整url 也可以是不太完整的,比如/path?id=123&name=abc
Parameters
urlURL。
para返回参数部分(形如 "?id=123&name=abc")。
Returns
引用,指向结果字符串。

◆ getPara() [2/2]

static std::string_view & stt::data::HttpStringUtil::getPara ( const std::string_view & url,
std::string_view & para )
static

获取 URL 中的查询参数字符串(包括 ?)。

Note
可以是完整url 也可以是不太完整的,比如/path?id=123&name=abc
Parameters
urlURL。
para返回参数部分(形如 "?id=123&name=abc")。
Returns
引用,指向结果字符串。

◆ getPort()

static int & stt::data::HttpStringUtil::getPort ( const std::string & url,
int & port )
static

从 URL 中提取端口号。

例如从 http://127.0.0.1:8080/ 中提取 8080。

Warning
必须是完整的url,而且必须显式指定端口 还有路径 就算路径没有 也要写个/
Parameters
url完整 URL。
port存储解析出的端口号。
Returns
引用,指向端口号。

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