annotate libmpdemux/url.c @ 9148:946b14a9e743

type autodetection (from filemask/filelist extension)
author arpi
date Tue, 28 Jan 2003 22:00:57 +0000
parents 72a156e6d482
children a9b7b6055563
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3496
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
1 /*
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
2 * URL Helper
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
4 * (C) 2001, MPlayer team.
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
5 *
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
6 */
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
7
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
8 #include <string.h>
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
9 #include <stdlib.h>
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
10 #include <stdio.h>
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
11
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
12 #include "url.h"
5933
14b46420840b printf to mp_msg
albeu
parents: 4653
diff changeset
13 #include "mp_msg.h"
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
14
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
15 URL_t*
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
16 url_new(char* url) {
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
17 int pos1, pos2;
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
18 URL_t* Curl;
3494
fb9de639ed30 Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents: 3040
diff changeset
19 char *ptr1, *ptr2, *ptr3;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
20
4653
d2a7fcfeec6f Removed the url_copy function , since it was badly implemented,
bertrand
parents: 4119
diff changeset
21 if( url==NULL ) return NULL;
d2a7fcfeec6f Removed the url_copy function , since it was badly implemented,
bertrand
parents: 4119
diff changeset
22
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
23 // Create the URL container
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
24 Curl = (URL_t*)malloc(sizeof(URL_t));
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
25 if( Curl==NULL ) {
5933
14b46420840b printf to mp_msg
albeu
parents: 4653
diff changeset
26 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
1631
09284c9c2b49 exit() -> return NULL
arpi
parents: 1530
diff changeset
27 return NULL;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
28 }
840
9141234715a2 Added initialisation of URL pointers.
bertrand
parents: 833
diff changeset
29 // Initialisation of the URL container members
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
30 memset( Curl, 0, sizeof(URL_t) );
840
9141234715a2 Added initialisation of URL pointers.
bertrand
parents: 833
diff changeset
31
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
32 // Copy the url in the URL container
3612
a1522fa7728a x = malloc(strlen(s) + c) ... strcpy(x, s)
pl
parents: 3496
diff changeset
33 Curl->url = strdup(url);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
34 if( Curl->url==NULL ) {
5933
14b46420840b printf to mp_msg
albeu
parents: 4653
diff changeset
35 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
7809
72a156e6d482 Fixed more potential mem leaks and use the url_free method
bertrand
parents: 7761
diff changeset
36 url_free(Curl);
1631
09284c9c2b49 exit() -> return NULL
arpi
parents: 1530
diff changeset
37 return NULL;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
38 }
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
39
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
40 // extract the protocol
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
41 ptr1 = strstr(url, "://");
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
42 if( ptr1==NULL ) {
5933
14b46420840b printf to mp_msg
albeu
parents: 4653
diff changeset
43 mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n");
7809
72a156e6d482 Fixed more potential mem leaks and use the url_free method
bertrand
parents: 7761
diff changeset
44 url_free(Curl);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
45 return NULL;
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
46 }
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
47 pos1 = ptr1-url;
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
48 Curl->protocol = (char*)malloc(pos1+1);
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
49 strncpy(Curl->protocol, url, pos1);
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
50 if( Curl->protocol==NULL ) {
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
51 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
7809
72a156e6d482 Fixed more potential mem leaks and use the url_free method
bertrand
parents: 7761
diff changeset
52 url_free(Curl);
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
53 return NULL;
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
54 }
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
55 Curl->protocol[pos1] = '\0';
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
56
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
57 // jump the "://"
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
58 ptr1 += 3;
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
59 pos1 += 3;
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
60
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
61 // check if a username:password is given
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
62 ptr2 = strstr(ptr1, "@");
7330
2f3fe8274028 Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents: 6513
diff changeset
63 ptr3 = strstr(ptr1, "/");
2f3fe8274028 Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents: 6513
diff changeset
64 if( ptr3!=NULL && ptr3<ptr2 ) {
2f3fe8274028 Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents: 6513
diff changeset
65 // it isn't really a username but rather a part of the path
2f3fe8274028 Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents: 6513
diff changeset
66 ptr2 = NULL;
2f3fe8274028 Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents: 6513
diff changeset
67 }
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
68 if( ptr2!=NULL ) {
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
69 // We got something, at least a username...
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
70 int len = ptr2-ptr1;
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
71 Curl->username = (char*)malloc(len+1);
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
72 if( Curl->username==NULL ) {
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
73 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
7809
72a156e6d482 Fixed more potential mem leaks and use the url_free method
bertrand
parents: 7761
diff changeset
74 url_free(Curl);
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
75 return NULL;
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
76 }
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
77 strncpy(Curl->username, ptr1, len);
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
78 Curl->username[len] = '\0';
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
79
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
80 ptr3 = strstr(ptr1, ":");
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
81 if( ptr3!=NULL && ptr3<ptr2 ) {
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
82 // We also have a password
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
83 int len2 = ptr2-ptr3-1;
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
84 Curl->username[ptr3-ptr1]='\0';
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
85 Curl->password = (char*)malloc(len2+1);
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
86 if( Curl->password==NULL ) {
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
87 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
7809
72a156e6d482 Fixed more potential mem leaks and use the url_free method
bertrand
parents: 7761
diff changeset
88 url_free(Curl);
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
89 return NULL;
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
90 }
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
91 strncpy( Curl->password, ptr3+1, len2);
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
92 Curl->password[len2]='\0';
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
93 }
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
94 ptr1 = ptr2+1;
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
95 pos1 = ptr1-url;
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
96 }
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
97
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
98 // look if the port is given
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
99 ptr2 = strstr(ptr1, ":");
3494
fb9de639ed30 Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents: 3040
diff changeset
100 // If the : is after the first / it isn't the port
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
101 ptr3 = strstr(ptr1, "/");
3494
fb9de639ed30 Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents: 3040
diff changeset
102 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
103 if( ptr2==NULL ) {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
104 // No port is given
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
105 // Look if a path is given
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
106 ptr2 = strstr(ptr1, "/");
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
107 if( ptr2==NULL ) {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
108 // No path/filename
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
109 // So we have an URL like http://www.hostname.com
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
110 pos2 = strlen(url);
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
111 } else {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
112 // We have an URL like http://www.hostname.com/file.txt
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
113 pos2 = ptr2-url;
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
114 }
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
115 } else {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
116 // We have an URL beginning like http://www.hostname.com:1212
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
117 // Get the port number
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
118 Curl->port = atoi(ptr2+1);
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
119 pos2 = ptr2-url;
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
120 }
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
121 // copy the hostname in the URL container
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
122 Curl->hostname = (char*)malloc(pos2-pos1+1);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
123 if( Curl->hostname==NULL ) {
5933
14b46420840b printf to mp_msg
albeu
parents: 4653
diff changeset
124 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
7809
72a156e6d482 Fixed more potential mem leaks and use the url_free method
bertrand
parents: 7761
diff changeset
125 url_free(Curl);
1631
09284c9c2b49 exit() -> return NULL
arpi
parents: 1530
diff changeset
126 return NULL;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
127 }
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
128 strncpy(Curl->hostname, ptr1, pos2-pos1);
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
129 Curl->hostname[pos2-pos1] = '\0';
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
130
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
131 // Look if a path is given
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
132 ptr2 = strstr(ptr1, "/");
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
133 if( ptr2!=NULL ) {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
134 // A path/filename is given
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
135 // check if it's not a trailing '/'
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
136 if( strlen(ptr2)>1 ) {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
137 // copy the path/filename in the URL container
3612
a1522fa7728a x = malloc(strlen(s) + c) ... strcpy(x, s)
pl
parents: 3496
diff changeset
138 Curl->file = strdup(ptr2);
840
9141234715a2 Added initialisation of URL pointers.
bertrand
parents: 833
diff changeset
139 if( Curl->file==NULL ) {
5933
14b46420840b printf to mp_msg
albeu
parents: 4653
diff changeset
140 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
7809
72a156e6d482 Fixed more potential mem leaks and use the url_free method
bertrand
parents: 7761
diff changeset
141 url_free(Curl);
1631
09284c9c2b49 exit() -> return NULL
arpi
parents: 1530
diff changeset
142 return NULL;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
143 }
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
144 }
869
e350849ff400 Url given without a filename/path get the filename/path '/'
bertrand
parents: 840
diff changeset
145 }
997
647f5781d490 Modified code for path/filename extraction.
bertrand
parents: 902
diff changeset
146 // Check if a filenme was given or set, else set it with '/'
869
e350849ff400 Url given without a filename/path get the filename/path '/'
bertrand
parents: 840
diff changeset
147 if( Curl->file==NULL ) {
e350849ff400 Url given without a filename/path get the filename/path '/'
bertrand
parents: 840
diff changeset
148 Curl->file = (char*)malloc(2);
e350849ff400 Url given without a filename/path get the filename/path '/'
bertrand
parents: 840
diff changeset
149 if( Curl->file==NULL ) {
5933
14b46420840b printf to mp_msg
albeu
parents: 4653
diff changeset
150 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
7809
72a156e6d482 Fixed more potential mem leaks and use the url_free method
bertrand
parents: 7761
diff changeset
151 url_free(Curl);
1631
09284c9c2b49 exit() -> return NULL
arpi
parents: 1530
diff changeset
152 return NULL;
869
e350849ff400 Url given without a filename/path get the filename/path '/'
bertrand
parents: 840
diff changeset
153 }
e350849ff400 Url given without a filename/path get the filename/path '/'
bertrand
parents: 840
diff changeset
154 strcpy(Curl->file, "/");
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
155 }
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
156
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
157 return Curl;
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
158 }
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
159
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
160 void
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
161 url_free(URL_t* url) {
1530
3effaac0ddb7 silly bug fixed - thanx Szekeres Istvan <szekeres@webvilag.com>
arpi
parents: 997
diff changeset
162 if(!url) return;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
163 if(url->url) free(url->url);
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
164 if(url->protocol) free(url->protocol);
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
165 if(url->hostname) free(url->hostname);
840
9141234715a2 Added initialisation of URL pointers.
bertrand
parents: 833
diff changeset
166 if(url->file) free(url->file);
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
167 if(url->username) free(url->username);
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
168 if(url->password) free(url->password);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
169 free(url);
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
170 }
3496
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
171
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
172
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
173 /* Replace escape sequences in an URL (or a part of an URL) */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
174 /* works like strcpy(), but without return argument */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
175 /* unescape_url_string comes from ASFRecorder */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
176 void
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
177 url_unescape_string(char *outbuf, char *inbuf)
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
178 {
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
179 unsigned char c;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
180 do {
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
181 c = *inbuf++;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
182 if (c == '%') {
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
183 unsigned char c1 = *inbuf++;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
184 unsigned char c2 = *inbuf++;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
185 if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) &&
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
186 ((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) {
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
187 if (c1>='0' && c1<='9') c1-='0';
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
188 else c1-='A';
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
189 if (c2>='0' && c2<='9') c2-='0';
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
190 else c2-='A';
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
191 c = (c1<<4) + c2;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
192 }
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
193 }
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
194 *outbuf++ = c;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
195 } while (c != '\0');
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
196 }
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
197
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
198 /* Replace specific characters in the URL string by an escape sequence */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
199 /* works like strcpy(), but without return argument */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
200 /* escape_url_string comes from ASFRecorder */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
201 void
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
202 url_escape_string(char *outbuf, char *inbuf) {
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
203 unsigned char c;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
204 do {
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
205 c = *inbuf++;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
206 if( (c >= 'A' && c <= 'Z') ||
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
207 (c >= 'a' && c <= 'z') ||
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
208 (c >= '0' && c <= '9') ||
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
209 (c >= 0x7f) || /* fareast languages(Chinese, Korean, Japanese) */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
210 c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
211 c=='*' || c=='\'' || c=='(' || c==')' || c=='%' || /* do not touch escape character */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
212 c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
213 c=='&' || c=='=' || c=='+' || c=='$' || c==',' || /* see RFC 2396 */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
214 c=='\0' ) {
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
215 *outbuf++ = c;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
216 } else {
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
217 /* all others will be escaped */
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
218 unsigned char c1 = ((c & 0xf0) >> 4);
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
219 unsigned char c2 = (c & 0x0f);
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
220 if (c1 < 10) c1+='0';
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
221 else c1+='A';
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
222 if (c2 < 10) c2+='0';
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
223 else c2+='A';
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
224 *outbuf++ = '%';
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
225 *outbuf++ = c1;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
226 *outbuf++ = c2;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
227 }
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
228 } while (c != '\0');
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
229 }
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
230
4119
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
231 #ifdef __URL_DEBUG
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
232 void
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
233 url_debug(URL_t *url) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
234 if( url==NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
235 printf("URL pointer NULL\n");
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
236 return;
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
237 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
238 if( url->url!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
239 printf("url=%s\n", url->url );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
240 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
241 if( url->protocol!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
242 printf("protocol=%s\n", url->protocol );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
243 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
244 if( url->hostname!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
245 printf("hostname=%s\n", url->hostname );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
246 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
247 printf("port=%d\n", url->port );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
248 if( url->file!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
249 printf("file=%s\n", url->file );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
250 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
251 if( url->username!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
252 printf("username=%s\n", url->username );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
253 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
254 if( url->password!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
255 printf("password=%s\n", url->password );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
256 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
257 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
258 #endif //__URL_DEBUG