From 5dc479386e3451fb2f1429b19c41d4a3cdd5423b Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Wed, 28 Sep 2022 21:07:31 +0200 Subject: [PATCH] [xhr] Handle timeout. Also hexify post body Timeout is silent now. --- src/ecmascript/spidermonkey/xhr.cpp | 35 ++++++++++++++++++++++------- src/network/connection.c | 16 +++++++++++++ src/network/connection.h | 2 ++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/ecmascript/spidermonkey/xhr.cpp b/src/ecmascript/spidermonkey/xhr.cpp index 71849bd25..1ab342040 100644 --- a/src/ecmascript/spidermonkey/xhr.cpp +++ b/src/ecmascript/spidermonkey/xhr.cpp @@ -685,23 +685,42 @@ xhr_send(JSContext *ctx, unsigned int argc, JS::Value *rval) if (xhr->method == POST && argc == 1) { body = jsval_to_string(ctx, args[0]); - xhr->uri->post = body; - char *url2 = get_uri_string(xhr->uri, URI_DIR_LOCATION | URI_PATH | URI_USER | URI_PASSWORD | URI_POST); - if (!url2) { + if (body) { + struct string post; + if (!init_string(&post)) { + mem_free(body); + } + + add_to_string(&post, "text/plain\n"); + for (int i = 0; body[i]; i++) { + char p[3]; + + ulonghexcat(p, NULL, (int)body[i], 2, '0', 0); + add_to_string(&post, p); + } + xhr->uri->post = post.source; + char *url2 = get_uri_string(xhr->uri, URI_DIR_LOCATION | URI_PATH | URI_USER | URI_PASSWORD | URI_POST); + done_string(&post); + + if (!url2) { + mem_free(body); + return false; + } + done_uri(xhr->uri); + xhr->uri = get_uri(url2, URI_DIR_LOCATION | URI_PATH | URI_USER | URI_PASSWORD | URI_POST); + mem_free(url2); mem_free(body); - return false; } - done_uri(xhr->uri); - xhr->uri = get_uri(url2, URI_DIR_LOCATION | URI_PATH | URI_USER | URI_PASSWORD | URI_POST); - mem_free(url2); - mem_free(body); } xhr->download.data = xhr; xhr->download.callback = (download_callback_T *)xhr_loading_callback; if (xhr->uri) { load_uri(xhr->uri, doc_view->session->referrer, &xhr->download, PRI_MAIN, CACHE_MODE_NORMAL, -1); + if (xhr->timeout) { + set_connection_timeout_xhr(xhr->download.conn, xhr->timeout); + } } args.rval().setUndefined(); diff --git a/src/network/connection.c b/src/network/connection.c index f78ad3a7b..f829bf5c4 100644 --- a/src/network/connection.c +++ b/src/network/connection.c @@ -1205,6 +1205,9 @@ connection_timeout_1(struct connection *conn) void set_connection_timeout(struct connection *conn) { + if (conn->xhr_timeout) { + return; + } kill_timer(&conn->timer); install_timer(&conn->timer, (milliseconds_T) @@ -1214,6 +1217,19 @@ set_connection_timeout(struct connection *conn) * 500), (void (*)(void *)) connection_timeout_1, conn); } +static void +connection_timeout_xhr_1(struct connection *conn) +{ + install_timer(&conn->timer, conn->xhr_timeout / 2, (void (*)(void *)) connection_timeout, conn); +} + +void +set_connection_timeout_xhr(struct connection *conn, milliseconds_T timeout) +{ + kill_timer(&conn->timer); + conn->xhr_timeout = timeout; + install_timer(&conn->timer, timeout / 2, (void (*)(void *)) connection_timeout_xhr_1, conn); +} void abort_all_connections(void) diff --git a/src/network/connection.h b/src/network/connection.h index 27815bafc..ff1e0e696 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -65,6 +65,7 @@ struct connection { int tries; timer_id_T timer; + milliseconds_T xhr_timeout; unsigned int running:1; unsigned int unrestartable:1; @@ -111,6 +112,7 @@ void abort_all_connections(void); void abort_background_connections(void); void set_connection_timeout(struct connection *); +void set_connection_timeout_xhr(struct connection *conn, milliseconds_T timeout); void shutdown_connection_stream(struct connection *conn);