-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path01-cache-control-headers.patch
198 lines (183 loc) · 5.82 KB
/
01-cache-control-headers.patch
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
Copyright (c) 2020 - 2024 Matthias Pressfreund
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.
Index: usr.sbin/httpd/http.h
@@ -1,6 +1,7 @@
/* $OpenBSD: http.h,v 1.17 2024/03/24 10:53:27 job Exp $ */
/*
+ * Copyright (c) 2020 Matthias Pressfreund
* Copyright (c) 2012 - 2015 Reyk Floeter <reyk@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
@@ -209,6 +210,7 @@
char *media_name;
char *media_type;
char *media_subtype;
+ char *media_cache;
};
/*
* Some default media types based on (2014-08-04 version):
Index: usr.sbin/httpd/httpd.conf.5
@@ -754,16 +754,19 @@
section must include one or more lines of the following syntax,
enclosed in curly braces:
.Bl -tag -width Ds
-.It Ar type/subtype Ar name Op Ar name ...
+.It Ar type/subtype Oo Bro Ic cache Ar string Brc Oc Ar name ...
Set the media
.Ar type
and
.Ar subtype
to the specified extension
.Ar name .
-One or more names can be specified per line.
-Each line may end with an optional semicolon.
-Later lines overwrite earlier lines.
+One or more names can be specified per entry.
+If required, a
+.Ic cache
+control header string may be specified.
+Each entry may end with an optional semicolon.
+Later entries overwrite earlier entries.
.It Ic include Ar file
Include types definitions from an external file, for example
.Pa /usr/share/misc/mime.types .
@@ -794,14 +797,14 @@
}
types {
- text/css css
- text/html html htm
- text/plain txt
- image/gif gif
- image/jpeg jpeg jpg
- image/png png
- application/javascript js
- application/xml xml
+ text/css css
+ text/html html htm
+ text/plain txt
+ image/gif gif
+ image/jpeg { cache "max-age=2592000, public" } jpeg jpg
+ image/png png
+ application/javascript js
+ application/xml xml
}
.Ed
.Pp
Index: usr.sbin/httpd/httpd.h
@@ -1,6 +1,7 @@
/* $OpenBSD: httpd.h,v 1.165 2024/10/08 05:28:11 jsg Exp $ */
/*
+ * Copyright (c) 2020 Matthias Pressfreund
* Copyright (c) 2006 - 2015 Reyk Floeter <reyk@openbsd.org>
* Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -57,7 +58,8 @@
#define HTTPD_MAX_ALIAS_IP 16
#define HTTPD_REALM_MAX 255
#define HTTPD_LOCATION_MAX 255
-#define HTTPD_DEFAULT_TYPE { "bin", "application", "octet-stream", NULL }
+#define HTTPD_DEFAULT_TYPE { \
+ "bin", "application", "octet-stream", "", NULL }
#define HTTPD_LOGVIS VIS_NL|VIS_TAB|VIS_CSTYLE
#define HTTPD_TLS_CERT "/etc/ssl/server.crt"
#define HTTPD_TLS_KEY "/etc/ssl/private/server.key"
@@ -89,6 +91,7 @@
#define MEDIATYPE_NAMEMAX 128 /* file name extension */
#define MEDIATYPE_TYPEMAX 64 /* length of type/subtype */
+#define MEDIATYPE_CACHEMAX 128 /* length of cache control */
#define CONFIG_RELOAD 0x00
#define CONFIG_MEDIA 0x01
@@ -442,6 +445,7 @@
char media_name[MEDIATYPE_NAMEMAX];
char media_type[MEDIATYPE_TYPEMAX];
char media_subtype[MEDIATYPE_TYPEMAX];
+ char media_cache[MEDIATYPE_CACHEMAX];
char *media_encoding;
RB_ENTRY(media_type) media_entry;
};
Index: usr.sbin/httpd/parse.y
@@ -141,7 +141,7 @@
%token TIMEOUT TLS TYPE TYPES HSTS MAXAGE SUBDOMAINS DEFAULT PRELOAD REQUEST
%token ERROR INCLUDE AUTHENTICATE WITH BLOCK DROP RETURN PASS REWRITE
%token CA CLIENT CRL OPTIONAL PARAM FORWARDED FOUND NOT
-%token ERRDOCS GZIPSTATIC
+%token ERRDOCS GZIPSTATIC CACHE
%token <v.string> STRING
%token <v.number> NUMBER
%type <v.port> port
@@ -1284,7 +1284,7 @@
| mediaoptsl nl
;
-mediaoptsl : mediastring medianames_l optsemicolon
+mediaoptsl : mediastring optmediacache medianames_l optsemicolon
| include
;
@@ -1305,6 +1305,24 @@
}
;
+optmediacache : /* empty */ {
+ *media.media_cache = '\0';
+ }
+ | '{' optnl mediacache '}'
+ ;
+
+mediacache : CACHE STRING optnl {
+ if (strlcpy(media.media_cache, $2,
+ sizeof(media.media_cache)) >=
+ sizeof(media.media_cache)) {
+ yyerror("cache string too long");
+ free($2);
+ YYERROR;
+ }
+ free($2);
+ }
+ ;
+
medianames_l : medianames_l medianamesl
| medianamesl
;
@@ -1432,6 +1450,7 @@
{ "body", BODY },
{ "buffer", BUFFER },
{ "ca", CA },
+ { "cache", CACHE },
{ "certificate", CERTIFICATE },
{ "chroot", CHROOT },
{ "ciphers", CIPHERS },
@@ -1948,6 +1967,7 @@
(void)strlcpy(m.media_subtype,
mediatypes[i].media_subtype,
sizeof(m.media_subtype));
+ *m.media_cache = '\0';
m.media_encoding = NULL;
if (media_add(conf->sc_mediatypes, &m) == NULL) {
Index: usr.sbin/httpd/server_http.c
@@ -1547,7 +1547,7 @@
struct http_descriptor *desc = clt->clt_descreq;
struct http_descriptor *resp = clt->clt_descresp;
const char *error;
- struct kv *ct, *cl;
+ struct kv *ct, *cl, *cc;
char tmbuf[32];
if (desc == NULL || media == NULL ||
@@ -1583,6 +1583,12 @@
if (size >= 0 && ((cl =
kv_add(&resp->http_headers, "Content-Length", NULL)) == NULL ||
kv_set(cl, "%lld", (long long)size) == -1))
+ return (-1);
+
+ /* Set cache control, if specified */
+ if (*media->media_cache != '\0' && ((cc =
+ kv_add(&resp->http_headers, "Cache-Control", NULL)) == NULL ||
+ kv_set(cc, "%s", media->media_cache) == -1))
return (-1);
/* Set last modification time */