forked from lpereira/lwan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lwan-serve-files.c
173 lines (145 loc) · 4.83 KB
/
lwan-serve-files.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
/*
* lwan - simple web server
* Copyright (c) 2012 Leandro A. F. Pereira <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "lwan.h"
static lwan_http_status_t
_serve_file_stream(lwan_t* l, lwan_request_t *request, void *data)
{
char headers[512];
lwan_http_status_t return_status;
int file_fd;
struct stat st;
size_t header_len;
if (UNLIKELY((file_fd = open(data, O_RDONLY)) < 0)) {
return_status = (errno == EACCES) ? HTTP_FORBIDDEN : HTTP_NOT_FOUND;
goto end_no_close;
}
if (UNLIKELY(fstat(file_fd, &st) < 0)) {
return_status = (errno == EACCES) ? HTTP_FORBIDDEN : HTTP_NOT_FOUND;
goto end;
}
if (S_ISDIR(st.st_mode)) {
char *index_file;
if (asprintf(&index_file, "%s/index.html", (char *)data) < 0) {
return_status = HTTP_INTERNAL_ERROR;
goto end;
}
close(file_fd);
free(data);
request->response->mime_type = "text/html";
return _serve_file_stream(l, request, index_file);
}
request->response->content_length = st.st_size;
header_len = lwan_prepare_response_header(l, request, HTTP_OK, headers);
if (!header_len) {
return_status = HTTP_INTERNAL_ERROR;
goto end;
}
if (request->method == HTTP_HEAD) {
if (UNLIKELY(write(request->fd, headers, header_len) < 0)) {
perror("write");
return_status = HTTP_INTERNAL_ERROR;
} else
return_status = HTTP_OK;
goto end;
}
lwan_request_set_corked(request, true);
if (UNLIKELY(write(request->fd, headers, header_len) < 0)) {
perror("write");
return_status = HTTP_INTERNAL_ERROR;
goto end_corked;
}
if (UNLIKELY(sendfile(request->fd, file_fd, NULL, st.st_size) < 0))
return_status = HTTP_INTERNAL_ERROR;
else
return_status = HTTP_OK;
end_corked:
lwan_request_set_corked(request, false);
end:
close(file_fd);
end_no_close:
free(data);
free(request->response);
return return_status;
}
lwan_http_status_t
serve_files(lwan_request_t *request, void *root_directory)
{
lwan_response_t *response = NULL;
lwan_http_status_t return_status = HTTP_OK;
char path_to_canonicalize[PATH_MAX];
char *canonical_path;
char *canonical_root;
lwan_request_set_response(request, response);
/* FIXME: ``canonical_root'' should be cached somewhere. */
canonical_root = canonicalize_file_name(root_directory);
if (!canonical_root)
return (errno == EACCES) ? HTTP_FORBIDDEN : HTTP_INTERNAL_ERROR;
if (UNLIKELY(snprintf(path_to_canonicalize, PATH_MAX,
"%s/%s", (char *)root_directory, request->url) < 0)) {
return_status = HTTP_INTERNAL_ERROR;
goto end;
}
canonical_path = canonicalize_file_name(path_to_canonicalize);
if (!canonical_path) {
switch (errno) {
case EACCES:
return_status = HTTP_FORBIDDEN;
goto end;
case ENOENT:
case ENOTDIR:
return_status = HTTP_NOT_FOUND;
goto end;
}
return_status = HTTP_BAD_REQUEST;
goto end;
}
if (strncmp(canonical_path, canonical_root, strlen(canonical_root))) {
free(canonical_path);
return_status = HTTP_FORBIDDEN;
goto end;
}
response = calloc(1, sizeof(*response));
if (!response) {
free(canonical_path);
return_status = HTTP_INTERNAL_ERROR;
goto end_no_reset_stream_content;
}
response->mime_type = (char*)lwan_determine_mime_type_for_file_name(canonical_path);
response->stream_content.callback = _serve_file_stream;
response->stream_content.data = canonical_path;
lwan_request_set_response(request, response);
goto end_no_reset_stream_content;
end:
if (response)
request->response->stream_content.callback = NULL;
end_no_reset_stream_content:
free(canonical_root);
return return_status;
}