Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sol-http-server.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <sol-common-buildopts.h>
22 #include <sol-certificate.h>
23 #include <sol-http.h>
24 #include <sol-network.h>
25 #include <time.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
56 struct sol_http_server;
58 
65 typedef struct sol_http_server_config {
66 #ifndef SOL_NO_API_VERSION
67 #define SOL_HTTP_SERVER_CONFIG_API_VERSION (1)
71  uint16_t api_version;
72 #endif
73 
74  uint16_t port;
75  struct {
76  const struct sol_cert *cert;
77  const struct sol_cert *key;
78  } security;
80 
93 struct sol_http_request;
95 
108 
127 struct sol_http_server *sol_http_server_new(const struct sol_http_server_config *config);
128 
135 
148 int sol_http_server_register_handler(struct sol_http_server *server, const char *path,
149  int (*request_cb)(void *data, struct sol_http_request *request),
150  const void *data);
151 
160 int sol_http_server_unregister_handler(struct sol_http_server *server, const char *path);
161 
177 int sol_http_server_add_dir(struct sol_http_server *server, const char *basename, const char *rootdir);
178 
188 int sol_http_server_remove_dir(struct sol_http_server *server, const char *basename, const char *rootdir);
189 
190 #ifdef SOL_FEATURE_FILESYSTEM
191 
201 int sol_http_server_set_error_page(struct sol_http_server *server,
202  const enum sol_http_status_code error, const char *page);
203 
214 int sol_http_server_remove_error_page(struct sol_http_server *server,
215  const enum sol_http_status_code error);
216 
217 #endif /*SOL_FEATURE_FILESYSTEM*/
218 
235 int sol_http_server_set_last_modified(struct sol_http_server *server, const char *path, time_t modified);
236 
252 int sol_http_server_send_response(struct sol_http_request *request, struct sol_http_response *response);
253 
261 static inline int
263 {
264  const struct { const char *k; const char *v; } *itr, headers[] = {
265  { "Content-Type", "text/event-stream" },
266  { "Connection", "keep-alive" },
267  { "Cache-Control", "no-cache" },
268  { NULL, NULL }
269  };
270 
271  if (!response)
272  return -EINVAL;
273 
274  for (itr = headers; itr->k != NULL; itr++) {
275  if (sol_http_params_add(&response->param,
276  SOL_HTTP_REQUEST_PARAM_HEADER(itr->k, itr->v)) < 0) {
277  return -ENOMEM;
278  }
279  }
280 
281  return 0;
282 }
283 
290 #ifndef SOL_NO_API_VERSION
291 #define SOL_HTTP_SERVER_PROGRESSIVE_CONFIG_API_VERSION (1)
292 
296  uint16_t api_version;
297 #endif
298 
306  void (*on_feed_done)(void *data, struct sol_http_progressive_response *progressive, struct sol_blob *blob, int status);
313  void (*on_close)(void *data, const struct sol_http_progressive_response *progressive);
314  const void *user_data; /*<< User data to @c on_feed and @c on_close*/
330  size_t feed_size;
332 
354  const struct sol_http_response *response, const struct sol_http_server_progressive_config *config);
355 
374  struct sol_blob *blob);
375 
395  struct sol_blob *blob);
396 
412 void sol_http_progressive_response_del(struct sol_http_progressive_response *progressive, bool graceful_del);
413 
421 const char *sol_http_request_get_url(const struct sol_http_request *request);
422 
430 const struct sol_http_params *sol_http_request_get_params(const struct sol_http_request *request);
431 
440 
452 
460 int sol_http_request_get_client_address(const struct sol_http_request *request,
462 
476 int sol_http_server_set_buffer_size(struct sol_http_server *server, size_t buf_size);
477 
488 int sol_http_server_get_buffer_size(struct sol_http_server *server, size_t *buf_size);
489 
494 #ifdef __cplusplus
495 }
496 #endif
int sol_http_params_add(struct sol_http_params *params, struct sol_http_param_value value)
Add a new parameter to HTTP parameters vector.
Handle for an HTTP response.
Definition: sol-http.h:248
void(* on_feed_done)(void *data, struct sol_http_progressive_response *progressive, struct sol_blob *blob, int status)
Callback used to inform that a struct sol_blob was sent.
Definition: sol-http-server.h:306
struct sol_http_server_progressive_config sol_http_server_progressive_config
Progressive server response configuration.
struct sol_http_progressive_response sol_http_progressive_response
Opaque handler used for send data progressively.
Definition: sol-http-server.h:107
int sol_http_server_get_buffer_size(struct sol_http_server *server, size_t *buf_size)
Get the request buffer's size.
int sol_http_request_get_interface_address(const struct sol_http_request *request, struct sol_network_link_addr *address)
Gets the address of the interface that request comes.
int sol_http_server_send_response(struct sol_http_request *request, struct sol_http_response *response)
Send the response to request given in the callback registered on sol_http_server_register_handler().
uint16_t port
Definition: sol-http-server.h:74
size_t feed_size
The output buffer size - 0 means unlimited data.
Definition: sol-http-server.h:330
const struct sol_cert * key
Definition: sol-http-server.h:77
int sol_http_server_set_buffer_size(struct sol_http_server *server, size_t buf_size)
Set the buffer size for a request.
struct sol_cert sol_cert
Certificate handler.
Definition: sol-certificate.h:48
enum sol_http_method sol_http_request_get_method(const struct sol_http_request *request)
Gets the method (GET, POST, ...) from a given request.
static struct sol_http_server * server
Definition: server-https.c:52
void(* on_close)(void *data, const struct sol_http_progressive_response *progressive)
Callback used to inform that the client has closed the connection.
Definition: sol-http-server.h:313
const char * sol_http_request_get_url(const struct sol_http_request *request)
Gets the URL from a given request.
static int sol_http_response_set_sse_headers(struct sol_http_response *response)
Set the necessary headers to allow server sent events.
Definition: sol-http-server.h:262
#define SOL_HTTP_REQUEST_PARAM_HEADER(header_, content_)
Macro to set a struct sol_http_param_value with type SOL_HTTP_PARAM_HEADER, a header and content...
Definition: sol-http.h:438
static struct sol_network_link_addr address
Definition: echo-client.c:44
int sol_http_server_remove_dir(struct sol_http_server *server, const char *basename, const char *rootdir)
Removes a dir registered with sol_http_server_add_dir()
void sol_http_server_del(struct sol_http_server *server)
Destroy the server instance.
struct sol_http_server_config::@2 security
int sol_http_progressive_response_sse_feed(struct sol_http_progressive_response *progressive, struct sol_blob *blob)
Send sse data for the progressive response.
void sol_http_progressive_response_del(struct sol_http_progressive_response *progressive, bool graceful_del)
Delete the progressive response.
uint16_t api_version
api_version must match SOL_HTTP_REQUEST_INTERFACE_API_VERSION at runtime.
Definition: sol-http-server.h:296
struct sol_http_server sol_http_server
Opaque handler for an HTTP server instance.
Definition: sol-http-server.h:57
int sol_http_server_register_handler(struct sol_http_server *server, const char *path, int(*request_cb)(void *data, struct sol_http_request *request), const void *data)
Register a handler for a specific path.
struct sol_http_progressive_response * sol_http_server_send_progressive_response(struct sol_http_request *request, const struct sol_http_response *response, const struct sol_http_server_progressive_config *config)
Send the response and keep connection alive to request given in the callback registered on sol_http_s...
struct sol_http_params param
Definition: sol-http.h:257
sol_http_status_code
Status codes as defined by HTTP protocol.
Definition: sol-http.h:123
const struct sol_http_params * sol_http_request_get_params(const struct sol_http_request *request)
Gets the parameters from a given request.
It's created with sol_http_server_new() and should be later deleted with sol_http_server_del() ...
Definition: sol-http-server.h:65
int sol_http_server_set_last_modified(struct sol_http_server *server, const char *path, time_t modified)
Set the last time the specified path had its value modified.
struct sol_http_server * sol_http_server_new(const struct sol_http_server_config *config)
Creates an HTTP server, binding on all interfaces in the specified port.
sol_http_method
Type of HTTP method.
Definition: sol-http.h:47
Keep vector of HTTP parameters to be sent in a request.
Definition: sol-http.h:178
Data type describing the default blob implementation.
Definition: sol-types.h:468
int sol_http_progressive_response_feed(struct sol_http_progressive_response *progressive, struct sol_blob *blob)
Send data for the progressive response.
struct sol_http_request sol_http_request
Opaque handler for a request made to an HTTP server.
Definition: sol-http-server.h:94
int sol_http_request_get_client_address(const struct sol_http_request *request, struct sol_network_link_addr *address)
Gets the client address that made the request.
const void * user_data
Definition: sol-http-server.h:314
Progressive server response configuration.
Definition: sol-http-server.h:289
int sol_http_server_unregister_handler(struct sol_http_server *server, const char *path)
Removes a handler registered with sol_http_server_register_handler()
These routines are used for Soletta's certificate handling.
static int request_cb(void *data, struct sol_http_request *request)
Definition: server-https.c:56
int sol_http_server_add_dir(struct sol_http_server *server, const char *basename, const char *rootdir)
Add a root dir where the server will look for static files to serve.
struct sol_http_server_config sol_http_server_config
It's created with sol_http_server_new() and should be later deleted with sol_http_server_del() ...
These are routines that Soletta provides for handling network link interfaces, making it possible to ...
const struct sol_cert * cert
Definition: sol-http-server.h:76