implemented http authorization, support for redirection and user-agent

This commit is contained in:
Florian Jacky
2015-06-24 22:30:37 +02:00
committed by Jocelyn Fiat
parent 3f69081d32
commit 770488dbd3
3 changed files with 43 additions and 6 deletions

View File

@@ -16,8 +16,9 @@ feature -- Status
deferred deferred
end end
note 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)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[ source: "[
Eiffel Software Eiffel Software

View File

@@ -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) make (a_url: READABLE_STRING_8; a_request_method: like request_method; a_session: like session; ctx: like context)
-- Initialize `Current'. -- Initialize `Current'.
do do
current_redirects := 0
request_method := a_request_method request_method := a_request_method
session := a_session session := a_session
url := a_url url := a_url
@@ -36,6 +37,8 @@ feature {NONE} -- Internal
context: detachable HTTP_CLIENT_REQUEST_CONTEXT context: detachable HTTP_CLIENT_REQUEST_CONTEXT
current_redirects: INTEGER_32
feature -- Status report feature -- Status report
is_debug: BOOLEAN is_debug: BOOLEAN

View File

@@ -24,6 +24,7 @@ create
feature {NONE} -- Internal feature {NONE} -- Internal
session: NET_HTTP_CLIENT_SESSION session: NET_HTTP_CLIENT_SESSION
net_http_client_version: STRING = "0.1"
feature -- Access feature -- Access
@@ -41,6 +42,10 @@ feature -- Access
l_content_length: INTEGER l_content_length: INTEGER
l_location: detachable READABLE_STRING_8 l_location: detachable READABLE_STRING_8
l_port: INTEGER l_port: INTEGER
l_authorization: HTTP_AUTHORIZATION
l_session: NET_HTTP_CLIENT_SESSION
l_platform: STRING
l_useragent: STRING
do do
create Result.make (url) create Result.make (url)
@@ -61,8 +66,14 @@ feature -- Access
l_host := l_url.host l_host := l_url.host
end end
if attached l_uri.userinfo as l_userinfo then if attached l_uri.userinfo as l_userinfo then
-- TODO: Add support for HTTP Authorization if attached l_uri.username as u_name then
-- See {HTTP_AUTHORIZATION} from http_authorization EWF library. 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 end
create l_request_uri.make_from_string (l_uri.path) create l_request_uri.make_from_string (l_uri.path)
@@ -71,6 +82,23 @@ feature -- Access
l_request_uri.append (l_query) l_request_uri.append (l_query)
end 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 -- Connect
create socket.make_client_by_port (l_port, l_host) create socket.make_client_by_port (l_port, l_host)
socket.set_timeout (timeout) socket.set_timeout (timeout)
@@ -87,6 +115,7 @@ feature -- Access
s.append (Http_host_header) s.append (Http_host_header)
s.append (": ") s.append (": ")
s.append (l_host) s.append (l_host)
s.append (http_end_of_header_line)
if headers.is_empty then if headers.is_empty then
s.append (Http_end_of_command) s.append (Http_end_of_command)
else else
@@ -176,11 +205,15 @@ feature -- Access
end end
end end
end end
if l_location /= Void then if l_location /= Void then
-- TODO: add redirection support. if current_redirects < max_redirects then
-- See if it could be similar to libcurl implementation current_redirects := current_redirects + 1
-- otherwise, maybe update the class HTTP_CLIENT_RESPONSE. url := l_location
Result := response
end
end end
current_redirects := 0
else else
Result.set_error_message ("Could not connect") Result.set_error_message ("Could not connect")
end end