2015-04-22 11:35:37 -07:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
ss: rx metadata
At the moment you can define and set per-stream metadata at the client,
which will be string-substituted and if configured in the policy, set in
related outgoing protocol specific content like h1 headers.
This patch extends the metadata concept to also check incoming protocol-
specific content like h1 headers and where it matches the binding in the
streamtype's metadata entry, make it available to the client by name, via
a new lws_ss_get_metadata() api.
Currently warmcat.com has additional headers for
server: lwsws (well-known header name)
test-custom-header: hello (custom header name)
minimal-secure-streams test is updated to try to recover these both
in direct and -client (via proxy) versions. The corresponding metadata
part of the "mintest" stream policy from warmcat.com is
{
"srv": "server:"
}, {
"test": "test-custom-header:"
},
If built direct, or at the proxy, the stream has access to the static
policy metadata definitions and can store the rx metadata in the stream
metadata allocation, with heap-allocated a value. For client side that
talks to a proxy, only the proxy knows the policy, and it returns rx
metadata inside the serialized link to the client, which stores it on
the heap attached to the stream.
In addition an optimization for mapping static policy metadata definitions
to individual stream handle metadata is changed to match by name.
2020-09-10 06:43:43 +01:00
|
|
|
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
2015-04-22 11:35:37 -07:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* 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:
|
2015-04-22 11:35:37 -07:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2015-04-22 11:35:37 -07:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* 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.
|
2015-04-22 11:35:37 -07:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
2015-04-22 11:35:37 -07:00
|
|
|
#include "lextable-strings.h"
|
|
|
|
|
2016-07-23 14:18:25 +08:00
|
|
|
|
2017-11-05 14:28:57 +08:00
|
|
|
const unsigned char *
|
|
|
|
lws_token_to_string(enum lws_token_indexes token)
|
2015-04-22 11:35:37 -07:00
|
|
|
{
|
2018-08-16 19:10:32 +08:00
|
|
|
if ((unsigned int)token >= LWS_ARRAY_SIZE(set))
|
2015-04-22 11:35:37 -07:00
|
|
|
return NULL;
|
2015-12-06 08:40:00 +08:00
|
|
|
|
2015-04-22 11:35:37 -07:00
|
|
|
return (unsigned char *)set[token];
|
|
|
|
}
|
|
|
|
|
ss: rx metadata
At the moment you can define and set per-stream metadata at the client,
which will be string-substituted and if configured in the policy, set in
related outgoing protocol specific content like h1 headers.
This patch extends the metadata concept to also check incoming protocol-
specific content like h1 headers and where it matches the binding in the
streamtype's metadata entry, make it available to the client by name, via
a new lws_ss_get_metadata() api.
Currently warmcat.com has additional headers for
server: lwsws (well-known header name)
test-custom-header: hello (custom header name)
minimal-secure-streams test is updated to try to recover these both
in direct and -client (via proxy) versions. The corresponding metadata
part of the "mintest" stream policy from warmcat.com is
{
"srv": "server:"
}, {
"test": "test-custom-header:"
},
If built direct, or at the proxy, the stream has access to the static
policy metadata definitions and can store the rx metadata in the stream
metadata allocation, with heap-allocated a value. For client side that
talks to a proxy, only the proxy knows the policy, and it returns rx
metadata inside the serialized link to the client, which stores it on
the heap attached to the stream.
In addition an optimization for mapping static policy metadata definitions
to individual stream handle metadata is changed to match by name.
2020-09-10 06:43:43 +01:00
|
|
|
/*
|
|
|
|
* Return http header index if one matches slen chars of s, or -1
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_http_string_to_known_header(const char *s, size_t slen)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(set); n++)
|
|
|
|
if (!strncmp(set[n], s, slen))
|
|
|
|
return n;
|
|
|
|
|
2020-10-04 07:27:22 +01:00
|
|
|
return LWS_HTTP_NO_KNOWN_HEADER;
|
ss: rx metadata
At the moment you can define and set per-stream metadata at the client,
which will be string-substituted and if configured in the policy, set in
related outgoing protocol specific content like h1 headers.
This patch extends the metadata concept to also check incoming protocol-
specific content like h1 headers and where it matches the binding in the
streamtype's metadata entry, make it available to the client by name, via
a new lws_ss_get_metadata() api.
Currently warmcat.com has additional headers for
server: lwsws (well-known header name)
test-custom-header: hello (custom header name)
minimal-secure-streams test is updated to try to recover these both
in direct and -client (via proxy) versions. The corresponding metadata
part of the "mintest" stream policy from warmcat.com is
{
"srv": "server:"
}, {
"test": "test-custom-header:"
},
If built direct, or at the proxy, the stream has access to the static
policy metadata definitions and can store the rx metadata in the stream
metadata allocation, with heap-allocated a value. For client side that
talks to a proxy, only the proxy knows the policy, and it returns rx
metadata inside the serialized link to the client, which stores it on
the heap attached to the stream.
In addition an optimization for mapping static policy metadata definitions
to individual stream handle metadata is changed to match by name.
2020-09-10 06:43:43 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 21:02:40 +00:00
|
|
|
#ifdef LWS_WITH_HTTP2
|
|
|
|
static int
|
|
|
|
lws_wsi_is_h2(struct lws *wsi)
|
|
|
|
{
|
|
|
|
return wsi->upgraded_to_http2 ||
|
|
|
|
wsi->mux_substream ||
|
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
wsi->client_mux_substream ||
|
|
|
|
#endif
|
|
|
|
lwsi_role_h2(wsi) ||
|
|
|
|
lwsi_role_h2_ENCAPSULATION(wsi);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-12-06 08:40:00 +08:00
|
|
|
int
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
|
2015-12-06 08:40:00 +08:00
|
|
|
const unsigned char *value, int length,
|
|
|
|
unsigned char **p, unsigned char *end)
|
2015-04-22 11:35:37 -07:00
|
|
|
{
|
2017-09-28 11:29:03 +08:00
|
|
|
#ifdef LWS_WITH_HTTP2
|
2021-01-23 21:02:40 +00:00
|
|
|
if (lws_wsi_is_h2(wsi))
|
2015-12-16 18:19:08 +08:00
|
|
|
return lws_add_http2_header_by_name(wsi, name,
|
2015-12-06 08:40:00 +08:00
|
|
|
value, length, p, end);
|
2015-11-02 13:10:33 +08:00
|
|
|
#else
|
|
|
|
(void)wsi;
|
2015-04-22 11:35:37 -07:00
|
|
|
#endif
|
|
|
|
if (name) {
|
|
|
|
while (*p < end && *name)
|
|
|
|
*((*p)++) = *name++;
|
|
|
|
if (*p == end)
|
|
|
|
return 1;
|
|
|
|
*((*p)++) = ' ';
|
|
|
|
}
|
|
|
|
if (*p + length + 3 >= end)
|
|
|
|
return 1;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
memcpy(*p, value, (unsigned int)length);
|
2015-04-22 11:35:37 -07:00
|
|
|
*p += length;
|
|
|
|
*((*p)++) = '\x0d';
|
|
|
|
*((*p)++) = '\x0a';
|
2015-12-06 08:40:00 +08:00
|
|
|
|
2015-04-22 11:35:37 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-16 18:19:08 +08:00
|
|
|
int lws_finalize_http_header(struct lws *wsi, unsigned char **p,
|
|
|
|
unsigned char *end)
|
2015-04-22 11:35:37 -07:00
|
|
|
{
|
2017-09-28 11:29:03 +08:00
|
|
|
#ifdef LWS_WITH_HTTP2
|
2021-01-23 21:02:40 +00:00
|
|
|
if (lws_wsi_is_h2(wsi))
|
2015-04-22 11:35:37 -07:00
|
|
|
return 0;
|
2015-11-02 13:10:33 +08:00
|
|
|
#else
|
|
|
|
(void)wsi;
|
2015-04-22 11:35:37 -07:00
|
|
|
#endif
|
2017-07-07 08:32:04 +08:00
|
|
|
if ((lws_intptr_t)(end - *p) < 3)
|
2015-04-22 11:35:37 -07:00
|
|
|
return 1;
|
|
|
|
*((*p)++) = '\x0d';
|
|
|
|
*((*p)++) = '\x0a';
|
2015-12-06 08:40:00 +08:00
|
|
|
|
2015-04-22 11:35:37 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-20 10:31:53 +08:00
|
|
|
int
|
|
|
|
lws_finalize_write_http_header(struct lws *wsi, unsigned char *start,
|
|
|
|
unsigned char **pp, unsigned char *end)
|
|
|
|
{
|
|
|
|
unsigned char *p;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (lws_finalize_http_header(wsi, pp, end))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
p = *pp;
|
|
|
|
len = lws_ptr_diff(p, start);
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
if (lws_write(wsi, start, (unsigned int)len, LWS_WRITE_HTTP_HEADERS) != len)
|
2018-03-20 10:31:53 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-06 08:40:00 +08:00
|
|
|
int
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_add_http_header_by_token(struct lws *wsi, enum lws_token_indexes token,
|
2015-12-06 08:40:00 +08:00
|
|
|
const unsigned char *value, int length,
|
|
|
|
unsigned char **p, unsigned char *end)
|
2015-04-22 11:35:37 -07:00
|
|
|
{
|
|
|
|
const unsigned char *name;
|
2017-09-28 11:29:03 +08:00
|
|
|
#ifdef LWS_WITH_HTTP2
|
2021-01-23 21:02:40 +00:00
|
|
|
if (lws_wsi_is_h2(wsi))
|
2017-09-23 12:55:21 +08:00
|
|
|
return lws_add_http2_header_by_token(wsi, token, value,
|
|
|
|
length, p, end);
|
2015-04-22 11:35:37 -07:00
|
|
|
#endif
|
|
|
|
name = lws_token_to_string(token);
|
|
|
|
if (!name)
|
|
|
|
return 1;
|
2017-11-05 14:28:57 +08:00
|
|
|
|
2015-12-16 18:19:08 +08:00
|
|
|
return lws_add_http_header_by_name(wsi, name, value, length, p, end);
|
2015-04-22 11:35:37 -07:00
|
|
|
}
|
|
|
|
|
2018-11-23 08:47:56 +08:00
|
|
|
int
|
|
|
|
lws_add_http_header_content_length(struct lws *wsi,
|
|
|
|
lws_filepos_t content_length,
|
|
|
|
unsigned char **p, unsigned char *end)
|
2015-04-22 11:35:37 -07:00
|
|
|
{
|
|
|
|
char b[24];
|
|
|
|
int n;
|
|
|
|
|
2019-08-01 23:03:14 +08:00
|
|
|
n = lws_snprintf(b, sizeof(b) - 1, "%llu", (unsigned long long)content_length);
|
2015-12-17 17:03:59 +08:00
|
|
|
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH,
|
|
|
|
(unsigned char *)b, n, p, end))
|
2015-04-22 11:35:37 -07:00
|
|
|
return 1;
|
2017-12-01 11:09:32 +08:00
|
|
|
wsi->http.tx_content_length = content_length;
|
|
|
|
wsi->http.tx_content_remain = content_length;
|
2015-04-22 11:35:37 -07:00
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: tx_content_length/remain %llu\n", __func__,
|
|
|
|
lws_wsi_tag(wsi), (unsigned long long)content_length);
|
2017-11-05 14:28:57 +08:00
|
|
|
|
2015-04-22 11:35:37 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
|
|
|
|
2018-03-20 10:31:53 +08:00
|
|
|
int
|
|
|
|
lws_add_http_common_headers(struct lws *wsi, unsigned int code,
|
|
|
|
const char *content_type, lws_filepos_t content_len,
|
|
|
|
unsigned char **p, unsigned char *end)
|
|
|
|
{
|
2018-09-02 14:35:37 +08:00
|
|
|
const char *ka[] = { "close", "keep-alive" };
|
|
|
|
int types[] = { HTTP_CONNECTION_CLOSE, HTTP_CONNECTION_KEEP_ALIVE },
|
|
|
|
t = 0;
|
|
|
|
|
2018-03-20 10:31:53 +08:00
|
|
|
if (lws_add_http_header_status(wsi, code, p, end))
|
|
|
|
return 1;
|
2018-04-20 07:12:31 +08:00
|
|
|
|
2020-07-27 10:03:12 +01:00
|
|
|
if (content_type &&
|
|
|
|
lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
|
2018-03-20 10:31:53 +08:00
|
|
|
(unsigned char *)content_type,
|
2018-04-16 19:52:28 +08:00
|
|
|
(int)strlen(content_type), p, end))
|
2018-03-20 10:31:53 +08:00
|
|
|
return 1;
|
2018-04-20 07:12:31 +08:00
|
|
|
|
2018-09-02 14:35:37 +08:00
|
|
|
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
2021-01-11 08:29:02 +00:00
|
|
|
if (!wsi->http.lcs && content_type &&
|
2018-09-02 14:35:37 +08:00
|
|
|
(!strncmp(content_type, "text/", 5) ||
|
|
|
|
!strcmp(content_type, "application/javascript") ||
|
|
|
|
!strcmp(content_type, "image/svg+xml")))
|
|
|
|
lws_http_compression_apply(wsi, NULL, p, end, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we decided to compress it, we don't know the content length...
|
|
|
|
* the compressed data will go out chunked on h1
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
|
|
|
!wsi->http.lcs &&
|
|
|
|
#endif
|
|
|
|
content_len != LWS_ILLEGAL_HTTP_CONTENT_LEN) {
|
|
|
|
if (lws_add_http_header_content_length(wsi, content_len,
|
|
|
|
p, end))
|
2018-06-16 13:23:06 +08:00
|
|
|
return 1;
|
|
|
|
} else {
|
2018-09-02 14:35:37 +08:00
|
|
|
/* there was no length... it normally means CONNECTION_CLOSE */
|
|
|
|
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
if (!wsi->mux_substream && wsi->http.lcs) {
|
2018-09-02 14:35:37 +08:00
|
|
|
/* so...
|
|
|
|
* - h1 connection
|
|
|
|
* - http compression transform active
|
|
|
|
* - did not send content length
|
|
|
|
*
|
|
|
|
* then mark as chunked...
|
|
|
|
*/
|
|
|
|
wsi->http.comp_ctx.chunking = 1;
|
|
|
|
if (lws_add_http_header_by_token(wsi,
|
|
|
|
WSI_TOKEN_HTTP_TRANSFER_ENCODING,
|
|
|
|
(unsigned char *)"chunked", 7, p, end))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* ... but h1 compression is chunked, if active we can
|
|
|
|
* still pipeline
|
|
|
|
*/
|
|
|
|
if (wsi->http.lcs &&
|
|
|
|
wsi->http.conn_type == HTTP_CONNECTION_KEEP_ALIVE)
|
|
|
|
t = 1;
|
|
|
|
}
|
|
|
|
#endif
|
2019-12-23 11:31:57 +00:00
|
|
|
if (!wsi->mux_substream) {
|
2018-11-23 08:47:56 +08:00
|
|
|
if (lws_add_http_header_by_token(wsi,
|
|
|
|
WSI_TOKEN_CONNECTION,
|
2018-09-02 14:35:37 +08:00
|
|
|
(unsigned char *)ka[t],
|
|
|
|
(int)strlen(ka[t]), p, end))
|
|
|
|
return 1;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
wsi->http.conn_type = (enum http_conn_type)types[t];
|
2018-09-02 14:35:37 +08:00
|
|
|
}
|
2018-06-16 13:23:06 +08:00
|
|
|
}
|
2018-03-20 10:31:53 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
static const char * const err400[] = {
|
2015-04-22 11:35:37 -07:00
|
|
|
"Bad Request",
|
|
|
|
"Unauthorized",
|
|
|
|
"Payment Required",
|
|
|
|
"Forbidden",
|
|
|
|
"Not Found",
|
|
|
|
"Method Not Allowed",
|
|
|
|
"Not Acceptable",
|
|
|
|
"Proxy Auth Required",
|
|
|
|
"Request Timeout",
|
|
|
|
"Conflict",
|
|
|
|
"Gone",
|
|
|
|
"Length Required",
|
|
|
|
"Precondition Failed",
|
|
|
|
"Request Entity Too Large",
|
|
|
|
"Request URI too Long",
|
|
|
|
"Unsupported Media Type",
|
|
|
|
"Requested Range Not Satisfiable",
|
|
|
|
"Expectation Failed"
|
|
|
|
};
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
static const char * const err500[] = {
|
2015-04-22 11:35:37 -07:00
|
|
|
"Internal Server Error",
|
|
|
|
"Not Implemented",
|
|
|
|
"Bad Gateway",
|
|
|
|
"Service Unavailable",
|
|
|
|
"Gateway Timeout",
|
|
|
|
"HTTP Version Not Supported"
|
|
|
|
};
|
|
|
|
|
2018-11-19 07:40:47 +08:00
|
|
|
/* security best practices from Mozilla Observatory */
|
|
|
|
|
|
|
|
static const
|
|
|
|
struct lws_protocol_vhost_options pvo_hsbph[] = {{
|
|
|
|
NULL, NULL, "referrer-policy:", "no-referrer"
|
|
|
|
}, {
|
|
|
|
&pvo_hsbph[0], NULL, "x-frame-options:", "deny"
|
|
|
|
}, {
|
|
|
|
&pvo_hsbph[1], NULL, "x-xss-protection:", "1; mode=block"
|
|
|
|
}, {
|
|
|
|
&pvo_hsbph[2], NULL, "x-content-type-options:", "nosniff"
|
|
|
|
}, {
|
|
|
|
&pvo_hsbph[3], NULL, "content-security-policy:",
|
|
|
|
"default-src 'none'; img-src 'self' data: ; "
|
|
|
|
"script-src 'self'; font-src 'self'; "
|
2019-12-26 22:11:04 +00:00
|
|
|
"style-src 'self'; connect-src 'self' ws: wss:; "
|
2018-11-19 07:40:47 +08:00
|
|
|
"frame-ancestors 'none'; base-uri 'none';"
|
|
|
|
"form-action 'self';"
|
|
|
|
}};
|
|
|
|
|
2015-12-06 08:40:00 +08:00
|
|
|
int
|
2017-03-08 07:51:47 +08:00
|
|
|
lws_add_http_header_status(struct lws *wsi, unsigned int _code,
|
2015-12-17 17:03:59 +08:00
|
|
|
unsigned char **p, unsigned char *end)
|
2015-04-22 11:35:37 -07:00
|
|
|
{
|
2018-06-16 09:37:07 +08:00
|
|
|
static const char * const hver[] = {
|
2016-04-15 12:29:06 +08:00
|
|
|
"HTTP/1.0", "HTTP/1.1", "HTTP/2"
|
2016-04-15 12:00:23 +08:00
|
|
|
};
|
2017-03-08 07:51:47 +08:00
|
|
|
const struct lws_protocol_vhost_options *headers;
|
|
|
|
unsigned int code = _code & LWSAHH_CODE_MASK;
|
|
|
|
const char *description = "", *p1;
|
|
|
|
unsigned char code_and_desc[60];
|
|
|
|
int n;
|
2016-04-15 12:00:23 +08:00
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
wsi->http.response_code = code;
|
2016-04-15 12:00:23 +08:00
|
|
|
#ifdef LWS_WITH_ACCESS_LOG
|
2020-12-12 06:21:40 +00:00
|
|
|
wsi->http.access_log.response = (int)code;
|
2016-04-15 12:00:23 +08:00
|
|
|
#endif
|
2015-04-22 11:35:37 -07:00
|
|
|
|
2017-09-28 11:29:03 +08:00
|
|
|
#ifdef LWS_WITH_HTTP2
|
2021-01-23 21:02:40 +00:00
|
|
|
if (lws_wsi_is_h2(wsi)) {
|
2018-08-24 14:43:50 +08:00
|
|
|
n = lws_add_http2_header_status(wsi, code, p, end);
|
|
|
|
if (n)
|
|
|
|
return n;
|
|
|
|
} else
|
2015-04-22 11:35:37 -07:00
|
|
|
#endif
|
2018-08-24 14:43:50 +08:00
|
|
|
{
|
|
|
|
if (code >= 400 && code < (400 + LWS_ARRAY_SIZE(err400)))
|
|
|
|
description = err400[code - 400];
|
|
|
|
if (code >= 500 && code < (500 + LWS_ARRAY_SIZE(err500)))
|
|
|
|
description = err500[code - 500];
|
|
|
|
|
|
|
|
if (code == 100)
|
|
|
|
description = "Continue";
|
|
|
|
if (code == 200)
|
|
|
|
description = "OK";
|
|
|
|
if (code == 304)
|
|
|
|
description = "Not Modified";
|
|
|
|
else
|
|
|
|
if (code >= 300 && code < 400)
|
|
|
|
description = "Redirect";
|
|
|
|
|
|
|
|
if (wsi->http.request_version < LWS_ARRAY_SIZE(hver))
|
|
|
|
p1 = hver[wsi->http.request_version];
|
|
|
|
else
|
|
|
|
p1 = hver[0];
|
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
n = lws_snprintf((char *)code_and_desc,
|
|
|
|
sizeof(code_and_desc) - 1, "%s %u %s",
|
|
|
|
p1, code, description);
|
2018-08-24 14:43:50 +08:00
|
|
|
|
|
|
|
if (lws_add_http_header_by_name(wsi, NULL, code_and_desc, n, p,
|
|
|
|
end))
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-02 14:35:37 +08:00
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
headers = wsi->a.vhost->headers;
|
2016-08-27 17:07:06 +08:00
|
|
|
while (headers) {
|
|
|
|
if (lws_add_http_header_by_name(wsi,
|
|
|
|
(const unsigned char *)headers->name,
|
|
|
|
(unsigned char *)headers->value,
|
2017-10-25 08:00:23 +08:00
|
|
|
(int)strlen(headers->value), p, end))
|
2016-08-27 17:07:06 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
headers = headers->next;
|
|
|
|
}
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (wsi->a.vhost->options &
|
2018-11-19 07:40:47 +08:00
|
|
|
LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE) {
|
|
|
|
headers = &pvo_hsbph[LWS_ARRAY_SIZE(pvo_hsbph) - 1];
|
|
|
|
while (headers) {
|
|
|
|
if (lws_add_http_header_by_name(wsi,
|
|
|
|
(const unsigned char *)headers->name,
|
|
|
|
(unsigned char *)headers->value,
|
|
|
|
(int)strlen(headers->value), p, end))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
headers = headers->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (wsi->a.context->server_string &&
|
2019-08-27 06:06:13 +01:00
|
|
|
!(_code & LWSAHH_FLAG_NO_SERVER_NAME)) {
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
assert(wsi->a.context->server_string_len > 0);
|
2017-03-08 07:51:47 +08:00
|
|
|
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER,
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
(unsigned char *)wsi->a.context->server_string,
|
|
|
|
wsi->a.context->server_string_len, p, end))
|
2017-03-08 07:35:27 +08:00
|
|
|
return 1;
|
2019-08-27 06:06:13 +01:00
|
|
|
}
|
2016-04-15 13:33:52 +08:00
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (wsi->a.vhost->options & LWS_SERVER_OPTION_STS)
|
2016-04-15 13:33:52 +08:00
|
|
|
if (lws_add_http_header_by_name(wsi, (unsigned char *)
|
|
|
|
"Strict-Transport-Security:",
|
|
|
|
(unsigned char *)"max-age=15768000 ; "
|
|
|
|
"includeSubDomains", 36, p, end))
|
|
|
|
return 1;
|
|
|
|
|
2018-08-25 12:27:00 +08:00
|
|
|
if (*p >= (end - 2)) {
|
|
|
|
lwsl_err("%s: reached end of buffer\n", __func__);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:33:52 +08:00
|
|
|
return 0;
|
2015-04-22 11:35:37 -07:00
|
|
|
}
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
2018-11-23 08:47:56 +08:00
|
|
|
lws_return_http_status(struct lws *wsi, unsigned int code,
|
|
|
|
const char *html_body)
|
2015-04-22 11:35:37 -07:00
|
|
|
{
|
2015-12-17 18:25:25 +08:00
|
|
|
struct lws_context *context = lws_get_context(wsi);
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
unsigned char *p = pt->serv_buf + LWS_PRE;
|
2017-06-29 11:26:22 +08:00
|
|
|
unsigned char *start = p;
|
2016-05-19 12:34:35 +08:00
|
|
|
unsigned char *end = p + context->pt_serv_buf_size - LWS_PRE;
|
2018-09-16 14:18:33 +08:00
|
|
|
char *body = (char *)start + context->pt_serv_buf_size - 512;
|
2017-10-13 10:33:02 +08:00
|
|
|
int n = 0, m = 0, len;
|
2016-02-20 07:53:24 +08:00
|
|
|
char slen[20];
|
2015-04-22 11:35:37 -07:00
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (!wsi->a.vhost) {
|
2018-03-19 07:57:13 +08:00
|
|
|
lwsl_err("%s: wsi not bound to vhost\n", __func__);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2018-04-27 15:20:56 +08:00
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
2018-03-19 07:57:13 +08:00
|
|
|
if (!wsi->handling_404 &&
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
wsi->a.vhost->http.error_document_404 &&
|
2018-03-07 19:57:34 +08:00
|
|
|
code == HTTP_STATUS_NOT_FOUND)
|
|
|
|
/* we should do a redirect, and do the 404 there */
|
|
|
|
if (lws_http_redirect(wsi, HTTP_STATUS_FOUND,
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
(uint8_t *)wsi->a.vhost->http.error_document_404,
|
|
|
|
(int)strlen(wsi->a.vhost->http.error_document_404),
|
2018-09-02 14:35:37 +08:00
|
|
|
&p, end) > 0)
|
2018-03-07 19:57:34 +08:00
|
|
|
return 0;
|
2018-04-27 15:20:56 +08:00
|
|
|
#endif
|
2018-03-07 19:57:34 +08:00
|
|
|
|
|
|
|
/* if the redirect failed, just do a simple status */
|
|
|
|
p = start;
|
|
|
|
|
2015-04-22 11:35:37 -07:00
|
|
|
if (!html_body)
|
|
|
|
html_body = "";
|
|
|
|
|
2015-12-16 18:19:08 +08:00
|
|
|
if (lws_add_http_header_status(wsi, code, &p, end))
|
2015-04-22 11:35:37 -07:00
|
|
|
return 1;
|
2016-04-15 13:33:52 +08:00
|
|
|
|
2015-12-16 18:19:08 +08:00
|
|
|
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
|
2015-12-06 08:40:00 +08:00
|
|
|
(unsigned char *)"text/html", 9,
|
|
|
|
&p, end))
|
2015-04-22 11:35:37 -07:00
|
|
|
return 1;
|
2017-06-29 11:26:22 +08:00
|
|
|
|
2018-09-16 14:18:33 +08:00
|
|
|
len = lws_snprintf(body, 510, "<html><head>"
|
|
|
|
"<meta charset=utf-8 http-equiv=\"Content-Language\" "
|
|
|
|
"content=\"en\"/>"
|
|
|
|
"<link rel=\"stylesheet\" type=\"text/css\" "
|
|
|
|
"href=\"/error.css\"/>"
|
|
|
|
"</head><body><h1>%u</h1>%s</body></html>", code, html_body);
|
|
|
|
|
2017-06-29 11:26:22 +08:00
|
|
|
|
2019-08-01 23:03:14 +08:00
|
|
|
n = lws_snprintf(slen, 12, "%d", len);
|
2016-02-20 07:53:24 +08:00
|
|
|
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH,
|
2017-11-05 14:28:57 +08:00
|
|
|
(unsigned char *)slen, n, &p, end))
|
2016-02-20 07:53:24 +08:00
|
|
|
return 1;
|
|
|
|
|
2015-12-16 18:19:08 +08:00
|
|
|
if (lws_finalize_http_header(wsi, &p, end))
|
2015-04-22 11:35:37 -07:00
|
|
|
return 1;
|
|
|
|
|
2017-09-28 11:29:03 +08:00
|
|
|
#if defined(LWS_WITH_HTTP2)
|
2019-12-23 11:31:57 +00:00
|
|
|
if (wsi->mux_substream) {
|
2017-06-29 11:26:22 +08:00
|
|
|
|
2017-10-13 10:33:02 +08:00
|
|
|
/*
|
|
|
|
* for HTTP/2, the headers must be sent separately, since they
|
|
|
|
* go out in their own frame. That puts us in a bind that
|
|
|
|
* we won't always be able to get away with two lws_write()s in
|
|
|
|
* sequence, since the first may use up the writability due to
|
|
|
|
* the pipe being choked or SSL_WANT_.
|
|
|
|
*
|
|
|
|
* However we do need to send the human-readable body, and the
|
|
|
|
* END_STREAM.
|
|
|
|
*
|
|
|
|
* Solve it by writing the headers now...
|
|
|
|
*/
|
2020-12-12 06:21:40 +00:00
|
|
|
m = lws_write(wsi, start, lws_ptr_diff_size_t(p, start),
|
2018-09-04 08:06:46 +08:00
|
|
|
LWS_WRITE_HTTP_HEADERS);
|
2017-10-25 08:00:23 +08:00
|
|
|
if (m != lws_ptr_diff(p, start))
|
2017-06-29 11:26:22 +08:00
|
|
|
return 1;
|
2015-04-22 11:35:37 -07:00
|
|
|
|
2017-10-13 10:33:02 +08:00
|
|
|
/*
|
|
|
|
* ... but stash the body and send it as a priority next
|
|
|
|
* handle_POLLOUT
|
|
|
|
*/
|
2020-12-12 06:21:40 +00:00
|
|
|
wsi->http.tx_content_length = (unsigned int)len;
|
|
|
|
wsi->http.tx_content_remain = (unsigned int)len;
|
2017-06-29 11:26:22 +08:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
wsi->h2.pending_status_body = lws_malloc((unsigned int)len + LWS_PRE + 1,
|
2017-10-13 10:33:02 +08:00
|
|
|
"pending status body");
|
2017-12-01 11:09:32 +08:00
|
|
|
if (!wsi->h2.pending_status_body)
|
2017-10-13 10:33:02 +08:00
|
|
|
return -1;
|
|
|
|
|
2018-09-04 08:06:46 +08:00
|
|
|
strcpy(wsi->h2.pending_status_body + LWS_PRE, body);
|
2017-10-13 10:33:02 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else
|
2017-06-29 11:26:22 +08:00
|
|
|
#endif
|
2017-10-13 10:33:02 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* for http/1, we can just append the body after the finalized
|
|
|
|
* headers and send it all in one go.
|
|
|
|
*/
|
|
|
|
|
2018-09-16 14:18:33 +08:00
|
|
|
n = lws_ptr_diff(p, start) + len;
|
2020-12-12 06:21:40 +00:00
|
|
|
memcpy(p, body, (unsigned int)len);
|
|
|
|
m = lws_write(wsi, start, (unsigned int)n, LWS_WRITE_HTTP);
|
2017-10-13 10:33:02 +08:00
|
|
|
if (m != n)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:35:37 -07:00
|
|
|
return m != n;
|
|
|
|
}
|
2016-04-15 20:09:36 +08:00
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
2016-04-25 10:04:49 +08:00
|
|
|
lws_http_redirect(struct lws *wsi, int code, const unsigned char *loc, int len,
|
2016-04-15 20:09:36 +08:00
|
|
|
unsigned char **p, unsigned char *end)
|
|
|
|
{
|
|
|
|
unsigned char *start = *p;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
if (lws_add_http_header_status(wsi, (unsigned int)code, p, end))
|
2016-04-15 20:09:36 +08:00
|
|
|
return -1;
|
|
|
|
|
2017-11-05 14:28:57 +08:00
|
|
|
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_LOCATION, loc, len,
|
|
|
|
p, end))
|
2016-04-15 20:09:36 +08:00
|
|
|
return -1;
|
|
|
|
/*
|
2017-09-23 12:55:21 +08:00
|
|
|
* if we're going with http/1.1 and keepalive, we have to give fake
|
|
|
|
* content metadata so the client knows we completed the transaction and
|
2016-04-15 20:09:36 +08:00
|
|
|
* it can do the redirect...
|
|
|
|
*/
|
2017-11-05 14:28:57 +08:00
|
|
|
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
|
|
|
|
(unsigned char *)"text/html", 9, p,
|
|
|
|
end))
|
2016-04-15 20:09:36 +08:00
|
|
|
return -1;
|
2017-11-05 14:28:57 +08:00
|
|
|
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH,
|
|
|
|
(unsigned char *)"0", 1, p, end))
|
2016-04-15 20:09:36 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (lws_finalize_http_header(wsi, p, end))
|
|
|
|
return -1;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
return lws_write(wsi, start, lws_ptr_diff_size_t(*p, start),
|
|
|
|
LWS_WRITE_HTTP_HEADERS | LWS_WRITE_H2_STREAM_END);
|
2016-04-15 20:09:36 +08:00
|
|
|
}
|
2019-08-18 05:04:15 +01:00
|
|
|
#endif
|
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
|
|
|
|
|
|
|
#if !defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
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
|
|
|
lws_http_compression_apply(struct lws *wsi, const char *name,
|
|
|
|
unsigned char **p, unsigned char *end, char decomp)
|
|
|
|
{
|
|
|
|
(void)wsi;
|
|
|
|
(void)name;
|
|
|
|
(void)p;
|
|
|
|
(void)end;
|
|
|
|
(void)decomp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-30 14:38:11 +08:00
|
|
|
int
|
|
|
|
lws_http_headers_detach(struct lws *wsi)
|
|
|
|
{
|
|
|
|
return lws_header_table_detach(wsi, 0);
|
|
|
|
}
|
2019-08-09 10:12:09 +01:00
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
void
|
|
|
|
lws_sul_http_ah_lifecheck(lws_sorted_usec_list_t *sul)
|
|
|
|
{
|
|
|
|
struct allocated_headers *ah;
|
|
|
|
struct lws_context_per_thread *pt = lws_container_of(sul,
|
|
|
|
struct lws_context_per_thread, sul_ah_lifecheck);
|
|
|
|
struct lws *wsi;
|
|
|
|
time_t now;
|
|
|
|
int m;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
|
|
|
|
ah = pt->http.ah_list;
|
|
|
|
while (ah) {
|
|
|
|
int len;
|
|
|
|
char buf[256];
|
|
|
|
const unsigned char *c;
|
|
|
|
|
|
|
|
if (!ah->in_use || !ah->wsi || !ah->assigned ||
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
(ah->wsi->a.vhost &&
|
2019-08-09 10:12:09 +01:00
|
|
|
(now - ah->assigned) <
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
ah->wsi->a.vhost->timeout_secs_ah_idle + 360)) {
|
2019-08-09 10:12:09 +01:00
|
|
|
ah = ah->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* a single ah session somehow got held for
|
|
|
|
* an unreasonable amount of time.
|
|
|
|
*
|
|
|
|
* Dump info on the connection...
|
|
|
|
*/
|
|
|
|
wsi = ah->wsi;
|
|
|
|
buf[0] = '\0';
|
|
|
|
#if !defined(LWS_PLAT_OPTEE)
|
|
|
|
lws_get_peer_simple(wsi, buf, sizeof(buf));
|
|
|
|
#else
|
|
|
|
buf[0] = '\0';
|
|
|
|
#endif
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_notice("%s: ah excessive hold: wsi %p\n"
|
2019-08-09 10:12:09 +01:00
|
|
|
" peer address: %s\n"
|
2020-12-25 05:54:19 +00:00
|
|
|
" ah pos %lu\n", __func__, lws_wsi_tag(wsi),
|
|
|
|
buf, (unsigned long)ah->pos);
|
2019-08-09 10:12:09 +01:00
|
|
|
buf[0] = '\0';
|
|
|
|
m = 0;
|
|
|
|
do {
|
2020-12-12 06:21:40 +00:00
|
|
|
c = lws_token_to_string((enum lws_token_indexes)m);
|
2019-08-09 10:12:09 +01:00
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
if (!(*c))
|
|
|
|
break;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
len = lws_hdr_total_length(wsi, (enum lws_token_indexes)m);
|
2019-08-09 10:12:09 +01:00
|
|
|
if (!len || len > (int)sizeof(buf) - 1) {
|
|
|
|
m++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
if (lws_hdr_copy(wsi, buf, sizeof buf, (enum lws_token_indexes)m) > 0) {
|
2019-08-09 10:12:09 +01:00
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
|
|
|
|
lwsl_notice(" %s = %s\n",
|
|
|
|
(const char *)c, buf);
|
|
|
|
}
|
|
|
|
m++;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
/* explicitly detach the ah */
|
|
|
|
lws_header_table_detach(wsi, 0);
|
|
|
|
|
|
|
|
/* ... and then drop the connection */
|
|
|
|
|
|
|
|
__lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"excessive ah");
|
|
|
|
|
|
|
|
ah = pt->http.ah_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
}
|
2019-08-18 05:04:15 +01:00
|
|
|
#endif
|