summaryrefslogtreecommitdiffstats
path: root/src/plugins/http
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/http')
-rw-r--r--src/plugins/http/http.c90
-rw-r--r--src/plugins/http/http.h221
-rw-r--r--src/plugins/http/http_content_types.h19
-rw-r--r--src/plugins/http/http_header_names.h21
-rw-r--r--src/plugins/http/http_plugin.rst97
5 files changed, 351 insertions, 97 deletions
diff --git a/src/plugins/http/http.c b/src/plugins/http/http.c
index c5e2cc1df7d..db101539ac4 100644
--- a/src/plugins/http/http.c
+++ b/src/plugins/http/http.c
@@ -36,12 +36,6 @@ const char *http_status_code_str[] = {
#undef _
};
-const char *http_content_type_str[] = {
-#define _(s, ext, str) str,
- foreach_http_content_type
-#undef _
-};
-
const http_buffer_type_t msg_to_buf_type[] = {
[HTTP_MSG_DATA_INLINE] = HTTP_BUFFER_FIFO,
[HTTP_MSG_DATA_PTR] = HTTP_BUFFER_PTR,
@@ -374,22 +368,17 @@ http_ts_reset_callback (session_t *ts)
*/
static const char *http_error_template = "HTTP/1.1 %s\r\n"
"Date: %U GMT\r\n"
- "Content-Type: text/html\r\n"
"Connection: close\r\n"
- "Pragma: no-cache\r\n"
"Content-Length: 0\r\n\r\n";
-static const char *http_redirect_template = "HTTP/1.1 %s\r\n";
-
/**
* http response boilerplate
*/
static const char *http_response_template = "HTTP/1.1 %s\r\n"
"Date: %U GMT\r\n"
- "Expires: %U GMT\r\n"
"Server: %v\r\n"
- "Content-Type: %s\r\n"
- "Content-Length: %lu\r\n\r\n";
+ "Content-Length: %u\r\n"
+ "%s";
static const char *http_request_template = "GET %s HTTP/1.1\r\n"
"User-Agent: %v\r\n"
@@ -1004,49 +993,54 @@ http_state_wait_app_reply (http_conn_t *hc, transport_send_params_t *sp)
goto error;
}
- http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
- msg.data.len);
+ if (msg.code >= HTTP_N_STATUS)
+ {
+ clib_warning ("unsupported status code: %d", msg.code);
+ return HTTP_SM_ERROR;
+ }
/*
- * Add headers. For now:
+ * Add "protocol layer" headers:
* - current time
- * - expiration time
* - server name
- * - content type
* - data length
*/
now = clib_timebase_now (&hm->timebase);
-
- switch (msg.code)
- {
- case HTTP_STATUS_NOT_FOUND:
- case HTTP_STATUS_METHOD_NOT_ALLOWED:
- case HTTP_STATUS_BAD_REQUEST:
- case HTTP_STATUS_INTERNAL_ERROR:
- case HTTP_STATUS_FORBIDDEN:
- case HTTP_STATUS_OK:
- header =
- format (0, http_response_template, http_status_code_str[msg.code],
- /* Date */
- format_clib_timebase_time, now,
- /* Expires */
- format_clib_timebase_time, now + 600.0,
- /* Server */
- hc->app_name,
- /* Content type */
- http_content_type_str[msg.content_type],
- /* Length */
- msg.data.len);
- break;
- case HTTP_STATUS_MOVED:
- header =
- format (0, http_redirect_template, http_status_code_str[msg.code]);
- /* Location: http(s)://new-place already queued up as data */
- break;
- default:
- clib_warning ("unsupported status code: %d", msg.code);
- return HTTP_SM_ERROR;
+ header = format (0, http_response_template, http_status_code_str[msg.code],
+ /* Date */
+ format_clib_timebase_time, now,
+ /* Server */
+ hc->app_name,
+ /* Length */
+ msg.data.body_len,
+ /* Any headers from app? */
+ msg.data.headers_len ? "" : "\r\n");
+
+ /* Add headers from app (if any) */
+ if (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 (header, uword_to_pointer (app_headers_ptr, u8 *));
+ }
+ else
+ {
+ u32 orig_len = vec_len (header);
+ vec_resize (header, msg.data.headers_len);
+ u8 *p = header + orig_len;
+ rv = svm_fifo_dequeue (as->tx_fifo, msg.data.headers_len, p);
+ ASSERT (rv == msg.data.headers_len);
+ }
}
+ HTTP_DBG (0, "%v", header);
+
+ http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
+ msg.data.body_len);
offset = http_send_data (hc, header, vec_len (header), 0);
if (offset != vec_len (header))
diff --git a/src/plugins/http/http.h b/src/plugins/http/http.h
index debdebcf050..07d5472346e 100644
--- a/src/plugins/http/http.h
+++ b/src/plugins/http/http.h
@@ -51,6 +51,14 @@ typedef struct http_conn_id_
STATIC_ASSERT (sizeof (http_conn_id_t) <= TRANSPORT_CONN_ID_LEN,
"ctx id must be less than TRANSPORT_CONN_ID_LEN");
+typedef struct
+{
+ char *base;
+ uword len;
+} http_token_t;
+
+#define http_token_lit(s) (s), sizeof (s) - 1
+
typedef enum http_conn_state_
{
HTTP_CONN_STATE_LISTEN,
@@ -235,50 +243,100 @@ typedef enum http_status_code_
HTTP_N_STATUS
} http_status_code_t;
-#define HTTP_HEADER_ACCEPT "Accept"
-#define HTTP_HEADER_ACCEPT_CHARSET "Accept-Charset"
-#define HTTP_HEADER_ACCEPT_ENCODING "Accept-Encoding"
-#define HTTP_HEADER_ACCEPT_LANGUAGE "Accept-Language"
-#define HTTP_HEADER_ACCEPT_RANGES "Accept-Ranges"
-#define HTTP_HEADER_ALLOW "Allow"
-#define HTTP_HEADER_AUTHENTICATION_INFO "Authentication-Info"
-#define HTTP_HEADER_AUTHORIZATION "Authorization"
-#define HTTP_HEADER_CLOSE "Close"
-#define HTTP_HEADER_CONNECTION "Connection"
-#define HTTP_HEADER_CONTENT_ENCODING "Content-Encoding"
-#define HTTP_HEADER_CONTENT_LANGUAGE "Content-Language"
-#define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
-#define HTTP_HEADER_CONTENT_LOCATION "Content-Location"
-#define HTTP_HEADER_CONTENT_RANGE "Content-Range"
-#define HTTP_HEADER_CONTENT_TYPE "Content-Type"
-#define HTTP_HEADER_DATE "Date"
-#define HTTP_HEADER_ETAG "ETag"
-#define HTTP_HEADER_EXPECT "Expect"
-#define HTTP_HEADER_FROM "From"
-#define HTTP_HEADER_HOST "Host"
-#define HTTP_HEADER_IF_MATCH "If-Match"
-#define HTTP_HEADER_IF_MODIFIED_SINCE "If-Modified-Since"
-#define HTTP_HEADER_IF_NONE_MATCH "If-None-Match"
-#define HTTP_HEADER_IF_RANGE "If-Range"
-#define HTTP_HEADER_IF_UNMODIFIED_SINCE "If-Unmodified-Since"
-#define HTTP_HEADER_LAST_MODIFIED "Last-Modified"
-#define HTTP_HEADER_LOCATION "Location"
-#define HTTP_HEADER_MAX_FORWARDS "Max-Forwards"
-#define HTTP_HEADER_PROXY_AUTHENTICATE "Proxy-Authenticate"
-#define HTTP_HEADER_PROXY_AUTHENTICATION_INFO "Proxy-Authentication-Info"
-#define HTTP_HEADER_PROXY_AUTHORIZATION "Proxy-Authorization"
-#define HTTP_HEADER_RANGE "Range"
-#define HTTP_HEADER_REFERER "Referer"
-#define HTTP_HEADER_RETRY_AFTER "Retry-After"
-#define HTTP_HEADER_SERVER "Server"
-#define HTTP_HEADER_TE "TE"
-#define HTTP_HEADER_TRAILER "Trailer"
-#define HTTP_HEADER_TRANSFER_ENCODING "Transfer-Encoding"
-#define HTTP_HEADER_UPGRADE "Upgrade"
-#define HTTP_HEADER_USER_AGENT "User-Agent"
-#define HTTP_HEADER_VARY "Vary"
-#define HTTP_HEADER_VIA "Via"
-#define HTTP_HEADER_WWW_AUTHENTICATE "WWW-Authenticate"
+#define foreach_http_header_name \
+ _ (ACCEPT, "Accept") \
+ _ (ACCEPT_CHARSET, "Accept-Charset") \
+ _ (ACCEPT_ENCODING, "Accept-Encoding") \
+ _ (ACCEPT_LANGUAGE, "Accept-Language") \
+ _ (ACCEPT_RANGES, "Accept-Ranges") \
+ _ (ACCESS_CONTROL_ALLOW_CREDENTIALS, "Access-Control-Allow-Credentials") \
+ _ (ACCESS_CONTROL_ALLOW_HEADERS, "Access-Control-Allow-Headers") \
+ _ (ACCESS_CONTROL_ALLOW_METHODS, "Access-Control-Allow-Methods") \
+ _ (ACCESS_CONTROL_ALLOW_ORIGIN, "Access-Control-Allow-Origin") \
+ _ (ACCESS_CONTROL_EXPOSE_HEADERS, "Access-Control-Expose-Headers") \
+ _ (ACCESS_CONTROL_MAX_AGE, "Access-Control-Max-Age") \
+ _ (ACCESS_CONTROL_REQUEST_HEADERS, "Access-Control-Request-Headers") \
+ _ (ACCESS_CONTROL_REQUEST_METHOD, "Access-Control-Request-Method") \
+ _ (AGE, "Age") \
+ _ (ALLOW, "Allow") \
+ _ (ALPN, "ALPN") \
+ _ (ALT_SVC, "Alt-Svc") \
+ _ (ALT_USED, "Alt-Used") \
+ _ (ALTERNATES, "Alternates") \
+ _ (AUTHENTICATION_CONTROL, "Authentication-Control") \
+ _ (AUTHENTICATION_INFO, "Authentication-Info") \
+ _ (AUTHORIZATION, "Authorization") \
+ _ (CACHE_CONTROL, "Cache-Control") \
+ _ (CACHE_STATUS, "Cache-Status") \
+ _ (CAPSULE_PROTOCOL, "Capsule-Protocol") \
+ _ (CDN_CACHE_CONTROL, "CDN-Cache-Control") \
+ _ (CDN_LOOP, "CDN-Loop") \
+ _ (CLIENT_CERT, "Client-Cert") \
+ _ (CLIENT_CERT_CHAIN, "Client-Cert-Chain") \
+ _ (CLOSE, "Close") \
+ _ (CONNECTION, "Connection") \
+ _ (CONTENT_DIGEST, "Content-Digest") \
+ _ (CONTENT_DISPOSITION, "Content-Disposition") \
+ _ (CONTENT_ENCODING, "Content-Encoding") \
+ _ (CONTENT_LANGUAGE, "Content-Language") \
+ _ (CONTENT_LENGTH, "Content-Length") \
+ _ (CONTENT_LOCATION, "Content-Location") \
+ _ (CONTENT_RANGE, "Content-Range") \
+ _ (CONTENT_TYPE, "Content-Type") \
+ _ (COOKIE, "Cookie") \
+ _ (DATE, "Date") \
+ _ (DIGEST, "Digest") \
+ _ (DPOP, "DPoP") \
+ _ (DPOP_NONCE, "DPoP-Nonce") \
+ _ (EARLY_DATA, "Early-Data") \
+ _ (ETAG, "ETag") \
+ _ (EXPECT, "Expect") \
+ _ (EXPIRES, "Expires") \
+ _ (FORWARDED, "Forwarded") \
+ _ (FROM, "From") \
+ _ (HOST, "Host") \
+ _ (IF_MATCH, "If-Match") \
+ _ (IF_MODIFIED_SINCE, "If-Modified-Since") \
+ _ (IF_NONE_MATCH, "If-None-Match") \
+ _ (IF_RANGE, "If-Range") \
+ _ (IF_UNMODIFIED_SINCE, "If-Unmodified-Since") \
+ _ (KEEP_ALIVE, "Keep-Alive") \
+ _ (LAST_MODIFIED, "Last-Modified") \
+ _ (LINK, "Link") \
+ _ (LOCATION, "Location") \
+ _ (MAX_FORWARDS, "Max-Forwards") \
+ _ (ORIGIN, "Origin") \
+ _ (PRIORITY, "Priority") \
+ _ (PROXY_AUTHENTICATE, "Proxy-Authenticate") \
+ _ (PROXY_AUTHENTICATION_INFO, "Proxy-Authentication-Info") \
+ _ (PROXY_AUTHORIZATION, "Proxy-Authorization") \
+ _ (PROXY_STATUS, "Proxy-Status") \
+ _ (RANGE, "Range") \
+ _ (REFERER, "Referer") \
+ _ (REPR_DIGEST, "Repr-Digest") \
+ _ (SET_COOKIE, "Set-Cookie") \
+ _ (SIGNATURE, "Signature") \
+ _ (SIGNATURE_INPUT, "Signature-Input") \
+ _ (STRICT_TRANSPORT_SECURITY, "Strict-Transport-Security") \
+ _ (RETRY_AFTER, "Retry-After") \
+ _ (SERVER, "Server") \
+ _ (TE, "TE") \
+ _ (TRAILER, "Trailer") \
+ _ (TRANSFER_ENCODING, "Transfer-Encoding") \
+ _ (UPGRADE, "Upgrade") \
+ _ (USER_AGENT, "User-Agent") \
+ _ (VARY, "Vary") \
+ _ (VIA, "Via") \
+ _ (WANT_CONTENT_DIGEST, "Want-Content-Digest") \
+ _ (WANT_REPR_DIGEST, "Want-Repr-Digest") \
+ _ (WWW_AUTHENTICATE, "WWW-Authenticate")
+
+typedef enum http_header_name_
+{
+#define _(sym, str) HTTP_HEADER_##sym,
+ foreach_http_header_name
+#undef _
+} http_header_name_t;
typedef enum http_msg_data_type_
{
@@ -669,11 +727,17 @@ typedef struct
{
u8 *name;
u8 *value;
+} http_header_ht_t;
+
+typedef struct
+{
+ http_token_t name;
+ http_token_t value;
} http_header_t;
typedef struct
{
- http_header_t *headers;
+ http_header_ht_t *headers;
uword *value_by_name;
} http_header_table_t;
@@ -685,7 +749,7 @@ typedef struct
always_inline void
http_free_header_table (http_header_table_t *ht)
{
- http_header_t *header;
+ http_header_ht_t *header;
vec_foreach (header, ht->headers)
{
vec_free (header->name);
@@ -713,7 +777,7 @@ http_parse_headers (u8 *headers, http_header_table_t **header_table)
u8 *pos, *end, *name_start, *value_start, *name;
u32 name_len, value_len;
int rv;
- http_header_t *header;
+ http_header_ht_t *header;
http_header_table_t *ht;
uword *p;
@@ -779,7 +843,7 @@ always_inline const char *
http_get_header (http_header_table_t *header_table, const char *name)
{
uword *p;
- http_header_t *header;
+ http_header_ht_t *header;
p = hash_get_mem (header_table->value_by_name, name);
if (p)
@@ -791,6 +855,67 @@ http_get_header (http_header_table_t *header_table, const char *name)
return 0;
}
+/**
+ * Add header to the list.
+ *
+ * @param headers Header list.
+ * @param name Pointer to header's name buffer.
+ * @param name_len Length of the name.
+ * @param value Pointer to header's value buffer.
+ * @param value_len Length of the value.
+ *
+ * @note Headers added at protocol layer: Date, Server, Content-Length
+ */
+always_inline void
+http_add_header (http_header_t **headers, const char *name, uword name_len,
+ const char *value, uword value_len)
+{
+ http_header_t *header;
+ vec_add2 (*headers, header, 1);
+ header->name.base = (char *) name;
+ header->name.len = name_len;
+ header->value.base = (char *) value;
+ header->value.len = value_len;
+}
+
+/**
+ * Serialize the header list.
+ *
+ * @param headers Header list to serialize.
+ *
+ * @return New vector with serialized headers.
+ *
+ * The caller is always responsible to free the returned vector.
+ */
+always_inline u8 *
+http_serialize_headers (http_header_t *headers)
+{
+ u8 *headers_buf = 0, *dst;
+ u32 headers_buf_len = 2;
+ http_header_t *header;
+
+ vec_foreach (header, headers)
+ headers_buf_len += header->name.len + header->value.len + 4;
+
+ vec_validate (headers_buf, headers_buf_len - 1);
+ dst = headers_buf;
+
+ vec_foreach (header, headers)
+ {
+ clib_memcpy (dst, header->name.base, header->name.len);
+ dst += header->name.len;
+ *dst++ = ':';
+ *dst++ = ' ';
+ clib_memcpy (dst, header->value.base, header->value.len);
+ dst += header->value.len;
+ *dst++ = '\r';
+ *dst++ = '\n';
+ }
+ *dst++ = '\r';
+ *dst = '\n';
+ return headers_buf;
+}
+
#endif /* SRC_PLUGINS_HTTP_HTTP_H_ */
/*
diff --git a/src/plugins/http/http_content_types.h b/src/plugins/http/http_content_types.h
new file mode 100644
index 00000000000..ddc02566db7
--- /dev/null
+++ b/src/plugins/http/http_content_types.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2024 Cisco Systems, Inc.
+ */
+
+#ifndef SRC_PLUGINS_HTTP_HTTP_CONTENT_TYPES_H_
+#define SRC_PLUGINS_HTTP_HTTP_CONTENT_TYPES_H_
+
+#include <http/http.h>
+
+static http_token_t http_content_types[] = {
+#define _(s, ext, str) { http_token_lit (str) },
+ foreach_http_content_type
+#undef _
+};
+
+#define http_content_type_token(e) \
+ http_content_types[e].base, http_content_types[e].len
+
+#endif /* SRC_PLUGINS_HTTP_HTTP_CONTENT_TYPES_H_ */
diff --git a/src/plugins/http/http_header_names.h b/src/plugins/http/http_header_names.h
new file mode 100644
index 00000000000..99acac786db
--- /dev/null
+++ b/src/plugins/http/http_header_names.h
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2024 Cisco Systems, Inc.
+ */
+
+#ifndef SRC_PLUGINS_HTTP_HTTP_HEADER_NAMES_H_
+#define SRC_PLUGINS_HTTP_HTTP_HEADER_NAMES_H_
+
+#include <http/http.h>
+
+static http_token_t http_header_names[] = {
+#define _(sym, str) { http_token_lit (str) },
+ foreach_http_header_name
+#undef _
+};
+
+#define http_header_name_token(e) \
+ http_header_names[e].base, http_header_names[e].len
+
+#define http_header_name_str(e) http_header_names[e].base
+
+#endif /* SRC_PLUGINS_HTTP_HTTP_HEADER_NAMES_H_ */
diff --git a/src/plugins/http/http_plugin.rst b/src/plugins/http/http_plugin.rst
index c4c4d2c8234..2f7a58ef9bf 100644
--- a/src/plugins/http/http_plugin.rst
+++ b/src/plugins/http/http_plugin.rst
@@ -121,6 +121,7 @@ Following example shows how to parse headers:
.. code-block:: C
+ #include <http/http_header_names.h>
if (msg.data.headers_len)
{
u8 *headers = 0;
@@ -134,7 +135,7 @@ Following example shows how to parse headers:
/* your error handling */
}
/* get Accept header */
- const char *accept_value = http_get_header (ht, HTTP_HEADER_ACCEPT);
+ const char *accept_value = http_get_header (ht, http_header_name_str (HTTP_HEADER_ACCEPT));
if (accept_value)
{
/* do something interesting */
@@ -154,3 +155,97 @@ Finally application reads body:
rv = svm_fifo_peek (ts->rx_fifo, msg.data.body_offset, msg.data.body_len, body);
ASSERT (rv == msg.data.body_len);
}
+
+Sending data
+""""""""""""""
+
+When server application sends response back to HTTP layer it starts with message metadata, followed by optional serialized headers and finally body (if any).
+
+Application should set following items:
+
+* Status code
+* target form
+* header section offset and length
+* body offset and length
+
+Application could pass headers back to HTTP layer. Header list is created dynamically as vector of ``http_header_t``,
+where we store only pointers to buffers (zero copy).
+Well known header names are predefined.
+The list is serialized just before you send buffer to HTTP layer.
+
+.. note::
+ Following headers are added at protocol layer and **MUST NOT** be set by application: Date, Server, Content-Length
+
+Following example shows how to create headers section:
+
+.. code-block:: C
+
+ #include <http/http.h>
+ #include <http/http_header_names.h>
+ #include <http/http_content_types.h>
+ http_header_t *resp_headers = 0;
+ u8 *headers_buf = 0;
+ http_add_header (resp_headers,
+ http_header_name_token (HTTP_HEADER_CONTENT_TYPE),
+ http_content_type_token (HTTP_CONTENT_TEXT_HTML));
+ http_add_header (resp_headers,
+ http_header_name_token (HTTP_HEADER_CACHE_CONTROL),
+ http_token_lit ("max-age=600"));
+ http_add_header (&hs->resp_headers,
+ http_header_name_token (HTTP_HEADER_LOCATION),
+ (const char *) redirect, vec_len (redirect));
+ headers_buf = http_serialize_headers (hs->resp_headers);
+
+The example below show how to create and send response HTTP message metadata:
+
+.. code-block:: C
+
+ http_msg_t msg;
+ msg.type = HTTP_MSG_REPLY;
+ msg.code = HTTP_STATUS_MOVED
+ msg.data.headers_offset = 0;
+ msg.data.headers_len = vec_len (headers_buf);
+ msg.data.type = HTTP_MSG_DATA_INLINE;
+ msg.data.body_len = vec_len (tx_buf);
+ msg.data.body_offset = msg.data.headers_len;
+ msg.data.len = msg.data.body_len + msg.data.headers_len;
+ ts = session_get (hs->vpp_session_index, hs->thread_index);
+ rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
+ ASSERT (rv == sizeof (msg));
+
+Next you will send your serialized headers:
+
+.. code-block:: C
+
+ rv = svm_fifo_enqueue (ts->tx_fifo, vec_len (headers_buf), headers_buf);
+ ASSERT (rv == msg.data.headers_len);
+ vec_free (headers_buf);
+
+Finally application sends response body:
+
+.. code-block:: C
+
+ rv = svm_fifo_enqueue (ts->tx_fifo, vec_len (tx_buf), tx_buf);
+ if (rv != vec_len (hs->tx_buf))
+ {
+ hs->tx_offset = rv;
+ svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
+ }
+ else
+ {
+ vec_free (tx_buf);
+ }
+ if (svm_fifo_set_event (ts->tx_fifo))
+ session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
+
+Example above shows how to send body data by copy, alternatively you could pass it as pointer:
+
+.. code-block:: C
+
+ msg.data.type = HTTP_MSG_DATA_PTR;
+ /* code omitted for brevity */
+ uword data = pointer_to_uword (tx_buf);
+ rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (data), (u8 *) &data);
+ ASSERT (rv == sizeof (data));
+
+In this case you need to free data when you receive next request or when session is closed.