-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.c
More file actions
45 lines (37 loc) · 1.17 KB
/
http.c
File metadata and controls
45 lines (37 loc) · 1.17 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
#include "http.h"
#include "utils.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 104857600
void build_http_response(const char *file_name, const char *file_ext, char *response, size_t *response_len) {
const char *mime_type = get_mime_type(file_ext);
char header[256];
snprintf(header, sizeof(header),
"HTTP/1.1 200 OK\r\n"
"Content-Type: %s\r\n"
"\r\n",
mime_type);
int file_fd = open(file_name, O_RDONLY);
if (file_fd == -1) {
snprintf(response, BUFFER_SIZE,
"HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"404 Not Found");
*response_len = strlen(response);
return;
}
struct stat file_stat;
fstat(file_fd, &file_stat);
*response_len = 0;
memcpy(response, header, strlen(header));
*response_len += strlen(header);
ssize_t bytes_read;
while ((bytes_read = read(file_fd, response + *response_len, BUFFER_SIZE - *response_len)) > 0) {
*response_len += bytes_read;
}
close(file_fd);
}