@@ -32,6 +32,10 @@ processingParams.clientActivated = scriptParams:get('clientActivated')
3232processingParams .hostnameVerification = scriptParams :get (' hostnameVerification' )
3333processingParams .peerVerification = scriptParams :get (' peerVerification' )
3434processingParams .cookieStore = scriptParams :get (' cookieStore' )
35+ processingParams .basicAuthentication = scriptParams :get (' basicAuthentication' )
36+ processingParams .basicAuthenticationUser = scriptParams :get (' basicAuthenticationUser' )
37+ processingParams .basicAuthenticationPassword = scriptParams :get (' basicAuthenticationPassword' )
38+
3539processingParams .clientAuthentication = scriptParams :get (' clientAuthentication' )
3640processingParams .caBundleFileName = scriptParams :get (' caBundleFileName' )
3741processingParams .clientCertificateType = scriptParams :get (' clientCertificateType' )
@@ -119,6 +123,11 @@ local function updateClient()
119123 clientObject = HTTPClient .create ()
120124
121125 clientObject :setVerbose (processingParams .verboseMode )
126+
127+ if processingParams .basicAuthentication then
128+ clientObject :setAuthCredentials (processingParams .basicAuthenticationUser , processingParams .basicAuthenticationPassword )
129+ end
130+
122131 if processingParams .clientAuthentication then
123132 if File .exists (processingParams .caBundleFileName ) then
124133 clientObject :setCABundle (processingParams .caBundleFileName )
@@ -179,12 +188,14 @@ end
179188
180189--- Function to execute a request
181190--- @param tempRequestActive bool Status if it is just a temporarly configured request or a preconfigured one
182- --- @param showResponse bool Status if repsonse should be notified as event (e.g. to show it on UI )
183- --- @param eventName string Name of event to notify the response
184- local function sendRequest (tempRequestActive , showResponse , eventName )
191+ --- @param showResponse bool Status if response should be notified as event (e.g. to show it on UI )
192+ --- @param eventName string ? Optional name of event to notify the response
193+ --- @return string response Response of HTTP request
194+ local function sendInternalRequest (tempRequestActive , showResponse , eventName )
185195
186196 local timerQueueSize = 0
187197 local eventQueueSize = 0
198+ local jsonResponse = {}
188199
189200 if tempRequestActive then
190201 updateInternalRequest (processingParams )
@@ -200,37 +211,50 @@ local function sendRequest(tempRequestActive, showResponse, eventName)
200211
201212 if timerQueueSize >= processingParams .queueSize - 1 or eventQueueSize >= processingParams .queueSize - 1 then
202213 _G .logger :warning (nameOfModule .. " : Internal request queue too high '(Timer:" .. tostring (timerQueueSize ) .. ' /Events:' .. tostring (eventQueueSize ) .. " )'. Will skip new requests..." )
214+ return ' '
203215 else
204216 local tic = DateTime .getTimestamp ()
217+
205218 local response = clientObject :execute (request )
206219 local procTime = DateTime .getTimestamp () - tic
207220
208221 local success = HTTPClient .Response .getSuccess (response )
222+ jsonResponse .Success = tostring (success )
209223 _G .logger :fine (nameOfModule .. " : Response success = " .. tostring (success ))
210224
211225 -- Check response
212226 local responseMessage = ' After ' .. tostring (procTime ) .. ' ms --> ' .. ' Request success = ' .. tostring (success ) .. ' \n '
213- responseMessage = responseMessage .. ' Code: ' .. HTTPClient .Response .getStatusCode (response ) .. ' \n '
227+ local statusCode = HTTPClient .Response .getStatusCode (response )
228+ responseMessage = responseMessage .. ' Code: ' .. statusCode .. ' \n '
229+ jsonResponse .StatusCode = statusCode
214230
215231 if success then
216232 -- Check if extended information of the response should be shown
217233 if processingParams .extendedResponse then
218- responseMessage = responseMessage .. ' Request content type: ' .. HTTPClient .Response .getContentType (response ) .. ' \n '
234+ local contentType = HTTPClient .Response .getContentType (response )
235+ responseMessage = responseMessage .. ' Request content type: ' .. contentType .. ' \n '
236+ jsonResponse .ContentType = contentType
219237
220238 local tempKeys = HTTPClient .Response .getHeaderKeys (response )
239+ jsonResponse .Headers = {}
221240 for _ , headerKeys in pairs (tempKeys ) do
241+ jsonResponse .Headers [headerKeys ] = {}
222242
223243 local suc , tempValues = HTTPClient .Response .getHeaderValues (response , headerKeys )
224244 for _ , headerValue in pairs (tempValues ) do
225245 responseMessage = responseMessage .. ' Header-key: ' .. headerKeys .. ' = ' .. headerValue .. ' \n '
246+ table.insert (jsonResponse .Headers [headerKeys ], headerValue )
226247 end
227248 end
228249 end
229250
251+ jsonResponse .Response = HTTPClient .Response .getContent (response )
230252 responseMessage = responseMessage .. helperFuncs .jsonLine2Table (HTTPClient .Response .getContent (response )) .. ' \n '
231253 else
232254 local error = HTTPClient .Response .getError (response )
233255 local errorDetail = HTTPClient .Response .getErrorDetail (response )
256+ jsonResponse .Error = error
257+ jsonResponse .ErrorDetail = errorDetail
234258 responseMessage = responseMessage .. ' Error = ' .. error .. ' \n ' .. ' Error details = ' .. errorDetail .. ' \n '
235259 end
236260 if eventName then
@@ -244,8 +268,27 @@ local function sendRequest(tempRequestActive, showResponse, eventName)
244268 Script .notifyEvent (" MultiHTTPClient_OnNewValueToForward" .. multiHTTPClientInstanceNumberString , " MultiHTTPClient_OnNewResponseMessage" , responseMessage )
245269 end
246270 end
271+ return json .encode (jsonResponse )
272+ end
273+ end
274+
275+ local function sendRequest (mode , endpoint , port , header , body , contentType )
276+ processingParams .extendedResponse = true
277+ processingParams .requestMode = mode
278+ processingParams .requestEndpoint = endpoint
279+ processingParams .requestPort = port
280+
281+ if body then
282+ processingParams .requestContent = body
283+ else
284+ processingParams .requestContent = ' '
247285 end
286+
287+ local response = sendInternalRequest (true , false )
288+
289+ return response
248290end
291+ Script .serveFunction (' CSK_MultiHTTPClient.sendRequest' .. multiHTTPClientInstanceNumberString , sendRequest , ' string, string, int, string:?, string:?, string:?' , ' string' )
249292
250293--- Function to set timer if request should be executed periodically
251294--- @param requestName string Name of preconfigured request
@@ -260,9 +303,9 @@ local function setTimer(requestName)
260303 if processingParams .clientActivated then
261304 setSpecificRequest (requestName )
262305 if selectedRequest == requestName then
263- sendRequest (false , true , " MultiHTTPClient_OnNewReponse" .. multiHTTPClientInstanceNumberString .. ' _' .. processingParams .requests [requestName ].requestName )
306+ sendInternalRequest (false , true , " MultiHTTPClient_OnNewReponse" .. multiHTTPClientInstanceNumberString .. ' _' .. processingParams .requests [requestName ].requestName )
264307 else
265- sendRequest (false , false , " MultiHTTPClient_OnNewReponse" .. multiHTTPClientInstanceNumberString .. ' _' .. processingParams .requests [requestName ].requestName )
308+ sendInternalRequest (false , false , " MultiHTTPClient_OnNewReponse" .. multiHTTPClientInstanceNumberString .. ' _' .. processingParams .requests [requestName ].requestName )
266309 end
267310 end
268311 end
@@ -306,9 +349,9 @@ local function setRegisteredEvent(requestName)
306349 end
307350 setSpecificRequest (requestName )
308351 if selectedRequest == requestName then
309- sendRequest (false , true , " MultiHTTPClient_OnNewReponse" .. multiHTTPClientInstanceNumberString .. ' _' .. processingParams .requests [requestName ].requestName )
352+ sendInternalRequest (false , true , " MultiHTTPClient_OnNewReponse" .. multiHTTPClientInstanceNumberString .. ' _' .. processingParams .requests [requestName ].requestName )
310353 else
311- sendRequest (false , false , " MultiHTTPClient_OnNewReponse" .. multiHTTPClientInstanceNumberString .. ' _' .. processingParams .requests [requestName ].requestName )
354+ sendInternalRequest (false , false , " MultiHTTPClient_OnNewReponse" .. multiHTTPClientInstanceNumberString .. ' _' .. processingParams .requests [requestName ].requestName )
312355 end
313356 end
314357 end
@@ -501,7 +544,7 @@ local function handleOnNewProcessingParameter(multiHTTPClientNo, parameter, valu
501544
502545 elseif parameter == ' sendRequest' then
503546 if processingParams .clientActivated then
504- sendRequest (true , true )
547+ sendInternalRequest (true , true )
505548 end
506549
507550 elseif parameter == ' headerUpdate' then
0 commit comments