drop libwebsockets < 3.2.0 support

This commit is contained in:
Shuanglei Tao
2021-08-10 22:20:56 +08:00
parent 1e5c5242fd
commit b2cda1d68c
10 changed files with 194 additions and 288 deletions

View File

@@ -1,10 +1,10 @@
#include <errno.h>
#include <json.h>
#include <libwebsockets.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <json.h>
#include <libwebsockets.h>
#include "pty.h"
#include "server.h"
@@ -35,16 +35,14 @@ static int send_initial_message(struct lws *wsi, int index) {
return lws_write(wsi, p, (size_t)n, LWS_WRITE_BINARY);
}
static json_object* parse_window_size(const char *buf, size_t len, uint16_t *cols, uint16_t *rows) {
static json_object *parse_window_size(const char *buf, size_t len, uint16_t *cols, uint16_t *rows) {
json_tokener *tok = json_tokener_new();
json_object *obj = json_tokener_parse_ex(tok, buf, len);
struct json_object *o = NULL;
if (json_object_object_get_ex(obj, "columns", &o))
*cols = (uint16_t) json_object_get_int(o);
if (json_object_object_get_ex(obj, "rows", &o))
*rows = (uint16_t) json_object_get_int(o);
if (json_object_object_get_ex(obj, "columns", &o)) *cols = (uint16_t)json_object_get_int(o);
if (json_object_object_get_ex(obj, "rows", &o)) *rows = (uint16_t)json_object_get_int(o);
json_tokener_free(tok);
return obj;
}
@@ -52,7 +50,7 @@ static json_object* parse_window_size(const char *buf, size_t len, uint16_t *col
static bool check_host_origin(struct lws *wsi) {
char buf[256];
memset(buf, 0, sizeof(buf));
int len = lws_hdr_copy(wsi, buf, (int) sizeof(buf), WSI_TOKEN_ORIGIN);
int len = lws_hdr_copy(wsi, buf, (int)sizeof(buf), WSI_TOKEN_ORIGIN);
if (len <= 0) {
return false;
}
@@ -68,23 +66,23 @@ static bool check_host_origin(struct lws *wsi) {
char host_buf[256];
memset(host_buf, 0, sizeof(host_buf));
len = lws_hdr_copy(wsi, host_buf, (int) sizeof(host_buf), WSI_TOKEN_HOST);
len = lws_hdr_copy(wsi, host_buf, (int)sizeof(host_buf), WSI_TOKEN_HOST);
return len > 0 && strcasecmp(buf, host_buf) == 0;
}
static void process_read_cb(void *ctx, pty_buf_t *buf, bool eof) {
struct pss_tty *pss = (struct pss_tty *) ctx;
struct pss_tty *pss = (struct pss_tty *)ctx;
if (eof && !process_running(pss->process))
pss->lws_close_status = pss->process->exit_code == 0 ? 1000 : 1006;
else
pss->pty_buf = buf;
lws_callback_on_writable(pss->wsi);
}
static void process_exit_cb(void *ctx, pty_process *process) {
struct pss_tty *pss = (struct pss_tty *) ctx;
struct pss_tty *pss = (struct pss_tty *)ctx;
pss->process = NULL;
if (process->killed) {
lwsl_notice("process killed with signal %d, pid: %d\n", process->exit_signal, process->pid);
@@ -107,7 +105,7 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows)
}
argv[n] = NULL;
pty_process *process = process_init((void *) pss, server->loop, argv);
pty_process *process = process_init((void *)pss, server->loop, argv);
if (columns > 0) process->columns = columns;
if (rows > 0) process->rows = rows;
strncpy(process->term, server->terminal_type, sizeof(process->term));
@@ -126,13 +124,13 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows)
static void wsi_output(struct lws *wsi, pty_buf_t *buf) {
if (buf == NULL) return;
char *message = xmalloc(LWS_PRE + 1 + buf->len);
char *ptr= message + LWS_PRE;
char *ptr = message + LWS_PRE;
*ptr = OUTPUT;
memcpy(ptr + 1, buf->base, buf->len);
size_t n = buf->len + 1;
if (lws_write(wsi, (unsigned char *) ptr, n, LWS_WRITE_BINARY) < n) {
if (lws_write(wsi, (unsigned char *)ptr, n, LWS_WRITE_BINARY) < n) {
lwsl_err("write OUTPUT to WS\n");
}
@@ -157,7 +155,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
}
if (server->credential != NULL) {
n = lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_AUTHORIZATION);
if (n < 7 || !strstr(buf, "Basic ") || strcmp(buf +6, server->credential)) return 1;
if (n < 7 || !strstr(buf, "Basic ") || strcmp(buf + 6, server->credential)) return 1;
}
n = lws_hdr_copy(wsi, pss->path, sizeof(pss->path), WSI_TOKEN_GET_URI);
@@ -195,13 +193,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
server->client_count++;
#if LWS_LIBRARY_VERSION_NUMBER >= 2004000
lws_get_peer_simple(lws_get_network_wsi(wsi), pss->address, sizeof(pss->address));
#else
char name[100];
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), pss->address,
sizeof(pss->address));
#endif
lwsl_notice("WS %s - %s, clients: %d\n", pss->path, pss->address, server->client_count);
break;
@@ -269,8 +261,8 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
}
break;
case RESIZE_TERMINAL:
json_object_put(parse_window_size(pss->buffer + 1, pss->len - 1,
&pss->process->columns, &pss->process->rows));
json_object_put(parse_window_size(pss->buffer + 1, pss->len - 1, &pss->process->columns,
&pss->process->rows));
pty_resize(pss->process);
break;
case PAUSE: