-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
300 lines (220 loc) · 8.06 KB
/
server.c
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
292
293
294
295
296
297
298
299
/* Main Server File
+------------------------------------------------------------------+
| Project Code for Computer Systems COMP30023 2018 S1 |
| A simple HTML 1.0 Web Server which handles basic GET requests |
| To compile: Run the Makefile with "make" |
| Written by: Wei How Ng (828472) wein4 |
+------------------------------------------------------------------+
*/
// Library and header includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <pthread.h>
// Importing header files
#include "parse.h"
#include "read.h"
// Constant Declarations and Initialisation
#define BUFFERSIZE 8192
#define SPACE " "
// Resquest Reponses
const char* res200 = "HTTP/1.0 200 OK\r\nContent-Type: %s\r\n\r\n";
const char* res404 = "HTTP/1.0 404 Not Found\r\nContent-Type: %s\r\n\r\n";
// -------------------
// Struct Definition
// Struct for storing thread arguments
typedef struct {
int sockfd;
char* root_path;
} thread_arg_t;
// -------------------
// Function Declarations
void* thread_activity(void* arg);
void print_res(int sockfd, char* response,char* file, int bytes, char* mimetype);
//--------------------------------------------------------------------------------------------------
/* Main Functions
*
*/
// Main Function
int
main(int argc, char *argv[])
{
// Declaring the sockets file descriptions and the port number.
int sockfd, newsockfd, portno;// clilen;
// Structs for server and client address
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
// Checking if the number of command line arguements are valid
if (argc < 3) {
fprintf(stderr,"ERROR, no port or root path provided! \n");
exit(1);
}
// Saving the Port Number as an int
portno = atoi(argv[1]);
// ---------------------------------------
/* Creating TCP Socket*/
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// Return an error if the socket cannot be opened
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
// Allocating memory for server_addr
memset(&serv_addr, '0', sizeof(serv_addr));
/* Create address we're going to listen on (given port number)
- converted to network byte order & any IP address for
this machine */
// Determining what type of socket is created using socket()
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
// Specify the port to use for connections
serv_addr.sin_port = htons(portno);
// ---------------------------------------
/* Binding*/
// Binding socket to the specified address
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
// ---------------------------------------
/* Listening*/
/* Listen on socket - means we're ready to accept connections -
incoming connection requests will be queued */
listen(sockfd,10);
clilen = sizeof(cli_addr);
// ---------------------------------------
/* Accepting*/
/* Accept a connection - block until a connection is ready to
be accepted. Get back a new file descriptor to communicate on. */
// Loop forever if there are requests coming from the socket
while(1) {
// Assigning a new socket to the accepted one
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
// Exit on error with accept
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
// Parsing newsockfd and the root directory into a thread_arg_t struct
thread_arg_t thread_arg;
thread_arg.sockfd = newsockfd;
thread_arg.root_path = argv[2];
// Create pthread **HERE**
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
int error_c = pthread_create(&tid, &attr,
thread_activity,
&thread_arg);
if(error_c) {
perror("ERROR on creating thread");
exit(1);
}
// Detach pthread **HERE**
int error_d = pthread_detach(tid);
if(error_d) {
perror("ERROR on detaching thread");
exit(1);
}
}
// ---------------------------------------
return 0;
}
//--------------------------------------------------------------------------------------------------
/* Void Pointer Function
* ---------------------
* Handles the activities each thread executes
*/
void* thread_activity(void* thread_arg) {
/* Handle formatting of the void argument
struct into normal arguments*/
thread_arg_t* thread_arg_ptr = (thread_arg_t*) thread_arg;
int newsockfd = thread_arg_ptr->sockfd;
char* root = thread_arg_ptr->root_path;
// --------------
// Processes start here
// Array used for header buffer
char* header_buffer;
char response_buffer[BUFFERSIZE];
// Allocate Space to the buffer for the header
// memset(&header_buffer, '0', (BUFFERSIZE*sizeof(char)));
header_buffer = malloc(sizeof(char) * BUFFERSIZE);
// Read characters from the socket and then process them
int n = read(newsockfd, header_buffer, BUFFERSIZE-1);
// Error Handling if error from reading to socket
if(n < 0) {
perror("ERROR reading from socket");
exit(1);
}
// Parse the header in the buffer and extract the path
char* relative_path = parse_header(header_buffer, SPACE);
// Free the header char buffer used
free(header_buffer);
// Concatenate the relative_path with the given root
char* abs_path = concat(root, relative_path);
free(relative_path);
// Create a buffer info struct and default the length to 0
buffer_info bf;
bf.buff_len = 0;
// Find the extension and its respective mime type
char* ext = find_extension(abs_path);
char* mimetype = match_ext(ext);
// ---------------------------------------
/* Writing*/
/* Writing the response - parse the correct content type into
the respective response and write it into the socket. */
// Check that the file exists first
int res_int = check_file_exist(abs_path);
if (res_int) {
// Handle the response header formatting
sprintf(response_buffer, res200, mimetype);
// Open the file located at the abs_path
read_file(abs_path,&bf);
print_res(newsockfd, (response_buffer), bf.buff_str, bf.buff_len, mimetype);
// Free the buffer_info struct
free(bf.buff_str);
}
// Print response if not found
else {
// Handle the response header formatting.
sprintf(response_buffer, res404, mimetype);
// Write to socket.
print_res(newsockfd, (response_buffer), NULL, 0, mimetype);
}
// Free abs_path
free(abs_path);
// Free mimetype
if (mimetype != NULL) {
free(mimetype);
}
return NULL;
}
//--------------------------------------------------------------------------------------------------
/* Helper Functions
*
*/
/* Write the responses to the socket
* --------------------------------
* sockfd: The new socket file descriptor passed in for every resquest
* response: The response type the response should take depending on the file
* file: The file stored in the buffer if there is one
* bytes: The length of the file written in bytes
*/
void
print_res(int sockfd, char* response, char* file , int bytes, char* mimetype) {
// Check the type of response coming in.
if(bytes) {
// If file found and is not empty.
write(sockfd, response, strlen(response));
write(sockfd,file,bytes);
}
// If file was not found, just write response
else{
write(sockfd, response, strlen(response));
}
close(sockfd);
}