-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.c
203 lines (168 loc) · 4.77 KB
/
app.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
#include <sys/socket.h>
#include <string.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <unistd.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/select.h>
#include <stdbool.h>
#define MAX_BUFFER 5000
#define MAX_THREADS 25
#define MAX_QUEUE 250
typedef struct {
int client_fd;
} client_t;
volatile bool stop_server = false;
client_t client_queue[MAX_QUEUE];
int queue_front = 0, queue_rear = 0;
pthread_mutex_t queue_lock;
pthread_cond_t queue_cond;
void check_enter_key() {
char buf;
if (read(STDIN_FILENO, &buf, 1) == 1 && buf == '\n') {
stop_server = true;
}
}
ssize_t safe_send(int client_fd, const void *data, size_t size) {
ssize_t bytes_sent = 0;
while (bytes_sent < size) {
ssize_t ret = send(client_fd, data + bytes_sent, size - bytes_sent, 0);
if (ret < 0) {
perror("Send failed");
return ret;
}
bytes_sent += ret;
}
return bytes_sent;
}
void *handle_client(void *arg) {
while (!stop_server) {
pthread_mutex_lock(&queue_lock);
while (queue_front == queue_rear && !stop_server) {
pthread_cond_wait(&queue_cond, &queue_lock);
}
if (stop_server) {
pthread_mutex_unlock(&queue_lock);
break;
}
int client_fd = client_queue[queue_front].client_fd;
queue_front = (queue_front + 1) % MAX_QUEUE; // Move front to the next request
pthread_mutex_unlock(&queue_lock);
char buffer[MAX_BUFFER] = {0};
ssize_t bytes_received = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
if (bytes_received <= 0) {
perror("Recv failed or client disconnected");
close(client_fd);
continue;
}
char *f = buffer + 5;
char *end = strchr(f, ' ');
if (end) *end = 0;
if (strcmp(f, "") == 0 || strcmp(f, "/") == 0) {
f = "index.html";
}
char file_path[512];
snprintf(file_path, sizeof(file_path), "pages/%s", f);
int fd = open(file_path, O_RDONLY);
if (fd < 0) {
char *response = "HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/html\r\n"
"Content-Length: 86\r\n"
"\r\n"
"<html><body><h1>404 Not Found</h1><p>The requested file does not exist.</p></body></html>";
if (safe_send(client_fd, response, strlen(response)) < 0) {
perror("Send failed");
}
} else {
char *header = "HTTP/1.1 200 OK\r\n\r\n";
if (safe_send(client_fd, header, strlen(header)) < 0) {
perror("Send failed");
}
char file_buffer[MAX_BUFFER];
ssize_t bytes_read;
while ((bytes_read = read(fd, file_buffer, sizeof(file_buffer))) > 0) {
if (safe_send(client_fd, file_buffer, bytes_read) < 0) {
break;
}
}
close(fd);
}
close(client_fd);
}
return NULL;
}
void *accept_clients(void *arg) {
int server_fd = *((int *)arg);
while (!stop_server) {
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_fd < 0) {
perror("Accept failed");
continue;
}
pthread_mutex_lock(&queue_lock);
if ((queue_rear + 1) % MAX_QUEUE == queue_front) {
printf("Queue is full... Rejecting connection\n");
close(client_fd);
} else {
client_queue[queue_rear].client_fd = client_fd;
queue_rear = (queue_rear + 1) % MAX_QUEUE;
pthread_cond_signal(&queue_cond);
}
pthread_mutex_unlock(&queue_lock);
}
return NULL;
}
int main() {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("Socket creation failed");
exit(1);
}
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(8000),
.sin_addr.s_addr = INADDR_ANY
};
if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Bind failed");
close(server_fd);
exit(1);
}
if (listen(server_fd, 10) < 0) {
perror("Listen failed");
close(server_fd);
exit(1);
}
printf("Server is up and running\n");
pthread_mutex_init(&queue_lock, NULL);
pthread_cond_init(&queue_cond, NULL);
pthread_t threads[MAX_THREADS];
for (int i = 0; i < MAX_THREADS; i++) {
if (pthread_create(&threads[i], NULL, handle_client, NULL) != 0) {
perror("Error creating thread");
exit(1);
}
}
pthread_t accept_thread;
if (pthread_create(&accept_thread, NULL, accept_clients, &server_fd) != 0) {
perror("Error creating accept thread");
exit(1);
}
while (!stop_server) {
check_enter_key();
}
for (int i = 0; i < MAX_THREADS; i++) {
pthread_join(threads[i], NULL);
}
pthread_join(accept_thread, NULL);
pthread_mutex_destroy(&queue_lock);
pthread_cond_destroy(&queue_cond);
close(server_fd);
printf("Server stopped.\n");
return 0;
}