2018-05-18 23:18:10 +03:00
|
|
|
//===================================================//
|
|
|
|
// //
|
|
|
|
// HTTP //
|
|
|
|
// //
|
|
|
|
//===================================================//
|
|
|
|
|
|
|
|
struct _http
|
|
|
|
{
|
|
|
|
dword url;
|
|
|
|
dword transfer;
|
|
|
|
dword content_length;
|
|
|
|
dword content_received;
|
|
|
|
dword status_code;
|
|
|
|
dword receive_result;
|
|
|
|
dword content_pointer;
|
2018-05-20 16:40:58 +03:00
|
|
|
char redirect_url[4096*3];
|
|
|
|
char finaladress[4096*3];
|
2018-05-18 23:18:10 +03:00
|
|
|
|
|
|
|
dword get();
|
|
|
|
void free();
|
|
|
|
void receive();
|
|
|
|
bool handle_redirect();
|
|
|
|
};
|
|
|
|
|
|
|
|
dword _http::get(dword _url)
|
|
|
|
{
|
|
|
|
url = _url;
|
|
|
|
http_get stdcall (url, 0, 0, #accept_language);
|
|
|
|
transfer = EAX;
|
|
|
|
return transfer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _http::free()
|
|
|
|
{
|
|
|
|
http_free stdcall (transfer);
|
|
|
|
transfer=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _http::receive()
|
|
|
|
{
|
|
|
|
http_receive stdcall (transfer);
|
|
|
|
receive_result = EAX;
|
|
|
|
|
2018-05-20 16:40:58 +03:00
|
|
|
EDI = transfer;
|
|
|
|
if (!EAX) {
|
|
|
|
status_code = EDI.http_msg.status;
|
|
|
|
content_pointer = EDI.http_msg.content_ptr;
|
|
|
|
}
|
|
|
|
content_length = EDI.http_msg.content_length;
|
|
|
|
content_received = EDI.http_msg.content_received;
|
2018-05-18 23:18:10 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-05 02:50:41 +03:00
|
|
|
:bool _http::handle_redirect()
|
2018-05-18 23:18:10 +03:00
|
|
|
{
|
2018-05-20 16:40:58 +03:00
|
|
|
dword redirect;
|
2018-05-18 23:18:10 +03:00
|
|
|
http_find_header_field stdcall (transfer, "location\0");
|
|
|
|
if (EAX!=0) {
|
|
|
|
ESI = EAX;
|
|
|
|
EDI = #redirect_url;
|
|
|
|
do {
|
|
|
|
$lodsb;
|
|
|
|
$stosb;
|
|
|
|
} while (AL != 0) && (AL != 13) && (AL != 10);
|
|
|
|
DSBYTE[EDI-1]='\0';
|
|
|
|
get_absolute_url(#finaladress, url, #redirect_url);
|
|
|
|
strcpy(url, #finaladress);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===================================================//
|
|
|
|
// //
|
|
|
|
// DOWNLOADER //
|
|
|
|
// //
|
|
|
|
//===================================================//
|
|
|
|
|
2016-01-15 02:05:51 +03:00
|
|
|
|
|
|
|
enum {
|
|
|
|
STATE_NOT_STARTED,
|
|
|
|
STATE_IN_PROGRESS,
|
|
|
|
STATE_COMPLETED
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DOWNLOADER {
|
2018-05-18 23:18:10 +03:00
|
|
|
_http httpd;
|
2016-01-15 02:05:51 +03:00
|
|
|
dword bufpointer, bufsize, url;
|
2018-05-18 23:18:10 +03:00
|
|
|
int state;
|
2016-01-15 02:05:51 +03:00
|
|
|
dword Start();
|
|
|
|
void Stop();
|
2018-05-20 16:40:58 +03:00
|
|
|
bool MonitorProgress();
|
2018-05-20 16:54:35 +03:00
|
|
|
};
|
2016-01-15 02:05:51 +03:00
|
|
|
|
|
|
|
dword DOWNLOADER::Start(dword _url)
|
|
|
|
{
|
|
|
|
url = _url;
|
|
|
|
state = STATE_IN_PROGRESS;
|
2018-05-18 23:18:10 +03:00
|
|
|
httpd.get(url);
|
2018-05-24 21:47:42 +03:00
|
|
|
if (!httpd.transfer) Stop();
|
2018-05-18 23:18:10 +03:00
|
|
|
return httpd.transfer;
|
2016-01-15 02:05:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DOWNLOADER::Stop()
|
|
|
|
{
|
|
|
|
state = STATE_NOT_STARTED;
|
2018-05-18 23:18:10 +03:00
|
|
|
if (httpd.transfer!=0)
|
2016-01-15 02:05:51 +03:00
|
|
|
{
|
2018-05-18 23:18:10 +03:00
|
|
|
EAX = httpd.transfer;
|
2016-01-15 02:05:51 +03:00
|
|
|
EAX = EAX.http_msg.content_ptr; // get pointer to data
|
|
|
|
$push EAX // save it on the stack
|
2018-05-18 23:18:10 +03:00
|
|
|
http_free stdcall (httpd.transfer); // abort connection
|
2016-01-15 02:05:51 +03:00
|
|
|
$pop EAX
|
2018-05-18 23:18:10 +03:00
|
|
|
free(EAX); // free data
|
|
|
|
httpd.transfer=0;
|
2016-01-15 02:05:51 +03:00
|
|
|
bufsize = 0;
|
2018-05-18 23:18:10 +03:00
|
|
|
bufpointer = free(bufpointer);
|
2016-01-15 02:05:51 +03:00
|
|
|
}
|
2018-05-20 16:40:58 +03:00
|
|
|
httpd.content_received = httpd.content_length = 0;
|
2016-01-15 02:05:51 +03:00
|
|
|
}
|
|
|
|
|
2018-05-20 16:40:58 +03:00
|
|
|
bool DOWNLOADER::MonitorProgress()
|
2016-01-15 02:05:51 +03:00
|
|
|
{
|
2018-05-18 23:18:10 +03:00
|
|
|
if (httpd.transfer <= 0) return false;
|
2018-05-20 16:40:58 +03:00
|
|
|
httpd.receive();
|
|
|
|
if (!httpd.content_length) httpd.content_length = httpd.content_received * 20;
|
|
|
|
|
|
|
|
if (httpd.receive_result == 0) {
|
2018-05-18 23:18:10 +03:00
|
|
|
if (httpd.status_code >= 300) && (httpd.status_code < 400)
|
2016-01-15 02:05:51 +03:00
|
|
|
{
|
2018-05-18 23:18:10 +03:00
|
|
|
httpd.handle_redirect();
|
|
|
|
strcpy(url, httpd.url);
|
2018-05-20 16:40:58 +03:00
|
|
|
get_absolute_url(#httpd.finaladress, url, #httpd.redirect_url);
|
2016-01-15 02:05:51 +03:00
|
|
|
Stop();
|
2018-05-20 16:40:58 +03:00
|
|
|
Start(#httpd.finaladress);
|
|
|
|
url = #httpd.finaladress;
|
2016-01-15 02:05:51 +03:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-20 16:40:58 +03:00
|
|
|
state = STATE_COMPLETED;
|
|
|
|
bufpointer = httpd.content_pointer;
|
|
|
|
bufsize = httpd.content_received;
|
|
|
|
httpd.free();
|
2016-01-15 02:05:51 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*=====================================
|
|
|
|
== ==
|
2018-05-18 23:18:10 +03:00
|
|
|
== CHECK PATH TYPE ==
|
2016-01-15 02:05:51 +03:00
|
|
|
== ==
|
|
|
|
=====================================*/
|
|
|
|
|
|
|
|
|
2020-05-05 02:50:41 +03:00
|
|
|
:int check_is_the_adress_local(dword _in)
|
2016-01-15 02:05:51 +03:00
|
|
|
{
|
|
|
|
if (ESBYTE[_in]!='/') return false;
|
|
|
|
_in++;
|
|
|
|
if(!strncmp(_in,"rd/",3)) return true;
|
|
|
|
if(!strncmp(_in,"fd/",3)) return true;
|
2016-02-13 22:59:07 +03:00
|
|
|
if(!strncmp(_in,"hd",2)) return true;
|
|
|
|
if(!strncmp(_in,"bd",2)) return true;
|
|
|
|
if(!strncmp(_in,"cd",2)) return true;
|
2016-01-15 02:05:51 +03:00
|
|
|
if(!strncmp(_in,"sys/",4)) return true;
|
|
|
|
if(!strncmp(_in,"tmp/",4)) return true;
|
2016-02-13 22:59:07 +03:00
|
|
|
if(!strncmp(_in,"usbhd",5)) return true;
|
2018-10-01 19:41:14 +03:00
|
|
|
if(!strncmp(_in,"kolibrios",9)) return true;
|
2016-01-15 02:05:51 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-05 02:50:41 +03:00
|
|
|
:int check_is_the_url_absolute(dword _in)
|
2016-01-15 02:05:51 +03:00
|
|
|
{
|
|
|
|
if(!strncmp(_in,"ftp:",4)) return true;
|
|
|
|
if(!strncmp(_in,"http:",5)) return true;
|
|
|
|
if(!strncmp(_in,"https:",6)) return true;
|
|
|
|
if(!strncmp(_in,"mailto:",7)) return true;
|
2020-03-21 17:33:54 +03:00
|
|
|
if(!strncmp(_in,"tel:",4)) return true;
|
|
|
|
if(!strncmp(_in,"#",1)) return true;
|
2020-03-21 21:49:45 +03:00
|
|
|
if(!strncmp(_in,"WebView:",8)) return true;
|
2016-01-15 02:05:51 +03:00
|
|
|
if(check_is_the_adress_local(_in)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-05 02:50:41 +03:00
|
|
|
:void get_absolute_url(dword _rez, _base, _new)
|
2016-01-15 02:05:51 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
//case: ./valera.html
|
|
|
|
if (!strncmp(_new,"./", 2)) _new+=2;
|
|
|
|
//case: http://site.name
|
|
|
|
if (check_is_the_url_absolute(_new)) {
|
|
|
|
strcpy(_rez, _new);
|
|
|
|
goto _GET_ABSOLUTE_URL_END;
|
|
|
|
}
|
|
|
|
//case: /valera.html
|
|
|
|
if (ESBYTE[_new] == '/') //remove everything after site domain name
|
|
|
|
{
|
|
|
|
strcpy(_rez, _base);
|
|
|
|
i = strchr(_rez+8,'/');
|
|
|
|
if (i<1) i=strlen(_rez)+_rez;
|
|
|
|
strcpy(i, _new);
|
|
|
|
goto _GET_ABSOLUTE_URL_END;
|
|
|
|
}
|
|
|
|
//case: ../../valera.html
|
|
|
|
strcpy(_rez, _base);
|
|
|
|
ESBYTE[ strrchr(_rez,'/') + _rez -1 ] = '\0'; //remove name
|
|
|
|
while (!strncmp(_new,"../",3))
|
|
|
|
{
|
|
|
|
_new += 3;
|
|
|
|
ESBYTE[ strrchr(_rez,'/') + _rez -1 ] = '\0';
|
|
|
|
}
|
|
|
|
//case: valera.html
|
|
|
|
chrcat(_rez, '/');
|
|
|
|
strcat(_rez, _new);
|
|
|
|
_GET_ABSOLUTE_URL_END:
|
|
|
|
while (i=strstr(_rez, "&")) strcpy(i+1, i+5);
|
|
|
|
}
|
2020-03-31 00:29:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
:dword GetAbsoluteURL(dword new_URL, base_URL)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
dword orig_URL = new_URL;
|
|
|
|
char newurl[URL_SIZE+1];
|
|
|
|
strcpy(#newurl, base_URL);
|
|
|
|
|
|
|
|
while (i=strstr(new_URL, "&")) strcpy(i+1, i+5);
|
|
|
|
|
|
|
|
if (check_is_the_url_absolute(new_URL)) return orig_URL;
|
|
|
|
|
|
|
|
IF (!strncmp(new_URL,"//", 2))
|
|
|
|
{
|
|
|
|
strcpy(#newurl, "http:");
|
|
|
|
strcat(#newurl, new_URL);
|
|
|
|
strcpy(orig_URL, #newurl);
|
|
|
|
return orig_URL;
|
|
|
|
}
|
|
|
|
|
|
|
|
IF (!strncmp(new_URL,"./", 2)) new_URL+=2;
|
|
|
|
|
|
|
|
if (ESBYTE[new_URL] == '/') //remove everything after site domain name
|
|
|
|
{
|
|
|
|
i = strchr(#newurl+8, '/');
|
|
|
|
if (i) ESBYTE[i]=0;
|
|
|
|
new_URL+=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_CUT_ST_LEVEL_MARK:
|
|
|
|
|
|
|
|
if (newurl[strrchr(#newurl, '/')-2]<>'/')
|
|
|
|
{
|
|
|
|
newurl[strrchr(#newurl, '/')] = 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
IF (!strncmp(new_URL,"../",3))
|
|
|
|
{
|
|
|
|
new_URL+=3;
|
|
|
|
newurl[strrchr(#newurl, '/')-1] = 0x00;
|
|
|
|
goto _CUT_ST_LEVEL_MARK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newurl[strlen(#newurl)-1]<>'/') strcat(#newurl, "/");
|
|
|
|
|
|
|
|
strcat(#newurl, new_URL);
|
|
|
|
strcpy(orig_URL, #newurl);
|
|
|
|
return orig_URL;
|
|
|
|
}
|