Asterisk - The Open Source Telephony Project  18.5.0
Functions
uri.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

struct ast_uriast_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)
 Copy the given uri replacing any value in the new uri with any given. More...
 
struct ast_uriast_uri_create (const char *scheme, const char *user_info, const char *host, const char *port, const char *path, const char *query)
 Create a uri with the given parameters. More...
 
const char * ast_uri_host (const struct ast_uri *uri)
 Retrieve the uri host. More...
 
int attribute_pure ast_uri_is_secure (const struct ast_uri *uri)
 Retrieve if the uri is of a secure type. More...
 
char * ast_uri_make_host_with_port (const struct ast_uri *uri)
 Retrieve a string of the host and port. More...
 
struct ast_uriast_uri_parse (const char *uri)
 Parse the given uri into a structure. More...
 
struct ast_uriast_uri_parse_http (const char *uri)
 Parse the given http uri into a structure. More...
 
struct ast_uriast_uri_parse_websocket (const char *uri)
 Parse the given websocket uri into a structure. More...
 
const char * ast_uri_path (const struct ast_uri *uri)
 Retrieve the uri path. More...
 
const char * ast_uri_port (const struct ast_uri *uri)
 Retrieve the uri port. More...
 
const char * ast_uri_query (const struct ast_uri *uri)
 Retrieve the uri query parameters. More...
 
const char * ast_uri_scheme (const struct ast_uri *uri)
 Retrieve the uri scheme. More...
 
const char * ast_uri_user_info (const struct ast_uri *uri)
 Retrieve the uri user information. More...
 

Function Documentation

◆ ast_uri_copy_replace()

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 
)

Copy the given uri replacing any value in the new uri with any given.

Parameters
urithe uri object to copy
schemethe uri scheme (ex: http)
user_infouser credentials (ex: <name><pass>)
hosthost name or ip address
portthe port
paththe path
queryquery parameters
Returns
a copy of the given uri with specified values replaced.
NULL on error
Since
13

Definition at line 101 of file uri.c.

References ast_uri_create(), ast_uri::host, ast_uri::path, ast_uri::port, ast_uri::query, ast_uri::scheme, and ast_uri::user_info.

Referenced by uri_parse_and_default().

105 {
106  return ast_uri_create(
107  scheme ? scheme : uri->scheme,
108  user_info ? user_info : uri->user_info,
109  host ? host : uri->host,
110  port ? port : uri->port,
111  path ? path : uri->path,
112  query ? query : uri->query);
113 }
char * scheme
Definition: uri.c:32
char * user_info
Definition: uri.c:34
char * port
Definition: uri.c:38
char * path
Definition: uri.c:40
static char host[256]
Definition: muted.c:77
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)
Create a uri with the given parameters.
Definition: uri.c:88
char * query
Definition: uri.c:42
char * host
Definition: uri.c:36

◆ ast_uri_create()

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 
)

Create a uri with the given parameters.

Parameters
schemethe uri scheme (ex: http)
user_infouser credentials (ex: <name><pass>)
hosthost name or ip address
portthe port
paththe path
queryquery parameters
Returns
a structure containing parsed uri data.
NULL on error
Since
13

Definition at line 88 of file uri.c.

References ast_uri_create_().

Referenced by ast_uri_copy_replace().

91 {
92  return ast_uri_create_(
93  scheme, scheme ? strlen(scheme) + 1 : 0,
94  user_info, user_info ? strlen(user_info) + 1 : 0,
95  host, host ? strlen(host) + 1 : 0,
96  port, port ? strlen(port) + 1 : 0,
97  path, path ? strlen(path) + 1 : 0,
98  query, query ? strlen(query) + 1 : 0);
99 }
char * scheme
Definition: uri.c:32
char * user_info
Definition: uri.c:34
char * port
Definition: uri.c:38
char * path
Definition: uri.c:40
static char host[256]
Definition: muted.c:77
char * query
Definition: uri.c:42
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)
Construct a uri object with the given values.
Definition: uri.c:54

◆ ast_uri_host()

const char* ast_uri_host ( const struct ast_uri uri)

Retrieve the uri host.

Returns
the uri host.
Since
13

Definition at line 125 of file uri.c.

References ast_uri::host.

Referenced by AST_TEST_DEFINE(), and ast_uri_make_host_with_port().

126 {
127  return uri->host;
128 }
char * host
Definition: uri.c:36

◆ ast_uri_is_secure()

int attribute_pure ast_uri_is_secure ( const struct ast_uri uri)

Retrieve if the uri is of a secure type.

Note
Secure types are recognized by an 's' at the end of the scheme.
Returns
True if secure, False otherwise.
Since
13

Definition at line 145 of file uri.c.

References ast_strlen_zero, and ast_uri::scheme.

Referenced by AST_TEST_DEFINE(), and uri_parse_and_default().

146 {
147  return ast_strlen_zero(uri->scheme) ? 0 :
148  *(uri->scheme + strlen(uri->scheme) - 1) == 's';
149 }
char * scheme
Definition: uri.c:32
#define ast_strlen_zero(foo)
Definition: strings.h:52

◆ ast_uri_make_host_with_port()

char* ast_uri_make_host_with_port ( const struct ast_uri uri)

Retrieve a string of the host and port.

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.
Parameters
urithe uri object
Returns
a string value of the host and optional port.
Since
13

Definition at line 300 of file uri.c.

References ast_malloc, ast_uri_host(), ast_uri_port(), and NULL.

Referenced by websocket_client_parse_uri().

301 {
302  int host_size = ast_uri_host(uri) ?
303  strlen(ast_uri_host(uri)) : 0;
304  /* if there is a port +1 for the colon */
305  int port_size = ast_uri_port(uri) ?
306  strlen(ast_uri_port(uri)) + 1 : 0;
307  char *res = ast_malloc(host_size + port_size + 1);
308 
309  if (!res) {
310  return NULL;
311  }
312 
313  memcpy(res, ast_uri_host(uri), host_size);
314 
315  if (ast_uri_port(uri)) {
316  res[host_size] = ':';
317  memcpy(res + host_size + 1,
318  ast_uri_port(uri), port_size - 1);
319  }
320 
321  res[host_size + port_size] = '\0';
322  return res;
323 }
#define NULL
Definition: resample.c:96
const char * ast_uri_port(const struct ast_uri *uri)
Retrieve the uri port.
Definition: uri.c:130
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
const char * ast_uri_host(const struct ast_uri *uri)
Retrieve the uri host.
Definition: uri.c:125

◆ ast_uri_parse()

struct ast_uri* ast_uri_parse ( const char *  uri)

Parse the given uri into a structure.

Note
Expects the following form: <scheme>://[user:pass@]<host>[:port][/<path>]
Parameters
uria string uri to parse
Returns
a structure containing parsed uri data.
NULL on error
Since
13

Definition at line 152 of file uri.c.

References ast_log, ast_uri_create_(), ast_uri::host, LOG_ERROR, NULL, ast_uri::path, ast_uri::port, ast_uri::query, ast_uri::scheme, state, ast_uri::uri, and ast_uri::user_info.

Referenced by AST_TEST_DEFINE(), and uri_parse_and_default().

153 {
154  UriParserStateA state;
155  UriUriA uria;
156  struct ast_uri *res;
157  unsigned int scheme_size, user_info_size, host_size;
158  unsigned int port_size, path_size, query_size;
159  const char *path_start, *path_end;
160 
161  state.uri = &uria;
162  if (uriParseUriA(&state, uri) != URI_SUCCESS) {
163  ast_log(LOG_ERROR, "Unable to parse URI %s\n", uri);
164  uriFreeUriMembersA(&uria);
165  return NULL;
166  }
167 
168  scheme_size = uria.scheme.first ?
169  uria.scheme.afterLast - uria.scheme.first + 1 : 0;
170  user_info_size = uria.userInfo.first ?
171  uria.userInfo.afterLast - uria.userInfo.first + 1 : 0;
172  host_size = uria.hostText.first ?
173  uria.hostText.afterLast - uria.hostText.first + 1 : 0;
174  port_size = uria.portText.first ?
175  uria.portText.afterLast - uria.portText.first + 1 : 0;
176 
177  path_start = uria.pathHead && uria.pathHead->text.first ?
178  uria.pathHead->text.first : NULL;
179  path_end = path_start ? uria.pathTail->text.afterLast : NULL;
180  path_size = path_end ? path_end - path_start + 1 : 0;
181 
182  query_size = uria.query.first ?
183  uria.query.afterLast - uria.query.first + 1 : 0;
184 
185  res = ast_uri_create_(uria.scheme.first, scheme_size,
186  uria.userInfo.first, user_info_size,
187  uria.hostText.first, host_size,
188  uria.portText.first, port_size,
189  path_start, path_size,
190  uria.query.first, query_size);
191  uriFreeUriMembersA(&uria);
192  return res;
193 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
Stores parsed uri information.
Definition: uri.c:30
#define NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
char uri[0]
Definition: uri.c:44
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)
Construct a uri object with the given values.
Definition: uri.c:54

◆ ast_uri_parse_http()

struct ast_uri* ast_uri_parse_http ( const char *  uri)

Parse the given http uri into a structure.

Note
Expects the following form:
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.
Parameters
urian http string uri to parse
Returns
a structure containing parsed http uri data.
NULL on error
Since
13

Definition at line 290 of file uri.c.

References uri_parse_and_default().

Referenced by AST_TEST_DEFINE().

291 {
292  return uri_parse_and_default(uri, "http", "80", "443");
293 }
char uri[0]
Definition: uri.c:44
static struct ast_uri * uri_parse_and_default(const char *uri, const char *scheme, const char *port, const char *secure_port)
Definition: uri.c:253

◆ ast_uri_parse_websocket()

struct ast_uri* ast_uri_parse_websocket ( const char *  uri)

Parse the given websocket uri into a structure.

Note
Expects the following form:
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.
Parameters
uria websocket string uri to parse
Returns
a structure containing parsed http uri data.
NULL on error
Since
13

Definition at line 295 of file uri.c.

References uri_parse_and_default().

Referenced by websocket_client_parse_uri().

296 {
297  return uri_parse_and_default(uri, "ws", "80", "443");
298 }
char uri[0]
Definition: uri.c:44
static struct ast_uri * uri_parse_and_default(const char *uri, const char *scheme, const char *port, const char *secure_port)
Definition: uri.c:253

◆ ast_uri_path()

const char* ast_uri_path ( const struct ast_uri uri)

Retrieve the uri path.

Returns
the uri path.
Since
13

Definition at line 135 of file uri.c.

References ast_uri::path.

Referenced by AST_TEST_DEFINE(), and websocket_client_parse_uri().

136 {
137  return uri->path;
138 }
char * path
Definition: uri.c:40

◆ ast_uri_port()

const char* ast_uri_port ( const struct ast_uri uri)

Retrieve the uri port.

Returns
the uri port.
Since
13

Definition at line 130 of file uri.c.

References ast_uri::port.

Referenced by AST_TEST_DEFINE(), ast_uri_make_host_with_port(), and uri_parse_and_default().

131 {
132  return uri->port;
133 }
char * port
Definition: uri.c:38

◆ ast_uri_query()

const char* ast_uri_query ( const struct ast_uri uri)

Retrieve the uri query parameters.

Returns
the uri query parameters.
Since
13

Definition at line 140 of file uri.c.

References ast_uri::query.

Referenced by AST_TEST_DEFINE(), and websocket_client_parse_uri().

141 {
142  return uri->query;
143 }
char * query
Definition: uri.c:42

◆ ast_uri_scheme()

const char* ast_uri_scheme ( const struct ast_uri uri)

Retrieve the uri scheme.

Returns
the uri scheme.
Since
13

Definition at line 115 of file uri.c.

References ast_uri::scheme.

Referenced by AST_TEST_DEFINE().

116 {
117  return uri->scheme;
118 }
char * scheme
Definition: uri.c:32

◆ ast_uri_user_info()

const char* ast_uri_user_info ( const struct ast_uri uri)

Retrieve the uri user information.

Returns
the uri user information.
Since
13

Definition at line 120 of file uri.c.

References ast_uri::user_info.

Referenced by AST_TEST_DEFINE().

121 {
122  return uri->user_info;
123 }
char * user_info
Definition: uri.c:34