[M] Change in osmo-ttcn3-hacks[master]: HTTP_Adapter: allow API users to specifiy custom header
dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/36504?usp=email ) Change subject: HTTP_Adapter: allow API users to specifiy custom header ...................................................................... HTTP_Adapter: allow API users to specifiy custom header At the moment HTTP_Adapter can only make requests with a fixed pre-defined HTTP header. However, some application may require additional custom header lines or different values than the ones specified in the pre-defined HTTP header. Related: SYS#6824 Change-Id: I115fd14254e0957c0955649aeb47059dc180bf57 --- M library/HTTP_Adapter.ttcn 1 file changed, 57 insertions(+), 10 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-ttcn3-hacks refs/changes/04/36504/1 diff --git a/library/HTTP_Adapter.ttcn b/library/HTTP_Adapter.ttcn index 0885f05..879313d 100644 --- a/library/HTTP_Adapter.ttcn +++ b/library/HTTP_Adapter.ttcn @@ -15,6 +15,7 @@ import from HTTPmsg_Types all; import from HTTPmsg_PortType all; +import from Native_Functions all; type component http_CT { port HTTPmsg_PT HTTP; @@ -37,24 +38,54 @@ } template (value) Close ts_HTTP_Close := { client_id := omit }; -template (value) HeaderLines ts_HTTP_Header(charstring body, charstring host) := { - { header_name := "Host", header_value := host }, - { header_name := "Content-Type", header_value := "application/json" }, - { header_name := "Content-Length", header_value := int2str(lengthof(body)) } +function ts_HTTP_Header(template charstring body := omit, + template charstring host := omit, + HeaderLines custom_hdr := { }) +return template (value) HeaderLines { + var HeaderLines hdr := { }; + var integer i; + var integer k; + var boolean updated; + + /* Build default header */ + if (ispresent(host)) { + hdr := hdr & {{ header_name := "Host", header_value := valueof(host) }}; + } + hdr := hdr & {{ header_name := "Content-Type", header_value := "application/json" }}; + if (ispresent(body)) { + hdr := hdr & {{ header_name := "Content-Length", header_value := int2str(lengthof(body)) }} + } + + /* Add custom header lines, already existing headers (name) are updated */ + for (i := 0; i < lengthof(custom_hdr); i := i+1) { + updated := false; + for (k := 0; k < lengthof(hdr); k := k+1) { + if (f_str_tolower(hdr[k].header_name) == f_str_tolower(custom_hdr[i].header_name)) { + hdr[k] := custom_hdr[i]; + updated := true; + } + } + if (updated == false) { + hdr := hdr & { custom_hdr[i] }; + } + } + + return hdr; } template (value) HTTPMessage ts_HTTP_Req(charstring url, charstring method := "GET", charstring body := "", integer v_maj := 1, integer v_min := 1, - charstring host) := { + charstring host, + HeaderLines custom_hdr := { }) := { request := { client_id := omit, method := method, uri := url, version_major := v_maj, version_minor := v_min, - header := valueof(ts_HTTP_Header(body, host)), + header := valueof(ts_HTTP_Header(body, host, custom_hdr)), body := body } } @@ -73,11 +104,12 @@ template HTTPMessage tr_HTTP_Resp2xx := tr_HTTP_Resp((200..299)); -function f_http_tx_request(charstring url, charstring method := "GET", charstring body := "") +function f_http_tx_request(charstring url, charstring method := "GET", charstring body := "", + HeaderLines custom_hdr := { }) runs on http_CT { HTTP.send(ts_HTTP_Connect(g_http_host, g_http_port)); HTTP.receive(Connect_result:?); - HTTP.send(ts_HTTP_Req(url, method, body, host := g_http_host & ":" & int2str(g_http_port))); + HTTP.send(ts_HTTP_Req(url, method, body, host := g_http_host & ":" & int2str(g_http_port), custom_hdr := custom_hdr)); } function f_http_rx_response(template HTTPMessage exp := tr_HTTP_Resp2xx, float tout := 2.0) @@ -104,9 +136,9 @@ /* run a HTTP request and return the response */ function f_http_transact(charstring url, charstring method := "GET", charstring body := "", template HTTPMessage exp := tr_HTTP_Resp2xx, - float tout := 2.0) + float tout := 2.0, HeaderLines custom_hdr := { }) runs on http_CT return HTTPMessage { - f_http_tx_request(url, method, body); + f_http_tx_request(url, method, body, custom_hdr); return f_http_rx_response(exp, tout); } -- To view, visit https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/36504?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: osmo-ttcn3-hacks Gerrit-Branch: master Gerrit-Change-Id: I115fd14254e0957c0955649aeb47059dc180bf57 Gerrit-Change-Number: 36504 Gerrit-PatchSet: 1 Gerrit-Owner: dexter <pmaier@sysmocom.de> Gerrit-MessageType: newchange
participants (1)
-
dexter