📄 osip_uri.c
字号:
if (url == NULL) return -1; if (url->host == NULL && url->string == NULL) return -1; if (url->scheme == NULL && url->string != NULL) return -1; if (url->string == NULL && url->scheme == NULL) scheme = "sip"; /* default is sipurl */ else scheme = url->scheme; if (url->string != NULL) { buf = (char *) osip_malloc (strlen (scheme) + strlen (url->string) + 3); if (buf == NULL) return -1; *dest = buf; sprintf (buf, "%s:", scheme); buf = buf + strlen (scheme) + 1; sprintf (buf, "%s", url->string); buf = buf + strlen (url->string); return 0; } len = strlen (scheme) + 1 + strlen (url->host) + 5; if (url->username != NULL) len = len + (strlen (url->username) * 3) + 1; /* count escaped char */ if (url->password != NULL) len = len + (strlen (url->password) * 3) + 1; if (url->port != NULL) len = len + strlen (url->port) + 3; buf = (char *) osip_malloc (len); if (buf == NULL) return -1; tmp = buf; sprintf (tmp, "%s:", scheme); tmp = tmp + strlen (tmp); if (url->username != NULL) { char *tmp2 = __osip_uri_escape_userinfo (url->username); sprintf (tmp, "%s", tmp2); osip_free (tmp2); tmp = tmp + strlen (tmp); } if ((url->password != NULL) && (url->username != NULL)) { /* be sure that when a password is given, a username is also given */ char *tmp2 = __osip_uri_escape_password (url->password); sprintf (tmp, ":%s", tmp2); osip_free (tmp2); tmp = tmp + strlen (tmp); } if (url->username != NULL) { /* we add a '@' only when username is present... */ sprintf (tmp, "@"); tmp++; } if (strchr (url->host, ':') != NULL) { sprintf (tmp, "[%s]", url->host); tmp = tmp + strlen (tmp); } else { sprintf (tmp, "%s", url->host); tmp = tmp + strlen (tmp); } if (url->port != NULL) { sprintf (tmp, ":%s", url->port); tmp = tmp + strlen (tmp); } { int pos = 0; osip_uri_param_t *u_param; while (!osip_list_eol (url->url_params, pos)) { char *tmp1; char *tmp2 = NULL; u_param = (osip_uri_param_t *) osip_list_get (url->url_params, pos); tmp1 = __osip_uri_escape_uri_param (u_param->gname); if (u_param->gvalue == NULL) plen = strlen (tmp1) + 2; else { tmp2 = __osip_uri_escape_uri_param (u_param->gvalue); plen = strlen (tmp1) + strlen (tmp2) + 3; } len = len + plen; buf = (char *) realloc (buf, len); tmp = buf; tmp = tmp + strlen (tmp); if (u_param->gvalue == NULL) sprintf (tmp, ";%s", tmp1); else { sprintf (tmp, ";%s=%s", tmp1, tmp2); osip_free (tmp2); } osip_free (tmp1); pos++; } } { int pos = 0; osip_uri_header_t *u_header; while (!osip_list_eol (url->url_headers, pos)) { char *tmp1; char *tmp2; u_header = (osip_uri_header_t *) osip_list_get (url->url_headers, pos); tmp1 = __osip_uri_escape_header_param (u_header->gname); tmp2 = __osip_uri_escape_header_param (u_header->gvalue); if (tmp1 == NULL || tmp2 == NULL) { osip_free (buf); return -1; } plen = strlen (tmp1) + strlen (tmp2) + 4; len = len + plen; buf = (char *) realloc (buf, len); tmp = buf; tmp = tmp + strlen (tmp); if (pos == 0) { sprintf (tmp, "?%s=%s", u_header->gname, u_header->gvalue); } else sprintf (tmp, "&%s=%s", u_header->gname, u_header->gvalue); osip_free (tmp1); osip_free (tmp2); pos++; } } *dest = buf; return 0;}voidosip_uri_free (osip_uri_t * url){ int pos = 0; if (url == NULL) return; osip_free (url->scheme); osip_free (url->username); osip_free (url->password); osip_free (url->host); osip_free (url->port); osip_uri_param_freelist (url->url_params); { osip_uri_header_t *u_header; while (!osip_list_eol (url->url_headers, pos)) { u_header = (osip_uri_header_t *) osip_list_get (url->url_headers, pos); osip_list_remove (url->url_headers, pos); osip_uri_header_free (u_header); } osip_free (url->url_headers); } osip_free (url->string); osip_free (url);}intosip_uri_clone (const osip_uri_t * url, osip_uri_t ** dest){ int i; osip_uri_t *ur; *dest = NULL; if (url == NULL) return -1; if (url->host == NULL && url->string == NULL) return -1; i = osip_uri_init (&ur); if (i == -1) /* allocation failed */ return -1; if (url->scheme != NULL) ur->scheme = osip_strdup (url->scheme); if (url->username != NULL) ur->username = osip_strdup (url->username); if (url->password != NULL) ur->password = osip_strdup (url->password); if (url->host != NULL) ur->host = osip_strdup (url->host); if (url->port != NULL) ur->port = osip_strdup (url->port); if (url->string != NULL) ur->string = osip_strdup (url->string); { int pos = 0; osip_uri_param_t *u_param; osip_uri_param_t *dest_param; while (!osip_list_eol (url->url_params, pos)) { u_param = (osip_uri_param_t *) osip_list_get (url->url_params, pos); i = osip_uri_param_clone (u_param, &dest_param); if (i != 0) return -1; osip_list_add (ur->url_params, dest_param, -1); pos++; } } { int pos = 0; osip_uri_param_t *u_param; osip_uri_param_t *dest_param; while (!osip_list_eol (url->url_headers, pos)) { u_param = (osip_uri_param_t *) osip_list_get (url->url_headers, pos); i = osip_uri_param_clone (u_param, &dest_param); if (i != 0) return -1; osip_list_add (ur->url_headers, dest_param, -1); pos++; } } *dest = ur; return 0;}intosip_uri_param_init (osip_uri_param_t ** url_param){ *url_param = (osip_uri_param_t *) osip_malloc (sizeof (osip_uri_param_t)); (*url_param)->gname = NULL; (*url_param)->gvalue = NULL; return 0;}voidosip_uri_param_free (osip_uri_param_t * url_param){ osip_free (url_param->gname); osip_free (url_param->gvalue); osip_free (url_param);}intosip_uri_param_set (osip_uri_param_t * url_param, char *pname, char *pvalue){ url_param->gname = pname; /* not needed for url, but for all other generic params */ osip_clrspace (url_param->gname); url_param->gvalue = pvalue; if (url_param->gvalue != NULL) osip_clrspace (url_param->gvalue); return 0;}intosip_uri_param_add (osip_list_t * url_params, char *pname, char *pvalue){ int i; osip_uri_param_t *url_param; i = osip_uri_param_init (&url_param); if (i != 0) return -1; i = osip_uri_param_set (url_param, pname, pvalue); if (i != 0) { osip_uri_param_free (url_param); return -1; } osip_list_add (url_params, url_param, -1); return 0;}voidosip_uri_param_freelist (osip_list_t * params){ osip_uri_param_t *u_param; while (!osip_list_eol (params, 0)) { u_param = (osip_uri_param_t *) osip_list_get (params, 0); osip_list_remove (params, 0); osip_uri_param_free (u_param); } osip_free (params);}intosip_uri_param_get_byname (osip_list_t * params, char *pname, osip_uri_param_t ** url_param){ int pos = 0; int pname_len; osip_uri_param_t *u_param; *url_param = NULL; if (pname==NULL) return -1; pname_len = strlen(pname); if (pname_len<=0) return -1; while (!osip_list_eol (params, pos)) { int len; u_param = (osip_uri_param_t *) osip_list_get (params, pos); len = strlen(u_param->gname); if (pname_len == len && osip_strncasecmp (u_param->gname, pname, strlen (pname)) == 0) { *url_param = u_param; return 0; } pos++; } return -1;}intosip_uri_param_clone (const osip_uri_param_t * uparam, osip_uri_param_t ** dest){ int i; osip_uri_param_t *up; *dest = NULL; if (uparam == NULL) return -1; if (uparam->gname == NULL) return -1; /* name is mandatory */ i = osip_uri_param_init (&up); if (i != 0) /* allocation failed */ return -1; up->gname = osip_strdup (uparam->gname); if (uparam->gvalue != NULL) up->gvalue = osip_strdup (uparam->gvalue); else up->gvalue = NULL; *dest = up; return 0;}#define _ALPHANUM_ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\0"#define _RESERVED_ ";/?:@&=+$\0"#define _MARK_ "-_.!~*'()\0"#define _MARK__USER_UNRESERVED_ "-_.!~*'()&=+$,;?/\0"#define _MARK__PWORD_UNRESERVED_ "-_.!~*'()&=+$,\0"#define _MARK__URI_PARAM_UNRESERVED_ "-_.!~*'()[]/:&+$\0"#define _MARK__HEADER_PARAM_UNRESERVED_ "-_.!~*'()[]/?:+$\0"#define osip_is_alphanum(in) ( \ (in >= 'a' && in <= 'z') || \ (in >= 'A' && in <= 'Z') || \ (in >= '0' && in <= '9'))char *__osip_uri_escape_nonascii_and_nondef (const char *string, const char *def){ size_t alloc = strlen (string) + 1; size_t length; char *ns = osip_malloc (alloc); unsigned char in; size_t newlen = alloc; int index = 0; const char *tmp; int i; length = alloc - 1; while (length--) { in = *string; i = 0; tmp = NULL; if (osip_is_alphanum (in)) tmp = string; else { for (; def[i] != '\0' && def[i] != in; i++) { } if (def[i] != '\0') tmp = string; } if (tmp == NULL) { /* encode it */ newlen += 2; /* the size grows with two, since this'll become a %XX */ if (newlen > alloc) { alloc *= 2; ns = realloc (ns, alloc); if (!ns) return NULL; } sprintf (&ns[index], "%%%02X", in); index += 3; } else { /* just copy this */ ns[index++] = in; } string++; } ns[index] = 0; /* terminate it */ return ns;}/* user = *( unreserved / escaped / user-unreserved ) */const char *userinfo_def = /* implied _ALPHANUM_ */ _MARK__USER_UNRESERVED_;char *__osip_uri_escape_userinfo (const char *string){ return __osip_uri_escape_nonascii_and_nondef (string, userinfo_def);}/* user = *( unreserved / escaped / user-unreserved ) */const char *password_def = _MARK__PWORD_UNRESERVED_;char *__osip_uri_escape_password (const char *string){ return __osip_uri_escape_nonascii_and_nondef (string, password_def);}const char *uri_param_def = _MARK__URI_PARAM_UNRESERVED_;char *__osip_uri_escape_uri_param (char *string){ return __osip_uri_escape_nonascii_and_nondef (string, uri_param_def);}const char *header_param_def = _MARK__HEADER_PARAM_UNRESERVED_;char *__osip_uri_escape_header_param (char *string){ return __osip_uri_escape_nonascii_and_nondef (string, header_param_def);}void__osip_uri_unescape (char *string){ size_t alloc = strlen (string) + 1; unsigned char in; int index = 0; unsigned int hex; char *ptr; ptr = string; while (--alloc > 0) { in = *ptr; if ('%' == in) { /* encoded part */ if (sscanf (ptr + 1, "%02X", &hex)) { in = (unsigned char) hex; ptr += 2; alloc -= 2; } } string[index++] = in; ptr++; } string[index] = 0; /* terminate it */}/* Robin Nayathodan <roooot@softhome.net> N.K Electronics INDIA RFC3261 16.5 */intosip_uri_to_str_canonical (const osip_uri_t * url, char **dest){ int result; *dest = NULL; result = osip_uri_to_str (url, dest); if (result == 0) { /* tmp = strchr(*dest, ";"); if (tmp !=NULL) { buf=strndup(*dest, tmp-(*dest)); osip_free(*dest); *dest=buf; } */ __osip_uri_unescape (*dest); } return result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -