net: use InternetReadFile() on win32 tcc (r-lyeh)

main
Dominik Madarász 2023-09-18 19:59:59 +02:00
parent 20796c35fa
commit 9408093b0e
3 changed files with 114 additions and 3 deletions

View File

@ -338571,8 +338571,43 @@ void* forget( void *ptr ) {
#line 0 #line 0
#line 1 "v4k_network.c" #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 <wininet.h>
# 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 download_file( FILE *out, const char *url ) {
int ok = false; int ok = false;
if( out ) for( https_t *h = https_get(url, NULL); h; https_release(h), h = NULL ) { 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; return out;
} }
#endif
bool network_tests() { bool network_tests() {
// network test (https) // network test (https)
array(char) webfile = download("https://www.google.com/"); array(char) webfile = download("https://www.google.com/");
printf("Network test: %d bytes downloaded from google.com\n", array_count(webfile)); printf("Network test: %d bytes downloaded from google.com\n", array_count(webfile));
// array_push(webfile, '\0'); puts(webfile);
return true; return true;
} }

View File

@ -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 <wininet.h>
# 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 download_file( FILE *out, const char *url ) {
int ok = false; int ok = false;
if( out ) for( https_t *h = https_get(url, NULL); h; https_release(h), h = NULL ) { 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; return out;
} }
#endif
bool network_tests() { bool network_tests() {
// network test (https) // network test (https)
array(char) webfile = download("https://www.google.com/"); array(char) webfile = download("https://www.google.com/");
printf("Network test: %d bytes downloaded from google.com\n", array_count(webfile)); printf("Network test: %d bytes downloaded from google.com\n", array_count(webfile));
// array_push(webfile, '\0'); puts(webfile);
return true; return true;
} }

View File

@ -9266,8 +9266,43 @@ void* forget( void *ptr ) {
#line 0 #line 0
#line 1 "v4k_network.c" #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 <wininet.h>
# 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 download_file( FILE *out, const char *url ) {
int ok = false; int ok = false;
if( out ) for( https_t *h = https_get(url, NULL); h; https_release(h), h = NULL ) { 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; return out;
} }
#endif
bool network_tests() { bool network_tests() {
// network test (https) // network test (https)
array(char) webfile = download("https://www.google.com/"); array(char) webfile = download("https://www.google.com/");
printf("Network test: %d bytes downloaded from google.com\n", array_count(webfile)); printf("Network test: %d bytes downloaded from google.com\n", array_count(webfile));
// array_push(webfile, '\0'); puts(webfile);
return true; return true;
} }