mirror of https://github.com/asterisk/asterisk
Added a websocket server client in Asterisk. Asterisk has a websocket server, but not a client. The ability to have Asterisk be able to connect to a websocket server can potentially be useful for future work (for instance this could allow ARI to connect back to some external system, although more work would be needed in order to incorporate that). Also a couple of things to note - proxy connection support has not been implemented and there is limited http response code handling (basically, it is connect or not). Also added an initial new URI handling mechanism to core. Internet type URI's are parsed into a data structure that contains pointers to the various parts of the URI. (closes issue ASTERISK-23742) Reported by: Kevin Harwell Review: https://reviewboard.asterisk.org/r/3541/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@415223 65c4cc65-6c06-0410-ace0-fbb531ad65f3changes/97/197/1
parent
fd45b82247
commit
e763d70470
@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2014, Digium, Inc.
|
||||
*
|
||||
* Kevin Harwell <kharwell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef _ASTERISK_URI_H
|
||||
#define _ASTERISK_URI_H
|
||||
|
||||
/*! \brief Opaque structure that stores uri information. */
|
||||
struct ast_uri;
|
||||
|
||||
/*!
|
||||
* \brief Create a uri with the given parameters
|
||||
*
|
||||
* \param scheme the uri scheme (ex: http)
|
||||
* \param user_info user credentials (ex: <name>@<pass>)
|
||||
* \param host host name or ip address
|
||||
* \param port the port
|
||||
* \param path the path
|
||||
* \param query query parameters
|
||||
* \return a structure containing parsed uri data.
|
||||
* \return \c NULL on error
|
||||
* \since 13
|
||||
*/
|
||||
struct ast_uri *ast_uri_create(const char *scheme, const char *user_info,
|
||||
const char *host, const char *port,
|
||||
const char *path, const char *query);
|
||||
|
||||
/*!
|
||||
* \brief Copy the given uri replacing any value in the new uri with
|
||||
* any given.
|
||||
*
|
||||
* \param uri the uri object to copy
|
||||
* \param scheme the uri scheme (ex: http)
|
||||
* \param user_info user credentials (ex: <name>@<pass>)
|
||||
* \param host host name or ip address
|
||||
* \param port the port
|
||||
* \param path the path
|
||||
* \param query query parameters
|
||||
* \return a copy of the given uri with specified values replaced.
|
||||
* \return \c NULL on error
|
||||
* \since 13
|
||||
*/
|
||||
struct ast_uri *ast_uri_copy_replace(const struct ast_uri *uri, const char *scheme,
|
||||
const char *user_info, const char *host,
|
||||
const char *port, const char *path,
|
||||
const char *query);
|
||||
/*!
|
||||
* \brief Retrieve the uri scheme.
|
||||
*
|
||||
* \return the uri scheme.
|
||||
* \since 13
|
||||
*/
|
||||
const char *ast_uri_scheme(const struct ast_uri *uri);
|
||||
|
||||
/*!
|
||||
* \brief Retrieve the uri user information.
|
||||
*
|
||||
* \return the uri user information.
|
||||
* \since 13
|
||||
*/
|
||||
const char *ast_uri_user_info(const struct ast_uri *uri);
|
||||
|
||||
/*!
|
||||
* \brief Retrieve the uri host.
|
||||
*
|
||||
* \return the uri host.
|
||||
* \since 13
|
||||
*/
|
||||
const char *ast_uri_host(const struct ast_uri *uri);
|
||||
|
||||
/*!
|
||||
* \brief Retrieve the uri port
|
||||
*
|
||||
* \return the uri port.
|
||||
* \since 13
|
||||
*/
|
||||
const char *ast_uri_port(const struct ast_uri *uri);
|
||||
|
||||
/*!
|
||||
* \brief Retrieve the uri path.
|
||||
*
|
||||
* \return the uri path.
|
||||
* \since 13
|
||||
*/
|
||||
const char *ast_uri_path(const struct ast_uri *uri);
|
||||
|
||||
/*!
|
||||
* \brief Retrieve the uri query parameters.
|
||||
*
|
||||
* \return the uri query parameters.
|
||||
* \since 13
|
||||
*/
|
||||
const char *ast_uri_query(const struct ast_uri *uri);
|
||||
|
||||
/*!
|
||||
* \brief Retrieve if the uri is of a secure type
|
||||
*
|
||||
* \note Secure types are recognized by an 's' at the end
|
||||
* of the scheme.
|
||||
*
|
||||
* \return True if secure, False otherwise.
|
||||
* \since 13
|
||||
*/
|
||||
const int ast_uri_is_secure(const struct ast_uri *uri);
|
||||
|
||||
/*!
|
||||
* \brief Parse the given uri into a structure.
|
||||
*
|
||||
* \note Expects the following form:
|
||||
* <scheme>://[user:pass@]<host>[:port][/<path>]
|
||||
*
|
||||
* \param uri a string uri to parse
|
||||
* \return a structure containing parsed uri data.
|
||||
* \return \c NULL on error
|
||||
* \since 13
|
||||
*/
|
||||
struct ast_uri *ast_uri_parse(const char *uri);
|
||||
|
||||
/*!
|
||||
* \brief Parse the given http uri into a structure.
|
||||
*
|
||||
* \note Expects the following form:
|
||||
* [http[s]://][user:pass@]<host>[:port][/<path>]
|
||||
*
|
||||
* \note If no scheme is given it defaults to 'http' and if
|
||||
* no port is specified it will default to 443 if marked
|
||||
* secure, otherwise to 80.
|
||||
*
|
||||
* \param uri an http string uri to parse
|
||||
* \return a structure containing parsed http uri data.
|
||||
* \return \c NULL on error
|
||||
* \since 13
|
||||
*/
|
||||
struct ast_uri *ast_uri_parse_http(const char *uri);
|
||||
|
||||
/*!
|
||||
* \brief Parse the given websocket uri into a structure.
|
||||
*
|
||||
* \note Expects the following form:
|
||||
* [ws[s]://][user:pass@]<host>[:port][/<path>]
|
||||
*
|
||||
* \note If no scheme is given it defaults to 'ws' and if
|
||||
* no port is specified it will default to 443 if marked
|
||||
* secure, otherwise to 80.
|
||||
*
|
||||
* \param uri a websocket string uri to parse
|
||||
* \return a structure containing parsed http uri data.
|
||||
* \return \c NULL on error
|
||||
* \since 13
|
||||
*/
|
||||
struct ast_uri *ast_uri_parse_websocket(const char *uri);
|
||||
|
||||
/*!
|
||||
* \brief Retrieve a string of the host and port.
|
||||
*
|
||||
* \detail Combine the host and port (<host>:<port>) if the port
|
||||
* is available, otherwise just return the host.
|
||||
*
|
||||
* \note Caller is responsible for release the returned string.
|
||||
*
|
||||
* \param uri the uri object
|
||||
* \return a string value of the host and optional port.
|
||||
* \since 13
|
||||
*/
|
||||
char *ast_uri_make_host_with_port(const struct ast_uri *uri);
|
||||
|
||||
#endif /* _ASTERISK_URI_H */
|
@ -0,0 +1,321 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2014, Digium, Inc.
|
||||
*
|
||||
* Kevin Harwell <kharwell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
#include "asterisk/astobj2.h"
|
||||
#include "asterisk/strings.h"
|
||||
#include "asterisk/uri.h"
|
||||
|
||||
#ifdef HAVE_URIPARSER
|
||||
#include <uriparser/Uri.h>
|
||||
#endif
|
||||
|
||||
/*! \brief Stores parsed uri information */
|
||||
struct ast_uri {
|
||||
/*! scheme (e.g. http, https, ws, wss, etc...) */
|
||||
char *scheme;
|
||||
/*! username:password */
|
||||
char *user_info;
|
||||
/*! host name or address */
|
||||
char *host;
|
||||
/*! associated port */
|
||||
char *port;
|
||||
/*! path info following host[:port] */
|
||||
char *path;
|
||||
/*! query information */
|
||||
char *query;
|
||||
/*! storage for uri string */
|
||||
char uri[0];
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief Construct a uri object with the given values.
|
||||
*
|
||||
* \note The size parameters [should] include room for the string terminator
|
||||
* (strlen(<param>) + 1). For instance, if a scheme of 'http' is given
|
||||
* then the 'scheme_size' should be equal to 5.
|
||||
*/
|
||||
static struct ast_uri *ast_uri_create_(
|
||||
const char *scheme, unsigned int scheme_size,
|
||||
const char *user_info, unsigned int user_info_size,
|
||||
const char *host, unsigned int host_size,
|
||||
const char *port, unsigned int port_size,
|
||||
const char *path, unsigned int path_size,
|
||||
const char *query, unsigned int query_size)
|
||||
{
|
||||
#define SET_VALUE(param, field, size) \
|
||||
do { if (param) { \
|
||||
ast_copy_string(p, param, size); \
|
||||
field = p; \
|
||||
p += size; } } while (0)
|
||||
|
||||
char *p;
|
||||
struct ast_uri *res = ao2_alloc(
|
||||
sizeof(*res) + scheme_size + user_info_size + host_size +
|
||||
port_size + path_size + query_size, NULL);
|
||||
|
||||
if (!res) {
|
||||
ast_log(LOG_ERROR, "Unable to create URI object\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p = res->uri;
|
||||
SET_VALUE(scheme, res->scheme, scheme_size);
|
||||
SET_VALUE(user_info, res->user_info, user_info_size);
|
||||
SET_VALUE(host, res->host, host_size);
|
||||
SET_VALUE(port, res->port, port_size);
|
||||
SET_VALUE(path, res->path, path_size);
|
||||
SET_VALUE(query, res->query, query_size);
|
||||
return res;
|
||||
}
|
||||
|
||||
struct ast_uri *ast_uri_create(const char *scheme, const char *user_info,
|
||||
const char *host, const char *port,
|
||||
const char *path, const char *query)
|
||||
{
|
||||
return ast_uri_create_(
|
||||
scheme, scheme ? strlen(scheme) + 1 : 0,
|
||||
user_info, user_info ? strlen(user_info) + 1 : 0,
|
||||
host, host ? strlen(host) + 1 : 0,
|
||||
port, port ? strlen(port) + 1 : 0,
|
||||
path, path ? strlen(path) + 1 : 0,
|
||||
query, query ? strlen(query) + 1 : 0);
|
||||
}
|
||||
|
||||
struct ast_uri *ast_uri_copy_replace(const struct ast_uri *uri, const char *scheme,
|
||||
const char *user_info, const char *host,
|
||||
const char *port, const char *path,
|
||||
const char *query)
|
||||
{
|
||||
return ast_uri_create(
|
||||
scheme ? scheme : uri->scheme,
|
||||
user_info ? user_info : uri->user_info,
|
||||
host ? host : uri->host,
|
||||
port ? port : uri->port,
|
||||
path ? path : uri->path,
|
||||
query ? query : uri->query);
|
||||
}
|
||||
|
||||
const char *ast_uri_scheme(const struct ast_uri *uri)
|
||||
{
|
||||
return uri->scheme;
|
||||
}
|
||||
|
||||
const char *ast_uri_user_info(const struct ast_uri *uri)
|
||||
{
|
||||
return uri->user_info;
|
||||
}
|
||||
|
||||
const char *ast_uri_host(const struct ast_uri *uri)
|
||||
{
|
||||
return uri->host;
|
||||
}
|
||||
|
||||
const char *ast_uri_port(const struct ast_uri *uri)
|
||||
{
|
||||
return uri->port;
|
||||
}
|
||||
|
||||
const char *ast_uri_path(const struct ast_uri *uri)
|
||||
{
|
||||
return uri->path;
|
||||
}
|
||||
|
||||
const char *ast_uri_query(const struct ast_uri *uri)
|
||||
{
|
||||
return uri->query;
|
||||
}
|
||||
|
||||
const int ast_uri_is_secure(const struct ast_uri *uri)
|
||||
{
|
||||
return ast_strlen_zero(uri->scheme) ? 0 :
|
||||
*(uri->scheme + strlen(uri->scheme) - 1) == 's';
|
||||
}
|
||||
|
||||
#ifdef HAVE_URIPARSER
|
||||
struct ast_uri *ast_uri_parse(const char *uri)
|
||||
{
|
||||
UriParserStateA state;
|
||||
UriUriA uria;
|
||||
struct ast_uri *res;
|
||||
unsigned int scheme_size, user_info_size, host_size;
|
||||
unsigned int port_size, path_size, query_size;
|
||||
const char *path_start, *path_end;
|
||||
|
||||
state.uri = &uria;
|
||||
if (uriParseUriA(&state, uri) != URI_SUCCESS) {
|
||||
ast_log(LOG_ERROR, "Unable to parse URI %s\n", uri);
|
||||
uriFreeUriMembersA(&uria);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
scheme_size = uria.scheme.first ?
|
||||
uria.scheme.afterLast - uria.scheme.first + 1 : 0;
|
||||
user_info_size = uria.userInfo.first ?
|
||||
uria.userInfo.afterLast - uria.userInfo.first + 1 : 0;
|
||||
host_size = uria.hostText.first ?
|
||||
uria.hostText.afterLast - uria.hostText.first + 1 : 0;
|
||||
port_size = uria.portText.first ?
|
||||
uria.portText.afterLast - uria.portText.first + 1 : 0;
|
||||
|
||||
path_start = uria.pathHead && uria.pathHead->text.first ?
|
||||
uria.pathHead->text.first : NULL;
|
||||
path_end = path_start ? uria.pathTail->text.afterLast : NULL;
|
||||
path_size = path_end ? path_end - path_start + 1 : 0;
|
||||
|
||||
query_size = uria.query.first ?
|
||||
uria.query.afterLast - uria.query.first + 1 : 0;
|
||||
|
||||
res = ast_uri_create_(uria.scheme.first, scheme_size,
|
||||
uria.userInfo.first, user_info_size,
|
||||
uria.hostText.first, host_size,
|
||||
uria.portText.first, port_size,
|
||||
path_start, path_size,
|
||||
uria.query.first, query_size);
|
||||
uriFreeUriMembersA(&uria);
|
||||
return res;
|
||||
}
|
||||
#else
|
||||
struct ast_uri *ast_uri_parse(const char *uri)
|
||||
{
|
||||
#define SET_VALUES(value) \
|
||||
value = uri; \
|
||||
size_##value = p - uri + 1; \
|
||||
uri = p + 1;
|
||||
|
||||
const char *p, *scheme = NULL, *user_info = NULL, *host = NULL;
|
||||
const char *port = NULL, *path = NULL, *query = NULL;
|
||||
unsigned int size_scheme = 0, size_user_info = 0, size_host = 0;
|
||||
unsigned int size_port = 0, size_path = 0, size_query = 0;
|
||||
|
||||
if ((p = strstr(uri, "://"))) {
|
||||
scheme = uri;
|
||||
size_scheme = p - uri + 1;
|
||||
uri = p + 3;
|
||||
}
|
||||
|
||||
if ((p = strchr(uri, '@'))) {
|
||||
SET_VALUES(user_info);
|
||||
}
|
||||
|
||||
if ((p = strchr(uri, ':'))) {
|
||||
SET_VALUES(host);
|
||||
}
|
||||
|
||||
if ((p = strchr(uri, '/'))) {
|
||||
if (!host) {
|
||||
SET_VALUES(host);
|
||||
} else {
|
||||
SET_VALUES(port);
|
||||
}
|
||||
}
|
||||
|
||||
if ((p = strchr(uri, '?'))) {
|
||||
query = p + 1;
|
||||
size_query = strlen(query) + 1;
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
SET_VALUES(host);
|
||||
} else if (*(uri - 1) == ':') {
|
||||
SET_VALUES(port);
|
||||
} else if (*(uri - 1) == '/') {
|
||||
SET_VALUES(path);
|
||||
}
|
||||
|
||||
return ast_uri_create_(scheme, size_scheme,
|
||||
user_info, size_user_info,
|
||||
host, size_host,
|
||||
port, size_port,
|
||||
path, size_path,
|
||||
query, size_query);
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct ast_uri *uri_parse_and_default(const char *uri, const char *scheme,
|
||||
const char *port, const char *secure_port)
|
||||
{
|
||||
struct ast_uri *res;
|
||||
int len = strlen(scheme);
|
||||
|
||||
if (!strncmp(uri, scheme, len)) {
|
||||
res = ast_uri_parse(uri);
|
||||
} else {
|
||||
/* make room for <scheme>:// */
|
||||
char *with_scheme = ast_malloc(len + strlen(uri) + 4);
|
||||
if (!with_scheme) {
|
||||
ast_log(LOG_ERROR, "Unable to allocate uri '%s' with "
|
||||
"scheme '%s'", uri, scheme);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* safe - 'with_scheme' created with size equal to len of
|
||||
scheme plus length of uri plus space for extra characters
|
||||
'://' and terminator */
|
||||
sprintf(with_scheme, "%s://%s", scheme, uri);
|
||||
res = ast_uri_parse(with_scheme);
|
||||
ast_free(with_scheme);
|
||||
}
|
||||
|
||||
if (res && ast_strlen_zero(ast_uri_port(res))) {
|
||||
/* default the port if not given */
|
||||
struct ast_uri *tmp = ast_uri_copy_replace(
|
||||
res, NULL, NULL, NULL,
|
||||
ast_uri_is_secure(res) ? secure_port : port,
|
||||
NULL, NULL);
|
||||
ao2_ref(res, -1);
|
||||
res = tmp;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
struct ast_uri *ast_uri_parse_http(const char *uri)
|
||||
{
|
||||
return uri_parse_and_default(uri, "http", "80", "443");
|
||||
}
|
||||
|
||||
struct ast_uri *ast_uri_parse_websocket(const char *uri)
|
||||
{
|
||||
return uri_parse_and_default(uri, "ws", "80", "443");
|
||||
}
|
||||
|
||||
char *ast_uri_make_host_with_port(const struct ast_uri *uri)
|
||||
{
|
||||
int host_size = ast_uri_host(uri) ?
|
||||
strlen(ast_uri_host(uri)) : 0;
|
||||
/* if there is a port +1 for the colon */
|
||||
int port_size = ast_uri_port(uri) ?
|
||||
strlen(ast_uri_port(uri)) + 1 : 0;
|
||||
char *res = ast_malloc(host_size + port_size + 1);
|
||||
|
||||
if (!res) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(res, ast_uri_host(uri), host_size);
|
||||
|
||||
if (ast_uri_port(uri)) {
|
||||
res[host_size] = ':';
|
||||
memcpy(res + host_size + 1,
|
||||
ast_uri_port(uri), port_size);
|
||||
}
|
||||
|
||||
res[host_size + port_size + 1] = '\0';
|
||||
return res;
|
||||
}
|
@ -1,22 +1,6 @@
|
||||
{
|
||||
global:
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_add_protocol;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_remove_protocol;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_read;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_write;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_close;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_reconstruct_enable;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_reconstruct_disable;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_ref;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_unref;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_fd;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_remote_address;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_is_secure;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_set_nonblock;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_uri_cb;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_server_create;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_server_add_protocol;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_server_remove_protocol;
|
||||
LINKER_SYMBOL_PREFIX*ast_websocket_*;
|
||||
local:
|
||||
*;
|
||||
};
|
||||
|
@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2014, Digium, Inc.
|
||||
*
|
||||
* Kevin Harwell <kharwell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \brief URI Unit Tests
|
||||
*
|
||||
* \author Kevin Harwell <kharwell@digium.com>
|
||||
*
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<depend>TEST_FRAMEWORK</depend>
|
||||
<support_level>core</support_level>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "")
|
||||
|
||||
#include "asterisk/test.h"
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/uri.h"
|
||||
|
||||
#define CATEGORY "/main/uri/"
|
||||
|
||||
static const char *scenarios[][7] = {
|
||||
{"http://name:pass@localhost", "http", "name:pass", "localhost", NULL, NULL, NULL},
|
||||
{"http://localhost", "http", NULL, "localhost", NULL, NULL, NULL},
|
||||
{"http://localhost:80", "http", NULL, "localhost", "80", NULL, NULL},
|
||||
{"http://localhost/path/", "http", NULL, "localhost", NULL, "path/", NULL},
|
||||
{"http://localhost/?query", "http", NULL, "localhost", NULL, "", "query"},
|
||||
{"http://localhost:80/path", "http", NULL, "localhost", "80", "path", NULL},
|
||||
{"http://localhost:80/?query", "http", NULL, "localhost", "80", "", "query"},
|
||||
{"http://localhost:80/path?query", "http", NULL, "localhost", "80", "path", "query"},
|
||||
};
|
||||
|
||||
AST_TEST_DEFINE(uri_parse)
|
||||
{
|
||||
#define VALIDATE(value, expected_value) \
|
||||
do { ast_test_validate(test, \
|
||||
(value == expected_value) || \
|
||||
(value && expected_value && \
|
||||
!strcmp(value, expected_value))); \
|
||||
} while (0)
|
||||
|
||||
int i;
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = __func__;
|
||||
info->category = CATEGORY;
|
||||
info->summary = "Uri parsing scenarios";
|
||||
info->description = "For each scenario validate result(s)";
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
for (i = 0; i < ARRAY_LEN(scenarios); ++i) {
|
||||
RAII_VAR(struct ast_uri *, uri, NULL, ao2_cleanup);
|
||||
const char **scenario = scenarios[i];
|
||||
|
||||
ast_test_validate(test, (uri = ast_uri_parse(scenario[0])));
|
||||
VALIDATE(ast_uri_scheme(uri), scenario[1]);
|
||||
VALIDATE(ast_uri_user_info(uri), scenario[2]);
|
||||
VALIDATE(ast_uri_host(uri), scenario[3]);
|
||||
VALIDATE(ast_uri_port(uri), scenario[4]);
|
||||
VALIDATE(ast_uri_path(uri), scenario[5]);
|
||||
VALIDATE(ast_uri_query(uri), scenario[6]);
|
||||
}
|
||||
|
||||
return AST_TEST_PASS;
|
||||
}
|
||||
|
||||
AST_TEST_DEFINE(uri_default_http)
|
||||
{
|
||||
RAII_VAR(struct ast_uri *, uri, NULL, ao2_cleanup);
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = __func__;
|
||||
info->category = CATEGORY;
|
||||
info->summary = "parse an http uri with host only";
|
||||
info->description = info->summary;
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
ast_test_validate(test, (uri = ast_uri_parse_http("localhost")));
|
||||
ast_test_validate(test, !strcmp(ast_uri_scheme(uri), "http"));
|
||||
ast_test_validate(test, !strcmp(ast_uri_host(uri), "localhost"));
|
||||
ast_test_validate(test, !strcmp(ast_uri_port(uri), "80"));
|
||||
ast_test_validate(test, !ast_uri_is_secure(uri));
|
||||
|
||||
return AST_TEST_PASS;
|
||||
}
|
||||
|
||||
AST_TEST_DEFINE(uri_default_http_secure)
|
||||
{
|
||||
RAII_VAR(struct ast_uri *, uri, NULL, ao2_cleanup);
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = __func__;
|
||||
info->category = CATEGORY;
|
||||
info->summary = "parse an https uri with host only";
|
||||
info->description = info->summary;
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
ast_test_validate(test, (uri = ast_uri_parse_http("https://localhost")));
|
||||
ast_test_validate(test, !strcmp(ast_uri_scheme(uri), "https"));
|
||||
ast_test_validate(test, !strcmp(ast_uri_host(uri), "localhost"));
|
||||
ast_test_validate(test, !strcmp(ast_uri_port(uri), "443"));
|
||||
ast_test_validate(test, ast_uri_is_secure(uri));
|
||||
|
||||
return AST_TEST_PASS;
|
||||
}
|
||||
|
||||
static int load_module(void)
|
||||
{
|
||||
AST_TEST_REGISTER(uri_parse);
|
||||
AST_TEST_REGISTER(uri_default_http);
|
||||
AST_TEST_REGISTER(uri_default_http_secure);
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
static int unload_module(void)
|
||||
{
|
||||
AST_TEST_UNREGISTER(uri_default_http_secure);
|
||||
AST_TEST_UNREGISTER(uri_default_http);
|
||||
AST_TEST_UNREGISTER(uri_parse);
|
||||
return 0;
|
||||
}
|
||||
|
||||
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "URI test module");
|
@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2014, Digium, Inc.
|
||||
*
|
||||
* Kevin Harwell <kharwell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \brief Websocket Client Unit Tests
|
||||
*
|
||||
* \author Kevin Harwell <kharwell@digium.com>
|
||||
*
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<depend>TEST_FRAMEWORK</depend>
|
||||
<depend>res_http_websocket</depend>
|
||||
<support_level>core</support_level>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "")
|
||||
|
||||
#include "asterisk/test.h"
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/astobj2.h"
|
||||
#include "asterisk/pbx.h"
|
||||
#include "asterisk/http_websocket.h"
|
||||
|
||||
#define CATEGORY "/res/websocket/"
|
||||
#define REMOTE_URL "ws://localhost:8088/ws"
|
||||
|
||||
AST_TEST_DEFINE(websocket_client_create_and_connect)
|
||||
{
|
||||
RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
|
||||
|
||||
enum ast_websocket_result result;
|
||||
struct ast_str *write_buf;
|
||||
struct ast_str *read_buf;
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = __func__;
|
||||
info->category = CATEGORY;
|
||||
info->summary = "test creation and connection of a client websocket";
|
||||
info->description = "test creation and connection of a client websocket";
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
write_buf = ast_str_alloca(20);
|
||||
read_buf = ast_str_alloca(20);
|
||||
|
||||
ast_test_validate(test, (client = ast_websocket_client_create(
|
||||
REMOTE_URL, "echo", NULL, &result)));
|
||||
|
||||
ast_str_set(&write_buf, 0, "this is only a test");
|
||||
ast_test_validate(test, !ast_websocket_write_string(client, write_buf));
|
||||
ast_test_validate(test, ast_websocket_read_string(client, &read_buf) > 0);
|
||||
ast_test_validate(test, !strcmp(ast_str_buffer(write_buf), ast_str_buffer(read_buf)));
|
||||
|
||||
return AST_TEST_PASS;
|
||||
}
|
||||
|
||||
AST_TEST_DEFINE(websocket_client_bad_url)
|
||||
{
|
||||
RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
|
||||
enum ast_websocket_result result;
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = __func__;
|
||||
info->category = CATEGORY;
|
||||
info->summary = "websocket client - test bad url";
|
||||
info->description = "pass a bad url and make sure it fails";
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
ast_test_validate(test, !(client = ast_websocket_client_create(
|
||||
"invalid", NULL, NULL, &result)));
|
||||
return AST_TEST_PASS;
|
||||
}
|
||||
|
||||
AST_TEST_DEFINE(websocket_client_unsupported_protocol)
|
||||
{
|
||||
RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
|
||||
enum ast_websocket_result result;
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = __func__;
|
||||
info->category = CATEGORY;
|
||||
info->summary = "websocket client - unsupported protocol";
|
||||
info->description = "fails on an unsupported protocol";
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
ast_test_validate(test, !(client = ast_websocket_client_create(
|
||||
REMOTE_URL, "unsupported", NULL, &result)));
|
||||
return AST_TEST_PASS;
|
||||
}
|
||||
|
||||
AST_TEST_DEFINE(websocket_client_multiple_protocols)
|
||||
{
|
||||
RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
|
||||
const char *accept_protocol;
|
||||
enum ast_websocket_result result;
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = __func__;
|
||||
info->category = CATEGORY;
|
||||
info->summary = "websocket client - test multiple protocols";
|
||||
info->description = "test multi-protocol client";
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
ast_test_validate(test, (client = ast_websocket_client_create(
|
||||
REMOTE_URL, "echo,unsupported", NULL, &result)));
|
||||
|
||||
accept_protocol = ast_websocket_client_accept_protocol(client);
|
||||
ast_test_validate(test, accept_protocol && !strcmp(accept_protocol, "echo"));
|
||||
|
||||
return AST_TEST_PASS;
|
||||
}
|
||||
|
||||
static int load_module(void)
|
||||
{
|
||||
AST_TEST_REGISTER(websocket_client_create_and_connect);
|
||||
AST_TEST_REGISTER(websocket_client_bad_url);
|
||||
AST_TEST_REGISTER(websocket_client_unsupported_protocol);
|
||||
AST_TEST_REGISTER(websocket_client_multiple_protocols);
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
static int unload_module(void)
|
||||
{
|
||||
AST_TEST_UNREGISTER(websocket_client_multiple_protocols);
|
||||
AST_TEST_UNREGISTER(websocket_client_unsupported_protocol);
|
||||
AST_TEST_UNREGISTER(websocket_client_bad_url);
|
||||
AST_TEST_UNREGISTER(websocket_client_create_and_connect);
|
||||
return 0;
|
||||
}
|
||||
|
||||
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Websocket client test module");
|
Loading…
Reference in new issue