-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.cpp
264 lines (227 loc) · 7.08 KB
/
main.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <stdio.h>
#include "WebServer.h"
#include <unistd.h>
#include <curl/curl.h>
#include <string>
#include <string.h>
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <netinet/tcp.h>
#include "stdlib.h"
#include <sys/time.h>
#include <errno.h>
using namespace Dumais::WebServer;
std::string lastURL;
class Test: public IWebServerListener
{
public:
virtual HTTPResponse* processHTTPRequest(HTTPRequest* request)
{
lastURL = request->getURL();
printf("%s [%s]\r\n",request->getMethod().c_str(),lastURL.c_str());
return HTTPProtocol::buildBufferedResponse(HTTPResponseCode::OK,request->getURL(),"");
}
virtual void onConnectionOpen()
{
printf("Connection open\r\n");
}
virtual void onConnectionClosed()
{
printf("Connection closed\r\n");
}
};
char *curlbuf = 0;
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
size_t s = size*nmemb;
if (curlbuf) delete curlbuf;
curlbuf = new char[s+1];
memcpy(curlbuf,ptr,s);
curlbuf[s] = 0;
return s;
}
long query(std::string url, bool auth=false)
{
CURL *curl = curl_easy_init();
curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
if (auth)
{
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_USERPWD, "pat:pat");
}
CURLcode code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
printf("http code: %i %i\r\n",http_code, code);
return http_code;
}
long post(std::string url)
{
CURL *curl = curl_easy_init();
curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,"test=1&test2=2");
curl_easy_setopt(curl, CURLOPT_COOKIE, "meow=cat; woof=dog;");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
CURLcode code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
return http_code;
}
int getURL(std::string server, int port)
{
std::string ret="";
bool chunked = false;
sockaddr_in sockadd;
int sock=socket(AF_INET,SOCK_STREAM,0);
memset(&sockadd,0,sizeof(sockadd));
struct hostent *he;
he = gethostbyname(server.c_str());
sockadd.sin_family = AF_INET;
sockadd.sin_port = htons(port);
memcpy(&sockadd.sin_addr, he->h_addr_list[0], he->h_length);
bool content = false;
std::string st;
if (connect(sock,(struct sockaddr *)&sockadd, sizeof(sockadd)) >= 0)
{
return sock;
}
return 0;
}
#define ASSERT(f) if (f) printf("%i: OK\r\n",__LINE__); else printf("%i: ------- > FAIL\r\n",__LINE__);
int main(int argc, char** argv)
{
Test test;
char rcv[1024];
bool started;
std::string ret;
#ifdef USING_OPENSSL
WebServer webs(5556,"0.0.0.0",10);
webs.setListener(&test);
started = webs.startSecure("cert.crt","key.key");
ASSERT(started);
if (!started) return -1;
// test ssl
ASSERT(query("https://127.0.0.1:5556/test/123")==200);
if (curlbuf !=0) ret = curlbuf;
ASSERT(ret == "/test/123");
webs.stop();
#endif
WebServer web(5555,"0.0.0.0",100);
web.setListener(&test);
started = web.start();
ASSERT(started);
if (!started) return -1;
// test post
ASSERT(post("http://127.0.0.1:5555/test")==200);
ret = curlbuf;
ASSERT(ret == "/test");
// test get and echoing url no auth
ASSERT(query("http://127.0.0.1:5555/test/123")==200);
ret = curlbuf;
ASSERT(ret == "/test/123");
// test sending in 10bytes packets increments (fragmented)
{
std::stringstream ss;
ss << "GET /test242 HTTP/1.1\r\n";
ss << "Host: 127.0.0.1\r\n";
ss << "Connection: close\r\n";
ss << "Content-length: 3\r\n";
ss << "\r\nabc";
const char* buf = ss.str().c_str();
int i = 0;
int sock = getURL("127.0.0.1",5555);
while (i < ss.str().size())
{
int s = ss.str().size()-i;
if (s > 10) s = 10;
send(sock,(char*)&buf[i],s,0);
i+=10;
}
recv(sock,(char*)&rcv[0],1,0); // just receive 1 byte, we will ignore the response
close(sock);
ASSERT(lastURL=="/test242");
}
// test 2 messages in one packet
{
std::stringstream ss;
ss << "GET /test243 HTTP/1.1\r\n";
ss << "Host: 127.0.0.1\r\n";
ss << "Connection: close\r\n";
ss << "Content-length: 3\r\n";
ss << "\r\nabc";
ss << "GET /test244 HTTP/1.1\r\n";
ss << "Host: 127.0.0.1\r\n";
ss << "Connection: close\r\n";
ss << "Content-length: 3\r\n";
ss << "\r\nabc";
const char* buf = ss.str().c_str();
int sock = getURL("127.0.0.1",5555);
send(sock,ss.str().c_str(),ss.str().size(),0);
recv(sock,(char*)&rcv[0],1,0); // just receive 1 byte, we will ignore the response
close(sock);
ASSERT(lastURL=="/test244");
}
// test that chunked transfers are refused
{
std::stringstream ss;
ss << "GET /test243 HTTP/1.1\r\n";
ss << "Host: 127.0.0.1\r\n";
ss << "Connection: close\r\n";
ss << "Transfer-Encoding: chunked\r\n";
ss << "\r\nabc";
const char* buf = ss.str().c_str();
int sock = getURL("127.0.0.1",5555);
send(sock,ss.str().c_str(),ss.str().size(),0);
recv(sock,(char*)&rcv[0],12,0); // just receive 1 byte, we will ignore the response
rcv[12]=0;
ASSERT(!strcmp((char*)&rcv[9],"406"));
}
// test 100 connections at the same time
{
std::stringstream ss;
ss << "GET /test/100 HTTP/1.1\r\n";
ss << "Host: 127.0.0.1\r\n";
ss << "Connection: close\r\n";
ss << "Content-length: 3\r\n";
ss << "\r\nabc";
int sockets[100];
for (int i = 0; i < 100; i++) sockets[i] = getURL("127.0.0.1",5555);
for (int i = 0; i < 100; i++) send(sockets[i],ss.str().c_str(),ss.str().size(),0);
for (int i = 0; i < 100; i++)
{
recv(sockets[i],(char*)&rcv[0],12,0); // just receive 1 byte, we will ignore the response
rcv[12]=0;
ASSERT(!strcmp((char*)&rcv[9],"200"));
}
}
// test sending invalid data
{
std::stringstream ss;
for (int i=0;i<200;i++) ss << "GET asdfsdfsfdfs HTTP/1.1 dsfkjnasdfnsmdnfsmndfmn.,sdfasdf";
const char* buf = ss.str().c_str();
int sock = getURL("127.0.0.1",5555);
send(sock,ss.str().c_str(),ss.str().size(),0);
recv(sock,(char*)&rcv[0],12,0); // just receive 1 byte, we will ignore the response
close(sock);
rcv[12]=0;
ASSERT(!strcmp((char*)&rcv[9],"413"));
}
// test with auth, bad password
web.requireAuth("passwd",30);
ASSERT(query("http://127.0.0.1:5555/test/123")==401);
// test with auth, good password
ASSERT(query("http://127.0.0.1:5555/test/123",true)==200);
ret = curlbuf;
ASSERT(ret == "/test/123");
web.stop();
}