-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_conn.h
206 lines (149 loc) · 4.24 KB
/
http_conn.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef HTTPCONNECTION_H
#define HTTPCONNECTION_H
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/uio.h>
#include "locker.h"
#include <hiredis/hiredis.h>
#include <mysql/mysql.h>
struct account_t
{
char* Account;
char* Password;
char* Username;
};
struct zset_t
{
char* set_name;
char* user_name;
};
class http_conn
{
public:
static const int FILENAME_LEN = 200;
static const int READ_BUFFER_SIZE = 2048;
static const int WRITE_BUFFER_SIZE = 1024;
static const int DATABASE_BUFFER_SIZE = 1024;
static const int CONTENT_BUFFER_SIZE = 1024;
enum METHOD { GET = 0, POST, HEAD, PUT, DELETE, TRACE, OPTIONS,
CONNECT, PATHC};
enum CHECK_STATE {CHECK_STATE_REQUESTLINE = 0,
CHECK_STATE_HEADER,
CHECK_STATE_CONTENT};
enum HTTP_CODE { NO_REQUEST, GET_REQUEST, BAD_REQUEST,
NO_RESOURCE, FORBIDDEN_REQUEST, FILE_REQUEST,
INTERNAL_ERROR, CLOSED_CONNECTION,
SUCCESS_LOGIN_REQUEST,BAD_LOGIN_REQUEST
,SUCCESS_SIGNUP_REQUEST, BAD_SIGNUP_REQUEST
,SUCCESS_LIKE_REQUEST, BAD_LIKE_REQUEST
};
enum LINE_STATUS {LINE_OK = 0, LINE_BAD, LINE_OPEN};
public:
void init(int sockfd, const sockaddr_in & addr);
void close_conn(bool read_close = true);
void process();
bool read();
bool write();
private:
void init();
//parse HTTP request
HTTP_CODE process_read();
//fill the HTTP response
bool process_write ( HTTP_CODE ret);
//set of funcs for parse request
HTTP_CODE parse_request_line( char * text);
HTTP_CODE parse_headers(char* text);
HTTP_CODE parse_content(char* text);
HTTP_CODE do_request();
char* get_line() {return m_read_buf + m_start_line;}
LINE_STATUS parse_line();
// api
HTTP_CODE api_login();
HTTP_CODE api_artical();
HTTP_CODE api_signup();
HTTP_CODE api_like(bool increase);
//set of funcs for fill response
void unmap();
bool add_response(const char* format, ...);
bool add_content( const char* content);
bool add_status_line( int status, const char* title);
bool add_headers( int cotent_length);
bool add_content_length(int content_length);
bool add_linger();
bool add_blank_line();
public:
static int m_epollfd;
static int m_user_count;
/*
static redisContext* m_redis_connect;
static MYSQL* m_mysql_connect;
static locker m_redis_lock;
static locker m_mysql_lock;
*/
private:
int m_sockfd;
sockaddr_in m_address;
//read buffer
char m_read_buf [READ_BUFFER_SIZE];
//position of the data hasn't read
int m_read_idx;
//position of the data hasn't analysed;
int m_checked_idx;
//the line in parsing;
int m_start_line;
//write buffer
char m_write_buf[WRITE_BUFFER_SIZE];
//the bytes haven't send
int m_write_idx;
//the state of main state machine
CHECK_STATE m_check_state;
//request method
METHOD m_method;
//the target's path of the client requst
//equels doc_root + m_url
// doc_root is the root of website
char m_real_file [ FILENAME_LEN];
//the request url
char* m_url;
//HTTP version
char* m_version;
//host name
char* m_host;
//http body length
int m_content_length;
//HTTP keepalive
bool m_linger;
//bigfile upload
bool m_bigfile;
//the posioton of target file which has been mmap to memory
char* m_file_address;
//the status of target file
struct stat m_file_stat;
//we use writev to process write
struct iovec m_iv[2];
int m_iv_count;
//redis requst and response
char * m_redis_request;
//mysql requst and response
char* m_mysql_request;
//database response
char m_database_response[DATABASE_BUFFER_SIZE];
//content buffer
char* m_content;
};
#endif