html: embed gziped version

This commit is contained in:
Shuanglei Tao
2020-02-05 16:42:43 +08:00
parent 0e728e61ce
commit 437e63a39c
8 changed files with 8541 additions and 34036 deletions

View File

@@ -20,7 +20,7 @@ jobs:
- name: Install packages
run: |
sudo apt-get update
sudo apt-get install build-essential cmake libjson-c-dev libssl-dev libuv1-dev
sudo apt-get install build-essential cmake libjson-c-dev zlib1g-dev libssl-dev libuv1-dev
- name: Install libwebsockets-${{ matrix.lws-version }}
env:
LWS_VERSION: ${{ matrix.lws-version }}

View File

@@ -33,6 +33,7 @@ set(LIBWEBSOCKETS_MIN_VERSION 1.7.0)
set(SOURCE_FILES src/server.c src/http.c src/protocol.c src/terminal.c src/utils.c)
find_package(OpenSSL REQUIRED)
find_package(ZLIB REQUIRED)
find_package(Libwebsockets ${LIBWEBSOCKETS_MIN_VERSION} QUIET)
find_package(PkgConfig)
@@ -59,8 +60,8 @@ find_library(LIBUV_LIBRARIES NAMES uv libuv HINTS ${PC_LIBUV_LIBDIR})
find_package_handle_standard_args(LIBUV DEFAULT_MSG LIBUV_LIBRARIES LIBUV_INCLUDE_DIRS)
mark_as_advanced(LIBUV_INCLUDE_DIRS LIBUV_LIBRARIES)
set(INCLUDE_DIRS ${OPENSSL_INCLUDE_DIR} ${LIBWEBSOCKETS_INCLUDE_DIRS} ${JSON-C_INCLUDE_DIRS} ${LIBUV_INCLUDE_DIRS})
set(LINK_LIBS ${OPENSSL_LIBRARIES} ${LIBWEBSOCKETS_LIBRARIES} ${JSON-C_LIBRARIES} ${LIBUV_LIBRARIES})
set(INCLUDE_DIRS ${OPENSSL_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR} ${LIBWEBSOCKETS_INCLUDE_DIRS} ${JSON-C_INCLUDE_DIRS} ${LIBUV_INCLUDE_DIRS})
set(LINK_LIBS ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBWEBSOCKETS_LIBRARIES} ${JSON-C_LIBRARIES} ${LIBUV_LIBRARIES})
if(APPLE)
# required for the new homebrew version of libwebsockets

View File

@@ -30,7 +30,7 @@ brew install ttyd
- Build from source (debian/ubuntu):
```bash
sudo apt-get install cmake g++ pkg-config git vim-common libwebsockets-dev libjson-c-dev libssl-dev
sudo apt-get install cmake g++ pkg-config git libwebsockets-dev libjson-c-dev zlib1g-dev libssl-dev libuv1-dev
git clone https://github.com/tsl0922/ttyd.git
cd ttyd && mkdir build && cd build
cmake ..

View File

@@ -1,39 +1,36 @@
const { src, dest, task } = require("gulp");
const clean = require('gulp-clean');
const gzip = require('gulp-gzip');
const inlineSource = require('gulp-inline-source');
const rename = require("gulp-rename");
const through = require('through2');
const toCHeader = () => {
return through.obj((file, enc, cb) => {
const buf = file.contents;
const len = buf.length;
let idx = 0;
let data = "unsigned char index_html[] = {\n ";
const genHeader = (size, buf, len) => {
let idx = 0;
let data = "unsigned char index_html[] = {\n ";
for (const value of buf) {
idx++;
for (const value of buf) {
idx++;
let current = value < 0 ? value + 256 : value;
let current = value < 0 ? value + 256 : value;
data += "0x";
data += (current >>> 4).toString(16);
data += (current & 0xF).toString(16);
data += "0x";
data += (current >>> 4).toString(16);
data += (current & 0xF).toString(16);
if (idx === len) {
data += "\n";
} else {
data += idx % 12 === 0 ? ",\n " : ", ";
}
if (idx === len) {
data += "\n";
} else {
data += idx % 12 === 0 ? ",\n " : ", ";
}
}
data += "};\n";
data += `unsigned int index_html_len = ${len};\n`;
file.contents = Buffer.from(data);
return cb(null, file);
});
data += "};\n";
data += `unsigned int index_html_len = ${len};\n`;
data += `unsigned int index_html_size = ${size};\n`;
return data;
};
let fileSize = 0;
task('clean', () => {
return src('dist', {read: false, allowEmpty: true})
@@ -43,7 +40,16 @@ task('clean', () => {
task('default', () => {
return src('dist/index.html')
.pipe(inlineSource())
.pipe(toCHeader())
.pipe(through.obj((file, enc, cb) => {
fileSize = file.contents.length;
return cb(null, file);
}))
.pipe(gzip())
.pipe(through.obj((file, enc, cb) => {
const buf = file.contents;
file.contents = Buffer.from(genHeader(fileSize, buf, buf.length));
return cb(null, file);
}))
.pipe(rename("html.h"))
.pipe(dest('../src/'));
});

View File

@@ -37,6 +37,7 @@
"gts": "^1.1.2",
"gulp": "^4.0.2",
"gulp-clean": "^0.4.0",
"gulp-gzip": "^1.4.2",
"gulp-inline-source": "^4.0.0",
"gulp-rename": "^2.0.0",
"html-webpack-plugin": "^3.2.0",

View File

@@ -415,6 +415,11 @@ any-observable@^0.3.0:
resolved "https://registry.yarnpkg.com/any-observable/-/any-observable-0.3.0.tgz#af933475e5806a67d0d7df090dd5e8bef65d119b"
integrity sha512-/FQM1EDkTsf63Ub2C6O7GuYFDsSXUwsaZDurV0np41ocwq0jthUAYCmhBX9f+KwlaCgIuWyr/4WlUQUBfKfZog==
any-promise@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"
integrity sha1-q8av7tzqUugJzcA3au0845Y10X8=
anymatch@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-2.0.0.tgz#bcb24b4f37934d9aa7ac17b4adaf89e7c76ef2eb"
@@ -950,7 +955,7 @@ bytes@3.0.0:
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
integrity sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=
bytes@3.1.0:
bytes@3.1.0, bytes@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6"
integrity sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==
@@ -3282,6 +3287,18 @@ gulp-cli@^2.2.0:
v8flags "^3.0.1"
yargs "^7.1.0"
gulp-gzip@^1.4.2:
version "1.4.2"
resolved "https://registry.yarnpkg.com/gulp-gzip/-/gulp-gzip-1.4.2.tgz#0422a94014248655b5b1a9eea1c2abee1d4f4337"
integrity sha512-ZIxfkUwk2XmZPTT9pPHrHUQlZMyp9nPhg2sfoeN27mBGpi7OaHnOD+WCN41NXjfJQ69lV1nQ9LLm1hYxx4h3UQ==
dependencies:
ansi-colors "^1.0.1"
bytes "^3.0.0"
fancy-log "^1.3.2"
plugin-error "^1.0.0"
stream-to-array "^2.3.0"
through2 "^2.0.3"
gulp-inline-source@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/gulp-inline-source/-/gulp-inline-source-4.0.0.tgz#e0958b631719fc6a91bee29905415fa825164c69"
@@ -5881,7 +5898,7 @@ plugin-error@^0.1.2:
arr-union "^2.0.1"
extend-shallow "^1.1.2"
plugin-error@~1.0.1:
plugin-error@^1.0.0, plugin-error@~1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/plugin-error/-/plugin-error-1.0.1.tgz#77016bd8919d0ac377fdcdd0322328953ca5781c"
integrity sha512-L1zP0dk7vGweZME2i+EeakvUNqSrdiI3F91TwEoYiGrAfUXmVv6fJIq4g82PAXxNsWOp0J7ZqQy/3Szz0ajTxA==
@@ -7384,6 +7401,13 @@ stream-shift@^1.0.0:
resolved "https://registry.yarnpkg.com/stream-shift/-/stream-shift-1.0.0.tgz#d5c752825e5367e786f78e18e445ea223a155952"
integrity sha1-1cdSgl5TZ+eG944Y5EXqIjoVWVI=
stream-to-array@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/stream-to-array/-/stream-to-array-2.3.0.tgz#bbf6b39f5f43ec30bc71babcb37557acecf34353"
integrity sha1-u/azn19D7DC8cbq8s3VXrOzzQ1M=
dependencies:
any-promise "^1.1.0"
strict-uri-encode@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"

42405
src/html.h

File diff suppressed because it is too large Load Diff

View File

@@ -1,14 +1,19 @@
#include <string.h>
#include <libwebsockets.h>
#include <openssl/ssl.h>
#include <zlib.h>
#include "server.h"
#include "html.h"
#include "utils.h"
enum {
AUTH_OK, AUTH_FAIL, AUTH_ERROR
};
char * html_cache = NULL;
size_t html_cache_len = 0;
int
check_auth(struct lws *wsi, struct pss_http *pss) {
if (server->credential == NULL)
@@ -62,6 +67,51 @@ check_auth(struct lws *wsi, struct pss_http *pss) {
return AUTH_FAIL;
}
bool accept_gzip(struct lws *wsi) {
int hdr_length = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING);
char buf[hdr_length + 1];
int len = lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_ACCEPT_ENCODING);
return len > 0 && strstr(buf, "gzip") != NULL;
}
bool
uncompress_html(char **output, size_t *output_len) {
if (html_cache == NULL || html_cache_len == 0) {
z_stream stream;
memset(&stream, 0, sizeof(stream));
if (inflateInit2(&stream, 16 + 15) != Z_OK)
return false;
html_cache_len = index_html_size;
html_cache = xmalloc(html_cache_len);
stream.avail_in = index_html_len;
stream.avail_out = html_cache_len;
stream.next_in = (void *) index_html;
stream.next_out = (void *) html_cache;
int ret = inflate(&stream, Z_SYNC_FLUSH);
inflateEnd(&stream);
if (ret != Z_STREAM_END) {
free(html_cache);
html_cache = NULL;
html_cache_len = 0;
return false;
}
}
*output = html_cache;
*output_len = html_cache_len;
return true;
}
bool
pss_buffer_free(struct pss_http *pss) {
if (pss->buffer != (char *) index_html && pss->buffer != html_cache)
free(pss->buffer);
}
void
access_log(struct lws *wsi, const char *path) {
char rip[50];
@@ -132,23 +182,39 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi
if (n < 0 || (n > 0 && lws_http_transaction_completed(wsi)))
return 1;
} else {
char *output = (char *) index_html;
size_t output_len = index_html_len;
if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
return 1;
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE, (const unsigned char *) content_type, 9, &p, end))
return 1;
if (lws_add_http_header_content_length(wsi, (unsigned long) index_html_len, &p, end))
#ifdef LWS_WITH_HTTP_STREAM_COMPRESSION
if (!uncompress_html(&output, &output_len))
return 1;
#else
if (accept_gzip(wsi)) {
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_ENCODING, (unsigned char *) "gzip", 4, &p, end))
return 1;
} else {
if (!uncompress_html(&output, &output_len))
return 1;
}
#endif
if (lws_add_http_header_content_length(wsi, (unsigned long) output_len, &p, end))
return 1;
if (lws_finalize_http_header(wsi, &p, end))
return 1;
if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0)
return 1;
#if LWS_LIBRARY_VERSION_MAJOR < 2
if (lws_write_http(wsi, index_html, index_html_len) < 0)
if (lws_write_http(wsi, output, output_len) < 0)
return 1;
goto try_to_reuse;
#else
pss->buffer = pss->ptr = (char *) index_html;
pss->len = index_html_len;
pss->buffer = pss->ptr = output;
pss->len = output_len;
lws_callback_on_writable(wsi);
#endif
}
@@ -175,7 +241,7 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi
memcpy(buffer + LWS_PRE, pss->ptr, n);
pss->ptr += n;
if (lws_write_http(wsi, buffer + LWS_PRE, (size_t) n) < n) {
if (pss->buffer != (char *) index_html) free(pss->buffer);
pss_buffer_free(pss);
return -1;
}
} while (!lws_send_pipe_choked(wsi) && !done);
@@ -185,9 +251,7 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi
break;
}
if (pss->buffer != (char *) index_html) {
free(pss->buffer);
}
pss_buffer_free(pss);
goto try_to_reuse;
case LWS_CALLBACK_HTTP_FILE_COMPLETION: