generic-uri.c 4.47 KB
Newer Older
1 2
/*
	belle-sip - SIP (RFC3261) library.
3
	Copyright (C) 2010-2018  Belledonne Communications SARL
4

5 6 7 8
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.
9

10 11 12 13
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
14

15 16
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

19 20
#include "grammars/belle_sip_messageLexer.h"
#include "grammars/belle_sip_messageParser.h"
21 22 23 24 25 26 27 28

#include "belle-sip/generic-uri.h"
#include "belle_sip_internal.h"




struct _belle_generic_uri {
jehan's avatar
jehan committed
29
	belle_sip_parameters_t parameters; /*just for parsing maddr*/
30 31 32 33 34 35 36
	char* scheme;
	char* user;
	char* user_password;
	char* host;
	int port;
	char* path;
	char* query;
37
	char* opaque_part;
38 39 40 41 42 43 44 45 46 47 48 49 50
};

void belle_generic_uri_init(belle_generic_uri_t *uri) {
	uri->port=-1;
}

static void belle_generic_uri_destroy(belle_generic_uri_t* uri) {
	DESTROY_STRING(uri,scheme)
	DESTROY_STRING(uri,user)
	DESTROY_STRING(uri,user_password)
	DESTROY_STRING(uri,host)
	DESTROY_STRING(uri,path)
	DESTROY_STRING(uri,query)
51
	DESTROY_STRING(uri,opaque_part)
52 53 54 55 56 57 58 59 60
}
static void belle_generic_uri_clone(belle_generic_uri_t* uri, const belle_generic_uri_t *orig){
	CLONE_STRING(belle_generic_uri,scheme,uri,orig)
	CLONE_STRING(belle_generic_uri,user,uri,orig)
	CLONE_STRING(belle_generic_uri,user_password,uri,orig)
	CLONE_STRING(belle_generic_uri,host,uri,orig)
	uri->port=orig->port;
	CLONE_STRING(belle_generic_uri,path,uri,orig)
	CLONE_STRING(belle_generic_uri,query,uri,orig)
61
	CLONE_STRING(belle_generic_uri,opaque_part,uri,orig)
62 63 64 65 66 67 68 69 70

}
belle_sip_error_code belle_generic_uri_marshal(const belle_generic_uri_t* uri, char* buff, size_t buff_size, size_t *offset) {
	belle_sip_error_code error=BELLE_SIP_OK;

	if (uri->scheme) {
		error=belle_sip_snprintf(buff,buff_size,offset,"%s:",uri->scheme);
		if (error!=BELLE_SIP_OK) return error;
	}
71 72
	if (uri->opaque_part) {
		error=belle_sip_snprintf(buff,buff_size,offset,"%s",uri->opaque_part);
73
		if (error!=BELLE_SIP_OK) return error;
74 75 76 77 78
	} else {
		if (uri->host) {
			error=belle_sip_snprintf(buff,buff_size,offset,"//");
			if (error!=BELLE_SIP_OK) return error;
		}
79

80 81 82 83 84
		if (uri->user) {
			char* escaped_username=belle_sip_uri_to_escaped_username(uri->user);
			error=belle_sip_snprintf(buff,buff_size,offset,"%s",escaped_username);
			belle_sip_free(escaped_username);
			if (error!=BELLE_SIP_OK) return error;
85

86 87 88 89 90 91 92
			if (uri->user_password) {
				char* escaped_password=belle_sip_uri_to_escaped_userpasswd(uri->user_password);
				error=belle_sip_snprintf(buff,buff_size,offset,":%s",escaped_password);
				belle_sip_free(escaped_password);
				if (error!=BELLE_SIP_OK) return error;
			}
			error=belle_sip_snprintf(buff,buff_size,offset,"@");
93 94
			if (error!=BELLE_SIP_OK) return error;

95
		}
96

97 98 99 100 101 102 103
		if (uri->host) {
			if (strchr(uri->host,':')) { /*ipv6*/
				error=belle_sip_snprintf(buff,buff_size,offset,"[%s]",uri->host);
			} else {
				error=belle_sip_snprintf(buff,buff_size,offset,"%s",uri->host);
			}
			if (error!=BELLE_SIP_OK) return error;
104 105
		}

106 107 108 109
		if (uri->port>0) {
			error=belle_sip_snprintf(buff,buff_size,offset,":%i",uri->port);
			if (error!=BELLE_SIP_OK) return error;
		}
110

111 112 113 114 115 116
		if (uri->path) {
			char* escaped_path=belle_generic_uri_to_escaped_path(uri->path);
			error=belle_sip_snprintf(buff,buff_size,offset,"%s",escaped_path);
			belle_sip_free(escaped_path);
			if (error!=BELLE_SIP_OK) return error;
		}
117

118 119 120 121 122 123
		if (uri->query) {
			char* escaped_query=belle_generic_uri_to_escaped_query(uri->query);
			error=belle_sip_snprintf(buff,buff_size,offset,"?%s",escaped_query);
			belle_sip_free(escaped_query);
			if (error!=BELLE_SIP_OK) return error;
		}
124 125 126 127 128 129 130 131 132 133 134
	}

	return BELLE_SIP_OK;
}

GET_SET_STRING(belle_generic_uri,scheme);
GET_SET_STRING(belle_generic_uri,user);
GET_SET_STRING(belle_generic_uri,user_password);
GET_SET_STRING(belle_generic_uri,host);
GET_SET_STRING(belle_generic_uri,path);
GET_SET_STRING(belle_generic_uri,query);
135
GET_SET_STRING(belle_generic_uri,opaque_part);
136
GET_SET_INT(belle_generic_uri,port,int)
jehan's avatar
jehan committed
137
BELLE_NEW(belle_generic_uri,belle_sip_parameters)
138
BELLE_PARSE(belle_sip_messageParser,belle_,generic_uri)
139 140 141 142 143


char*	belle_generic_uri_to_string(belle_generic_uri_t* uri)  {
	return  belle_sip_object_to_string(uri);
}