Repository navigation
Expand file tree
/
Copy pathInboundTest.cpp
More file actions
147 lines (129 loc) · 2.93 KB
/
Copy pathInboundTest.cpp
File metadata and controls
147 lines (129 loc) · 2.93 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
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
#include "InboundTest.h"
#include <libutils/Logger.h>
#include <libutils/String.h>
#include <libopi/SysInfo.h>
#include <fcntl.h>
#include <list>
using namespace std;
using namespace Utils;
void TcpServer::ParseRequest(const Utils::Net::SocketPtr& c)
{
this->headers.clear();
size_t r = c->Read(buf, sizeof(buf) );
if( r>0 )
{
buf[r]=0;
list<string> rows = String::Split(buf,"\r\n");
for( const auto& row: rows )
{
if( row.compare(0, 3, "GET") == 0)
{
// remove 'GET ' and split the remainder on ' ' (has a ' HTTP/1.0' at the end)
list<string> request = String::Split(row.substr(4, string::npos), " ",2);
this->url = request.front();
}
list<string> words = String::Split(row, ": ",2);
if( words.size() == 2 )
{
headers[ String::ToLower( words.front() ) ] = words.back();
}
}
}
}
void TcpServer::Redirect( const Utils::Net::SocketPtr& c )
{
ostringstream ss;
string url = this->headers["host"]+this->url;
ss << "HTTP/1.1 307 Temporary Redirect\r\n"
<< "Location: https://"<<url<<"\r\n"
<< "Connection: close\n\n";
c->Write(ss.str().c_str(), ss.str().size());
}
TcpServer::TcpServer(uint16_t port):
Thread(false),
s( OPI::sysinfo.NetworkDevice(), port),
port(port),
dorun(true)
{
int fd = this->s.getSocketFd();
int flags = fcntl( fd, F_GETFD, 0);
if( flags < 0 )
{
throw ErrnoException("Failed to get socket flags");
}
if( fcntl( fd, F_SETFD, flags | FD_CLOEXEC ) != 0 )
{
throw ErrnoException("Failed to update socket fd");
}
}
int TcpServer::Port()
{
return this->port;
}
void TcpServer::Stop()
{
this->dorun = false;
this->Join();
}
void TcpServer::Run()
{
logg << Logger::Debug << "Inbound test server starting at " << this->port << lend;
string msg("{\"connection\" : \"success\"}\n");
while( dorun )
{
s.SetTimeout(1,0);
Utils::Net::SocketPtr c = s.Accept();
if( c )
{
this->ParseRequest(c);
if( this->headers.find("host") != this->headers.end() )
{
this->Redirect(c);
}
else
{
c->Write(msg.c_str(), msg.size());
}
}
}
logg << Logger::Debug << "Test server at " << this->port << " terminating" << lend;
}
InboundTest::InboundTest(const vector<uint16_t> &ports)
{
for(uint16_t port: ports)
{
try
{
logg << Logger::Debug << "Create ibound test server at port "<< port << lend;
TcpServerPtr server(new TcpServer(port) );
servers.push_back( server );
}
catch(runtime_error& err)
{
logg << Logger::Error << "Failed to start server at port " << port << " ("<<err.what()<<")"<<lend;
}
}
}
void InboundTest::Start()
{
for(const TcpServerPtr& server: this->servers)
{
try
{
logg << Logger::Debug << "Start inbound test server at "<< server->Port()<<lend;
server->Start();
}
catch(runtime_error& err)
{
logg << Logger::Error << "Failed to start server "<<err.what()<<lend;
}
}
}
void InboundTest::Stop()
{
for(const auto& server: this->servers)
{
server->Stop();
}
}
InboundTest::~InboundTest() = default;