From 46379956b305037df7599985ecc29ae9006d2b7b Mon Sep 17 00:00:00 2001
From: Paul Duncan <pabs@pablotron.org>
Date: Mon, 3 Sep 2018 11:03:39 -0400
Subject: add HTTPResponseWriter#get_headers()

---
 src/ZipStream.php | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

(limited to 'src')

diff --git a/src/ZipStream.php b/src/ZipStream.php
index 8648b9f..3f2d480 100644
--- a/src/ZipStream.php
+++ b/src/ZipStream.php
@@ -201,11 +201,13 @@ final class HTTPResponseWriter implements Writer {
   /**
    * Flush metadata and begin streaming archive contents.
    *
-   * @todo
    * @return void
    */
   public function open() : void {
-    # TODO: send http headers
+    # write response headers
+    foreach ($this->get_headers as $key => $val) {
+      header("$key: $val");
+    }
   }
 
   /**
@@ -227,6 +229,35 @@ final class HTTPResponseWriter implements Writer {
   public function close() : void {
     # ignore
   }
+
+  /**
+   * Get HTTP headers.
+   *
+   * @internal
+   *
+   * @return array Hash of HTTP headers.
+   */
+  private function get_headers() : array {
+    # build pre-RFC6266 file name
+    $old_name = preg_replace('/[^a-z0-9_.-]+/', '_', $this->args['name']);
+
+    # build URI-encoded (RFC6266) file name
+    $new_name = urlencode($this->args['name']);
+
+    # build and return response headers
+    return [
+      'Content-Type' => $this->args['type'],
+      'Content-Disposition' => join('; ', [
+        'attachment',
+        "filename=\"{$old_name}\"",
+        "filename*=UTF-8''{$new_name}"
+      ]),
+
+      'Pragma' => 'public',
+      'Cache-Control' => 'public, must-revalidate',
+      'Content-Transfer-Encoding' => 'binary',
+    ];
+  }
 };
 
 /**
-- 
cgit v1.2.3