Made it possible to specify the content-type to sendfile.
[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>
01b2ebce
FT
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include <utils.h>
33#include <log.h>
34#include <resp.h>
35
36static void passdata(int in, int out)
37{
38 int ret, len, off;
39 char *buf;
40
41 buf = smalloc(65536);
42 while(1) {
43 len = read(in, buf, 65536);
44 if(len < 0) {
45 flog(LOG_ERR, "sendfile: could not read input: %s", strerror(errno));
46 break;
47 }
48 if(len == 0)
49 break;
50 for(off = 0; off < len; off += ret) {
51 ret = write(out, buf + off, len - off);
52 if(ret < 0) {
53 flog(LOG_ERR, "sendfile: could not write output: %s", strerror(errno));
54 break;
55 }
56 }
57 }
58 free(buf);
59}
60
61static int strrcmp(char *str, char *end)
62{
63 return(strcmp(str + strlen(str) - strlen(end), end));
64}
65
66static char *getmimetype(char *file, struct stat *sb)
67{
68 /* Rewrite with libmagic. */
69 if(!strrcmp(file, ".html"))
70 return("text/html");
71 if(!strrcmp(file, ".xhtml"))
72 return("application/xhtml+xml");
73 if(!strrcmp(file, ".txt"))
74 return("text/plain");
ee1b55f6
FT
75 if(!strrcmp(file, ".css"))
76 return("text/css");
b4164ce6
FT
77 if(!strrcmp(file, ".py"))
78 return("text/plain");
79 if(!strrcmp(file, ".c"))
80 return("text/plain");
01b2ebce
FT
81 return("application/octet-stream");
82}
83
b4164ce6
FT
84static void checkcache(char *file, struct stat *sb)
85{
86 char *hdr;
87
88 if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
89 if(parsehttpdate(hdr) < sb->st_mtime)
90 return;
91 printf("HTTP/1.1 304 Not Modified\r\n");
92 printf("Date: %s\r\n", fmthttpdate(time(NULL)));
93 printf("Content-Length: 0\r\n");
94 printf("\r\n");
95 exit(0);
96 }
97}
98
289c63fc
FT
99static void usage(void)
100{
101 flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] METHOD URL REST");
102}
103
01b2ebce
FT
104int main(int argc, char **argv)
105{
289c63fc 106 int c;
01b2ebce
FT
107 char *file;
108 struct stat sb;
109 int fd;
289c63fc 110 char *contype;
01b2ebce 111
289c63fc
FT
112 contype = NULL;
113 while((c = getopt(argc, argv, "c:")) >= 0) {
114 switch(c) {
115 case 'c':
116 contype = optarg;
117 break;
118 default:
119 usage();
120 exit(1);
121 break;
122 }
123 }
124 if(argc - optind < 3) {
125 usage();
01b2ebce
FT
126 exit(1);
127 }
128 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
129 flog(LOG_ERR, "sendfile: needs to be called with the X-Ash-File header");
130 exit(1);
131 }
289c63fc 132 if(*argv[optind + 2]) {
01b2ebce
FT
133 simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
134 exit(0);
135 }
136 if(stat(file, &sb) || ((fd = open(file, O_RDONLY)) < 0)) {
137 flog(LOG_ERR, "sendfile: could not stat input file %s: %s", file, strerror(errno));
138 simpleerror(1, 500, "Internal Error", "The server could not access its own data.");
139 exit(1);
140 }
289c63fc
FT
141 if(contype == NULL)
142 contype = getmimetype(file, &sb);
01b2ebce 143
b4164ce6
FT
144 checkcache(file, &sb);
145
01b2ebce 146 printf("HTTP/1.1 200 OK\r\n");
289c63fc 147 printf("Content-Type: %s\r\n", contype);
01b2ebce 148 printf("Content-Length: %ji\r\n", (intmax_t)sb.st_size);
b4164ce6
FT
149 printf("Last-Modified: %s\r\n", fmthttpdate(sb.st_mtime));
150 printf("Date: %s\r\n", fmthttpdate(time(NULL)));
01b2ebce
FT
151 printf("\r\n");
152 fflush(stdout);
289c63fc 153 if(strcasecmp(argv[optind], "head"))
01b2ebce
FT
154 passdata(fd, 1);
155 return(0);
156}