and added files
[bcm963xx.git] / userapps / opensource / libosip2 / src / test / turls.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc2543-)
3   Copyright (C) 2001  Aymeric MOIZARD jack@atosc.org
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20
21 #ifdef ENABLE_MPATROL
22 #include <mpatrol.h>
23 #endif
24
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <osip2/internal.h>
29 #include <osipparser2/osip_uri.h>
30
31 int osip_uri_test_accessor_api (osip_uri_t * url);
32
33
34 int
35 main (int argc, char **argv)
36 {
37   FILE *urls_file;
38
39
40   osip_uri_t *url;
41   char *a_url;
42   char *dest;
43   char *res;
44
45   urls_file = fopen (argv[1], "r");
46   if (urls_file == NULL)
47     {
48       fprintf (stdout, "Failed to open %s file.\nUsage: turls urls.txt\n",
49                argv[1]);
50       exit (0);
51     }
52
53   a_url = (char *) osip_malloc (200);
54   res = fgets (a_url, 200, urls_file);  /* lines are under 200 */
55   while (res != NULL)
56     {
57       int errcode;
58
59       /* remove the last '\n' before parsing */
60       osip_strncpy (a_url + strlen (a_url) - 1, "\0", 1);
61
62       if (0 != strncmp (a_url, "#", 1))
63         {
64           /* allocate & init url */
65           osip_uri_init (&url);
66           printf ("=================================================\n");
67           printf ("URL TO PARSE: |%s|\n", a_url);
68           errcode = osip_uri_parse (url, a_url);
69           if (errcode != -1)
70             {
71               if (osip_uri_to_str (url, &dest) != -1)
72                 {
73                   printf ("result:       |%s|\n", dest);
74                   osip_uri_test_accessor_api (url);
75                   osip_free (dest);
76                 }
77             }
78           else
79             printf ("Bad url format: %s\n", a_url);
80           osip_uri_free (url);
81           printf ("=================================================\n");
82         }
83       res = fgets (a_url, 200, urls_file);      /* lines are under 200 */
84     }
85   osip_free (a_url);
86   return 0;
87 }
88
89 int
90 osip_uri_test_accessor_api (osip_uri_t * url)
91 {
92   if (url->scheme != NULL)
93     fprintf (stdout, "%s:", url->scheme);
94   if (url->string != NULL)
95     {
96       fprintf (stdout, "|%s", url->string);
97       fprintf (stdout, "\n");
98       return 0;
99     }
100   if (url->username != NULL)
101     fprintf (stdout, "%s|", url->username);
102
103   if ((url->password != NULL) && (url->username != NULL))
104     fprintf (stdout, ":%s|", url->password);
105   if (url->username != NULL)
106     fprintf (stdout, "@|");
107   /*   if (url->host!=NULL)  mandatory */
108   if (strchr (url->host, ':') != NULL)
109     fprintf (stdout, "[%s]|", url->host);
110   else
111     fprintf (stdout, "%s|", url->host);
112   if (url->port != NULL)
113     fprintf (stdout, ":%s|", url->port);
114
115   fprintf (stdout, "\nuri-params\n");
116
117   {
118     int pos = 0;
119     osip_uri_param_t *u_param;
120
121     while (!osip_list_eol (url->url_params, pos))
122       {
123         u_param = (osip_uri_param_t *) osip_list_get (url->url_params, pos);
124         if (u_param->gvalue != NULL)
125           fprintf (stdout, ";%s|=|%s|", u_param->gname, u_param->gvalue);
126         else
127           fprintf (stdout, ";%s|", u_param->gname);
128         pos++;
129       }
130   }
131
132   fprintf (stdout, "\nheaders\n");
133
134   {
135     int pos = 0;
136     osip_uri_header_t *u_header;
137
138     while (!osip_list_eol (url->url_headers, pos))
139       {
140         u_header =
141           (osip_uri_header_t *) osip_list_get (url->url_headers, pos);
142         if (pos == 0)
143           fprintf (stdout, "?%s|=|%s|", u_header->gname, u_header->gvalue);
144         else
145           fprintf (stdout, "&%s|=|%s|", u_header->gname, u_header->gvalue);
146         pos++;
147       }
148   }
149   fprintf (stdout, "\n");
150   return 0;
151 }