URI Dissector

The URI dissector is a helper class to dissect an URI into its different components.

File information

Filecommon/ilib/uri.h

Classes uri_dissector

Examples URI dissection

Classes

uri_dissector

class uri_dissector {
public:

	char *scheme, *e_scheme;      /* e.g.: http */
	char *hier, *e_hier;          /* hierarchy e.g.: // */
	char *usr, *e_usr;            /* user */
	char *pwd, *e_pwd;            /* password */
	char *ip, *e_ip;              /* host part is an ip address */
	char *fqdn, *e_fqdn;          /* host part is a fully qualified domain name */
	char *port, *e_port;          /* port past ip/fqdn */
	char *path, *e_path;
	char *file, *e_file;
	char *anchor, *e_anchor;      /* eg: #bla */
	char *query, *e_query;        /* '?' cgi query string */
	char *params, *e_params;      /* ';' host parameters */

	uri_dissector();

	bool dissect_uri(char *uri);
	bool path_to_argv(char *buf, int sz, int &argc, char * argv[]);
	bool querystring_to_argv(char *buf, int sz, int &argc, char * argv[]);

#define COMPOSE_SCHEME  0x01
#define COMPOSE_HIER    0x02
#define COMPOSE_USR     0x04
#define COMPOSE_PWD     0x08
#define COMPOSE_HOST    0x10
#define COMPOSE_PORT    0x20
#define COMPOSE_PATH    0x40
#define COMPOSE_FILE    0x80
#define COMPOSE_ANCHOR  0x100
#define COMPOSE_QUERY   0x200
#define COMPOSE_PARAMS  0x400

#define COMPOSE_ALL     0xFFFFFFFF
#define COMPOSE_ALL_NO_USERPWD COMPOSE_ALL&~COMPOSE_USR&~COMPOSE_PWD

	char* compose(char *buf, dword len_buf, dword flags = COMPOSE_ALL);
};

Overview

The uri_dissector class is used to dissect an URI into its compontents.

Public functions

uri_dissector
Creates the uri_dissector instance.
dissect_uri
Dissects the uri and sets the public variables like scheme, e_scheme etc.

Parameters

char * uriThe uri which is dissected. The char * buffer is not modified!

Return value

True if the dissection was successfull, otherwise false.
path_to_argv
Splits the path parts into to argv array.

Parameters

char * bufThis buffer will be used for the arguments of the argv array.
int szThe size of buf.
int &argcThe initial element count of the argv array. Will contain the actual element count after successfull completion.
char *argv[]The array which will contain the different parts of the path

Return value

True if the buffer was large enough and argv had enough elements. Otherwise false.
querystring_to_argv
Splits the query string into the argv array, while argv[0] is a name and argv[1] is a value and so on.

Parameters

char * bufThis buffer will be used for the arguments of the argv array.
int szThe size of buf.
int &argcThe initial element count of the argv array. Will contain the actual element count after successfull completion.
char *argv[]The array which will contain the different parts of the query. The first element contains the name, the second the value and so on.

Return value

True if the buffer was large enough and argv had enough elements. Otherwise false.
compose
Writes the different parts from the URI to a buffer. You can use the COMPOSE_* flags to specify the parts you want.

Parameters

char * bufThis buffer will be used for the arguments of the argv array.
dword len_bufThe length of buf.
dword flagsSpecifies the parts to compose. The default value is COMPOSE_ALL.

Return value

Returns buf again.

Public Members

char * scheme; char * e_schemeStart and end pointer of the scheme, e.g. http.
char * hier; char * e_hierStart and end pointer of the hierarchy, e.g. //.
char * usr; char * e_usrStart and end pointer of the user.
char * pwd; char * e_pwdStart and end pointer of the password.
char * ip; char * e_ipStart and end pointer of the IP address.
char * fqdn; char * e_fqdnStart and end pointer of the domain name (if there is no IP address).
char * port; char * e_portStart and end pointer of the port.
char * path; char * e_pathStart and end pointer of the path.
char * file; char * e_fileStart and end pointer of the file after the path.
char * anchor; char * e_anchorStart and end pointer of the anchor, e.g. #test.
char * query; char * e_queryStart and end pointer of the query string, e.g. ?test=123.
char * params; char * e_paramsStart and end pointer of the host parameters.

Code Example

URI dissection

uri_dissector uri;
uri.dissect_uri("http://admin:ip800@172.16.16.43/bla#funny?cgpn=73&cdpn=123");
debug->printf("%.*s", uri.e_user - uri.user, uri.user); // prints "admin"

char buf[256];                                                                       
uri.dissect_uri("sip:1234@anywhere.de:8888;user=phone");                             
uri.compose(buf,sizeof(buf),COMPOSE_PARAMS);   // yields "user=phone"                
uri.compose(buf,sizeof(buf),COMPOSE_USR);      // yields "1234"                      
uri.compose(buf,sizeof(buf),COMPOSE_HOST);     // yields "anywhere.de"               
uri.compose(buf,sizeof(buf),COMPOSE_PORT);     // yields "8888"