Line data Source code
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 : 16 : #include <vnet/vnet.h> 17 : #include <vnet/plugin/plugin.h> 18 : #include <http_static/http_static.h> 19 : 20 : #include <vlibapi/api.h> 21 : #include <vlibmemory/api.h> 22 : #include <vpp/app/version.h> 23 : 24 : /* define message IDs */ 25 : #include <http_static/http_static.api_enum.h> 26 : #include <http_static/http_static.api_types.h> 27 : 28 : #include <vpp/api/types.h> 29 : 30 : 31 : #define REPLY_MSG_ID_BASE hsm->msg_id_base 32 : #include <vlibapi/api_helper_macros.h> 33 : 34 : __clib_export void 35 0 : hss_register_url_handler (hss_url_handler_fn fp, const char *url, 36 : http_req_method_t request_type) 37 : { 38 0 : hss_main_t *hsm = &hss_main; 39 : uword *p, *url_table; 40 : 41 0 : url_table = (request_type == HTTP_REQ_GET) ? hsm->get_url_handlers : 42 : hsm->post_url_handlers; 43 : 44 0 : p = hash_get_mem (url_table, url); 45 : 46 0 : if (p) 47 : { 48 0 : clib_warning ("WARNING: attempt to replace handler for %s '%s' ignored", 49 : (request_type == HTTP_REQ_GET) ? "GET" : "POST", url); 50 0 : return; 51 : } 52 : 53 0 : hash_set_mem (url_table, url, (uword) fp); 54 : 55 : /* 56 : * Need to update the hash table pointer in http_static_server_main 57 : * in case we just expanded it... 58 : */ 59 0 : if (request_type == HTTP_REQ_GET) 60 0 : hsm->get_url_handlers = url_table; 61 : else 62 0 : hsm->post_url_handlers = url_table; 63 : } 64 : 65 : /** \brief API helper function for vl_api_http_static_enable_t messages 66 : */ 67 : static int 68 0 : hss_enable_api (u32 fifo_size, u32 cache_limit, u32 prealloc_fifos, 69 : u32 private_segment_size, u8 *www_root, u8 *uri) 70 : { 71 0 : hss_main_t *hsm = &hss_main; 72 : int rv; 73 : 74 0 : hsm->fifo_size = fifo_size; 75 0 : hsm->cache_size = cache_limit; 76 0 : hsm->prealloc_fifos = prealloc_fifos; 77 0 : hsm->private_segment_size = private_segment_size; 78 0 : hsm->www_root = format (0, "%s%c", www_root, 0); 79 0 : hsm->uri = format (0, "%s%c", uri, 0); 80 : 81 0 : if (vec_len (hsm->www_root) < 2) 82 0 : return VNET_API_ERROR_INVALID_VALUE; 83 : 84 0 : if (hsm->app_index != ~0) 85 0 : return VNET_API_ERROR_APP_ALREADY_ATTACHED; 86 : 87 0 : vnet_session_enable_disable (hsm->vlib_main, 1 /* turn on TCP, etc. */); 88 : 89 0 : rv = hss_create (hsm->vlib_main); 90 0 : switch (rv) 91 : { 92 0 : case 0: 93 0 : break; 94 0 : default: 95 0 : vec_free (hsm->www_root); 96 0 : vec_free (hsm->uri); 97 0 : return VNET_API_ERROR_INIT_FAILED; 98 : } 99 0 : return 0; 100 : } 101 : 102 : /* API message handler */ 103 0 : static void vl_api_http_static_enable_t_handler 104 : (vl_api_http_static_enable_t * mp) 105 : { 106 : vl_api_http_static_enable_reply_t *rmp; 107 0 : hss_main_t *hsm = &hss_main; 108 : int rv; 109 : 110 0 : mp->uri[ARRAY_LEN (mp->uri) - 1] = 0; 111 0 : mp->www_root[ARRAY_LEN (mp->www_root) - 1] = 0; 112 : 113 : rv = 114 0 : hss_enable_api (ntohl (mp->fifo_size), ntohl (mp->cache_size_limit), 115 : ntohl (mp->prealloc_fifos), 116 0 : ntohl (mp->private_segment_size), mp->www_root, mp->uri); 117 : 118 0 : REPLY_MACRO (VL_API_HTTP_STATIC_ENABLE_REPLY); 119 : } 120 : 121 : #include <http_static/http_static.api.c> 122 : static clib_error_t * 123 559 : hss_api_init (vlib_main_t *vm) 124 : { 125 559 : hss_main_t *hsm = &hss_main; 126 : 127 : /* Ask for a correctly-sized block of API message decode slots */ 128 559 : hsm->msg_id_base = setup_message_id_table (); 129 : 130 559 : return 0; 131 : } 132 : 133 1119 : VLIB_INIT_FUNCTION (hss_api_init); 134 : 135 : VLIB_PLUGIN_REGISTER () = 136 : { 137 : .version = VPP_BUILD_VER, 138 : .description = "HTTP Static Server" 139 : }; 140 : 141 : /* 142 : * fd.io coding-style-patch-verification: ON 143 : * 144 : * Local Variables: 145 : * eval: (c-set-style "gnu") 146 : * End: 147 : */