implemented http authorization, support for redirection and user-agent
This commit is contained in:
@@ -16,8 +16,9 @@ feature -- Status
|
||||
deferred
|
||||
end
|
||||
|
||||
|
||||
note
|
||||
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
|
||||
copyright: "2011-2015, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
|
||||
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||
source: "[
|
||||
Eiffel Software
|
||||
|
||||
@@ -17,6 +17,7 @@ feature {NONE} -- Initialization
|
||||
make (a_url: READABLE_STRING_8; a_request_method: like request_method; a_session: like session; ctx: like context)
|
||||
-- Initialize `Current'.
|
||||
do
|
||||
current_redirects := 0
|
||||
request_method := a_request_method
|
||||
session := a_session
|
||||
url := a_url
|
||||
@@ -36,6 +37,8 @@ feature {NONE} -- Internal
|
||||
|
||||
context: detachable HTTP_CLIENT_REQUEST_CONTEXT
|
||||
|
||||
current_redirects: INTEGER_32
|
||||
|
||||
feature -- Status report
|
||||
|
||||
is_debug: BOOLEAN
|
||||
|
||||
@@ -24,6 +24,7 @@ create
|
||||
feature {NONE} -- Internal
|
||||
|
||||
session: NET_HTTP_CLIENT_SESSION
|
||||
net_http_client_version: STRING = "0.1"
|
||||
|
||||
feature -- Access
|
||||
|
||||
@@ -41,6 +42,10 @@ feature -- Access
|
||||
l_content_length: INTEGER
|
||||
l_location: detachable READABLE_STRING_8
|
||||
l_port: INTEGER
|
||||
l_authorization: HTTP_AUTHORIZATION
|
||||
l_session: NET_HTTP_CLIENT_SESSION
|
||||
l_platform: STRING
|
||||
l_useragent: STRING
|
||||
do
|
||||
create Result.make (url)
|
||||
|
||||
@@ -61,8 +66,14 @@ feature -- Access
|
||||
l_host := l_url.host
|
||||
end
|
||||
if attached l_uri.userinfo as l_userinfo then
|
||||
-- TODO: Add support for HTTP Authorization
|
||||
-- See {HTTP_AUTHORIZATION} from http_authorization EWF library.
|
||||
if attached l_uri.username as u_name then
|
||||
if attached l_uri.password as u_pass then
|
||||
create l_authorization.make_basic_auth (u_name, u_pass)
|
||||
if attached l_authorization.http_authorization as auth then
|
||||
headers.extend (auth, "Authorization")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
create l_request_uri.make_from_string (l_uri.path)
|
||||
@@ -71,6 +82,23 @@ feature -- Access
|
||||
l_request_uri.append (l_query)
|
||||
end
|
||||
|
||||
-- user agent header
|
||||
-- User-Agent: eiffelhttpclient/0.1 .. and so on
|
||||
-- Namen, Version und Kommentar
|
||||
if {PLATFORM}.is_unix then
|
||||
l_platform := "Unix"
|
||||
else
|
||||
if {PLATFORM}.is_mac then
|
||||
l_platform := "Mac"
|
||||
else
|
||||
l_platform := "Windows"
|
||||
end
|
||||
end
|
||||
if l_platform /= Void then
|
||||
l_useragent := "eiffelhttpclient/" + net_http_client_version + " (" + l_platform + ")"
|
||||
headers.extend (l_useragent, "User-Agent")
|
||||
end
|
||||
|
||||
-- Connect
|
||||
create socket.make_client_by_port (l_port, l_host)
|
||||
socket.set_timeout (timeout)
|
||||
@@ -87,6 +115,7 @@ feature -- Access
|
||||
s.append (Http_host_header)
|
||||
s.append (": ")
|
||||
s.append (l_host)
|
||||
s.append (http_end_of_header_line)
|
||||
if headers.is_empty then
|
||||
s.append (Http_end_of_command)
|
||||
else
|
||||
@@ -176,11 +205,15 @@ feature -- Access
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if l_location /= Void then
|
||||
-- TODO: add redirection support.
|
||||
-- See if it could be similar to libcurl implementation
|
||||
-- otherwise, maybe update the class HTTP_CLIENT_RESPONSE.
|
||||
if current_redirects < max_redirects then
|
||||
current_redirects := current_redirects + 1
|
||||
url := l_location
|
||||
Result := response
|
||||
end
|
||||
end
|
||||
current_redirects := 0
|
||||
else
|
||||
Result.set_error_message ("Could not connect")
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user