STTNet
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 宏定义 
静态 Public 成员函数 | 所有成员列表
stt::data::HttpStringUtil类 参考

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

#include <sttnet.h>

静态 Public 成员函数

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 请求头字段。 更多...
 

详细描述

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

成员函数说明

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

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

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

参数
first第一个字段名。
second第一个字段值。
返回
构造的单条 HTTP 请求头字符串。
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");

最终生成:

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

参数
ori_str原始 URL。
str返回 path 部分。
返回
引用,指向结果字符串。
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

参数
ori_str原始 URL。
str返回 path 部分。
返回
引用,指向结果字符串。
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找不到,则默认到尾

参数
ori_str原始字符串。
str存储提取结果的字符串。
a起始标记字符串。
b终止标记字符串。
pos搜索起始位置。
返回
返回b在ori_str中的位置(可能返回string::npos,如果b找不到或者b为"")
注解
如果找不到,结果字符串为""
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找不到,则默认到尾

参数
ori_str原始字符串。
str存储提取结果的字符串。
a起始标记字符串。
b终止标记字符串。
pos搜索起始位置。
返回
返回b在ori_str中的位置(可能返回string::npos,如果b找不到或者b为"")
注解
如果找不到,结果字符串为""
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 请求头中提取指定字段的值。

参数
ori_str原始 HTTP 请求头字符串。
str提取结果。
name请求头字段名(如 "Host")。
返回
引用,指向结果字符串。
static std::string& stt::data::HttpStringUtil::get_value_header ( const std::string &  ori_str,
std::string &  str,
const std::string &  name 
)
static

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

参数
ori_str原始 HTTP 请求头字符串。
str提取结果。
name请求头字段名(如 "Host")。
返回
引用,指向结果字符串。
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 的值。

注解
url不需要完整也行 例如从 ?id=123&name=abc提取 id 的值和从http://xxxx/?id=123&name=abc提取 id 的值是一样的。
参数
ori_str原始 URL 字符串。
str存储提取结果的字符串。
name参数名(key)。
返回
引用,指向结果字符串。
static std::string& stt::data::HttpStringUtil::get_value_str ( const std::string &  ori_str,
std::string &  str,
const std::string &  name 
)
static

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

注解
url不需要完整也行 例如从 ?id=123&name=abc提取 id 的值和从http://xxxx/?id=123&name=abc提取 id 的值是一样的。
参数
ori_str原始 URL 字符串。
str存储提取结果的字符串。
name参数名(key)。
返回
引用,指向结果字符串。
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

警告
url必须完整而且显式指明端口
参数
url完整 URL。
IP存储提取的 IP 或域名。
返回
引用,指向结果字符串。
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 之后的所有内容(如参数)。

参数
URL。
locPara返回 path+参数部分。
返回
引用,指向结果字符串。
static std::string& stt::data::HttpStringUtil::getLocPara ( const std::string &  url,
std::string &  locPara 
)
static

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

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

参数
URL。
locPara返回 path+参数部分。
返回
引用,指向结果字符串。
static std::string_view& stt::data::HttpStringUtil::getPara ( const std::string_view &  url,
std::string_view &  para 
)
static

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

注解
可以是完整url 也可以是不太完整的,比如/path?id=123&name=abc
参数
URL。
para返回参数部分(形如 "?id=123&name=abc")。
返回
引用,指向结果字符串。
static std::string& stt::data::HttpStringUtil::getPara ( const std::string &  url,
std::string &  para 
)
static

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

注解
可以是完整url 也可以是不太完整的,比如/path?id=123&name=abc
参数
URL。
para返回参数部分(形如 "?id=123&name=abc")。
返回
引用,指向结果字符串。
static int& stt::data::HttpStringUtil::getPort ( const std::string &  url,
int &  port 
)
static

从 URL 中提取端口号。

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

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

该类的文档由以下文件生成: