-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.c
More file actions
305 lines (242 loc) · 8.21 KB
/
httpserver.c
File metadata and controls
305 lines (242 loc) · 8.21 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
292
293
294
295
296
297
298
299
/*
This code primarily comes from
http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
and
http://www.binarii.com/files/papers/c_sockets.txt
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "requestparse.h"
#include "readHTML.h"
#include "parse.h"
#include "sort.h"
#include <pthread.h>
void* respond(void* response_data);
int end = 0; // signal to stop the server
/*
* Stops thread after 'q' has been entered. (Will gracefully exit program after one request.)
*/
void* stop(void* arg){
char* input = malloc(sizeof(char)*10);
pthread_mutex_t* lock = (pthread_mutex_t*) arg;
while(end != 1){
printf("Enter 'q' to stop program: ");
scanf("%s", input);
// printf("%s\n", input);
if(strcmp("q",input) == 0) {
pthread_mutex_lock(lock);
end = 1;
pthread_mutex_unlock(lock);
}
}
free(input);
pthread_exit(NULL);
}
/*
* Structure for holding thread data.
*/
typedef struct threaddata {
char* header;
char* footer;
int fd;
data_container* data;
} thread_data;
/*
* Start server connection.
*/
int start_server(int PORT_NUMBER)
{
// structs to represent the server and client
struct sockaddr_in server_addr,client_addr;
int sock; // socket descriptor
// 1. socket: creates a socket descriptor that you later use to make other system calls
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
int temp;
if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&temp,sizeof(int)) == -1) {
perror("Setsockopt");
exit(1);
}
// configure the server
server_addr.sin_port = htons(PORT_NUMBER); // specify port number
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
// 2. bind: use the socket and associate it with the port number
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("Unable to bind");
exit(1);
}
// create data container
unlink("data.html");
data_container* data = parse_data("course_evals.txt");
// set header and footer of response
char* header = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n";
char* footer = "</body></html>";
// initialize thread array
unsigned int request_count = 0;
size_t num_threads = 100;
void* r = NULL;
pthread_t threads[num_threads];
pthread_mutex_t* lock = malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(lock,NULL);
// create stop thread
pthread_t t;
pthread_create(&t, NULL, &stop, lock);
data_to_HTML(data, "data.html");
pthread_mutex_lock(lock);
// will exit the loop after 'q' is entered in the terminal (exits the program, when end = 1)
while(end != 1) {
pthread_mutex_unlock(lock);
// 3. listen: indicates that we want to listen to the port to which we bound; second arg is number of allowed connections
// second arg here is the number of possible queued connections
if (listen(sock, 10) == -1) {
perror("Listen");
exit(1);
}
// once you get here, the server is set up and about to start listening
printf("\nServer configured to listen on port %d\n", PORT_NUMBER);
fflush(stdout);
// 4. accept: wait here until we get a connection on that port
int sin_size = sizeof(struct sockaddr_in);
int fd = accept(sock, (struct sockaddr *)&client_addr,(socklen_t *)&sin_size);
if (fd != -1) {
printf("Server got a connection from (%s, %d)\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
printf("fd: %d\n", fd);
printf("sock: %d\n\n", sock);
// create thread data
thread_data* pass_data = malloc(sizeof(thread_data));
pass_data->header = header;
pass_data->footer = footer;
pass_data->fd = fd;
pass_data->data = copy_data(data);
// wait for thread to return if request count exceeds number of threads
printf("creating thread");
if (request_count > num_threads) pthread_join(threads[request_count % num_threads], r);
// create new thread
printf("creating thread\n");
pthread_create(&threads[request_count % num_threads], NULL, respond, pass_data);
request_count++;
}
pthread_mutex_lock(lock);
}
//unlock mutex
pthread_mutex_unlock(lock);
// join end thread
pthread_join(t, NULL);
// join response threads
for (int i = 0; i < (request_count % num_threads) ; i++)
pthread_join(threads[i], r);
// free the data container
free_data_container(data);
// free the lock
free(lock);
// 8. close: close the socket
close(sock);
printf("Server shutting down\n");
unlink("data.html");
return 0;
}
/*
* Run main program here.
*/
int main(int argc, char *argv[])
{
// check the number of arguments
if (argc != 2) {
printf("\nUsage: %s [port_number]\n", argv[0]);
exit(-1);
}
int port_number = atoi(argv[1]);
if (port_number <= 1024) {
printf("\nPlease specify a port number greater than 1024\n");
exit(-1);
}
// start server
start_server(port_number);
return 0;
}
/*
* Method handles a response to a request.
*/
void* respond(void* response_data) {
thread_data* td = (thread_data*)response_data;
// buffer to read data into
char request[1024];
// 5. recv: read incoming message (request) into buffer
int bytes_received = recv(td->fd,request,1024,0);
// null-terminate the string
request[bytes_received] = '\0';
// parse request from user
parsed_request* pr = parse_request(request);
print_request(*pr);
data_container* pd;
char filename[100];
// if it is a post request, enter the code block successeding the conditional
if ( isPost(pr) ) {
post_request* post_req = malloc(sizeof(post_request));
if(post_req == NULL) return NULL;
// parse the post request
parse_post(post_req, pr->rest);
int (*comparep) (course_data*, course_data*) = compare_course_id;
// check to see if data should be filtered
pd = choose_filter(td->data, post_req);
// check to see if data should be sorted
comparep = choose_sort(post_req);
if (comparep != NULL) {
// sleep(30);
printf("sort request detected: sorting...");
quicksort_data(pd->data, 0, pd->length - 1, comparep);
}
sprintf(filename, "data%d.html", td->fd);
data_to_HTML(pd, filename);
// if(pd != td->data){
free_data_shallow(pd);
printf("FREEING MEMORY\n");
// }
// free post request
free(post_req);
}
else {
// copy file name
strcpy(filename,"data.html");
}
printf("configuring response\n");
//read in HTML file
char* resource = readHTML("index.html");
// parse data into structure and format data into html
char* data = readHTML(filename);
printf("%s",data);
char* header = td->header;
char* footer = td->footer;
// 6. send: send the outgoing message (response) over the socket
send(td->fd, header, strlen(header), 0);
send(td->fd, resource, strlen(resource), 0);
send(td->fd, data, strlen(data), 0);
send(td->fd, footer, strlen(footer), 0);
// check to see if post request
if (isPost(pr)) {
unlink(filename);
}
// free html from memory
free(data);
free(resource);
// free request
free(pr);
// 7. close: close the connection
printf("Server closed connection\n");
close(td->fd);
// free data
free_data_shallow(td->data);
free(td);
pthread_exit(NULL);
}