From e5eb11b4e7a436112a0b6b6bef87652b2fe0cb71 Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Thu, 6 Oct 2011 19:09:17 +0200 Subject: [PATCH] added support for data in POST request --- .../src/http_client_request_context.e | 28 +++++++++++++++++++ .../http_client/src/http_client_session.e | 4 +-- .../libcurl/libcurl_http_client_request.e | 16 +++++++++++ .../libcurl/libcurl_http_client_session.e | 20 +++++++++++-- 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/library/client/http_client/src/http_client_request_context.e b/library/client/http_client/src/http_client_request_context.e index 77a3cdcb..cdbb3498 100644 --- a/library/client/http_client/src/http_client_request_context.e +++ b/library/client/http_client/src/http_client_request_context.e @@ -38,6 +38,10 @@ feature -- Access form_data_parameters: HASH_TABLE [READABLE_STRING_32, READABLE_STRING_8] + upload_data: detachable READABLE_STRING_8 + + upload_filename: detachable READABLE_STRING_8 + feature -- Status report has_form_data: BOOLEAN @@ -45,6 +49,16 @@ feature -- Status report Result := not form_data_parameters.is_empty end + has_upload_data: BOOLEAN + do + Result := attached upload_data as d and then not d.is_empty + end + + has_upload_filename: BOOLEAN + do + Result := attached upload_filename as fn and then not fn.is_empty + end + feature -- Element change add_query_parameter (k: READABLE_STRING_8; v: READABLE_STRING_32) @@ -62,4 +76,18 @@ feature -- Element change credentials_required := b end + set_upload_data (a_data: like upload_data) + require + has_no_upload_data: not has_upload_data + do + upload_data := a_data + end + + set_upload_filename (a_fn: like upload_filename) + require + has_no_upload_filename: not has_upload_filename + do + upload_filename := a_fn + end + end diff --git a/library/client/http_client/src/http_client_session.e b/library/client/http_client/src/http_client_session.e index 37cf2e17..c9d77705 100644 --- a/library/client/http_client/src/http_client_session.e +++ b/library/client/http_client/src/http_client_session.e @@ -41,11 +41,11 @@ feature -- Basic operation deferred end - post (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT): HTTP_CLIENT_RESPONSE + post (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT; data: detachable READABLE_STRING_8): HTTP_CLIENT_RESPONSE deferred end - put (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT): HTTP_CLIENT_RESPONSE + put (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT; fn: detachable READABLE_STRING_8): HTTP_CLIENT_RESPONSE deferred end diff --git a/library/client/http_client/src/spec/libcurl/libcurl_http_client_request.e b/library/client/http_client/src/spec/libcurl/libcurl_http_client_request.e index 5efa63c6..b99d1912 100644 --- a/library/client/http_client/src/spec/libcurl/libcurl_http_client_request.e +++ b/library/client/http_client/src/spec/libcurl/libcurl_http_client_request.e @@ -142,6 +142,22 @@ feature -- Execution curl_easy.setopt_form (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_httppost, l_form) end end + if ctx /= Void then + if + request_method.is_case_insensitive_equal ("POST") and then + ctx.has_upload_data and then attached ctx.upload_data as l_upload_data + then + curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_postfields, l_upload_data) + curl_easy.setopt_integer (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_postfieldsize, l_upload_data.count) + elseif + request_method.is_case_insensitive_equal ("PUT") and then + ctx.has_upload_filename and then attached ctx.upload_filename as l_upload_filename + then +-- curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_postfields, l_upload_data) +-- curl_easy.setopt_integer (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_postfieldsize, l_upload_data.count) +--| Not Yet Implemented + end + end curl.global_init diff --git a/library/client/http_client/src/spec/libcurl/libcurl_http_client_session.e b/library/client/http_client/src/spec/libcurl/libcurl_http_client_session.e index 740f4e4e..a1131e2e 100644 --- a/library/client/http_client/src/spec/libcurl/libcurl_http_client_session.e +++ b/library/client/http_client/src/spec/libcurl/libcurl_http_client_session.e @@ -39,19 +39,35 @@ feature -- Basic operation Result := execute_request (req, ctx) end - post (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT): HTTP_CLIENT_RESPONSE + post (a_path: READABLE_STRING_8; a_ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT; a_data: detachable READABLE_STRING_8): HTTP_CLIENT_RESPONSE local req: HTTP_CLIENT_REQUEST + ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT do create {LIBCURL_HTTP_CLIENT_REQUEST} req.make (base_url + a_path, "POST", Current) + ctx := a_ctx + if a_data /= Void then + if ctx = Void then + create ctx.make + end + ctx.set_upload_data (a_data) + end Result := execute_request (req, ctx) end - put (a_path: READABLE_STRING_8; ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT): HTTP_CLIENT_RESPONSE + put (a_path: READABLE_STRING_8; a_ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT; fn: detachable READABLE_STRING_8): HTTP_CLIENT_RESPONSE local req: HTTP_CLIENT_REQUEST + ctx: detachable HTTP_CLIENT_REQUEST_CONTEXT do create {LIBCURL_HTTP_CLIENT_REQUEST} req.make (base_url + a_path, "PUT", Current) + ctx := a_ctx + if fn /= Void then + if ctx = Void then + create ctx.make + end + ctx.set_upload_filename (fn) + end Result := execute_request (req, ctx) end