This repository was archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.cc
91 lines (74 loc) · 1.7 KB
/
example.cc
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
/**
* Example HTTP Server
*
* Copyright (C) 2020 Ali Bakhtiar
*
* The MIT License
*
* https://twitter.com/ali_bakhtiar
* https://github.com/alibakhtiar
*/
/**
* Compile:
* g++ example.cc HttpServer.hh -std=c++11 -Wall -pthread -O3 -o example.out
* ./example.out <port>
*
* @modefied : 20 January 2020
* @created : 19 January 2020
* @author : Ali Bakhtiar
*/
#include "HttpServer.hh"
using namespace SimpleHttpServer;
/**
* Main
*/
int main(int argc, char* argv[])
{
Server server;
server.ip = "0.0.0.0";
server.port = 5000;
if (argc >= 2) {
server.port = std::atoi(argv[1]);
}
server.onRequest([](Request *req, Response *res) {
res->header("Content-Type", "text/plain");
res->header("Server", "ExampleHTTPServer");
res->header("Cache-Control", "no-cache, no-store, must-revalidate");
if (req->url == "/" || req->url == "/index.cpp") {
res->body = "Your Request:\n";
res->body += "method: ";
res->body += req->method;
res->body += "\n";
res->body += "http version: ";
res->body += std::to_string(req->httpMajorVersion);
res->body += ".";
res->body += std::to_string(req->httpMinorVersion);
res->body += "\n";
res->body += "url: ";
res->body += req->url;
res->body += "\n";
if (req->queryString != "") {
res->body += "query string: ";
res->body += req->queryString;
res->body += "\n";
}
res->body += "headers:\n";
for (auto const &h : req->headers) {
res->body += h.first;
res->body += ": ";
res->body += h.second;
res->body += "\n";
}
}
else {
res->statusCode = 404;
res->errorPage();
}
res->send();
return true;
});
bool rc = createServer(&server);
if (rc == false)
return 1;
return 0;
}