Last active
March 20, 2022 18:00
-
-
Save RainerRoss/6a4dba3975997e4ce3a6ef4af7c80b34 to your computer and use it in GitHub Desktop.
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
ctl-opt dftactgrp(*no) alloc(*teraspace) option(*nodebugio:*nounref) | |
main(main) actgrp('GETXML') bnddir('WEBSRVUTL'); | |
//------------------------------------------------------------------// | |
// // | |
// Get XMLInput from HTTP-Server and send XMLOutput to HTTP-Server // | |
// // | |
//----------------- // | |
// R.Ross 09.2018 * // | |
//------------------------------------------------------------------// | |
// Prototypen // | |
//------------------------------------------------------------------// | |
/include websrvutl/qcpysrc,websrvutl // Webservice Utilities | |
/include websrvutl/qcpysrc,iconv // ICONV | |
/include websrvutl/qcpysrc,dspgms // DSPGMS | |
/include websrvutl/qcpysrc,apierr // API-Error | |
/include yajl/qrpglesrc,yajl_h // JSON Utilities | |
//------------------------------------------------------------------// | |
// Array Input from HTTP-Server // | |
//------------------------------------------------------------------// | |
dcl-ds DsInput qualified inz; // Array Input | |
Data_p pointer; // Data-Pointer | |
DataLen int(10); // Data-Length | |
UTF8 ind; // UTF8 *on/*off | |
Method varchar(256); // Request-Method | |
ContType varchar(256); // Content-Type | |
AuthType varchar(256); // Authorization-Type | |
RmtUser char(10); // Remote-User | |
end-ds; | |
//------------------------------------------------------------------// | |
// Array XML-Input-Data // | |
//------------------------------------------------------------------// | |
dcl-ds Data qualified inz; | |
Id int(10); | |
Name char(30); | |
end-ds; | |
//------------------------------------------------------------------// | |
// Variablen // | |
//------------------------------------------------------------------// | |
dcl-s xmlData varchar(1000000) ccsid(*utf8); | |
//------------------------------------------------------------------// | |
// Main // | |
//------------------------------------------------------------------// | |
dcl-proc main; | |
dcl-s LocHeader like(GblHeader); // HTTP-Header | |
dcl-s LocData varchar(100000); // EBCDIC-Data | |
LocHeader = getHeader(XML); // Get HTTP-Header | |
DsInput = getXMLInput(); // Get XML Input | |
if DsInput.Data_p <> *null and DsInput.DataLen > *zero and | |
DsInput.UTF8 = *on; | |
LocData = cvtData(DsInput.Data_p:DsInput.DataLen:1141:1208); | |
parseXML(LocData); | |
endif; | |
xmlData = '<?xml version="1.0" encoding="UTF-8"?>' + | |
'<data>' + | |
'<id>1</id>' + | |
'<name>Tankstelle</name>' + | |
'</data>'; | |
wrtStdout(%addr(LocHeader:*data):%len(LocHeader):DsApierr); | |
wrtStdout(%addr(xmlData:*data):%len(xmlData):DsApierr); | |
end-proc; | |
//------------------------------------------------------------------// | |
// Parse XML Data // | |
//------------------------------------------------------------------// | |
dcl-proc parseXML; | |
dcl-pi *n; | |
PiData varchar(100000) const; | |
end-pi; | |
dcl-s LocOpt varchar(256); | |
clear Data; | |
LocOpt = 'doc=string + | |
case=any allowextra=yes + | |
allowmissing=yes'; // XML-Options | |
xml-into data %xml(%trim(PiData):%trim(LocOpt)); | |
end-proc; | |
//------------------------------------------------------------------// | |
// Convert Data between CCSID's // | |
//------------------------------------------------------------------// | |
dcl-proc cvtData; | |
dcl-pi *n varchar(100000) rtnparm; // Result | |
piSrc_p pointer const; // Source-Pointer | |
piLength uns(10) const; // Length | |
piTrgccsid uns(10) const; // Target-CCSID | |
piSrcccsid uns(10) const; // Source-CCSID | |
end-pi; | |
dcl-s LocTarget char(100000); // Target | |
dcl-s LocTrg_p pointer inz(%addr(LocTarget)); | |
dcl-s LocTrglen uns(10) inz(%size(LocTarget)); | |
dssrcccsid.tq_ccsid = piSrcccsid; // Source-CCSID | |
dstrgccsid.tq_ccsid = piTrgccsid; // Target-CCSID | |
dsconv = *allx'00'; | |
DsConv = iconv_open(DsTrgccsid:DsSrcccsid); // Open | |
iconv(DsConv:piSrc_p:piLength:LocTrg_p:LocTrglen); // Convert | |
iconv_close(DsConv); // Close | |
return %subst(LocTarget:1:%size(LocTarget) - LocTrglen); | |
end-proc; | |
//------------------------------------------------------------------// | |
// Get XML Input // | |
//------------------------------------------------------------------// | |
dcl-proc getXMLInput; | |
dcl-pi *n likeds(DsInput); // Array Input | |
end-pi; | |
dcl-ds PsInput likeds(DsInput) inz; // Array Input | |
dcl-s LocAvail int(10); // Available Length | |
PsInput.DataLen = %dec(%str(getenv('CONTENT_LENGTH':dsapierr)):10:0); | |
PsInput.Method = %str(getenv('REQUEST_METHOD':dsapierr)); | |
monitor; // Authorization-Type | |
PsInput.AuthType = %str(getenv('AUTH_TYPE':DsApierr)); | |
on-error; | |
endmon; | |
monitor; // Remote-User | |
PsInput.RmtUser = %str(getenv('REMOTE_USER':DsApierr)); | |
on-error; | |
endmon; | |
monitor; // Content-Type | |
PsInput.ContType = %str(getenv('CONTENT_TYPE':DsApierr)); | |
on-error; | |
endmon; | |
if PsInput.Method = 'POST' and PsInput.DataLen > *zero; | |
PsInput.Data_p = %alloc(PsInput.DataLen); // Data Pointer | |
readStdin(PsInput.Data_p:PsInput.DataLen:LocAvail:DsApierr); | |
if LocAvail > *zero; // Bytes available | |
if %scan('application/json':PsInput.ContType) > *zero or | |
%scan('application/xml' :PsInput.ContType) > *zero; | |
PsInput.UTF8 = *on; // UTF8-Data | |
endif; | |
endif; | |
endif; | |
return PsInput; | |
end-proc; | |
//------------------------------------------------------------------// |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<data> | |
<id>1</id> | |
<name>Tankstelle</name> | |
</data> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment