1d4afb5ceSopenharmony_ci/* 2d4afb5ceSopenharmony_ci * libwebsockets - small server side websockets and web server implementation 3d4afb5ceSopenharmony_ci * 4d4afb5ceSopenharmony_ci * Copyright (C) 2020 Andy Green <andy@warmcat.com> 5d4afb5ceSopenharmony_ci * 6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to 8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the 9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is 11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions: 12d4afb5ceSopenharmony_ci * 13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in 14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software. 15d4afb5ceSopenharmony_ci * 16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22d4afb5ceSopenharmony_ci * IN THE SOFTWARE. 23d4afb5ceSopenharmony_ci * 24d4afb5ceSopenharmony_ci * C++ classes for Secure Streams - file transaction 25d4afb5ceSopenharmony_ci */ 26d4afb5ceSopenharmony_ci 27d4afb5ceSopenharmony_ci#include <libwebsockets.hxx> 28d4afb5ceSopenharmony_ci 29d4afb5ceSopenharmony_ci#include <stdlib.h> 30d4afb5ceSopenharmony_ci#include <fcntl.h> 31d4afb5ceSopenharmony_ci#include <unistd.h> 32d4afb5ceSopenharmony_ci 33d4afb5ceSopenharmony_cistatic lws_ss_state_return_t 34d4afb5ceSopenharmony_cilssfile_rx(void *userobj, const uint8_t *buf, size_t len, int flags) 35d4afb5ceSopenharmony_ci{ 36d4afb5ceSopenharmony_ci lssFile *lf = (lssFile *)userobj_to_lss(userobj); 37d4afb5ceSopenharmony_ci 38d4afb5ceSopenharmony_ci return lf->write(buf, len, flags); 39d4afb5ceSopenharmony_ci} 40d4afb5ceSopenharmony_ci 41d4afb5ceSopenharmony_cistatic lws_ss_state_return_t 42d4afb5ceSopenharmony_cilssfile_tx(void *userobj, lws_ss_tx_ordinal_t ord,uint8_t *buf, size_t *len, 43d4afb5ceSopenharmony_ci int *flags) 44d4afb5ceSopenharmony_ci{ 45d4afb5ceSopenharmony_ci /* 46d4afb5ceSopenharmony_ci * TODO: we don't know how to send things yet 47d4afb5ceSopenharmony_ci */ 48d4afb5ceSopenharmony_ci return LWSSSSRET_TX_DONT_SEND; 49d4afb5ceSopenharmony_ci} 50d4afb5ceSopenharmony_ci 51d4afb5ceSopenharmony_cistatic lws_ss_state_return_t 52d4afb5ceSopenharmony_cilssfile_state(void *userobj, void *h_src, lws_ss_constate_t state, 53d4afb5ceSopenharmony_ci lws_ss_tx_ordinal_t ack) 54d4afb5ceSopenharmony_ci{ 55d4afb5ceSopenharmony_ci lssFile *lf = (lssFile *)userobj_to_lss(userobj); 56d4afb5ceSopenharmony_ci 57d4afb5ceSopenharmony_ci lwsl_info("%s: state %s\n", __func__, lws_ss_state_name(state)); 58d4afb5ceSopenharmony_ci 59d4afb5ceSopenharmony_ci switch (state) { 60d4afb5ceSopenharmony_ci 61d4afb5ceSopenharmony_ci /* 62d4afb5ceSopenharmony_ci * These reflect some kind of final disposition for the transaction, 63d4afb5ceSopenharmony_ci * that we want to report along with the completion. If no other chance 64d4afb5ceSopenharmony_ci * we'll report DESTROYING 65d4afb5ceSopenharmony_ci */ 66d4afb5ceSopenharmony_ci 67d4afb5ceSopenharmony_ci case LWSSSCS_DESTROYING: 68d4afb5ceSopenharmony_ci case LWSSSCS_ALL_RETRIES_FAILED: 69d4afb5ceSopenharmony_ci case LWSSSCS_QOS_ACK_REMOTE: 70d4afb5ceSopenharmony_ci case LWSSSCS_QOS_NACK_REMOTE: 71d4afb5ceSopenharmony_ci lf->call_completion(state); 72d4afb5ceSopenharmony_ci 73d4afb5ceSopenharmony_ci if (state == LWSSSCS_DESTROYING) { 74d4afb5ceSopenharmony_ci /* 75d4afb5ceSopenharmony_ci * we get DESTROYING because we are already in the 76d4afb5ceSopenharmony_ci * middle of destroying the m_ss, unlink the C++ lss 77d4afb5ceSopenharmony_ci * from the ss handle so it won't recursively try to 78d4afb5ceSopenharmony_ci * destroy it 79d4afb5ceSopenharmony_ci */ 80d4afb5ceSopenharmony_ci lf->m_ss = NULL; 81d4afb5ceSopenharmony_ci delete lf; 82d4afb5ceSopenharmony_ci } 83d4afb5ceSopenharmony_ci 84d4afb5ceSopenharmony_ci break; 85d4afb5ceSopenharmony_ci } 86d4afb5ceSopenharmony_ci 87d4afb5ceSopenharmony_ci return LWSSSSRET_OK; 88d4afb5ceSopenharmony_ci} 89d4afb5ceSopenharmony_ci 90d4afb5ceSopenharmony_cilws_ss_state_return_t lssFile::write(const uint8_t *buf, size_t len, int flags) 91d4afb5ceSopenharmony_ci{ 92d4afb5ceSopenharmony_ci if (fd == LWS_INVALID_FILE) { 93d4afb5ceSopenharmony_ci 94d4afb5ceSopenharmony_ci fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0640); 95d4afb5ceSopenharmony_ci if (fd == LWS_INVALID_FILE) 96d4afb5ceSopenharmony_ci return LWSSSSRET_DESTROY_ME; 97d4afb5ceSopenharmony_ci } 98d4afb5ceSopenharmony_ci 99d4afb5ceSopenharmony_ci if (::write(fd, buf, len) != len) { 100d4afb5ceSopenharmony_ci close(fd); 101d4afb5ceSopenharmony_ci fd = LWS_INVALID_FILE; 102d4afb5ceSopenharmony_ci 103d4afb5ceSopenharmony_ci return LWSSSSRET_DESTROY_ME; 104d4afb5ceSopenharmony_ci } 105d4afb5ceSopenharmony_ci 106d4afb5ceSopenharmony_ci rxlen += len; 107d4afb5ceSopenharmony_ci 108d4afb5ceSopenharmony_ci if (flags & LWSSS_FLAG_EOM) { 109d4afb5ceSopenharmony_ci close(fd); 110d4afb5ceSopenharmony_ci fd = LWS_INVALID_FILE; 111d4afb5ceSopenharmony_ci } 112d4afb5ceSopenharmony_ci 113d4afb5ceSopenharmony_ci return LWSSSSRET_OK; 114d4afb5ceSopenharmony_ci} 115d4afb5ceSopenharmony_ci 116d4afb5ceSopenharmony_cilssFile::lssFile(lws_ctx_t ctx, std::string uri, std::string _path, 117d4afb5ceSopenharmony_ci lsscomp_t comp, bool _psh) : 118d4afb5ceSopenharmony_ci lss(ctx, uri, comp, _psh, lssfile_rx, lssfile_tx, lssfile_state) 119d4afb5ceSopenharmony_ci{ 120d4afb5ceSopenharmony_ci path = _path; 121d4afb5ceSopenharmony_ci push = _psh; 122d4afb5ceSopenharmony_ci fd = LWS_INVALID_FILE; 123d4afb5ceSopenharmony_ci} 124d4afb5ceSopenharmony_ci 125d4afb5ceSopenharmony_cilssFile::~lssFile() 126d4afb5ceSopenharmony_ci{ 127d4afb5ceSopenharmony_ci if (fd == LWS_INVALID_FILE) 128d4afb5ceSopenharmony_ci return; 129d4afb5ceSopenharmony_ci 130d4afb5ceSopenharmony_ci close(fd); 131d4afb5ceSopenharmony_ci fd = LWS_INVALID_FILE; 132d4afb5ceSopenharmony_ci} 133