Skip to content

Commit 668fd17

Browse files
committed
bugfixes
1 parent 8245ad9 commit 668fd17

17 files changed

+721
-73
lines changed

TODO.txt

-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ TODO2:
44
15) static struct;
55
5) static_assert?
66
11) -rdynamic?
7-
16) = default
87
17) public, virtual in inheritance?
98

109
9) http cashing (including partial GET methods cashing)
1110
3) rewrite Log::print using variadic templates
1211

13-
15) does I really need global_socket_mutex?
14-
1512
19) to update lenovo yoga keyboard driver

buffer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void buffer::set(const char *const data) {
4848
}
4949

5050
bool buffer::empty() {
51-
return data.empty();
51+
return length() == 0;
5252
}
5353

5454
int buffer::length() {

buffer.o

142 KB
Binary file not shown.

file_descriptor.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99

1010
class file_descriptor {
1111
public:
12-
file_descriptor(const file_descriptor&) = delete;
12+
static int const INVALID_FD = -1;
1313

14+
file_descriptor(const file_descriptor&) = delete;
15+
file_descriptor(file_descriptor&& other)
16+
: fd(other.fd)
17+
{
18+
other.fd = INVALID_FD;
19+
}
1420
file_descriptor(int fd) : fd(fd) { }
1521

16-
file_descriptor() : fd(0) { }
22+
file_descriptor() : fd(INVALID_FD) { }
1723

1824
/*file_descriptor(file_descriptor &&rhs) {
1925
fd = rhs.fd;
@@ -22,7 +28,7 @@ class file_descriptor {
2228

2329
~file_descriptor() {
2430
Log::d("Closing fd(" + inttostr(fd) + ")");
25-
if (fd)
31+
if (fd != INVALID_FD)
2632
close(fd);
2733
Log::d("closed");
2834
}

handler.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ bool client_handler::read_message(handler *h, buffer &buf) {
6363

6464
if (message_type == VIA_TRANSFER_ENCODING) {
6565
while (buf.length() > message_len) {
66-
Log::d("kek is " + inttostr((int) buf.string_data()[message_len - 1]) + " " +
66+
/*Log::d("kek is " + inttostr((int) buf.string_data()[message_len - 1]) + " " +
6767
inttostr((int) buf.string_data()[message_len]) + " " +
68-
inttostr((int) buf.string_data()[message_len + 1]));
68+
inttostr((int) buf.string_data()[message_len + 1]));*/
6969
if (buf.string_data()[message_len] == '0') {
7070
return true;
7171
}
@@ -74,6 +74,8 @@ bool client_handler::read_message(handler *h, buffer &buf) {
7474
std::string chunklen = buf.string_data().substr((size_t) message_len, linebreak - message_len);
7575
Log::d("Next chunk len is " + chunklen);
7676
message_len += chunklen.length() + hextoint(chunklen) + 4;
77+
} else {
78+
break;
7779
}
7880
}
7981
return false;
@@ -155,13 +157,11 @@ void client_handler::resolve_host_ip(std::string hostname, uint flags) {
155157
}
156158

157159
void client_handler::client_request_handler::handle(const epoll_event &e) {
158-
if (!deleteme) {
160+
if (!(e.events & EPOLLOUT) || (e.events & EPOLLIN) || !clh->input_buffer.empty()) {
159161
Log::d("Client request handler: " + eetostr(e) + ", inputbuffer: " +
160162
(clh->input_buffer.empty() ? "empty" : "not empty"));
161-
deleteme = true;
162163
}
163164
if (e.events & EPOLLOUT) {
164-
if (!clh->input_buffer.empty()) deleteme = false;
165165
if (serv->write_chunk(this, clh->input_buffer) && clh->message_type != HTTPS_MODE) {
166166
Log::d("Finished resending query to host");
167167
serv->modify_handler(this, EPOLLIN);
@@ -173,7 +173,6 @@ void client_handler::client_request_handler::handle(const epoll_event &e) {
173173
}
174174

175175
if (e.events & EPOLLIN) {
176-
deleteme = false;
177176
if (clh->read_message(this, clh->output_buffer) && clh->message_type != HTTPS_MODE) {
178177
Log::d("It seems that all message was received.");
179178
serv->modify_handler(clh.get(), EPOLLOUT);

handler.h

-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ class client_handler : public handler {
142142

143143
private:
144144
std::shared_ptr<client_handler> clh;
145-
146-
bool deleteme = false;
147145
};
148146
};
149147

handler.o

549 KB
Binary file not shown.

hostname_resolver.h

+30-26
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,34 @@
1212

1313
class hostname_resolver {
1414
public:
15-
hostname_resolver() {}
15+
~hostname_resolver() {
16+
Log::d("releasing waiters");
17+
queue.release_waiters();
18+
19+
for (auto &w : workers) {
20+
w->join();
21+
}
22+
}
1623

1724
hostname_resolver(int threads) {
1825
for (int i = 0; i < threads; i++) {
19-
workers.push_back(std::make_shared<worker>(this));
26+
workers.push_back(std::make_unique<worker>(this));
2027
}
2128
}
2229

23-
void add_task(std::string hostname, std::function<void(my_addrinfo *)> task, std::function<void()> on_fail) {
30+
void add_task(std::string hostname, std::function<void(std::shared_ptr<my_addrinfo>)> task, std::function<void()> on_fail) {
2431
queue.push(resolver_task(hostname, task, on_fail));
2532
}
2633

27-
void terminate() {
28-
Log::d("releasing waiters");
29-
queue.release_waiters();
30-
31-
for (auto &w : workers) {
32-
w->join();
33-
}
34-
}
35-
3634
private:
3735
struct resolver_task {
3836
resolver_task() { }
3937

40-
resolver_task(std::string hostname, std::function<void(my_addrinfo *)> task, std::function<void()> on_fail)
38+
resolver_task(std::string hostname, std::function<void(std::shared_ptr<my_addrinfo>)> task, std::function<void()> on_fail)
4139
: hostname(hostname), task(task), on_fail(on_fail) { }
4240

4341
std::string hostname;
44-
std::function<void(my_addrinfo *)> task;
42+
std::function<void(std::shared_ptr<my_addrinfo>)> task;
4543
std::function<void()> on_fail;
4644
};
4745

@@ -50,20 +48,20 @@ class hostname_resolver {
5048
//worker &operator=(const worker &) = delete;
5149

5250
hostname_resolver *resolver;
53-
std::shared_ptr<std::thread> thread;
51+
std::unique_ptr<std::thread> thread;
5452

5553
public:
5654

5755
worker(hostname_resolver *_resolver) : resolver(_resolver) {
58-
thread = std::make_shared<std::thread>((std::function<void()>) ([this] {
56+
thread = std::make_unique<std::thread>((std::function<void()>) ([this] {
5957
Log::d("RESOLVER: resolver thread id: " + inttostr((int) pthread_self()));
6058
while (1) {
6159
resolver_task task;
6260
if (!resolver->queue.pop(task)) {
6361
return;
6462
}
65-
my_addrinfo *addrinfo;
66-
if (resolve_hostname(task.hostname, &addrinfo)) {
63+
std::shared_ptr<my_addrinfo> addrinfo;
64+
if (resolve_hostname(task.hostname, addrinfo)) {
6765
task.task(addrinfo);
6866
} else {
6967
task.on_fail();
@@ -83,7 +81,7 @@ class hostname_resolver {
8381
thread->join();
8482
}
8583

86-
bool resolve_hostname(std::string hostname, my_addrinfo **result) {
84+
bool resolve_hostname(std::string hostname, std::shared_ptr<my_addrinfo> &result) {
8785
Log::d("RESOLVER: \tResolving hostname \"" + hostname + "\"");
8886
uint16_t port = 80;
8987

@@ -95,10 +93,10 @@ class hostname_resolver {
9593

9694
Log::d("RESOLVER: \thostname is " + new_hostname + ", port is " + inttostr(port));
9795

98-
auto it = resolver->cashed_hostnames.find(new_hostname);
96+
auto it = resolver->cashed_hostnames.find(hostname);
9997
if (it != resolver->cashed_hostnames.end()) {
10098
Log::d("RESOLVER: taking " + new_hostname + " from cash");
101-
*result = &it->second;
99+
result = it->second;
102100
return true;
103101
}
104102

@@ -110,29 +108,35 @@ class hostname_resolver {
110108
hints.ai_socktype = SOCK_STREAM;
111109

112110
if (getaddrinfo(new_hostname.c_str(), inttostr(port).c_str(), &hints, &_servinfo) != 0) {
111+
Log::e("RESOLVER: getaddrinfo error");
112+
perror("getaddrinfo");
113113
return false;
114114
}
115115

116116
std::unique_lock<std::mutex> lock(resolver->cashing_mutex);
117-
it = resolver->cashed_hostnames.find(new_hostname);
117+
it = resolver->cashed_hostnames.find(hostname);
118118
if (it == resolver->cashed_hostnames.end()) {
119-
resolver->cashed_hostnames.insert(std::make_pair(new_hostname, my_addrinfo(_servinfo)));
119+
Log::d("RESOLVER: ok, not found in cash");
120+
result = std::make_shared<my_addrinfo>(_servinfo);
121+
resolver->cashed_hostnames.insert(std::make_pair(hostname, result));
120122
} else {
123+
Log::d("RESOLVER: suddenly found in cash");
121124
freeaddrinfo(_servinfo);
125+
result = it->second;
122126
}
123-
*result = &it->second;
127+
Log::d(std::string("RESOLVER: result->ptr is ") + (result->get_info() == nullptr ? "nullptr?!" : "not nullptr"));
124128
//*result = new my_addrinfo(_servinfo);
125129
return true;
126130
}
127131
};
128132

129133
concurrent_queue<resolver_task> queue;
130134

131-
std::map<std::string, my_addrinfo> cashed_hostnames;
135+
std::map<std::string, std::shared_ptr<my_addrinfo>> cashed_hostnames;
132136
std::mutex cashing_mutex;
133137

134138
//just for cleaning memory up
135-
std::vector<std::shared_ptr<worker>> workers;
139+
std::vector<std::unique_ptr<worker>> workers;
136140
};
137141

138142
#endif //KIMBERLY_HOSTNAME_RESOLVER_H

main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ std::shared_ptr<proxy_server> s;
66
bool term = false;
77

88
void signal_handler(int signum) {
9-
std::cout << "[][][][][][]cout error code " << signum << std::endl;
9+
//std::cout << "[][][][][][]cout error code " << signum << std::endl;
1010
Log::d("Error code: " + inttostr(signum));
1111
Log::print_stack_trace(6);
1212
if (term) {

main.o

731 KB
Binary file not shown.

my_addrinfo.h

+3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ class my_addrinfo {
1111
struct addrinfo *info;
1212

1313
public:
14+
my_addrinfo(const my_addrinfo &rhs) = delete;
15+
1416
my_addrinfo() {
1517
info = nullptr;
1618
}
1719

1820
my_addrinfo(my_addrinfo &&rhs) {
21+
Log::d("my_addrinfo::move");
1922
info = rhs.info;
2023
rhs.info = nullptr;
2124
}

0 commit comments

Comments
 (0)