-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathFileService.cpp
70 lines (57 loc) · 1.55 KB
/
FileService.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <iostream>
#include <map>
#include <string>
#include "FileService.h"
#include "ClientError.h"
using namespace std;
FileService::FileService(string basedir) : HttpService("/") {
while (endswith(basedir, "/")) {
basedir = basedir.substr(0, basedir.length() - 1);
}
if (basedir.length() == 0) {
cout << "invalid basedir" << endl;
exit(1);
}
this->m_basedir = basedir;
}
bool FileService::endswith(string str, string suffix) {
size_t pos = str.rfind(suffix);
return pos == (str.length() - suffix.length());
}
void FileService::get(HTTPRequest *request, HTTPResponse *response) {
string path = this->m_basedir + request->getPath();
string fileContents = this->readFile(path);
if (fileContents.size() == 0) {
throw ClientError::notFound();
} else {
if (this->endswith(path, ".css")) {
response->setContentType("text/css");
} else if (this->endswith(path, ".js")) {
response->setContentType("text/javascript");
}
response->setBody(fileContents);
}
}
string FileService::readFile(string path) {
int fd = open(path.c_str(), O_RDONLY);
if (fd < 0) {
return "";
}
string result;
int ret;
char buffer[4096];
while ((ret = read(fd, buffer, sizeof(buffer))) > 0) {
result.append(buffer, ret);
}
close(fd);
return result;
}
void FileService::head(HTTPRequest *request, HTTPResponse *response) {
// HEAD is the same as get but with no body
this->get(request, response);
response->setBody("");
}