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
);
70 int make_http_proxy_url(URL_t
*proxy
, const char *host_url
, char *dst
,
74 return snprintf(dst
, dst_size
, "http_proxy://%s:%s@%s:%d/%s",
76 proxy
->password
? proxy
->password
: "",
77 proxy
->hostname
, proxy
->port
, host_url
);
79 return snprintf(dst
, dst_size
, "http_proxy://%s:%d/%s",
80 proxy
->hostname
, proxy
->port
, host_url
);
84 url_new(const char* url
) {
85 int pos1
, pos2
,v6addr
= 0, noauth_len
;
87 char *escfilename
=NULL
;
88 char *ptr1
=NULL
, *ptr2
=NULL
, *ptr3
=NULL
, *ptr4
=NULL
;
91 if( url
==NULL
) return NULL
;
93 if (strlen(url
) > (SIZE_MAX
/ 3 - 1)) {
94 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
97 escfilename
=malloc(strlen(url
)*3+1);
99 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
103 // Create the URL container
104 Curl
= malloc(sizeof(URL_t
));
106 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
110 // Initialisation of the URL container members
111 memset( Curl
, 0, sizeof(URL_t
) );
113 url_escape_string(escfilename
,url
);
115 // Copy the url in the URL container
116 Curl
->url
= strdup(escfilename
);
117 if( Curl
->url
==NULL
) {
118 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
121 mp_msg(MSGT_OPEN
,MSGL_V
,"Filename for url is now %s\n",escfilename
);
123 // extract the protocol
124 ptr1
= strstr(escfilename
, "://");
126 // Check for a special case: "sip:" (without "//"):
127 if (strstr(escfilename
, "sip:") == escfilename
) {
128 ptr1
= (char *)&url
[3]; // points to ':'
131 mp_msg(MSGT_NETWORK
,MSGL_V
,"Not an URL!\n");
135 pos1
= ptr1
-escfilename
;
136 Curl
->protocol
= malloc(pos1
+1);
137 if( Curl
->protocol
==NULL
) {
138 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
141 strncpy(Curl
->protocol
, escfilename
, pos1
);
142 Curl
->protocol
[pos1
] = '\0';
148 // check if a username:password is given
149 ptr2
= strstr(ptr1
, "@");
150 ptr3
= strstr(ptr1
, "/");
151 if( ptr3
!=NULL
&& ptr3
<ptr2
) {
152 // it isn't really a username but rather a part of the path
156 // We got something, at least a username...
158 Curl
->username
= malloc(len
+1);
159 if( Curl
->username
==NULL
) {
160 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
163 strncpy(Curl
->username
, ptr1
, len
);
164 Curl
->username
[len
] = '\0';
166 ptr3
= strstr(ptr1
, ":");
167 if( ptr3
!=NULL
&& ptr3
<ptr2
) {
168 // We also have a password
169 int len2
= ptr2
-ptr3
-1;
170 Curl
->username
[ptr3
-ptr1
]='\0';
171 Curl
->password
= malloc(len2
+1);
172 if( Curl
->password
==NULL
) {
173 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
176 strncpy( Curl
->password
, ptr3
+1, len2
);
177 Curl
->password
[len2
]='\0';
178 url_unescape_string(Curl
->password
, Curl
->password
);
180 url_unescape_string(Curl
->username
, Curl
->username
);
182 pos1
= ptr1
-escfilename
;
185 // before looking for a port number check if we have an IPv6 type numeric address
186 // in IPv6 URL the numeric address should be inside square braces.
187 ptr2
= strstr(ptr1
, "[");
188 ptr3
= strstr(ptr1
, "]");
189 ptr4
= strstr(ptr1
, "/");
190 if( ptr2
!=NULL
&& ptr3
!=NULL
&& ptr2
< ptr3
&& (!ptr4
|| ptr4
> ptr3
)) {
191 // we have an IPv6 numeric address
201 // look if the port is given
202 ptr2
= strstr(ptr2
, ":");
203 // If the : is after the first / it isn't the port
204 ptr3
= strstr(ptr1
, "/");
205 if(ptr3
&& ptr3
- ptr2
< 0) ptr2
= NULL
;
208 // Look if a path is given
211 // So we have an URL like http://www.hostname.com
212 pos2
= strlen(escfilename
);
214 // We have an URL like http://www.hostname.com/file.txt
215 pos2
= ptr3
-escfilename
;
218 // We have an URL beginning like http://www.hostname.com:1212
219 // Get the port number
220 Curl
->port
= atoi(ptr2
+1);
221 pos2
= ptr2
-escfilename
;
224 // copy the hostname in the URL container
225 Curl
->hostname
= malloc(pos2
-pos1
+1);
226 if( Curl
->hostname
==NULL
) {
227 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
230 strncpy(Curl
->hostname
, ptr1
, pos2
-pos1
);
231 Curl
->hostname
[pos2
-pos1
] = '\0';
233 // Look if a path is given
234 ptr2
= strstr(ptr1
, "/");
236 // A path/filename is given
237 // check if it's not a trailing '/'
238 if( strlen(ptr2
)>1 ) {
239 // copy the path/filename in the URL container
240 Curl
->file
= strdup(ptr2
);
241 if( Curl
->file
==NULL
) {
242 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
247 // Check if a filename was given or set, else set it with '/'
248 if( Curl
->file
==NULL
) {
249 Curl
->file
= malloc(2);
250 if( Curl
->file
==NULL
) {
251 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
254 strcpy(Curl
->file
, "/");
257 noauth_len
= make_noauth_url(Curl
, NULL
, 0);
258 if (noauth_len
> 0) {
260 Curl
->noauth_url
= malloc(noauth_len
);
261 if (!Curl
->noauth_url
) {
262 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, "Memory allocation failed.\n");
265 make_noauth_url(Curl
, Curl
->noauth_url
, noauth_len
);
272 if (Curl
) url_free(Curl
);
277 url_free(URL_t
* url
) {
289 /* Replace escape sequences in an URL (or a part of an URL) */
290 /* works like strcpy(), but without return argument,
291 except that outbuf == inbuf is allowed */
293 url_unescape_string(char *outbuf
, const char *inbuf
)
295 unsigned char c
,c1
,c2
;
296 int i
,len
=strlen(inbuf
);
299 if (c
== '%' && i
<len
-2) { //must have 2 more chars
300 c1
= toupper(inbuf
[i
+1]); // we need uppercase characters
301 c2
= toupper(inbuf
[i
+2]);
302 if ( ((c1
>='0' && c1
<='9') || (c1
>='A' && c1
<='F')) &&
303 ((c2
>='0' && c2
<='9') || (c2
>='A' && c2
<='F')) ) {
304 if (c1
>='0' && c1
<='9') c1
-='0';
306 if (c2
>='0' && c2
<='9') c2
-='0';
309 i
=i
+2; //only skip next 2 chars if valid esc
314 *outbuf
++='\0'; //add nullterm to string
318 url_escape_string_part(char *outbuf
, const char *inbuf
) {
319 unsigned char c
,c1
,c2
;
320 int i
,len
=strlen(inbuf
);
322 for (i
=0;i
<len
;i
++) {
324 if ((c
=='%') && i
<len
-2 ) { //need 2 more characters
325 c1
=toupper(inbuf
[i
+1]); c2
=toupper(inbuf
[i
+2]); // need uppercase chars
327 c1
=129; c2
=129; //not escape chars
330 if( (c
>= 'A' && c
<= 'Z') ||
331 (c
>= 'a' && c
<= 'z') ||
332 (c
>= '0' && c
<= '9') ||
335 } else if ( c
=='%' && ((c1
>= '0' && c1
<= '9') || (c1
>= 'A' && c1
<= 'F')) &&
336 ((c2
>= '0' && c2
<= '9') || (c2
>= 'A' && c2
<= 'F'))) {
337 // check if part of an escape sequence
338 *outbuf
++=c
; // already
341 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"String appears to be already escaped in url_escape %c%c1%c2\n",c
,c1
,c2
);
342 // error as this should not happen against RFC 2396
343 // to escape a string twice
345 /* all others will be escaped */
346 c1
= ((c
& 0xf0) >> 4);
348 if (c1
< 10) c1
+='0';
350 if (c2
< 10) c2
+='0';
360 /* Replace specific characters in the URL string by an escape sequence */
361 /* works like strcpy(), but without return argument */
363 url_escape_string(char *outbuf
, const char *inbuf
) {
365 int i
= 0,j
,len
= strlen(inbuf
);
366 char* tmp
,*unesc
= NULL
, *in
;
368 // Look if we have an ip6 address, if so skip it there is
369 // no need to escape anything in there.
370 tmp
= strstr(inbuf
,"://[");
372 tmp
= strchr(tmp
+4,']');
373 if(tmp
&& (tmp
[1] == '/' || tmp
[1] == ':' ||
376 strncpy(outbuf
,inbuf
,i
);
384 // look for the next char that must be kept
385 for (j
=i
;j
<len
;j
++) {
387 if(c
=='-' || c
=='_' || c
=='.' || c
=='!' || c
=='~' || /* mark characters */
388 c
=='*' || c
=='\'' || c
=='(' || c
==')' || /* do not touch escape character */
389 c
==';' || c
=='/' || c
=='?' || c
==':' || c
=='@' || /* reserved characters */
390 c
=='&' || c
=='=' || c
=='+' || c
=='$' || c
==',') /* see RFC 2396 */
393 // we are on a reserved char, write it out
399 // we found one, take that part of the string
401 if(!tmp
) tmp
= malloc(len
+1);
402 strncpy(tmp
,inbuf
+i
,j
-i
);
405 } else // take the rest of the string
408 if(!unesc
) unesc
= malloc(len
+1);
409 // unescape first to avoid escaping escape
410 url_unescape_string(unesc
,in
);
411 // then escape, including mark and other reserved chars
412 // that can come from escape sequences
413 url_escape_string_part(outbuf
,unesc
);
414 outbuf
+= strlen(outbuf
);
424 url_debug(const URL_t
*url
) {
426 printf("URL pointer NULL\n");
429 if( url
->url
!=NULL
) {
430 printf("url=%s\n", url
->url
);
432 if( url
->protocol
!=NULL
) {
433 printf("protocol=%s\n", url
->protocol
);
435 if( url
->hostname
!=NULL
) {
436 printf("hostname=%s\n", url
->hostname
);
438 printf("port=%d\n", url
->port
);
439 if( url
->file
!=NULL
) {
440 printf("file=%s\n", url
->file
);
442 if( url
->username
!=NULL
) {
443 printf("username=%s\n", url
->username
);
445 if( url
->password
!=NULL
) {
446 printf("password=%s\n", url
->password
);
449 #endif /* URL_DEBUG */