-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConnection.cpp
More file actions
54 lines (46 loc) · 1.47 KB
/
Connection.cpp
File metadata and controls
54 lines (46 loc) · 1.47 KB
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
#include "Connection.h"
string HttpResp = "HTTP/1.1 200 OK\r\n\r\n";
Connection::Connection(int sockfd) {
this.sockfd = sockfd;
bzero(buffer, 256);
filter_map filters;
filters[onDisableBuild] = new std::regex("\\b(GET /disableBuild?id=)([^\r]+)");
}
Connection::start() {
pthread_t connThread;
int i1;
i1 = pthread_create(&connThread, NULL, Connection::loop(), (void*) "connThread");
}
Connection::loop() {
int n;
while(true) {
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
processMsg(buffer, n);
}
}
Connection::processMsg(char* buf, int len) {
std::smatch match;
for(filter_iterator iter = filter_map.begin(); iter != filter_map.end(); ++iter) {
// function = iterator->first = key
// regex = iterator->second = value
std::regex re = iter->second;
if (std::regex_search(buf, match, iter->second)) {
// The first sub_match is the whole string; the next
// sub_match is the first parenthesized expression.
if (match.size() == 0) continue;
string args[] = new string[match.size()];
for(int i = 0; i < match.size(); ++i) {
args[i] = match[i].str();
}
iterator->first(match.size(), args);
break;
}
}
}
Connection::write(char* buffer, int length) {
write(sockfd, buffer, length);
}
Connection::~Connection() {
close(sockfd);
}