use curl
This commit is contained in:
72
meson.build
72
meson.build
@@ -67,32 +67,6 @@ endif
|
|||||||
|
|
||||||
cmake = import('cmake')
|
cmake = import('cmake')
|
||||||
|
|
||||||
# Try to find system-installed mbedtls first
|
|
||||||
mbedtls_dep = dependency('mbedtls', static: true, required: false)
|
|
||||||
mbedx509_dep = dependency('mbedx509', static: true, required: false)
|
|
||||||
mbedcrypto_dep = dependency('mbedcrypto', static: true, required: false)
|
|
||||||
|
|
||||||
if not mbedtls_dep.found() or not mbedx509_dep.found() or not mbedcrypto_dep.found()
|
|
||||||
message('⚙ System mbedtls not found, building subproject...')
|
|
||||||
mbedtls_opts = cmake.subproject_options()
|
|
||||||
mbedtls_opts.add_cmake_defines({
|
|
||||||
'ENABLE_PROGRAMS': 'OFF', # Disable Mbed TLS programs
|
|
||||||
'ENABLE_TESTING': 'OFF', # Disable Mbed TLS tests
|
|
||||||
'CMAKE_BUILD_TYPE': 'Release', # Optimize for release
|
|
||||||
'MBEDTLS_FATAL_WARNINGS': 'ON', # Treat warnings as errors
|
|
||||||
'USE_STATIC_MBEDTLS_LIBRARY': 'ON',# Build static libraries
|
|
||||||
'USE_SHARED_MBEDTLS_LIBRARY': 'OFF'# Disable shared libraries
|
|
||||||
})
|
|
||||||
mbedtls_proj = cmake.subproject('mbedtls', options: mbedtls_opts)
|
|
||||||
deps += [
|
|
||||||
mbedtls_proj.dependency('mbedtls'),
|
|
||||||
mbedtls_proj.dependency('mbedx509'),
|
|
||||||
mbedtls_proj.dependency('mbedcrypto')
|
|
||||||
]
|
|
||||||
else
|
|
||||||
deps += [mbedtls_dep, mbedx509_dep, mbedcrypto_dep]
|
|
||||||
endif
|
|
||||||
|
|
||||||
sdl3_opts = cmake.subproject_options()
|
sdl3_opts = cmake.subproject_options()
|
||||||
sdl3_opts.add_cmake_defines({
|
sdl3_opts.add_cmake_defines({
|
||||||
'SDL_STATIC': 'ON',
|
'SDL_STATIC': 'ON',
|
||||||
@@ -222,6 +196,15 @@ else
|
|||||||
deps += chipmunk_dep
|
deps += chipmunk_dep
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# Try to find system-installed curl first
|
||||||
|
curl_dep = dependency('libcurl', static: true, required: false)
|
||||||
|
if not curl_dep.found()
|
||||||
|
message('⚙ System curl not found, building subproject...')
|
||||||
|
deps += dependency('libcurl', static:true)
|
||||||
|
else
|
||||||
|
deps += curl_dep
|
||||||
|
endif
|
||||||
|
|
||||||
if host_machine.system() != 'emscripten'
|
if host_machine.system() != 'emscripten'
|
||||||
# Try to find system-installed enet first
|
# Try to find system-installed enet first
|
||||||
enet_dep = dependency('enet', static: true, required: false)
|
enet_dep = dependency('enet', static: true, required: false)
|
||||||
@@ -288,48 +271,13 @@ else
|
|||||||
message('⚙ QR code support disabled')
|
message('⚙ QR code support disabled')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Always build for Steam unless it's Emscripten
|
|
||||||
if host_machine.system() == 'emscripten'
|
|
||||||
storefront = 'none'
|
|
||||||
else
|
|
||||||
storefront = 'steam'
|
|
||||||
endif
|
|
||||||
if storefront == 'steam'
|
|
||||||
steam_sdk_path = meson.current_source_dir() / 'sdk'
|
|
||||||
|
|
||||||
if host_machine.system() == 'darwin'
|
|
||||||
steam_lib_path = steam_sdk_path / 'redistributable_bin' / 'osx' / 'libsteam_api.dylib'
|
|
||||||
elif host_machine.system() == 'linux'
|
|
||||||
steam_lib_path = steam_sdk_path / 'redistributable_bin' / 'linux64' / 'libsteam_api.so'
|
|
||||||
elif host_machine.system() == 'windows'
|
|
||||||
steam_lib_path = steam_sdk_path / 'redistributable_bin' / 'win64' / 'steam_api64.lib'
|
|
||||||
else
|
|
||||||
steam_lib_path = ''
|
|
||||||
endif
|
|
||||||
|
|
||||||
if fs.exists(steam_lib_path)
|
|
||||||
steam_dep = declare_dependency(
|
|
||||||
include_directories: include_directories('sdk/public'),
|
|
||||||
link_args: [steam_lib_path]
|
|
||||||
)
|
|
||||||
deps += steam_dep
|
|
||||||
src += 'qjs_steam.cpp'
|
|
||||||
message('Steam SDK enabled')
|
|
||||||
else
|
|
||||||
error('Steam SDK required but not found at: ' + steam_lib_path)
|
|
||||||
endif
|
|
||||||
else
|
|
||||||
add_project_arguments('-DNSTEAM', language: ['c', 'cpp'])
|
|
||||||
message('Storefront: ' + storefront)
|
|
||||||
endif
|
|
||||||
|
|
||||||
link_args = link
|
link_args = link
|
||||||
sources = []
|
sources = []
|
||||||
src += [
|
src += [
|
||||||
'anim.c', 'config.c', 'datastream.c','font.c','HandmadeMath.c','jsffi.c','model.c',
|
'anim.c', 'config.c', 'datastream.c','font.c','HandmadeMath.c','jsffi.c','model.c',
|
||||||
'render.c','simplex.c','spline.c', 'transform.c','cell.c', 'wildmatch.c',
|
'render.c','simplex.c','spline.c', 'transform.c','cell.c', 'wildmatch.c',
|
||||||
'sprite.c', 'rtree.c', 'qjs_nota.c', 'qjs_soloud.c', 'qjs_sdl.c', 'qjs_sdl_input.c', 'qjs_sdl_video.c', 'qjs_sdl_surface.c', 'qjs_sdl_gpu.c', 'qjs_math.c', 'qjs_geometry.c', 'qjs_transform.c', 'qjs_sprite.c', 'qjs_io.c', 'qjs_fd.c', 'qjs_os.c', 'qjs_actor.c',
|
'sprite.c', 'rtree.c', 'qjs_nota.c', 'qjs_soloud.c', 'qjs_sdl.c', 'qjs_sdl_input.c', 'qjs_sdl_video.c', 'qjs_sdl_surface.c', 'qjs_sdl_gpu.c', 'qjs_math.c', 'qjs_geometry.c', 'qjs_transform.c', 'qjs_sprite.c', 'qjs_io.c', 'qjs_fd.c', 'qjs_os.c', 'qjs_actor.c',
|
||||||
'qjs_wota.c', 'monocypher.c', 'qjs_blob.c', 'qjs_crypto.c', 'qjs_time.c', 'qjs_http.c', 'qjs_rtree.c', 'qjs_spline.c', 'qjs_js.c', 'qjs_debug.c', 'picohttpparser.c', 'qjs_miniz.c', 'timer.c', 'qjs_socket.c', 'qjs_kim.c', 'qjs_utf8.c', 'qjs_fit.c', 'qjs_text.c', 'qjs_staef.c', 'qjs_layout.c', 'cell_qoi.c'
|
'qjs_wota.c', 'monocypher.c', 'qjs_blob.c', 'qjs_crypto.c', 'qjs_time.c', 'qjs_http.c', 'qjs_rtree.c', 'qjs_spline.c', 'qjs_js.c', 'qjs_debug.c', 'qjs_miniz.c', 'timer.c', 'qjs_socket.c', 'qjs_kim.c', 'qjs_utf8.c', 'qjs_fit.c', 'qjs_text.c', 'qjs_staef.c', 'qjs_layout.c', 'cell_qoi.c'
|
||||||
]
|
]
|
||||||
# quirc src
|
# quirc src
|
||||||
src += [
|
src += [
|
||||||
|
|||||||
@@ -55,9 +55,6 @@
|
|||||||
#include "qjs_staef.h"
|
#include "qjs_staef.h"
|
||||||
#include "qjs_io.h"
|
#include "qjs_io.h"
|
||||||
#include "qjs_fd.h"
|
#include "qjs_fd.h"
|
||||||
#ifndef NSTEAM
|
|
||||||
#include "qjs_steam.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// External transform function declarations
|
// External transform function declarations
|
||||||
extern JSClassID js_transform_id;
|
extern JSClassID js_transform_id;
|
||||||
@@ -1177,10 +1174,6 @@ void ffi_load(JSContext *js)
|
|||||||
|
|
||||||
arrput(rt->module_registry, MISTLINE(imgui));
|
arrput(rt->module_registry, MISTLINE(imgui));
|
||||||
|
|
||||||
#ifndef NSTEAM
|
|
||||||
arrput(rt->module_registry, MISTLINE(steam));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
JSValue globalThis = JS_GetGlobalObject(js);
|
JSValue globalThis = JS_GetGlobalObject(js);
|
||||||
|
|
||||||
JSValue prosp = JS_NewObject(js);
|
JSValue prosp = JS_NewObject(js);
|
||||||
|
|||||||
@@ -1,685 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
|
|
||||||
* Shigeo Mitsunari
|
|
||||||
*
|
|
||||||
* The software is licensed under either the MIT License (below) or the Perl
|
|
||||||
* license.
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <string.h>
|
|
||||||
#ifdef __SSE4_2__
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#include <nmmintrin.h>
|
|
||||||
#else
|
|
||||||
#include <x86intrin.h>
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#include "picohttpparser.h"
|
|
||||||
|
|
||||||
#if __GNUC__ >= 3
|
|
||||||
#define likely(x) __builtin_expect(!!(x), 1)
|
|
||||||
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
||||||
#else
|
|
||||||
#define likely(x) (x)
|
|
||||||
#define unlikely(x) (x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#define ALIGNED(n) _declspec(align(n))
|
|
||||||
#else
|
|
||||||
#define ALIGNED(n) __attribute__((aligned(n)))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define IS_PRINTABLE_ASCII(c) ((unsigned char)(c)-040u < 0137u)
|
|
||||||
|
|
||||||
#define CHECK_EOF() \
|
|
||||||
if (buf == buf_end) { \
|
|
||||||
*ret = -2; \
|
|
||||||
return NULL; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define EXPECT_CHAR_NO_CHECK(ch) \
|
|
||||||
if (*buf++ != ch) { \
|
|
||||||
*ret = -1; \
|
|
||||||
return NULL; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define EXPECT_CHAR(ch) \
|
|
||||||
CHECK_EOF(); \
|
|
||||||
EXPECT_CHAR_NO_CHECK(ch);
|
|
||||||
|
|
||||||
#define ADVANCE_TOKEN(tok, toklen) \
|
|
||||||
do { \
|
|
||||||
const char *tok_start = buf; \
|
|
||||||
static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \
|
|
||||||
int found2; \
|
|
||||||
buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \
|
|
||||||
if (!found2) { \
|
|
||||||
CHECK_EOF(); \
|
|
||||||
} \
|
|
||||||
while (1) { \
|
|
||||||
if (*buf == ' ') { \
|
|
||||||
break; \
|
|
||||||
} else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \
|
|
||||||
if ((unsigned char)*buf < '\040' || *buf == '\177') { \
|
|
||||||
*ret = -1; \
|
|
||||||
return NULL; \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
++buf; \
|
|
||||||
CHECK_EOF(); \
|
|
||||||
} \
|
|
||||||
tok = tok_start; \
|
|
||||||
toklen = buf - tok_start; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
static const char *token_char_map = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
|
||||||
"\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0"
|
|
||||||
"\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1"
|
|
||||||
"\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0"
|
|
||||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
|
||||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
|
||||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
|
||||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
|
|
||||||
|
|
||||||
static const char *findchar_fast(const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found)
|
|
||||||
{
|
|
||||||
*found = 0;
|
|
||||||
#if __SSE4_2__
|
|
||||||
if (likely(buf_end - buf >= 16)) {
|
|
||||||
__m128i ranges16 = _mm_loadu_si128((const __m128i *)ranges);
|
|
||||||
|
|
||||||
size_t left = (buf_end - buf) & ~15;
|
|
||||||
do {
|
|
||||||
__m128i b16 = _mm_loadu_si128((const __m128i *)buf);
|
|
||||||
int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS);
|
|
||||||
if (unlikely(r != 16)) {
|
|
||||||
buf += r;
|
|
||||||
*found = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
buf += 16;
|
|
||||||
left -= 16;
|
|
||||||
} while (likely(left != 0));
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
/* suppress unused parameter warning */
|
|
||||||
(void)buf_end;
|
|
||||||
(void)ranges;
|
|
||||||
(void)ranges_size;
|
|
||||||
#endif
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *get_token_to_eol(const char *buf, const char *buf_end, const char **token, size_t *token_len, int *ret)
|
|
||||||
{
|
|
||||||
const char *token_start = buf;
|
|
||||||
|
|
||||||
#ifdef __SSE4_2__
|
|
||||||
static const char ALIGNED(16) ranges1[16] = "\0\010" /* allow HT */
|
|
||||||
"\012\037" /* allow SP and up to but not including DEL */
|
|
||||||
"\177\177"; /* allow chars w. MSB set */
|
|
||||||
int found;
|
|
||||||
buf = findchar_fast(buf, buf_end, ranges1, 6, &found);
|
|
||||||
if (found)
|
|
||||||
goto FOUND_CTL;
|
|
||||||
#else
|
|
||||||
/* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */
|
|
||||||
while (likely(buf_end - buf >= 8)) {
|
|
||||||
#define DOIT() \
|
|
||||||
do { \
|
|
||||||
if (unlikely(!IS_PRINTABLE_ASCII(*buf))) \
|
|
||||||
goto NonPrintable; \
|
|
||||||
++buf; \
|
|
||||||
} while (0)
|
|
||||||
DOIT();
|
|
||||||
DOIT();
|
|
||||||
DOIT();
|
|
||||||
DOIT();
|
|
||||||
DOIT();
|
|
||||||
DOIT();
|
|
||||||
DOIT();
|
|
||||||
DOIT();
|
|
||||||
#undef DOIT
|
|
||||||
continue;
|
|
||||||
NonPrintable:
|
|
||||||
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
|
|
||||||
goto FOUND_CTL;
|
|
||||||
}
|
|
||||||
++buf;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
for (;; ++buf) {
|
|
||||||
CHECK_EOF();
|
|
||||||
if (unlikely(!IS_PRINTABLE_ASCII(*buf))) {
|
|
||||||
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
|
|
||||||
goto FOUND_CTL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FOUND_CTL:
|
|
||||||
if (likely(*buf == '\015')) {
|
|
||||||
++buf;
|
|
||||||
EXPECT_CHAR('\012');
|
|
||||||
*token_len = buf - 2 - token_start;
|
|
||||||
} else if (*buf == '\012') {
|
|
||||||
*token_len = buf - token_start;
|
|
||||||
++buf;
|
|
||||||
} else {
|
|
||||||
*ret = -1;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
*token = token_start;
|
|
||||||
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *is_complete(const char *buf, const char *buf_end, size_t last_len, int *ret)
|
|
||||||
{
|
|
||||||
int ret_cnt = 0;
|
|
||||||
buf = last_len < 3 ? buf : buf + last_len - 3;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
CHECK_EOF();
|
|
||||||
if (*buf == '\015') {
|
|
||||||
++buf;
|
|
||||||
CHECK_EOF();
|
|
||||||
EXPECT_CHAR('\012');
|
|
||||||
++ret_cnt;
|
|
||||||
} else if (*buf == '\012') {
|
|
||||||
++buf;
|
|
||||||
++ret_cnt;
|
|
||||||
} else {
|
|
||||||
++buf;
|
|
||||||
ret_cnt = 0;
|
|
||||||
}
|
|
||||||
if (ret_cnt == 2) {
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*ret = -2;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define PARSE_INT(valp_, mul_) \
|
|
||||||
if (*buf < '0' || '9' < *buf) { \
|
|
||||||
buf++; \
|
|
||||||
*ret = -1; \
|
|
||||||
return NULL; \
|
|
||||||
} \
|
|
||||||
*(valp_) = (mul_) * (*buf++ - '0');
|
|
||||||
|
|
||||||
#define PARSE_INT_3(valp_) \
|
|
||||||
do { \
|
|
||||||
int res_ = 0; \
|
|
||||||
PARSE_INT(&res_, 100) \
|
|
||||||
*valp_ = res_; \
|
|
||||||
PARSE_INT(&res_, 10) \
|
|
||||||
*valp_ += res_; \
|
|
||||||
PARSE_INT(&res_, 1) \
|
|
||||||
*valp_ += res_; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
/* returned pointer is always within [buf, buf_end), or null */
|
|
||||||
static const char *parse_token(const char *buf, const char *buf_end, const char **token, size_t *token_len, char next_char,
|
|
||||||
int *ret)
|
|
||||||
{
|
|
||||||
/* We use pcmpestri to detect non-token characters. This instruction can take no more than eight character ranges (8*2*8=128
|
|
||||||
* bits that is the size of a SSE register). Due to this restriction, characters `|` and `~` are handled in the slow loop. */
|
|
||||||
static const char ALIGNED(16) ranges[] = "\x00 " /* control chars and up to SP */
|
|
||||||
"\"\"" /* 0x22 */
|
|
||||||
"()" /* 0x28,0x29 */
|
|
||||||
",," /* 0x2c */
|
|
||||||
"//" /* 0x2f */
|
|
||||||
":@" /* 0x3a-0x40 */
|
|
||||||
"[]" /* 0x5b-0x5d */
|
|
||||||
"{\xff"; /* 0x7b-0xff */
|
|
||||||
const char *buf_start = buf;
|
|
||||||
int found;
|
|
||||||
buf = findchar_fast(buf, buf_end, ranges, sizeof(ranges) - 1, &found);
|
|
||||||
if (!found) {
|
|
||||||
CHECK_EOF();
|
|
||||||
}
|
|
||||||
while (1) {
|
|
||||||
if (*buf == next_char) {
|
|
||||||
break;
|
|
||||||
} else if (!token_char_map[(unsigned char)*buf]) {
|
|
||||||
*ret = -1;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
++buf;
|
|
||||||
CHECK_EOF();
|
|
||||||
}
|
|
||||||
*token = buf_start;
|
|
||||||
*token_len = buf - buf_start;
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* returned pointer is always within [buf, buf_end), or null */
|
|
||||||
static const char *parse_http_version(const char *buf, const char *buf_end, int *minor_version, int *ret)
|
|
||||||
{
|
|
||||||
/* we want at least [HTTP/1.<two chars>] to try to parse */
|
|
||||||
if (buf_end - buf < 9) {
|
|
||||||
*ret = -2;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
EXPECT_CHAR_NO_CHECK('H');
|
|
||||||
EXPECT_CHAR_NO_CHECK('T');
|
|
||||||
EXPECT_CHAR_NO_CHECK('T');
|
|
||||||
EXPECT_CHAR_NO_CHECK('P');
|
|
||||||
EXPECT_CHAR_NO_CHECK('/');
|
|
||||||
EXPECT_CHAR_NO_CHECK('1');
|
|
||||||
EXPECT_CHAR_NO_CHECK('.');
|
|
||||||
PARSE_INT(minor_version, 1);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, size_t *num_headers,
|
|
||||||
size_t max_headers, int *ret)
|
|
||||||
{
|
|
||||||
for (;; ++*num_headers) {
|
|
||||||
CHECK_EOF();
|
|
||||||
if (*buf == '\015') {
|
|
||||||
++buf;
|
|
||||||
EXPECT_CHAR('\012');
|
|
||||||
break;
|
|
||||||
} else if (*buf == '\012') {
|
|
||||||
++buf;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (*num_headers == max_headers) {
|
|
||||||
*ret = -1;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (!(*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) {
|
|
||||||
/* parsing name, but do not discard SP before colon, see
|
|
||||||
* http://www.mozilla.org/security/announce/2006/mfsa2006-33.html */
|
|
||||||
if ((buf = parse_token(buf, buf_end, &headers[*num_headers].name, &headers[*num_headers].name_len, ':', ret)) == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (headers[*num_headers].name_len == 0) {
|
|
||||||
*ret = -1;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
++buf;
|
|
||||||
for (;; ++buf) {
|
|
||||||
CHECK_EOF();
|
|
||||||
if (!(*buf == ' ' || *buf == '\t')) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
headers[*num_headers].name = NULL;
|
|
||||||
headers[*num_headers].name_len = 0;
|
|
||||||
}
|
|
||||||
const char *value;
|
|
||||||
size_t value_len;
|
|
||||||
if ((buf = get_token_to_eol(buf, buf_end, &value, &value_len, ret)) == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
/* remove trailing SPs and HTABs */
|
|
||||||
const char *value_end = value + value_len;
|
|
||||||
for (; value_end != value; --value_end) {
|
|
||||||
const char c = *(value_end - 1);
|
|
||||||
if (!(c == ' ' || c == '\t')) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
headers[*num_headers].value = value;
|
|
||||||
headers[*num_headers].value_len = value_end - value;
|
|
||||||
}
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *parse_request(const char *buf, const char *buf_end, const char **method, size_t *method_len, const char **path,
|
|
||||||
size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers,
|
|
||||||
size_t max_headers, int *ret)
|
|
||||||
{
|
|
||||||
/* skip first empty line (some clients add CRLF after POST content) */
|
|
||||||
CHECK_EOF();
|
|
||||||
if (*buf == '\015') {
|
|
||||||
++buf;
|
|
||||||
EXPECT_CHAR('\012');
|
|
||||||
} else if (*buf == '\012') {
|
|
||||||
++buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* parse request line */
|
|
||||||
if ((buf = parse_token(buf, buf_end, method, method_len, ' ', ret)) == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
do {
|
|
||||||
++buf;
|
|
||||||
CHECK_EOF();
|
|
||||||
} while (*buf == ' ');
|
|
||||||
ADVANCE_TOKEN(*path, *path_len);
|
|
||||||
do {
|
|
||||||
++buf;
|
|
||||||
CHECK_EOF();
|
|
||||||
} while (*buf == ' ');
|
|
||||||
if (*method_len == 0 || *path_len == 0) {
|
|
||||||
*ret = -1;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (*buf == '\015') {
|
|
||||||
++buf;
|
|
||||||
EXPECT_CHAR('\012');
|
|
||||||
} else if (*buf == '\012') {
|
|
||||||
++buf;
|
|
||||||
} else {
|
|
||||||
*ret = -1;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path,
|
|
||||||
size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len)
|
|
||||||
{
|
|
||||||
const char *buf = buf_start, *buf_end = buf_start + len;
|
|
||||||
size_t max_headers = *num_headers;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
*method = NULL;
|
|
||||||
*method_len = 0;
|
|
||||||
*path = NULL;
|
|
||||||
*path_len = 0;
|
|
||||||
*minor_version = -1;
|
|
||||||
*num_headers = 0;
|
|
||||||
|
|
||||||
/* if last_len != 0, check if the request is complete (a fast countermeasure
|
|
||||||
againt slowloris */
|
|
||||||
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, minor_version, headers, num_headers, max_headers,
|
|
||||||
&r)) == NULL) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (int)(buf - buf_start);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *parse_response(const char *buf, const char *buf_end, int *minor_version, int *status, const char **msg,
|
|
||||||
size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t max_headers, int *ret)
|
|
||||||
{
|
|
||||||
/* parse "HTTP/1.x" */
|
|
||||||
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
/* skip space */
|
|
||||||
if (*buf != ' ') {
|
|
||||||
*ret = -1;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
do {
|
|
||||||
++buf;
|
|
||||||
CHECK_EOF();
|
|
||||||
} while (*buf == ' ');
|
|
||||||
/* parse status code, we want at least [:digit:][:digit:][:digit:]<other char> to try to parse */
|
|
||||||
if (buf_end - buf < 4) {
|
|
||||||
*ret = -2;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
PARSE_INT_3(status);
|
|
||||||
|
|
||||||
/* get message including preceding space */
|
|
||||||
if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (*msg_len == 0) {
|
|
||||||
/* ok */
|
|
||||||
} else if (**msg == ' ') {
|
|
||||||
/* Remove preceding space. Successful return from `get_token_to_eol` guarantees that we would hit something other than SP
|
|
||||||
* before running past the end of the given buffer. */
|
|
||||||
do {
|
|
||||||
++*msg;
|
|
||||||
--*msg_len;
|
|
||||||
} while (**msg == ' ');
|
|
||||||
} else {
|
|
||||||
/* garbage found after status code */
|
|
||||||
*ret = -1;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
int phr_parse_response(const char *buf_start, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
|
|
||||||
struct phr_header *headers, size_t *num_headers, size_t last_len)
|
|
||||||
{
|
|
||||||
const char *buf = buf_start, *buf_end = buf + len;
|
|
||||||
size_t max_headers = *num_headers;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
*minor_version = -1;
|
|
||||||
*status = 0;
|
|
||||||
*msg = NULL;
|
|
||||||
*msg_len = 0;
|
|
||||||
*num_headers = 0;
|
|
||||||
|
|
||||||
/* if last_len != 0, check if the response is complete (a fast countermeasure
|
|
||||||
against slowloris */
|
|
||||||
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len, headers, num_headers, max_headers, &r)) == NULL) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (int)(buf - buf_start);
|
|
||||||
}
|
|
||||||
|
|
||||||
int phr_parse_headers(const char *buf_start, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len)
|
|
||||||
{
|
|
||||||
const char *buf = buf_start, *buf_end = buf + len;
|
|
||||||
size_t max_headers = *num_headers;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
*num_headers = 0;
|
|
||||||
|
|
||||||
/* if last_len != 0, check if the response is complete (a fast countermeasure
|
|
||||||
against slowloris */
|
|
||||||
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers, &r)) == NULL) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (int)(buf - buf_start);
|
|
||||||
}
|
|
||||||
|
|
||||||
enum {
|
|
||||||
CHUNKED_IN_CHUNK_SIZE,
|
|
||||||
CHUNKED_IN_CHUNK_EXT,
|
|
||||||
CHUNKED_IN_CHUNK_DATA,
|
|
||||||
CHUNKED_IN_CHUNK_CRLF,
|
|
||||||
CHUNKED_IN_TRAILERS_LINE_HEAD,
|
|
||||||
CHUNKED_IN_TRAILERS_LINE_MIDDLE
|
|
||||||
};
|
|
||||||
|
|
||||||
static int decode_hex(int ch)
|
|
||||||
{
|
|
||||||
if ('0' <= ch && ch <= '9') {
|
|
||||||
return ch - '0';
|
|
||||||
} else if ('A' <= ch && ch <= 'F') {
|
|
||||||
return ch - 'A' + 0xa;
|
|
||||||
} else if ('a' <= ch && ch <= 'f') {
|
|
||||||
return ch - 'a' + 0xa;
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *_bufsz)
|
|
||||||
{
|
|
||||||
size_t dst = 0, src = 0, bufsz = *_bufsz;
|
|
||||||
ssize_t ret = -2; /* incomplete */
|
|
||||||
|
|
||||||
decoder->_total_read += bufsz;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
switch (decoder->_state) {
|
|
||||||
case CHUNKED_IN_CHUNK_SIZE:
|
|
||||||
for (;; ++src) {
|
|
||||||
int v;
|
|
||||||
if (src == bufsz)
|
|
||||||
goto Exit;
|
|
||||||
if ((v = decode_hex(buf[src])) == -1) {
|
|
||||||
if (decoder->_hex_count == 0) {
|
|
||||||
ret = -1;
|
|
||||||
goto Exit;
|
|
||||||
}
|
|
||||||
/* the only characters that may appear after the chunk size are BWS, semicolon, or CRLF */
|
|
||||||
switch (buf[src]) {
|
|
||||||
case ' ':
|
|
||||||
case '\011':
|
|
||||||
case ';':
|
|
||||||
case '\012':
|
|
||||||
case '\015':
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ret = -1;
|
|
||||||
goto Exit;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (decoder->_hex_count == sizeof(size_t) * 2) {
|
|
||||||
ret = -1;
|
|
||||||
goto Exit;
|
|
||||||
}
|
|
||||||
decoder->bytes_left_in_chunk = decoder->bytes_left_in_chunk * 16 + v;
|
|
||||||
++decoder->_hex_count;
|
|
||||||
}
|
|
||||||
decoder->_hex_count = 0;
|
|
||||||
decoder->_state = CHUNKED_IN_CHUNK_EXT;
|
|
||||||
/* fallthru */
|
|
||||||
case CHUNKED_IN_CHUNK_EXT:
|
|
||||||
/* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */
|
|
||||||
for (;; ++src) {
|
|
||||||
if (src == bufsz)
|
|
||||||
goto Exit;
|
|
||||||
if (buf[src] == '\012')
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
++src;
|
|
||||||
if (decoder->bytes_left_in_chunk == 0) {
|
|
||||||
if (decoder->consume_trailer) {
|
|
||||||
decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
goto Complete;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
decoder->_state = CHUNKED_IN_CHUNK_DATA;
|
|
||||||
/* fallthru */
|
|
||||||
case CHUNKED_IN_CHUNK_DATA: {
|
|
||||||
size_t avail = bufsz - src;
|
|
||||||
if (avail < decoder->bytes_left_in_chunk) {
|
|
||||||
if (dst != src)
|
|
||||||
memmove(buf + dst, buf + src, avail);
|
|
||||||
src += avail;
|
|
||||||
dst += avail;
|
|
||||||
decoder->bytes_left_in_chunk -= avail;
|
|
||||||
goto Exit;
|
|
||||||
}
|
|
||||||
if (dst != src)
|
|
||||||
memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk);
|
|
||||||
src += decoder->bytes_left_in_chunk;
|
|
||||||
dst += decoder->bytes_left_in_chunk;
|
|
||||||
decoder->bytes_left_in_chunk = 0;
|
|
||||||
decoder->_state = CHUNKED_IN_CHUNK_CRLF;
|
|
||||||
}
|
|
||||||
/* fallthru */
|
|
||||||
case CHUNKED_IN_CHUNK_CRLF:
|
|
||||||
for (;; ++src) {
|
|
||||||
if (src == bufsz)
|
|
||||||
goto Exit;
|
|
||||||
if (buf[src] != '\015')
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (buf[src] != '\012') {
|
|
||||||
ret = -1;
|
|
||||||
goto Exit;
|
|
||||||
}
|
|
||||||
++src;
|
|
||||||
decoder->_state = CHUNKED_IN_CHUNK_SIZE;
|
|
||||||
break;
|
|
||||||
case CHUNKED_IN_TRAILERS_LINE_HEAD:
|
|
||||||
for (;; ++src) {
|
|
||||||
if (src == bufsz)
|
|
||||||
goto Exit;
|
|
||||||
if (buf[src] != '\015')
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (buf[src++] == '\012')
|
|
||||||
goto Complete;
|
|
||||||
decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE;
|
|
||||||
/* fallthru */
|
|
||||||
case CHUNKED_IN_TRAILERS_LINE_MIDDLE:
|
|
||||||
for (;; ++src) {
|
|
||||||
if (src == bufsz)
|
|
||||||
goto Exit;
|
|
||||||
if (buf[src] == '\012')
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
++src;
|
|
||||||
decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
assert(!"decoder is corrupt");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Complete:
|
|
||||||
ret = bufsz - src;
|
|
||||||
Exit:
|
|
||||||
if (dst != src)
|
|
||||||
memmove(buf + dst, buf + src, bufsz - src);
|
|
||||||
*_bufsz = dst;
|
|
||||||
/* if incomplete but the overhead of the chunked encoding is >=100KB and >80%, signal an error */
|
|
||||||
if (ret == -2) {
|
|
||||||
decoder->_total_overhead += bufsz - dst;
|
|
||||||
if (decoder->_total_overhead >= 100 * 1024 && decoder->_total_read - decoder->_total_overhead < decoder->_total_read / 4)
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder)
|
|
||||||
{
|
|
||||||
return decoder->_state == CHUNKED_IN_CHUNK_DATA;
|
|
||||||
}
|
|
||||||
|
|
||||||
#undef CHECK_EOF
|
|
||||||
#undef EXPECT_CHAR
|
|
||||||
#undef ADVANCE_TOKEN
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
|
|
||||||
* Shigeo Mitsunari
|
|
||||||
*
|
|
||||||
* The software is licensed under either the MIT License (below) or the Perl
|
|
||||||
* license.
|
|
||||||
*
|
|
||||||
* 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 picohttpparser_h
|
|
||||||
#define picohttpparser_h
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#define ssize_t intptr_t
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* contains name and value of a header (name == NULL if is a continuing line
|
|
||||||
* of a multiline header */
|
|
||||||
struct phr_header {
|
|
||||||
const char *name;
|
|
||||||
size_t name_len;
|
|
||||||
const char *value;
|
|
||||||
size_t value_len;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* returns number of bytes consumed if successful, -2 if request is partial,
|
|
||||||
* -1 if failed */
|
|
||||||
int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len,
|
|
||||||
int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len);
|
|
||||||
|
|
||||||
/* ditto */
|
|
||||||
int phr_parse_response(const char *_buf, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
|
|
||||||
struct phr_header *headers, size_t *num_headers, size_t last_len);
|
|
||||||
|
|
||||||
/* ditto */
|
|
||||||
int phr_parse_headers(const char *buf, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len);
|
|
||||||
|
|
||||||
/* should be zero-filled before start */
|
|
||||||
struct phr_chunked_decoder {
|
|
||||||
size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
|
|
||||||
char consume_trailer; /* if trailing headers should be consumed */
|
|
||||||
char _hex_count;
|
|
||||||
char _state;
|
|
||||||
uint64_t _total_read;
|
|
||||||
uint64_t _total_overhead;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* the function rewrites the buffer given as (buf, bufsz) removing the chunked-
|
|
||||||
* encoding headers. When the function returns without an error, bufsz is
|
|
||||||
* updated to the length of the decoded data available. Applications should
|
|
||||||
* repeatedly call the function while it returns -2 (incomplete) every time
|
|
||||||
* supplying newly arrived data. If the end of the chunked-encoded data is
|
|
||||||
* found, the function returns a non-negative number indicating the number of
|
|
||||||
* octets left undecoded, that starts from the offset returned by `*bufsz`.
|
|
||||||
* Returns -1 on error.
|
|
||||||
*/
|
|
||||||
ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *bufsz);
|
|
||||||
|
|
||||||
/* returns if the chunked decoder is in middle of chunked data */
|
|
||||||
int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,134 +1,10 @@
|
|||||||
#include "qjs_blob.h"
|
#include "qjs_blob.h"
|
||||||
#include "picohttpparser.h"
|
#define PAR_EASYCURL_IMPLEMENTATION
|
||||||
#include <mbedtls/net_sockets.h>
|
#include "thirdparty/par/par_easycurl.h"
|
||||||
#include <mbedtls/ssl.h>
|
|
||||||
#include <mbedtls/entropy.h>
|
|
||||||
#include <mbedtls/ctr_drbg.h>
|
|
||||||
#include <mbedtls/error.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#if defined(__MINGW32__) || defined(__MINGW64__)
|
|
||||||
static void *memmem(const void *hay, size_t haylen,
|
|
||||||
const void *ndl, size_t ndllen) {
|
|
||||||
const unsigned char *h = hay, *n = ndl;
|
|
||||||
if (!ndllen) return (void*)h;
|
|
||||||
haylen -= ndllen - 1;
|
|
||||||
for (size_t i = 0; i < haylen; i++)
|
|
||||||
if (memcmp(h + i, n, ndllen) == 0)
|
|
||||||
return (void*)(h + i);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *strndup(const char *s, size_t n) {
|
|
||||||
size_t len = strnlen(s, n);
|
|
||||||
char *d = malloc(len + 1);
|
|
||||||
if (!d) return NULL;
|
|
||||||
memcpy(d, s, len);
|
|
||||||
d[len] = '\0';
|
|
||||||
return d;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Simple dynamic buffer for reading the response
|
|
||||||
typedef struct {
|
|
||||||
char *data;
|
|
||||||
size_t size;
|
|
||||||
size_t capacity;
|
|
||||||
} buffer_t;
|
|
||||||
|
|
||||||
static void buffer_init(buffer_t *b) {
|
|
||||||
b->data = NULL;
|
|
||||||
b->size = 0;
|
|
||||||
b->capacity = 0;
|
|
||||||
}
|
|
||||||
static int buffer_append(buffer_t *b, const void *p, size_t len) {
|
|
||||||
if (b->size + len > b->capacity) {
|
|
||||||
size_t new_cap = b->capacity ? b->capacity * 2 : 4096;
|
|
||||||
while (new_cap < b->size + len) new_cap *= 2;
|
|
||||||
char *nx = realloc(b->data, new_cap);
|
|
||||||
if (!nx) return -1;
|
|
||||||
b->data = nx;
|
|
||||||
b->capacity = new_cap;
|
|
||||||
}
|
|
||||||
memcpy(b->data + b->size, p, len);
|
|
||||||
b->size += len;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
static void buffer_free(buffer_t *b) {
|
|
||||||
free(b->data);
|
|
||||||
b->data = NULL;
|
|
||||||
b->size = 0;
|
|
||||||
b->capacity = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parses "http://host:port/path" or "https://host:port/path"
|
|
||||||
static int parse_url(const char *url, int *use_ssl,
|
|
||||||
char **host, char **port, char **path) {
|
|
||||||
*host = *port = *path = NULL;
|
|
||||||
*use_ssl = 0;
|
|
||||||
const char *p = url;
|
|
||||||
if (strncmp(p, "https://", 8) == 0) {
|
|
||||||
*use_ssl = 1;
|
|
||||||
p += 8;
|
|
||||||
} else if (strncmp(p, "http://", 7) == 0) {
|
|
||||||
*use_ssl = 0;
|
|
||||||
p += 7;
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
// find path
|
|
||||||
const char *path_start = strchr(p, '/');
|
|
||||||
size_t hostport_len = path_start ? (size_t)(path_start - p) : strlen(p);
|
|
||||||
*path = path_start ? strdup(path_start) : strdup("/");
|
|
||||||
if (!*path) return -1;
|
|
||||||
|
|
||||||
// check for explicit port
|
|
||||||
const char *colon = memmem(p, hostport_len, ":", 1);
|
|
||||||
if (colon) {
|
|
||||||
size_t hlen = (size_t)(colon - p);
|
|
||||||
*host = strndup(p, hlen);
|
|
||||||
size_t plen = hostport_len - hlen - 1;
|
|
||||||
*port = strndup(colon + 1, plen);
|
|
||||||
} else {
|
|
||||||
*host = strndup(p, hostport_len);
|
|
||||||
*port = strdup(*use_ssl ? "443" : "80");
|
|
||||||
}
|
|
||||||
if (!*host || !*port) {
|
|
||||||
free(*host); free(*port); free(*path);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Builds a GET request into buf
|
|
||||||
static int build_get_request(buffer_t *buf,
|
|
||||||
const char *host, const char *port, const char *path) {
|
|
||||||
// If port is “80” for HTTP or “443” for HTTPS, omit the “:port” in Host:
|
|
||||||
char host_header[512];
|
|
||||||
if ((strcmp(port, "80") == 0) || (strcmp(port, "443") == 0)) {
|
|
||||||
// default port: just “Host: hostname”
|
|
||||||
snprintf(host_header, sizeof(host_header), "Host: %s\r\n", host);
|
|
||||||
} else {
|
|
||||||
// non-default port: “Host: hostname:port”
|
|
||||||
snprintf(host_header, sizeof(host_header), "Host: %s:%s\r\n", host, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now build the rest of the request, including a User-Agent:
|
|
||||||
char tmp[1024];
|
|
||||||
int n = snprintf(tmp, sizeof(tmp),
|
|
||||||
"GET %s HTTP/1.1\r\n"
|
|
||||||
"%s" // the host_header line
|
|
||||||
"User-Agent: ProsperonFetch/1.0\r\n"
|
|
||||||
"Accept: */*\r\n"
|
|
||||||
"Connection: close\r\n"
|
|
||||||
"\r\n",
|
|
||||||
path, host_header);
|
|
||||||
if (n < 0) return -1;
|
|
||||||
return buffer_append(buf, tmp, (size_t)n);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Performs a blocking HTTP GET and returns a QuickJS Blob of the body
|
// Performs a blocking HTTP GET and returns a QuickJS Blob of the body
|
||||||
static JSValue js_fetch_picoparser(JSContext *ctx, JSValueConst this_val,
|
static JSValue js_fetch_picoparser(JSContext *ctx, JSValueConst this_val,
|
||||||
int argc, JSValueConst *argv) {
|
int argc, JSValueConst *argv) {
|
||||||
@@ -138,158 +14,18 @@ static JSValue js_fetch_picoparser(JSContext *ctx, JSValueConst this_val,
|
|||||||
const char *url = JS_ToCString(ctx, argv[0]);
|
const char *url = JS_ToCString(ctx, argv[0]);
|
||||||
if (!url) return JS_ThrowTypeError(ctx, "fetch: invalid URL");
|
if (!url) return JS_ThrowTypeError(ctx, "fetch: invalid URL");
|
||||||
|
|
||||||
int use_ssl = 0;
|
par_byte *data = NULL;
|
||||||
char *host = NULL, *port = NULL, *path = NULL;
|
int nbytes = 0;
|
||||||
if (parse_url(url, &use_ssl, &host, &port, &path) < 0) {
|
if (!par_easycurl_to_memory(url, &data, &nbytes)) {
|
||||||
JS_FreeCString(ctx, url);
|
JS_FreeCString(ctx, url);
|
||||||
return JS_ThrowTypeError(ctx, "fetch: URL parse error");
|
return JS_ThrowTypeError(ctx, "fetch: failed to fetch URL");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare mbedTLS structures
|
|
||||||
mbedtls_net_context net_ctx;
|
|
||||||
mbedtls_ssl_context ssl;
|
|
||||||
mbedtls_ssl_config conf;
|
|
||||||
mbedtls_ctr_drbg_context drbg;
|
|
||||||
mbedtls_entropy_context entropy;
|
|
||||||
mbedtls_net_init(&net_ctx);
|
|
||||||
mbedtls_ssl_init(&ssl);
|
|
||||||
mbedtls_ssl_config_init(&conf);
|
|
||||||
mbedtls_ctr_drbg_init(&drbg);
|
|
||||||
mbedtls_entropy_init(&entropy);
|
|
||||||
|
|
||||||
int ret;
|
|
||||||
const char *pers = "js_fetch";
|
|
||||||
if (use_ssl) {
|
|
||||||
if ((ret = mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy,
|
|
||||||
(const unsigned char *)pers,
|
|
||||||
strlen(pers))) != 0)
|
|
||||||
goto cleanup_tls;
|
|
||||||
if ((ret = mbedtls_ssl_config_defaults(&conf,
|
|
||||||
MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
||||||
MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
|
|
||||||
goto cleanup_tls;
|
|
||||||
mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
|
|
||||||
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &drbg);
|
|
||||||
if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0)
|
|
||||||
goto cleanup_tls;
|
|
||||||
if ((ret = mbedtls_ssl_set_hostname(&ssl, host)) != 0)
|
|
||||||
goto cleanup_tls;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Connect to host:port
|
|
||||||
if ((ret = mbedtls_net_connect(&net_ctx, host, port,
|
|
||||||
MBEDTLS_NET_PROTO_TCP)) != 0)
|
|
||||||
goto cleanup_tls;
|
|
||||||
|
|
||||||
if (use_ssl) {
|
|
||||||
mbedtls_ssl_set_bio(&ssl, &net_ctx,
|
|
||||||
mbedtls_net_send, mbedtls_net_recv, NULL);
|
|
||||||
while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
|
|
||||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
|
||||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
||||||
goto cleanup_all;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build and send GET request
|
|
||||||
buffer_t req = {0}, resp = {0};
|
|
||||||
buffer_init(&req);
|
|
||||||
buffer_init(&resp);
|
|
||||||
if (build_get_request(&req, host, port, path) < 0)
|
|
||||||
goto cleanup_all;
|
|
||||||
|
|
||||||
// Write request
|
|
||||||
size_t off = 0;
|
|
||||||
while (off < req.size) {
|
|
||||||
if (use_ssl) {
|
|
||||||
ret = mbedtls_ssl_write(&ssl,
|
|
||||||
(const unsigned char *)req.data + off,
|
|
||||||
req.size - off);
|
|
||||||
if (ret == MBEDTLS_ERR_SSL_WANT_READ ||
|
|
||||||
ret == MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
ret = mbedtls_net_send(&net_ctx,
|
|
||||||
(unsigned char *)req.data + off,
|
|
||||||
req.size - off);
|
|
||||||
}
|
|
||||||
if (ret <= 0) goto cleanup_all;
|
|
||||||
off += (size_t)ret;
|
|
||||||
}
|
|
||||||
buffer_free(&req);
|
|
||||||
|
|
||||||
// Read entire response into resp.data
|
|
||||||
unsigned char buf[4096];
|
|
||||||
do {
|
|
||||||
if (use_ssl) {
|
|
||||||
ret = mbedtls_ssl_read(&ssl, buf, sizeof(buf));
|
|
||||||
if (ret == MBEDTLS_ERR_SSL_WANT_READ ||
|
|
||||||
ret == MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
ret = mbedtls_net_recv(&net_ctx, buf, sizeof(buf));
|
|
||||||
}
|
|
||||||
if (ret > 0) {
|
|
||||||
if (buffer_append(&resp, buf, (size_t)ret) < 0)
|
|
||||||
goto cleanup_all;
|
|
||||||
}
|
|
||||||
} while (ret > 0);
|
|
||||||
|
|
||||||
// Parse status-line + headers with picohttpparser
|
|
||||||
struct phr_header hdrv[32];
|
|
||||||
size_t hdr_count = sizeof(hdrv)/sizeof(hdrv[0]);
|
|
||||||
int minor, status;
|
|
||||||
const char *reason = NULL;
|
|
||||||
size_t reason_len = 0;
|
|
||||||
int pret = phr_parse_response(
|
|
||||||
resp.data, resp.size,
|
|
||||||
&minor, &status,
|
|
||||||
&reason, &reason_len, /* provide pointers, even if unused */
|
|
||||||
hdrv, &hdr_count,
|
|
||||||
0);
|
|
||||||
if (pret < 0) goto cleanup_all;
|
|
||||||
size_t header_len = (size_t)pret;
|
|
||||||
char *body_ptr = resp.data + header_len;
|
|
||||||
size_t body_len = resp.size - header_len;
|
|
||||||
|
|
||||||
// Check for chunked encoding
|
|
||||||
int is_chunked = 0;
|
|
||||||
for (size_t i = 0; i < hdr_count; i++) {
|
|
||||||
if ((hdrv[i].name_len == 17 &&
|
|
||||||
!strncasecmp(hdrv[i].name, "Transfer-Encoding", 17) &&
|
|
||||||
memmem(hdrv[i].value, hdrv[i].value_len, "chunked", 7))) {
|
|
||||||
is_chunked = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If chunked, decode in-place
|
|
||||||
if (is_chunked) {
|
|
||||||
struct phr_chunked_decoder cd = {0};
|
|
||||||
size_t blen = body_len;
|
|
||||||
ssize_t rest = phr_decode_chunked(&cd, body_ptr, &blen);
|
|
||||||
if (rest < 0) goto cleanup_all;
|
|
||||||
// blen = decoded length; rest = trailer start offset (ignored)
|
|
||||||
body_len = blen;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return a Blob wrapping the body buffer (no extra copy)
|
|
||||||
JSValue blob = js_new_blob_stoned_copy(ctx,
|
|
||||||
(uint8_t *)body_ptr, body_len);
|
|
||||||
|
|
||||||
cleanup_all:
|
|
||||||
buffer_free(&resp);
|
|
||||||
mbedtls_ssl_close_notify(&ssl);
|
|
||||||
|
|
||||||
cleanup_tls:
|
|
||||||
mbedtls_net_free(&net_ctx);
|
|
||||||
mbedtls_ssl_free(&ssl);
|
|
||||||
mbedtls_ssl_config_free(&conf);
|
|
||||||
mbedtls_ctr_drbg_free(&drbg);
|
|
||||||
mbedtls_entropy_free(&entropy);
|
|
||||||
|
|
||||||
JS_FreeCString(ctx, url);
|
JS_FreeCString(ctx, url);
|
||||||
free(host); free(port); free(path);
|
|
||||||
|
// Return a Blob wrapping the data
|
||||||
|
JSValue blob = js_new_blob_stoned_copy(ctx, data, (size_t)nbytes);
|
||||||
|
free(data); // par_easycurl allocates with malloc, so we free it
|
||||||
return blob;
|
return blob;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,6 +34,7 @@ static const JSCFunctionListEntry js_http_funcs[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
JSValue js_http_use(JSContext *js) {
|
JSValue js_http_use(JSContext *js) {
|
||||||
|
par_easycurl_init(0); // Initialize curl
|
||||||
JSValue obj = JS_NewObject(js);
|
JSValue obj = JS_NewObject(js);
|
||||||
JS_SetPropertyFunctionList(js, obj, js_http_funcs,
|
JS_SetPropertyFunctionList(js, obj, js_http_funcs,
|
||||||
sizeof(js_http_funcs)/sizeof(js_http_funcs[0]));
|
sizeof(js_http_funcs)/sizeof(js_http_funcs[0]));
|
||||||
|
|||||||
3265
source/qjs_steam.cpp
3265
source/qjs_steam.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,16 +0,0 @@
|
|||||||
#ifndef QJS_STEAM_H
|
|
||||||
#define QJS_STEAM_H
|
|
||||||
|
|
||||||
#include "cell.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
JSValue js_steam_use(JSContext *js);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif // QJS_STEAM_H
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
[wrap-git]
|
|
||||||
url = https://github.com/Mbed-TLS/mbedtls.git
|
|
||||||
revision = v3.6.3.1
|
|
||||||
depth = 1
|
|
||||||
@@ -1,15 +1,19 @@
|
|||||||
var http = use('http')
|
var http = use('http')
|
||||||
var text = use('text')
|
var text = use('text')
|
||||||
var time = use('time')
|
var time = use('time')
|
||||||
|
var io = use('io')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var st = time.number()
|
var st = time.number()
|
||||||
var b2 = http.fetch(arg[0])
|
var b2 = http.fetch(arg[0])
|
||||||
log.console(`time took ${time.number()-st}`)
|
log.console(`time took ${time.number()-st}`)
|
||||||
log.console(b2.length)
|
log.console(b2.length)
|
||||||
var text2 = text(b2)
|
io.writepath('.')
|
||||||
log.console(text(b2))
|
io.slurpwrite("download.zip", b2)
|
||||||
$_.stop()
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.console("dictionary error:", e)
|
log.console("error:", e)
|
||||||
|
log.console("message:", e.message)
|
||||||
|
log.console("stack:", e.stack)
|
||||||
|
} finally {
|
||||||
|
$_.stop()
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user