This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.cc
218 lines (196 loc) · 5.05 KB
/
server.cc
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <new>
#include "module.h"
#include "console.h"
#include "taskqueue.h"
int socketFD;
// SIGINT handler
void failExit(int signo) {
fprintf(stderr, "Quitting\n");
close(socketFD);
exit(EXIT_FAILURE);
}
// http related
static const char response_200_head[] = "\
HTTP/1.1 200 OK\r\n\
Content-Type: text/html; charset=utf-8\r\n\
\r\n\
";
static const char _404_response_head[] = "\
HTTP/1.1 404 Not Found\r\n\
Content-Type: text/plain; charset=utf-8\r\n\
\r\n\
404 not found!\n\
Try following avaliable modules:\n\
";
static const char response_400[] = "\
HTTP/1.1 400 Bad Request\r\n\
Content-Type: text/html; charset=utf-8\r\n\
\r\n\
Bad Request.\n\
";
static const char root_response_head[] = "\
HTTP/1.1 200 OK\r\n\
Content-Type: text/plain; charset=utf-8\r\n\
\r\n\
Welcome!\n\
This server WORKS!!!\n\
Following modules are working:\n\
";
void sendstr(int connectFD, const char * response_body) {
int count = strlen(response_body);
#define MAXSIZE 8192
while (count > MAXSIZE) {
if (send(connectFD, response_body, MAXSIZE, 0) == -1) {
perror("send response");
printf("fail send response");
break;
}
}
#undef MAXSIZE
send(connectFD, response_body, count, 0);
}
void connectionHandler(int connectFD) {
printf("\n[connection]");
if(connectFD < 0) {
perror("accept connection");
printf("fail accept connection");
failExit(0);
}
// read request head and get path
printf(" receiving...");
char recvbuff[1024];
recvbuff[0] = '\0';// prevent empty input
char * path = recvbuff;
for (;;) {// receiving
int count = recv(connectFD, recvbuff, sizeof(recvbuff), 0);
if (count > 0) {
if (recvbuff[0] != 'G') { path[0] = '\0'; break; }
if (recvbuff[1] != 'E') { path[0] = '\0'; break; }
if (recvbuff[2] != 'T') { path[0] = '\0'; break; }
path += 4;
int i = 0;
if (path[0] != '/') { path[0] = '\0'; break; }
while(path[i] != ' ')
if(i < sizeof(recvbuff) - 4) i++;
else { i = 0; break; }// prevent overflow
path[i] = '\0';
break;
}
if (count == 0)
break;
}
printf(" done.");
// send response
if (strcmp(path, "")) { // if path legal
if (!strcmp(path, "/")) {
printf(" responding root...");
// send response
sendstr(connectFD, root_response_head);
sendstr(connectFD, mod_list);
printf(" done.");
} else {
path++; // skip '/'
// check query
const char * params;
for (int i = 0;; i++) {
if(path[i] == '\0') { params = path + i; break;}
if(path[i] == '?') { path[i] = '\0'; params = path + i + 1; break; }
}
printf(" try serving with %s...", path);
char response_body[65536];
if (mod_serve(path, params, response_body)) {
// send head
send(connectFD, response_200_head, sizeof(response_200_head) - 1, 0);
// send body
sendstr(connectFD, response_body);
printf(" done.");
} else {
printf("\n[error] %s not found", path);
printf(" responding...");
// send head
send(connectFD, _404_response_head, sizeof(_404_response_head) - 1, 0);
// get/send body
sendstr(connectFD, mod_list);
printf("done.");
}
}
} else { // if path illegal
printf("\n[error] bad request");
printf(" responeding...");
send(connectFD, response_400, sizeof(response_400) - 1, 0);
printf(" done.");
}
// close connection
shutdown(connectFD, SHUT_RDWR);
close(connectFD);
printf("finished.\n");
fflush(stdout);
}
void * connectionAccepter(void *) {
TaskQueue<int> conqueue(connectionHandler);
for (;;) {
int connectFD = accept(socketFD, NULL, NULL);
conqueue.addTask(connectFD);
}
return NULL;
}
int main(int argc, char ** argv) {
int http_port = 8080;
argv++; argc--;
while (argc > 0) {
if (!strcmp(argv[0], "--port")) {
argv++; argc--;
int port = atoi(argv[0]);
if (port > 0)
http_port = port;
}
argv++; argc--;
}
// new socket
socketFD = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(socketFD == -1) {
perror("create socket");
exit(EXIT_FAILURE);
}
fprintf(stderr, "Socket Created\n");
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction( SIGPIPE, &sa, 0 );
// handle SIGINT
if(signal(SIGINT, failExit) == SIG_ERR) {
perror("handle signal");
failExit(0);
}
fprintf(stderr, "Signal Interupt Handled\n");
// bind socket on port 8080
struct sockaddr_in stSockAddr;
memset(&stSockAddr, 0, sizeof(struct sockaddr_in));
stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(http_port);
stSockAddr.sin_addr.s_addr = INADDR_ANY;
if(bind(socketFD, (const struct sockaddr *) &stSockAddr, sizeof(struct sockaddr_in)) == -1) {
perror("bind port");
failExit(0);
}
fprintf(stderr, "Port %d binded\n", http_port);
if(listen(socketFD, 12) == -1) {
perror("listen");
failExit(0);
}
fprintf(stderr, "Start Listening\n\n");
// accept and handle connections
pthread_t thread;
if (pthread_create(&thread, NULL, connectionAccepter, NULL)) { fprintf(stderr, "fail create thread."); }
console();
while (close(socketFD) == -1);
return 0;
}