50203dcd2f907509c66b3c680f697ce781cce88d
[vpp.git] / src / plugins / http / http.h
1 /*
2  * Copyright (c) 2022 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef SRC_PLUGINS_HTTP_HTTP_H_
17 #define SRC_PLUGINS_HTTP_HTTP_H_
18
19 #include <vnet/plugin/plugin.h>
20 #include <vpp/app/version.h>
21
22 #include <vppinfra/time_range.h>
23
24 #include <vnet/session/application_interface.h>
25 #include <vnet/session/application.h>
26 #include <http/http_buffer.h>
27
28 #define HTTP_DEBUG 0
29
30 #if HTTP_DEBUG
31 #define HTTP_DBG(_lvl, _fmt, _args...)                                        \
32   if (_lvl <= HTTP_DEBUG)                                                     \
33   clib_warning (_fmt, ##_args)
34 #else
35 #define HTTP_DBG(_lvl, _fmt, _args...)
36 #endif
37
38 typedef struct http_conn_id_
39 {
40   union
41   {
42     session_handle_t app_session_handle;
43     u32 parent_app_api_ctx;
44   };
45   session_handle_t tc_session_handle;
46   u32 parent_app_wrk_index;
47 } http_conn_id_t;
48
49 STATIC_ASSERT (sizeof (http_conn_id_t) <= TRANSPORT_CONN_ID_LEN,
50                "ctx id must be less than TRANSPORT_CONN_ID_LEN");
51
52 typedef enum http_conn_state_
53 {
54   HTTP_CONN_STATE_LISTEN,
55   HTTP_CONN_STATE_CONNECTING,
56   HTTP_CONN_STATE_ESTABLISHED,
57   HTTP_CONN_STATE_TRANSPORT_CLOSED,
58   HTTP_CONN_STATE_APP_CLOSED,
59   HTTP_CONN_STATE_CLOSED
60 } http_conn_state_t;
61
62 typedef enum http_state_
63 {
64   HTTP_STATE_WAIT_METHOD,
65   HTTP_STATE_WAIT_APP,
66   HTTP_STATE_IO_MORE_DATA,
67   HTTP_N_STATES,
68 } http_state_t;
69
70 typedef enum http_req_method_
71 {
72   HTTP_REQ_GET = 0,
73   HTTP_REQ_POST,
74 } http_req_method_t;
75
76 typedef enum http_msg_type_
77 {
78   HTTP_MSG_REQUEST,
79   HTTP_MSG_REPLY
80 } http_msg_type_t;
81
82 #define foreach_http_content_type                                             \
83   _ (APP_7Z, ".7z", "application / x - 7z - compressed")                      \
84   _ (APP_DOC, ".doc", "application / msword")                                 \
85   _ (APP_DOCX, ".docx",                                                       \
86      "application / vnd.openxmlformats - "                                    \
87      "officedocument.wordprocessingml.document")                              \
88   _ (APP_EPUB, ".epub", "application / epub + zip")                           \
89   _ (APP_FONT, ".eot", "application / vnd.ms - fontobject")                   \
90   _ (APP_JAR, ".jar", "application / java - archive")                         \
91   _ (APP_JSON, ".json", "application / json")                                 \
92   _ (APP_JSON_LD, ".jsonld", "application / ld + json")                       \
93   _ (APP_MPKG, ".mpkg", "application / vnd.apple.installer + xml")            \
94   _ (APP_ODP, ".odp", "application / vnd.oasis.opendocument.presentation")    \
95   _ (APP_ODS, ".ods", "application / vnd.oasis.opendocument.spreadsheet")     \
96   _ (APP_ODT, ".odt", "application / vnd.oasis.opendocument.text")            \
97   _ (APP_OGX, ".ogx", "application / ogg")                                    \
98   _ (APP_PDF, ".pdf", "application / pdf")                                    \
99   _ (APP_PHP, ".php", "application / x - httpd - php")                        \
100   _ (APP_PPT, ".ppt", "application / vnd.ms - powerpoint")                    \
101   _ (APP_PPTX, ".pptx", "application / vnd.ms - powerpoint")                  \
102   _ (APP_RAR, ".rar", "application / vnd.rar")                                \
103   _ (APP_RTF, ".rtf", "application / rtf")                                    \
104   _ (APP_SH, ".sh", "application / x - sh")                                   \
105   _ (APP_TAR, ".tar", "application / x - tar")                                \
106   _ (APP_VSD, ".vsd", "application / vnd.visio")                              \
107   _ (APP_XHTML, ".xhtml", "application / xhtml + xml")                        \
108   _ (APP_XLS, ".xls", "application / vnd.ms - excel")                         \
109   _ (APP_XML, ".xml", "application / xml")                                    \
110   _ (APP_XSLX, ".xlsx",                                                       \
111      "application / vnd.openxmlformats - officedocument.spreadsheetml.sheet") \
112   _ (APP_XUL, ".xul", "application / vnd.mozilla.xul + xml")                  \
113   _ (APP_ZIP, ".zip", "application / zip")                                    \
114   _ (AUDIO_AAC, ".aac", "audio / aac")                                        \
115   _ (AUDIO_CD, ".cda", "application / x - cdf")                               \
116   _ (AUDIO_WAV, ".wav", "audio / wav")                                        \
117   _ (AUDIO_WEBA, ".weba", "audio / webm")                                     \
118   _ (AUDO_MIDI, ".midi", "audio / midi")                                      \
119   _ (AUDO_MID, ".mid", "audo / midi")                                         \
120   _ (AUDO_MP3, ".mp3", "audio / mpeg")                                        \
121   _ (AUDO_OGA, ".oga", "audio / ogg")                                         \
122   _ (AUDO_OPUS, ".opus", "audio / opus")                                      \
123   _ (APP_OCTET_STREAM, ".bin", "application / octet - stream")                \
124   _ (BZIP2, ".bz2", "application / x - bzip2")                                \
125   _ (BZIP, ".bz", "application / x - bzip")                                   \
126   _ (FONT_OTF, ".otf", "font / otf")                                          \
127   _ (FONT_TTF, ".ttf", "font / ttf")                                          \
128   _ (FONT_WOFF2, ".woff2", "font / woff2")                                    \
129   _ (FONT_WOFF, ".woff", "font / woff")                                       \
130   _ (GZIP, ".gz", "application / gzip")                                       \
131   _ (IMAGE_AVIF, ".avif", "image / avif")                                     \
132   _ (IMAGE_BMP, ".bmp", "image / bmp")                                        \
133   _ (IMAGE_GIF, ".gif", "image / gif")                                        \
134   _ (IMAGE_ICON, ".ico", "image / vnd.microsoft.icon")                        \
135   _ (IMAGE_JPEG, ".jpeg", "image / jpeg")                                     \
136   _ (IMAGE_JPG, ".jpg", "image / jpeg")                                       \
137   _ (IMAGE_PNG, ".png", "image / png")                                        \
138   _ (IMAGE_SVG, ".svg", "image / svg + xml")                                  \
139   _ (IMAGE_TIFF, ".tiff", "image / tiff")                                     \
140   _ (IMAGE_TIF, ".tif", "image / tiff")                                       \
141   _ (IMAGE_WEBP, ".webp", "image / webp")                                     \
142   _ (SCRIPT_CSH, ".csh", "application / x - csh")                             \
143   _ (TEXT_ABIWORD, ".abw", "application / x - abiword")                       \
144   _ (TEXT_ARCHIVE, ".arc", "application / x - freearc")                       \
145   _ (TEXT_AZW, ".azw", "application / vnd.amazon.ebook")                      \
146   _ (TEXT_CALENDAR, ".ics", "text / calendar")                                \
147   _ (TEXT_CSS, ".css", "text / css")                                          \
148   _ (TEXT_CSV, ".csv", "text / csv")                                          \
149   _ (TEXT_HTM, ".htm", "text / html")                                         \
150   _ (TEXT_HTML, ".html", "text / html")                                       \
151   _ (TEXT_JS, ".js", "text / javascript")                                     \
152   _ (TEXT_MJS, ".mjs", "text / javascript")                                   \
153   _ (TEXT_PLAIN, ".txt", "text / plain")                                      \
154   _ (VIDEO_3GP2, ".3g2", "video / 3gpp2")                                     \
155   _ (VIDEO_3GP, ".3gp", "video / 3gpp")                                       \
156   _ (VIDEO_AVI, ".avi", "video / x - msvideo")                                \
157   _ (VIDEO_MP4, ".mp4", "video / mp4")                                        \
158   _ (VIDEO_MPEG, ".mpeg", "video / mpeg")                                     \
159   _ (VIDEO_OGG, ".ogv", "video / ogg")                                        \
160   _ (VIDEO_TS, ".ts", "video / mp2t")                                         \
161   _ (VIDEO_WEBM, ".webm", "video / webm")
162
163 typedef enum http_content_type_
164 {
165 #define _(s, ext, str) HTTP_CONTENT_##s,
166   foreach_http_content_type
167 #undef _
168 } http_content_type_t;
169
170 #define foreach_http_status_code                                              \
171   _ (200, OK, "200 OK")                                                       \
172   _ (301, MOVED, "301 Moved Permanently")                                     \
173   _ (400, BAD_REQUEST, "400 Bad Request")                                     \
174   _ (404, NOT_FOUND, "404 Not Found")                                         \
175   _ (405, METHOD_NOT_ALLOWED, "405 Method Not Allowed")                       \
176   _ (500, INTERNAL_ERROR, "500 Internal Server Error")
177
178 typedef enum http_status_code_
179 {
180 #define _(c, s, str) HTTP_STATUS_##s,
181   foreach_http_status_code
182 #undef _
183     HTTP_N_STATUS
184 } http_status_code_t;
185
186 typedef enum http_msg_data_type_
187 {
188   HTTP_MSG_DATA_INLINE,
189   HTTP_MSG_DATA_PTR
190 } http_msg_data_type_t;
191
192 typedef struct http_msg_data_
193 {
194   http_msg_data_type_t type;
195   u64 len;
196   u8 data[0];
197 } http_msg_data_t;
198
199 typedef struct http_msg_
200 {
201   http_msg_type_t type;
202   union
203   {
204     http_req_method_t method_type;
205     http_status_code_t code;
206   };
207   http_content_type_t content_type;
208   http_msg_data_t data;
209 } http_msg_t;
210
211 typedef struct http_tc_
212 {
213   union
214   {
215     transport_connection_t connection;
216     http_conn_id_t c_http_conn_id;
217   };
218 #define h_tc_session_handle c_http_conn_id.tc_session_handle
219 #define h_pa_wrk_index      c_http_conn_id.parent_app_wrk_index
220 #define h_pa_session_handle c_http_conn_id.app_session_handle
221 #define h_pa_app_api_ctx    c_http_conn_id.parent_app_api_ctx
222 #define h_hc_index          connection.c_index
223
224   http_conn_state_t state;
225   u32 timer_handle;
226
227   /*
228    * Current request
229    */
230   http_state_t http_state;
231   http_req_method_t method;
232   u8 *rx_buf;
233   u32 rx_buf_offset;
234   http_buffer_t tx_buf;
235   u8 is_client;
236   u32 to_recv;
237   u32 bytes_dequeued;
238 } http_conn_t;
239
240 typedef struct http_worker_
241 {
242   http_conn_t *conn_pool;
243 } http_worker_t;
244
245 typedef struct http_main_
246 {
247   http_worker_t *wrk;
248   http_conn_t *listener_pool;
249   u32 app_index;
250
251   clib_timebase_t timebase;
252
253   /*
254    * Runtime config
255    */
256   u8 debug_level;
257
258   /*
259    * Config
260    */
261   u64 first_seg_size;
262   u64 add_seg_size;
263   u32 fifo_size;
264 } http_main_t;
265
266 #endif /* SRC_PLUGINS_HTTP_HTTP_H_ */
267
268 /*
269  * fd.io coding-style-patch-verification: ON
270  *
271  * Local Variables:
272  * eval: (c-set-style "gnu")
273  * End:
274  */