Update Files
This commit is contained in:
29
Kinc/Sources/kinc/libs/secure-streams/cpp/README.md
Normal file
29
Kinc/Sources/kinc/libs/secure-streams/cpp/README.md
Normal file
@ -0,0 +1,29 @@
|
||||
## Secure Streams client C++ API
|
||||
|
||||
Enable for build by selecting `-DLWS_WITH_SECURE_STREAMS=1 -DLWS_WITH_SECURE_STREAMS_CPP=1` at
|
||||
cmake.
|
||||
|
||||
Because it's designed for OpenSSL + system trust bundle, the minimal
|
||||
example minimal-secure-streams-cpp requires `-DLWS_WITH_MINIMAL_EXAMPLES=1 -DLWS_WITH_MBEDTLS=0`
|
||||
|
||||
By default the -cpp example downloads https://warmcat.com/test-a.bin to the local
|
||||
file /tmp/test-a.bin. By giving, eg, -c 4, you can run four concurrent downloads of
|
||||
files test-a.bin through test-d.bin... up to 12 files may be downloaded concurrently.
|
||||
|
||||
By default it will connect over h2 and share the single connection between all the
|
||||
downloads.
|
||||
|
||||
### File level api
|
||||
|
||||
```
|
||||
#include <libwebsockets.hxx>
|
||||
|
||||
...
|
||||
|
||||
new lssFile(context, "https://warmcat.com/index.html",
|
||||
"/tmp/index.html", lss_completion, 0);
|
||||
```
|
||||
|
||||
This will copy the remote url to the given local file, and call the
|
||||
completion callback when it has succeeded or failed.
|
||||
|
154
Kinc/Sources/kinc/libs/secure-streams/cpp/lss.cxx
Normal file
154
Kinc/Sources/kinc/libs/secure-streams/cpp/lss.cxx
Normal file
@ -0,0 +1,154 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2020 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* C++ classes for Secure Streams
|
||||
*/
|
||||
|
||||
#include <libwebsockets.hxx>
|
||||
|
||||
static const char *pcols[] = {
|
||||
"http://", /* LWSSSP_H1 */
|
||||
"https://",
|
||||
"h2://", /* LWSSSP_H2 */
|
||||
"h2s://",
|
||||
"ws://", /* LWSSSP_WS */
|
||||
"wss://",
|
||||
"mqtt://", /* LWSSSP_MQTT */
|
||||
"mqtts://",
|
||||
"raw://", /* LWSSSP_RAW */
|
||||
"raws://",
|
||||
};
|
||||
|
||||
static const uint8_t pcols_len[] = {
|
||||
7, 8, 5, 6, 5, 6, 7, 8, 6, 7
|
||||
};
|
||||
|
||||
static const uint16_t pcols_port[] = {
|
||||
80, 443, 443, 443, 80, 443, 1883, 8883, 80, 443
|
||||
};
|
||||
|
||||
lss::lss(lws_ctx_t _ctx, std::string _uri, lsscomp_t _comp, bool _psh,
|
||||
lws_sscb_rx rx, lws_sscb_tx tx, lws_sscb_state state)
|
||||
{
|
||||
const char *p, *urlpath;
|
||||
lws_ss_info_t ssi;
|
||||
int n, port;
|
||||
|
||||
memset(&ssi, 0, sizeof(ssi));
|
||||
memset(&pol, 0, sizeof(pol));
|
||||
|
||||
ctx = _ctx;
|
||||
comp = _comp;
|
||||
comp_done = 0;
|
||||
rxlen = 0;
|
||||
|
||||
/*
|
||||
* We have a common stub userdata, our "real" userdata is in the
|
||||
* derived class members. The Opaque user pointer points to the
|
||||
* lss itself.
|
||||
*/
|
||||
|
||||
ssi.handle_offset = offsetof(lssPriv, lssPriv::m_ss);
|
||||
ssi.opaque_user_data_offset = offsetof(lssPriv, lssPriv::m_plss);
|
||||
|
||||
ssi.user_alloc = sizeof(lssPriv);
|
||||
ssi.rx = rx;
|
||||
ssi.tx = tx;
|
||||
ssi.state = state;
|
||||
ssi.policy = &pol; /* we will provide our own policy */
|
||||
|
||||
/*
|
||||
* _uri is like "https://warmcat.com:443/index.html"... we need to
|
||||
* deconstruct it into its policy implications
|
||||
*/
|
||||
|
||||
uri = strdup(_uri.c_str());
|
||||
|
||||
for (n = 0; n < LWS_ARRAY_SIZE(pcols); n++)
|
||||
if (!strncmp(uri, pcols[n], pcols_len[n]))
|
||||
break;
|
||||
|
||||
if (n == LWS_ARRAY_SIZE(pcols))
|
||||
throw lssException("unknown uri protocol://");
|
||||
|
||||
pol.protocol = n >> 1;
|
||||
if (n & 1)
|
||||
pol.flags |= LWSSSPOLF_TLS;
|
||||
|
||||
n = pcols_port[n];
|
||||
|
||||
if (lws_parse_uri(uri, &p, &pol.endpoint, &n, &urlpath))
|
||||
throw lssException("unable to parse uri://");
|
||||
|
||||
pol.port = (uint16_t)n;
|
||||
|
||||
if (pol.protocol <= LWSSSP_WS) {
|
||||
pol.u.http.url = urlpath;
|
||||
|
||||
/*
|
||||
* These are workarounds for common h2 server noncompliances
|
||||
*/
|
||||
|
||||
pol.flags |= LWSSSPOLF_QUIRK_NGHTTP2_END_STREAM |
|
||||
LWSSSPOLF_H2_QUIRK_OVERFLOWS_TXCR |
|
||||
LWSSSPOLF_H2_QUIRK_UNCLEAN_HPACK_STATE;
|
||||
|
||||
if (pol.protocol < LWSSSP_WS)
|
||||
pol.u.http.method = _psh ? "POST" : "GET";
|
||||
}
|
||||
|
||||
us_start = lws_now_usecs();
|
||||
|
||||
if (lws_ss_create(ctx, 0, &ssi, (void *)this, &m_ss, NULL, NULL))
|
||||
goto blow;
|
||||
|
||||
if (pol.protocol <= LWSSSP_WS)
|
||||
lws_ss_client_connect(m_ss);
|
||||
|
||||
return;
|
||||
|
||||
blow:
|
||||
if (uri)
|
||||
free(uri);
|
||||
throw lssException("ss creation failed");
|
||||
}
|
||||
|
||||
lss::~lss()
|
||||
{
|
||||
if (uri)
|
||||
free(uri);
|
||||
if (m_ss)
|
||||
lws_ss_destroy(&m_ss);
|
||||
}
|
||||
|
||||
int lss::call_completion(lws_ss_constate_t state)
|
||||
{
|
||||
if (comp_done)
|
||||
return 0;
|
||||
if (!comp)
|
||||
return 0;
|
||||
|
||||
comp_done = 1;
|
||||
|
||||
return comp(this, state, NULL);
|
||||
}
|
132
Kinc/Sources/kinc/libs/secure-streams/cpp/lssFile.cxx
Normal file
132
Kinc/Sources/kinc/libs/secure-streams/cpp/lssFile.cxx
Normal file
@ -0,0 +1,132 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2020 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* C++ classes for Secure Streams - file transaction
|
||||
*/
|
||||
|
||||
#include <libwebsockets.hxx>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static lws_ss_state_return_t
|
||||
lssfile_rx(void *userobj, const uint8_t *buf, size_t len, int flags)
|
||||
{
|
||||
lssFile *lf = (lssFile *)userobj_to_lss(userobj);
|
||||
|
||||
return lf->write(buf, len, flags);
|
||||
}
|
||||
|
||||
static lws_ss_state_return_t
|
||||
lssfile_tx(void *userobj, lws_ss_tx_ordinal_t ord,uint8_t *buf, size_t *len,
|
||||
int *flags)
|
||||
{
|
||||
/*
|
||||
* TODO: we don't know how to send things yet
|
||||
*/
|
||||
return LWSSSSRET_TX_DONT_SEND;
|
||||
}
|
||||
|
||||
static lws_ss_state_return_t
|
||||
lssfile_state(void *userobj, void *h_src, lws_ss_constate_t state,
|
||||
lws_ss_tx_ordinal_t ack)
|
||||
{
|
||||
lssFile *lf = (lssFile *)userobj_to_lss(userobj);
|
||||
|
||||
lwsl_info("%s: state %s\n", __func__, lws_ss_state_name(state));
|
||||
|
||||
switch (state) {
|
||||
|
||||
/*
|
||||
* These reflect some kind of final disposition for the transaction,
|
||||
* that we want to report along with the completion. If no other chance
|
||||
* we'll report DESTROYING
|
||||
*/
|
||||
|
||||
case LWSSSCS_DESTROYING:
|
||||
case LWSSSCS_ALL_RETRIES_FAILED:
|
||||
case LWSSSCS_QOS_ACK_REMOTE:
|
||||
case LWSSSCS_QOS_NACK_REMOTE:
|
||||
lf->call_completion(state);
|
||||
|
||||
if (state == LWSSSCS_DESTROYING) {
|
||||
/*
|
||||
* we get DESTROYING because we are already in the
|
||||
* middle of destroying the m_ss, unlink the C++ lss
|
||||
* from the ss handle so it won't recursively try to
|
||||
* destroy it
|
||||
*/
|
||||
lf->m_ss = NULL;
|
||||
delete lf;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
lws_ss_state_return_t lssFile::write(const uint8_t *buf, size_t len, int flags)
|
||||
{
|
||||
if (fd == LWS_INVALID_FILE) {
|
||||
|
||||
fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0640);
|
||||
if (fd == LWS_INVALID_FILE)
|
||||
return LWSSSSRET_DESTROY_ME;
|
||||
}
|
||||
|
||||
if (::write(fd, buf, len) != len) {
|
||||
close(fd);
|
||||
fd = LWS_INVALID_FILE;
|
||||
|
||||
return LWSSSSRET_DESTROY_ME;
|
||||
}
|
||||
|
||||
rxlen += len;
|
||||
|
||||
if (flags & LWSSS_FLAG_EOM) {
|
||||
close(fd);
|
||||
fd = LWS_INVALID_FILE;
|
||||
}
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
lssFile::lssFile(lws_ctx_t ctx, std::string uri, std::string _path,
|
||||
lsscomp_t comp, bool _psh) :
|
||||
lss(ctx, uri, comp, _psh, lssfile_rx, lssfile_tx, lssfile_state)
|
||||
{
|
||||
path = _path;
|
||||
push = _psh;
|
||||
fd = LWS_INVALID_FILE;
|
||||
}
|
||||
|
||||
lssFile::~lssFile()
|
||||
{
|
||||
if (fd == LWS_INVALID_FILE)
|
||||
return;
|
||||
|
||||
close(fd);
|
||||
fd = LWS_INVALID_FILE;
|
||||
}
|
60
Kinc/Sources/kinc/libs/secure-streams/cpp/lssMsg.cxx
Normal file
60
Kinc/Sources/kinc/libs/secure-streams/cpp/lssMsg.cxx
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2020 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* C++ classes for Secure Streams - atomic heap messages
|
||||
*/
|
||||
|
||||
#include <libwebsockets.hxx>
|
||||
|
||||
static lws_ss_state_return_t
|
||||
lssmsg_rx(void *userobj, const uint8_t *buf, size_t len, int flags)
|
||||
{
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
static lws_ss_state_return_t
|
||||
lssmsg_tx(void *userobj, lws_ss_tx_ordinal_t ord,uint8_t *buf, size_t *len,
|
||||
int *flags)
|
||||
{
|
||||
/*
|
||||
* TODO: we don't know how to send things yet
|
||||
*/
|
||||
return LWSSSSRET_TX_DONT_SEND;
|
||||
}
|
||||
|
||||
static lws_ss_state_return_t
|
||||
lssmsg_state(void *userobj, void *h_src, lws_ss_constate_t state,
|
||||
lws_ss_tx_ordinal_t ack)
|
||||
{
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
|
||||
lssMsg::lssMsg(lws_ctx_t ctx, lsscomp_t _comp, std::string uri) :
|
||||
lss(ctx, uri, comp, 0, lssmsg_rx, lssmsg_tx, lssmsg_state)
|
||||
{
|
||||
}
|
||||
|
||||
lssMsg::~lssMsg()
|
||||
{
|
||||
}
|
Reference in New Issue
Block a user