-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyRequestHandler.cpp
79 lines (69 loc) · 1.62 KB
/
MyRequestHandler.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
#include <iostream>
#include <conio.h>
#include <string>
#include "MyRequestHandler.h"
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Net/HTTPServer.h>
#include <Poco/Net/HTTPRequestHandlerFactory.h>
#include <Poco/ScopedLock.h>
#include <Poco/URI.h>
#include <Poco/StringTokenizer.h>
using namespace Poco::Net;
string MyServerApp::text = "Hello world!";
Mutex MyServerApp::textLock;
class CMyRequestHandler : public HTTPRequestHandler
{
public:
void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp)
{
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType("text/html");
ostream& out = resp.send();
URI uri(req.getURI());
if (uri.toString().find("/setText") == 0)
{
StringTokenizer str(uri.getQuery(), "=");
if (str.count() == 2 && str[0] == "text")
{
MyServerApp::setText(str[1]);
out << "ok";
out.flush();
return;
}
}
out << "error";
out.flush();
}
};
class MyRequestHandlerFactory : public HTTPRequestHandlerFactory
{
public:
virtual HTTPRequestHandler* createRequestHandler(const HTTPServerRequest &)
{
return new CMyRequestHandler;
}
};
void MyServerApp::setText(string newText)
{
ScopedLock<Mutex> lock(textLock);
text = newText;
}
string MyServerApp::getText()
{
ScopedLock<Mutex> lock(textLock);
return text;
}
int MyServerApp::main(const vector<string> &)
{
HTTPServer s(new MyRequestHandlerFactory, ServerSocket(8000), new HTTPServerParams);
s.start();
for(;;)
{
cout << MyServerApp::getText() << endl;
_getch();
}
s.stop();
return Application::EXIT_OK;
}