-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwebapp.cpp
159 lines (119 loc) · 4.06 KB
/
webapp.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "webapp.h"
void WebappResponder::reply(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply) {
QueryHandler::addHeader(reply);
if ( request.method() == "OPTIONS" ) {
reply.addHeader("Allow", "GET");
reply.httpReturn(200, "OK");
return;
}
if ( request.method() != "GET") {
reply.httpReturn(403, "To retrieve files use the GET method!");
return;
}
double timediff = -1;
string url = request.url();
string base = getBase(request);
getBase(request);
if ( base == request.url() ) {
reply.addHeader("Location", (base + "/").c_str());
reply.httpReturn(301, "Moved Permanently");
return;
}
if ( (int)url.find(base) == 0) {
esyslog("restfulapi Webapp: file request url %s", request.url().c_str());
string fileName = getFileName(base, url);
string file = getFile(base, fileName);
if (!FileExtension::get()->exists(file)) {
esyslog("restfulapi Webapp: file does not exist");
reply.httpReturn(404, "File not found");
return;
}
if (request.hasHeader("If-Modified-Since")) {
timediff = difftime(FileExtension::get()->getModifiedTime(file), FileExtension::get()->getModifiedSinceTime(request));
}
if (timediff > 0.0 || timediff < 0.0) {
streamResponse(fileName, out, file, reply);
} else {
esyslog("restfulapi Webapp: file not modified, returning 304");
reply.httpReturn(304, "Not-Modified");
}
}
}
/**
* retrieve base of webapp
*/
string WebappResponder::getBase(cxxtools::http::Request& request) {
string url = request.url();
if (url.find_first_of("/") == 0) {
url = url.substr(1, url.length());
}
return "/" + url.substr(0, url.find_first_of("/"));
};
/**
* retrieve filename width path
* @param string fileName
*/
string WebappResponder::getFile(string base, std::string fileName) {
map<string, string> webapps = Settings::get()->Webapps();
map<string, string>::iterator it;
string webappPath;
it = webapps.find(base.substr(1));
webappPath = it->second;
if ( webappPath.find_last_of("/") == (webappPath.length() - 1) ) {
webappPath = webappPath.substr(0, webappPath.length() - 1);
}
if ( fileName.find_first_of("/") == 0 ) {
fileName = fileName.substr(1, fileName.length() - 1);
}
return webappPath + (string)"/" + fileName;
};
/**
* retrieve filename
* @param string base url
* @param string url
*/
string WebappResponder::getFileName(string base, string url) {
if ( url.find_last_of("/") == (url.length() - 1) ) {
url = url.substr(0, url.length() - 1);
}
string file = url.replace(0, base.length(), "");
if ( file != "" && file.find(".") == string::npos ) {
file = file + "/index.html";
} else if ( file == "" ) {
file = "index.html";
}
esyslog("restfulapi: requested file %s", file.c_str());
return file;
};
/**
* determine contenttype
* @param string fileName
*/
string WebappResponder::getContentType(string fileName) {
map<string, string> types = Settings::get()->WebappFileTypes();
map<string, string>::iterator it;
string type = fileName.substr(fileName.find_last_of(".")+1);
string contentType = "application/octet-stream";
esyslog("restfulapi Webapp: file extension of %s is '%s'", fileName.c_str(), type.c_str());
it = types.find(type);
if (it != types.end()) {
contentType = it->second;
}
esyslog("restfulapi Webapp: file type of %s is '%s'", fileName.c_str(), contentType.c_str());
return contentType;
};
/**
* stream file
*/
void WebappResponder::streamResponse(string fileName, ostream& out, string file, cxxtools::http::Reply& reply) {
string contentType = getContentType(fileName);
StreamExtension se(&out);
if ( contentType != "" && se.writeBinary(file) ) {
esyslog("restfulapi Webapp: successfully piped file %s", fileName.c_str());
FileExtension::get()->addModifiedHeader(file, reply);
reply.addHeader("Content-Type", contentType.c_str());
} else {
esyslog("restfulapi Webapp: error piping file %s", fileName.c_str());
reply.httpReturn(404, "File not found");
}
};