aboutsummaryrefslogtreecommitdiff
path: root/liveapi/libuwsc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--liveapi/libuwsc/buffer.h311
-rw-r--r--liveapi/libuwsc/config.h39
-rw-r--r--liveapi/libuwsc/log.h44
-rw-r--r--liveapi/libuwsc/sha1.h42
-rw-r--r--liveapi/libuwsc/ssl.h46
-rw-r--r--liveapi/libuwsc/utils.h50
-rw-r--r--liveapi/libuwsc/uwsc.h132
-rw-r--r--liveapi/libuwsc/uwsc_lua.h53
8 files changed, 717 insertions, 0 deletions
diff --git a/liveapi/libuwsc/buffer.h b/liveapi/libuwsc/buffer.h
new file mode 100644
index 0000000..6d6abaa
--- /dev/null
+++ b/liveapi/libuwsc/buffer.h
@@ -0,0 +1,311 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Jianhui Zhao <jianhuizhao329@gmail.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.
+ */
+
+#ifndef _BUFFER_H
+#define _BUFFER_H
+
+#include <stdint.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdbool.h>
+#include <sys/types.h>
+
+/* Test for GCC < 2.96 */
+#if __GNUC__ < 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ < 96))
+#define __builtin_expect(x) (x)
+#endif
+
+#ifndef likely
+#define likely(x) __builtin_expect(!!(x), 1)
+#endif
+
+#ifndef unlikely
+#define unlikely(x) __builtin_expect(!!(x), 0)
+#endif
+
+enum {
+ P_FD_EOF = 0,
+ P_FD_ERR = -1,
+ P_FD_PENDING = -2
+};
+
+struct buffer {
+ size_t persistent; /* persistent size */
+ uint8_t *head; /* Head of buffer */
+ uint8_t *data; /* Data head pointer */
+ uint8_t *tail; /* Data tail pointer */
+ uint8_t *end; /* End of buffer */
+};
+
+int buffer_init(struct buffer *b, size_t size);
+int buffer_resize(struct buffer *b, size_t size);
+void buffer_free(struct buffer *b);
+
+
+/* Actual data Length */
+static inline size_t buffer_length(const struct buffer *b)
+{
+ return b->tail - b->data;
+}
+
+/* The total buffer size */
+static inline size_t buffer_size(const struct buffer *b)
+{
+ return b->end - b->head;
+}
+
+static inline size_t buffer_headroom(const struct buffer *b)
+{
+ return b->data - b->head;
+}
+
+static inline size_t buffer_tailroom(const struct buffer *b)
+{
+ return b->end - b->tail;
+}
+
+static inline void *buffer_data(const struct buffer *b)
+{
+ return b->data;
+}
+
+static inline void buffer_set_persistent_size(struct buffer *b, size_t size)
+{
+ size_t new_size = getpagesize();
+
+ while (new_size < size)
+ new_size <<= 1;
+
+ b->persistent = new_size;
+}
+
+static inline void buffer_check_persistent_size(struct buffer *b)
+{
+ if (b->persistent > 0 &&
+ buffer_size(b) > b->persistent && buffer_length(b) < b->persistent)
+ buffer_resize(b, b->persistent);
+}
+
+void *buffer_put(struct buffer *b, size_t len);
+
+static inline void *buffer_put_zero(struct buffer *b, size_t len)
+{
+ void *tmp = buffer_put(b, len);
+
+ if (likely(tmp))
+ memset(tmp, 0, len);
+ return tmp;
+}
+
+static inline void *buffer_put_data(struct buffer *b, const void *data, size_t len)
+{
+ void *tmp = buffer_put(b, len);
+
+ if (likely(tmp))
+ memcpy(tmp, data, len);
+ return tmp;
+}
+
+
+static inline int buffer_put_u8(struct buffer *b, uint8_t val)
+{
+ uint8_t *p = buffer_put(b, 1);
+
+ if (likely(p)) {
+ *p = val;
+ return 0;
+ }
+
+ return -1;
+}
+
+static inline int buffer_put_u16(struct buffer *b, uint16_t val)
+{
+ uint16_t *p = buffer_put(b, 2);
+
+ if (likely(p)) {
+ *p = val;
+ return 0;
+ }
+
+ return -1;
+}
+
+static inline int buffer_put_u32(struct buffer *b, uint32_t val)
+{
+ uint32_t *p = buffer_put(b, 4);
+
+ if (likely(p)) {
+ *p = val;
+ return 0;
+ }
+
+ return -1;
+}
+
+static inline int buffer_put_u64(struct buffer *b, uint64_t val)
+{
+ uint64_t *p = buffer_put(b, 8);
+
+ if (likely(p)) {
+ *p = val;
+ return 0;
+ }
+
+ return -1;
+}
+
+static inline int buffer_put_string(struct buffer *b, const char *s)
+{
+ size_t len = strlen(s);
+ char *p = buffer_put(b, len);
+
+ if (likely(p)) {
+ memcpy(p, s, len);
+ return 0;
+ }
+
+ return -1;
+}
+
+int buffer_put_vprintf(struct buffer *b, const char *fmt, va_list ap) __attribute__((format(printf, 2, 0)));
+int buffer_put_printf(struct buffer *b, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
+
+int buffer_put_fd(struct buffer *b, int fd, ssize_t len, bool *eof,
+ int (*rd)(int fd, void *buf, size_t count, void *arg), void *arg);
+
+/**
+ * buffer_truncate - remove end from a buffer
+ * @b: buffer to alter
+ * @len: new length
+ *
+ * Cut the length of a buffer down by removing data from the tail. If
+ * the buffer is already under the length specified it is not modified.
+ */
+static inline void buffer_truncate(struct buffer *b, size_t len)
+{
+ if (buffer_length(b) > len) {
+ b->tail = b->data + len;
+ buffer_check_persistent_size(b);
+ }
+}
+
+
+size_t buffer_pull(struct buffer *b, void *dest, size_t len);
+
+static inline uint8_t buffer_pull_u8(struct buffer *b)
+{
+ uint8_t val = 0;
+
+ if (likely(buffer_length(b) > 0)) {
+ val = b->data[0];
+ b->data += 1;
+ }
+
+ return val;
+}
+
+static inline uint16_t buffer_pull_u16(struct buffer *b)
+{
+ uint16_t val = 0;
+
+ if (likely(buffer_length(b) > 1)) {
+ val = *((uint16_t *)b->data);
+ b->data += 2;
+ }
+
+ return val;
+}
+
+static inline uint32_t buffer_pull_u32(struct buffer *b)
+{
+ uint32_t val = 0;
+
+ if (likely(buffer_length(b) > 3)) {
+ val = *((uint32_t *)b->data);
+ b->data += 4;
+ }
+
+ return val;
+}
+
+static inline uint64_t buffer_pull_u64(struct buffer *b)
+{
+ uint64_t val = 0;
+
+ if (likely(buffer_length(b) > 7)) {
+ val = *((uint64_t *)b->data);
+ b->data += 8;
+ }
+
+ return val;
+}
+
+static inline uint8_t buffer_get_u8(struct buffer *b, ssize_t offset)
+{
+ uint8_t val = 0;
+
+ if (likely(buffer_length(b) > (size_t)offset))
+ val = b->data[offset];
+
+ return val;
+}
+
+static inline uint16_t buffer_get_u16(struct buffer *b, ssize_t offset)
+{
+ uint16_t val = 0;
+
+ if (likely(buffer_length(b) > (size_t)offset + 1))
+ val = *((uint16_t *)(b->data + offset));
+
+ return val;
+}
+
+static inline uint32_t buffer_get_u32(struct buffer *b, ssize_t offset)
+{
+ uint32_t val = 0;
+
+ if (likely(buffer_length(b) > (size_t)offset + 3))
+ val = *((uint32_t *)(b->data + offset));
+
+ return val;
+}
+
+static inline uint64_t buffer_get_u64(struct buffer *b, ssize_t offset)
+{
+ uint64_t val = 0;
+
+ if (likely(buffer_length(b) > (size_t)offset + 7))
+ val = *((uint64_t *)(b->data + offset));
+
+ return val;
+}
+
+int buffer_pull_to_fd(struct buffer *b, int fd, size_t len,
+ int (*wr)(int fd, void *buf, size_t count, void *arg), void *arg);
+
+void buffer_hexdump(struct buffer *b, size_t offset, size_t len);
+
+#endif
diff --git a/liveapi/libuwsc/config.h b/liveapi/libuwsc/config.h
new file mode 100644
index 0000000..c133235
--- /dev/null
+++ b/liveapi/libuwsc/config.h
@@ -0,0 +1,39 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.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.
+ */
+
+#ifndef _UWSC_CONFIG_H
+#define _UWSC_CONFIG_H
+
+#define UWSC_VERSION_MAJOR 3
+#define UWSC_VERSION_MINOR 3
+#define UWSC_VERSION_PATCH 2
+#define UWSC_VERSION_STRING "3.3.2"
+
+#define UWSC_SSL_SUPPORT 1
+
+#define UWSC_HAVE_OPENSSL 1
+#define UWSC_HAVE_WOLFSSL 0
+#define UWSC_HAVE_MBEDTLS 0
+
+#endif
diff --git a/liveapi/libuwsc/log.h b/liveapi/libuwsc/log.h
new file mode 100644
index 0000000..60d500f
--- /dev/null
+++ b/liveapi/libuwsc/log.h
@@ -0,0 +1,44 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.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.
+ */
+
+#ifndef _UWSC_LOG_H
+#define _UWSC_LOG_H
+
+#include <syslog.h>
+#include <string.h>
+
+void uwsc_log_threshold(int threshold);
+void uwsc_log_close();
+
+#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
+
+#define uwsc_log(priority, fmt...) __uwsc_log(__FILENAME__, __LINE__, priority, fmt)
+
+#define uwsc_log_debug(fmt...) uwsc_log(LOG_DEBUG, fmt)
+#define uwsc_log_info(fmt...) uwsc_log(LOG_INFO, fmt)
+#define uwsc_log_err(fmt...) uwsc_log(LOG_ERR, fmt)
+
+void __uwsc_log(const char *filename, int line, int priority, const char *fmt, ...);
+
+#endif
diff --git a/liveapi/libuwsc/sha1.h b/liveapi/libuwsc/sha1.h
new file mode 100644
index 0000000..447204c
--- /dev/null
+++ b/liveapi/libuwsc/sha1.h
@@ -0,0 +1,42 @@
+/*
+ * Modified from mongoose(https://github.com/cesanta/mongoose)
+ *
+ * MIT License
+ *
+ * Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.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.
+ */
+
+#ifndef _SHA1_H
+#define _SHA1_H
+
+#include <stdint.h>
+
+struct sha1_ctx {
+ uint32_t state[5];
+ size_t count[2];
+ uint8_t buffer[64];
+};
+
+void sha1_init(struct sha1_ctx *ctx);
+void sha1_update(struct sha1_ctx *ctx, const void *data, size_t len);
+void sha1_final(struct sha1_ctx *ctx, uint8_t digest[20]);
+
+#endif
diff --git a/liveapi/libuwsc/ssl.h b/liveapi/libuwsc/ssl.h
new file mode 100644
index 0000000..8a8a533
--- /dev/null
+++ b/liveapi/libuwsc/ssl.h
@@ -0,0 +1,46 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.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.
+ */
+
+#ifndef _UWSC_SSL_H
+#define _UWSC_SSL_H
+
+#include <stdint.h>
+#include <sys/types.h>
+
+#include "config.h"
+
+#if UWSC_SSL_SUPPORT
+
+struct uwsc_ssl_ctx;
+
+int uwsc_ssl_init(struct uwsc_ssl_ctx **ctx, int sock);
+int uwsc_ssl_handshake(struct uwsc_ssl_ctx *ctx);
+void uwsc_ssl_free(struct uwsc_ssl_ctx *ctx);
+
+int uwsc_ssl_read(int fd, void *buf, size_t count, void *arg);
+int uwsc_ssl_write(int fd, void *buf, size_t count, void *arg);
+
+#endif
+
+#endif
diff --git a/liveapi/libuwsc/utils.h b/liveapi/libuwsc/utils.h
new file mode 100644
index 0000000..dacfa31
--- /dev/null
+++ b/liveapi/libuwsc/utils.h
@@ -0,0 +1,50 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.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.
+ */
+
+#ifndef _UTILS_H
+#define _UTILS_H
+
+#include <stddef.h>
+#include <stdbool.h>
+#include <inttypes.h>
+
+#include "config.h"
+
+#ifndef container_of
+#define container_of(ptr, type, member) \
+ ({ \
+ const __typeof__(((type *) NULL)->member) *__mptr = (ptr); \
+ (type *) ((char *) __mptr - offsetof(type, member)); \
+ })
+#endif
+
+int get_nonce(uint8_t *dest, int len);
+int parse_url(const char *url, char *host, int host_len,
+ int *port, const char **path, bool *ssl);
+
+int tcp_connect(const char *host, int port, int flags, bool *inprogress, int *eai);
+
+int b64_encode(const void *src, size_t srclen, void *dest, size_t destsize);
+
+#endif
diff --git a/liveapi/libuwsc/uwsc.h b/liveapi/libuwsc/uwsc.h
new file mode 100644
index 0000000..72ba2d8
--- /dev/null
+++ b/liveapi/libuwsc/uwsc.h
@@ -0,0 +1,132 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.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.
+ */
+
+#ifndef _UWSC_H
+#define _UWSC_H
+
+#include <ev.h>
+
+#include "log.h"
+#include "config.h"
+#include "buffer.h"
+
+#define HTTP_HEAD_LIMIT 4096
+#define UWSC_MAX_CONNECT_TIME 5 /* second */
+#define UWSC_BUFFER_PERSISTENT_SIZE 4096
+
+/* WebSocket close status codes defined in RFC 6455, section 11.7 */
+enum {
+ UWSC_CLOSE_STATUS_NORMAL = 1000,
+ UWSC_CLOSE_STATUS_GOINGAWAY = 1001,
+ UWSC_CLOSE_STATUS_PROTOCOL_ERR = 1002,
+ UWSC_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
+ UWSC_CLOSE_STATUS_RESERVED = 1004,
+ UWSC_CLOSE_STATUS_NO_STATUS = 1005,
+ UWSC_CLOSE_STATUS_ABNORMAL_CLOSE = 1006,
+ UWSC_CLOSE_STATUS_INVALID_PAYLOAD = 1007,
+ UWSC_CLOSE_STATUS_POLICY_VIOLATION = 1008,
+ UWSC_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009,
+ UWSC_CLOSE_STATUS_EXTENSION_REQUIRED = 1010,
+ UWSC_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
+ UWSC_CLOSE_STATUS_TLS_FAILURE = 1015
+};
+
+enum {
+ UWSC_ERROR_IO = 1,
+ UWSC_ERROR_INVALID_HEADER,
+ UWSC_ERROR_SERVER_MASKED,
+ UWSC_ERROR_NOT_SUPPORT,
+ UWSC_ERROR_PING_TIMEOUT,
+ UWSC_ERROR_CONNECT,
+ UWSC_ERROR_SSL_HANDSHAKE
+};
+
+enum {
+ CLIENT_STATE_CONNECTING,
+ CLIENT_STATE_SSL_HANDSHAKE,
+ CLIENT_STATE_HANDSHAKE,
+ CLIENT_STATE_PARSE_MSG_HEAD,
+ CLIENT_STATE_PARSE_MSG_PAYLEN,
+ CLIENT_STATE_PARSE_MSG_PAYLOAD
+};
+
+enum {
+ UWSC_OP_CONTINUE = 0x0,
+ UWSC_OP_TEXT = 0x1,
+ UWSC_OP_BINARY = 0x2,
+ UWSC_OP_CLOSE = 0x8,
+ UWSC_OP_PING = 0x9,
+ UWSC_OP_PONG = 0xA
+};
+
+struct uwsc_frame {
+ uint8_t opcode;
+ size_t payloadlen;
+};
+
+struct uwsc_client {
+ int sock;
+ int state;
+ struct ev_loop *loop;
+ struct ev_io ior;
+ struct ev_io iow;
+ struct buffer rb;
+ struct buffer wb;
+ struct uwsc_frame frame;
+ struct ev_timer timer;
+ bool wait_pong;
+ int ping_interval;
+ ev_tstamp start_time; /* Time stamp of begin connect */
+ ev_tstamp last_ping; /* Time stamp of last ping */
+ int ntimeout; /* Number of timeouts */
+ char key[256]; /* Sec-WebSocket-Key */
+ void *ssl; /* Context wrap of openssl, wolfssl and mbedtls */
+
+ void (*onopen)(struct uwsc_client *cl);
+ void (*set_ping_interval)(struct uwsc_client *cl, int interval);
+ void (*onmessage)(struct uwsc_client *cl, void *data, size_t len, bool binary);
+ void (*onerror)(struct uwsc_client *cl, int err, const char *msg);
+ void (*onclose)(struct uwsc_client *cl, int code, const char *reason);
+
+ int (*send)(struct uwsc_client *cl, const void *data, size_t len, int op);
+ int (*send_ex)(struct uwsc_client *cl, int op, int num, ...);
+ int (*send_close)(struct uwsc_client *cl, int code, const char *reason);
+ void (*ping)(struct uwsc_client *cl);
+ void (*free)(struct uwsc_client *cl);
+};
+
+/*
+ * uwsc_new - creat an uwsc_client struct and connect to server
+ * @loop: If NULL will use EV_DEFAULT
+ * @url: A websock url. ws://xxx.com/xx or wss://xxx.com/xx
+ * @ping_interval: ping interval
+ * @extra_header: extra http header. Authorization: a1d4cdb1a3cd6a0e94aa3599afcddcf5\r\n
+ */
+struct uwsc_client *uwsc_new(struct ev_loop *loop, const char *url,
+ int ping_interval, const char *extra_header);
+
+int uwsc_init(struct uwsc_client *cl, struct ev_loop *loop, const char *url,
+ int ping_interval, const char *extra_header);
+
+#endif
diff --git a/liveapi/libuwsc/uwsc_lua.h b/liveapi/libuwsc/uwsc_lua.h
new file mode 100644
index 0000000..9850af7
--- /dev/null
+++ b/liveapi/libuwsc/uwsc_lua.h
@@ -0,0 +1,53 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.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.
+ */
+
+#ifndef __UWSC_LUA_H
+#define __UWSC_LUA_H
+
+#include <lauxlib.h>
+#include <lualib.h>
+
+#include "uwsc.h"
+
+/* Compatibility defines */
+#if LUA_VERSION_NUM <= 501
+
+/* NOTE: this only works if nups == 0! */
+#define luaL_setfuncs(L, fns, nups) luaL_register((L), NULL, (fns))
+
+#endif
+
+struct uwsc_client_lua {
+ lua_State *L;
+
+ struct uwsc_client cli;
+ bool connected;
+
+ int onopen_ref;
+ int onmessage_ref;
+ int onerror_ref;
+ int onclose_ref;
+};
+
+#endif