-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequest.h
75 lines (71 loc) · 1.87 KB
/
HttpRequest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include "Buffer.h"
#include <stdbool.h>
#include "HttpResponse.h"
#include <map>
using namespace std;
// 当前的解析状态
enum class PrecessState:char
{
ParseReqLine,
ParseReqHeaders,
ParseReqBody,
ParseReqDone
};
// 定义http请求结构体
class HttpRequest
{
public:
HttpRequest();
~HttpRequest();
// 重置
void reset();
// 添加请求头
void addHeader(const string key, const string value);
// 根据key得到请求头的value
string getHeader(const string key);
// 解析请求行
bool parseRequestLine(Buffer* readBuf);
// 解析请求头
bool parseRequestHeader(Buffer* readBuf);
// 解析http请求协议
bool parseHttpRequest(Buffer* readBuf, HttpResponse* response, Buffer* sendBuf, int socket);
// 处理http请求协议
bool processHttpRequest(HttpResponse* response);
// 解码字符串
string decodeMsg(string from);
const string getFileType(const string name);
static void sendDir(string dirName, Buffer* sendBuf, int cfd);
static void sendFile(string dirName, Buffer* sendBuf, int cfd);
inline void setMethod(string method)
{
m_method = method;
}
inline void seturl(string url)
{
m_url = url;
}
inline void setVersion(string version)
{
m_version = version;
}
// 获取处理状态
inline PrecessState getState()
{
return m_curState;
}
inline void setState(PrecessState state)
{
m_curState = state;
}
private:
char* splitRequestLine(const char* start, const char* end,
const char* sub, function<void(string)> callback);
int hexToDec(char c);
private:
string m_method;
string m_url;
string m_version;
map<string, string> m_reqHeaders;
PrecessState m_curState;
};