-
Notifications
You must be signed in to change notification settings - Fork 0
/
nzServ.cpp
280 lines (238 loc) · 4.96 KB
/
nzServ.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
nzServ.cpp
Noah Betzen
A simple web server in C++.
Compile with:
g++ nzServ.cpp -o nzServ.out
Run with:
./nzServ.out PORT DIR
NOTE: Do not use relative directories. Just cd to the HTML location and run the exectuable from there.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
using std::ofstream;
#include <sstream>
using std::stringstream;
#define BUFFERSIZE 8096
struct
{
char *extension;
char *fileType;
} mimeTypes [] = {
{"html","text/html"},
{"js","text/js"},
{"css","test/css"},
{"png", "image/png"},
{"jpg", "image/jpeg"},
{0,0}
};
void logIt(stringstream &logStream)
{
ofstream logFile("nzServ.log", std::ios_base::out | std::ios_base::app);
logFile << logStream.rdbuf();
logStream.str("");
}
stringstream logStream;
void web(int fd, int hit)
{
int j;
int fileFd;
int bufferLength;
int len;
long i;
long ret;
char *fileStr;
static char buffer[BUFFERSIZE+1];
ret = read(fd, buffer, BUFFERSIZE);
if(ret == 0 || ret == -1)
{
logStream << "ERROR: Failed to read browser request!" << endl;
logIt(logStream);
exit(1);
}
if(ret > 0 && ret < BUFFERSIZE)
{
buffer[ret] = 0;
}
else
{
buffer[0] = 0;
}
for(i=0;i<ret;i++)
{
if(buffer[i] == '\r' || buffer[i] == '\n')
{
buffer[i]='*';
}
}
logStream << "LOG: Request: " << buffer << " " << hit << endl;
logIt(logStream);
if(strncmp(buffer,"GET ",4) && strncmp(buffer,"get ",4))
{
logStream << "ERROR: Can only handle GET requests!" << endl;
logIt(logStream);
exit(1);
}
for(i = 4; i < BUFFERSIZE; i++)
{
if(buffer[i] == ' ')
{
buffer[i] = 0;
break;
}
}
for(j = 0; j < i - 1; j++)
{
if(buffer[j] == '.' && buffer[j+1] == '.')
{
logStream << "ERROR: . and .. paths not supported." << endl;
logIt(logStream);
exit(1);
}
}
if(!strncmp(&buffer[0], "GET /\0", 6) || !strncmp(&buffer[0], "get /\0", 6))
{
strcpy(buffer, "GET /index.html");
}
bufferLength = strlen(buffer);
fileStr = (char *)0;
for(i = 0; mimeTypes[i].extension != 0; i++)
{
len = strlen(mimeTypes[i].extension);
if(!strncmp(&buffer[bufferLength-len], mimeTypes[i].extension, len))
{
fileStr = mimeTypes[i].fileType;
break;
}
}
if(fileStr == 0)
{
logStream << "ERROR: File extension not supported!" << endl;
logIt(logStream);
exit(1);
}
if((fileFd = open(&buffer[5], O_RDONLY)) == -1)
{
logStream << "ERROR: Failed to open file! " << buffer << endl;
logIt(logStream);
exit(1);
}
logStream << "LOG: Send: " << &buffer[5] << " " << hit << endl;
logIt(logStream);
sprintf(buffer,"HTTP/1.0 200 OK\r\nContent-Type: %s\r\n\r\n", fileStr);
write(fd, buffer, strlen(buffer));
while ((ret = read(fileFd, buffer, BUFFERSIZE)) > 0)
{
write(fd,buffer,ret);
}
sleep(1);
exit(1);
}
int main(int argc, char *argv[])
{
int i;
int port;
int pid;
int listenFd;
int socketFd;
int hit;
socklen_t length;
static struct sockaddr_in cli_addr;
static struct sockaddr_in serv_addr;
if( argc < 3 || argc > 3 || !strcmp(argv[1], "-?") )
{
cout << "Usage: server [port] [server directory] [&]" << endl;
exit(0);
}
if(fork() != 0)
{
return 0;
}
signal(SIGCLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
for(i = 0; i < 32; i++)
{
close(i);
}
setpgrp();
logStream << "LOG: Web server starting: " << argv[1] << " " << getpid() << endl;
logIt(logStream);
if((listenFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
logStream << "ERROR: Cannot create socket!" << endl;
logIt(logStream);
exit(1);
}
int enable = 1;
if(setsockopt(listenFd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
{
logStream << "ERROR: Cannot set socket options!" << endl;
logIt(logStream);
exit(1);
}
port = atoi(argv[1]);
logStream << "LOG: Port: " << port << endl;
logIt(logStream);
if(port < 0 || port > 65535)
{
logStream << "ERROR: Invalid port number, try 1 through 65535! Port: " << argv[1] << endl;
logIt(logStream);
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
if(bind(listenFd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
logStream << "ERROR: Cannot bind to socket!" << endl;
logIt(logStream);
exit(1);
}
if(listen(listenFd, 64) < 0)
{
logStream << "ERROR: Cannot listen on socket!" << endl;
logIt(logStream);
exit(1);
}
for(hit=1; ;hit++)
{
length = sizeof(cli_addr);
if((socketFd = accept(listenFd, (struct sockaddr *)&cli_addr, &length)) < 0)
{
logStream << "ERROR: Cannot accept socket!" << endl;
logIt(logStream);
exit(1);
}
if((pid = fork()) < 0)
{
logStream << "ERROR: Cannot fork!" << endl;
logIt(logStream);
exit(1);
}
else
{
if(pid == 0)
{
close(listenFd);
web(socketFd, hit);
}
else
{
close(socketFd);
}
}
}
}