From f770c236d592ad716b25ede924d8784fd5b20f8f Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Fri, 27 Oct 2017 19:24:52 +0200 Subject: [PATCH] Improved support for absolute url passed tp HTTP_REQUEST_SESSION . --- .../http_client/src/http_client_request.e | 6 ++-- .../http_client/src/http_client_session.e | 30 ++++++++++++++++++- .../http_client_request_parameter.e | 12 ++++++++ .../http_client_request_parameters.e | 5 ++++ .../http_client/tests/test_http_client_i.e | 16 ++++++++++ .../tests/test_libcurl_http_client.e | 5 ++++ .../http_client/tests/test_net_http_client.e | 5 ++++ 7 files changed, 75 insertions(+), 4 deletions(-) diff --git a/library/network/http_client/src/http_client_request.e b/library/network/http_client/src/http_client_request.e index c2217bf2..b645ebba 100644 --- a/library/network/http_client/src/http_client_request.e +++ b/library/network/http_client/src/http_client_request.e @@ -36,13 +36,13 @@ feature {NONE} -- Initialization i := a_url.substring_index ("://", 1) if i > 0 then check - a_url.substring (1, i).same_string ("http") - or a_url.substring (1, i).same_string ("https") + a_url.head (i - 1).same_string ("http") + or a_url.head (i - 1).same_string ("https") end url := a_url else url := session.url (a_url, Void) - end + end headers := session.headers.twin if ctx /= Void then context := ctx diff --git a/library/network/http_client/src/http_client_session.e b/library/network/http_client/src/http_client_session.e index e0b469dc..3968c7ec 100644 --- a/library/network/http_client/src/http_client_session.e +++ b/library/network/http_client/src/http_client_session.e @@ -61,12 +61,40 @@ feature -- Access url (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT): STRING_8 -- Url computed from Current and `ctx' data. do - Result := base_url + a_path + if is_absolute_url (a_path) then + -- Is Absolute url + Result := a_path + else + Result := base_url + a_path + end if ctx /= Void then ctx.append_query_parameters_to_url (Result) end end + is_absolute_url (s: READABLE_STRING_GENERAL): BOOLEAN + -- Does `s` represent an absolute url? + local + i, pos: INTEGER + sch: READABLE_STRING_GENERAL + do + pos := s.substring_index ("://", 1) + if pos > 0 then + sch := s.head (pos - 1) + if not sch.is_whitespace then + from + i := 1 + Result := True + until + not Result or i > sch.count + loop + Result := sch[i].is_alpha_numeric + i := i + 1 + end + end + end + end + feature {NONE} -- Access: verbose verbose_mode: INTEGER diff --git a/library/network/http_client/src/implementation/http_client_request_parameter.e b/library/network/http_client/src/implementation/http_client_request_parameter.e index fcaf13da..729562cb 100644 --- a/library/network/http_client/src/implementation/http_client_request_parameter.e +++ b/library/network/http_client/src/implementation/http_client_request_parameter.e @@ -7,6 +7,9 @@ note deferred class HTTP_CLIENT_REQUEST_PARAMETER +inherit + DEBUG_OUTPUT + feature -- Access name: READABLE_STRING_32 @@ -18,6 +21,15 @@ feature -- Access deferred end +feature -- Status report + + debug_output: STRING_32 + do + create Result.make_empty + Result.append (name) + Result.append ("=...") + end + feature -- Conversion append_form_url_encoded_to (a_output: STRING_8) diff --git a/library/network/http_client/src/implementation/http_client_request_parameters.e b/library/network/http_client/src/implementation/http_client_request_parameters.e index bb6a7a9b..242a5afc 100644 --- a/library/network/http_client/src/implementation/http_client_request_parameters.e +++ b/library/network/http_client/src/implementation/http_client_request_parameters.e @@ -29,6 +29,11 @@ feature -- Access Result := items.count end + has (a_parameter_name: READABLE_STRING_GENERAL): BOOLEAN + do + Result := across items as ic some a_parameter_name.same_string (ic.item.name) end + end + feature -- Element change extend, force (i: G) diff --git a/library/network/http_client/tests/test_http_client_i.e b/library/network/http_client/tests/test_http_client_i.e index f8228f3c..65d5105e 100644 --- a/library/network/http_client/tests/test_http_client_i.e +++ b/library/network/http_client/tests/test_http_client_i.e @@ -74,6 +74,22 @@ feature -- Test routines end end + test_abs_url + local + sess: like new_session + h: STRING_8 + l_url: STRING + do + sess := new_session ("https://www.eiffel.org") + l_url := "/foo/bar" + assert ("abs rel", sess.url (l_url, Void).same_string (sess.base_url + l_url)) + + l_url := "https://www.eiffel.org/foo/bar" + assert ("abs 1", sess.url (l_url, Void).same_string (l_url)) + l_url := "https://example.com/foo/bar" + assert ("abs 2", sess.url (l_url, Void).same_string (l_url)) + end + test_headers local res: HTTP_CLIENT_RESPONSE diff --git a/library/network/http_client/tests/test_libcurl_http_client.e b/library/network/http_client/tests/test_libcurl_http_client.e index 2cb0db17..ff35a09d 100644 --- a/library/network/http_client/tests/test_libcurl_http_client.e +++ b/library/network/http_client/tests/test_libcurl_http_client.e @@ -32,6 +32,11 @@ feature -- Tests test_http_client_ssl end + test_libcurl_abs_url + do + test_abs_url + end + test_libcurl_headers do test_headers diff --git a/library/network/http_client/tests/test_net_http_client.e b/library/network/http_client/tests/test_net_http_client.e index c96f83ca..030bb0a0 100644 --- a/library/network/http_client/tests/test_net_http_client.e +++ b/library/network/http_client/tests/test_net_http_client.e @@ -32,6 +32,11 @@ feature -- Tests test_http_client_ssl end + test_net_abs_url + do + test_abs_url + end + test_net_headers do test_headers