From 4c3dec64f3a940acbfdf80ed7f2442edb25b9843 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Mon, 8 Jun 2020 21:14:49 -0700 Subject: [PATCH] src/rangecgi.c: update to latest for Content-Disposition support With the -f option a "Content-Disposition:" header for an attachement with the specified will be emitted. Signed-off-by: Kyle J. McKay --- src/rangecgi.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/rangecgi.c b/src/rangecgi.c index 8279021..0c4f3be 100644 --- a/src/rangecgi.c +++ b/src/rangecgi.c @@ -1,7 +1,7 @@ /* rangecgi.c -- rangecgi utility to serve multiple files as one with range support -Copyright (C) 2014,2015,2016,2019 Kyle J. McKay +Copyright (C) 2014,2015,2016,2019,2020 Kyle J. McKay All rights reserved This program is free software; you can redistribute it and/or @@ -28,7 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. a "Range:" header with exactly one range. USAGE: - rangecgi ([--etag] | [-c ] [-e ]) [-m 0|1|2] file1 file2 + rangecgi ([--etag] | [-c ] [-f ] [-e ]) [-m 0|1|2] file1 file2 If --etag is given then all environment variables are ignored and the computed ETag value (with the "", but without the "ETag:" prefix part) is @@ -39,6 +39,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -c is given then the specified content type will be used as-is for the returned item. If -e is given then a cache-control and expires header will be output with the expiration set that many days into the future. + If -f is given, then a "Content-Disposition: attachment; + filename=" header will be emitted. If no -f option is + given, then no "Content-Disposition:" header will be emitted! The default is "-m 0" which means use the latest mtime of the given files when computing the ETag value. With "-m 1" always use the mtime from the @@ -117,8 +120,8 @@ static void errorexit_(unsigned code, const char *status, const char *extrahdr) exit(0); } -static void emithdrs(const char *ct, int exp, time_t lm, const char *etag, - bignum tl, int isr, bignum r1, bignum r2) +static void emithdrs(const char *ct, const char *fn, int exp, time_t lm, + const char *etag, bignum tl, int isr, bignum r1, bignum r2) { struct tm gt; char dtstr[32]; @@ -165,6 +168,8 @@ static void emithdrs(const char *ct, int exp, time_t lm, const char *etag, if (!ct || !*ct) ct = "application/octet-stream"; printf("Content-Type: %s\r\n", ct); + if (fn && *fn) + printf("Content-Disposition: attachment; filename=\"%s\"\r\n", fn); printf("%s\r\n", "Vary: Accept-Encoding"); } else { printf("%s\r\n", "Content-Type: text/plain; charset=utf-8; format=fixed"); @@ -175,7 +180,7 @@ static void emithdrs(const char *ct, int exp, time_t lm, const char *etag, static void error416(time_t lm, const char *etag, bignum tl) { - emithdrs(NULL, -1, lm, etag, tl, -1, 0, 0); + emithdrs(NULL, NULL, -1, lm, etag, tl, -1, 0, 0); fflush(stdout); exit(0); } @@ -271,6 +276,7 @@ int main(int argc, char *argv[]) const char *hr = NULL; const char *hir = NULL; const char *ct = NULL; + const char *fn = NULL; int expdays = -1, opt_e = 0; /* "inode_inode-size-time_t_micros" each in hex up to 8 bytes gives */ /* "16bytes_16bytes-16bytes-16bytes" plus NUL = 70 bytes (including "") */ @@ -285,7 +291,7 @@ int main(int argc, char *argv[]) ch = -2; ++optind; } else { - ch = getopt(argc, argv, "c:e:m:"); + ch = getopt(argc, argv, "c:e:f:m:"); } if (ch == -1) break; @@ -296,6 +302,9 @@ int main(int argc, char *argv[]) case 'c': ct = optarg; break; + case 'f': + fn = optarg; + break; case 'e': { int v, n; @@ -321,7 +330,7 @@ int main(int argc, char *argv[]) i = optind; if (isetag) { - if (ct || opt_e) + if (ct || fn || opt_e) exit(2); errorexit = errorfail_; } else { @@ -425,7 +434,7 @@ int main(int argc, char *argv[]) length = tl; } - emithdrs(ct, expdays, lm, etag, tl, hr?1:0, r1, r2); + emithdrs(ct, fn, expdays, lm, etag, tl, hr?1:0, r1, r2); fflush(stdout); if (strcmp(rm, "HEAD")) { -- 2.11.4.GIT