Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 1 | module HTTP_Adapter { |
| 2 | |
| 3 | /* HTTP Adapter component, originally part of Integration Tests for osmo-remsim-server |
| 4 | * (C) 2019 by Harald Welte <laforge@gnumonks.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Released under the terms of GNU General Public License, Version 2 or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 11 | * |
| 12 | * This test suite tests osmo-remsim-server by attaching to the external interfaces |
| 13 | * such as RSPRO for simulated clients + bankds and RSRES (REST backend interface). |
| 14 | */ |
| 15 | |
| 16 | import from HTTPmsg_Types all; |
| 17 | import from HTTPmsg_PortType all; |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 18 | import from Native_Functions all; |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 19 | |
| 20 | type component http_CT { |
| 21 | port HTTPmsg_PT HTTP; |
Philipp Maier | 260f708 | 2024-04-19 13:12:49 +0200 | [diff] [blame] | 22 | var HTTP_Adapter_Params g_pars; |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 23 | }; |
| 24 | |
Philipp Maier | 260f708 | 2024-04-19 13:12:49 +0200 | [diff] [blame] | 25 | type record HTTP_Adapter_Params { |
| 26 | charstring http_host, |
Philipp Maier | 832b1ef | 2024-03-26 17:12:37 +0100 | [diff] [blame] | 27 | integer http_port, |
| 28 | boolean use_ssl |
Philipp Maier | 260f708 | 2024-04-19 13:12:49 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | function f_http_init(HTTP_Adapter_Params pars) runs on http_CT { |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 32 | map(self:HTTP, system:HTTP); |
Philipp Maier | 260f708 | 2024-04-19 13:12:49 +0200 | [diff] [blame] | 33 | g_pars := pars; |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | template (value) Connect ts_HTTP_Connect(template (value) charstring hostname, |
| 37 | template (value) integer http_port := 80, |
| 38 | template (value) boolean use_ssl := false) := { |
| 39 | hostname := hostname, |
| 40 | portnumber := http_port, |
| 41 | use_ssl := use_ssl |
| 42 | } |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 43 | template (value) Close ts_HTTP_Close(template (omit) integer client_id := omit) := { client_id := client_id }; |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 44 | |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 45 | /* function to add HeaderLines to a an existing set of HeaderLines. HeaderLines that are already present, are updated. */ |
| 46 | function f_overlay_HTTP_Header(HeaderLines hdr, HeaderLines additional_hdr) return template (value) HeaderLines |
| 47 | { |
| 48 | var integer i; |
| 49 | var integer k; |
| 50 | var boolean updated; |
| 51 | |
| 52 | for (i := 0; i < lengthof(additional_hdr); i := i+1) { |
| 53 | updated := false; |
| 54 | for (k := 0; k < lengthof(hdr); k := k+1) { |
| 55 | if (f_str_tolower(hdr[k].header_name) == f_str_tolower(additional_hdr[i].header_name)) { |
| 56 | hdr[k] := additional_hdr[i]; |
| 57 | updated := true; |
| 58 | } |
| 59 | } |
| 60 | if (updated == false) { |
| 61 | hdr := hdr & { additional_hdr[i] }; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return hdr; |
| 66 | } |
| 67 | |
| 68 | template (value) HeaderLine ts_HeaderLine(charstring header_name, charstring header_value) := { |
| 69 | header_name := header_name, |
| 70 | header_value := header_value |
| 71 | } |
| 72 | |
| 73 | function f_ts_HTTP_Header(template (omit) charstring body := omit, |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 74 | template (omit) octetstring binary_body := omit, |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 75 | template (omit) charstring host := omit, |
| 76 | HeaderLines custom_hdr := { }) |
| 77 | return template (value) HeaderLines { |
| 78 | var HeaderLines hdr := { }; |
| 79 | |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 80 | /* Make sure we never use body or binary_body at the same time */ |
| 81 | if (not istemplatekind(body, "omit") and not istemplatekind(binary_body, "omit")) { |
| 82 | setverdict(fail, "use wither (ascii) body or binary_body"); |
| 83 | } |
| 84 | |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 85 | /* Build default header */ |
| 86 | if (not istemplatekind(host, "omit")) { |
| 87 | hdr := hdr & {valueof(ts_HeaderLine("Host", valueof(host)))}; |
| 88 | } |
| 89 | hdr := hdr & {{ header_name := "Content-Type", header_value := "application/json" }}; |
| 90 | if (not istemplatekind(body, "omit")) { |
| 91 | hdr := hdr & {valueof(ts_HeaderLine("Content-Length", int2str(lengthof(body))))}; |
| 92 | } |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 93 | else if (not istemplatekind(binary_body, "omit")) { |
| 94 | hdr := hdr & {valueof(ts_HeaderLine("Content-Length", int2str(lengthof(binary_body))))}; |
| 95 | } |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 96 | |
| 97 | return f_overlay_HTTP_Header(hdr, custom_hdr); |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Philipp Maier | ef20fa0 | 2024-03-26 15:50:22 +0100 | [diff] [blame] | 100 | function f_ts_body_or_empty(template (omit) charstring body) return template (value) charstring { |
| 101 | if (istemplatekind(body, "omit")) { |
| 102 | return ""; |
| 103 | } |
| 104 | return body; |
| 105 | } |
| 106 | |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 107 | template (value) HTTPMessage ts_HTTP_Req(charstring url, |
| 108 | charstring method := "GET", |
Philipp Maier | ef20fa0 | 2024-03-26 15:50:22 +0100 | [diff] [blame] | 109 | template (omit) charstring body := omit, |
Philipp Maier | 4a19b47 | 2024-03-25 15:08:43 +0100 | [diff] [blame] | 110 | integer v_maj := 1, integer v_min := 1, |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 111 | charstring host, |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 112 | HeaderLines custom_hdr := { }, |
| 113 | template (omit) integer client_id := omit) := { |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 114 | request := { |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 115 | client_id := client_id, |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 116 | method := method, |
| 117 | uri := url, |
| 118 | version_major := v_maj, |
| 119 | version_minor := v_min, |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 120 | header := f_ts_HTTP_Header(body, omit, host, custom_hdr), |
Philipp Maier | ef20fa0 | 2024-03-26 15:50:22 +0100 | [diff] [blame] | 121 | body := f_ts_body_or_empty(body) |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 125 | function f_ts_body_or_empty_bin(template (omit) octetstring body) return template (value) octetstring { |
| 126 | if (istemplatekind(body, "omit")) { |
| 127 | return ''O; |
| 128 | } |
| 129 | return body; |
| 130 | } |
| 131 | |
| 132 | template (value) HTTPMessage ts_HTTP_Req_Bin(charstring url, |
| 133 | charstring method := "GET", |
| 134 | template (omit) octetstring body := omit, |
| 135 | integer v_maj := 1, integer v_min := 1, |
| 136 | charstring host, |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 137 | HeaderLines custom_hdr := { }, |
| 138 | template (omit) integer client_id := omit) := { |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 139 | request_binary := { |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 140 | client_id := client_id, |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 141 | method := method, |
| 142 | uri := url, |
| 143 | version_major := v_maj, |
| 144 | version_minor := v_min, |
| 145 | header := f_ts_HTTP_Header(omit, body, host, custom_hdr), |
| 146 | body := f_ts_body_or_empty_bin(body) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 151 | template HTTPMessage tr_HTTP_Resp(template integer sts := ?) := { |
| 152 | response := { |
| 153 | client_id := ?, |
| 154 | version_major := ?, |
| 155 | version_minor := ?, |
| 156 | statuscode := sts, |
| 157 | statustext := ?, |
| 158 | header := ?, |
| 159 | body := ? |
| 160 | } |
| 161 | }; |
| 162 | |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 163 | template HTTPMessage tr_HTTP_Resp_Bin(template integer sts := ?) := { |
| 164 | response_binary := { |
| 165 | client_id := ?, |
| 166 | version_major := ?, |
| 167 | version_minor := ?, |
| 168 | statuscode := sts, |
| 169 | statustext := ?, |
| 170 | header := ?, |
| 171 | body := ? |
| 172 | } |
| 173 | }; |
| 174 | |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 175 | template HTTPMessage tr_HTTP_Resp2xx := tr_HTTP_Resp((200..299)); |
| 176 | |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 177 | function f_http_tx_request(charstring url, charstring method := "GET", |
| 178 | template charstring body := omit, |
| 179 | template octetstring binary_body := omit, |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 180 | HeaderLines custom_hdr := { }, |
| 181 | float tout := 2.0, |
| 182 | template integer client_id := omit) |
Harald Welte | 205b537 | 2021-02-22 09:16:21 +0100 | [diff] [blame] | 183 | runs on http_CT { |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 184 | var Connect_result rc; |
| 185 | timer T := tout; |
| 186 | var template integer use_client_id := omit; |
| 187 | |
| 188 | /* In case the caller didn't specify a client_id, we will create a new connection. */ |
| 189 | if (istemplatekind(client_id, "omit")) { |
| 190 | HTTP.send(ts_HTTP_Connect(g_pars.http_host, g_pars.http_port, g_pars.use_ssl)); |
| 191 | T.start; |
| 192 | alt { |
| 193 | [] HTTP.receive(Connect_result:?) -> value rc; |
| 194 | [] HTTP.receive { |
| 195 | setverdict(fail, "HTTP connection to client failed"); |
| 196 | self.stop; |
| 197 | } |
| 198 | [] T.timeout { |
| 199 | setverdict(fail, "Timeout waiting for completion of HTTP connection"); |
| 200 | self.stop; |
| 201 | } |
| 202 | } |
| 203 | use_client_id := rc.client_id; |
| 204 | } else { |
| 205 | use_client_id := client_id; |
| 206 | } |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 207 | |
| 208 | if (not istemplatekind(body, "omit")) { |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 209 | HTTP.send(ts_HTTP_Req(url, method, body, host := g_pars.http_host & ":" & int2str(g_pars.http_port), |
| 210 | custom_hdr := custom_hdr, client_id := use_client_id)); |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 211 | } else if (not istemplatekind(binary_body, "omit")) { |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 212 | HTTP.send(ts_HTTP_Req_Bin(url, method, binary_body, |
| 213 | host := g_pars.http_host & ":" & int2str(g_pars.http_port), |
| 214 | custom_hdr := custom_hdr, client_id := use_client_id)); |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 215 | } |
Harald Welte | 205b537 | 2021-02-22 09:16:21 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 218 | function f_http_rx_response(template HTTPMessage exp := tr_HTTP_Resp2xx, |
| 219 | float tout := 2.0, |
| 220 | template integer client_id := omit, |
| 221 | boolean keep_connection := false) |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 222 | runs on http_CT return HTTPMessage { |
| 223 | var HTTPMessage resp; |
Harald Welte | ad9d836 | 2021-02-22 10:23:00 +0100 | [diff] [blame] | 224 | timer T := tout; |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 225 | T.start; |
| 226 | alt { |
| 227 | [] HTTP.receive(exp) -> value resp { |
| 228 | setverdict(pass); |
| 229 | } |
| 230 | [] HTTP.receive(tr_HTTP_Resp) -> value resp { |
| 231 | setverdict(fail, "Unexpected HTTP response ", resp); |
| 232 | } |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 233 | [] HTTP.receive(tr_HTTP_Resp_Bin) -> value resp { |
| 234 | setverdict(fail, "Unexpected (binary) HTTP response ", resp); |
| 235 | } |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 236 | [] T.timeout { |
| 237 | setverdict(fail, "Timeout waiting for HTTP response"); |
| 238 | self.stop; |
| 239 | } |
| 240 | } |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 241 | |
| 242 | if (not keep_connection) { |
| 243 | HTTP.send(ts_HTTP_Close(client_id)); |
| 244 | } |
| 245 | |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 246 | return resp; |
| 247 | } |
| 248 | |
Harald Welte | 205b537 | 2021-02-22 09:16:21 +0100 | [diff] [blame] | 249 | /* run a HTTP request and return the response */ |
| 250 | function f_http_transact(charstring url, charstring method := "GET", |
Philipp Maier | 28e7ad3 | 2024-04-24 15:44:15 +0200 | [diff] [blame] | 251 | template (omit) charstring body := omit, |
| 252 | template (omit) octetstring binary_body := omit, |
| 253 | template HTTPMessage exp := tr_HTTP_Resp2xx, |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 254 | float tout := 2.0, HeaderLines custom_hdr := { }, |
| 255 | template integer client_id := omit, |
| 256 | boolean keep_connection := false) |
Harald Welte | 205b537 | 2021-02-22 09:16:21 +0100 | [diff] [blame] | 257 | runs on http_CT return HTTPMessage { |
Philipp Maier | 0957b1b | 2024-04-25 11:38:05 +0200 | [diff] [blame^] | 258 | f_http_tx_request(url, method, body, binary_body, custom_hdr, tout, client_id); |
| 259 | return f_http_rx_response(exp, tout, client_id, keep_connection); |
| 260 | } |
| 261 | |
| 262 | function f_http_client_id_from_http_response(template HTTPMessage response_http) return template integer { |
| 263 | if (ispresent(response_http.response_binary)) { |
| 264 | return response_http.response_binary.client_id; |
| 265 | } else if (ispresent(response_http.response)) { |
| 266 | return response_http.response.client_id; |
| 267 | } |
| 268 | |
| 269 | return omit; |
Harald Welte | 205b537 | 2021-02-22 09:16:21 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 272 | } |