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
|
#include "file.h"
#include <errno.h>
#include <linux/limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define FILE_READBUF_SIZE 1024
const char *file_path_for_uri(const char *uri)
{
struct stat statbuf;
char path[PATH_MAX];
size_t result_len;
char *result;
getcwd(path, PATH_MAX);
strncat(path, uri, PATH_MAX - 1);
/* Append 'index.html' if this is a directory */
stat(path, &statbuf);
if (S_ISDIR(statbuf.st_mode))
strcat(path, "index.html");
/* Allocate a string with only the needed size */
result_len = strlen(path);
result = malloc(result_len + 1);
memcpy(result, path, result_len);
result[result_len] = '\0';
return result;
}
enum serve_method file_method_for_path(const char *filepath, enum http_res_code *code)
{
if (access(filepath, F_OK) != 0) {
*code = HTTP_RESPONSE_NOTFOUND;
return SERVE_METHOD_ERROR;
}
*code = HTTP_RESPONSE_OK;
if (strstr(filepath, ".php") != 0)
return SERVE_METHOD_PHP;
return SERVE_METHOD_FILE;
}
int file_read(const char *filepath, int sockfd)
{
FILE *fp;
char buffer[FILE_READBUF_SIZE];
size_t bytes_read;
const char *content_type = "Content-Type: text/html\r\n\r\n";
fp = fopen(filepath, "rb");
if (fp == NULL) {
perror("Failed to open file");
return 1;
}
write(sockfd, content_type, strlen(content_type));
while ((bytes_read = fread(buffer, 1, FILE_READBUF_SIZE, fp)) > 0)
write(sockfd, buffer, bytes_read);
fclose(fp);
return 0;
}
int file_read_php(const char *filepath, const char *query_str, int sockfd)
{
FILE *fp;
char buffer[FILE_READBUF_SIZE];
size_t bytes_read;
setenv("REQUEST_METHOD", "GET", 1);
setenv("SCRIPT_FILENAME", filepath, 1);
if (query_str)
setenv("QUERY_STRING", query_str, 1);
fp = popen("php-cgi", "r");
if (fp == NULL) {
perror("Failed to read command");
return 1;
}
while ((bytes_read = fread(buffer, 1, FILE_READBUF_SIZE, fp)) > 0)
write(sockfd, buffer, bytes_read);
pclose(fp);
return 0;
}
|