Index: usr.sbin/httpd/httpd.conf.5 =================================================================== RCS file: /cvs/src/usr.sbin/httpd/httpd.conf.5,v diff -u -p -u -r1.137 httpd.conf.5 --- usr.sbin/httpd/httpd.conf.5 2 Oct 2026 04:47:07 -0000 1.137 +++ usr.sbin/httpd/httpd.conf.5 20 Oct 2026 18:39:52 -0000 @@ -492,6 +492,15 @@ Enable static gzip compression to save b If gzip encoding is accepted and if the requested file exists with an additional .gz suffix, use the compressed file instead and deliver it with content encoding gzip. +.It Ic brotli-static +Enable static brotli compression to save bandwidth. +.Pp +If brotli encoding is accepted and if the requested file exists with +a .br suffix and if and the client is connected with TLS, use the compressed +file instead and deliver it with content encoding br. +.Pp +If both brotli-static and gzip-static are enabled, brotli is preferred +if the above conditions are satisfied. .It Ic header Ar option Manipulate HTTP response headers. Multiple Index: usr.sbin/httpd/httpd.h =================================================================== RCS file: /cvs/src/usr.sbin/httpd/httpd.h,v diff -u -p -u -r1.185 httpd.h --- usr.sbin/httpd/httpd.h 2 Oct 2026 04:47:07 -0000 1.185 +++ usr.sbin/httpd/httpd.h 20 Oct 2026 18:39:52 -0000 @@ -1,4 +1,4 @@ -/* $OpenBSD: httpd.h,v 1.185 2026/10/02 04:47:07 rsadowski Exp $ */ +/* $OpenBSD: httpd.h,v 1.188 2026/10/15 10:59:00 rsadowski Exp $ */ /* * Copyright (c) 2006 - 2015 Reyk Floeter @@ -373,6 +373,8 @@ SPLAY_HEAD(client_tree, client); #define SRVFLAG_LOCATION_NOT_FOUND 0x0000000080000000ULL #define SRVFLAG_STATIC_CACHE_CONTROL 0x0000000100000000ULL #define SRVFLAG_NO_STATIC_CACHE_CONTROL 0x0000000200000000ULL +#define SRVFLAG_BROTLI_STATIC 0x0000000400000000ULL +#define SRVFLAG_NO_BROTLI_STATIC 0x0000000800000000ULL #define SRVFLAG_BITS \ "\10\01INDEX\02NO_INDEX\03AUTO_INDEX\04NO_AUTO_INDEX" \ @@ -382,7 +384,8 @@ SPLAY_HEAD(client_tree, client); "\26SERVER_MATCH\27SERVER_HSTS\30DEFAULT_TYPE\31PATH_REWRITE" \ "\32NO_PATH_REWRITE\34NO_BANNER\33GZIP_STATIC" \ "\35NO_GZIP_STATIC\37LOCATION_FOUND\40LOCATION_NOT_FOUND" \ - "\41STATIC_CACHE_CONTROL\42NO_STATIC_CACHE_CONTROL" + "\41STATIC_CACHE_CONTROL\42NO_STATIC_CACHE_CONTROL" \ + "\43BROTLI_STATIC\44NO_BROTLI_STATIC" #define TCPFLAG_NODELAY 0x01 #define TCPFLAG_NNODELAY 0x02 Index: usr.sbin/httpd/parse.y =================================================================== RCS file: /cvs/src/usr.sbin/httpd/parse.y,v diff -u -p -u -r1.139 parse.y --- usr.sbin/httpd/parse.y 2 Oct 2026 04:47:07 -0000 1.139 +++ usr.sbin/httpd/parse.y 20 Oct 2026 18:39:52 -0000 @@ -139,7 +139,7 @@ typedef struct { %} %token ACCESS ADD ALIAS ALWAYS AUTHENTICATE AUTO -%token BACKLOG BANNER BLOCK BODY BUFFER +%token BACKLOG BANNER BLOCK BODY BROTLISTATIC BUFFER %token CA CERTIFICATE CHROOT CIPHERS CLIENT COMBINED COMMON CONNECTION CRL %token DEFAULT DHE DIRECTORY DROP %token ECDHE ERR ERRDOCS ERROR @@ -582,6 +582,7 @@ serveroptsl : LISTEN ON STRING opttls po | fastcgi | authenticate | gzip_static + | brotli_static | filter | LOCATION optfound optmatch STRING { struct server *s; @@ -1477,6 +1478,16 @@ gzip_static : NO GZIPSTATIC { } ; +brotli_static : NO BROTLISTATIC { + srv->srv_conf.flags &= ~SRVFLAG_BROTLI_STATIC; + srv->srv_conf.flags |= SRVFLAG_NO_BROTLI_STATIC; + } + | BROTLISTATIC { + srv->srv_conf.flags |= SRVFLAG_BROTLI_STATIC; + srv->srv_conf.flags &= ~SRVFLAG_NO_BROTLI_STATIC; + } + ; + static_cache_control : NO STATIC_CACHE_CONTROL { srv_conf->flags &= ~SRVFLAG_STATIC_CACHE_CONTROL; srv_conf->flags |= SRVFLAG_NO_STATIC_CACHE_CONTROL; @@ -1694,6 +1705,7 @@ lookup(char *s) { "banner", BANNER }, { "block", BLOCK }, { "body", BODY }, + { "brotli-static", BROTLISTATIC }, { "buffer", BUFFER }, { "ca", CA }, { "certificate", CERTIFICATE }, Index: usr.sbin/httpd/server_file.c =================================================================== RCS file: /cvs/src/usr.sbin/httpd/server_file.c,v diff -u -p -u -r1.84 server_file.c --- usr.sbin/httpd/server_file.c 26 Jul 2026 14:46:32 -0000 1.84 +++ usr.sbin/httpd/server_file.c 20 Oct 2026 18:39:52 -0000 @@ -55,6 +55,8 @@ int server_file_method(struct client * int parse_range_spec(char *, off_t, struct range *); int parse_ranges(struct client *, char *, off_t); static int select_visible(const struct dirent *); +static int find_compressed_path(const struct client *, const char *, + int *, struct stat *); int server_file_access(struct httpd *env, struct client *clt, @@ -168,44 +170,10 @@ server_file_access(struct httpd *env, st fd, &st, r->kv_value)); } - /* change path to path.gz if necessary. */ - if (srv_conf->flags & SRVFLAG_GZIP_STATIC) { - struct http_descriptor *req = clt->clt_descreq; - struct http_descriptor *resp = clt->clt_descresp; - struct stat gzst; - int gzfd; - char gzpath[PATH_MAX]; - - /* check Accept-Encoding header */ - key.kv_key = "Accept-Encoding"; - r = kv_find(&req->http_headers, &key); - - if (r != NULL && strstr(r->kv_value, "gzip") != NULL) { - /* append ".gz" to path and check existence */ - ret = snprintf(gzpath, sizeof(gzpath), "%s.gz", path); - if (ret < 0 || (size_t)ret >= sizeof(gzpath)) { - close(fd); - return (500); - } - - if ((gzfd = open(gzpath, O_RDONLY)) != -1) { - /* .gz must be a file, and not older */ - if (fstat(gzfd, &gzst) != -1 && - S_ISREG(gzst.st_mode) && - timespeccmp(&gzst.st_mtim, &st.st_mtim, - >=)) { - kv_add(&resp->http_headers, - "Content-Encoding", "gzip"); - /* Use original file timestamp */ - gzst.st_mtim = st.st_mtim; - st = gzst; - close(fd); - fd = gzfd; - } else { - close(gzfd); - } - } - } + /* Point fd and st at path.br or .gz if appropriate. */ + if ((ret = find_compressed_path(clt, path, &fd, &st)) != 0) { + close(fd); + return (ret); } return (server_file_request(env, clt, media, fd, &st)); @@ -853,4 +821,109 @@ parse_range_spec(char *str, off_t size, return (0); return (1); +} + +static int +find_compressed_path(const struct client *clt, const char *path, int *fd, + struct stat *st) +{ + struct server_config *srv_conf = clt->clt_srv_conf; + struct http_descriptor *req = clt->clt_descreq; + struct http_descriptor *resp = clt->clt_descresp; + struct stat brst; + struct stat gzst; + struct kv *r, key; + int ret; + int brfd = -1; + int gzfd = -1; + char brpath[PATH_MAX]; + char gzpath[PATH_MAX]; + + key.kv_key = "Accept-Encoding"; + r = kv_find(&req->http_headers, &key); + + /* + * Look for path.br if brotli-static is set, + * and the client accepts brotli, and the connection is inside TLS. + */ + if ((srv_conf->flags & SRVFLAG_BROTLI_STATIC) && + r != NULL && + strstr(r->kv_value, "br") != NULL && + clt->clt_tls_ctx != NULL) { + /* Append .br... */ + ret = snprintf(brpath, sizeof(brpath), "%s.br", path); + if (ret < 0 || (size_t)ret >= sizeof(brpath)) { + return (500); + } + /* ...and check existence. */ + if ((brfd = open(brpath, O_RDONLY)) != -1) { + if (fstat(brfd, &brst) == -1) { + close(brfd); + return (500); + } + } + } + + /* Likewise for path.gz, minus TLS requirement. */ + if ((srv_conf->flags & SRVFLAG_GZIP_STATIC) && + r != NULL && + strstr(r->kv_value, "gzip") != NULL) { + ret = snprintf(gzpath, sizeof(gzpath), "%s.gz", path); + if (ret < 0 || (size_t)ret >= sizeof(gzpath)) { + /* brfd might be open here. */ + if (brfd != -1) + close(brfd); + return (500); + } + if ((gzfd = open(gzpath, O_RDONLY)) != -1) { + if (fstat(gzfd, &gzst) == -1) { + /* brfd might be open here. */ + if (brfd != -1) + close(brfd); + close(gzfd); + return (500); + } + } + } + + /* + * Serve path.br if it's not older then the base file + * by pointing *fd at brfd. + * + * Else serve path.gz if it's not older then path. + */ + if (brfd != -1 && + S_ISREG(brst.st_mode) && + timespeccmp(&brst.st_mtim, &st->st_mtim, >=)) + { + kv_add(&resp->http_headers, + "Content-Encoding", "br"); + /* Use original file timestamp. */ + brst.st_mtim = st->st_mtim; + *st = brst; + close(*fd); + *fd = brfd; + brfd = -1; + } else if (gzfd != -1 && + S_ISREG(gzst.st_mode) && + timespeccmp(&gzst.st_mtim, &st->st_mtim, >=)) + { + kv_add(&resp->http_headers, + "Content-Encoding", "gzip"); + /* Use original file timestamp. */ + gzst.st_mtim = st->st_mtim; + *st = gzst; + close(*fd); + *fd = gzfd; + gzfd = -1; + } + + /* + * brfd and gzfd could both be open here if they + * exist but are older than the uncompressed version. + */ + if (brfd != -1) close(brfd); + if (gzfd != -1) close(gzfd); + + return (0); }