This repository was archived by the owner on Apr 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod_static_files.c
More file actions
179 lines (153 loc) · 4.2 KB
/
Copy pathmod_static_files.c
File metadata and controls
179 lines (153 loc) · 4.2 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
/*
* Copyright (c) 2012-2013 Jon Mayo
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "logger.h"
#include "container_of.h"
#include "httpd.h"
#include "module.h"
#include "ext.h"
#include "mod_static_files.h"
struct mod_static_file_info {
struct data app_data;
int fd;
struct stat stat_buf;
const char *content_type;
const char *base;
const char *uri;
};
static void mod_free(struct data *app_data)
{
struct mod_static_file_info *info = container_of(app_data,
struct mod_static_file_info, app_data);
Debug("free %p (info=%p)\n", app_data, info);
assert(app_data != NULL);
close(info->fd);
free(info);
}
static int open_path(struct mod_static_file_info *info,
const char *base, const char *uri)
{
int fd;
char path[PATH_MAX];
int e;
size_t base_len = strlen(base);
/* fix up absolute paths */
while (uri[0] == '/')
uri++;
/* check that there is room for the string, plus an extra '/' */
if (base_len > sizeof(path) - 2)
return -1;
memcpy(path, base, base_len);
/* append a '/' if one is not found */
if (base_len && path[base_len - 1] != '/') {
path[base_len++] = '/';
path[base_len] = 0;
}
e = util_fixpath(path + base_len, sizeof(path) - base_len, uri);
if (e) {
Error("%s:buffer overflow prevented\n", uri);
return -1;
}
Debug("Using path=\"%s\" (%s/%s)\n", path, base, uri);
fd = open(path, O_RDONLY);
if (fd < 0)
goto failure;
if (fstat(fd, &info->stat_buf))
goto close_and_fail;
info->fd = fd;
info->content_type = ext_content_type(uri);
return 0;
close_and_fail:
close(fd);
failure:
Error("%s:%s\n", path, strerror(errno));
return -1;
}
static struct data *mod_start(const char *method, const char *uri,
const char *arg)
{
struct mod_static_file_info *info;
info = calloc(1, sizeof(*info));
if (!info) {
perror(uri);
return NULL;
}
info->fd = -1;
info->app_data.free_data = mod_free;
info->uri = uri;
info->base = arg; /* TODO: parse multiple options in arg */
Debug("module start (arg=\"%s\" uri=\"%s\"\n", arg, uri);
return &info->app_data;
}
static void on_header_done(struct channel *ch, struct data *app_data,
struct env *headers)
{
struct mod_static_file_info *info = container_of(app_data,
struct mod_static_file_info, app_data);
char length_str[20];
char buf[256]; /* TODO: use a bigger buffer size */
off_t total_sent;
if (open_path(info, info->base, info->uri)) {
httpd_response(ch, 404);
httpd_end_headers(ch);
ch_done(ch);
return;
}
httpd_response(ch, 200);
if (info->content_type)
httpd_header(ch, "Content-Type", info->content_type);
else
httpd_header(ch, "Content-Type", "text/plain");
snprintf(length_str, sizeof(length_str), "%lu",
(long)info->stat_buf.st_size);
httpd_header(ch, "Content-Length", length_str);
httpd_end_headers(ch);
/* stream file out */
total_sent = 0;
while (total_sent < info->stat_buf.st_size) {
ssize_t res;
res = read(info->fd, buf, sizeof(buf));
if (res < 0) {
perror(__func__);
ch_done(ch);
return;
}
if (total_sent + res > info->stat_buf.st_size)
res = info->stat_buf.st_size - total_sent;
ch_write(ch, buf, res);
total_sent += res;
}
/* TODO: support persistent */
ch_done(ch);
}
static void on_data(struct channel *ch, struct data *app_data, size_t len,
const void *data)
{
}
const struct module mod_static_files = {
.desc = __FILE__,
.start = mod_start,
.on_header_done = on_header_done,
.on_data = on_data,
};