Allow forcing multipart/form-data or application/x-www-form-urlencoded to choose how the form data should be sent.

This commit is contained in:
2017-03-08 22:01:04 +01:00
parent 8bb9675719
commit 6f200bbf22
2 changed files with 37 additions and 11 deletions

View File

@@ -47,6 +47,7 @@ feature -- Execution
l_result: INTEGER l_result: INTEGER
l_curl_string: detachable CURL_STRING l_curl_string: detachable CURL_STRING
l_url: READABLE_STRING_8 l_url: READABLE_STRING_8
l_use_curl_form: BOOLEAN
l_form: detachable CURL_FORM l_form: detachable CURL_FORM
l_last: CURL_FORM l_last: CURL_FORM
l_upload_file: detachable RAW_FILE l_upload_file: detachable RAW_FILE
@@ -166,19 +167,24 @@ feature -- Execution
if l_upload_data = Void and l_upload_filename = Void then if l_upload_data = Void and l_upload_filename = Void then
-- Send as form-urlencoded -- Send as form-urlencoded
if if
l_headers.has_key ("Content-Type") and then attached l_headers.item ("Content-Type") as l_ct
attached l_headers.found_item as l_ct
then then
if l_ct.starts_with ("application/x-www-form-urlencoded") then if l_ct.starts_with ("application/x-www-form-urlencoded") then
-- Content-Type is already application/x-www-form-urlencoded -- Content-Type is already application/x-www-form-urlencoded
l_upload_data := ctx.form_parameters_to_url_encoded_string l_upload_data := ctx.form_parameters_to_url_encoded_string
elseif l_ct.starts_with ("multipart/form-data") then
l_use_curl_form := True
else else
-- Existing Content-Type and not application/x-www-form-urlencoded -- Not supported, use libcurl form.
l_use_curl_form := True
end end
else else
l_upload_data := ctx.form_parameters_to_url_encoded_string l_upload_data := ctx.form_parameters_to_url_encoded_string
end end
else else
l_use_curl_form := True
end
if l_use_curl_form then
create l_form.make create l_form.make
create l_last.make create l_last.make
from from
@@ -434,7 +440,7 @@ feature {NONE} -- Implementation
end end
note note
copyright: "2011-2015, Jocelyn Fiat, Javier Velilla, Eiffel Software and others" copyright: "2011-2017, 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

@@ -194,16 +194,36 @@ feature -- Access
if ctx.has_form_data then if ctx.has_form_data then
l_form_data := ctx.form_parameters l_form_data := ctx.form_parameters
if l_upload_data = Void and l_upload_filename = Void then if l_upload_data = Void and l_upload_filename = Void then
-- Send as form-urlencoded if
headers.extend ("application/x-www-form-urlencoded", "Content-Type") attached headers.item ("Content-Type") as l_ct
l_upload_data := ctx.form_parameters_to_url_encoded_string then
headers.force (l_upload_data.count.out, "Content-Length") if l_ct.starts_with ("application/x-www-form-urlencoded") then
l_upload_data := ctx.form_parameters_to_url_encoded_string
elseif l_ct.starts_with ("multipart/form-data") then
-- create form using multipart/form-data encoding
l_boundary := new_mime_boundary (l_form_data)
headers.extend ("multipart/form-data; boundary=" + l_boundary, "Content-Type")
l_upload_data := form_date_and_uploaded_files_to_mime_string (l_form_data, l_upload_filename, l_boundary)
else
-- not supported !
-- Send as form-urlencoded
headers.extend ("application/x-www-form-urlencoded", "Content-Type")
l_upload_data := ctx.form_parameters_to_url_encoded_string
end
else
-- Send as form-urlencoded
headers.extend ("application/x-www-form-urlencoded", "Content-Type")
l_upload_data := ctx.form_parameters_to_url_encoded_string
end
headers.extend (l_upload_data.count.out, "Content-Length")
if l_is_chunked_transfer_encoding then if l_is_chunked_transfer_encoding then
-- Discard chunked transfer encoding -- Discard chunked transfer encoding
headers.remove ("Transfer-Encoding") headers.remove ("Transfer-Encoding")
l_is_chunked_transfer_encoding := False l_is_chunked_transfer_encoding := False
end end
elseif l_form_data /= Void then elseif l_form_data /= Void then
check l_upload_data = Void end
-- create form using multipart/form-data encoding -- create form using multipart/form-data encoding
l_boundary := new_mime_boundary (l_form_data) l_boundary := new_mime_boundary (l_form_data)
headers.extend ("multipart/form-data; boundary=" + l_boundary, "Content-Type") headers.extend ("multipart/form-data; boundary=" + l_boundary, "Content-Type")
@@ -400,9 +420,9 @@ feature -- Access
end end
-- follow redirect -- follow redirect
if if
is_redirection_http_status (Result.status) and is_redirection_http_status (Result.status) and
l_location /= Void l_location /= Void
then then
if Result.redirections_count < max_redirects then if Result.redirections_count < max_redirects then
initialize (l_location, ctx) initialize (l_location, ctx)
@@ -906,7 +926,7 @@ feature {NONE} -- Helpers
invariant invariant
note note
copyright: "2011-2016, Jocelyn Fiat, Javier Velilla, Eiffel Software and others" copyright: "2011-2017, 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