From 9408093b0e3627e2d15d74fec6b6ed8900f00f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Mon, 18 Sep 2023 19:59:59 +0200 Subject: [PATCH] net: use InternetReadFile() on win32 tcc (r-lyeh) --- engine/joint/v4k.h | 39 +++++++++++++++++++++++++++++++++++++- engine/split/v4k_network.c | 39 +++++++++++++++++++++++++++++++++++++- engine/v4k.c | 39 +++++++++++++++++++++++++++++++++++++- 3 files changed, 114 insertions(+), 3 deletions(-) diff --git a/engine/joint/v4k.h b/engine/joint/v4k.h index b8bd187..903006d 100644 --- a/engine/joint/v4k.h +++ b/engine/joint/v4k.h @@ -338571,8 +338571,43 @@ void* forget( void *ptr ) { #line 0 #line 1 "v4k_network.c" +#if is(tcc) && is(win32) // @fixme: https lib is broken with tcc. replaced with InternetReadFile() api for now -// @fixme: broken with tcc -m64 (our default tcc configuration) +# include +# pragma comment(lib,"wininet") + +int download_file( FILE *out, const char *url ) { + int ok = false; + char buffer[ 4096 ]; + DWORD response_size = 0; + + if( out ) + for( HINTERNET session = InternetOpenA("v4k.download_file", PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0); session; InternetCloseHandle(session), session = 0 ) + for( HINTERNET request = InternetOpenUrlA(session, url, NULL, 0, INTERNET_FLAG_RELOAD, 0); request; InternetCloseHandle(request), request = 0 ) + for(; InternetReadFile(request, buffer, sizeof(buffer), &response_size) != FALSE && response_size > 0; ) { + ok = (fwrite(buffer, response_size, 1, out) == 1); + if(!ok) break; + } + + return ok; +} + +array(char) download( const char *url ) { + int ok = false; + char buffer[ 4096 ]; + DWORD response_size = 0, pos = 0; + + array(char) out = 0; + for( HINTERNET session = InternetOpenA("v4k.download", PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0); session; InternetCloseHandle(session), session = 0 ) + for( HINTERNET request = InternetOpenUrlA(session, url, NULL, 0, INTERNET_FLAG_RELOAD, 0); request; InternetCloseHandle(request), request = 0 ) + for(; InternetReadFile(request, buffer, sizeof(buffer), &response_size) != FALSE && response_size > 0; ) { + array_resize(out, pos + response_size); + ok = !!memcpy(out + (pos += response_size) - response_size, buffer, response_size); + } + + return out; +} +#else int download_file( FILE *out, const char *url ) { int ok = false; if( out ) for( https_t *h = https_get(url, NULL); h; https_release(h), h = NULL ) { @@ -338597,11 +338632,13 @@ array(char) download( const char *url ) { } return out; } +#endif bool network_tests() { // network test (https) array(char) webfile = download("https://www.google.com/"); printf("Network test: %d bytes downloaded from google.com\n", array_count(webfile)); + // array_push(webfile, '\0'); puts(webfile); return true; } diff --git a/engine/split/v4k_network.c b/engine/split/v4k_network.c index ac49c96..6c9bcf9 100644 --- a/engine/split/v4k_network.c +++ b/engine/split/v4k_network.c @@ -1,5 +1,40 @@ +#if is(tcc) && is(win32) // @fixme: https lib is broken with tcc. replaced with InternetReadFile() api for now -// @fixme: broken with tcc -m64 (our default tcc configuration) +# include +# pragma comment(lib,"wininet") + +int download_file( FILE *out, const char *url ) { + int ok = false; + char buffer[ 4096 ]; + DWORD response_size = 0; + + if( out ) + for( HINTERNET session = InternetOpenA("v4k.download_file", PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0); session; InternetCloseHandle(session), session = 0 ) + for( HINTERNET request = InternetOpenUrlA(session, url, NULL, 0, INTERNET_FLAG_RELOAD, 0); request; InternetCloseHandle(request), request = 0 ) + for(; InternetReadFile(request, buffer, sizeof(buffer), &response_size) != FALSE && response_size > 0; ) { + ok = (fwrite(buffer, response_size, 1, out) == 1); + if(!ok) break; + } + + return ok; +} + +array(char) download( const char *url ) { + int ok = false; + char buffer[ 4096 ]; + DWORD response_size = 0, pos = 0; + + array(char) out = 0; + for( HINTERNET session = InternetOpenA("v4k.download", PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0); session; InternetCloseHandle(session), session = 0 ) + for( HINTERNET request = InternetOpenUrlA(session, url, NULL, 0, INTERNET_FLAG_RELOAD, 0); request; InternetCloseHandle(request), request = 0 ) + for(; InternetReadFile(request, buffer, sizeof(buffer), &response_size) != FALSE && response_size > 0; ) { + array_resize(out, pos + response_size); + ok = !!memcpy(out + (pos += response_size) - response_size, buffer, response_size); + } + + return out; +} +#else int download_file( FILE *out, const char *url ) { int ok = false; if( out ) for( https_t *h = https_get(url, NULL); h; https_release(h), h = NULL ) { @@ -24,11 +59,13 @@ array(char) download( const char *url ) { } return out; } +#endif bool network_tests() { // network test (https) array(char) webfile = download("https://www.google.com/"); printf("Network test: %d bytes downloaded from google.com\n", array_count(webfile)); + // array_push(webfile, '\0'); puts(webfile); return true; } diff --git a/engine/v4k.c b/engine/v4k.c index 0f17309..ac9d5f3 100644 --- a/engine/v4k.c +++ b/engine/v4k.c @@ -9266,8 +9266,43 @@ void* forget( void *ptr ) { #line 0 #line 1 "v4k_network.c" +#if is(tcc) && is(win32) // @fixme: https lib is broken with tcc. replaced with InternetReadFile() api for now -// @fixme: broken with tcc -m64 (our default tcc configuration) +# include +# pragma comment(lib,"wininet") + +int download_file( FILE *out, const char *url ) { + int ok = false; + char buffer[ 4096 ]; + DWORD response_size = 0; + + if( out ) + for( HINTERNET session = InternetOpenA("v4k.download_file", PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0); session; InternetCloseHandle(session), session = 0 ) + for( HINTERNET request = InternetOpenUrlA(session, url, NULL, 0, INTERNET_FLAG_RELOAD, 0); request; InternetCloseHandle(request), request = 0 ) + for(; InternetReadFile(request, buffer, sizeof(buffer), &response_size) != FALSE && response_size > 0; ) { + ok = (fwrite(buffer, response_size, 1, out) == 1); + if(!ok) break; + } + + return ok; +} + +array(char) download( const char *url ) { + int ok = false; + char buffer[ 4096 ]; + DWORD response_size = 0, pos = 0; + + array(char) out = 0; + for( HINTERNET session = InternetOpenA("v4k.download", PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0); session; InternetCloseHandle(session), session = 0 ) + for( HINTERNET request = InternetOpenUrlA(session, url, NULL, 0, INTERNET_FLAG_RELOAD, 0); request; InternetCloseHandle(request), request = 0 ) + for(; InternetReadFile(request, buffer, sizeof(buffer), &response_size) != FALSE && response_size > 0; ) { + array_resize(out, pos + response_size); + ok = !!memcpy(out + (pos += response_size) - response_size, buffer, response_size); + } + + return out; +} +#else int download_file( FILE *out, const char *url ) { int ok = false; if( out ) for( https_t *h = https_get(url, NULL); h; https_release(h), h = NULL ) { @@ -9292,11 +9327,13 @@ array(char) download( const char *url ) { } return out; } +#endif bool network_tests() { // network test (https) array(char) webfile = download("https://www.google.com/"); printf("Network test: %d bytes downloaded from google.com\n", array_count(webfile)); + // array_push(webfile, '\0'); puts(webfile); return true; }