1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "private-lib-core.h"
26
27 /* compression methods listed in order of preference */
28
29 struct lws_compression_support *lcs_available[] = {
30 #if defined(LWS_WITH_HTTP_BROTLI)
31 &lcs_brotli,
32 #endif
33 &lcs_deflate,
34 };
35
36 /* compute acceptable compression encodings while we still have an ah */
37
38 int
lws_http_compression_validate(struct lws * wsi)39 lws_http_compression_validate(struct lws *wsi)
40 {
41 const char *a;
42 size_t n;
43
44 wsi->http.comp_accept_mask = 0;
45
46 if (!wsi->http.ah || !lwsi_role_server(wsi))
47 return 0;
48
49 a = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING);
50 if (!a)
51 return 0;
52
53 for (n = 0; n < LWS_ARRAY_SIZE(lcs_available); n++)
54 if (strstr(a, lcs_available[n]->encoding_name))
55 wsi->http.comp_accept_mask = (uint8_t)(wsi->http.comp_accept_mask | (1 << n));
56
57 return 0;
58 }
59
60 int
lws_http_compression_apply(struct lws * wsi,const char * name,unsigned char ** p,unsigned char * end,char decomp)61 lws_http_compression_apply(struct lws *wsi, const char *name,
62 unsigned char **p, unsigned char *end, char decomp)
63 {
64 size_t n;
65
66 for (n = 0; n < LWS_ARRAY_SIZE(lcs_available); n++) {
67 /* if name is non-NULL, choose only that compression method */
68 if (name && !strcmp(lcs_available[n]->encoding_name, name))
69 continue;
70 /*
71 * If we're the server, confirm that the client told us he could
72 * handle this kind of compression transform...
73 */
74 if (!decomp && !(wsi->http.comp_accept_mask & (1 << n)))
75 continue;
76
77 /* let's go with this one then... */
78 break;
79 }
80
81 if (n == LWS_ARRAY_SIZE(lcs_available))
82 return 1;
83
84 lcs_available[n]->init_compression(&wsi->http.comp_ctx, decomp);
85 if (!wsi->http.comp_ctx.u.generic_ctx_ptr) {
86 lwsl_err("%s: init_compression %d failed\n", __func__, (int)n);
87 return 1;
88 }
89
90 wsi->http.lcs = lcs_available[n];
91 wsi->http.comp_ctx.may_have_more = 0;
92 wsi->http.comp_ctx.final_on_input_side = 0;
93 wsi->http.comp_ctx.chunking = 0;
94 wsi->http.comp_ctx.is_decompression = !!decomp;
95
96 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_ENCODING,
97 (unsigned char *)lcs_available[n]->encoding_name,
98 (int)strlen(lcs_available[n]->encoding_name), p, end))
99 return -1;
100
101 lwsl_info("%s: %s: applied %s content-encoding\n", __func__,
102 lws_wsi_tag(wsi), lcs_available[n]->encoding_name);
103
104 return 0;
105 }
106
107 void
lws_http_compression_destroy(struct lws * wsi)108 lws_http_compression_destroy(struct lws *wsi)
109 {
110 if (!wsi->http.lcs || !wsi->http.comp_ctx.u.generic_ctx_ptr)
111 return;
112
113 wsi->http.lcs->destroy(&wsi->http.comp_ctx);
114
115 wsi->http.lcs = NULL;
116 }
117
118 /*
119 * This manages the compression transform independent of h1 or h2.
120 *
121 * wsi->buflist_comp stashes pre-transform input that was not yet compressed
122 */
123
124 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)125 lws_http_compression_transform(struct lws *wsi, unsigned char *buf,
126 size_t len, enum lws_write_protocol *wp,
127 unsigned char **outbuf, size_t *olen_oused)
128 {
129 size_t ilen_iused = len;
130 int n, use = 0, wp1f = (*wp) & 0x1f;
131 lws_comp_ctx_t *ctx = &wsi->http.comp_ctx;
132
133 ctx->may_have_more = 0;
134
135 if (!wsi->http.lcs ||
136 (wp1f != LWS_WRITE_HTTP && wp1f != LWS_WRITE_HTTP_FINAL)) {
137 *outbuf = buf;
138 *olen_oused = len;
139
140 return 0;
141 }
142
143 if (wp1f == LWS_WRITE_HTTP_FINAL) {
144 /*
145 * ...we may get a large buffer that represents the final input
146 * buffer, but it may form multiple frames after being
147 * tranformed by compression; only the last of those is actually
148 * the final frame on the output stream.
149 *
150 * Note that we have received the FINAL input, and downgrade it
151 * to a non-final for now.
152 */
153 ctx->final_on_input_side = 1;
154 *wp = (unsigned int)(LWS_WRITE_HTTP | ((*wp) & ~0x1fu));
155 }
156
157 if (ctx->buflist_comp) {
158 /*
159 * we can't send this new stuff when we have old stuff
160 * buffered and not compressed yet. Add it to the tail
161 * and switch to trying to process the head.
162 */
163 if (buf && len) {
164 if (lws_buflist_append_segment(
165 &ctx->buflist_comp, buf, len) < 0)
166 return -1;
167 lwsl_debug("%s: %s: adding %d to comp buflist\n",
168 __func__, lws_wsi_tag(wsi), (int)len);
169 }
170
171 len = lws_buflist_next_segment_len(&ctx->buflist_comp, &buf);
172 ilen_iused = len;
173 use = 1;
174 lwsl_debug("%s: %s: trying comp buflist %d\n", __func__,
175 lws_wsi_tag(wsi), (int)len);
176 }
177
178 if (!buf && ilen_iused)
179 return 0;
180
181 lwsl_debug("%s: %s: pre-process: ilen_iused %d, olen_oused %d\n",
182 __func__, lws_wsi_tag(wsi), (int)ilen_iused, (int)*olen_oused);
183
184 n = wsi->http.lcs->process(ctx, buf, &ilen_iused, *outbuf, olen_oused);
185
186 if (n && n != 1) {
187 lwsl_err("%s: problem with compression\n", __func__);
188
189 return -1;
190 }
191
192 if (!ctx->may_have_more && ctx->final_on_input_side)
193
194 *wp = (unsigned int)(LWS_WRITE_HTTP_FINAL | ((*wp) & ~0x1fu));
195
196 lwsl_debug("%s: %s: more %d, ilen_iused %d\n", __func__, lws_wsi_tag(wsi),
197 ctx->may_have_more, (int)ilen_iused);
198
199 if (use && ilen_iused) {
200 /*
201 * we were flushing stuff from the buflist head... account for
202 * however much actually got processed by the compression
203 * transform
204 */
205 lws_buflist_use_segment(&ctx->buflist_comp, ilen_iused);
206 lwsl_debug("%s: %s: marking %d of comp buflist as used "
207 "(ctx->buflist_comp %p)\n", __func__,
208 lws_wsi_tag(wsi), (int)len, ctx->buflist_comp);
209 }
210
211 if (!use && ilen_iused != len) {
212 /*
213 * ...we were sending stuff from the caller directly and not
214 * all of it got processed... stash on the buflist tail
215 */
216 if (lws_buflist_append_segment(&ctx->buflist_comp,
217 buf + ilen_iused, len - ilen_iused) < 0)
218 return -1;
219
220 lwsl_debug("%s: buffering %d unused comp input\n", __func__,
221 (int)(len - ilen_iused));
222 }
223 if (ctx->buflist_comp || ctx->may_have_more)
224 lws_callback_on_writable(wsi);
225
226 return 0;
227 }
228