-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequestData.h
104 lines (88 loc) · 2.27 KB
/
requestData.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Created by Jason.Z on 2020/6/25.
//
#ifndef SOCKETSERVERCPLUS_REQUESTDATA_H
#define SOCKETSERVERCPLUS_REQUESTDATA_H
#include <string>
#include <unordered_map>
#include <memory>
#include <pthread.h>
const int STATE_PARSE_URI = 1;
const int STATE_PARSE_HEADERS = 2;
const int STATE_RECV_BODY = 3;
const int STATE_ANALYSIS = 4;
const int STATE_FINISH = 5;
const int MAX_BUFF = 4096;
// 有请求出现但是读不到数据,可能是Request Aborted,
// 或者来自网络的数据没有达到等原因,
// 对这样的请求尝试超过一定的次数就抛弃
const int AGAIN_MAX_TIMES = 200;
const int PARSE_URI_AGAIN = -1;
const int PARSE_URI_ERROR = -2;
const int PARSE_URI_SUCCESS = 0;
const int PARSE_HEADER_AGAIN = -1;
const int PARSE_HEADER_ERROR = -2;
const int PARSE_HEADER_SUCCESS = 0;
const int ANALYSIS_ERROR = -2;
const int ANALYSIS_SUCCESS = 0;
const int METHOD_POST = 1;
const int METHOD_GET = 2;
const int HTTP_10 = 1;
const int HTTP_11 = 2;
const int EPOLL_WAIT_TIME = 500;
class MimeType{
private:
static void init();
static std::unordered_map<std::string, std::string> mime;
MimeType();
MimeType(const MimeType &m);
pthread_once_t once_control;
public:
static std::string getMime(const std::string &suffix);
};
enum HeadersState
{
h_start = 0,
h_key,
h_colon,
h_spaces_after_colon,
h_value,
h_CR,
h_LF,
h_end_CR,
h_end_LF
};
class RequestData: public std::enable_shared_from_this<RequestData>{
private:
int againTime;
std::string path;
int fd;
int epollfd;
// content的内容用完就清
std::string content;
int method;
int HTTPversion;
std::string file_name;
int now_read_pos;
int state;
bool is_fin;
bool keep_alive;
std::unordered_map<std::string, std::string> headers;
// std::weak_ptr<>
private:
int parse_URI();
int parse_Headers();
int analysisRequest();
public:
RequestData();
RequestData(int _epollfd, int _fd, std::string _path);
~RequestData();
// void linkTimer(std::shared_ptr<TimerNode> mtimer);
void reset();
// void seperateTimer();
int getFd();
void setFd(int _fd);
void handleRequest();
void handleError(int fd, int err_num, std::string short_msg);
};
#endif //SOCKETSERVERCPLUS_REQUESTDATA_H