-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStaticHandler.cpp
129 lines (102 loc) · 3.4 KB
/
StaticHandler.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
#include "StaticHandler.h"
#include "NotFoundHandler.h"
#include "mime_types.hpp"
#include <iterator>
#include <fstream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include "markdown.h"
#include <map>
namespace Team15 {
namespace server {
RequestHandler::Status StaticHandler::Init(const std::string& uri_prefix,
NginxConfig config) {
compressionHandler_.Init(uri_prefix,config);
uri_prefix_ = uri_prefix;
std::string root = "";
for (auto statement : config.statements_) {
if (statement->tokens_[0] == "root") {
root = statement->tokens_[1];
break;
}
}
if (root == "") {
// Error no root for file handler
return RequestHandler::Status::INVALID_INPUT;
}
rootPath_ = root;
return RequestHandler::Status::OK;
}
RequestHandler::Status StaticHandler::HandleRequest(const Request& request,
Response* response) {
std::string path = '.' + rootPath_.string();
// Connect file with root path
path = path + "/" + request.uri().substr(uri_prefix_.size());
// default file
if (path[path.size() - 1] == '/') {
path += "index.html";
}
std::cout << path << std::endl;
// determine file extension
std::size_t last_slash_pos = path.find_last_of("/");
// used for testing threads; will stall if requesting hold
// if (path.substr(last_slash_pos + 1) == "hold")
// while(true) {}
std::size_t last_dot_pos = path.find_last_of(".");
std::string extension;
if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos) {
extension = path.substr(last_dot_pos + 1);
}
boost::filesystem::path boost_path(path);
if (!boost::filesystem::exists(path)
|| !boost::filesystem::is_regular_file(path)) {
NotFoundHandler not_found_handler;
not_found_handler.HandleRequest(request, response);
return RequestHandler::Status::OK;
}
std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
// here
if (!is) {
NotFoundHandler not_found_handler;
not_found_handler.HandleRequest(request, response);
return RequestHandler::Status::OK;
}
std::string body;
if (extension == "md") {
markdown::Document doc;
char c;
std::string temp;
while (is.get(c)) {
temp += c;
}
is.close();
doc.read(temp);
std::ostringstream s;
doc.write(s);
body = s.str();
body = "<html>" + body + "</html>";
response->AddHeader("Content-Type", "text/html");
} else {
char c;
while (is.get(c)) {
body += c;
}
is.close();
response->AddHeader("Content-Type", http::server::mime_types::extension_to_type(extension));
}
std::string content_length = std::to_string((int) body.size());
response->SetStatus(Response::ResponseCodeOK);
response->SetReasoning("OK");
std::map<std::string, std::string> headers = request.GetHeaders();
if ((headers.find("Accept-Encoding") != headers.end())
&& (request.FetchHeaderField(HttpMessage::HttpHeaderFields::ACCEPT_ENCODING).find("gzip") != std::string::npos)) {
response->SetBody(body);
return compressionHandler_.HandleRequest(request,response);
}
response->SetBody(body);
response->AddHeader("Content-Length", content_length);
return RequestHandler::Status::OK;
}
}
}