2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 - 2018 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "private-lib-core.h"
24 /* compression methods listed in order of preference */
26 struct lws_compression_support *lcs_available[] = {
27 #if defined(LWS_WITH_HTTP_BROTLI)
33 /* compute acceptable compression encodings while we still have an ah */
36 lws_http_compression_validate(struct lws *wsi)
41 wsi->http.comp_accept_mask = 0;
43 if (!wsi->http.ah || !lwsi_role_server(wsi))
46 a = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING);
50 for (n = 0; n < LWS_ARRAY_SIZE(lcs_available); n++)
51 if (strstr(a, lcs_available[n]->encoding_name))
52 wsi->http.comp_accept_mask |= 1 << n;
58 lws_http_compression_apply(struct lws *wsi, const char *name,
59 unsigned char **p, unsigned char *end, char decomp)
63 for (n = 0; n < LWS_ARRAY_SIZE(lcs_available); n++) {
64 /* if name is non-NULL, choose only that compression method */
65 if (name && !strcmp(lcs_available[n]->encoding_name, name))
68 * If we're the server, confirm that the client told us he could
69 * handle this kind of compression transform...
71 if (!decomp && !(wsi->http.comp_accept_mask & (1 << n)))
74 /* let's go with this one then... */
78 if (n == LWS_ARRAY_SIZE(lcs_available))
81 lcs_available[n]->init_compression(&wsi->http.comp_ctx, decomp);
82 if (!wsi->http.comp_ctx.u.generic_ctx_ptr) {
83 lwsl_err("%s: init_compression %d failed\n", __func__, (int)n);
87 wsi->http.lcs = lcs_available[n];
88 wsi->http.comp_ctx.may_have_more = 0;
89 wsi->http.comp_ctx.final_on_input_side = 0;
90 wsi->http.comp_ctx.chunking = 0;
91 wsi->http.comp_ctx.is_decompression = decomp;
93 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_ENCODING,
94 (unsigned char *)lcs_available[n]->encoding_name,
95 strlen(lcs_available[n]->encoding_name), p, end))
98 lwsl_info("%s: wsi %p: applied %s content-encoding\n", __func__,
99 wsi, lcs_available[n]->encoding_name);
105 lws_http_compression_destroy(struct lws *wsi)
107 if (!wsi->http.lcs || !wsi->http.comp_ctx.u.generic_ctx_ptr)
110 wsi->http.lcs->destroy(&wsi->http.comp_ctx);
112 wsi->http.lcs = NULL;
116 * This manages the compression transform independent of h1 or h2.
118 * wsi->buflist_comp stashes pre-transform input that was not yet compressed
122 lws_http_compression_transform(struct lws *wsi, unsigned char *buf,
123 size_t len, enum lws_write_protocol *wp,
124 unsigned char **outbuf, size_t *olen_oused)
126 size_t ilen_iused = len;
127 int n, use = 0, wp1f = (*wp) & 0x1f;
128 lws_comp_ctx_t *ctx = &wsi->http.comp_ctx;
130 ctx->may_have_more = 0;
132 if (!wsi->http.lcs ||
133 (wp1f != LWS_WRITE_HTTP && wp1f != LWS_WRITE_HTTP_FINAL)) {
140 if (wp1f == LWS_WRITE_HTTP_FINAL) {
142 * ...we may get a large buffer that represents the final input
143 * buffer, but it may form multiple frames after being
144 * tranformed by compression; only the last of those is actually
145 * the final frame on the output stream.
147 * Note that we have received the FINAL input, and downgrade it
148 * to a non-final for now.
150 ctx->final_on_input_side = 1;
151 *wp = LWS_WRITE_HTTP | ((*wp) & ~0x1f);
154 if (ctx->buflist_comp || ctx->may_have_more) {
156 * we can't send this new stuff when we have old stuff
157 * buffered and not compressed yet. Add it to the tail
158 * and switch to trying to process the head.
161 if (lws_buflist_append_segment(
162 &ctx->buflist_comp, buf, len) < 0)
164 lwsl_debug("%s: %p: adding %d to comp buflist\n",
165 __func__,wsi, (int)len);
168 len = lws_buflist_next_segment_len(&ctx->buflist_comp, &buf);
171 lwsl_debug("%s: %p: trying comp buflist %d\n", __func__, wsi,
175 if (!buf && ilen_iused)
178 lwsl_debug("%s: %p: pre-process: ilen_iused %d, olen_oused %d\n",
179 __func__, wsi, (int)ilen_iused, (int)*olen_oused);
181 n = wsi->http.lcs->process(ctx, buf, &ilen_iused, *outbuf, olen_oused);
184 lwsl_err("%s: problem with compression\n", __func__);
189 if (!ctx->may_have_more && ctx->final_on_input_side)
190 *wp = LWS_WRITE_HTTP_FINAL | ((*wp) & ~0x1f);
192 lwsl_debug("%s: %p: more %d, ilen_iused %d\n", __func__, wsi,
193 ctx->may_have_more, (int)ilen_iused);
195 if (use && ilen_iused) {
197 * we were flushing stuff from the buflist head... account for
198 * however much actually got processed by the compression
201 lws_buflist_use_segment(&ctx->buflist_comp, ilen_iused);
202 lwsl_debug("%s: %p: marking %d of comp buflist as used "
203 "(ctx->buflist_comp %p)\n", __func__, wsi,
204 (int)len, ctx->buflist_comp);
207 if (!use && ilen_iused != len) {
209 * ...we were sending stuff from the caller directly and not
210 * all of it got processed... stash on the buflist tail
212 if (lws_buflist_append_segment(&ctx->buflist_comp,
213 buf + ilen_iused, len - ilen_iused) < 0)
216 lwsl_debug("%s: buffering %d unused comp input\n", __func__,
217 (int)(len - ilen_iused));
219 if (ctx->buflist_comp || ctx->may_have_more)
220 lws_callback_on_writable(wsi);