From 66a6182a403c0093798d3326c362bbf36e65bf5f Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 30 Apr 2012 23:58:15 -0400 Subject: [PATCH] Add cache-control headers to hopefully disable caching on streaming responses; untested so far --- httpd.ml | 6 ++++++ httpd_date.ml | 29 +++++++++++++++++++++++++++++ ui_relay.ml | 6 ++++-- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 httpd_date.ml diff --git a/httpd.ml b/httpd.ml index cdf6fb0..d1404ff 100644 --- a/httpd.ml +++ b/httpd.ml @@ -56,6 +56,12 @@ let content_type_header_name = "Content-Type" let html_content_type_header = (content_type_header_name, html_content_type) let text_content_type_header = (content_type_header_name, text_content_type) +let disable_cache_headers () = + ["Expires", "Thu, 01 Jan 1981 00:00:00 GMT"; + "Last-Modified", Httpd_date.http_gmtime (Unix.time ()); + "Cache-Control", "no-cache, must-revalidate, max-age=0"; + "Pragma", "no-cache"] + let add_completion_callback resp cb = {resp with completion_callbacks = cb :: resp.completion_callbacks} diff --git a/httpd_date.ml b/httpd_date.ml new file mode 100644 index 0000000..4304001 --- /dev/null +++ b/httpd_date.ml @@ -0,0 +1,29 @@ +(* Copyright 2012 Tony Garnock-Jones . *) + +(* This file is part of Ocamlmsg. *) + +(* Ocamlmsg is free software: you can redistribute it and/or modify it *) +(* under the terms of the GNU General Public License as published by the *) +(* Free Software Foundation, either version 3 of the License, or (at your *) +(* option) any later version. *) + +(* Ocamlmsg is distributed in the hope that it will be useful, but *) +(* WITHOUT ANY WARRANTY; without even the implied warranty of *) +(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *) +(* General Public License for more details. *) + +(* You should have received a copy of the GNU General Public License *) +(* along with Ocamlmsg. If not, see . *) + +open Unix + +let days = ["Sun"; "Mon"; "Tue"; "Wed"; "Thu"; "Fri"; "Sat"] +let months = ["Jan"; "Feb"; "Mar"; "Apr"; "May"; "Jun"; "Jul"; "Aug"; "Sep"; "Oct"; "Nov"; "Dec"] + +(* Example: *) +(* Wed, 15 Nov 1995 06:25:24 GMT *) +let http_gmtime t = + let tm = gmtime t in + Printf.sprintf "%s, %02d %s %04d %02d:%02d:%02d GMT" + (List.nth days tm.tm_wday) tm.tm_mday (List.nth months tm.tm_mon) (tm.tm_year + 1900) + tm.tm_hour tm.tm_min tm.tm_sec diff --git a/ui_relay.ml b/ui_relay.ml index 0e43708..e2b3789 100644 --- a/ui_relay.ml +++ b/ui_relay.ml @@ -46,8 +46,10 @@ let rec api_tap_source id r = Sexp.Str "", Sexp.Str "")); Httpd.add_completion_callback (Httpd.resp_generic 200 "Streaming" - [Httpd.text_content_type_header; - "Access-Control-Allow-Origin", "*"] + ([Httpd.text_content_type_header; + "Access-Control-Allow-Origin", "*"; + "Date", Httpd_date.http_gmtime (Unix.time ())] + @ Httpd.disable_cache_headers ()) (Httpd.Variable (Stringstream.switch_after 131072 (Stringstream.seq id_block_and_padding (Stringstream.make (message_stream ch)))