-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
141 lines (103 loc) · 3.88 KB
/
server.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
#include <Poco/Net/ServerSocket.h>
#include <Poco/Net/HTTPServer.h>
#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/Net/HTTPRequestHandlerFactory.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Util/ServerApplication.h>
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
using namespace Poco::Net;
using namespace Poco::Util;
using namespace std;
bool is_numeric( char *number_string){
string temp_number_string(number_string);
return (temp_number_string.find_first_not_of( "0123456789" ) == string::npos);
}
class MyGetRequestHandler : public HTTPRequestHandler {
public:
virtual void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp){
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType("text/html");
// Generate requested HTML file
ostream& out = resp.send();
out << "<h1>Hello world! This files name is " << req.getURI() << "</h1>"
<< "<p>Host: " << req.getHost() << "</p>"
<< "<p>Method: " << req.getMethod() << "</p>";
out.flush();
cout << endl << "Client requested: =" << req.getURI() << endl;
}
};
class MyPutRequestHandler : public HTTPRequestHandler {
public:
virtual void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp){
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType("text/html");
string fileName = req.getURI();
ostream& out = resp.send();
out << "<h1>Hello friend!</h1>"
<< "<p>Host: " << req.getHost() << "</p>"
<< "<p>Method: " << req.getMethod() << "</p>"
<< "<p>Filename: " << req.getURI() << "</p>";
out.flush();
/** Does not work
StreamSocket strs = req.acceptConnection();
SocketStream ostr(strs);
std::string file("test.in");
Poco::FileInputStream istr(file, std::ios::binary);
StreamCopier::copyStream(istr, ostr);
*/
cout << endl << "URI=" << req.getURI() << endl;
}
};
class MyRequestHandlerFactory : public HTTPRequestHandlerFactory
{
public:
virtual HTTPRequestHandler* createRequestHandler(const HTTPServerRequest &req) {
string httpMethod = req.getMethod();
if( httpMethod.compare( HTTPRequest::HTTP_GET ) == 0 ){
return new MyGetRequestHandler;
} else if( httpMethod.compare( HTTPRequest::HTTP_PUT ) == 0 ) {
return new MyPutRequestHandler;
} else {
// Other request type
}
}
};
class MyServerApp : public ServerApplication {
protected:
int main(const vector<string> &args){
HTTPServer s(new MyRequestHandlerFactory, ServerSocket( atoi( args[1].c_str() ) ), new HTTPServerParams );
s.start();
cout << endl << "Server started" << endl;
cout << "Listening on port: " << args[1] << endl ;
waitForTerminationRequest();
cout << endl << "Shutting down server..." << endl;
s.stop();
return Application::EXIT_OK;
}
};
void usage(){
cout << "Usage: ./server <PORT_NUMBER>" << endl;
cout << "Please provide a port in range of 1024 - 49151 for the server to run on. Recommended: 300" << endl;
exit(-1);
}
int main(int argc, char* argv[]){
if( argc <= 1 ) { // Check that at least one argument was given.
cout << "Missing arguments" << endl << endl;
usage();
} else if( !is_numeric( argv[1] )){ // Check that only strings containing numbers were passed
cout << '"' << argv[1] << "\" is not a valid port number." << endl << endl;
usage();
} else if( not (1023 < atoi( argv[1] ) and atoi( argv[1] ) < 49152) ){ // Check that port was in specified range
cout << argv[1] << " is out of the specified range. Please enter a valid port number." << endl << endl;
usage();
} else {
// Nothing to do here, all good
}
MyServerApp app;
return app.run(argc, argv);
}