http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 - 2018 Andy Green <andy@warmcat.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* This is included from core/private.h if LWS_WITH_HTTP_STREAM_COMPRESSION
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
#if defined(LWS_WITH_HTTP_BROTLI)
|
|
|
|
#include <brotli/encode.h>
|
|
|
|
#include <brotli/decode.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* struct holding union of all the available compression methods' context data,
|
|
|
|
* and state if it's compressing or decompressing
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct lws_compression_ctx {
|
|
|
|
union {
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_HTTP_BROTLI)
|
|
|
|
BrotliEncoderState *br_en;
|
|
|
|
BrotliDecoderState *br_de;
|
|
|
|
#endif
|
|
|
|
z_stream *deflate;
|
|
|
|
void *generic_ctx_ptr;
|
|
|
|
} u;
|
|
|
|
|
|
|
|
struct lws_buflist *buflist_comp;
|
|
|
|
|
|
|
|
unsigned int is_decompression:1;
|
|
|
|
unsigned int final_on_input_side:1;
|
|
|
|
unsigned int may_have_more:1;
|
|
|
|
unsigned int chunking:1;
|
|
|
|
} lws_comp_ctx_t;
|
|
|
|
|
|
|
|
/* generic structure defining the interface to a compression method */
|
|
|
|
|
|
|
|
struct lws_compression_support {
|
|
|
|
/** compression name as used by, eg, content-ecoding */
|
|
|
|
const char *encoding_name;
|
|
|
|
/** create a compression context for the compression method, or NULL */
|
|
|
|
int (*init_compression)(lws_comp_ctx_t *ctx, int decomp);
|
|
|
|
/** pass data into the context to be processed */
|
|
|
|
int (*process)(lws_comp_ctx_t *ctx, const void *in, size_t *ilen_iused,
|
2018-11-23 08:47:56 +08:00
|
|
|
void *out, size_t *olen_oused);
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
/** destroy the de/compression context */
|
|
|
|
void (*destroy)(lws_comp_ctx_t *ctx);
|
|
|
|
};
|
|
|
|
|
|
|
|
extern struct lws_compression_support lcs_deflate;
|
|
|
|
extern struct lws_compression_support lcs_brotli;
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_http_compression_validate(struct lws *wsi);
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_http_compression_transform(struct lws *wsi, unsigned char *buf,
|
|
|
|
size_t len, enum lws_write_protocol *wp,
|
|
|
|
unsigned char **outbuf, size_t *olen_oused);
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_http_compression_destroy(struct lws *wsi);
|