summaryrefslogtreecommitdiffstats
path: root/debian/vendor-h2o/lib/handler/compress
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--debian/vendor-h2o/lib/handler/compress.c144
-rw-r--r--debian/vendor-h2o/lib/handler/compress/brotli.cc138
-rw-r--r--debian/vendor-h2o/lib/handler/compress/gzip.c190
3 files changed, 472 insertions, 0 deletions
diff --git a/debian/vendor-h2o/lib/handler/compress.c b/debian/vendor-h2o/lib/handler/compress.c
new file mode 100644
index 0000000..6159771
--- /dev/null
+++ b/debian/vendor-h2o/lib/handler/compress.c
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2015,2016 Justin Zhu, DeNA Co., Ltd., Kazuho Oku
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+#include <assert.h>
+#include <stdlib.h>
+#include "h2o.h"
+
+#ifndef BUF_SIZE
+#define BUF_SIZE 8192
+#endif
+
+struct st_compress_filter_t {
+ h2o_filter_t super;
+ h2o_compress_args_t args;
+};
+
+struct st_compress_encoder_t {
+ h2o_ostream_t super;
+ h2o_compress_context_t *compressor;
+};
+
+static void do_send(h2o_ostream_t *_self, h2o_req_t *req, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state)
+{
+ struct st_compress_encoder_t *self = (void *)_self;
+ h2o_iovec_t *outbufs;
+ size_t outbufcnt;
+
+ self->compressor->transform(self->compressor, inbufs, inbufcnt, state, &outbufs, &outbufcnt);
+ h2o_ostream_send_next(&self->super, req, outbufs, outbufcnt, state);
+}
+
+static void on_setup_ostream(h2o_filter_t *_self, h2o_req_t *req, h2o_ostream_t **slot)
+{
+ struct st_compress_filter_t *self = (void *)_self;
+ struct st_compress_encoder_t *encoder;
+ int compressible_types;
+ h2o_compress_context_t *compressor;
+ ssize_t i;
+
+ if (req->version < 0x101)
+ goto Next;
+ if (req->res.status != 200)
+ goto Next;
+ if (h2o_memis(req->input.method.base, req->input.method.len, H2O_STRLIT("HEAD")))
+ goto Next;
+
+ switch (req->compress_hint) {
+ case H2O_COMPRESS_HINT_DISABLE:
+ /* compression was explicitely disabled, skip */
+ goto Next;
+ case H2O_COMPRESS_HINT_ENABLE:
+ /* compression was explicitely enabled */
+ break;
+ case H2O_COMPRESS_HINT_AUTO:
+ default:
+ /* no hint from the producer, decide whether to compress based
+ on the configuration */
+ if (req->res.mime_attr == NULL)
+ h2o_req_fill_mime_attributes(req);
+ if (!req->res.mime_attr->is_compressible)
+ goto Next;
+ if (req->res.content_length < self->args.min_size)
+ goto Next;
+ }
+
+ /* skip if failed to gather the list of compressible types */
+ if ((compressible_types = h2o_get_compressible_types(&req->headers)) == 0)
+ goto Next;
+
+ /* skip if content-encoding header is being set (as well as obtain the location of accept-ranges) */
+ size_t content_encoding_header_index = -1, accept_ranges_header_index = -1;
+ for (i = 0; i != req->res.headers.size; ++i) {
+ if (req->res.headers.entries[i].name == &H2O_TOKEN_CONTENT_ENCODING->buf)
+ content_encoding_header_index = i;
+ else if (req->res.headers.entries[i].name == &H2O_TOKEN_ACCEPT_RANGES->buf)
+ accept_ranges_header_index = i;
+ else
+ continue;
+ }
+ if (content_encoding_header_index != -1)
+ goto Next;
+
+/* open the compressor */
+#if H2O_USE_BROTLI
+ if (self->args.brotli.quality != -1 && (compressible_types & H2O_COMPRESSIBLE_BROTLI) != 0) {
+ compressor = h2o_compress_brotli_open(&req->pool, self->args.brotli.quality, req->res.content_length);
+ } else
+#endif
+ if (self->args.gzip.quality != -1 && (compressible_types & H2O_COMPRESSIBLE_GZIP) != 0) {
+ compressor = h2o_compress_gzip_open(&req->pool, self->args.gzip.quality);
+ } else {
+ /* let proxies know that we looked at accept-encoding when deciding not to compress */
+ h2o_set_header_token(&req->pool, &req->res.headers, H2O_TOKEN_VARY, H2O_STRLIT("accept-encoding"));
+ goto Next;
+ }
+
+ /* adjust the response headers */
+ req->res.content_length = SIZE_MAX;
+ h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_ENCODING, NULL, compressor->name.base, compressor->name.len);
+ h2o_set_header_token(&req->pool, &req->res.headers, H2O_TOKEN_VARY, H2O_STRLIT("accept-encoding"));
+ if (accept_ranges_header_index != -1) {
+ req->res.headers.entries[accept_ranges_header_index].value = h2o_iovec_init(H2O_STRLIT("none"));
+ } else {
+ h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_ACCEPT_RANGES, NULL, H2O_STRLIT("none"));
+ }
+
+ /* setup filter */
+ encoder = (void *)h2o_add_ostream(req, sizeof(*encoder), slot);
+ encoder->super.do_send = do_send;
+ slot = &encoder->super.next;
+ encoder->compressor = compressor;
+
+ /* adjust preferred chunk size (compress by 8192 bytes) */
+ if (req->preferred_chunk_size > BUF_SIZE)
+ req->preferred_chunk_size = BUF_SIZE;
+
+Next:
+ h2o_setup_next_ostream(req, slot);
+}
+
+void h2o_compress_register(h2o_pathconf_t *pathconf, h2o_compress_args_t *args)
+{
+ struct st_compress_filter_t *self = (void *)h2o_create_filter(pathconf, sizeof(*self));
+ self->super.on_setup_ostream = on_setup_ostream;
+ self->args = *args;
+}
diff --git a/debian/vendor-h2o/lib/handler/compress/brotli.cc b/debian/vendor-h2o/lib/handler/compress/brotli.cc
new file mode 100644
index 0000000..d4d06e9
--- /dev/null
+++ b/debian/vendor-h2o/lib/handler/compress/brotli.cc
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+#include <cassert>
+#include <cstdlib>
+#include <vector>
+#include <zlib.h>
+#include "h2o.h"
+#include "encode.h"
+
+namespace {
+ class brotli_context : public h2o_compress_context_t {
+ protected:
+ brotli::BrotliCompressor *brotli_;
+ brotli::BrotliParams params_;
+ std::vector<h2o_iovec_t> bufs_; // all bufs_[nnn].base must be free(3)ed
+ public:
+ brotli_context(int quality, size_t estimated_content_length) : brotli_(NULL) {
+ name = h2o_iovec_init(H2O_STRLIT("br"));
+ transform = _compress;
+ params_.quality = quality;
+ if (estimated_content_length != std::numeric_limits<size_t>::max())
+ _update_lgwin(params_, estimated_content_length);
+ }
+ ~brotli_context() {
+ _clear_bufs();
+ delete brotli_;
+ }
+ static void dispose(void *_self) {
+ brotli_context *self = static_cast<brotli_context *>(_self);
+ self->~brotli_context();
+ }
+ private:
+ void _clear_bufs();
+ void _emit(bool is_last, bool force_flush);
+ void _compress(h2o_iovec_t *inbufs, size_t inbufcnt, int is_final, h2o_iovec_t **outbufs, size_t *outbufcnt);
+ static void _compress(h2o_compress_context_t *self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state,
+ h2o_iovec_t **outbufs, size_t *outbufcnt) {
+ static_cast<brotli_context*>(self)->_compress(inbufs, inbufcnt, !h2o_send_state_is_in_progress(state), outbufs, outbufcnt);
+ }
+ static void _update_lgwin(brotli::BrotliParams &params, size_t estimated_content_length);
+ };
+}
+
+void brotli_context::_clear_bufs()
+{
+ for (std::vector<h2o_iovec_t>::iterator i = bufs_.begin(); i != bufs_.end(); ++i)
+ free(i->base);
+ bufs_.clear();
+}
+
+void brotli_context::_emit(bool is_last, bool force_flush)
+{
+ uint8_t *output;
+ size_t out_size;
+ bool ret = brotli_->WriteBrotliData(is_last, force_flush, &out_size, &output);
+ assert(ret);
+ (void)ret;
+ if (out_size != 0)
+ bufs_.push_back(h2o_strdup(NULL, reinterpret_cast<const char *>(output), out_size));
+}
+
+void brotli_context::_compress(h2o_iovec_t *inbufs, size_t inbufcnt, int is_final, h2o_iovec_t **outbufs, size_t *outbufcnt)
+{
+ if (brotli_ == NULL) {
+ if (is_final) {
+ uint64_t len = 0;
+ for (size_t i = 0; i != inbufcnt; ++i)
+ len += inbufs[i].len;
+ if (len < std::numeric_limits<size_t>::max())
+ _update_lgwin(params_, len);
+ }
+ brotli_ = new brotli::BrotliCompressor(params_);
+ }
+
+ _clear_bufs();
+
+ if (inbufcnt != 0) {
+ size_t inbufindex = 0, offset = 0, block_space = brotli_->input_block_size();
+ while (inbufindex != inbufcnt) {
+ size_t copy_len = std::min(block_space, inbufs[inbufindex].len - offset);
+ brotli_->CopyInputToRingBuffer(copy_len, reinterpret_cast<const uint8_t *>(inbufs[inbufindex].base) + offset);
+ offset += copy_len;
+ if (inbufs[inbufindex].len == offset) {
+ if (++inbufindex == inbufcnt)
+ break;
+ offset = 0;
+ }
+ if (block_space == 0) {
+ _emit(false, false);
+ block_space = brotli_->input_block_size();
+ }
+ }
+ _emit(is_final, !is_final);
+ } else {
+ if (is_final)
+ _emit(true, false);
+ }
+
+ if (is_final) {
+ delete brotli_;
+ brotli_ = NULL;
+ }
+
+ *outbufs = &bufs_.front();
+ *outbufcnt = bufs_.size();
+}
+
+void brotli_context::_update_lgwin(brotli::BrotliParams &params, size_t estimated_content_length)
+{
+ int bits = estimated_content_length > 1 ? sizeof(unsigned long long) * 8 - __builtin_clzll(estimated_content_length - 1) : 1;
+ if (bits < params.lgwin)
+ params.lgwin = std::max(bits, brotli::kMinWindowBits);
+}
+
+h2o_compress_context_t *h2o_compress_brotli_open(h2o_mem_pool_t *pool, int quality, size_t estimated_content_length)
+{
+ brotli_context *ctx = static_cast<brotli_context *>(h2o_mem_alloc_shared(pool, sizeof(*ctx), brotli_context::dispose));
+ return new (ctx) brotli_context(quality, estimated_content_length);
+}
diff --git a/debian/vendor-h2o/lib/handler/compress/gzip.c b/debian/vendor-h2o/lib/handler/compress/gzip.c
new file mode 100644
index 0000000..c12260d
--- /dev/null
+++ b/debian/vendor-h2o/lib/handler/compress/gzip.c
@@ -0,0 +1,190 @@
+/*
+ * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+#include <assert.h>
+#include <stdlib.h>
+#include <zlib.h>
+#include "h2o.h"
+
+#define WINDOW_BITS 31
+#ifndef BUF_SIZE /* is altered by unit test */
+#define BUF_SIZE 8192
+#endif
+
+typedef H2O_VECTOR(h2o_iovec_t) iovec_vector_t;
+
+struct st_gzip_context_t {
+ h2o_compress_context_t super;
+ z_stream zs;
+ int zs_is_open;
+ iovec_vector_t bufs;
+};
+
+static void do_compress(h2o_compress_context_t *_self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state,
+ h2o_iovec_t **outbufs, size_t *outbufcnt);
+static void do_decompress(h2o_compress_context_t *_self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state,
+ h2o_iovec_t **outbufs, size_t *outbufcnt);
+
+static void *alloc_cb(void *_unused, unsigned int cnt, unsigned int sz)
+{
+ return h2o_mem_alloc(cnt * (size_t)sz);
+}
+
+static void free_cb(void *_unused, void *p)
+{
+ free(p);
+}
+
+static void expand_buf(iovec_vector_t *bufs)
+{
+ h2o_vector_reserve(NULL, bufs, bufs->size + 1);
+ bufs->entries[bufs->size++] = h2o_iovec_init(h2o_mem_alloc(BUF_SIZE), 0);
+}
+
+typedef int (*processor)(z_streamp strm, int flush);
+
+static size_t process_chunk(struct st_gzip_context_t *self, const void *src, size_t len, int flush, size_t bufindex, processor proc)
+{
+ int ret;
+
+ self->zs.next_in = (void *)src;
+ self->zs.avail_in = (unsigned)len;
+
+ /* man says: If inflate/deflate returns with avail_out == 0, this function must be called again with the same value of the flush
+ * parameter and more output space (updated avail_out), until the flush is complete (function returns with non-zero avail_out).
+ */
+ do {
+ /* expand buffer (note: in case of Z_SYNC_FLUSH we need to supply at least 6 bytes of output buffer) */
+ if (self->bufs.entries[bufindex].len + 32 > BUF_SIZE) {
+ ++bufindex;
+ if (bufindex == self->bufs.size)
+ expand_buf(&self->bufs);
+ self->bufs.entries[bufindex].len = 0;
+ }
+ self->zs.next_out = (void *)(self->bufs.entries[bufindex].base + self->bufs.entries[bufindex].len);
+ self->zs.avail_out = (unsigned)(BUF_SIZE - self->bufs.entries[bufindex].len);
+ ret = proc(&self->zs, flush);
+ /* inflate() returns Z_BUF_ERROR if flush is set to Z_FINISH at the middle of the compressed data */
+ assert(ret == Z_OK || ret == Z_STREAM_END || ret == Z_BUF_ERROR);
+ self->bufs.entries[bufindex].len = BUF_SIZE - self->zs.avail_out;
+ } while (self->zs.avail_out == 0 && ret != Z_STREAM_END);
+
+ return bufindex;
+}
+
+static void do_process(h2o_compress_context_t *_self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state,
+ h2o_iovec_t **outbufs, size_t *outbufcnt, processor proc)
+{
+ struct st_gzip_context_t *self = (void *)_self;
+ size_t outbufindex;
+ h2o_iovec_t last_buf;
+
+ outbufindex = 0;
+ self->bufs.entries[0].len = 0;
+
+ if (inbufcnt != 0) {
+ size_t i;
+ for (i = 0; i != inbufcnt - 1; ++i)
+ outbufindex = process_chunk(self, inbufs[i].base, inbufs[i].len, Z_NO_FLUSH, outbufindex, proc);
+ last_buf = inbufs[i];
+ } else {
+ last_buf = h2o_iovec_init(NULL, 0);
+ }
+ outbufindex = process_chunk(self, last_buf.base, last_buf.len, h2o_send_state_is_in_progress(state) ? Z_SYNC_FLUSH : Z_FINISH,
+ outbufindex, proc);
+
+ *outbufs = self->bufs.entries;
+ *outbufcnt = outbufindex + 1;
+
+ if (!h2o_send_state_is_in_progress(state)) {
+ if (self->super.transform == do_compress) {
+ deflateEnd(&self->zs);
+ } else {
+ inflateEnd(&self->zs);
+ }
+ self->zs_is_open = 0;
+ }
+}
+
+static void do_compress(h2o_compress_context_t *_self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state,
+ h2o_iovec_t **outbufs, size_t *outbufcnt)
+{
+ do_process(_self, inbufs, inbufcnt, state, outbufs, outbufcnt, (processor)deflate);
+}
+
+static void do_decompress(h2o_compress_context_t *_self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state,
+ h2o_iovec_t **outbufs, size_t *outbufcnt)
+{
+ do_process(_self, inbufs, inbufcnt, state, outbufs, outbufcnt, (processor)inflate);
+}
+
+static void do_free(void *_self)
+{
+ struct st_gzip_context_t *self = _self;
+ size_t i;
+
+ if (self->zs_is_open) {
+ if (self->super.transform == do_compress) {
+ deflateEnd(&self->zs);
+ } else {
+ inflateEnd(&self->zs);
+ }
+ }
+
+ for (i = 0; i != self->bufs.size; ++i)
+ free(self->bufs.entries[i].base);
+ free(self->bufs.entries);
+}
+
+static struct st_gzip_context_t *gzip_open(h2o_mem_pool_t *pool)
+{
+ struct st_gzip_context_t *self = h2o_mem_alloc_shared(pool, sizeof(*self), do_free);
+
+ self->super.name = h2o_iovec_init(H2O_STRLIT("gzip"));
+ self->super.transform = NULL;
+ self->zs.zalloc = alloc_cb;
+ self->zs.zfree = free_cb;
+ self->zs.opaque = NULL;
+ self->zs_is_open = 1;
+ self->bufs = (iovec_vector_t){NULL};
+ expand_buf(&self->bufs);
+
+ return self;
+}
+
+h2o_compress_context_t *h2o_compress_gzip_open(h2o_mem_pool_t *pool, int quality)
+{
+ struct st_gzip_context_t *self = gzip_open(pool);
+ self->super.transform = do_compress;
+ /* Z_BEST_SPEED for on-the-fly compression, memlevel set to 8 as suggested by the manual */
+ deflateInit2(&self->zs, quality, Z_DEFLATED, WINDOW_BITS, 8, Z_DEFAULT_STRATEGY);
+
+ return &self->super;
+}
+
+h2o_compress_context_t *h2o_compress_gunzip_open(h2o_mem_pool_t *pool)
+{
+ struct st_gzip_context_t *self = gzip_open(pool);
+ self->super.transform = do_decompress;
+ inflateInit2(&self->zs, WINDOW_BITS);
+
+ return &self->super;
+}