Mercurial > mplayer.hg
annotate libmpdemux/url.c @ 13783:4277e06630bf
compilation fix, mostly by Reza Jelveh
author | diego |
---|---|
date | Thu, 28 Oct 2004 00:50:21 +0000 |
parents | 5dc89722fad2 |
children | c41e22d77214 |
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 | 2 * URL Helper |
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 * | |
6 */ | |
7 | |
833 | 8 #include <string.h> |
9 #include <stdlib.h> | |
10 #include <stdio.h> | |
12391 | 11 #include <ctype.h> |
833 | 12 |
13 #include "url.h" | |
5933 | 14 #include "mp_msg.h" |
833 | 15 |
16 URL_t* | |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
17 url_new(const char* url) { |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
18 int pos1, pos2,v6addr = 0; |
833 | 19 URL_t* Curl; |
12391 | 20 char *escfilename=NULL; |
21 char *unescfilename=NULL; | |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
22 char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL; |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
23 int jumpSize = 3; |
833 | 24 |
4653
d2a7fcfeec6f
Removed the url_copy function , since it was badly implemented,
bertrand
parents:
4119
diff
changeset
|
25 if( url==NULL ) return NULL; |
d2a7fcfeec6f
Removed the url_copy function , since it was badly implemented,
bertrand
parents:
4119
diff
changeset
|
26 |
12391 | 27 // Create temp filename space |
28 unescfilename=malloc(strlen(url)+1); | |
29 if (!unescfilename ) { | |
30 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); | |
31 return NULL; | |
32 } | |
33 escfilename=malloc(strlen(url)*3+1); | |
34 if (!escfilename ) { | |
35 free(unescfilename); | |
36 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); | |
37 return NULL; | |
38 } | |
39 | |
833 | 40 // Create the URL container |
41 Curl = (URL_t*)malloc(sizeof(URL_t)); | |
42 if( Curl==NULL ) { | |
5933 | 43 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
12391 | 44 if (unescfilename) free(unescfilename); |
45 if (escfilename) free(escfilename); | |
1631 | 46 return NULL; |
833 | 47 } |
12391 | 48 |
840 | 49 // Initialisation of the URL container members |
902 | 50 memset( Curl, 0, sizeof(URL_t) ); |
840 | 51 |
12391 | 52 //create unescaped/escaped versions of url |
53 url_unescape_string(unescfilename,url); // this is to prevent us from escaping an unescaped string | |
54 // violating RFC 2396 | |
55 url_escape_string(escfilename,unescfilename); | |
56 free(unescfilename); | |
57 | |
833 | 58 // Copy the url in the URL container |
12391 | 59 Curl->url = strdup(escfilename); |
833 | 60 if( Curl->url==NULL ) { |
5933 | 61 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
|
62 url_free(Curl); |
1631 | 63 return NULL; |
833 | 64 } |
12391 | 65 mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename); |
833 | 66 |
67 // extract the protocol | |
12391 | 68 ptr1 = strstr(escfilename, "://"); |
833 | 69 if( ptr1==NULL ) { |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
70 // Check for a special case: "sip:" (without "//"): |
12391 | 71 if (strstr(escfilename, "sip:") == escfilename) { |
11412 | 72 ptr1 = (char *)&url[3]; // points to ':' |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
73 jumpSize = 1; |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
74 } else { |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
75 mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n"); |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
76 url_free(Curl); |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
77 return NULL; |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
78 } |
833 | 79 } |
12391 | 80 pos1 = ptr1-escfilename; |
833 | 81 Curl->protocol = (char*)malloc(pos1+1); |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
82 if( Curl->protocol==NULL ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
83 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
|
84 url_free(Curl); |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
85 return NULL; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
86 } |
12637 | 87 strncpy(Curl->protocol, escfilename, pos1); |
833 | 88 Curl->protocol[pos1] = '\0'; |
89 | |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
90 // jump the "://" |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
91 ptr1 += jumpSize; |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
92 pos1 += jumpSize; |
6513
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 // check if a username:password is given |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
95 ptr2 = strstr(ptr1, "@"); |
7330
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
96 ptr3 = strstr(ptr1, "/"); |
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
97 if( ptr3!=NULL && ptr3<ptr2 ) { |
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
98 // 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
|
99 ptr2 = NULL; |
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
100 } |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
101 if( ptr2!=NULL ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
102 // We got something, at least a username... |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
103 int len = ptr2-ptr1; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
104 Curl->username = (char*)malloc(len+1); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
105 if( Curl->username==NULL ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
106 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
|
107 url_free(Curl); |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
108 return NULL; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
109 } |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
110 strncpy(Curl->username, ptr1, len); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
111 Curl->username[len] = '\0'; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
112 |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
113 ptr3 = strstr(ptr1, ":"); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
114 if( ptr3!=NULL && ptr3<ptr2 ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
115 // We also have a password |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
116 int len2 = ptr2-ptr3-1; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
117 Curl->username[ptr3-ptr1]='\0'; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
118 Curl->password = (char*)malloc(len2+1); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
119 if( Curl->password==NULL ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
120 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
|
121 url_free(Curl); |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
122 return NULL; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
123 } |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
124 strncpy( Curl->password, ptr3+1, len2); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
125 Curl->password[len2]='\0'; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
126 } |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
127 ptr1 = ptr2+1; |
12391 | 128 pos1 = ptr1-escfilename; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
129 } |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
130 |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
131 // before looking for a port number check if we have an IPv6 type numeric address |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
132 // in IPv6 URL the numeric address should be inside square braces. |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
133 ptr2 = strstr(ptr1, "["); |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
134 ptr3 = strstr(ptr1, "]"); |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
135 ptr4 = strstr(ptr1, "/"); |
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
136 if( ptr2!=NULL && ptr3!=NULL && ptr2 < ptr3 && (!ptr4 || ptr4 > ptr3)) { |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
137 // we have an IPv6 numeric address |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
138 ptr1++; |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
139 pos1++; |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
140 ptr2 = ptr3; |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
141 v6addr = 1; |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
142 } else { |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
143 ptr2 = ptr1; |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
144 |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
145 } |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
146 |
833 | 147 // look if the port is given |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
148 ptr2 = strstr(ptr2, ":"); |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
149 // 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
|
150 ptr3 = strstr(ptr1, "/"); |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
151 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL; |
833 | 152 if( ptr2==NULL ) { |
153 // No port is given | |
154 // Look if a path is given | |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
155 if( ptr3==NULL ) { |
833 | 156 // No path/filename |
157 // So we have an URL like http://www.hostname.com | |
12391 | 158 pos2 = strlen(escfilename); |
833 | 159 } else { |
160 // We have an URL like http://www.hostname.com/file.txt | |
12391 | 161 pos2 = ptr3-escfilename; |
833 | 162 } |
163 } else { | |
164 // We have an URL beginning like http://www.hostname.com:1212 | |
165 // Get the port number | |
166 Curl->port = atoi(ptr2+1); | |
12391 | 167 pos2 = ptr2-escfilename; |
833 | 168 } |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
169 if( v6addr ) pos2--; |
833 | 170 // copy the hostname in the URL container |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
171 Curl->hostname = (char*)malloc(pos2-pos1+1); |
833 | 172 if( Curl->hostname==NULL ) { |
5933 | 173 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
|
174 url_free(Curl); |
1631 | 175 return NULL; |
833 | 176 } |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
177 strncpy(Curl->hostname, ptr1, pos2-pos1); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
178 Curl->hostname[pos2-pos1] = '\0'; |
833 | 179 |
180 // Look if a path is given | |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
181 ptr2 = strstr(ptr1, "/"); |
833 | 182 if( ptr2!=NULL ) { |
183 // A path/filename is given | |
184 // check if it's not a trailing '/' | |
185 if( strlen(ptr2)>1 ) { | |
186 // copy the path/filename in the URL container | |
3612 | 187 Curl->file = strdup(ptr2); |
840 | 188 if( Curl->file==NULL ) { |
5933 | 189 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
|
190 url_free(Curl); |
1631 | 191 return NULL; |
833 | 192 } |
193 } | |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
194 } |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
195 // Check if a filename 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
|
196 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
197 Curl->file = (char*)malloc(2); |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
198 if( Curl->file==NULL ) { |
5933 | 199 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
|
200 url_free(Curl); |
1631 | 201 return NULL; |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
202 } |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
203 strcpy(Curl->file, "/"); |
833 | 204 } |
205 | |
12391 | 206 free(escfilename); |
833 | 207 return Curl; |
208 } | |
209 | |
210 void | |
902 | 211 url_free(URL_t* url) { |
1530
3effaac0ddb7
silly bug fixed - thanx Szekeres Istvan <szekeres@webvilag.com>
arpi
parents:
997
diff
changeset
|
212 if(!url) return; |
833 | 213 if(url->url) free(url->url); |
214 if(url->protocol) free(url->protocol); | |
215 if(url->hostname) free(url->hostname); | |
840 | 216 if(url->file) free(url->file); |
902 | 217 if(url->username) free(url->username); |
218 if(url->password) free(url->password); | |
833 | 219 free(url); |
220 } | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
221 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
222 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
223 /* 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
|
224 /* 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
|
225 void |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
226 url_unescape_string(char *outbuf, const char *inbuf) |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
227 { |
12391 | 228 unsigned char c,c1,c2; |
229 int i,len=strlen(inbuf); | |
230 for (i=0;i<len;i++){ | |
231 c = inbuf[i]; | |
232 if (c == '%' && i<len-2) { //must have 2 more chars | |
233 c1 = toupper(inbuf[i+1]); // we need uppercase characters | |
234 c2 = toupper(inbuf[i+2]); | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
235 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
|
236 ((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
|
237 if (c1>='0' && c1<='9') c1-='0'; |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
238 else c1-='A'-10; |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
239 if (c2>='0' && c2<='9') c2-='0'; |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
240 else c2-='A'-10; |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
241 c = (c1<<4) + c2; |
12391 | 242 i=i+2; //only skip next 2 chars if valid esc |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
243 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
244 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
245 *outbuf++ = c; |
12391 | 246 } |
247 *outbuf++='\0'; //add nullterm to string | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
248 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
249 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
250 /* 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
|
251 /* 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
|
252 void |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
253 url_escape_string(char *outbuf, const char *inbuf) { |
12391 | 254 unsigned char c,c1,c2; |
255 int i,len=strlen(inbuf); | |
256 | |
257 for (i=0;i<len;i++) { | |
258 c = inbuf[i]; | |
259 if ((c=='%') && i<len-2 ) { //need 2 more characters | |
260 c1=toupper(inbuf[i+1]); c2=toupper(inbuf[i+2]); // need uppercase chars | |
261 } else { | |
262 c1=129; c2=129; //not escape chars | |
263 } | |
264 | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
265 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
|
266 (c >= 'a' && c <= 'z') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
267 (c >= '0' && c <= '9') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
268 (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
|
269 c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */ |
12153 | 270 c=='*' || c=='\'' || c=='(' || c==')' || /* do not touch escape character */ |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
271 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
|
272 c=='&' || c=='=' || c=='+' || c=='$' || c==',' || /* see RFC 2396 */ |
12391 | 273 c=='\0' ) { /* string term char */ |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
274 *outbuf++ = c; |
12391 | 275 } else if ( c=='%' && ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) && |
276 ((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) { | |
277 // check if part of an escape sequence | |
278 *outbuf++=c; // already | |
279 | |
280 // dont escape again | |
281 mp_msg(MSGT_NETWORK,MSGL_ERR,"string appears to be already escaped in url_escape %c%c1%c2\n",c,c1,c2); | |
282 // error as this should not happen against RFC 2396 | |
283 // to escape a string twice | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
284 } else { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
285 /* all others will be escaped */ |
12391 | 286 c1 = ((c & 0xf0) >> 4); |
287 c2 = (c & 0x0f); | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
288 if (c1 < 10) c1+='0'; |
9924 | 289 else c1+='A'-10; |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
290 if (c2 < 10) c2+='0'; |
9924 | 291 else c2+='A'-10; |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
292 *outbuf++ = '%'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
293 *outbuf++ = c1; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
294 *outbuf++ = c2; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
295 } |
12391 | 296 } |
297 *outbuf++='\0'; | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
298 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
299 |
4119
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
300 #ifdef __URL_DEBUG |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
301 void |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
302 url_debug(const URL_t *url) { |
4119
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
303 if( url==NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
304 printf("URL pointer NULL\n"); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
305 return; |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
306 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
307 if( url->url!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
308 printf("url=%s\n", url->url ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
309 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
310 if( url->protocol!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
311 printf("protocol=%s\n", url->protocol ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
312 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
313 if( url->hostname!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
314 printf("hostname=%s\n", url->hostname ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
315 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
316 printf("port=%d\n", url->port ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
317 if( url->file!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
318 printf("file=%s\n", url->file ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
319 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
320 if( url->username!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
321 printf("username=%s\n", url->username ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
322 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
323 if( url->password!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
324 printf("password=%s\n", url->password ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
325 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
326 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
327 #endif //__URL_DEBUG |