From 36bea486e431519d51dd8e1b51503e7e428611dd Mon Sep 17 00:00:00 2001 From: FellippeHeitor Date: Fri, 30 Oct 2020 16:26:51 -0300 Subject: [PATCH] Fixes Download function - Addresses #95 --- InForm/UiEditor.bas | 62 +---------------------------- InForm/extensions/download.bas | 63 +++++++++++++++++++++++++++++ InForm/setup/InFormSetup.bas | 68 +++----------------------------- InForm/updater/InFormUpdater.bas | 62 +---------------------------- 4 files changed, 70 insertions(+), 185 deletions(-) create mode 100644 InForm/extensions/download.bas diff --git a/InForm/UiEditor.bas b/InForm/UiEditor.bas index 00d8daa..4cc83d5 100644 --- a/InForm/UiEditor.bas +++ b/InForm/UiEditor.bas @@ -304,6 +304,7 @@ $END IF '$include:'xp.uitheme' '$include:'UiEditor.frm' '$include:'ini.bm' +'$include:'extensions/download.bas' 'Event procedures: --------------------------------------------------------------- SUB __UI_Click (id AS LONG) @@ -5219,67 +5220,6 @@ FUNCTION QuotedFilename$ (f$) $END IF END FUNCTION -FUNCTION Download$ (url$, file$, timelimit) STATIC - 'as seen on http://www.qb64.org/wiki/Downloading_Files - 'adapted for use with InForm - - DIM theClient AS LONG, l AS LONG - DIM prevUrl$, prevUrl2$, url2$, x AS LONG - DIM e$, url3$, x$, t!, a2$, a$, i AS LONG - DIM i2 AS LONG, i3 AS LONG, d$, fh AS LONG - - IF url$ <> prevUrl$ OR url$ = "" THEN - prevUrl$ = url$ - IF url$ = "" THEN - IF theClient THEN CLOSE theClient: theClient = 0 - EXIT SUB - END IF - url2$ = url$ - x = INSTR(url2$, "/") - IF x THEN url2$ = LEFT$(url$, x - 1) - IF url2$ <> prevUrl2$ THEN - prevUrl2$ = url2$ - IF theClient THEN CLOSE theClient: theClient = 0 - theClient = _OPENCLIENT("TCP/IP:80:" + url2$) - IF theClient = 0 THEN Download = MKI$(2): prevUrl$ = "": EXIT FUNCTION - END IF - e$ = CHR$(13) + CHR$(10) ' end of line characters - url3$ = RIGHT$(url$, LEN(url$) - x + 1) - x$ = "GET " + url3$ + " HTTP/1.1" + e$ - x$ = x$ + "Host: " + url2$ + e$ + e$ - PUT #theClient, , x$ - t! = TIMER ' start time - END IF - - GET #theClient, , a2$ - a$ = a$ + a2$ - i = INSTR(a$, "Content-Length:") - IF i THEN - i2 = INSTR(i, a$, e$) - IF i2 THEN - l = VAL(MID$(a$, i + 15, i2 - i - 14)) - i3 = INSTR(i2, a$, e$ + e$) - IF i3 THEN - i3 = i3 + 4 'move i3 to start of data - IF (LEN(a$) - i3 + 1) = l THEN - d$ = MID$(a$, i3, l) - fh = FREEFILE - OPEN file$ FOR OUTPUT AS #fh: CLOSE #fh 'Warning! Clears data from existing file - OPEN file$ FOR BINARY AS #fh - PUT #fh, , d$ - CLOSE #fh - Download = MKI$(1) + MKL$(l) 'indicates download was successful - prevUrl$ = "" - a$ = "" - EXIT FUNCTION - END IF ' availabledata = l - END IF ' i3 - END IF ' i2 - END IF ' i - IF TIMER > t! + timelimit THEN CLOSE theClient: theClient = 0: Download = MKI$(3): prevUrl$ = "": EXIT FUNCTION - Download = MKI$(0) 'still working -END FUNCTION - '--------------------------------------------------------------------------------- FUNCTION SpecialCharsToEscapeCode$ (Text$) DIM i AS LONG, Temp$ diff --git a/InForm/extensions/download.bas b/InForm/extensions/download.bas new file mode 100644 index 0000000..fd2d9b7 --- /dev/null +++ b/InForm/extensions/download.bas @@ -0,0 +1,63 @@ +FUNCTION Download$ (url$, file$, timelimit) STATIC + 'as seen on http://www.qb64.org/wiki/Downloading_Files + 'adapted for use with InForm + + DIM theClient AS LONG, l AS LONG + DIM prevUrl$, prevUrl2$, url2$, x AS LONG + DIM e$, url3$, x$, t!, a2$, a$, i AS LONG + DIM i2 AS LONG, i3 AS LONG, d$, fh AS LONG + + IF url$ <> prevUrl$ OR url$ = "" THEN + prevUrl$ = url$ + IF url$ = "" THEN + IF theClient THEN CLOSE theClient: theClient = 0 + EXIT SUB + END IF + url2$ = url$ + x = INSTR(url2$, "/") + IF x THEN url2$ = LEFT$(url$, x - 1) + IF url2$ <> prevUrl2$ THEN + prevUrl2$ = url2$ + IF theClient THEN CLOSE theClient: theClient = 0 + theClient = _OPENCLIENT("TCP/IP:80:" + url2$) + IF theClient = 0 THEN Download = MKI$(2): prevUrl$ = "": EXIT FUNCTION + END IF + e$ = CHR$(13) + CHR$(10) ' end of line characters + url3$ = RIGHT$(url$, LEN(url$) - x + 1) + x$ = "GET " + url3$ + " HTTP/1.1" + e$ + x$ = x$ + "Host: " + url2$ + e$ + e$ + PUT #theClient, , x$ + t! = TIMER ' start time + END IF + + GET #theClient, , a2$ + a$ = a$ + a2$ + i = INSTR(a$, "Content-Length:") + IF i THEN + i2 = INSTR(i, a$, e$) + IF i2 THEN + l = VAL(MID$(a$, i + 15, i2 - i - 14)) + i3 = INSTR(i2, a$, e$ + e$) + IF i3 THEN + i3 = i3 + 4 'move i3 to start of data + IF (LEN(a$) - i3 + 1) = l THEN + d$ = MID$(a$, i3, l) + fh = FREEFILE + OPEN file$ FOR OUTPUT AS #fh: CLOSE #fh 'Warning! Clears data from existing file + OPEN file$ FOR BINARY AS #fh + PUT #fh, , d$ + CLOSE #fh + Download = MKI$(1) + MKL$(l) 'indicates download was successful + prevUrl$ = "" + prevUrl2$ = "" + a$ = "" + CLOSE theClient + theClient = 0 + EXIT FUNCTION + END IF ' availabledata = l + END IF ' i3 + END IF ' i2 + END IF ' i + IF TIMER > t! + timelimit THEN CLOSE theClient: theClient = 0: Download = MKI$(3): prevUrl$ = "": EXIT FUNCTION + Download = MKI$(0) 'still working +END FUNCTION diff --git a/InForm/setup/InFormSetup.bas b/InForm/setup/InFormSetup.bas index c78e458..3dbc4e1 100644 --- a/InForm/setup/InFormSetup.bas +++ b/InForm/setup/InFormSetup.bas @@ -43,6 +43,7 @@ $END IF '$INCLUDE:'../xp.uitheme' '$INCLUDE:'InFormSetup.frm' '$INCLUDE:'../ini.bm' +'$include:'../extensions/download.bas' 'Icon: 'http://www.iconarchive.com/show/oxygen-icons-by-oxygen-icons.org/Apps-system-software-update-icon.html @@ -157,7 +158,9 @@ SUB __UI_BeforeUpdateDisplay STATIC CASE 1 'Success 'Checksum: IF getChecksum(outputFileName$) <> checksum$ THEN - Report "Failed." + Report "Checksum failed." + Report "Please contact fellippe@qb64.org - + Report "or @fellippeheitor on Twitter." ThisStep = -1 NextEvent = True EXIT SUB @@ -233,7 +236,7 @@ SUB __UI_BeforeUpdateDisplay STATIC CASE ELSE IF NextEvent THEN NextEvent = False: Report "Installation failed.": AddItem ListBox1, "" Result$ = Download$("", "", 30) 'close client - KILL "InFormSetup.ini" + IF _FILEEXISTS("InFormSetup.ini") THEN KILL "InFormSetup.ini" Control(RetryBT).Hidden = False Control(ActivityIndicator).Hidden = True END SELECT @@ -317,67 +320,6 @@ FUNCTION crc32~& (buf AS STRING) crc32~& = NOT crc END FUNCTION -FUNCTION Download$ (url$, file$, timelimit) STATIC - 'as seen on http://www.qb64.org/wiki/Downloading_Files - 'adapted for use with InForm - - DIM client AS LONG, l AS LONG - - IF url$ = "" THEN - IF client THEN CLOSE client: client = 0 - prevUrl$ = "" - EXIT SUB - END IF - - IF url$ <> prevUrl$ THEN - prevUrl$ = url$ - a$ = "" - url2$ = url$ - x = INSTR(url2$, "/") - IF x THEN url2$ = LEFT$(url$, x - 1) - IF url2$ <> prevUrl2$ THEN - prevUrl2$ = url2$ - IF client THEN CLOSE client: client = 0 - client = _OPENCLIENT("TCP/IP:80:" + url2$) - IF client = 0 THEN Download = MKI$(2): prevUrl$ = "": EXIT FUNCTION - END IF - e$ = CHR$(13) + CHR$(10) ' end of line characters - url3$ = RIGHT$(url$, LEN(url$) - x + 1) - x$ = "GET " + url3$ + " HTTP/1.1" + e$ - x$ = x$ + "Host: " + url2$ + e$ + e$ - PUT #client, , x$ - t! = TIMER ' start time - END IF - - GET #client, , a2$ - a$ = a$ + a2$ - i = INSTR(a$, "Content-Length:") - IF i THEN - i2 = INSTR(i, a$, e$) - IF i2 THEN - l = VAL(MID$(a$, i + 15, i2 - i - 14)) - i3 = INSTR(i2, a$, e$ + e$) - IF i3 THEN - i3 = i3 + 4 'move i3 to start of data - IF (LEN(a$) - i3 + 1) = l THEN - d$ = MID$(a$, i3, l) - fh = FREEFILE - OPEN file$ FOR OUTPUT AS #fh: CLOSE #fh 'Warning! Clears data from existing file - OPEN file$ FOR BINARY AS #fh - PUT #fh, , d$ - CLOSE #fh - Download = MKI$(1) + MKL$(l) 'indicates download was successful - prevUrl$ = "" - a$ = "" - EXIT FUNCTION - END IF ' availabledata = l - END IF ' i3 - END IF ' i2 - END IF ' i - IF TIMER > t! + timelimit THEN CLOSE client: client = 0: Download = MKI$(3): prevUrl$ = "": EXIT FUNCTION - Download = MKI$(0) 'still working -END FUNCTION - SUB CircleFill (x AS LONG, y AS LONG, R AS LONG, C AS _UNSIGNED LONG) DIM x0 AS SINGLE, y0 AS SINGLE DIM e AS SINGLE diff --git a/InForm/updater/InFormUpdater.bas b/InForm/updater/InFormUpdater.bas index a60f4ec..a29ef9f 100644 --- a/InForm/updater/InFormUpdater.bas +++ b/InForm/updater/InFormUpdater.bas @@ -31,6 +31,7 @@ $END IF '$INCLUDE:'../xp.uitheme' '$INCLUDE:'InFormUpdater.frm' '$INCLUDE:'../ini.bm' +'$include:'../extensions/download.bas' ': Event procedures: --------------------------------------------------------------- SUB __UI_BeforeInit @@ -305,67 +306,6 @@ FUNCTION crc32~& (buf AS STRING) crc32~& = NOT crc END FUNCTION -FUNCTION Download$ (url$, file$, timelimit) STATIC - 'as seen on http://www.qb64.org/wiki/Downloading_Files - 'adapted for use with InForm - - DIM client AS LONG, l AS LONG - - IF url$ = "" THEN - IF client THEN CLOSE client: client = 0 - prevUrl$ = "" - EXIT SUB - END IF - - IF url$ <> prevUrl$ THEN - prevUrl$ = url$ - a$ = "" - url2$ = url$ - x = INSTR(url2$, "/") - IF x THEN url2$ = LEFT$(url$, x - 1) - IF url2$ <> prevUrl2$ THEN - prevUrl2$ = url2$ - IF client THEN CLOSE client: client = 0 - client = _OPENCLIENT("TCP/IP:80:" + url2$) - IF client = 0 THEN Download = MKI$(2): prevUrl$ = "": EXIT FUNCTION - END IF - e$ = CHR$(13) + CHR$(10) ' end of line characters - url3$ = RIGHT$(url$, LEN(url$) - x + 1) - x$ = "GET " + url3$ + " HTTP/1.1" + e$ - x$ = x$ + "Host: " + url2$ + e$ + e$ - PUT #client, , x$ - t! = TIMER ' start time - END IF - - GET #client, , a2$ - a$ = a$ + a2$ - i = INSTR(a$, "Content-Length:") - IF i THEN - i2 = INSTR(i, a$, e$) - IF i2 THEN - l = VAL(MID$(a$, i + 15, i2 - i - 14)) - i3 = INSTR(i2, a$, e$ + e$) - IF i3 THEN - i3 = i3 + 4 'move i3 to start of data - IF (LEN(a$) - i3 + 1) = l THEN - d$ = MID$(a$, i3, l) - fh = FREEFILE - OPEN file$ FOR OUTPUT AS #fh: CLOSE #fh 'Warning! Clears data from existing file - OPEN file$ FOR BINARY AS #fh - PUT #fh, , d$ - CLOSE #fh - Download = MKI$(1) + MKL$(l) 'indicates download was successful - prevUrl$ = "" - a$ = "" - EXIT FUNCTION - END IF ' availabledata = l - END IF ' i3 - END IF ' i2 - END IF ' i - IF TIMER > t! + timelimit THEN CLOSE client: client = 0: Download = MKI$(3): prevUrl$ = "": EXIT FUNCTION - Download = MKI$(0) 'still working -END FUNCTION - SUB CircleFill (x AS LONG, y AS LONG, R AS LONG, C AS _UNSIGNED LONG) DIM x0 AS SINGLE, y0 AS SINGLE DIM e AS SINGLE