now supports sending requests, receiving headers, receiving message text, redirection, agent header, cookies, basic http authorization, sending data using post using url-encoding, sending file as post as data, sending put data
This commit is contained in:
@@ -185,6 +185,9 @@ feature -- Access
|
|||||||
headers: HASH_TABLE [READABLE_STRING_8, READABLE_STRING_8]
|
headers: HASH_TABLE [READABLE_STRING_8, READABLE_STRING_8]
|
||||||
-- Headers common to any request created by Current session.
|
-- Headers common to any request created by Current session.
|
||||||
|
|
||||||
|
cookie: detachable READABLE_STRING_8
|
||||||
|
-- Cookie for the current base_url
|
||||||
|
|
||||||
feature -- Authentication
|
feature -- Authentication
|
||||||
|
|
||||||
auth_type: STRING
|
auth_type: STRING
|
||||||
@@ -212,6 +215,12 @@ feature -- Element change
|
|||||||
set_base_url (u: like base_url)
|
set_base_url (u: like base_url)
|
||||||
do
|
do
|
||||||
base_url := u
|
base_url := u
|
||||||
|
cookie := Void
|
||||||
|
end
|
||||||
|
|
||||||
|
set_cookie (c: READABLE_STRING_8)
|
||||||
|
do
|
||||||
|
cookie := c
|
||||||
end
|
end
|
||||||
|
|
||||||
set_timeout (n_seconds: like timeout)
|
set_timeout (n_seconds: like timeout)
|
||||||
@@ -300,7 +309,7 @@ feature -- Element change
|
|||||||
end
|
end
|
||||||
|
|
||||||
note
|
note
|
||||||
copyright: "2011-2013, 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
|
||||||
|
|||||||
@@ -46,7 +46,13 @@ feature -- Access
|
|||||||
l_session: NET_HTTP_CLIENT_SESSION
|
l_session: NET_HTTP_CLIENT_SESSION
|
||||||
l_platform: STRING
|
l_platform: STRING
|
||||||
l_useragent: STRING
|
l_useragent: STRING
|
||||||
|
l_upload_data: detachable READABLE_STRING_8
|
||||||
|
ctx: like context
|
||||||
|
l_has_content: BOOLEAN
|
||||||
|
l_upload_file: detachable RAW_FILE
|
||||||
|
l_upload_filename: detachable READABLE_STRING_GENERAL
|
||||||
do
|
do
|
||||||
|
ctx := context
|
||||||
create Result.make (url)
|
create Result.make (url)
|
||||||
|
|
||||||
-- Get URL data
|
-- Get URL data
|
||||||
@@ -59,12 +65,15 @@ feature -- Access
|
|||||||
l_port := 80
|
l_port := 80
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if attached l_uri.host as h then
|
if attached l_uri.host as h then
|
||||||
l_host := h
|
l_host := h
|
||||||
else
|
else
|
||||||
create l_url.make (url)
|
create l_url.make (url)
|
||||||
l_host := l_url.host
|
l_host := l_url.host
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- add headers for authorization
|
||||||
if attached l_uri.userinfo as l_userinfo then
|
if attached l_uri.userinfo as l_userinfo then
|
||||||
if attached l_uri.username as u_name then
|
if attached l_uri.username as u_name then
|
||||||
if attached l_uri.password as u_pass then
|
if attached l_uri.password as u_pass then
|
||||||
@@ -82,6 +91,7 @@ feature -- Access
|
|||||||
l_request_uri.append (l_query)
|
l_request_uri.append (l_query)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- add headers for user agent
|
||||||
if {PLATFORM}.is_unix then
|
if {PLATFORM}.is_unix then
|
||||||
l_platform := "Unix"
|
l_platform := "Unix"
|
||||||
else
|
else
|
||||||
@@ -96,6 +106,56 @@ feature -- Access
|
|||||||
headers.extend (l_useragent, "User-Agent")
|
headers.extend (l_useragent, "User-Agent")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- handle sending data
|
||||||
|
if attached ctx then
|
||||||
|
if ctx.has_upload_filename then
|
||||||
|
l_upload_filename := ctx.upload_filename
|
||||||
|
end
|
||||||
|
|
||||||
|
if ctx.has_upload_data then
|
||||||
|
l_upload_data := ctx.upload_data
|
||||||
|
end
|
||||||
|
|
||||||
|
-- handle post requests
|
||||||
|
if request_method.is_case_insensitive_equal ("POST") then
|
||||||
|
if ctx /= Void then
|
||||||
|
if ctx.has_upload_data then
|
||||||
|
l_upload_data := ctx.upload_data
|
||||||
|
if l_upload_data /= Void then
|
||||||
|
headers.extend ("application/x-www-form-urlencoded", "Content-Type")
|
||||||
|
headers.extend (l_upload_data.count.out, "Content-Length")
|
||||||
|
end
|
||||||
|
elseif ctx.has_upload_filename then
|
||||||
|
if l_upload_filename /= Void then
|
||||||
|
create l_upload_file.make_with_name (l_upload_filename)
|
||||||
|
if l_upload_file.exists and then l_upload_file.is_readable then
|
||||||
|
headers.extend (l_upload_file.count.out, "Content-Length")
|
||||||
|
l_upload_file.open_read
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- handle put requests
|
||||||
|
if request_method.is_case_insensitive_equal ("PUT") then
|
||||||
|
if ctx /= Void then
|
||||||
|
if ctx.has_upload_filename then
|
||||||
|
if l_upload_filename /= Void then
|
||||||
|
create l_upload_file.make_with_name (l_upload_filename)
|
||||||
|
if l_upload_file.exists and then l_upload_file.is_readable then
|
||||||
|
headers.extend (l_upload_file.count.out, "Content-Length")
|
||||||
|
l_upload_file.open_read
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
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)
|
||||||
@@ -113,6 +173,9 @@ feature -- Access
|
|||||||
s.append (": ")
|
s.append (": ")
|
||||||
s.append (l_host)
|
s.append (l_host)
|
||||||
s.append (http_end_of_header_line)
|
s.append (http_end_of_header_line)
|
||||||
|
if attached session.cookie as cookie then
|
||||||
|
s.append ("Cookie: " + cookie + http_end_of_header_line)
|
||||||
|
end
|
||||||
if headers.is_empty then
|
if headers.is_empty then
|
||||||
s.append (Http_end_of_command)
|
s.append (Http_end_of_command)
|
||||||
else
|
else
|
||||||
@@ -127,6 +190,22 @@ feature -- Access
|
|||||||
s.append (Http_end_of_header_line)
|
s.append (Http_end_of_header_line)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if l_upload_data /= Void then
|
||||||
|
s.append (l_upload_data)
|
||||||
|
s.append (http_end_of_header_line)
|
||||||
|
end
|
||||||
|
if attached l_upload_file then
|
||||||
|
if not l_upload_file.after then
|
||||||
|
from
|
||||||
|
until
|
||||||
|
l_upload_file.after
|
||||||
|
loop
|
||||||
|
l_upload_file.read_line
|
||||||
|
s.append (l_upload_file.last_string)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
socket.put_string (s)
|
socket.put_string (s)
|
||||||
|
|
||||||
-- Get header message
|
-- Get header message
|
||||||
@@ -162,6 +241,12 @@ feature -- Access
|
|||||||
s.left_adjust -- Remove startung spaces
|
s.left_adjust -- Remove startung spaces
|
||||||
s.right_adjust -- Remove trailing %R
|
s.right_adjust -- Remove trailing %R
|
||||||
l_location := s
|
l_location := s
|
||||||
|
elseif s.starts_with_general ("Set-Cookie:") then
|
||||||
|
i := s.index_of (':', 1)
|
||||||
|
s.remove_head (i)
|
||||||
|
s.left_adjust
|
||||||
|
s.right_adjust
|
||||||
|
session.set_cookie (s)
|
||||||
end
|
end
|
||||||
-- Next iteration
|
-- Next iteration
|
||||||
socket.read_line
|
socket.read_line
|
||||||
@@ -180,6 +265,7 @@ feature -- Access
|
|||||||
end
|
end
|
||||||
l_message.append (socket.last_string)
|
l_message.append (socket.last_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
Result.set_response_message (l_message, context)
|
Result.set_response_message (l_message, context)
|
||||||
-- Get status code.
|
-- Get status code.
|
||||||
if attached Result.status_line as l_status_line then
|
if attached Result.status_line as l_status_line then
|
||||||
@@ -203,6 +289,7 @@ feature -- Access
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- follow redirect
|
||||||
if l_location /= Void then
|
if l_location /= Void then
|
||||||
if current_redirects < max_redirects then
|
if current_redirects < max_redirects then
|
||||||
current_redirects := current_redirects + 1
|
current_redirects := current_redirects + 1
|
||||||
@@ -210,6 +297,7 @@ feature -- Access
|
|||||||
Result := response
|
Result := response
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
current_redirects := 0
|
current_redirects := 0
|
||||||
else
|
else
|
||||||
Result.set_error_message ("Could not connect")
|
Result.set_error_message ("Could not connect")
|
||||||
|
|||||||
@@ -136,9 +136,10 @@ feature {NONE} -- Implementation
|
|||||||
create {NET_HTTP_CLIENT_REQUEST} req.make (base_url + a_path, a_method, Current, ctx)
|
create {NET_HTTP_CLIENT_REQUEST} req.make (base_url + a_path, a_method, Current, ctx)
|
||||||
Result := req.response
|
Result := req.response
|
||||||
|
|
||||||
if f /= Void then
|
-- FIXME file should be deleted, but uncommenting the following leads to a PERMISSION DENIED exception..
|
||||||
f.delete
|
--if f /= Void then
|
||||||
end
|
-- f.delete
|
||||||
|
--end
|
||||||
if l_data /= Void and a_ctx /= Void then
|
if l_data /= Void and a_ctx /= Void then
|
||||||
a_ctx.set_upload_filename (Void)
|
a_ctx.set_upload_filename (Void)
|
||||||
a_ctx.set_upload_data (l_data)
|
a_ctx.set_upload_data (l_data)
|
||||||
|
|||||||
Reference in New Issue
Block a user