-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcode.c
253 lines (216 loc) · 9.29 KB
/
code.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
#include <stdio.h>
#include <string.h> // for strlen
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h> // for inet_addr
#include <unistd.h> // for write
#include <pthread.h> // for threading, link with lpthread
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
#define PATH "/home/ozgur/workspace/assignment2/sources" // Last char of this PATH cannot be "/", please mind this. (PATH connot finish "/")
// For example PATH cannot be "/home/ozgur/workspace/assignment2/sources/"
#define PORT_NO 8888
#define BUFFER_SIZE 1024
#define CONNECTION_NUMBER 10
int thread_count = 0; // Keeps the number of the threads working simultaneously.
sem_t mutex; // To control thread_counter.
void jpeg_handler(int socket, char *file_name) // handle jpeg files
{
char *buffer;
char *full_path = (char *)malloc((strlen(PATH) + strlen(file_name)) * sizeof(char));
int fp;
strcpy(full_path, PATH); // Merge the file name that requested and path of the root folder
strcat(full_path, file_name);
puts(full_path);
if ((fp=open(full_path, O_RDONLY)) > 0) //FILE FOUND
{
puts("Image Found.");
int bytes;
char buffer[BUFFER_SIZE];
send(socket, "HTTP/1.0 200 OK\r\nContent-Type: image/jpeg\r\n\r\n", 45, 0);
while ( (bytes=read(fp, buffer, BUFFER_SIZE))>0 ) // Read the file to buffer. If not the end of the file, then continue reading the file
write (socket, buffer, bytes); // Send the part of the jpeg to client.
}
else // If there is not such a file.
{
write(socket, "HTTP/1.0 404 Not Found\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n<!doctype html><html><body>404 File Not Found</body></html>", strlen("HTTP/1.0 404 Not Found\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n<!doctype html><html><body>404 File Not Found</body></html>"));
}
free(full_path);
close(fp);
}
void html_handler(int socket, char *file_name) // handle html files
{
char *buffer;
char *full_path = (char *)malloc((strlen(PATH) + strlen(file_name)) * sizeof(char));
FILE *fp;
strcpy(full_path, PATH); // Merge the file name that requested and path of the root folder
strcat(full_path, file_name);
fp = fopen(full_path, "r");
if (fp != NULL) //FILE FOUND
{
puts("File Found.");
fseek(fp, 0, SEEK_END); // Find the file size.
long bytes_read = ftell(fp);
fseek(fp, 0, SEEK_SET);
send(socket, "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n", 44, 0); // Send the header for succesful respond.
buffer = (char *)malloc(bytes_read * sizeof(char));
fread(buffer, bytes_read, 1, fp); // Read the html file to buffer.
write (socket, buffer, bytes_read); // Send the content of the html file to client.
free(buffer);
fclose(fp);
}
else // If there is not such a file.
{
write(socket, "HTTP/1.0 404 Not Found\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n<!doctype html><html><body>404 File Not Found</body></html>", strlen("HTTP/1.0 404 Not Found\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n<!doctype html><html><body>404 File Not Found</body></html>"));
}
free(full_path);
}
void *connection_handler(void *socket_desc)
{
int request;
char client_reply[BUFFER_SIZE], *request_lines[3];
char *file_name;
char *extension;
// Get the socket descriptor.
int sock = *((int *)socket_desc);
// Get the request.
request = recv(sock, client_reply, BUFFER_SIZE, 0);
sem_wait(&mutex);
thread_count++;
if(thread_count > 10) // If there is 10 request at the same time, other request will be refused.
{
char *message = "HTTP/1.0 400 Bad Request\r\nContent-Type: text/html\r\n\r\n<!doctype html><html><body>System is busy right now.</body></html>";
write(sock, message, strlen(message));
thread_count--;
sem_post(&mutex);
free(socket_desc);
shutdown(sock, SHUT_RDWR);
close(sock);
sock = -1;
pthread_exit(NULL);
}
sem_post(&mutex);
if (request < 0) // Receive failed.
{
puts("Recv failed");
}
else if (request == 0) // receive socket closed. Client disconnected upexpectedly.
{
puts("Client disconnected upexpectedly.");
}
else // Message received.
{
printf("%s", client_reply);
request_lines[0] = strtok(client_reply, " \t\n");
if (strncmp(request_lines[0], "GET\0", 4) == 0)
{
// Parsing the request header.
request_lines[1] = strtok(NULL, " \t");
request_lines[2] = strtok(NULL, " \t\n");
if (strncmp(request_lines[2], "HTTP/1.0", 8) != 0 && strncmp(request_lines[2], "HTTP/1.1", 8) != 0) // Bad request if not HTTP 1.0 or 1.1
{
char *message = "HTTP/1.0 400 Bad Request\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n<!doctype html><html><body>400 Bad Request</body></html>";
write(sock, message, strlen(message));
}
else
{
char *tokens[2]; // For parsing the file name and extension.
file_name = (char *)malloc(strlen(request_lines[1]) * sizeof(char));
strcpy(file_name, request_lines[1]);
puts(file_name);
// Getting the file name and extension
tokens[0] = strtok(file_name, ".");
tokens[1] = strtok(NULL, ".");
if(strcmp(tokens[0], "/favicon") == 0 && strcmp(tokens[1], "ico")) // Discard the favicon.ico requests.
{
sem_wait(&mutex);
thread_count--;
sem_post(&mutex);
free(socket_desc);
shutdown(sock, SHUT_RDWR);
close(sock);
sock = -1;
pthread_exit(NULL);
}
else if (tokens[0] == NULL || tokens[1] == NULL) // If there is not an extension in request or request to just localhost:8888/
{
char *message = "HTTP/1.0 400 Bad Request\r\nConnection: close\r\n\r\n<!doctype html><html><body>400 Bad Request. (You need to request to jpeg and html files)</body></html>";
write(sock, message, strlen(message));
}
else
{
if (strcmp(tokens[1], "html") != 0 && strcmp(tokens[1], "jpeg") != 0) // If the request is not to html or jpeg files, it will be respond 400 Bad Request
{
char *message = "HTTP/1.0 400 Bad Request\r\nConnection: close\r\n\r\n<!doctype html><html><body>400 Bad Request. Not Supported File Type (Suppoerted File Types: html and jpeg)</body></html>";
write(sock, message, strlen(message));
}
else
{
if (strcmp(tokens[1], "html") == 0)
{
sem_wait(&mutex); // Prevent two or more thread do some IO operation same time.
html_handler(sock, request_lines[1]);
sem_post(&mutex);
}
else if (strcmp(tokens[1], "jpeg") == 0)
{
sem_wait(&mutex); // Prevent two or more thread do some IO operation same time
jpeg_handler(sock, request_lines[1]);
sem_post(&mutex);
}
}
free(extension);
}
free(file_name);
}
}
}
//sleep(50); // If you want to see just 10 thread is working simultaneously, you can sleep here.
// After send 10 request, 11th request will be responded as "System is busy right now".
free(socket_desc);
shutdown(sock, SHUT_RDWR);
close(sock);
sock = -1;
sem_wait(&mutex);
thread_count--;
sem_post(&mutex);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
sem_init(&mutex, 0, 1); // Inıtialize the mutex from 1.
int socket_desc, new_socket, c, *new_sock;
struct sockaddr_in server, client;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
puts("Could not create socket");
return 1;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT_NO);
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
puts("Binding failed");
return 1;
}
listen(socket_desc, 20);
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while ((new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c))) // Accept the connection.
{
puts("Connection accepted \n");
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = new_socket;
if (pthread_create(&sniffer_thread, NULL, connection_handler, (void *)new_sock) < 0) // Create a thread for each request.
{
puts("Could not create thread");
return 1;
}
}
return 0;
}