-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.h
160 lines (118 loc) · 3.56 KB
/
handler.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
//
// Created by root on 08.01.16.
//
#ifndef KIMBERLY_HANDLER_H
#define KIMBERLY_HANDLER_H
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "util.h"
#include "proxy_server.h"
#include "buffer.h"
#include "file_descriptor.h"
#include <sys/signalfd.h>
#include <signal.h>
class handler {
friend class proxy_server;
friend class client_handler;
friend class hostname_resolver;
protected:
file_descriptor fd;
proxy_server *serv;
public:
handler(int fd, proxy_server *serv) : fd(fd), serv(serv) { }
handler(proxy_server *serv) : serv(serv) { }
virtual ~handler() { }
virtual void handle(const epoll_event &) = 0;
virtual void disconnect() const {
Log::d("disconnecting fd(" + inttostr(fd.get_fd()) + ")");
serv->remove_handler(this);
Log::d("disconnected successful");
}
};
class notifier : public handler {
public:
//TODO: take it's bodies out in .cpp file
file_descriptor write_pipe;
notifier(proxy_server *serv) : handler(serv) {
int pipefds[2] = {};
pipe(pipefds);
fd.set_fd(pipefds[0]);
write_pipe.set_fd(pipefds[1]);
Log::d("write_pipe is " + inttostr(write_pipe.get_fd()) + ", read_pipe is " + inttostr(fd.get_fd()));
// make read-end non-blocking
setnonblocking(fd.get_fd());
}
virtual ~notifier() { }
void handle(const epoll_event &) {
char ch;
Log::d("reading from fd " + inttostr(fd.get_fd()));
read(fd.get_fd(), &ch, 1);
}
void notify() {
Log::d("pre-notifying");
char ch = 'x';
write(write_pipe.get_fd(), &ch, 1);
Log::d("successful notifying");
}
void disconnect() const {
handler::disconnect();
}
private:
};
class client_handler : public handler {
friend class proxy_server;
friend class hostname_resolver;
public:
client_handler(int sock, proxy_server *serv) : handler(sock, serv) {
message_len = -1;
message_type = NOT_EVALUATED;
}
virtual ~client_handler() { }
void handle(const epoll_event &);
private:
buffer input_buffer;
buffer output_buffer;
int message_len;
enum {
NOT_EVALUATED, WITHOUT_BODY, VIA_CONTENT_LENGTH, VIA_TRANSFER_ENCODING, PRE_HTTPS_MODE, HTTPS_MODE
} message_type;
handler *clrh = 0;
void resolve_host_ip(std::string, uint flags);
void disconnect() const {
if (clrh) {
clrh->disconnect();
}
handler::disconnect();
}
// retunrs true if all the message was read
bool read_message(handler *h, buffer &buf);
class client_request_handler : public handler {
friend class proxy_server;
public:
client_request_handler(int sock, proxy_server *serv, std::shared_ptr<client_handler> clh) : handler(sock,
serv) {
this->clh = clh;
clh->clrh = this;
}
~client_request_handler() {
clh->clrh = 0;
}
void handle(const epoll_event &);
void disconnect() const {
clh->clrh = 0;
clh->disconnect();
handler::disconnect();
}
private:
std::shared_ptr<client_handler> clh;
};
};
class server_handler : public handler {
friend class proxy_server;
public:
server_handler(int sock, proxy_server *serv) : handler(sock, serv) { }
virtual ~server_handler() { }
void handle(const epoll_event &);
};
#endif //KIMBERLY_HANDLER_H