diff options
Diffstat (limited to 'src/plugins/http/http_plugin.rst')
-rw-r--r-- | src/plugins/http/http_plugin.rst | 79 |
1 files changed, 56 insertions, 23 deletions
diff --git a/src/plugins/http/http_plugin.rst b/src/plugins/http/http_plugin.rst index f86c796bd83..bf414cf96ef 100644 --- a/src/plugins/http/http_plugin.rst +++ b/src/plugins/http/http_plugin.rst @@ -15,9 +15,11 @@ 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_add_header``, ``http_serialize_headers``, ``http_parse_authority_form_target``, -``http_serialize_authority_form_target``, ``http_parse_absolute_form``, ``http_parse_masque_host_port``. +``http_percent_decode``, ``http_path_remove_dot_segments``, ``http_build_header_table``, ``http_get_header``, +``http_reset_header_table``, ``http_free_header_table``, ``http_add_header``, +``http_serialize_headers``, ``http_parse_authority_form_target``, ``http_serialize_authority_form_target``, +``http_parse_absolute_form``, ``http_parse_masque_host_port``, ``http_decap_udp_payload_datagram``, +``http_encap_udp_payload_datagram``. ``http_token_is``, ``http_token_is_case``, ``http_token_contains`` It relies on the hoststack constructs and uses ``http_msg_data_t`` data structure for passing metadata to/from applications. @@ -125,24 +127,60 @@ Following example shows how to parse headers: #include <http/http_header_names.h> if (msg.data.headers_len) { - u8 *headers = 0; - http_header_table_t *ht; - vec_validate (headers, msg.data.headers_len - 1); + http_header_table_t ht = HTTP_HEADER_TABLE_NULL; + http_init_header_table_buf (&ht, msg); rv = svm_fifo_peek (ts->rx_fifo, msg.data.headers_offset, - msg.data.headers_len, headers); + msg.data.headers_len, ht.buf); ASSERT (rv == msg.data.headers_len); - if (http_parse_headers (headers, &ht)) + http_build_header_table (&ht, msg); + /* get Accept header */ + const http_header_t *accept = http_get_header (&ht, http_header_name_token (HTTP_HEADER_ACCEPT)); + if (accept_value) { - /* your error handling */ + /* do something interesting */ } + http_free_header_table (&ht); + } + +Allocated header table memory can be reused, you just need to reset it using ``http_reset_header_table`` before reuse. +We will add following member to our session context structure: + +.. code-block:: C + + typedef struct + { + /* ... */ + http_header_table_t ht; + } session_ctx_t; + +Don't forget to zero allocated session context. + +And in ``session_cleanup_callback`` we free header table memory: + +.. code-block:: C + + http_free_header_table (&ctx->ht); + +Modified example above: + +.. code-block:: C + + #include <http/http_header_names.h> + http_reset_header_table (&ctx->ht); + /* ... */ + if (msg.data.headers_len) + { + http_init_header_table_buf (&ctx->ht, msg); + rv = svm_fifo_peek (ts->rx_fifo, msg.data.headers_offset, + msg.data.headers_len, ctx->ht.buf); + ASSERT (rv == msg.data.headers_len); + http_build_header_table (&ctx->ht, msg); /* get Accept header */ - const char *accept_value = http_get_header (ht, http_header_name_str (HTTP_HEADER_ACCEPT)); + const http_header_t *accept = http_get_header (&ctx->ht, http_header_name_token (HTTP_HEADER_ACCEPT)); if (accept_value) { /* do something interesting */ } - http_free_header_table (ht); - vec_free (headers); } Finally application reads body (if any), which might be received in multiple pieces (depends on size), so we might need some state machine in ``builtin_app_rx_callback``. @@ -437,24 +475,19 @@ Following example shows how to parse headers: #include <http/http_header_names.h> if (msg.data.headers_len) { - u8 *headers = 0; - http_header_table_t *ht; - vec_validate (headers, msg.data.headers_len - 1); + http_header_table_t ht = HTTP_HEADER_TABLE_NULL; + http_init_header_table_buf (&ht, msg); rv = svm_fifo_peek (ts->rx_fifo, msg.data.headers_offset, - msg.data.headers_len, headers); + msg.data.headers_len, ht.buf); ASSERT (rv == msg.data.headers_len); - if (http_parse_headers (headers, &ht)) - { - /* your error handling */ - } + http_build_header_table (&ht, msg); /* get Content-Type header */ - const char *content_type = http_get_header (ht, http_header_name_str (HTTP_HEADER_CONTENT_TYPE)); + const http_header_t *content_type = http_get_header (&ht, http_header_name_token (HTTP_HEADER_CONTENT_TYPE)); if (content_type) { /* do something interesting */ } - http_free_header_table (ht); - vec_free (headers); + http_free_header_table (&ht); } Finally application reads body, which might be received in multiple pieces (depends on size), so we might need some state machine in ``builtin_app_rx_callback``. |