aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/http
diff options
context:
space:
mode:
authorMatus Fabian <matfabia@cisco.com>2024-07-31 16:08:40 +0200
committerFlorin Coras <florin.coras@gmail.com>2024-08-06 16:01:02 +0000
commitd46e674abc5605b58584bbf9a0607ef621ca0eee (patch)
tree4f42d19311c0036726d72d5d17af97082a05bed0 /src/plugins/http
parentfc3464dac99c9e3e56616e6c6bc6d10886b2f567 (diff)
http: client POST method
Type: improvement Change-Id: Iaa70abcee02866f9a6426a6e8e4709eeba0e8114 Signed-off-by: Matus Fabian <matfabia@cisco.com>
Diffstat (limited to 'src/plugins/http')
-rw-r--r--src/plugins/http/http.c164
-rw-r--r--src/plugins/http/http.h2
-rw-r--r--src/plugins/http/http_buffer.c2
-rw-r--r--src/plugins/http/http_plugin.rst34
4 files changed, 152 insertions, 50 deletions
diff --git a/src/plugins/http/http.c b/src/plugins/http/http.c
index 017fd50ebae..c504b0c5028 100644
--- a/src/plugins/http/http.c
+++ b/src/plugins/http/http.c
@@ -385,10 +385,16 @@ static const char *http_response_template = "HTTP/1.1 %s\r\n"
/**
* http request boilerplate
*/
-static const char *http_request_template = "GET %s HTTP/1.1\r\n"
- "Host: %v\r\n"
- "User-Agent: %v\r\n"
- "%s";
+static const char *http_get_request_template = "GET %s HTTP/1.1\r\n"
+ "Host: %v\r\n"
+ "User-Agent: %v\r\n"
+ "%s";
+
+static const char *http_post_request_template = "POST %s HTTP/1.1\r\n"
+ "Host: %v\r\n"
+ "User-Agent: %v\r\n"
+ "Content-Length: %u\r\n"
+ "%s";
static u32
http_send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
@@ -1134,9 +1140,11 @@ http_state_wait_app_method (http_conn_t *hc, transport_send_params_t *sp)
{
http_msg_t msg;
session_t *as;
- u8 *target = 0, *request;
+ u8 *target_buff = 0, *request = 0, *target;
u32 offset;
int rv;
+ http_sm_result_t sm_result = HTTP_SM_ERROR;
+ http_state_t next_state;
as = session_get_from_handle (hc->h_pa_session_handle);
@@ -1155,47 +1163,107 @@ http_state_wait_app_method (http_conn_t *hc, transport_send_params_t *sp)
goto error;
}
- /* currently we support only GET method */
- if (msg.method_type != HTTP_REQ_GET)
+ /* read request target */
+ if (msg.data.type == HTTP_MSG_DATA_PTR)
{
- clib_warning ("unsupported method %d", msg.method_type);
- goto error;
+ uword target_ptr;
+ rv = svm_fifo_dequeue (as->tx_fifo, sizeof (target_ptr),
+ (u8 *) &target_ptr);
+ ASSERT (rv == sizeof (target_ptr));
+ target = uword_to_pointer (target_ptr, u8 *);
}
- if (msg.data.body_len != 0)
+ else
{
- clib_warning ("GET request shouldn't include data");
- goto error;
+ vec_validate (target_buff, msg.data.target_path_len - 1);
+ rv =
+ svm_fifo_dequeue (as->tx_fifo, msg.data.target_path_len, target_buff);
+ ASSERT (rv == msg.data.target_path_len);
+ target = target_buff;
}
- /* read request target */
- vec_validate (target, msg.data.target_path_len - 1);
- rv = svm_fifo_dequeue (as->tx_fifo, msg.data.target_path_len, target);
- ASSERT (rv == msg.data.target_path_len);
-
- /*
- * Add "protocol layer" headers:
- * - host
- * - user agent
- */
- request = format (0, http_request_template,
- /* target */
- target,
- /* Host */
- hc->host,
- /* User-Agent*/
- hc->app_name,
- /* Any headers from app? */
- msg.data.headers_len ? "" : "\r\n");
+ /* currently we support only GET and POST method */
+ if (msg.method_type == HTTP_REQ_GET)
+ {
+ if (msg.data.body_len)
+ {
+ clib_warning ("GET request shouldn't include data");
+ goto error;
+ }
+ /*
+ * Add "protocol layer" headers:
+ * - host
+ * - user agent
+ */
+ request = format (0, http_get_request_template,
+ /* target */
+ target,
+ /* Host */
+ hc->host,
+ /* User-Agent */
+ hc->app_name,
+ /* Any headers from app? */
+ msg.data.headers_len ? "" : "\r\n");
+
+ next_state = HTTP_STATE_WAIT_SERVER_REPLY;
+ sm_result = HTTP_SM_STOP;
+ }
+ else if (msg.method_type == HTTP_REQ_POST)
+ {
+ if (!msg.data.body_len)
+ {
+ clib_warning ("POST request should include data");
+ goto error;
+ }
+ /*
+ * Add "protocol layer" headers:
+ * - host
+ * - user agent
+ * - content length
+ */
+ request = format (0, http_post_request_template,
+ /* target */
+ target,
+ /* Host */
+ hc->host,
+ /* User-Agent */
+ hc->app_name,
+ /* Content-Length */
+ msg.data.body_len,
+ /* Any headers from app? */
+ msg.data.headers_len ? "" : "\r\n");
+
+ http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type],
+ as->tx_fifo, msg.data.body_len);
+
+ next_state = HTTP_STATE_APP_IO_MORE_DATA;
+ sm_result = HTTP_SM_CONTINUE;
+ }
+ else
+ {
+ clib_warning ("unsupported method %d", msg.method_type);
+ goto error;
+ }
/* Add headers from app (if any) */
if (msg.data.headers_len)
{
- HTTP_DBG (0, "get headers from app, len %d", msg.data.headers_len);
- u32 orig_len = vec_len (request);
- vec_resize (request, msg.data.headers_len);
- u8 *p = request + orig_len;
- rv = svm_fifo_dequeue (as->tx_fifo, msg.data.headers_len, p);
- ASSERT (rv == msg.data.headers_len);
+ HTTP_DBG (0, "got headers from app, len %d", msg.data.headers_len);
+ if (msg.data.type == HTTP_MSG_DATA_PTR)
+ {
+ uword app_headers_ptr;
+ rv = svm_fifo_dequeue (as->tx_fifo, sizeof (app_headers_ptr),
+ (u8 *) &app_headers_ptr);
+ ASSERT (rv == sizeof (app_headers_ptr));
+ vec_append (request, uword_to_pointer (app_headers_ptr, u8 *));
+ }
+ else
+ {
+ u32 orig_len = vec_len (request);
+ vec_resize (request, msg.data.headers_len);
+ u8 *p = request + orig_len;
+ rv = svm_fifo_dequeue (as->tx_fifo, msg.data.headers_len, p);
+ ASSERT (rv == msg.data.headers_len);
+ }
}
HTTP_DBG (0, "%v", request);
@@ -1203,22 +1271,23 @@ http_state_wait_app_method (http_conn_t *hc, transport_send_params_t *sp)
if (offset != vec_len (request))
{
clib_warning ("sending request failed!");
+ sm_result = HTTP_SM_ERROR;
goto error;
}
- http_state_change (hc, HTTP_STATE_WAIT_SERVER_REPLY);
-
- vec_free (target);
- vec_free (request);
-
- return HTTP_SM_STOP;
+ http_state_change (hc, next_state);
+ goto done;
error:
svm_fifo_dequeue_drop_all (as->tx_fifo);
session_transport_closing_notify (&hc->connection);
session_transport_closed_notify (&hc->connection);
http_disconnect_transport (hc);
- return HTTP_SM_ERROR;
+
+done:
+ vec_free (target_buff);
+ vec_free (request);
+ return sm_result;
}
static http_sm_result_t
@@ -1334,8 +1403,11 @@ http_state_app_io_more_data (http_conn_t *hc, transport_send_params_t *sp)
if (sent && svm_fifo_set_event (ts->tx_fifo))
session_program_tx_io_evt (ts->handle, SESSION_IO_EVT_TX_FLUSH);
- /* Finished transaction, back to HTTP_STATE_WAIT_METHOD */
- http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
+ /* Finished transaction:
+ * server back to HTTP_STATE_WAIT_METHOD
+ * client to HTTP_STATE_WAIT_SERVER_REPLY */
+ http_state_change (hc, hc->is_server ? HTTP_STATE_WAIT_CLIENT_METHOD :
+ HTTP_STATE_WAIT_SERVER_REPLY);
http_buffer_free (&hc->tx_buf);
}
diff --git a/src/plugins/http/http.h b/src/plugins/http/http.h
index e3267dfc3b2..63d05860099 100644
--- a/src/plugins/http/http.h
+++ b/src/plugins/http/http.h
@@ -132,6 +132,8 @@ typedef enum http_target_form_
_ (APP_XSLX, ".xlsx", \
"application / vnd.openxmlformats - officedocument.spreadsheetml.sheet") \
_ (APP_XUL, ".xul", "application / vnd.mozilla.xul + xml") \
+ _ (APP_X_WWW_FORM_URLENCODED, ".invalid", \
+ "application / x-www-form-urlencoded") \
_ (APP_ZIP, ".zip", "application / zip") \
_ (AUDIO_AAC, ".aac", "audio / aac") \
_ (AUDIO_CD, ".cda", "application / x - cdf") \
diff --git a/src/plugins/http/http_buffer.c b/src/plugins/http/http_buffer.c
index f3dc308dbf8..bc1b8c08630 100644
--- a/src/plugins/http/http_buffer.c
+++ b/src/plugins/http/http_buffer.c
@@ -173,7 +173,7 @@ buf_ptr_drain (http_buffer_t *hb, u32 len)
bf->segs[1].data += len;
bf->segs[0].len -= len;
- HTTP_DBG (1, "drained %u left %u", len, bf->segs[1].len);
+ HTTP_DBG (1, "drained %u left %u", len, bf->segs[0].len);
if (!bf->segs[0].len)
{
diff --git a/src/plugins/http/http_plugin.rst b/src/plugins/http/http_plugin.rst
index 273812735f7..4daef79b9cf 100644
--- a/src/plugins/http/http_plugin.rst
+++ b/src/plugins/http/http_plugin.rst
@@ -9,14 +9,14 @@ Overview
--------
This plugin adds the HTTP protocol to VPP's Host Stack.
-As a result parsing of HTTP/1 request or response is available for internal VPP applications.
+As a result parsing and serializing of HTTP/1 requests or responses are available for internal VPP applications.
Usage
-----
The plugin exposes following inline functions: ``http_validate_abs_path_syntax``, ``http_validate_query_syntax``,
``http_percent_decode``, ``http_path_remove_dot_segments``, ``http_parse_headers``, ``http_get_header``,
-``http_free_header_table``.
+``http_free_header_table``, ``http_add_header``, ``http_serialize_headers``.
It relies on the hoststack constructs and uses ``http_msg_data_t`` data structure for passing metadata to/from applications.
@@ -238,12 +238,18 @@ Finally application sends response body:
if (svm_fifo_set_event (ts->tx_fifo))
session_program_tx_io_evt (ts->handle, SESSION_IO_EVT_TX);
-Example above shows how to send body data by copy, alternatively you could pass it as pointer:
+Examples above shows how to send body and headers by copy, alternatively you could pass them as pointer:
.. code-block:: C
msg.data.type = HTTP_MSG_DATA_PTR;
/* code omitted for brevity */
+ if (msg.data.headers_len)
+ {
+ uword headers = pointer_to_uword (headers_buf);
+ rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (headers), (u8 *) &headers);
+ ASSERT (rv == sizeof (headers));
+ }
uword data = pointer_to_uword (tx_buf);
rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (data), (u8 *) &data);
ASSERT (rv == sizeof (data));
@@ -332,6 +338,28 @@ Finally application sends everything to HTTP layer:
if (svm_fifo_set_event (as->tx_fifo))
session_program_tx_io_evt (as->handle, SESSION_IO_EVT_TX);
+Examples above shows how to send buffers by copy, alternatively you could pass them as pointer:
+
+.. code-block:: C
+
+ msg.data.type = HTTP_MSG_DATA_PTR;
+ msg.method_type = HTTP_REQ_POST;
+ msg.data.body_len = vec_len (data);
+ /* code omitted for brevity */
+ uword target = pointer_to_uword (target);
+ uword headers = pointer_to_uword (headers_buf);
+ uword body = pointer_to_uword (data);
+ svm_fifo_seg_t segs[4] = {
+ { (u8 *) &msg, sizeof (msg) },
+ { (u8 *) &target, sizeof (target) },
+ { (u8 *) &headers, sizeof (headers) },
+ { (u8 *) &body, sizeof (body) },
+ };
+ rv = svm_fifo_enqueue_segments (s->tx_fifo, segs, 4, 0 /* allow partial */);
+ ASSERT (rv == (sizeof (msg) + sizeof (target) + sizeof (headers) + sizeof (body)));
+
+In this case you need to free data when you receive response or when session is closed.
+
Receiving data
""""""""""""""