fetching_20a_20secure_20web_20page
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
fetching_20a_20secure_20web_20page [2018/04/17 16:06] – Added syntax highlighting tbest3112 | fetching_20a_20secure_20web_20page [2025/03/23 15:13] (current) – richardrussell | ||
---|---|---|---|
Line 1: | Line 1: | ||
=====Fetching a secure web page===== | =====Fetching a secure web page===== | ||
- | //by Richard Russell, July 2009//\\ \\ | + | //by Richard Russell, July 2009, updated July 2018 and March 2025// |
+ | |||
+ | The procedure listed below fetches the contents of a web resource | ||
- | * The directory from which the application will be loaded (@dir$). | ||
- | * The ' | ||
- | * The system directory (e.g. **C: | ||
- | * The Windows directory (e.g. **C: | ||
- | * One of the directories listed in the PATH environment variable. | ||
- | \\ If you want to distribute **libssl32.dll** and **libeay32.dll** with your application then store them in **@dir$** and embed them in the executable. Alternatively you can put them in **@lib$** (or a sub-directory) but in that case you will need to amend the procedure below to load them explicitly from that location.\\ \\ The procedure should be called in the following context:\\ \\ | ||
<code bb4w> | <code bb4w> | ||
- | port$ = "443" : REM https | + | url$ = "https://www.fortify.net/ |
- | host$ = "www.fortify.net" | + | file$ = @tmp$ + " |
- | page$ = "/ | + | SYS " |
- | file$ = @tmp$ + " | + | SYS " |
- | | + | SYS `URLDownloadToFile`, |
+ | IF fail% ERROR 100, "File download failed" | ||
+ | OSCLI "RUN notepad """ | ||
</ | </ | ||
- | This fetches | + | |
+ | If you find this code doesn' | ||
+ | | ||
+ | |||
+ | If you want to distribute **libssl-3.dll** and **libcrypto-3.dll** with your application then store them in **@dir$** and embed them in the executable. Alternatively you can put them in **@lib$** (or a sub-directory) but in that case you will need to amend the procedure | ||
+ | |||
+ | The procedure should be called in the following context: | ||
<code bb4w> | <code bb4w> | ||
- | DEF PROCsslfetch(port$, | + | port$ = " |
- | LOCAL libssl%, libeay%, meth%, ctx%, sock%, temp%, res%, ssl%, sbio%, file% | + | host$ = " |
- | LOCAL req$, buf&() | + | page$ = "/ |
+ | file$ = @tmp$ + " | ||
+ | | ||
+ | </ | ||
- | FIONBIO = & | + | This fetches the page https:// |
- | | + | |
- | | + | Here is the procedure: |
+ | |||
+ | <code bb4w> | ||
+ | DEF PROCsslfetch(port$, | ||
+ | LOCAL libssl%, libeay%, meth%, ctx%, sock%, temp%, res%, ssl%, sbio%, file% | ||
+ | LOCAL req$, buf&() | ||
- | ON ERROR LOCAL RESTORE ERROR : INSTALL @lib$+" | + | FIONBIO = & |
- | | + | |
+ | BUFSIZ = 256 | ||
- | SYS " | + | ON ERROR LOCAL RESTORE ERROR : INSTALL @lib$+"SOCKLIB" |
- | IF libssl% = 0 PROCsslcleanup | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | | + | |
- | IF libeay% = 0 PROCsslcleanup : ERROR 100, " | + | IF libeay% = 0 PROCsslcleanup : ERROR 100, " |
- | SYS " | + | SYS " |
- | REM Global system initialisation: | + | SYS " |
- | SYS `SSL_library_init` | + | IF libssl% = 0 PROCsslcleanup |
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | | ||
- | | + | |
- | SYS `SSLv23_method` TO meth% | + | SYS `TLSv1_2_method` TO meth% |
- | SYS `SSL_CTX_new`, | + | SYS `SSL_CTX_new`, |
- | IF ctx% = 0 PROCsslcleanup : ERROR 100, " | + | IF ctx% = 0 PROCsslcleanup : ERROR 100, " |
- | | + | |
- | sock% = FN_tcpconnect(host$, | + | sock% = FN_tcpconnect(host$, |
- | IF sock% < 0 PROCsslcleanup : ERROR 100, " | + | IF sock% < 0 PROCsslcleanup : ERROR 100, " |
- | | + | |
- | SYS `ioctlsocket`, | + | SYS `ioctlsocket`, |
- | IF res% PROCsslcleanup : ERROR 105, " | + | IF res% PROCsslcleanup : ERROR 105, " |
- | | + | |
- | SYS `SSL_new`, ctx% TO ssl% | + | SYS `SSL_new`, ctx% TO ssl% |
- | SYS `BIO_new_socket`, | + | SYS `BIO_new_socket`, |
- | SYS `SSL_set_bio`, | + | SYS `SSL_set_bio`, |
- | | + | |
- | IF res% <= 0 PROCsslcleanup : ERROR 100, "SSL connect failed: " + STR$res% | + | IF res% <= 0 PROCsslcleanup : ERROR 100, "SSL connect failed: " + STR$res% |
- | | + | |
- | req$ = "GET " + page$ + " HTTP/ | + | req$ = "GET " + page$ + " HTTP/ |
- | req$ += " | + | req$ += " |
- | req$ += "Host: " + host$ + ":" | + | req$ += "Host: " + host$ + ":" |
- | req$ += CHR$13 + CHR$10 | + | req$ += CHR$13 + CHR$10 |
- | | + | |
- | IF res% <> LEN(req$) PROCsslcleanup : ERROR 100, "SSL write failed: " + STR$res% | + | IF res% <> LEN(req$) PROCsslcleanup : ERROR 100, "SSL write failed: " + STR$res% |
- | | + | |
- | DIM buf& | + | DIM buf& |
- | | + | |
- | REPEAT | + | REPEAT |
- | SYS `SSL_read`, ssl%, ^buf& | + | SYS `SSL_read`, ssl%, ^buf& |
- | IF res% > 0 SYS " | + | IF res% > 0 SYS " |
- | UNTIL res% <= 0 | + | UNTIL res% <= 0 |
- | CLOSE #file% | + | CLOSE #file% |
- | IF res% PROCsslcleanup : ERROR 100, "SSL read failed: " + STR$res% | + | IF res% PROCsslcleanup : ERROR 100, "SSL read failed: " + STR$res% |
- | | + | |
- | PROCsslcleanup | + | PROCsslcleanup |
- | ENDPROC | + | ENDPROC |
- | | + | |
- | sock% += 0 : IF sock% PROC_closesocket(sock%) : sock% = 0 | + | sock% += 0 : IF sock% PROC_closesocket(sock%) : sock% = 0 |
- | ctx% += 0 : IF ctx% SYS `SSL_CTX_free`, | + | ctx% += 0 : IF ctx% SYS `SSL_CTX_free`, |
- | libssl% += 0 : IF libssl% SYS " | + | libssl% += 0 : IF libssl% SYS " |
- | libeay% += 0 : IF libeay% SYS " | + | libeay% += 0 : IF libeay% SYS " |
- | PROC_exitsockets | + | PROC_exitsockets |
- | ENDPROC | + | ENDPROC |
</ | </ |
fetching_20a_20secure_20web_20page.1523981192.txt.gz · Last modified: 2024/01/05 00:17 (external edit)