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
);
61 url_new(const char* url
) {
62 int pos1
, pos2
,v6addr
= 0;
64 char *escfilename
=NULL
;
65 char *ptr1
=NULL
, *ptr2
=NULL
, *ptr3
=NULL
, *ptr4
=NULL
;
68 if( url
==NULL
) return NULL
;
70 if (strlen(url
) > (SIZE_MAX
/ 3 - 1)) {
71 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
74 escfilename
=malloc(strlen(url
)*3+1);
76 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
80 // Create the URL container
81 Curl
= malloc(sizeof(URL_t
));
83 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
87 // Initialisation of the URL container members
88 memset( Curl
, 0, sizeof(URL_t
) );
90 url_escape_string(escfilename
,url
);
92 // Copy the url in the URL container
93 Curl
->url
= strdup(escfilename
);
94 if( Curl
->url
==NULL
) {
95 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
98 mp_msg(MSGT_OPEN
,MSGL_V
,"Filename for url is now %s\n",escfilename
);
100 // extract the protocol
101 ptr1
= strstr(escfilename
, "://");
103 // Check for a special case: "sip:" (without "//"):
104 if (strstr(escfilename
, "sip:") == escfilename
) {
105 ptr1
= (char *)&url
[3]; // points to ':'
108 mp_msg(MSGT_NETWORK
,MSGL_V
,"Not an URL!\n");
112 pos1
= ptr1
-escfilename
;
113 Curl
->protocol
= malloc(pos1
+1);
114 if( Curl
->protocol
==NULL
) {
115 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
118 strncpy(Curl
->protocol
, escfilename
, pos1
);
119 Curl
->protocol
[pos1
] = '\0';
125 // check if a username:password is given
126 ptr2
= strstr(ptr1
, "@");
127 ptr3
= strstr(ptr1
, "/");
128 if( ptr3
!=NULL
&& ptr3
<ptr2
) {
129 // it isn't really a username but rather a part of the path
133 // We got something, at least a username...
135 Curl
->username
= malloc(len
+1);
136 if( Curl
->username
==NULL
) {
137 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
140 strncpy(Curl
->username
, ptr1
, len
);
141 Curl
->username
[len
] = '\0';
143 ptr3
= strstr(ptr1
, ":");
144 if( ptr3
!=NULL
&& ptr3
<ptr2
) {
145 // We also have a password
146 int len2
= ptr2
-ptr3
-1;
147 Curl
->username
[ptr3
-ptr1
]='\0';
148 Curl
->password
= malloc(len2
+1);
149 if( Curl
->password
==NULL
) {
150 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
153 strncpy( Curl
->password
, ptr3
+1, len2
);
154 Curl
->password
[len2
]='\0';
157 pos1
= ptr1
-escfilename
;
160 // before looking for a port number check if we have an IPv6 type numeric address
161 // in IPv6 URL the numeric address should be inside square braces.
162 ptr2
= strstr(ptr1
, "[");
163 ptr3
= strstr(ptr1
, "]");
164 ptr4
= strstr(ptr1
, "/");
165 if( ptr2
!=NULL
&& ptr3
!=NULL
&& ptr2
< ptr3
&& (!ptr4
|| ptr4
> ptr3
)) {
166 // we have an IPv6 numeric address
176 // look if the port is given
177 ptr2
= strstr(ptr2
, ":");
178 // If the : is after the first / it isn't the port
179 ptr3
= strstr(ptr1
, "/");
180 if(ptr3
&& ptr3
- ptr2
< 0) ptr2
= NULL
;
183 // Look if a path is given
186 // So we have an URL like http://www.hostname.com
187 pos2
= strlen(escfilename
);
189 // We have an URL like http://www.hostname.com/file.txt
190 pos2
= ptr3
-escfilename
;
193 // We have an URL beginning like http://www.hostname.com:1212
194 // Get the port number
195 Curl
->port
= atoi(ptr2
+1);
196 pos2
= ptr2
-escfilename
;
199 // copy the hostname in the URL container
200 Curl
->hostname
= malloc(pos2
-pos1
+1);
201 if( Curl
->hostname
==NULL
) {
202 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
205 strncpy(Curl
->hostname
, ptr1
, pos2
-pos1
);
206 Curl
->hostname
[pos2
-pos1
] = '\0';
208 // Look if a path is given
209 ptr2
= strstr(ptr1
, "/");
211 // A path/filename is given
212 // check if it's not a trailing '/'
213 if( strlen(ptr2
)>1 ) {
214 // copy the path/filename in the URL container
215 Curl
->file
= strdup(ptr2
);
216 if( Curl
->file
==NULL
) {
217 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
222 // Check if a filename was given or set, else set it with '/'
223 if( Curl
->file
==NULL
) {
224 Curl
->file
= malloc(2);
225 if( Curl
->file
==NULL
) {
226 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
229 strcpy(Curl
->file
, "/");
235 if (escfilename
) free(escfilename
);
236 if (Curl
) url_free(Curl
);
241 url_free(URL_t
* url
) {
243 if(url
->url
) free(url
->url
);
244 if(url
->protocol
) free(url
->protocol
);
245 if(url
->hostname
) free(url
->hostname
);
246 if(url
->file
) free(url
->file
);
247 if(url
->username
) free(url
->username
);
248 if(url
->password
) free(url
->password
);
253 /* Replace escape sequences in an URL (or a part of an URL) */
254 /* works like strcpy(), but without return argument */
256 url_unescape_string(char *outbuf
, const char *inbuf
)
258 unsigned char c
,c1
,c2
;
259 int i
,len
=strlen(inbuf
);
262 if (c
== '%' && i
<len
-2) { //must have 2 more chars
263 c1
= toupper(inbuf
[i
+1]); // we need uppercase characters
264 c2
= toupper(inbuf
[i
+2]);
265 if ( ((c1
>='0' && c1
<='9') || (c1
>='A' && c1
<='F')) &&
266 ((c2
>='0' && c2
<='9') || (c2
>='A' && c2
<='F')) ) {
267 if (c1
>='0' && c1
<='9') c1
-='0';
269 if (c2
>='0' && c2
<='9') c2
-='0';
272 i
=i
+2; //only skip next 2 chars if valid esc
277 *outbuf
++='\0'; //add nullterm to string
281 url_escape_string_part(char *outbuf
, const char *inbuf
) {
282 unsigned char c
,c1
,c2
;
283 int i
,len
=strlen(inbuf
);
285 for (i
=0;i
<len
;i
++) {
287 if ((c
=='%') && i
<len
-2 ) { //need 2 more characters
288 c1
=toupper(inbuf
[i
+1]); c2
=toupper(inbuf
[i
+2]); // need uppercase chars
290 c1
=129; c2
=129; //not escape chars
293 if( (c
>= 'A' && c
<= 'Z') ||
294 (c
>= 'a' && c
<= 'z') ||
295 (c
>= '0' && c
<= '9') ||
298 } else if ( c
=='%' && ((c1
>= '0' && c1
<= '9') || (c1
>= 'A' && c1
<= 'F')) &&
299 ((c2
>= '0' && c2
<= '9') || (c2
>= 'A' && c2
<= 'F'))) {
300 // check if part of an escape sequence
301 *outbuf
++=c
; // already
304 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"String appears to be already escaped in url_escape %c%c1%c2\n",c
,c1
,c2
);
305 // error as this should not happen against RFC 2396
306 // to escape a string twice
308 /* all others will be escaped */
309 c1
= ((c
& 0xf0) >> 4);
311 if (c1
< 10) c1
+='0';
313 if (c2
< 10) c2
+='0';
323 /* Replace specific characters in the URL string by an escape sequence */
324 /* works like strcpy(), but without return argument */
326 url_escape_string(char *outbuf
, const char *inbuf
) {
328 int i
= 0,j
,len
= strlen(inbuf
);
329 char* tmp
,*unesc
= NULL
, *in
;
331 // Look if we have an ip6 address, if so skip it there is
332 // no need to escape anything in there.
333 tmp
= strstr(inbuf
,"://[");
335 tmp
= strchr(tmp
+4,']');
336 if(tmp
&& (tmp
[1] == '/' || tmp
[1] == ':' ||
339 strncpy(outbuf
,inbuf
,i
);
347 // look for the next char that must be kept
348 for (j
=i
;j
<len
;j
++) {
350 if(c
=='-' || c
=='_' || c
=='.' || c
=='!' || c
=='~' || /* mark characters */
351 c
=='*' || c
=='\'' || c
=='(' || c
==')' || /* do not touch escape character */
352 c
==';' || c
=='/' || c
=='?' || c
==':' || c
=='@' || /* reserved characters */
353 c
=='&' || c
=='=' || c
=='+' || c
=='$' || c
==',') /* see RFC 2396 */
356 // we are on a reserved char, write it out
362 // we found one, take that part of the string
364 if(!tmp
) tmp
= malloc(len
+1);
365 strncpy(tmp
,inbuf
+i
,j
-i
);
368 } else // take the rest of the string
371 if(!unesc
) unesc
= malloc(len
+1);
372 // unescape first to avoid escaping escape
373 url_unescape_string(unesc
,in
);
374 // then escape, including mark and other reserved chars
375 // that can come from escape sequences
376 url_escape_string_part(outbuf
,unesc
);
377 outbuf
+= strlen(outbuf
);
382 if(unesc
) free(unesc
);
387 url_debug(const URL_t
*url
) {
389 printf("URL pointer NULL\n");
392 if( url
->url
!=NULL
) {
393 printf("url=%s\n", url
->url
);
395 if( url
->protocol
!=NULL
) {
396 printf("protocol=%s\n", url
->protocol
);
398 if( url
->hostname
!=NULL
) {
399 printf("hostname=%s\n", url
->hostname
);
401 printf("port=%d\n", url
->port
);
402 if( url
->file
!=NULL
) {
403 printf("file=%s\n", url
->file
);
405 if( url
->username
!=NULL
) {
406 printf("username=%s\n", url
->username
);
408 if( url
->password
!=NULL
) {
409 printf("password=%s\n", url
->password
);
412 #endif /* URL_DEBUG */