Redirect directory requests to dirplex to always include a trailing slash.
[ashd.git] / src / sendfile.c
CommitLineData
01b2ebce
FT
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <stdlib.h>
20#include <string.h>
21#include <stdio.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <sys/stat.h>
26#include <stdint.h>
b4164ce6 27#include <time.h>
8f728a25 28#include <magic.h>
01b2ebce
FT
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include <utils.h>
34#include <log.h>
35#include <resp.h>
36
0328ac04
FT
37#ifdef HAVE_XATTR
38#include <attr/xattr.h>
39#endif
40
8f728a25
FT
41static magic_t cookie = NULL;
42
01b2ebce
FT
43static void passdata(int in, int out)
44{
45 int ret, len, off;
46 char *buf;
47
48 buf = smalloc(65536);
49 while(1) {
50 len = read(in, buf, 65536);
51 if(len < 0) {
52 flog(LOG_ERR, "sendfile: could not read input: %s", strerror(errno));
53 break;
54 }
55 if(len == 0)
56 break;
57 for(off = 0; off < len; off += ret) {
58 ret = write(out, buf + off, len - off);
59 if(ret < 0) {
60 flog(LOG_ERR, "sendfile: could not write output: %s", strerror(errno));
61 break;
62 }
63 }
64 }
65 free(buf);
66}
67
0328ac04
FT
68static char *attrmimetype(char *file)
69{
70#ifdef HAVE_XATTR
71 static char buf[1024];
72 int i;
73 ssize_t sz;
74
75 if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0)
76 goto found;
77 if((sz = getxattr(file, "user.mime-type", buf, sizeof(buf) - 1)) > 0)
78 goto found;
79 if((sz = getxattr(file, "user.mime_type", buf, sizeof(buf) - 1)) > 0)
80 goto found;
81 if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0)
82 goto found;
83 return(NULL);
84found:
85 for(i = 0; i < sz; i++) {
86 if((buf[sz] < 32) || (buf[sz] >= 128))
87 return(NULL);
88 }
89 buf[sz] = 0;
90 return(buf);
91#else
92 return(NULL);
93#endif
94}
95
8f728a25 96static const char *getmimetype(char *file, struct stat *sb)
01b2ebce 97{
8f728a25
FT
98 const char *ret;
99
0328ac04
FT
100 if((ret = attrmimetype(file)) != NULL)
101 return(ret);
8f728a25
FT
102 if(cookie == NULL) {
103 cookie = magic_open(MAGIC_MIME_TYPE);
104 magic_load(cookie, NULL);
105 }
106 if((ret = magic_file(cookie, file)) != NULL)
107 return(ret);
01b2ebce
FT
108 return("application/octet-stream");
109}
110
b4164ce6
FT
111static void checkcache(char *file, struct stat *sb)
112{
113 char *hdr;
114
115 if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
116 if(parsehttpdate(hdr) < sb->st_mtime)
117 return;
81cfca6c
FT
118 printf("HTTP/1.1 304 Not Modified\n");
119 printf("Date: %s\n", fmthttpdate(time(NULL)));
120 printf("Content-Length: 0\n");
121 printf("\n");
b4164ce6
FT
122 exit(0);
123 }
124}
125
289c63fc
FT
126static void usage(void)
127{
128 flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] METHOD URL REST");
129}
130
01b2ebce
FT
131int main(int argc, char **argv)
132{
289c63fc 133 int c;
01b2ebce
FT
134 char *file;
135 struct stat sb;
136 int fd;
8f728a25 137 const char *contype;
01b2ebce 138
289c63fc
FT
139 contype = NULL;
140 while((c = getopt(argc, argv, "c:")) >= 0) {
141 switch(c) {
142 case 'c':
143 contype = optarg;
144 break;
145 default:
146 usage();
147 exit(1);
148 break;
149 }
150 }
151 if(argc - optind < 3) {
152 usage();
01b2ebce
FT
153 exit(1);
154 }
155 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
156 flog(LOG_ERR, "sendfile: needs to be called with the X-Ash-File header");
157 exit(1);
158 }
289c63fc 159 if(*argv[optind + 2]) {
01b2ebce
FT
160 simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
161 exit(0);
162 }
163 if(stat(file, &sb) || ((fd = open(file, O_RDONLY)) < 0)) {
164 flog(LOG_ERR, "sendfile: could not stat input file %s: %s", file, strerror(errno));
165 simpleerror(1, 500, "Internal Error", "The server could not access its own data.");
166 exit(1);
167 }
289c63fc
FT
168 if(contype == NULL)
169 contype = getmimetype(file, &sb);
01b2ebce 170
b4164ce6
FT
171 checkcache(file, &sb);
172
81cfca6c
FT
173 printf("HTTP/1.1 200 OK\n");
174 printf("Content-Type: %s\n", contype);
175 printf("Content-Length: %ji\n", (intmax_t)sb.st_size);
176 printf("Last-Modified: %s\n", fmthttpdate(sb.st_mtime));
177 printf("Date: %s\n", fmthttpdate(time(NULL)));
178 printf("\n");
01b2ebce 179 fflush(stdout);
289c63fc 180 if(strcasecmp(argv[optind], "head"))
01b2ebce
FT
181 passdata(fd, 1);
182 return(0);
183}