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
|
#include <Main.h>
#include <HttpServer.h>
#include <TaskMan.h>
#define DAEMON_NAME "Balau/1.0"
using namespace Balau;
class TestAction : public HttpServer::Action {
public:
TestAction() : Action(Regexes::any) { }
virtual bool Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO<Handle> out) throw (GeneralException);
};
bool TestAction::Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO<Handle> out) throw (GeneralException) {
HttpServer::Response response(server, req, out);
response->writeString(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
" <head>\n"
" <title>Test</title>\n"
" </head>\n"
"\n"
" <body>\n"
" This is a test document.\n"
" </body>\n"
"</html>\n");
response.Flush();
return true;
}
Balau::Regex testFailureURL("^/failure.html$");
class TestFailure : public HttpServer::Action {
public:
TestFailure() : Action(testFailureURL) { }
virtual bool Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO<Handle> out) throw (GeneralException);
};
bool TestFailure::Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO<Handle> out) throw (GeneralException) {
throw GeneralException("Test...");
}
#define NTHREADS 4
void MainTask::Do() {
Printer::enable(M_DEBUG);
Printer::log(M_STATUS, "Test::Http running.");
Thread * tms[NTHREADS];
for (int i = 0; i < NTHREADS; i++)
tms[i] = TaskMan::createThreadedTaskMan();
HttpServer * s = new HttpServer();
HttpServer::Action * a = new TestAction();
HttpServer::Action * f = new TestFailure();
a->registerMe(s);
f->registerMe(s);
s->setPort(8080);
s->setLocal("localhost");
s->start();
yield();
s->stop();
for (int i = 0; i < NTHREADS; i++)
tms[i]->join();
Printer::log(M_STATUS, "Test::Http passed.");
}
|