Tizen RT Libs&Environment  v1.0 D5
http_server.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the License.
16  *
17  ****************************************************************************/
35 #ifndef __http_server_h__
36 #define __http_server_h__
37 
38 /****************************************************************************
39  * Included Files
40  ****************************************************************************/
41 
42 #include <stdio.h>
43 #include <mqueue.h>
44 #include <sys/socket.h>
45 #include <sys/select.h>
46 #include <netinet/in.h>
47 
48 #ifdef CONFIG_NETUTILS_WEBSOCKET
49 #include <apps/netutils/websocket.h>
50 #endif
51 
52 #ifdef CONFIG_NET_SECURITY_TLS
53 #include "tls/config.h"
54 #include "tls/entropy.h"
55 #include "tls/ctr_drbg.h"
56 #include "tls/certs.h"
57 #include "tls/x509.h"
58 #include "tls/ssl.h"
59 #include "tls/net.h"
60 #include "tls/error.h"
61 #include "tls/debug.h"
62 #include "tls/ssl_cache.h"
63 #endif
64 
65 /****************************************************************************
66  * Pre-processor Definitions
67  ****************************************************************************/
68 
69 #define HTTP_METHOD_UNKNOWN -1
70 #define HTTP_METHOD_GET 0
71 #define HTTP_METHOD_PUT 1
72 #define HTTP_METHOD_POST 2
73 #define HTTP_METHOD_DELETE 3
74 
75 #define HTTP_HTTP_VERSION_UNKNOWN 0
76 #define HTTP_HTTP_VERSION_09 9
77 #define HTTP_HTTP_VERSION_10 10
78 #define HTTP_HTTP_VERSION_11 11
79 
80 #define HTTP_CONTENT_LENGTH 0
81 #define HTTP_CHUNKED_ENCODING 1
82 
83 #define HTTP_CONF_MAX_CLIENT 16
84 #define HTTP_CONF_CLIENT_STACKSIZE 8192
85 #define HTTP_CONF_MIN_TLS_MEMORY 80000
86 #define HTTP_CONF_SOCKET_TIMEOUT_MSEC 5000
87 #define HTTP_CONF_MAX_CLIENT_HANDLE 2
88 #define HTTP_CONF_SERVER_MQ_MAX_MSG 10
89 #define HTTP_CONF_SERVER_MQ_PRIO 50
90 #define HTTP_CONF_SERVER_SIGWAKEUP 18
91 
92 #define HTTP_CONF_MAX_REQUEST_LENGTH 2048
93 #define HTTP_CONF_MAX_REQUEST_LINE_LENGTH 256
94 #define HTTP_CONF_MAX_REQUEST_HEADER_URL_LENGTH 128
95 #define HTTP_CONF_MAX_URL_QUERY_LENGTH 64
96 #define HTTP_CONF_MAX_URL_PARAMS_LENGTH 256
97 #define HTTP_CONF_MAX_KEY_LENGTH 32
98 #define HTTP_CONF_MAX_VALUE_LENGTH 256
99 #define HTTP_CONF_MAX_DIVIDED_PATH_LENGTH 32
100 #define HTTP_CONF_MAX_SLASH_COUNT 32
101 #define HTTP_CONF_MAX_QUERY_HANDLER_COUNT 64
102 #define HTTP_CONF_MAX_ENTITY_LENGTH 2048
103 
104 #define HTTP_ERROR_400 "Bad Request"
105 #define HTTP_ERROR_404 "Not Found"
106 #define HTTP_ERROR_500 "Internal Server Error"
107 
108 /****************************************************************************
109  * Public Types
110  ****************************************************************************/
111 
112 struct http_client_t;
113 struct http_keyvalue_list_t;
114 
119 struct ssl_config_t {
120 #ifdef CONFIG_HW_RSA_SIGN
121  unsigned int ca_key_index;
122  unsigned int dev_key_index;
123  unsigned int ca_cert_index;
124  unsigned int dev_cert_index;
125 #else
126  char *root_ca;
127  char *dev_cert;
128  char *private_key;
129  unsigned int root_ca_len;
130  unsigned int dev_cert_len;
131  unsigned int private_key_len;
132 #endif /* CONFIG_HW_RSA_SIGN */
133 };
134 
135 typedef enum {
141 
147  char *req_msg;
148  int method;
149  uint32_t client_ip;
150  char *url;
152  char *entity;
154 };
155 
160 typedef void (*http_cb_t)(struct http_client_t * client, struct http_req_message * msg);
161 
167  int port;
174 
175 #ifdef CONFIG_NET_SECURITY_TLS
176  int tls_init;
177  mbedtls_ssl_config tls_conf;
178  mbedtls_entropy_context tls_entropy;
179  mbedtls_ctr_drbg_context tls_ctr_drbg;
180  mbedtls_x509_crt tls_srvcert;
181  mbedtls_pk_context tls_pkey;
182  mbedtls_ssl_cache_context tls_cache;
183  mbedtls_net_context tls_ctx;
184 #endif
185 
186  struct sockaddr_in servaddr;
187  http_cb_t cb[4];
188  struct http_query_handler_t
189  *query_handlers[HTTP_CONF_MAX_QUERY_HANDLER_COUNT];
190 #ifdef CONFIG_NETUTILS_WEBSOCKET
191  websocket_cb_t ws_cb;
192 #endif
193 };
194 
195 /****************************************************************************
196  * Public Function Prototypes
197  ****************************************************************************/
198 
207 struct http_server_t *http_server_init(int port);
208 
217 int http_server_start(struct http_server_t *server);
218 
228 int http_server_stop(struct http_server_t *server);
229 
237 void http_server_release(struct http_server_t **server);
238 
254 int http_server_register_cb(struct http_server_t *server, int method, const char *url_format, http_cb_t func);
255 
270 int http_server_deregister_cb(struct http_server_t *server, int method, const char *url_format);
271 
285 int http_send_response(struct http_client_t *client, int status, const char *body, struct http_keyvalue_list_t *headers);
286 
287 #ifdef CONFIG_NET_SECURITY_TLS
288 
297 int http_tls_init(struct http_server_t *server, struct ssl_config_t *ssl_config);
298 #endif
299 
300 #endif
301 
unsigned int root_ca_len
Definition: http_server.h:129
http_server_state_t
Definition: http_server.h:135
mbedtls_x509_crt tls_srvcert
Definition: http_server.h:180
pthread_t tid
Definition: http_server.h:171
void(* http_cb_t)(struct http_client_t *client, struct http_req_message *msg)
typedef for callback function.
Definition: http_server.h:160
int http_server_stop(struct http_server_t *server)
http_server_stop() stops the webserver. Both HTTP server and HTTPS server are stoped by this function...
int http_server_deregister_cb(struct http_server_t *server, int method, const char *url_format)
http_server_deregister_cb() deregisters the cb function to each method on webserver.
struct http_keyvalue_list_t * headers
Definition: http_server.h:151
FAR struct mq_des * mqd_t
Definition: mqueue.h:96
mbedtls_pk_context tls_pkey
Definition: http_server.h:181
mbedtls_net_context tls_ctx
Definition: http_server.h:183
http server structure.
Definition: http_server.h:166
uint32_t client_ip
Definition: http_server.h:149
mbedtls_ctr_drbg_context tls_ctr_drbg
Definition: http_server.h:179
unsigned int private_key_len
Definition: http_server.h:131
sem_t sem_thread_sync
Definition: http_server.h:170
char * dev_cert
Definition: http_server.h:127
#define HTTP_CONF_MAX_QUERY_HANDLER_COUNT
Definition: http_server.h:101
int http_server_register_cb(struct http_server_t *server, int method, const char *url_format, http_cb_t func)
http_server_register_cb() registers the cb function to each method on webserver.
mbedtls_ssl_cache_context tls_cache
Definition: http_server.h:182
Mqueue APIs.
synchronous I/O multiplexing APIs
int http_server_start(struct http_server_t *server)
http_server_start() starts the webserver.
unsigned int dev_cert_len
Definition: http_server.h:130
mbedtls_entropy_context tls_entropy
Definition: http_server.h:178
Standard Input / Output APIs.
http request message.
Definition: http_server.h:146
http_server_state_t state
Definition: http_server.h:169
int http_tls_init(struct http_server_t *server, struct ssl_config_t *ssl_config)
http_tls_init() initializes the TLS configuere for webserver.
Structure of generic semaphore.
Definition: semaphore.h:115
int http_send_response(struct http_client_t *client, int status, const char *body, struct http_keyvalue_list_t *headers)
http_send_response() sends the response. If receive request, you must send a response by this functio...
mbedtls_ssl_config tls_conf
Definition: http_server.h:177
struct http_server_t * http_server_init(int port)
http_server_init() initializes the webserver.
http server ssl config structure.
Definition: http_server.h:119
#define HTTP_CONF_MAX_CLIENT_HANDLE
Definition: http_server.h:87
HTTP keyvalue linked list structure.
#define websocket_cb_t
Websocket structure wrapper to carry call back pointers.
Definition: websocket.h:168
Socket APIs.
void http_server_release(struct http_server_t **server)
http_server_release() releases the struct http_server_t.
char * private_key
Definition: http_server.h:128
char * root_ca
Definition: http_server.h:126
pid_t pthread_t
Definition: pthread.h:230