-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
204 lines (172 loc) · 5.06 KB
/
server.cpp
File metadata and controls
204 lines (172 loc) · 5.06 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
*
* @file server.cpp
* @author ajhende4 Alec Henderson
*/
#include <cstring>
#include <pthread.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <filesystem>
#include <fstream>
#include <thread>
#include <csignal>
#include "http.cpp"
#define HTTP_PORT "8000"
#define LISTEN_QUEUE_LEN 8
#define BLOCK_SIZE 1024
using namespace std;
//TODO: make this work wherever
set<string> allowed;
bool running = false;
int boundSocket = -1;
string resourcePath;
void sendMessage(int socket, httpResponse &response) {
string header = response.getHeader();
auto len = header.size();
const char *x = header.c_str();
cout << "sending file \"" << response.filepath << "\"\n";
while(len > 0) {
ssize_t bytesWritten;
if(header.size() < BLOCK_SIZE) {
bytesWritten = write(socket, x, header.size());
} else {
bytesWritten = write(socket, x, BLOCK_SIZE);
}
if(bytesWritten < 0) {
cerr << "error writing getHeader to socket\n";
return;
} else if(bytesWritten == 0) {
cerr << "connection closed\n";
return;
}
x += bytesWritten;
len -= bytesWritten;
}
if(response.filepath.empty()) {
return;
}
if(!filesystem::exists(response.filepath)) {
cerr << "file \"" << response.filepath << "\" DNE\n";
return;
}
std::ifstream inputFile(response.filepath, std::ios::binary);
if (!inputFile.is_open()) {
std::cerr << "Error opening image file." << std::endl;
return;
}
char data[BLOCK_SIZE];
ssize_t bytesWritten;
auto n = filesystem::file_size(response.filepath);
while(n > 0 && inputFile.good()) {
inputFile.read(data, BLOCK_SIZE);
if(n < BLOCK_SIZE) {
bytesWritten = write(socket, data, n);
} else {
bytesWritten = write(socket, data, BLOCK_SIZE);
}
if(bytesWritten < 0) {
cerr << "error writing file to socket\n";
return;
} else if(bytesWritten == 0) {
cerr << "connection closed\n";
return;
}
n -= bytesWritten;
}
}
void *handleClient(void *arg) {
int sock = (int) (long) arg;
httpRequest req(sock);
httpResponse resp(allowed, req, resourcePath);
sendMessage(sock, resp);
close(sock);
return nullptr;
}
void sigHandler(int) {
cout << "shutting down\n";
running = false;
close(boundSocket);
exit(0);
}
int startServer() {
struct sigaction sig{};
sig.sa_flags = 0;
sigemptyset(&sig.sa_mask);
sig.sa_handler = sigHandler;
sigaction(SIGINT, &sig, nullptr);
sigaction(SIGTERM, &sig, nullptr);
sigaction(SIGKILL, &sig, nullptr);
// Prepare a description of server address criteria.
struct addrinfo addrCriteria{};
memset(&addrCriteria, 0, sizeof(addrCriteria));
addrCriteria.ai_family = AF_INET;
addrCriteria.ai_flags = AI_PASSIVE;
addrCriteria.ai_socktype = SOCK_STREAM;
addrCriteria.ai_protocol = IPPROTO_TCP;
struct addrinfo *servAddr;
if(getaddrinfo(nullptr, HTTP_PORT, &addrCriteria, &servAddr)) {
perror("cant get address info");
exit(1);
}
//try and use first address
if(servAddr == nullptr) {
perror("can't get address");
exit(1);
}
boundSocket = socket(servAddr->ai_family, servAddr->ai_socktype, servAddr->ai_protocol);
if(boundSocket < 0) {
perror("cant create socket");
exit(1);
}
if(bind(boundSocket, servAddr->ai_addr, servAddr->ai_addrlen) != 0) {
perror("cant bind socket");
exit(1);
}
if(listen(boundSocket, LISTEN_QUEUE_LEN) != 0) {
perror("couldn't listen to port");
exit(1);
}
struct sockaddr_in clientAddr = {};
socklen_t clientAddrLen = sizeof (clientAddr);
running = true;
while(running) {
int sock = accept(boundSocket, (struct sockaddr *) &clientAddr, &clientAddrLen);
pthread_t clientThread;
pthread_create(&clientThread, nullptr, handleClient, (void *) (long) sock);
//pthread_join(clientThread, nullptr);
pthread_detach(clientThread);
}
close(boundSocket);
return 0;
}
inline void addToFileset(const string& directory) {
static const unsigned long len = directory.size();
for (const auto & entry : filesystem::directory_iterator(directory)) {
string p = entry.path();
if(filesystem::is_directory(p)) {
addToFileset(p);
} else {
allowed.insert(p.substr(len));
}
}
}
int main(int argc, char *argv[]) {
if(argc == 2) {
if(filesystem::exists(argv[1])) {
resourcePath = string(argv[1]);
} else {
resourcePath = "/home/alec/Documents/http/siteFiles";
}
} else {
resourcePath = "/home/alec/Documents/http/siteFiles";
}
cout << "server directory is: " << resourcePath << '\n';
addToFileset(resourcePath);
startServer();
}