4 * Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #define SIZE_MAX ((size_t)-1)
36 URL_t
*url_redirect(URL_t
**url
, const char *redir
) {
39 if (!strchr(redir
, '/') || *redir
== '/') {
41 char *newurl
= malloc(strlen(u
->url
) + strlen(redir
) + 1);
42 strcpy(newurl
, u
->url
);
45 tmp
= strstr(newurl
, "://");
46 if (tmp
) tmp
= strchr(tmp
+ 3, '/');
48 tmp
= strrchr(newurl
, '/');
50 strcat(newurl
, redir
);
51 res
= url_new(newurl
);
60 static int make_noauth_url(URL_t
*url
, char *dst
, int dst_size
)
63 return snprintf(dst
, dst_size
, "%s://%s:%d%s", url
->protocol
,
64 url
->hostname
, url
->port
, url
->file
);
66 return snprintf(dst
, dst_size
, "%s://%s%s", url
->protocol
,
67 url
->hostname
, url
->file
);
71 url_new(const char* url
) {
72 int pos1
, pos2
,v6addr
= 0, noauth_len
;
74 char *escfilename
=NULL
;
75 char *ptr1
=NULL
, *ptr2
=NULL
, *ptr3
=NULL
, *ptr4
=NULL
;
78 if( url
==NULL
) return NULL
;
80 if (strlen(url
) > (SIZE_MAX
/ 3 - 1)) {
81 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
84 escfilename
=malloc(strlen(url
)*3+1);
86 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
90 // Create the URL container
91 Curl
= malloc(sizeof(URL_t
));
93 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
97 // Initialisation of the URL container members
98 memset( Curl
, 0, sizeof(URL_t
) );
100 url_escape_string(escfilename
,url
);
102 // Copy the url in the URL container
103 Curl
->url
= strdup(escfilename
);
104 if( Curl
->url
==NULL
) {
105 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
108 mp_msg(MSGT_OPEN
,MSGL_V
,"Filename for url is now %s\n",escfilename
);
110 // extract the protocol
111 ptr1
= strstr(escfilename
, "://");
113 // Check for a special case: "sip:" (without "//"):
114 if (strstr(escfilename
, "sip:") == escfilename
) {
115 ptr1
= (char *)&url
[3]; // points to ':'
118 mp_msg(MSGT_NETWORK
,MSGL_V
,"Not an URL!\n");
122 pos1
= ptr1
-escfilename
;
123 Curl
->protocol
= malloc(pos1
+1);
124 if( Curl
->protocol
==NULL
) {
125 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
128 strncpy(Curl
->protocol
, escfilename
, pos1
);
129 Curl
->protocol
[pos1
] = '\0';
135 // check if a username:password is given
136 ptr2
= strstr(ptr1
, "@");
137 ptr3
= strstr(ptr1
, "/");
138 if( ptr3
!=NULL
&& ptr3
<ptr2
) {
139 // it isn't really a username but rather a part of the path
143 // We got something, at least a username...
145 Curl
->username
= malloc(len
+1);
146 if( Curl
->username
==NULL
) {
147 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
150 strncpy(Curl
->username
, ptr1
, len
);
151 Curl
->username
[len
] = '\0';
153 ptr3
= strstr(ptr1
, ":");
154 if( ptr3
!=NULL
&& ptr3
<ptr2
) {
155 // We also have a password
156 int len2
= ptr2
-ptr3
-1;
157 Curl
->username
[ptr3
-ptr1
]='\0';
158 Curl
->password
= malloc(len2
+1);
159 if( Curl
->password
==NULL
) {
160 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
163 strncpy( Curl
->password
, ptr3
+1, len2
);
164 Curl
->password
[len2
]='\0';
165 url_unescape_string(Curl
->password
, Curl
->password
);
167 url_unescape_string(Curl
->username
, Curl
->username
);
169 pos1
= ptr1
-escfilename
;
172 // before looking for a port number check if we have an IPv6 type numeric address
173 // in IPv6 URL the numeric address should be inside square braces.
174 ptr2
= strstr(ptr1
, "[");
175 ptr3
= strstr(ptr1
, "]");
176 ptr4
= strstr(ptr1
, "/");
177 if( ptr2
!=NULL
&& ptr3
!=NULL
&& ptr2
< ptr3
&& (!ptr4
|| ptr4
> ptr3
)) {
178 // we have an IPv6 numeric address
188 // look if the port is given
189 ptr2
= strstr(ptr2
, ":");
190 // If the : is after the first / it isn't the port
191 ptr3
= strstr(ptr1
, "/");
192 if(ptr3
&& ptr3
- ptr2
< 0) ptr2
= NULL
;
195 // Look if a path is given
198 // So we have an URL like http://www.hostname.com
199 pos2
= strlen(escfilename
);
201 // We have an URL like http://www.hostname.com/file.txt
202 pos2
= ptr3
-escfilename
;
205 // We have an URL beginning like http://www.hostname.com:1212
206 // Get the port number
207 Curl
->port
= atoi(ptr2
+1);
208 pos2
= ptr2
-escfilename
;
211 // copy the hostname in the URL container
212 Curl
->hostname
= malloc(pos2
-pos1
+1);
213 if( Curl
->hostname
==NULL
) {
214 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
217 strncpy(Curl
->hostname
, ptr1
, pos2
-pos1
);
218 Curl
->hostname
[pos2
-pos1
] = '\0';
220 // Look if a path is given
221 ptr2
= strstr(ptr1
, "/");
223 // A path/filename is given
224 // check if it's not a trailing '/'
225 if( strlen(ptr2
)>1 ) {
226 // copy the path/filename in the URL container
227 Curl
->file
= strdup(ptr2
);
228 if( Curl
->file
==NULL
) {
229 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
234 // Check if a filename was given or set, else set it with '/'
235 if( Curl
->file
==NULL
) {
236 Curl
->file
= malloc(2);
237 if( Curl
->file
==NULL
) {
238 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
241 strcpy(Curl
->file
, "/");
244 noauth_len
= make_noauth_url(Curl
, NULL
, 0);
245 if (noauth_len
> 0) {
247 Curl
->noauth_url
= malloc(noauth_len
);
248 if (!Curl
->noauth_url
) {
249 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, "Memory allocation failed.\n");
252 make_noauth_url(Curl
, Curl
->noauth_url
, noauth_len
);
259 if (Curl
) url_free(Curl
);
264 url_free(URL_t
* url
) {
276 /* Replace escape sequences in an URL (or a part of an URL) */
277 /* works like strcpy(), but without return argument,
278 except that outbuf == inbuf is allowed */
280 url_unescape_string(char *outbuf
, const char *inbuf
)
282 unsigned char c
,c1
,c2
;
283 int i
,len
=strlen(inbuf
);
286 if (c
== '%' && i
<len
-2) { //must have 2 more chars
287 c1
= toupper(inbuf
[i
+1]); // we need uppercase characters
288 c2
= toupper(inbuf
[i
+2]);
289 if ( ((c1
>='0' && c1
<='9') || (c1
>='A' && c1
<='F')) &&
290 ((c2
>='0' && c2
<='9') || (c2
>='A' && c2
<='F')) ) {
291 if (c1
>='0' && c1
<='9') c1
-='0';
293 if (c2
>='0' && c2
<='9') c2
-='0';
296 i
=i
+2; //only skip next 2 chars if valid esc
301 *outbuf
++='\0'; //add nullterm to string
305 url_escape_string_part(char *outbuf
, const char *inbuf
) {
306 unsigned char c
,c1
,c2
;
307 int i
,len
=strlen(inbuf
);
309 for (i
=0;i
<len
;i
++) {
311 if ((c
=='%') && i
<len
-2 ) { //need 2 more characters
312 c1
=toupper(inbuf
[i
+1]); c2
=toupper(inbuf
[i
+2]); // need uppercase chars
314 c1
=129; c2
=129; //not escape chars
317 if( (c
>= 'A' && c
<= 'Z') ||
318 (c
>= 'a' && c
<= 'z') ||
319 (c
>= '0' && c
<= '9') ||
322 } else if ( c
=='%' && ((c1
>= '0' && c1
<= '9') || (c1
>= 'A' && c1
<= 'F')) &&
323 ((c2
>= '0' && c2
<= '9') || (c2
>= 'A' && c2
<= 'F'))) {
324 // check if part of an escape sequence
325 *outbuf
++=c
; // already
328 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"String appears to be already escaped in url_escape %c%c1%c2\n",c
,c1
,c2
);
329 // error as this should not happen against RFC 2396
330 // to escape a string twice
332 /* all others will be escaped */
333 c1
= ((c
& 0xf0) >> 4);
335 if (c1
< 10) c1
+='0';
337 if (c2
< 10) c2
+='0';
347 /* Replace specific characters in the URL string by an escape sequence */
348 /* works like strcpy(), but without return argument */
350 url_escape_string(char *outbuf
, const char *inbuf
) {
352 int i
= 0,j
,len
= strlen(inbuf
);
353 char* tmp
,*unesc
= NULL
, *in
;
355 // Look if we have an ip6 address, if so skip it there is
356 // no need to escape anything in there.
357 tmp
= strstr(inbuf
,"://[");
359 tmp
= strchr(tmp
+4,']');
360 if(tmp
&& (tmp
[1] == '/' || tmp
[1] == ':' ||
363 strncpy(outbuf
,inbuf
,i
);
371 // look for the next char that must be kept
372 for (j
=i
;j
<len
;j
++) {
374 if(c
=='-' || c
=='_' || c
=='.' || c
=='!' || c
=='~' || /* mark characters */
375 c
=='*' || c
=='\'' || c
=='(' || c
==')' || /* do not touch escape character */
376 c
==';' || c
=='/' || c
=='?' || c
==':' || c
=='@' || /* reserved characters */
377 c
=='&' || c
=='=' || c
=='+' || c
=='$' || c
==',') /* see RFC 2396 */
380 // we are on a reserved char, write it out
386 // we found one, take that part of the string
388 if(!tmp
) tmp
= malloc(len
+1);
389 strncpy(tmp
,inbuf
+i
,j
-i
);
392 } else // take the rest of the string
395 if(!unesc
) unesc
= malloc(len
+1);
396 // unescape first to avoid escaping escape
397 url_unescape_string(unesc
,in
);
398 // then escape, including mark and other reserved chars
399 // that can come from escape sequences
400 url_escape_string_part(outbuf
,unesc
);
401 outbuf
+= strlen(outbuf
);
411 url_debug(const URL_t
*url
) {
413 printf("URL pointer NULL\n");
416 if( url
->url
!=NULL
) {
417 printf("url=%s\n", url
->url
);
419 if( url
->protocol
!=NULL
) {
420 printf("protocol=%s\n", url
->protocol
);
422 if( url
->hostname
!=NULL
) {
423 printf("hostname=%s\n", url
->hostname
);
425 printf("port=%d\n", url
->port
);
426 if( url
->file
!=NULL
) {
427 printf("file=%s\n", url
->file
);
429 if( url
->username
!=NULL
) {
430 printf("username=%s\n", url
->username
);
432 if( url
->password
!=NULL
) {
433 printf("password=%s\n", url
->password
);
436 #endif /* URL_DEBUG */