Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
http.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2006, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19#ifndef _ASTERISK_HTTP_H
20#define _ASTERISK_HTTP_H
21
22#include "asterisk/config.h"
23#include "asterisk/tcptls.h"
25
26/*!
27 * \file
28 *
29 * \brief Support for Private Asterisk HTTP Servers.
30 *
31 * \note Note: The Asterisk HTTP servers are extremely simple and minimal and
32 * only support the "GET" method.
33 *
34 * \author Mark Spencer <markster@digium.com>
35 *
36 * \note In order to have TLS/SSL support, we need the openssl libraries.
37 * Still we can decide whether or not to use them by commenting
38 * in or out the DO_SSL macro.
39 * TLS/SSL support is basically implemented by reading from a config file
40 * (currently http.conf) the names of the certificate and cipher to use,
41 * and then run ssl_setup() to create an appropriate SSL_CTX (ssl_ctx)
42 * If we support multiple domains, presumably we need to read multiple
43 * certificates.
44 * When we are requested to open a TLS socket, we run make_file_from_fd()
45 * on the socket, to do the necessary setup. At the moment the context's name
46 * is hardwired in the function, but we can certainly make it into an extra
47 * parameter to the function.
48 * We declare most of ssl support variables unconditionally,
49 * because their number is small and this simplifies the code.
50 *
51 * \note: the ssl-support variables (ssl_ctx, do_ssl, certfile, cipher)
52 * and their setup should be moved to a more central place, e.g. asterisk.conf
53 * and the source files that processes it. Similarly, ssl_setup() should
54 * be run earlier in the startup process so modules have it available.
55 */
56
57/*! \brief HTTP Request methods known by Asterisk */
59 AST_HTTP_UNKNOWN = -1, /*!< Unknown response */
66 AST_HTTP_MAX_METHOD, /*!< Last entry in ast_http_method enum */
67};
68
69struct ast_http_uri;
70
71/*!
72 * \brief HTTP Callbacks
73 *
74 * \param ser TCP/TLS session object
75 * \param urih Registered URI handler struct for the URI.
76 * \param uri Remaining request URI path (also with the get_params removed).
77 * \param method enum ast_http_method GET, POST, etc.
78 * \param get_params URI argument list passed with the HTTP request.
79 * \param headers HTTP request header-name/value pair list
80 *
81 * \note Should use the ast_http_send() function for sending content
82 * allocated with ast_str and/or content from an opened file descriptor.
83 *
84 * Status and status text should be sent as arguments to the ast_http_send()
85 * function to reflect the status of the request (200 or 304, for example).
86 * Content length is calculated by ast_http_send() automatically.
87 *
88 * Static content may be indicated to the ast_http_send() function,
89 * to indicate that it may be cached.
90 *
91 * For a need authentication response, the ast_http_auth() function
92 * should be used.
93 *
94 * For an error response, the ast_http_error() function should be used.
95 *
96 * \retval 0 Continue and process the next HTTP request.
97 * \retval -1 Fatal HTTP connection error. Force the HTTP connection closed.
98 */
99typedef int (*ast_http_callback)(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers);
100
101/*! \brief Definition of a URI handler */
104 const char *description;
105 const char *uri;
106 const char *prefix;
108 unsigned int has_subtree:1;
109 /*! Structure is malloc'd */
110 unsigned int mallocd:1;
111 /*! Data structure is malloc'd */
112 unsigned int dmallocd:1;
113 /*! Don't automatically decode URI before passing it to the callback */
114 unsigned int no_decode_uri:1;
115 /*! Data to bind to the uri if needed */
116 void *data;
117 /*! Key to be used for unlinking if multiple URIs registered */
118 const char *key;
119};
120
121/*! \brief Get cookie from Request headers */
122struct ast_variable *ast_http_get_cookies(struct ast_variable *headers);
123
124/*! \brief HTTP authentication information. */
126 /*! Provided userid. */
127 char *userid;
128 /*! For Basic auth, the provided password. */
129 char *password;
130};
131
132/*!
133 * \brief Get HTTP authentication information from headers.
134 *
135 * The returned object is AO2 managed, so clean up with ao2_cleanup().
136 *
137 * \param headers HTTP request headers.
138 * \return HTTP auth structure.
139 * \retval NULL if no supported HTTP auth headers present.
140 * \since 12
141 */
142struct ast_http_auth *ast_http_get_auth(struct ast_variable *headers);
143
144/*!
145 * \brief Create an HTTP authorization header.
146 *
147 * The returned ast_variable must be freed with ast_variables_destroy()
148 *
149 * \param userid User ID or "<userid>:<password>".
150 * \param password Password if not in userid.
151 *
152 * \return ast_variable with name="Authorization" and value="Basic <base64enc>"
153 * \retval NULL if memory allocation failed.
154 */
155struct ast_variable *ast_http_create_basic_auth_header(const char *userid,
156 const char *password);
157
158/*! \brief Register a URI handler */
159int ast_http_uri_link(struct ast_http_uri *urihandler);
160
161/*! \brief Unregister a URI handler */
162void ast_http_uri_unlink(struct ast_http_uri *urihandler);
163
164/*! \brief Unregister all handlers with matching key */
165void ast_http_uri_unlink_all_with_key(const char *key);
166
167/*!
168 * \brief Return http method name string
169 * \since 1.8
170 */
172
173/*!
174 * \brief Return http method from string
175 */
177
178/*!
179 * \brief Return mime type based on extension
180 * \param ftype filename extension
181 * \return String containing associated MIME type
182 * \since 1.8
183 */
184const char *ast_http_ftype2mtype(const char *ftype) attribute_pure;
185
186/*!
187 * \brief Return manager id, if exist, from request headers
188 * \param headers List of HTTP headers
189 * \return 32-bit associated manager session identifier
190 * \since 1.8
191 */
193
194/*!
195 * \brief Generic function for sending HTTP/1.1 response.
196 * \param ser TCP/TLS session object
197 * \param method GET/POST/HEAD
198 * \param status_code HTTP response code (200/401/403/404/500)
199 * \param status_title English equivalent to the status_code parameter
200 * \param http_header An ast_str object containing all headers
201 * \param out An ast_str object containing the body of the response
202 * \param fd If out is NULL, a file descriptor where the body of the response is held (otherwise -1)
203 * \param static_content Zero if the content is dynamically generated and should not be cached; nonzero otherwise
204 *
205 * \note Function determines the HTTP response header from status_code,
206 * status_header, and http_header.
207 *
208 * Extra HTTP headers MUST be present only in the http_header argument. The
209 * argument "out" should contain only content of the response (no headers!).
210 *
211 * HTTP content can be constructed from the argument "out", if it is not NULL;
212 * otherwise, the function will read content from FD.
213 *
214 * This function calculates the content-length http header itself.
215 *
216 * Both the http_header and out arguments will be freed by this function;
217 * however, if FD is open, it will remain open.
218 *
219 * \since 1.8
220 */
222 int status_code, const char *status_title, struct ast_str *http_header,
223 struct ast_str *out, int fd, unsigned int static_content);
224
225/*!
226 * \brief Creates and sends a formatted http response message.
227 * \param ser TCP/TLS session object
228 * \param status_code HTTP response code (200/401/403/404/500)
229 * \param status_title English equivalent to the status_code parameter
230 * \param http_header_data The formatted text to use in the http header
231 * \param text Additional informational text to use in the
232 * response
233 *
234 * \note Function constructs response headers from the status_code, status_title and
235 * http_header_data parameters.
236 *
237 * The response body is created as HTML content, from the status_code,
238 * status_title, and the text parameters.
239 *
240 * The http_header_data parameter will be freed as a result of calling function.
241 *
242 * \since 13.2.0
243 */
244void ast_http_create_response(struct ast_tcptls_session_instance *ser, int status_code,
245 const char *status_title, struct ast_str *http_header_data, const char *text);
246
247/*! \brief Send http "401 Unauthorized" response and close socket */
248void ast_http_auth(struct ast_tcptls_session_instance *ser, const char *realm, const unsigned long nonce, const unsigned long opaque, int stale, const char *text);
249
250/*! \brief Send HTTP error message and close socket */
251void ast_http_error(struct ast_tcptls_session_instance *ser, int status, const char *title, const char *text);
252
253/*!
254 * \brief Return the current prefix
255 * \param[out] buf destination buffer for previous
256 * \param[in] len length of prefix to copy
257 * \since 1.6.1
258 */
259void ast_http_prefix(char *buf, int len);
260
261/*!
262 * \brief Request the HTTP connection be closed after this HTTP request.
263 * \since 12.4.0
264 *
265 * \param ser HTTP TCP/TLS session object.
266 *
267 * \note Call before ast_http_error() to make the connection close.
268 */
270
271/*!
272 * \brief Update the body read success status.
273 * \since 12.4.0
274 *
275 * \param ser HTTP TCP/TLS session object.
276 * \param read_success TRUE if body was read successfully.
277 */
278void ast_http_body_read_status(struct ast_tcptls_session_instance *ser, int read_success);
279
280/*!
281 * \brief Read and discard any unread HTTP request body.
282 * \since 12.4.0
283 *
284 * \param ser HTTP TCP/TLS session object.
285 *
286 * \retval 0 on success.
287 * \retval -1 on error.
288 */
290
291/*!
292 * \brief Get post variables from client Request Entity-Body, if content type is application/x-www-form-urlencoded.
293 * \param ser TCP/TLS session object
294 * \param headers List of HTTP headers
295 * \return List of variables within the POST body
296 * \note Since returned list is malloc'd, list should be free'd by the calling function
297 * \since 1.8
298 */
300
301/*!
302 * \brief Get post variables from an application/x-www-form-urlencoded buffer
303 * \param buf input buffer
304 * \param content_len Buffer length
305 * \param content_type Content type (must be "application/x-www-form-urlencoded")
306 *
307 * \warning The input buffer will be modified by strsep() so pass in a copy
308 * if you need to keep the original.
309 *
310 * \return List of ast_variables from the buffer. Must be freed with ast_variables_destroy().
311 */
312struct ast_variable *ast_http_parse_post_form(char *buf, int content_length,
313 const char *content_type);
314
315struct ast_json;
316
317/*!
318 * \brief Get JSON from client Request Entity-Body, if content type is
319 * application/json.
320 * \param ser TCP/TLS session object
321 * \param headers List of HTTP headers
322 * \return Parsed JSON content body
323 * \retval NULL on error, if no content, or if different content type.
324 * \since 12
325 */
327 struct ast_tcptls_session_instance *ser, struct ast_variable *headers);
328
329/*!\brief Parse the http response status line.
330 *
331 * \param buf the http response line information
332 * \param version the expected http version (e.g. HTTP/1.1)
333 * \param code the expected status code
334 * \retval -1 if version didn't match or status code conversion fails.
335 * \return status code (>0)
336 * \since 13
337 */
338int ast_http_response_status_line(const char *buf, const char *version, int code);
339
340/*!\brief Parse a header into the given name/value strings.
341 *
342 * \note This modifies the given buffer and the out parameters point (not
343 * allocated) to the start of the header name and header value,
344 * respectively.
345 *
346 * \param buf a string containing the name/value to point to
347 * \param[out] name header name
348 * \param[out] value header value
349 * \retval -1 if buf is empty
350 * \retval 0 if buf could be separated into name and value
351 * \retval 1 if name or value portion don't exist
352 * \since 13
353 */
354int ast_http_header_parse(char *buf, char **name, char **value);
355
356/*!\brief Check if the header and value match (case insensitive) their
357 * associated expected values.
358 *
359 * \param name header name to check
360 * \param expected_name the expected name of the header
361 * \param value header value to check
362 * \param expected_value the expected value of the header
363 * \retval 0 if the name and expected name do not match
364 * \retval -1 if the value and expected value do not match
365 * \retval 1 if the both the name and value match their expected value
366 * \since 13
367 */
368int ast_http_header_match(const char *name, const char *expected_name,
369 const char *value, const char *expected_value);
370
371/*!\brief Check if the header name matches the expected header name. If so,
372 * then check to see if the value can be located in the expected value.
373 *
374 * \note Both header and value checks are case insensitive.
375 *
376 * \param name header name to check
377 * \param expected_name the expected name of the header
378 * \param value header value to check if in expected value
379 * \param expected_value the expected value(s)
380 * \retval 0 if the name and expected name do not match
381 * \retval -1 if the value and is not in the expected value
382 * \retval 1 if the name matches expected name and value is in expected value
383 * \since 13
384 */
385int ast_http_header_match_in(const char *name, const char *expected_name,
386 const char *value, const char *expected_value);
387
388#ifdef TEST_FRAMEWORK
389
390/*!
391 * Currently multiple HTTP servers are only allowed when the TEST_FRAMEWORK
392 * is enabled, so putting this here:
393 *
394 * If a server is listening on 'any' (i.e. 0.0.0.0), and another server attempts
395 * to listen on 'localhost' on the same port (and vice versa) then you'll get an
396 * "Address already in use" error. For now use a different port, or match the
397 * addresses exactly.
398 */
399
400struct ast_http_server;
401
402/*!
403 * \brief Retrieve a HTTP server listening at the given host
404 *
405 * A given host can include the port, e.g. <host>[:<port>]. If no port is specified
406 * then the port defaults to '8088'. If a host parameter is NULL, or empty and a
407 * configured server is already listening then that server is returned. If no
408 * server is and enabled then the host defaults to 'localhost:8088'.
409 *
410 * \note When finished with a successfully returned server object
411 * ast_http_test_server_discard MUST be called on the object
412 * in order for proper 'cleanup' to occur.
413 *
414 * \param name Optional name for the server (default 'http test server')
415 * \param host Optional host, or address with port to bind to (default 'localhost:8088')
416 *
417 * \return a HTTP server object, or NULL on error
418 */
419struct ast_http_server *ast_http_test_server_get(const char *name, const char *host);
420
421/*!
422 * \brief Discard, or drop a HTTP server
423 *
424 * This function MUST eventually be called for every successful call to
425 * ast_http_test_server_get.
426 *
427 * \note NULL tolerant
428 *
429 * \param server The HTTP server to discard
430 */
431void ast_http_test_server_discard(struct ast_http_server *server);
432
433#endif
434
435#endif /* _ASTERISK_SRV_H */
jack_status_t status
Definition: app_jack.c:149
char * text
Definition: app_queue.c:1809
static char version[AST_MAX_EXTENSION]
Definition: chan_ooh323.c:391
#define attribute_pure
Definition: compiler.h:35
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static const char name[]
Definition: format_mp3.c:68
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_http_prefix(char *buf, int len)
Return the current prefix.
Definition: http.c:249
void ast_http_send(struct ast_tcptls_session_instance *ser, enum ast_http_method method, int status_code, const char *status_title, struct ast_str *http_header, struct ast_str *out, int fd, unsigned int static_content)
Generic function for sending HTTP/1.1 response.
Definition: http.c:472
struct ast_variable * ast_http_get_post_vars(struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
Get post variables from client Request Entity-Body, if content type is application/x-www-form-urlenco...
Definition: http.c:1405
struct ast_json * ast_http_get_json(struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
Get JSON from client Request Entity-Body, if content type is application/json.
Definition: http.c:1330
ast_http_method
HTTP Request methods known by Asterisk.
Definition: http.h:58
@ AST_HTTP_PUT
Definition: http.h:63
@ AST_HTTP_DELETE
Definition: http.h:64
@ AST_HTTP_POST
Definition: http.h:61
@ AST_HTTP_GET
Definition: http.h:60
@ AST_HTTP_MAX_METHOD
Definition: http.h:66
@ AST_HTTP_UNKNOWN
Definition: http.h:59
@ AST_HTTP_OPTIONS
Definition: http.h:65
@ AST_HTTP_HEAD
Definition: http.h:62
int ast_http_header_parse(char *buf, char **name, char **value)
Parse a header into the given name/value strings.
Definition: http.c:1764
struct ast_variable * ast_http_get_cookies(struct ast_variable *headers)
Get cookie from Request headers.
Definition: http.c:1564
int ast_http_header_match_in(const char *name, const char *expected_name, const char *value, const char *expected_value)
Check if the header name matches the expected header name. If so, then check to see if the value can ...
Definition: http.c:1802
const char * ast_http_ftype2mtype(const char *ftype) attribute_pure
Return mime type based on extension.
Definition: http.c:219
void ast_http_uri_unlink(struct ast_http_uri *urihandler)
Unregister a URI handler.
Definition: http.c:721
int ast_http_body_discard(struct ast_tcptls_session_instance *ser)
Read and discard any unread HTTP request body.
Definition: http.c:1135
enum ast_http_method ast_get_http_method_from_string(const char *method)
Return http method from string.
Definition: http.c:206
int(* ast_http_callback)(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers)
HTTP Callbacks.
Definition: http.h:99
struct ast_variable * ast_http_parse_post_form(char *buf, int content_length, const char *content_type)
Get post variables from an application/x-www-form-urlencoded buffer.
Definition: http.c:1369
struct ast_variable * ast_http_create_basic_auth_header(const char *userid, const char *password)
Create an HTTP authorization header.
Definition: http.c:1668
struct ast_http_auth * ast_http_get_auth(struct ast_variable *headers)
Get HTTP authentication information from headers.
Definition: http.c:1611
int ast_http_header_match(const char *name, const char *expected_name, const char *value, const char *expected_value)
Check if the header and value match (case insensitive) their associated expected values.
Definition: http.c:1786
uint32_t ast_http_manid_from_vars(struct ast_variable *headers) attribute_pure
Return manager id, if exist, from request headers.
Definition: http.c:233
int ast_http_response_status_line(const char *buf, const char *version, int code)
Parse the http response status line.
Definition: http.c:1712
const char * ast_get_http_method(enum ast_http_method method) attribute_pure
Return http method name string.
Definition: http.c:193
void ast_http_auth(struct ast_tcptls_session_instance *ser, const char *realm, const unsigned long nonce, const unsigned long opaque, int stale, const char *text)
Send http "401 Unauthorized" response and close socket.
Definition: http.c:638
void ast_http_request_close_on_completion(struct ast_tcptls_session_instance *ser)
Request the HTTP connection be closed after this HTTP request.
Definition: http.c:853
void ast_http_error(struct ast_tcptls_session_instance *ser, int status, const char *title, const char *text)
Send HTTP error message and close socket.
Definition: http.c:664
int ast_http_uri_link(struct ast_http_uri *urihandler)
Register a URI handler.
Definition: http.c:689
void ast_http_create_response(struct ast_tcptls_session_instance *ser, int status_code, const char *status_title, struct ast_str *http_header_data, const char *text)
Creates and sends a formatted http response message.
Definition: http.c:583
void ast_http_uri_unlink_all_with_key(const char *key)
Unregister all handlers with matching key.
Definition: http.c:728
void ast_http_body_read_status(struct ast_tcptls_session_instance *ser, int read_success)
Update the body read success status.
Definition: http.c:914
Configuration File Parser.
A set of macros to manage forward-linked lists.
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:410
const char * method
Definition: res_pjsip.c:1279
HTTP authentication information.
Definition: http.h:125
char * password
Definition: http.h:129
char * userid
Definition: http.h:127
Definition of a URI handler.
Definition: http.h:102
unsigned int has_subtree
Definition: http.h:108
unsigned int no_decode_uri
Definition: http.h:114
ast_http_callback callback
Definition: http.h:107
unsigned int dmallocd
Definition: http.h:112
const char * prefix
Definition: http.h:106
const char * description
Definition: http.h:104
const char * uri
Definition: http.h:105
void * data
Definition: http.h:116
const char * key
Definition: http.h:118
unsigned int mallocd
Definition: http.h:110
struct ast_http_uri::@230 entry
Abstract JSON element (object, array, string, int, ...).
Support for dynamic strings.
Definition: strings.h:623
describes a server instance
Definition: tcptls.h:151
Structure for variables, used for configurations and for channel variables.
int value
Definition: syslog.c:37
Generic support for tcp/tls servers in Asterisk.
FILE * out
Definition: utils/frame.c:33