-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
225 lines (191 loc) · 5.98 KB
/
Copy pathmain.c
File metadata and controls
225 lines (191 loc) · 5.98 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
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <strings.h>
#include <signal.h>
#include <zlib.h>
void gzip_compress(const char *input, size_t input_len, char *output, size_t *output_len) {
z_stream zs;
memset(&zs, 0, sizeof(zs));
if (deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
printf("Gzip init failed\n");
return;
}
zs.next_in = (Bytef *)input;
zs.avail_in = input_len;
zs.next_out = (Bytef *)output;
zs.avail_out = *output_len;
deflate(&zs, Z_FINISH);
*output_len = zs.total_out;
deflateEnd(&zs);
}
int main() {
setbuf(stdout, NULL);
setbuf(stderr, NULL);
signal(SIGCHLD, SIG_IGN);
char *base_dir = getenv("HOME");
if (base_dir == NULL)
base_dir = ".";
printf("Server is running. Serving files from home: %s\n", base_dir);
int server_fd, client_addr_len;
struct sockaddr_in client_addr;
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
printf("Socket creation failed: %s...\n", strerror(errno));
return 1;
}
int reuse = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
printf("SO_REUSEADDR failed: %s \n", strerror(errno));
return 1;
}
struct sockaddr_in serv_addr = { .sin_family = AF_INET ,
.sin_port = htons(4221),
.sin_addr = { htonl(INADDR_ANY) },
};
if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) {
printf("Bind failed: %s \n", strerror(errno));
return 1;
}
int connection_backlog = 5;
if (listen(server_fd, connection_backlog) != 0) {
printf("Listen failed: %s \n", strerror(errno));
return 1;
}
printf("Waiting for a client to connect...\n");
client_addr_len = sizeof(client_addr);
int cnt = 0;
while(1) {
int client_fd = accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);
if (client_fd == -1) {
perror("Accept failed");
continue;
}
cnt++;
printf("\nClient %d connected\n", cnt);
if (fork() == 0) {
close(server_fd);
struct timeval tv;
tv.tv_sec = 10;
tv.tv_usec = 0;
if (setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv) < 0)
perror("Setsockopt failed");
char buffer[4096];
while (1) {
int valrd = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
if (valrd <= 0) {
printf("Client %d disconnected.\n", cnt);
break;
}
buffer[valrd] = '\0';
char content_type[101] = "text/plain";
char body_send[1024] = "", status[101];
char content_encoding[101] = "", *body, *final_body;
char connection[101] = "";
unsigned long content_length = 0;
int needs_free = 0;
char method[16], path[256], version[16];
sscanf(buffer, "%s %s %s", method, path, version);
body = strstr(buffer, "\r\n\r\n");
if (body)
body += 4;
else
body = "";
strcpy(connection, "keep-alive");
char full_path[1024];
snprintf(full_path, sizeof(full_path), "%s%s", base_dir, path);
if (strncmp(method, "POST", 4) == 0) {
FILE *f = fopen(full_path, "w");
if (f) {
fwrite(body, 1, strlen(body), f);
strcpy(status, "HTTP/1.1 201 Created");
fclose(f);
} else {
strcpy(status, "HTTP/1.1 403 Forbidden");
}
} else {
if (strcmp(path, "/") == 0) {
strcpy(status, "HTTP/1.1 200 OK");
} else if (strcasestr(buffer, "Dir:")) {
FILE *f = fopen(full_path, "r");
if (f == NULL) {
strcpy(status, "HTTP/1.1 404 Not Found");
printf("FILE NOT FOUND");
} else {
size_t r = fread(body_send, 1, sizeof(body_send), f);
char header[256];
strcpy(status, "HTTP/1.1 200 OK");
strcpy(content_type, "application/octet-stream");
fclose(f);
content_length = r;
}
} else if (strstr(path, "/echo/")) {
char *msj = path + 6;
strcpy(status, "HTTP/1.1 200 OK");
strcpy(content_type, "text/plain");
content_length = strlen(msj);
strcpy(body_send, msj);
} else if (strcmp(path, "/user-agent") == 0){
char *p = strcasestr(buffer, "User-Agent:");
if (p) {
p += 12;
strcpy(status, "HTTP/1.1 200 OK");
strcpy(content_type, "text/plain");
char *fin = strstr(p, "\r\n");
int len = fin ? (fin - p) : strlen(p);
strncpy(body_send, p, len);
body_send[len] = '\0';
content_length = len;
} else {
strcpy(status, "HTTP/1.1 400 Bad Request");
}
} else {
strcpy(status, "HTTP/1.1 404 Not Found");
}
final_body = body_send;
if (strcasestr(buffer, "Accept-Encoding") && strcasestr(strcasestr(buffer, "Accept-Encoding:"), "gzip")) {
strcpy(content_encoding, "Content-Encoding: gzip\r\n");
unsigned long compressed_len = compressBound(content_length);
char *compressed_body = calloc(compressed_len, sizeof(char));
gzip_compress(body_send, content_length, compressed_body, &compressed_len);
if (compressed_len > 0) {
final_body = compressed_body;
content_length = compressed_len;
needs_free = 1;
}
else {
free(compressed_body);
}
}
if (strcasestr(buffer, "Connection: close"))
strcpy(connection, "close");
}
char header_buffer[1024];
int header_len = sprintf(header_buffer, "%s\r\n%sContent-Type: %s\r\nContent-Length: %zu\r\nConnection: %s\r\n\r\n",
status, content_encoding, content_type, content_length, connection);
send(client_fd, header_buffer, header_len, 0);
if (content_length > 0) {
send(client_fd, final_body, content_length, 0);
}
if (needs_free == 1)
free(final_body);
if (strstr(connection, "close")) {
printf("Connection with client %d is closing as requested in header.", cnt);
break;
}
}
exit(0);
close(client_fd);
} else {
close(client_fd);
}
}
close(server_fd);
return 0;
}