generic-uri.c 4.47 KB
Newer Older
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2013  Belledonne Communications SARL, Grenoble, France

    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
7
    the Free Software Foundation, either version 2 of the License, or
8 9 10 11 12 13 14 15 16 17
    (at your option) any later version.

    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.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
18 19
#include "grammars/belle_sip_messageLexer.h"
#include "grammars/belle_sip_messageParser.h"
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

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




struct _belle_generic_uri {
	belle_sip_object_t object;
	char* scheme;
	char* user;
	char* user_password;
	char* host;
	int port;
	char* path;
	char* query;
36
	char* opaque_part;
37 38 39 40 41 42 43 44 45 46 47 48 49
};

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)
50
	DESTROY_STRING(uri,opaque_part)
51 52 53 54 55 56 57 58 59
}
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)
60
	CLONE_STRING(belle_generic_uri,opaque_part,uri,orig)
61 62 63 64 65 66 67 68 69

}
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;
	}
70 71
	if (uri->opaque_part) {
		error=belle_sip_snprintf(buff,buff_size,offset,"%s",uri->opaque_part);
72
		if (error!=BELLE_SIP_OK) return error;
73 74 75 76 77
	} else {
		if (uri->host) {
			error=belle_sip_snprintf(buff,buff_size,offset,"//");
			if (error!=BELLE_SIP_OK) return error;
		}
78

79 80 81 82 83
		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;
84

85 86 87 88 89 90 91
			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,"@");
92 93
			if (error!=BELLE_SIP_OK) return error;

94
		}
95

96 97 98 99 100 101 102
		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;
103 104
		}

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

110 111 112 113 114 115
		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;
		}
116

117 118 119 120 121 122
		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;
		}
123 124 125 126 127 128 129 130 131 132 133
	}

	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);
134
GET_SET_STRING(belle_generic_uri,opaque_part);
135 136
GET_SET_INT(belle_generic_uri,port,int)
BELLE_NEW(belle_generic_uri,belle_sip_object)
137
BELLE_PARSE(belle_sip_messageParser,belle_,generic_uri)
138 139 140 141 142


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