fixeria has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/41124?usp=email )


Change subject: library/HTTP_Adapter: do not hard-code 'Content-Type'
......................................................................

library/HTTP_Adapter: do not hard-code 'Content-Type'

* Do not send `Content-Type` when request contains no body.
** This is wrong and some servers would reject such a request.
* Allow passing custom `Content-Type` to
** `f_http_tx_request()` and
** `f_http_transact()`.
* Use `application/json` by default.

Change-Id: Ie35ffc56b4fedc0b4d8c4a689a2232b515b7b326
---
M library/HTTP_Adapter.ttcn
1 file changed, 14 insertions(+), 6 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-ttcn3-hacks 
refs/changes/24/41124/1

diff --git a/library/HTTP_Adapter.ttcn b/library/HTTP_Adapter.ttcn
index ab5e6b1..8f15958 100644
--- a/library/HTTP_Adapter.ttcn
+++ b/library/HTTP_Adapter.ttcn
@@ -73,6 +73,7 @@

 function f_ts_HTTP_Header(template (omit) charstring body := omit,
                          template (omit) octetstring binary_body := omit,
+                         template (omit) charstring content_type := omit,
                          template (omit) charstring host := omit,
                          HeaderLines custom_hdr := { })
 return template (value) HeaderLines {
@@ -87,7 +88,9 @@
        if (not istemplatekind(host, "omit")) {
                hdr := hdr & {valueof(ts_HeaderLine("Host", valueof(host)))};
        }
-       hdr := hdr & {{ header_name := "Content-Type", header_value := 
"application/json" }};
+       if (not istemplatekind(content_type, "omit")) {
+               hdr := hdr & {valueof(ts_HeaderLine("Content-Type", 
valueof(content_type)))};
+       }
        if (not istemplatekind(body, "omit")) {
                hdr := hdr & {valueof(ts_HeaderLine("Content-Length", 
int2str(lengthof(body))))};
        }
@@ -108,6 +111,7 @@
 template (value) HTTPMessage ts_HTTP_Req(charstring url,
                                         charstring method := "GET",
                                         template (omit) charstring body := 
omit,
+                                        template (omit) charstring 
content_type := omit,
                                         integer v_maj := 1, integer v_min := 1,
                                         charstring host,
                                         HeaderLines custom_hdr := { },
@@ -118,7 +122,7 @@
                uri := url,
                version_major := v_maj,
                version_minor := v_min,
-               header := f_ts_HTTP_Header(body, omit, host, custom_hdr),
+               header := f_ts_HTTP_Header(body, omit, content_type, host, 
custom_hdr),
                body := f_ts_body_or_empty(body)
        }
 }
@@ -133,6 +137,7 @@
 template (value) HTTPMessage ts_HTTP_Req_Bin(charstring url,
                                             charstring method := "GET",
                                             template (omit) octetstring body 
:= omit,
+                                            template (omit) charstring 
content_type := omit,
                                             integer v_maj := 1, integer v_min 
:= 1,
                                             charstring host,
                                             HeaderLines custom_hdr := { },
@@ -143,7 +148,7 @@
                uri := url,
                version_major := v_maj,
                version_minor := v_min,
-               header := f_ts_HTTP_Header(omit, body, host, custom_hdr),
+               header := f_ts_HTTP_Header(omit, body, content_type, host, 
custom_hdr),
                body := f_ts_body_or_empty_bin(body)
        }
 }
@@ -177,6 +182,7 @@
 function f_http_tx_request(charstring url, charstring method := "GET",
                           template (omit) charstring body := omit,
                           template (omit) octetstring binary_body := omit,
+                          charstring content_type := "application/json",
                           HeaderLines custom_hdr := { },
                           float tout := 2.0,
                           template (omit) integer client_id := omit,
@@ -223,11 +229,12 @@

        if (not istemplatekind(body, "omit") and istemplatekind(binary_body, 
"omit")) {
                /* HTTP message with ASCII content */
-               HTTP.send(ts_HTTP_Req(url, method, body, host := 
use_http_pars.http_host & ":" & int2str(use_http_pars.http_port),
+               HTTP.send(ts_HTTP_Req(url, method, body, content_type,
+                                     host := use_http_pars.http_host & ":" & 
int2str(use_http_pars.http_port),
                                      custom_hdr := custom_hdr, client_id := 
use_client_id));
        } else if (not istemplatekind(binary_body, "omit") and 
istemplatekind(body, "omit")) {
                /* HTTP message with binary content */
-               HTTP.send(ts_HTTP_Req_Bin(url, method, binary_body,
+               HTTP.send(ts_HTTP_Req_Bin(url, method, binary_body, 
content_type,
                                          host := use_http_pars.http_host & ":" 
& int2str(use_http_pars.http_port),
                                          custom_hdr := custom_hdr, client_id 
:= use_client_id));
        } else if (istemplatekind(binary_body, "omit") and istemplatekind(body, 
"omit")) {
@@ -274,13 +281,14 @@
 function f_http_transact(charstring url, charstring method := "GET",
                         template (omit) charstring body := omit,
                         template (omit) octetstring binary_body := omit,
+                        charstring content_type := "application/json",
                         template HTTPMessage exp := tr_HTTP_Resp2xx,
                         float tout := 2.0, HeaderLines custom_hdr := { },
                         template (omit) integer client_id := omit,
                         boolean keep_connection := false,
                         template (omit) HTTP_Adapter_Params http_pars := omit)
 runs on http_CT return HTTPMessage {
-       f_http_tx_request(url, method, body, binary_body, custom_hdr, tout, 
client_id, http_pars);
+       f_http_tx_request(url, method, body, binary_body, content_type, 
custom_hdr, tout, client_id, http_pars);
        return f_http_rx_response(exp, tout, client_id, keep_connection);
 }


--
To view, visit https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/41124?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: osmo-ttcn3-hacks
Gerrit-Branch: master
Gerrit-Change-Id: Ie35ffc56b4fedc0b4d8c4a689a2232b515b7b326
Gerrit-Change-Number: 41124
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <vyanits...@sysmocom.de>

Reply via email to