WebView 2.7: distinguish files to download from pages to show, download files via HTTPS, remove duplicated function of getting absolute URL from relative

git-svn-id: svn://kolibrios.org@8291 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Kirill Lipatov (Leency) 2020-12-01 17:37:47 +00:00
parent e303140339
commit d178e4875c
5 changed files with 79 additions and 97 deletions

View File

@ -19,7 +19,7 @@ dword _img::add(dword _path, _x, _y)
{ {
char full_path[URL_SIZE]; char full_path[URL_SIZE];
strncpy(#full_path, _path, URL_SIZE); strncpy(#full_path, _path, URL_SIZE);
GetAbsoluteURL(#full_path, history.current()); get_absolute_url(#full_path, history.current());
url.add(#full_path); url.add(#full_path);
xywh.add(_x); xywh.add(_x);
@ -93,7 +93,7 @@ void ImageCache::Images(dword left1, top1, width1)
dword image; dword image;
dword imgw=0, imgh=0, img_lines_first=0, cur_pic=0; dword imgw=0, imgh=0, img_lines_first=0, cur_pic=0;
//GetAbsoluteURL(#img_path); //getting abs url from (#img_path);
//cur_pic = GetImage(#img_path); //cur_pic = GetImage(#img_path);
if (!pics[cur_pic].image) if (!pics[cur_pic].image)

View File

@ -34,7 +34,7 @@ char editbox_icons[] = FROM "res/editbox_icons.raw";
#define URL_SIZE 4000 #define URL_SIZE 4000
char version[]="WebView 2.66"; char version[]="WebView 2.7";
#include "texts.h" #include "texts.h"
#include "cache.h" #include "cache.h"
@ -240,6 +240,12 @@ void main()
if (http.transfer <= 0) break; if (http.transfer <= 0) break;
http.receive(); http.receive();
EventUpdateProgressBar(); EventUpdateProgressBar();
if (http.check_content_type()) && (!strncmp(#http.content_type,"application",11)) {
EventOpenDownloader(history.current());
StopLoading();
history.back();
EventRefreshPage();
}
if (http.receive_result != 0) break; if (http.receive_result != 0) break;
if (http.status_code >= 300) && (http.status_code < 400) if (http.status_code >= 300) && (http.status_code < 400)
{ {
@ -247,8 +253,6 @@ void main()
if (redirect_count<=5) { if (redirect_count<=5) {
redirect_count++; redirect_count++;
http.handle_redirect(); http.handle_redirect();
http.free();
GetAbsoluteURL(#http.redirect_url, history.current());
history.back(); history.back();
if (http_get_type==PAGE) OpenPage(#http.redirect_url); if (http_get_type==PAGE) OpenPage(#http.redirect_url);
else if (http_get_type==IMG) http.get(#http.redirect_url); else if (http_get_type==IMG) http.get(#http.redirect_url);
@ -260,7 +264,7 @@ void main()
} else { } else {
// Loading the page is complete, free resources // Loading the page is complete, free resources
redirect_count = 0; redirect_count = 0;
http.free(); http.hfree();
if (http_get_type==PAGE) { if (http_get_type==PAGE) {
cache.add(history.current(), http.content_pointer, http.content_received, PAGE); cache.add(history.current(), http.content_pointer, http.content_received, PAGE);
LoadInternalPage(http.content_pointer, http.content_received); LoadInternalPage(http.content_pointer, http.content_received);
@ -951,7 +955,7 @@ dword GetAbsoluteActiveURL()
char abs_url[URL_SIZE]; char abs_url[URL_SIZE];
if (links.active_url) { if (links.active_url) {
strncpy(#abs_url, links.active_url, URL_SIZE); strncpy(#abs_url, links.active_url, URL_SIZE);
GetAbsoluteURL(#abs_url, history.current()); get_absolute_url(#abs_url, history.current());
return #abs_url; return #abs_url;
} }
return 0; return 0;

View File

@ -6,14 +6,13 @@
#include "../lib/obj/box_lib.h" #include "../lib/obj/box_lib.h"
#include "../lib/obj/http.h" #include "../lib/obj/http.h"
#include "../lib/obj/libini.h"
#include "../lib/patterns/http_downloader.h" #include "../lib/patterns/http_downloader.h"
#include "const.h" #include "const.h"
DOWNLOADER downloader; DOWNLOADER downloader;
checkbox autoclose = { T_AUTOCLOSE, true }; checkbox autoclose = { T_AUTOCLOSE, false };
char downloader_edit[4000]; char downloader_edit[4000];
char filepath[4096]; char filepath[4096];
@ -30,7 +29,6 @@ void main()
dword shared_url; dword shared_url;
load_dll(boxlib, #box_lib_init,0); load_dll(boxlib, #box_lib_init,0);
load_dll(libHTTP, #http_lib_init,1); load_dll(libHTTP, #http_lib_init,1);
load_dll(libini, #lib_init,1);
if (!dir_exists(#save_to)) CreateDir(#save_to); if (!dir_exists(#save_to)) CreateDir(#save_to);
@ -74,8 +72,8 @@ void main()
default: default:
if (!downloader.MonitorProgress()) break; if (!downloader.MonitorProgress()) break;
pb.max = downloader.httpd.content_length / 100; pb.max = downloader.content_length / 100;
EDI = downloader.httpd.content_received/100; EDI = downloader.content_received/100;
if (pb.value != EDI) if (pb.value != EDI)
{ {
pb.value = EDI; pb.value = EDI;
@ -97,7 +95,7 @@ void ProcessEvent(int id)
{ {
autoclose.click(id); autoclose.click(id);
if (id==001) { StopDownloading(); ExitProcess(); } if (id==001) { StopDownloading(); ExitProcess(); }
if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading(); if (id==301) && (downloader.transfer <= 0) StartDownloading();
if (id==302) StopDownloading(); if (id==302) StopDownloading();
if (id==305) RunProgram("/sys/File managers/Eolite", #filepath); if (id==305) RunProgram("/sys/File managers/Eolite", #filepath);
if (id==306) { if (id==306) {
@ -141,17 +139,17 @@ void StartDownloading()
char proxy_url[URL_SIZE]; char proxy_url[URL_SIZE];
StopDownloading(); StopDownloading();
if (!strncmp(#downloader_edit,"https://",7)) { if (!strncmp(#downloader_edit,"https://",7)) {
notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W"); //notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W");
miniprintf(#http_url, "http://%s", #downloader_edit+8); //miniprintf(#http_url, "http://%s", #downloader_edit+8);
if (!downloader.Start(#http_url)) { //if (!downloader.Start(#http_url)) {
notify("'Download failed.' -E");
StopDownloading();
}
//sprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit);
//if (!downloader.Start(#proxy_url)) {
// notify("'Download failed.' -E"); // notify("'Download failed.' -E");
// StopDownloading(); // StopDownloading();
//} //}
miniprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit);
if (!downloader.Start(#proxy_url)) {
notify("'Download failed.' -E");
StopDownloading();
}
DrawWindow(); DrawWindow();
return; return;
} }
@ -185,11 +183,11 @@ void CalculateSpeed()
if (time.old) { if (time.old) {
time.gone = time.cur - time.old; time.gone = time.cur - time.old;
if (time.gone > 200) { if (time.gone > 200) {
speed = downloader.httpd.content_received - netdata_received / time.gone * 100; speed = downloader.content_received - netdata_received / time.gone * 100;
debugval("speed", speed); debugval("speed", speed);
debugln(ConvertSizeToKb(speed) ); debugln(ConvertSizeToKb(speed) );
time.old = time.cur; time.old = time.cur;
netdata_received = downloader.httpd.content_received; netdata_received = downloader.content_received;
} }
} }
else time.old = time.cur; else time.old = time.cur;
@ -199,7 +197,7 @@ void CalculateSpeed()
void DrawDownloading() void DrawDownloading()
{ {
char bytes_received[70]; char bytes_received[70];
miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.httpd.content_received) ); miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.content_received) );
WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work); WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work);
//CalculateSpeed(); //CalculateSpeed();
progressbar_draw stdcall(#pb); progressbar_draw stdcall(#pb);
@ -232,14 +230,14 @@ void SaveDownloadedFile()
for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-'; for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-';
if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) { if (CreateFile(downloader.content_received, downloader.bufpointer, #filepath)==0) {
miniprintf(#notify_message, FILE_SAVED_AS, #filepath); miniprintf(#notify_message, FILE_SAVED_AS, #filepath);
} else { } else {
miniprintf(#notify_message, FILE_NOT_SAVED, #filepath); miniprintf(#notify_message, FILE_NOT_SAVED, #filepath);
} }
/* /*
if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) { if (CreateFile(downloader.content_received, downloader.bufpointer, #filepath)==0) {
strcpy(#notify_message, "'Download complete' -Dt"); strcpy(#notify_message, "'Download complete' -Dt");
} else { } else {
strcpy(#notify_message, "'Error saving downloaded file!' -Et"); strcpy(#notify_message, "'Error saving downloaded file!' -Et");

View File

@ -6,7 +6,7 @@
struct _http struct _http
{ {
dword url; dword cur_url;
dword transfer; dword transfer;
dword content_length; dword content_length;
dword content_received; dword content_received;
@ -14,23 +14,25 @@ struct _http
dword receive_result; dword receive_result;
dword content_pointer; dword content_pointer;
char redirect_url[4096*3]; char redirect_url[4096*3];
char finaladress[4096*3]; char content_type[64];
dword get(); dword get();
void free(); void hfree();
void receive(); void receive();
bool handle_redirect(); bool handle_redirect();
dword check_content_type();
}; };
dword _http::get(dword _url) dword _http::get(dword _url)
{ {
url = _url; cur_url = _url;
http_get stdcall (url, 0, 0, #accept_language); http_get stdcall (_url, 0, 0, #accept_language);
content_type[0] = '\0';
transfer = EAX; transfer = EAX;
return transfer; return transfer;
} }
void _http::free() void _http::hfree()
{ {
http_free stdcall (transfer); http_free stdcall (transfer);
transfer=0; transfer=0;
@ -53,7 +55,6 @@ void _http::receive()
:bool _http::handle_redirect() :bool _http::handle_redirect()
{ {
dword redirect;
http_find_header_field stdcall (transfer, "location\0"); http_find_header_field stdcall (transfer, "location\0");
if (EAX!=0) { if (EAX!=0) {
ESI = EAX; ESI = EAX;
@ -63,11 +64,29 @@ void _http::receive()
$stosb; $stosb;
} while (AL != 0) && (AL != 13) && (AL != 10); } while (AL != 0) && (AL != 13) && (AL != 10);
DSBYTE[EDI-1]='\0'; DSBYTE[EDI-1]='\0';
get_absolute_url(#finaladress, url, #redirect_url); get_absolute_url(#redirect_url, cur_url);
strcpy(url, #finaladress); hfree();
return true; return #redirect_url;
} }
return false; return NULL;
}
:dword _http::check_content_type()
{
if (content_type[0]) return NULL;
http_find_header_field stdcall (transfer, "content-type\0");
if (EAX!=0) {
ESI = EAX;
EDI = #content_type;
do {
$lodsb;
$stosb;
} while (AL != 0) && (AL != 13) && (AL != 10);
DSBYTE[EDI-1]='\0';
debugln(#content_type);
return #content_type;
}
return NULL;
} }
//===================================================// //===================================================//
@ -83,8 +102,7 @@ enum {
STATE_COMPLETED STATE_COMPLETED
}; };
struct DOWNLOADER { struct DOWNLOADER : _http {
_http httpd;
dword bufpointer, bufsize, url; dword bufpointer, bufsize, url;
int state; int state;
dword Start(); dword Start();
@ -96,50 +114,47 @@ dword DOWNLOADER::Start(dword _url)
{ {
url = _url; url = _url;
state = STATE_IN_PROGRESS; state = STATE_IN_PROGRESS;
httpd.get(url); get(_url);
if (!httpd.transfer) Stop(); if (!transfer) Stop();
return httpd.transfer; return transfer;
} }
void DOWNLOADER::Stop() void DOWNLOADER::Stop()
{ {
state = STATE_NOT_STARTED; state = STATE_NOT_STARTED;
if (httpd.transfer!=0) if (transfer!=0)
{ {
EAX = httpd.transfer; EAX = transfer;
EAX = EAX.http_msg.content_ptr; // get pointer to data EAX = EAX.http_msg.content_ptr; // get pointer to data
$push EAX // save it on the stack $push EAX // save it on the stack
http_free stdcall (httpd.transfer); // abort connection http_free stdcall (transfer); // abort connection
$pop EAX $pop EAX
free(EAX); // free data free(EAX); // free data
httpd.transfer=0; transfer=0;
bufsize = 0; bufsize = 0;
bufpointer = free(bufpointer); bufpointer = free(bufpointer);
} }
httpd.content_received = httpd.content_length = 0; content_received = content_length = 0;
} }
bool DOWNLOADER::MonitorProgress() bool DOWNLOADER::MonitorProgress()
{ {
if (httpd.transfer <= 0) return false; if (transfer <= 0) return false;
httpd.receive(); receive();
if (!httpd.content_length) httpd.content_length = httpd.content_received * 20; if (!content_length) content_length = content_received * 20;
if (httpd.receive_result == 0) { if (receive_result == 0) {
if (httpd.status_code >= 300) && (httpd.status_code < 400) if (status_code >= 300) && (status_code < 400)
{ {
httpd.handle_redirect(); url = handle_redirect();
strcpy(url, httpd.url);
get_absolute_url(#httpd.finaladress, url, #httpd.redirect_url);
Stop(); Stop();
Start(#httpd.finaladress); Start(url);
url = #httpd.finaladress;
return false; return false;
} }
state = STATE_COMPLETED; state = STATE_COMPLETED;
bufpointer = httpd.content_pointer; bufpointer = content_pointer;
bufsize = httpd.content_received; bufsize = content_received;
httpd.free(); hfree();
} }
return true; return true;
} }
@ -181,42 +196,7 @@ bool DOWNLOADER::MonitorProgress()
return false; return false;
} }
:void get_absolute_url(dword _rez, _base, _new) :dword get_absolute_url(dword new_URL, base_URL)
{
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, "&amp;")) strcpy(i+1, i+5);
}
:dword GetAbsoluteURL(dword new_URL, base_URL)
{ {
int i; int i;
dword orig_URL = new_URL; dword orig_URL = new_URL;

View File

@ -117,7 +117,7 @@ void main()
if (!http.receive_result) { if (!http.receive_result) {
CreateFile(http.content_received, CreateFile(http.content_received,
http.content_pointer, "/tmp0/1/latest.img"); http.content_pointer, "/tmp0/1/latest.img");
http.free(); http.hfree();
EventDownloadComplete(); EventDownloadComplete();
} }
} }