http_static: incorporate builtinurl plugin
[vpp.git] / src / plugins / http_static / http_static.h
1 /*
2  * Copyright (c) 2017-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 #ifndef __included_http_static_h__
16 #define __included_http_static_h__
17
18 #include <vnet/session/application_interface.h>
19 #include <vnet/session/session.h>
20 #include <http/http.h>
21
22 #include <vppinfra/hash.h>
23 #include <vppinfra/error.h>
24 #include <vppinfra/bihash_vec8_8.h>
25
26 /** @file http_static.h
27  * Static http server definitions
28  */
29
30 /** \brief Application session
31  */
32 typedef struct
33 {
34   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
35   u32 session_index;
36   /** rx thread index */
37   u32 thread_index;
38   /** vpp session index, handle */
39   u32 vpp_session_index;
40   session_handle_t vpp_session_handle;
41   /** Fully-resolved file path */
42   u8 *path;
43   /** File data, a vector */
44   u8 *data;
45   /** Current data send offset */
46   u32 data_offset;
47   /** Need to free data in detach_cache_entry */
48   int free_data;
49   /** File cache pool index */
50   u32 cache_pool_index;
51 } hss_session_t;
52
53 /** \brief In-memory file data cache entry
54  */
55 typedef struct
56 {
57   /** Name of the file */
58   u8 *filename;
59   /** Contents of the file, as a u8 * vector */
60   u8 *data;
61   /** Last time the cache entry was used */
62   f64 last_used;
63   /** Cache LRU links */
64   u32 next_index;
65   u32 prev_index;
66   /** Reference count, so we don't recycle while referenced */
67   int inuse;
68 } hss_cache_entry_t;
69
70 typedef int (*hss_url_handler_t) (http_req_method_t, u8 *, hss_session_t *);
71
72 /** \brief Main data structure
73  */
74 typedef struct
75 {
76   /** Per thread vector of session pools */
77   hss_session_t **sessions;
78   /** Session pool reader writer lock */
79   clib_spinlock_t cache_lock;
80
81   /** Unified file data cache pool */
82   hss_cache_entry_t *cache_pool;
83   /** Hash table which maps file name to file data */
84     BVT (clib_bihash) name_to_data;
85
86   /** Hash tables for built-in GET and POST handlers */
87   uword *get_url_handlers;
88   uword *post_url_handlers;
89
90   /** Current cache size */
91   u64 cache_size;
92   /** Max cache size in bytes */
93   u64 cache_limit;
94   /** Number of cache evictions */
95   u64 cache_evictions;
96
97   /** Cache LRU listheads */
98   u32 first_index;
99   u32 last_index;
100
101   /** root path to be served */
102   u8 *www_root;
103
104   /** Application index */
105   u32 app_index;
106
107   /** Cert and key pair for tls */
108   u32 ckpair_index;
109
110   /* API message ID base */
111   u16 msg_id_base;
112
113   vlib_main_t *vlib_main;
114
115   /*
116    * Config
117    */
118
119   /** Enable debug messages */
120   int debug_level;
121   /** Number of preallocated fifos, usually 0 */
122   u32 prealloc_fifos;
123   /** Private segment size, usually 0 */
124   u64 private_segment_size;
125   /** Size of the allocated rx, tx fifos, roughly 8K or so */
126   u32 fifo_size;
127   /** The bind URI, defaults to tcp://0.0.0.0/80 */
128   u8 *uri;
129   /** Threshold for switching to ptr data in http msgs */
130   u32 use_ptr_thresh;
131   /** Enable the use of builtinurls */
132   u8 enable_url_handlers;
133 } hss_main_t;
134
135 extern hss_main_t hss_main;
136
137 int hss_create (vlib_main_t *vm);
138
139 /**
140  * Register a GET or POST URL handler
141  */
142 void hss_register_url_handler (hss_url_handler_t fp, const char *url,
143                                http_req_method_t type);
144 void hss_builtinurl_json_handlers_init (void);
145
146 #endif /* __included_http_static_h__ */
147
148 /*
149  * fd.io coding-style-patch-verification: ON
150  *
151  * Local Variables:
152  * eval: (c-set-style "gnu")
153  * End:
154  */