Last active
June 5, 2020 14:10
-
-
Save NielsLiisberg/9e2e06fc0228c3c1fdb0253deea18a38 to your computer and use it in GitHub Desktop.
SQL Get a stream file via the HTTP/HTTPS protocol and store it on the IFS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Get a stream file via the HTTP/HTTPS protocol and store it on the IFS. | |
-- This was inspired by the "wget" AIX command, that is not always available | |
-- Note1: This also works for save files | |
-- Note2: This also compiles on vanilla systems without QSYSINCL installed | |
-- Note3: I am using library QUSRSYS. I suggest you put it into your tool library | |
-- I doubt it is a good idea to build huge applications this way, however it | |
-- is a cool example how far you can go with SQL: Have fun - | |
-- (C) Niels Liisberg 2020 | |
---------------------------------------------------------------------------------------------- | |
call qcmdexc ('crtsrcpf FILE(QTEMP/C) MBR(C)'); | |
delete from qtemp.c; | |
insert into qtemp.c (srcdta) values | |
-- Prototype warnings are chekked and OK | |
-- ('#include <sys/types.h>') | |
-- ('#include <sys/stat.h>') | |
-- ('#include <fcntl.h>') | |
('#include <sqludf.h>') | |
,('int outFile;') | |
,('long pos = 1;') | |
,('long remains;') | |
,('long retlen;') | |
,('char buf [32760];') | |
,('long chunk = sizeof(buf); ') | |
,('int mode = 448; // mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR;') | |
,('int opt = 106 ; // int opt = O_WRONLY | O_CREAT | O_TRUNC | O_CCSID ;') | |
,('int rc = sqludf_length(&MAIN.BUF, &remains);') | |
,('MAIN.NAME.DAT[MAIN.NAME.LEN] =0;') | |
,('outFile = open(MAIN.NAME.DAT, opt, mode, 1252);') | |
,('while (rc == 0 && remains > 0) {') | |
,(' rc = sqludf_substr(&MAIN.BUF, pos, chunk , buf , &retlen),') | |
,(' write (outFile , buf, retlen);') | |
,(' pos += chunk;') | |
,(' remains -= chunk;') | |
,('} ') | |
,('close(outFile);') | |
; | |
create or replace procedure qusrsys.webget | |
( | |
url varchar(4096), | |
tofile varchar(256) | |
) | |
external action | |
modifies sql data | |
set option output=*print, commit=*chg, dbgview = *list | |
main: begin | |
declare buf blob(2G); | |
declare header clob (1K) default null; | |
declare name varchar(256) ; | |
-- "buf" and "name" are in the C-code as MAIN.BUF and MAIN.NAME | |
set buf = SYSTOOLS.HTTPGETBLOB( url => url , httpheader => header); | |
set name = tofile; | |
include qtemp/c(c); | |
end; | |
-- Simple get a stream file and put it on the IFS | |
call qusrsys.webget ( | |
url=> 'http://webfiles.system-method.com/packages/Icebox2pkg/ip2.tar' , | |
tofile => '/tmp/ip2.tar' | |
); | |
-- Savefile example | |
cl: crtsavf qgpl/icebox2; | |
call qusrsys.webget ( | |
url=> 'http://webfiles.system-method.com/packages/Icebox2pkg/icebox2.savf' , | |
tofile => '/qsys.lib/qgpl.lib/icebox2.file' | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment