-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_utils.c
More file actions
291 lines (268 loc) · 6.66 KB
/
Copy pathserver_utils.c
File metadata and controls
291 lines (268 loc) · 6.66 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
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
281
282
283
284
285
286
287
288
289
290
291
/*
* Network computing
* Assignment 2: A HTTP program
* Written by Tran Quoc Hoan
* programmed by milestones method (step by step)
*
* ServerUtils.c
* Connect socket, socket handle
*/
#include <stdio.h>
#include <string.h> /* for memset() function */
#include <stdlib.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <netdb.h>
#include "server_utils.h"
#include "request.h"
#include "router.h"
#include "mime.h"
#define TRUE 1
#define FALSE 0
#define WORK_DIR "/var/www"
extern router *rtr;
/* Error handling */
void err_handle( char * msg ) {
perror( msg );
exit ( EXIT_FAILURE );
}
/* Send home page */
void send_home( int sock ) {
char outbuf[ MAXBUF + 1];
memset( &outbuf, 0, sizeof(outbuf) );
// get time now
time_t timer = time( NULL );
struct tm *date = localtime( &timer );
snprintf( outbuf, sizeof(outbuf),
"HTTP/1.0 200 OK\r\n"
"Server: %s\r\n"
"Content-Type:text/html\r\n"
"Date: %s\r\n"
"Connection: Closed\r\n"
"<font color=red><h1>404 Not Available.</h1></font>\r\n", SERVER,
asctime( date ) );
send( sock, outbuf, (int)strlen(outbuf), 0);
#ifdef DEBUG
printf("Finish send_home \n");
#endif
return;
}
/* Send html header */
void send_header( int sock, char *fmime )
{
if(sock < -1 || fmime == NULL)
{
printf("Error: Invalid Socket or mime type\n");
return;
}
char outbuf[ MAXBUF + 1];
memset( &outbuf, 0, sizeof(outbuf) );
// get time now
time_t timer = time( NULL );
struct tm *date = localtime( &timer );
snprintf( outbuf, sizeof(outbuf),
"HTTP/1.0 200 OK\r\n"
"Server: %s\r\n"
"Content-Type: %s\r\n"
"Connection: Closed\r\n"
"Date: %s\r\n", SERVER, fmime, asctime( date ) );
send( sock, outbuf, (int)strlen(outbuf), 0);
#ifdef DEBUG
printf("Finish send_header \n");
#endif
return;
}
/* Send html data */
void send_data( int sock, FILE* fp ) {
int len;
char outbuf[ MAXBUF + 1];
memset( &outbuf, 0, sizeof(outbuf) );
while ( !feof(fp) ) {
len = fread( outbuf, 1, sizeof( outbuf ), fp );
outbuf[len] ='\0';
send ( sock, outbuf, len, 0 );
}
#ifdef DEBUG
printf("Finish send_data \n");
#endif
return;
}
/* Send html file */
void send_html( int sock, const char* FILENAME ) {
FILE* fp;
fp = fopen( FILENAME, "r" );
if ( fp == NULL ) {
// send html header only
send_home( sock );
} else {
send_header( sock, "text/html" );
send_data( sock, fp );
fclose( fp );
}
#ifdef DEBUG
printf("Finish send_html \n");
#endif
return;
}
void send_static_file(int sock, const char *filename)
{
FILE *fp=NULL;
fp = fopen(filename, "r");
if(fp == NULL)
{
send_home(sock);
}
else
{
send_header( sock, get_mime_type(filename) );
send_data( sock, fp );
fclose( fp );
}
}
/* Handle for each client */
int client_handle( Client *c ) {
/* recv(), send(), close() */
// recv()
char inbuf[ MAXBUF + 1];
//char *inbuf;
memset( &inbuf, 0, sizeof(inbuf) );
struct timeval timeout;
timeout.tv_sec = 30;
timeout.tv_usec = 0;
printf("Serving client (IP:port)%s:%d\n",c->IP,c->port);
if (setsockopt (c->socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
printf("setsockopt failed\n");
if (setsockopt (c->socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
printf("setsockopt failed\n");
if ( recv( c->socket, inbuf, sizeof(inbuf), 0 ) < 0 ) {
close( c->socket );
perror("recv");
return FALSE;
};
#ifdef DEBUG
printf("Buffer--->\n");
printf("%s\n", inbuf );
printf("==========\n");
#endif
req *r = parse_request(inbuf);
if(r==NULL)
{
printf("Unable to parse request\n");
return(FALSE);
}
#ifdef DEBUG
printf("method %s uri %s proto %s \n",r->method,r->uri,r->protocol);
#endif
if(do_router(rtr,c->socket,r)==-1)
{
//try static service
// here we should check if uri is a file
// if so then we will only allow "html,js,css,txt" extension
// else we will handover to router
char file_mime[100];
strcpy(file_mime,get_mime_type(r->uri));
if(strcasecmp(file_mime,"text/plain")==0 || strcasecmp(file_mime,"text/css")==0 \
|| strcasecmp(file_mime,"text/html")==0 || strcasecmp(file_mime,"application/javascript")==0)
{
char request_file[1024]="";
strcpy(request_file,WORK_DIR); // append working directory
strcat(request_file,"/static");
strcat(request_file,r->uri);
send_static_file(c->socket,request_file);
}
else
send_home(c->socket);
}
/* the following are for static file serving
if(strcasecmp(r->method,"GET")==0)
do_get(c->socket,r);
else if(strcasecmp(r->method,"POST")==0)
do_post(c->socket,r);
else if(strcasecmp(r->method,"PUT")==0)
do_put(c->socket,r);
else if(strcasecmp(r->method,"DELETE")==0)
do_del(c->socket,r);
else if(strcasecmp(r->method,"HEAD")==0)
do_head(c->socket,r);
else
do_unknown(c->socket,r);
*/
if(r)
{
if(r->payload)
{
free(r->payload);
r->payload = NULL;
}
free(r);
r = NULL;
}
return TRUE;
}
void do_get(int sock,req *r)
{
char request_file[1024]="";
strcpy(request_file,WORK_DIR); // append working directory
// for the time being we will serv only disk files no virtual resource
//printf("%s\n",r->uri);
// we dont allow 2 dots consecutively in url
if(strstr(r->uri,".."))
{
do_unknown(sock, r);
return;
}
if(!strncmp(r->uri,"/",strlen("/")) && (strlen(r->uri) == 1 ) )
{
// landing page or resource
// for the time being index.html
strcat(request_file ,"/index.html");
}
else
{
// strcpy(request_file,WORK_DIR);
strcat(request_file,r->uri);
}
//printf("request file = %s\n",request_file);
send_html(sock,request_file);
}
void do_unknown(int sock, req* r)
{
char outbuf[ MAXBUF + 1];
memset( &outbuf, 0, sizeof(outbuf) );
// get time now
time_t timer = time( NULL );
struct tm *date = localtime( &timer );
snprintf( outbuf, sizeof(outbuf), \
"HTTP/1.0 200 OK\r\n" \
"Content-Type:text/html\r\n" \
"Date: %s\r\n" \
"<font color=red><h1>HELLO! Hacker! The feature is NOT Implemented yet.</h1></font>\r\n", \
asctime( date ) );
send( sock, outbuf, (int)strlen(outbuf), 0);
}
void do_post(int sock,req *r)
{
//do_unknown(sock,r);
printf("payload = %s \n",r->payload);
send_header(sock,"text/plain");
}
void do_put(int sock,req *r)
{
//do_unknown(sock,r);
printf("File tobe created %s%s\n",WORK_DIR,r->uri);
printf("With content--->\n%s\n",r->payload);
}
void do_del(int sock,req *r)
{
do_unknown(sock,r);
}
void do_head(int sock,req *r)
{
//do_unknown(sock,r);
send_header(sock,"text/plain");
}