Debian Codegraph @

third-party/libhv-v1.3.3/docs/cn/HttpContext.md

raw
1 ```c++
2
3 class HttpContext {
4
5 /* 获取请求信息 */
6 // 获取客户端IP
7 std::string ip();
8 // 获取客户端端口
9 int port();
10 // 获取请求method
11 http_method method();
12 // 获取请求url
13 std::string url();
14 // 获取请求path
15 std::string path();
16 // 获取请求host
17 std::string host();
18 // 获取请求头部
19 const http_headers& headers();
20 std::string header(const char* key, const std::string& defvalue = hv::empty_string);
21 // 获取请求参数
22 const hv::QueryParams& params();
23 std::string param(const char* key, const std::string& defvalue = hv::empty_string);
24 // 获取请求cookie
25 const HttpCookie& cookie(const char* name);
26 // 获取请求 `Content-Length`
27 int length();
28 // 获取请求 `Content-Type`
29 http_content_type type();
30 // 判断请求 `Content-Type`
31 bool is(http_content_type content_type);
32 // 获取请求body
33 std::string& body();
34 // 获取 `application/json` 格式数据
35 const hv::Json& json();
36 // 获取 `multipart/form-data` 格式数据
37 const hv::MultiPart& form();
38 std::string form(const char* name, const std::string& defvalue = hv::empty_string);
39 // 获取 `application/x-www-urlencoded` 格式数据
40 const hv::KeyValue& urlencoded();
41 std::string urlencoded(const char* key, const std::string& defvalue = hv::empty_string);
42 // 根据 `Content-Type` 获取对应格式数据
43 template<typename T>
44 T get(const char* key, T defvalue = 0);
45 std::string get(const char* key, const std::string& defvalue = hv::empty_string);
46
47 /* 设置响应信息 */
48 // 设置响应状态码
49 void setStatus(http_status status);
50 // 设置响应 `Content-Type`
51 void setContentType(http_content_type type);
52 // 设置响应头部
53 void setHeader(const char* key, const std::string& value);
54 // 设置响应cookie
55 void setCookie(const HttpCookie& cookie);
56 // 设置响应body
57 void setBody(const std::string& body);
58 template<typename T>
59 // 根据 `Content-Type` 设置对应格式数据
60 void set(const char* key, const T& value);
61
62 // 发送
63 int send();
64 int send(const std::string& str, http_content_type type = APPLICATION_JSON);
65 // 发送文本数据
66 int sendString(const std::string& str);
67 // 发送二进制数据
68 int sendData(void* data, int len, bool nocopy = true);
69 // 发送文件
70 int sendFile(const char* filepath);
71 // 发送json数据
72 template<typename T>
73 int sendJson(const T& t);
74
75 // 重定向
76 int redirect(const std::string& location, http_status status = HTTP_STATUS_FOUND);
77
78 // 主动关闭连接
79 int close();
80
81 };
82
83 ```