From 1c88038821e0624d35647b0c94e68b6336a1ae26 Mon Sep 17 00:00:00 2001 From: "Kirill Lipatov (Leency)" Date: Thu, 3 Dec 2020 02:56:54 +0000 Subject: [PATCH] http, dl, webview: refactoring git-svn-id: svn://kolibrios.org@8305 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/browser/WebView.c | 214 ++++++------- programs/cmm/downloader/const.h | 26 +- programs/cmm/downloader/dl.c | 324 ++++++++++---------- programs/cmm/lib/gui.h | 2 +- programs/cmm/lib/obj/http.h | 197 +++++++++++- programs/cmm/lib/patterns/http_downloader.h | 248 --------------- programs/cmm/liza/liza.c | 2 - 7 files changed, 461 insertions(+), 552 deletions(-) delete mode 100644 programs/cmm/lib/patterns/http_downloader.h diff --git a/programs/cmm/browser/WebView.c b/programs/cmm/browser/WebView.c index e15fd1be1d..dc1691ac28 100644 --- a/programs/cmm/browser/WebView.c +++ b/programs/cmm/browser/WebView.c @@ -25,14 +25,13 @@ //useful patterns #include "..\lib\patterns\history.h" -#include "..\lib\patterns\http_downloader.h" #include "..\lib\patterns\simple_open_dialog.h" #include "..\lib\patterns\toolbar_button.h" #include "..\lib\patterns\restart_process.h" char editbox_icons[] = FROM "res/editbox_icons.raw"; -char version[]="WebView 2.7a"; +char version[]="WebView 2.7b"; #include "texts.h" #include "cache.h" @@ -60,7 +59,7 @@ enum { IN_NEW_TAB, IN_NEW_WINDOW, COPY_LINK_URL, - DOWNLOAD_LINK_CONTENTS, + DOWNLOAD_LINK_CT, TAB_ID, TAB_CLOSE_ID = 900 }; @@ -143,7 +142,6 @@ void main() { int i, redirect_count=0; LoadLibraries(); - //CreateDir("/tmp0/1/WebView_Cache"); HandleParam(); WB1.list.SetFont(8, 14, 10011000b); WB1.list.no_selection = true; @@ -238,23 +236,14 @@ void main() if (http.transfer <= 0) break; http.receive(); EventUpdateProgressBar(); - if (http.check_content_type()) // application || image - if (http.content_type[0] == 'a') || (http.content_type[0] == 'i') { - EventOpenDownloader(history.current()); - StopLoading(); - history.back(); - EventRefreshPage(); - } + CheckContentType(); if (http.receive_result != 0) break; if (http.status_code >= 300) && (http.status_code < 400) { // Handle redirects if (redirect_count<=5) { redirect_count++; - http.handle_redirect(); - history.back(); - if (http_get_type==PAGE) OpenPage(#http.redirect_url); - else if (http_get_type==IMG) http.get(#http.redirect_url); + HandleRedirect(); } else { notify("'Too many redirects.' -E"); StopLoading(); @@ -382,116 +371,78 @@ void ProcessEvent(dword id__) { switch (id__) { - case 1: - ExitProcess(); - break; - case ENCODINGS...ENCODINGS+6: - EventChangeEncodingAndLoadPage(id__-ENCODINGS); - return; - case NEW_WINDOW: - RunProgram(#program_path, NULL); - return; + case 1: ExitProcess(); + case TAB_CLOSE_ID...TAB_CLOSE_ID+TABS_MAX: EventTabClose(id__ - TAB_CLOSE_ID); return; + case TAB_ID...TAB_ID+TABS_MAX: EventAllTabsClick(id__ - TAB_ID); return; + case ENCODINGS...ENCODINGS+6: EventChangeEncodingAndLoadPage(id__-ENCODINGS); return; + case NEW_WINDOW: RunProgram(#program_path, NULL); return; + case NEW_TAB: if (!http.transfer) EventOpenNewTab(URL_SERVICE_HOMEPAGE); return; case SCAN_CODE_BS: - case BACK_BUTTON: - if (history.back()) { - OpenPage(history.current()); - } - return; - case FORWARD_BUTTON: - if (history.forward()) { - OpenPage(history.current()); - } - return; + case BACK_BUTTON: if (history.back()) OpenPage(history.current()); return; + case FORWARD_BUTTON: if (history.forward()) OpenPage(history.current()); return; case GOTOURL_BUTTON: - case SCAN_CODE_ENTER: - EventSubmitOmnibox(); - return; - case REFRESH_BUTTON: - EventRefreshPage(); - return; - case CHANGE_ENCODING: - EventShowEncodingsList(); - return; - case SANDWICH_BUTTON: - EventShowMainMenu(); - return; - case VIEW_SOURCE: - EventViewSource(); - break; - case EDIT_SOURCE: - if (check_is_the_adress_local(history.current())) { - RunProgram("/rd/1/tinypad", history.current()); - } else { - CreateFile(WB1.bufsize, WB1.bufpointer, "/tmp0/1/WebView_tmp.htm"); - if (!EAX) RunProgram("/rd/1/tinypad", "/tmp0/1/WebView_tmp.htm"); - } - return; - case VIEW_HISTORY: - OpenPage(URL_SERVICE_HISTORY); - return; - case DOWNLOAD_MANAGER: - EventOpenDownloader(""); - return; - case UPDATE_BROWSER: - EventUpdateBrowser(); - return; - case CLEAR_CACHE: - cache.clear(); - notify(#clear_cache_ok); - EventRefreshPage(); - return; - case IN_NEW_TAB: - EventClickLink(TARGET_NEW_TAB); - return; - case IN_NEW_WINDOW: - EventClickLink(TARGET_NEW_WINDOW); - return; - case COPY_LINK_URL: - Clipboard__CopyText(GetAbsoluteActiveURL()); - notify("'URL copied to clipboard'O"); - return; - case DOWNLOAD_LINK_CONTENTS: - EventOpenDownloader( GetAbsoluteActiveURL() ); - return; - case OPEN_FILE: - EventOpenDialog(); - return; - case SCAN_CODE_F12: - debug_mode ^= 1; - if (debug_mode) notify("'Debug mode ON'-I"); - else notify("'Debug mode OFF'-I"); - return; - case NEW_TAB: - if (http.transfer) break; - EventOpenNewTab(URL_SERVICE_HOMEPAGE); - return; - case TAB_ID...TAB_ID+TABS_MAX: - if (http.transfer) break; - if (mouse.mkm) { - EventTabClose(id__ - TAB_ID); - } else { - EventTabClick(id__ - TAB_ID); - } - return; - case TAB_CLOSE_ID...TAB_CLOSE_ID+TABS_MAX: - EventTabClose(id__ - TAB_CLOSE_ID); - return; + case SCAN_CODE_ENTER: EventSubmitOmnibox(); return; + case REFRESH_BUTTON: EventRefreshPage(); return; + case CHANGE_ENCODING: EventShowEncodingsList(); return; + case SANDWICH_BUTTON: EventShowMainMenu(); return; + case VIEW_SOURCE: EventViewSource(); return; + case EDIT_SOURCE: EventEditSource(); return; + case VIEW_HISTORY: OpenPage(URL_SERVICE_HISTORY); return; + case DOWNLOAD_MANAGER: EventOpenDownloader(""); return; + case UPDATE_BROWSER: EventUpdateBrowser(); return; + case CLEAR_CACHE: EventClearCache(); return; + case IN_NEW_TAB: EventClickLink(TARGET_NEW_TAB); return; + case IN_NEW_WINDOW: EventClickLink(TARGET_NEW_WINDOW); return; + case COPY_LINK_URL: EventCopyLinkToClipboard(); return; + case DOWNLOAD_LINK_CT: EventOpenDownloader( GetAbsoluteActiveURL() ); return; + case OPEN_FILE: EventOpenDialog(); return; + case SCAN_CODE_F12: EventToggleDebugMode(); return; } } +void EventToggleDebugMode() +{ + debug_mode ^= 1; + if (debug_mode) notify("'Debug mode ON'-I"); + else notify("'Debug mode OFF'-I"); +} + +void EventAllTabsClick(dword _n) +{ + if (http.transfer) return; + if (mouse.mkm) { + EventTabClose(_n); + } else { + EventTabClick(_n); + } +} + +void EventEditSource() +{ + if (check_is_the_adress_local(history.current())) { + RunProgram("/rd/1/tinypad", history.current()); + } else { + CreateFile(WB1.bufsize, WB1.bufpointer, "/tmp0/1/WebView_tmp.htm"); + if (!EAX) RunProgram("/rd/1/tinypad", "/tmp0/1/WebView_tmp.htm"); + } +} + +void EventClearCache() +{ + cache.clear(); + notify(#clear_cache_ok); + EventRefreshPage(); +} + +void EventCopyLinkToClipboard() +{ + Clipboard__CopyText(GetAbsoluteActiveURL()); + notify("'URL copied to clipboard'O"); +} + void StopLoading() { - if (http.transfer) - { - EAX = http.transfer; - EAX = EAX.http_msg.content_ptr; // get pointer to data - $push EAX // save it on the stack - http_free stdcall (http.transfer); // abort connection - $pop EAX - free(EAX); // free data - http.transfer=0; - pause(10); - } + if (http.stop()) pause(10); wv_progress_bar.value = 0; DrawOmnibox(); } @@ -842,10 +793,6 @@ void EventOpenDialog() void EventViewSource() { - char source_view_param[URL_SIZE+1]; - //strcpy(#source_view_param, "-source "); - //strncat(#source_view_param, history.current(), URL_SIZE); - //RunProgram(#program_path, #source_view_param); source_mode = true; EventOpenNewTab(history.current()); } @@ -961,6 +908,29 @@ dword GetAbsoluteActiveURL() return 0; } +void CheckContentType() +{ + char content_type[64]; + if (http.header_field("content-type\0", #content_type, sizeof(content_type))) // application || image + if (content_type[0] == 'a') || (content_type[0] == 'i') { + EventOpenDownloader(history.current()); + StopLoading(); + history.back(); + EventRefreshPage(); + } +} + +void HandleRedirect() +{ + char redirect_url[URL_SIZE]; + http.header_field("location\0", #redirect_url, URL_SIZE); + get_absolute_url(#redirect_url, history.current()); + history.back(); + http.hfree(); + if (http_get_type==PAGE) OpenPage(#redirect_url); + else if (http_get_type==IMG) http.get(#redirect_url); +} + dword GetImg() { while (WB1.page_img.next_url()) { diff --git a/programs/cmm/downloader/const.h b/programs/cmm/downloader/const.h index e7ca0cb0b0..e8da5af7c1 100644 --- a/programs/cmm/downloader/const.h +++ b/programs/cmm/downloader/const.h @@ -6,10 +6,10 @@ #ifdef LANG_RUS #define DL_WINDOW_HEADER "Менеджер загрузок" - #define START_DOWNLOADING "Скачать" - #define STOP_DOWNLOADING "Отмена" - #define SHOW_IN_FOLDER "Показать в папке" - #define OPEN_FILE_TEXT "Открыть файл" + #define T_DOWNLOAD "Скачать" + #define T_CANCEL "Отмена" + #define T_OPEN_DIR "Показать в папке" + #define T_RUN "Открыть файл" #define FILE_SAVED_AS "'Менеджер загрузок\nФайл сохранен как %s' -Dt" #define FILE_NOT_SAVED "'Менеджер загрузок\nОшибка! Файл не может быть сохранен как\n%s' -Et" #define KB_RECEIVED "Идет скачивание... %s получено" @@ -18,10 +18,10 @@ char accept_language[]= "Accept-Language: ru\n"; #else #define DL_WINDOW_HEADER "Download Manager" - #define START_DOWNLOADING "Download" - #define STOP_DOWNLOADING "Cancel" - #define SHOW_IN_FOLDER "Show in folder" - #define OPEN_FILE_TEXT "Open file" + #define T_DOWNLOAD "Download" + #define T_CANCEL "Cancel" + #define T_OPEN_DIR " Show in folder " + #define T_RUN "Open file" #define FILE_SAVED_AS "'Download manager\nFile saved as %s' -Dt" #define FILE_NOT_SAVED "'Download manager\nError! Can\96t save file as %s' -Et" #define KB_RECEIVED "Downloading... %s received" @@ -34,7 +34,13 @@ #define WIN_W 580 #define WIN_H 100 -#define URL_SIZE 4000 - char save_to[] = "/tmp0/1/Downloads"; char dl_shared[] = "DL"; + +enum { + BTN_EXIT=1, + BTN_START, + BTN_STOP, + BTN_DIR, + BTN_RUN, +}; \ No newline at end of file diff --git a/programs/cmm/downloader/dl.c b/programs/cmm/downloader/dl.c index 1b7951e6cc..3b96954bd1 100644 --- a/programs/cmm/downloader/dl.c +++ b/programs/cmm/downloader/dl.c @@ -1,29 +1,27 @@ -//Copyright 2020 by Leency - #define MEMSIZE 1024 * 100 +//Copyright 2020 by Leency #include "../lib/gui.h" #include "../lib/random.h" - #include "../lib/obj/box_lib.h" #include "../lib/obj/http.h" -#include "../lib/patterns/http_downloader.h" - #include "const.h" -DOWNLOADER downloader; +dword bufpointer, bufsize; +bool exit_param = false; + +_http http; + checkbox autoclose = { T_AUTOCLOSE, false }; -char downloader_edit[4000]; -char filepath[4096]; -edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000, - sizeof(downloader_edit)-2,#downloader_edit,0,ed_focus,19,19}; -progress_bar pb = {0, GAPX, 58, 315, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F}; -//progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color; - -bool exit_when_done = false; +char main_url[URL_SIZE]; +char filepath[URL_SIZE+96]; + +progress_bar pb = {0, GAPX, 58, 315, 17, 0, NULL, NULL, 0xFFFfff, 0x74DA00, NULL}; +edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff, + 0x10000000, sizeof(main_url)-2,#main_url,0,ed_focus,19,19}; + - void main() { dword shared_url; @@ -31,140 +29,206 @@ void main() load_dll(libHTTP, #http_lib_init,1); if (!dir_exists(#save_to)) CreateDir(#save_to); + SetCurDir(#save_to); if (param) { if (!strncmp(#param, "-exit ", 6)) { - exit_when_done = true; + exit_param = true; param += 6; } if (!strncmp(#param, "-mem", 5)) { //shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE); - strcpy(#downloader_edit, shared_url); + strcpy(#main_url, shared_url); } else { - strcpy(#downloader_edit, #param); + strcpy(#main_url, #param); } - } - if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://"); - ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit); + } + if (main_url[0]) StartDownloading(); else strcpy(#main_url, "http://"); + EditBox_UpdateText(#ed, #main_url); @SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK); @SetWindowLayerBehaviour(-1, ZPOS_ALWAYS_TOP); loop() switch(@WaitEvent()) { - case evMouse: - edit_box_mouse stdcall (#ed); - break; - - case evButton: - ProcessEvent(GetButtonID()); - break; - - case evKey: - GetKeys(); - edit_box_key stdcall(#ed); - if (key_scancode==SCAN_CODE_ENTER) ProcessEvent(301); - break; - - case evReDraw: - DrawWindow(); - break; - - default: - if (!downloader.MonitorProgress()) break; - pb.max = downloader.content_length / 100; - EDI = downloader.content_received/100; - if (pb.value != EDI) - { - pb.value = EDI; - progressbar_draw stdcall(#pb); - DrawDownloading(); - } - if (downloader.state == STATE_COMPLETED) - { - SaveDownloadedFile(); - if (exit_when_done) ExitProcess(); - StopDownloading(); - DrawWindow(); - break; - } + case evMouse: edit_box_mouse stdcall (#ed); break; + case evButton: ProcessButtonClick(); break; + case evKey: ProcessKeyPress(); break; + case evReDraw: DrawWindow(); break; + default: MonitorProgress(); } } -void ProcessEvent(int id) +void ProcessButtonClick() { + int id = @GetButtonID(); autoclose.click(id); - if (id==001) { StopDownloading(); ExitProcess(); } - if (id==301) && (downloader.transfer <= 0) StartDownloading(); - if (id==302) StopDownloading(); - if (id==305) RunProgram("/sys/File managers/Eolite", #filepath); - if (id==306) { - SetCurDir(#save_to); - RunProgram("/sys/@open", #filepath); - } + if (id==BTN_EXIT) { StopDownloading(); ExitProcess(); } + if (id==BTN_START) StartDownloading(); + if (id==BTN_STOP) StopDownloading(); + if (id==BTN_DIR) RunProgram("/sys/File managers/Eolite", #filepath); + if (id==BTN_RUN) RunProgram("/sys/@open", #filepath); +} + +void ProcessKeyPress() +{ + @GetKey(); + edit_box_key stdcall(#ed); + EAX >>= 16; + if (AL == SCAN_CODE_ENTER) StartDownloading(); + if (AL == SCAN_CODE_ESC) StopDownloading(); } void DrawWindow() { - int but_x = 0; - #define BUT_Y 58; - sc.get(); pb.frame_color = sc.work_dark; - DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9, WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0); + DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9, + WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0); - autoclose.draw(WIN_W-135, BUT_Y+6); - - if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED) + #define BUT_Y 58; + //autoclose.draw(WIN_W-135, BUT_Y+6); + if (!http.transfer) { - but_x = GAPX + DrawStandartCaptButton(GAPX, BUT_Y, 301, START_DOWNLOADING); - if (filepath[0]) + DrawStandartCaptButton(GAPX, BUT_Y, BTN_START, T_DOWNLOAD); + if (filepath) { - but_x += DrawStandartCaptButton(but_x, BUT_Y, 305, SHOW_IN_FOLDER); - DrawStandartCaptButton(but_x, BUT_Y, 306, OPEN_FILE_TEXT); + DrawStandartCaptButton(GAPX+102, BUT_Y, BTN_DIR, T_OPEN_DIR); + DrawStandartCaptButton(GAPX+276, BUT_Y, BTN_RUN, T_RUN); } + } else { + DrawStandartCaptButton(WIN_W - 240, BUT_Y, BTN_STOP, T_CANCEL); + DrawDownloadingProgress(); } - if (downloader.state == STATE_IN_PROGRESS) - { - DrawStandartCaptButton(WIN_W - 240, BUT_Y, 302, STOP_DOWNLOADING); - DrawDownloading(); - } - ed.offset=0; + //ed.offset=0; //DEL? DrawEditBox(#ed); } void StartDownloading() { - char http_url[URL_SIZE]; char proxy_url[URL_SIZE]; - StopDownloading(); - if (!strncmp(#downloader_edit,"https://",7)) { + if (http.transfer > 0) return; + filepath = '\0'; + if (!strncmp(#main_url,"https:",6)) { + miniprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #main_url); + strcpy(#main_url, #proxy_url); + EditBox_UpdateText(#ed, #main_url); //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", #main_url+8); //if (!downloader.Start(#http_url)) { // notify("'Download failed.' -E"); // StopDownloading(); //} - miniprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit); - if (!downloader.Start(#proxy_url)) { - notify("'Download failed.' -E"); - StopDownloading(); - } - DrawWindow(); - return; } - if (!downloader.Start(#downloader_edit)) { - if (exit_when_done) ExitProcess(); + if (http.get(#main_url)) { + ed.blur_border_color = 0xCACACA; + ed.flags = 100000000000b; + pb.value = 0; + DrawWindow(); + } else { notify(T_ERROR_STARTING_DOWNLOAD); StopDownloading(); - return; + if (exit_param) ExitProcess(); } - ed.blur_border_color = 0xCACACA; - ed.flags = 100000000000b; - pb.value = 0; +} + + +void DrawDownloadingProgress() +{ + char bytes_received[70]; + + EDI = http.content_received / 100; + if (pb.value == EDI) return; + + pb.value = EDI; + pb.max = http.content_length / 100; + progressbar_draw stdcall(#pb); + miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(http.content_received) ); + WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work); + //CalculateSpeed(); +} + +void StopDownloading() +{ + if (http.stop()) { + bufsize = 0; + bufpointer = free(bufpointer); + } + http.content_received = http.content_length = 0; + + ed.blur_border_color = 0xFFFfff; + ed.flags = 10b; DrawWindow(); } +void MonitorProgress() +{ + char redirect_url[URL_SIZE]; + if (http.transfer <= 0) return; + http.receive(); + if (!http.content_length) http.content_length = http.content_received * 20; //MOVE? + + if (http.receive_result == 0) { + if (http.status_code >= 300) && (http.status_code < 400) + { + http.header_field("location\0", #redirect_url, URL_SIZE); + get_absolute_url(#redirect_url, #main_url); + strcpy(#main_url, #redirect_url); + EditBox_UpdateText(#ed, #main_url); + StopDownloading(); + StartDownloading(); + return; + } + bufpointer = http.content_pointer; + bufsize = http.content_received; + http.hfree(); + } + + DrawDownloadingProgress(); + + if (!http.receive_result) + { + SaveFile(); + if (exit_param) ExitProcess(); + StopDownloading(); + DrawWindow(); + return; + } +} + +void SaveFile() +{ + int i; + char aux[2048]; + char notify_message[4296]; + + //char file_name[URL_SIZE]; + //Content-Disposition: attachment; filename="RealFootball_2018_Nokia_5800_EN_IGP_EU_TS_101.zip" + //header_field("Content-Disposition\0", #redirect_url, URL_SIZE); + + // Clean all slashes at the end + strcpy(#aux, #main_url); + while (aux[strlen(#aux)-1] == '/') { + aux[strlen(#aux)-1] = 0; + } + + strcpy(#filepath, #save_to); + chrcat(#filepath, '/'); + strcat(#filepath, #aux+strrchr(#aux, '/')); + + for (i=0; i 200) { - speed = downloader.content_received - netdata_received / time.gone * 100; + speed = http.content_received - netdata_received / time.gone * 100; debugval("speed", speed); debugln(ConvertSizeToKb(speed) ); time.old = time.cur; - netdata_received = downloader.content_received; + netdata_received = http.content_received; } } else time.old = time.cur; } -*/ - -void DrawDownloading() -{ - char bytes_received[70]; - miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.content_received) ); - WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work); - //CalculateSpeed(); - progressbar_draw stdcall(#pb); -} - -void StopDownloading() -{ - downloader.Stop(); - ed.blur_border_color = 0xFFFfff; - ed.flags = 10b; - DrawWindow(); -} - -void SaveDownloadedFile() -{ - int i; - char aux[2048]; - char notify_message[4296]; - - // Clean all slashes at the end - strcpy(#aux, #downloader_edit); - while (aux[strlen(#aux)-1] == '/') { - aux[strlen(#aux)-1] = 0; - } - - //miniprintf(#filepath, "%s/", #save_to); - strcpy(#filepath, #save_to); - chrcat(#filepath, '/'); - strcat(#filepath, #aux+strrchr(#aux, '/')); - - for (i=0; i0); + DSBYTE[EDI-1]='\0'; + debugln(_dst); + return _dst; + } + return NULL; +} + + +//===================================================// +// // +// CHECK PATH TYPE // +// // +//===================================================// + +:int check_is_the_adress_local(dword _in) +{ + if (ESBYTE[_in]!='/') return false; + _in++; + if(!strncmp(_in,"rd/",3)) return true; + if(!strncmp(_in,"fd/",3)) return true; + if(!strncmp(_in,"hd",2)) return true; + if(!strncmp(_in,"bd",2)) return true; + if(!strncmp(_in,"cd",2)) return true; + if(!strncmp(_in,"sys/",4)) return true; + if(!strncmp(_in,"tmp",3)) return true; + if(!strncmp(_in,"usbhd",5)) return true; + if(!strncmp(_in,"kolibrios",9)) return true; + return false; +} + +:int check_is_the_url_absolute(dword _in) +{ + 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; + if(!strncmp(_in,"tel:",4)) return true; + if(!strncmp(_in,"#",1)) return true; + if(!strncmp(_in,"WebView:",8)) return true; + if(check_is_the_adress_local(_in)) return true; + return false; +} + +:dword get_absolute_url(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; +} + + #endif \ No newline at end of file diff --git a/programs/cmm/lib/patterns/http_downloader.h b/programs/cmm/lib/patterns/http_downloader.h deleted file mode 100644 index 84046e267b..0000000000 --- a/programs/cmm/lib/patterns/http_downloader.h +++ /dev/null @@ -1,248 +0,0 @@ -#define URL_SIZE 4000 - -//===================================================// -// // -// HTTP // -// // -//===================================================// - -struct _http -{ - dword cur_url; - dword transfer; - dword content_length; - dword content_received; - dword status_code; - dword receive_result; - dword content_pointer; - char redirect_url[4096*3]; - char content_type[64]; - - dword get(); - void hfree(); - void receive(); - bool handle_redirect(); - dword check_content_type(); -}; - -dword _http::get(dword _url) -{ - cur_url = _url; - http_get stdcall (_url, 0, 0, #accept_language); - content_type[0] = '\0'; - transfer = EAX; - return transfer; -} - -void _http::hfree() -{ - http_free stdcall (transfer); - transfer=0; -} - -void _http::receive() -{ - http_receive stdcall (transfer); - receive_result = EAX; - - 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; - - } - -:bool _http::handle_redirect() -{ - 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(#redirect_url, cur_url); - hfree(); - return #redirect_url; - } - 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; -} - -//===================================================// -// // -// DOWNLOADER // -// // -//===================================================// - - -enum { - STATE_NOT_STARTED, - STATE_IN_PROGRESS, - STATE_COMPLETED -}; - -struct DOWNLOADER : _http { - dword bufpointer, bufsize, url; - int state; - dword Start(); - void Stop(); - bool MonitorProgress(); -}; - -dword DOWNLOADER::Start(dword _url) -{ - url = _url; - state = STATE_IN_PROGRESS; - get(_url); - if (!transfer) Stop(); - return transfer; -} - -void DOWNLOADER::Stop() -{ - state = STATE_NOT_STARTED; - if (transfer!=0) - { - EAX = transfer; - EAX = EAX.http_msg.content_ptr; // get pointer to data - $push EAX // save it on the stack - http_free stdcall (transfer); // abort connection - $pop EAX - free(EAX); // free data - transfer=0; - bufsize = 0; - bufpointer = free(bufpointer); - } - content_received = content_length = 0; -} - -bool DOWNLOADER::MonitorProgress() -{ - if (transfer <= 0) return false; - receive(); - if (!content_length) content_length = content_received * 20; - - if (receive_result == 0) { - if (status_code >= 300) && (status_code < 400) - { - url = handle_redirect(); - Stop(); - Start(url); - return false; - } - state = STATE_COMPLETED; - bufpointer = content_pointer; - bufsize = content_received; - hfree(); - } - return true; -} - - -/*===================================== -== == -== CHECK PATH TYPE == -== == -=====================================*/ - - -:int check_is_the_adress_local(dword _in) -{ - if (ESBYTE[_in]!='/') return false; - _in++; - if(!strncmp(_in,"rd/",3)) return true; - if(!strncmp(_in,"fd/",3)) return true; - if(!strncmp(_in,"hd",2)) return true; - if(!strncmp(_in,"bd",2)) return true; - if(!strncmp(_in,"cd",2)) return true; - if(!strncmp(_in,"sys/",4)) return true; - if(!strncmp(_in,"tmp/",4)) return true; - if(!strncmp(_in,"usbhd",5)) return true; - if(!strncmp(_in,"kolibrios",9)) return true; - return false; -} - -:int check_is_the_url_absolute(dword _in) -{ - 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; - if(!strncmp(_in,"tel:",4)) return true; - if(!strncmp(_in,"#",1)) return true; - if(!strncmp(_in,"WebView:",8)) return true; - if(check_is_the_adress_local(_in)) return true; - return false; -} - -:dword get_absolute_url(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; -} diff --git a/programs/cmm/liza/liza.c b/programs/cmm/liza/liza.c index 27e27ecafa..dbebbd8f7b 100644 --- a/programs/cmm/liza/liza.c +++ b/programs/cmm/liza/liza.c @@ -21,8 +21,6 @@ #include "../lib/obj/libimg.h" #include "../lib/obj/netcode.h" #include "../lib/obj/iconv.h" -//patterns -#include "../lib/patterns/http_downloader.h" //images byte letter_icons[sizeof(file "img/letter_icons.raw")] = FROM "img/letter_icons.raw"; #include "img/letter_icons.h"