📄 urls.c
字号:
int len; int plen; char *tmp; *dest = NULL; 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) url->scheme = sgetcopy ("sip"); /* default is sipurl */ if (url->string != NULL) { buf = (char *) smalloc (strlen (url->scheme) + strlen (url->string) + 3); if (buf == NULL) return -1; *dest = buf; sprintf (buf, "%s:", url->scheme); buf = buf + strlen (url->scheme) + 1; sprintf (buf, "%s", url->string); buf = buf + strlen (url->string); return 0; } len = strlen (url->scheme) + 1 + strlen (url->host) + 5; if (url->username != NULL) len = len + strlen (url->username) + 10; /* count escaped char */ if (url->password != NULL) len = len + strlen (url->password) + 10; if (url->port != NULL) len = len + strlen (url->port) + 3; buf = (char *) smalloc (len); if (buf == NULL) return -1; tmp = buf; sprintf (tmp, "%s:", url->scheme); tmp = tmp + strlen (tmp); if (url->username != NULL) { char *tmp2 = url_escape_userinfo (url->username); sprintf (tmp, "%s", tmp2); sfree (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 = url_escape_password (url->password); sprintf (tmp, ":%s", tmp2); sfree (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; url_param_t *u_param; while (!list_eol (url->url_params, pos)) { char *tmp1; char *tmp2 = NULL; u_param = (url_param_t *) list_get (url->url_params, pos); tmp1 = url_escape_uri_param (u_param->gname); if (u_param->gvalue == NULL) plen = strlen (tmp1) + 2; else { tmp2 = url_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); sfree (tmp2); } sfree (tmp1); pos++; } } { int pos = 0; url_header_t *u_header; while (!list_eol (url->url_headers, pos)) { char *tmp1; char *tmp2; u_header = (url_header_t *) list_get (url->url_headers, pos); tmp1 = url_escape_header_param (u_header->gname); tmp2 = url_escape_header_param (u_header->gvalue); if (tmp1 == NULL || tmp2 == NULL) { sfree (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); sfree (tmp1); sfree (tmp2); pos++; } } *dest = buf; return 0;}voidurl_free (url_t * url){ int pos = 0; if (url == NULL) return; sfree (url->scheme); sfree (url->username); sfree (url->password); sfree (url->host); sfree (url->port); url_param_freelist (url->url_params); sfree (url->url_params); { url_header_t *u_header; while (!list_eol (url->url_headers, pos)) { u_header = (url_header_t *) list_get (url->url_headers, pos); list_remove (url->url_headers, pos); url_header_free (u_header); sfree (u_header); } sfree (url->url_headers); } sfree (url->string); url->scheme = NULL; url->username = NULL; url->password = NULL; url->host = NULL; url->port = NULL; url->url_params = NULL; url->url_headers = NULL; url->string = NULL;}inturl_clone (url_t * url, url_t ** dest){ int i; url_t *ur; *dest = NULL; if (url == NULL) return -1; if (url->host == NULL && url->string == NULL) return -1; i = url_init (&ur); if (i == -1) /* allocation failed */ return -1; if (url->scheme != NULL) ur->scheme = sgetcopy (url->scheme); if (url->username != NULL) ur->username = sgetcopy (url->username); if (url->password != NULL) ur->password = sgetcopy (url->password); if (url->host != NULL) ur->host = sgetcopy (url->host); if (url->port != NULL) ur->port = sgetcopy (url->port); if (url->string != NULL) ur->string = sgetcopy (url->string); { int pos = 0; url_param_t *u_param; url_param_t *dest_param; while (!list_eol (url->url_params, pos)) { u_param = (url_param_t *) list_get (url->url_params, pos); i = url_param_clone (u_param, &dest_param); if (i != 0) return -1; list_add (ur->url_params, dest_param, -1); pos++; } } { int pos = 0; url_param_t *u_param; url_param_t *dest_param; while (!list_eol (url->url_headers, pos)) { u_param = (url_param_t *) list_get (url->url_headers, pos); i = url_param_clone (u_param, &dest_param); if (i != 0) return -1; list_add (ur->url_headers, dest_param, -1); pos++; } } *dest = ur; return 0;}inturl_param_init (url_param_t ** url_param){ *url_param = (url_param_t *) smalloc (sizeof (url_param_t)); (*url_param)->gname = NULL; (*url_param)->gvalue = NULL; return 0;}voidurl_param_free (url_param_t * url_param){ sfree (url_param->gname); sfree (url_param->gvalue); url_param->gname = NULL; url_param->gvalue = NULL;}inturl_param_set (url_param_t * url_param, char *pname, char *pvalue){ url_param->gname = pname; /* not needed for url, but for all other generic params */ sclrspace (url_param->gname); url_param->gvalue = pvalue; if (url_param->gvalue != NULL) sclrspace (url_param->gvalue); return 0;}inturl_param_add (list_t * url_params, char *pname, char *pvalue){ int i; url_param_t *url_param; i = url_param_init (&url_param); if (i != 0) return -1; i = url_param_set (url_param, pname, pvalue); if (i != 0) { url_param_free (url_param); sfree (url_param); return -1; } list_add (url_params, url_param, -1); return 0;}voidurl_param_freelist (list_t * params){ url_param_t *u_param; while (!list_eol (params, 0)) { u_param = (url_param_t *) list_get (params, 0); list_remove (params, 0); url_param_free (u_param); sfree (u_param); }}inturl_param_getbyname (list_t * params, char *pname, url_param_t ** url_param){ int pos = 0; url_param_t *u_param; *url_param = NULL; while (!list_eol (params, pos)) { u_param = (url_param_t *) list_get (params, pos); if (strncmp (u_param->gname, pname, strlen (pname)) == 0) { *url_param = u_param; return 0; } pos++; } return -1;}inturl_param_clone (url_param_t * uparam, url_param_t ** dest){ int i; url_param_t *up; *dest = NULL; if (uparam == NULL) return -1; if (uparam->gname == NULL) return -1; /* name is mandatory */ i = url_param_init (&up); if (i != 0) /* allocation failed */ return -1; up->gname = sgetcopy (uparam->gname); if (uparam->gvalue != NULL) up->gvalue = sgetcopy (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 *url_escape_nonascii_and_nondef (const char *string, const char *def){ int alloc = strlen (string) + 1; int length; char *ns = smalloc (alloc); unsigned char in; int 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 *url_escape_userinfo (const char *string){ return url_escape_nonascii_and_nondef (string, userinfo_def);}/* user = *( unreserved / escaped / user-unreserved ) */const char *password_def = _MARK__PWORD_UNRESERVED_;char *url_escape_password (const char *string){ return url_escape_nonascii_and_nondef (string, password_def);}const char *uri_param_def = _MARK__URI_PARAM_UNRESERVED_;char *url_escape_uri_param (char *string){ return url_escape_nonascii_and_nondef (string, uri_param_def);}const char *header_param_def = _MARK__HEADER_PARAM_UNRESERVED_;char *url_escape_header_param (char *string){ return url_escape_nonascii_and_nondef (string, header_param_def);}voidurl_unescape (char *string){ int 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 */inturl_2char_canonical (url_t * url, char **dest){ int result; *dest = NULL; result = url_2char (url, dest); if (result == 0) { /* tmp = strchr(*dest, ";"); if (tmp !=NULL) { buf=strndup(*dest, tmp-(*dest)); sfree(*dest); *dest=buf; } */ url_unescape (*dest); } return result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -