-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHttpData.h
134 lines (116 loc) · 2.66 KB
/
HttpData.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*************************************************************************
> File Name: HttpData.h
> Author: zhangfeng
> Mail: [email protected]
> Created Time: Fri 04 Oct 2019 02:20:58 PM CST
> Target:
************************************************************************/
#ifndef _HTTPDATA_H
#define _HTTPDATA_H
#include "Timer.h"
#include <string>
#include <unordered_map>
#include <map>
#include <memory>
#include <sys/epoll.h>
#include <functional>
#include <unistd.h>
class EventLoop;
class TimerNode;
class Channel;
enum ProcessState {
STATE_PARSE_URI = 1,
STATE_PARSE_HEADERS,
STATE_RECV_BODY,
STATE_ANALYSIS,
STATE_FINISH
};
enum URIState {
PARSE_URI_AGAIN = 1,
PARSE_URI_ERROR,
PARSE_URI_SUCCESS
};
enum HeaderState {
PARSE_HEADER_SUCCESS = 1,
PARSE_HEADER_AGAIN,
PARSE_HEADER_ERROR
};
enum AnalysisState {
ANALYSIS_SUCCESS = 1,
ANALYSIS_ERROR
};
enum ParseState {
H_START = 0,
H_KEY,
H_COLON,
H_SPACES_AFTER_COLON,
H_VALUE,
H_CR,
H_LF,
H_END_CR,
H_END_LF
};
enum ConnectionState {
H_CONNECTED = 0,
H_DISCONNECTING,
H_DISCONNECTED
};
enum HttpMethod {
METHOD_POST = 1,
METHOD_GET,
METHOD_HEAD
};
enum HttpVersion {
HTTP_10 = 1,
HTTP_11
};
class MimeType {
public:
static std::string getMime(const std::string &suffix);
private:
static void init();
static std::unordered_map<std::string, std::string> mime;
MimeType();
MimeType(const MimeType &m);
static pthread_once_t once_control;
};
class HttpData: public std::enable_shared_from_this<HttpData> {
public:
HttpData(EventLoop *loop, int connfd);
~HttpData( ) { close(fd_); }
void reset();
void seperateTimer();
void linkTimer(std::shared_ptr<TimerNode> mtimer) {
timer_ = mtimer;
}
std::shared_ptr<Channel> getChannel() { return channel_; }
EventLoop *getLoop() {return loop_;}
void handleClose();
void newEvent();
private:
EventLoop *loop_;
std::shared_ptr<Channel> channel_;
int fd_;
std::string inBuffer_;
std::string outBuffer_;
bool error_;
ConnectionState connectionState_;
HttpMethod method_;
HttpVersion HTTPVersion_;
std::string fileName_;
std::string path_;
int nowReadPos_;
ProcessState state_;
ParseState hState_;
bool keepAlive_;
std::map<std::string, std::string> headers_;
std::weak_ptr<TimerNode> timer_;
void handleRead();
void handleWrite();
void handleConn();
void handleError(int fd, int err_num, std::string short_msg);
URIState parseURI();
HeaderState parseHeaders();
AnalysisState analysisRequest();
};
#endif