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.
34 #define SIZE_MAX ((size_t)-1)
37 URL_t
*url_redirect(URL_t
**url
, const char *redir
) {
40 if (!strchr(redir
, '/') || *redir
== '/') {
42 char *newurl
= malloc(strlen(u
->url
) + strlen(redir
) + 1);
43 strcpy(newurl
, u
->url
);
46 tmp
= strstr(newurl
, "://");
47 if (tmp
) tmp
= strchr(tmp
+ 3, '/');
49 tmp
= strrchr(newurl
, '/');
51 strcat(newurl
, redir
);
52 res
= url_new(newurl
);
62 url_new(const char* url
) {
63 int pos1
, pos2
,v6addr
= 0;
65 char *escfilename
=NULL
;
66 char *ptr1
=NULL
, *ptr2
=NULL
, *ptr3
=NULL
, *ptr4
=NULL
;
69 if( url
==NULL
) return NULL
;
71 if (strlen(url
) > (SIZE_MAX
/ 3 - 1)) {
72 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
75 escfilename
=malloc(strlen(url
)*3+1);
77 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
81 // Create the URL container
82 Curl
= malloc(sizeof(URL_t
));
84 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
88 // Initialisation of the URL container members
89 memset( Curl
, 0, sizeof(URL_t
) );
91 url_escape_string(escfilename
,url
);
93 // Copy the url in the URL container
94 Curl
->url
= strdup(escfilename
);
95 if( Curl
->url
==NULL
) {
96 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
99 mp_msg(MSGT_OPEN
,MSGL_V
,"Filename for url is now %s\n",escfilename
);
101 // extract the protocol
102 ptr1
= strstr(escfilename
, "://");
104 // Check for a special case: "sip:" (without "//"):
105 if (strstr(escfilename
, "sip:") == escfilename
) {
106 ptr1
= (char *)&url
[3]; // points to ':'
109 mp_msg(MSGT_NETWORK
,MSGL_V
,"Not an URL!\n");
113 pos1
= ptr1
-escfilename
;
114 Curl
->protocol
= malloc(pos1
+1);
115 if( Curl
->protocol
==NULL
) {
116 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
119 strncpy(Curl
->protocol
, escfilename
, pos1
);
120 Curl
->protocol
[pos1
] = '\0';
126 // check if a username:password is given
127 ptr2
= strstr(ptr1
, "@");
128 ptr3
= strstr(ptr1
, "/");
129 if( ptr3
!=NULL
&& ptr3
<ptr2
) {
130 // it isn't really a username but rather a part of the path
134 // We got something, at least a username...
136 Curl
->username
= malloc(len
+1);
137 if( Curl
->username
==NULL
) {
138 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
141 strncpy(Curl
->username
, ptr1
, len
);
142 Curl
->username
[len
] = '\0';
144 ptr3
= strstr(ptr1
, ":");
145 if( ptr3
!=NULL
&& ptr3
<ptr2
) {
146 // We also have a password
147 int len2
= ptr2
-ptr3
-1;
148 Curl
->username
[ptr3
-ptr1
]='\0';
149 Curl
->password
= malloc(len2
+1);
150 if( Curl
->password
==NULL
) {
151 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
154 strncpy( Curl
->password
, ptr3
+1, len2
);
155 Curl
->password
[len2
]='\0';
158 pos1
= ptr1
-escfilename
;
161 // before looking for a port number check if we have an IPv6 type numeric address
162 // in IPv6 URL the numeric address should be inside square braces.
163 ptr2
= strstr(ptr1
, "[");
164 ptr3
= strstr(ptr1
, "]");
165 ptr4
= strstr(ptr1
, "/");
166 if( ptr2
!=NULL
&& ptr3
!=NULL
&& ptr2
< ptr3
&& (!ptr4
|| ptr4
> ptr3
)) {
167 // we have an IPv6 numeric address
177 // look if the port is given
178 ptr2
= strstr(ptr2
, ":");
179 // If the : is after the first / it isn't the port
180 ptr3
= strstr(ptr1
, "/");
181 if(ptr3
&& ptr3
- ptr2
< 0) ptr2
= NULL
;
184 // Look if a path is given
187 // So we have an URL like http://www.hostname.com
188 pos2
= strlen(escfilename
);
190 // We have an URL like http://www.hostname.com/file.txt
191 pos2
= ptr3
-escfilename
;
194 // We have an URL beginning like http://www.hostname.com:1212
195 // Get the port number
196 Curl
->port
= atoi(ptr2
+1);
197 pos2
= ptr2
-escfilename
;
200 // copy the hostname in the URL container
201 Curl
->hostname
= malloc(pos2
-pos1
+1);
202 if( Curl
->hostname
==NULL
) {
203 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
206 strncpy(Curl
->hostname
, ptr1
, pos2
-pos1
);
207 Curl
->hostname
[pos2
-pos1
] = '\0';
209 // Look if a path is given
210 ptr2
= strstr(ptr1
, "/");
212 // A path/filename is given
213 // check if it's not a trailing '/'
214 if( strlen(ptr2
)>1 ) {
215 // copy the path/filename in the URL container
216 Curl
->file
= strdup(ptr2
);
217 if( Curl
->file
==NULL
) {
218 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
223 // Check if a filename was given or set, else set it with '/'
224 if( Curl
->file
==NULL
) {
225 Curl
->file
= malloc(2);
226 if( Curl
->file
==NULL
) {
227 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
230 strcpy(Curl
->file
, "/");
236 if (escfilename
) free(escfilename
);
237 if (Curl
) url_free(Curl
);
242 url_free(URL_t
* url
) {
244 if(url
->url
) free(url
->url
);
245 if(url
->protocol
) free(url
->protocol
);
246 if(url
->hostname
) free(url
->hostname
);
247 if(url
->file
) free(url
->file
);
248 if(url
->username
) free(url
->username
);
249 if(url
->password
) free(url
->password
);
254 /* Replace escape sequences in an URL (or a part of an URL) */
255 /* works like strcpy(), but without return argument */
257 url_unescape_string(char *outbuf
, const char *inbuf
)
259 unsigned char c
,c1
,c2
;
260 int i
,len
=strlen(inbuf
);
263 if (c
== '%' && i
<len
-2) { //must have 2 more chars
264 c1
= toupper(inbuf
[i
+1]); // we need uppercase characters
265 c2
= toupper(inbuf
[i
+2]);
266 if ( ((c1
>='0' && c1
<='9') || (c1
>='A' && c1
<='F')) &&
267 ((c2
>='0' && c2
<='9') || (c2
>='A' && c2
<='F')) ) {
268 if (c1
>='0' && c1
<='9') c1
-='0';
270 if (c2
>='0' && c2
<='9') c2
-='0';
273 i
=i
+2; //only skip next 2 chars if valid esc
278 *outbuf
++='\0'; //add nullterm to string
282 url_escape_string_part(char *outbuf
, const char *inbuf
) {
283 unsigned char c
,c1
,c2
;
284 int i
,len
=strlen(inbuf
);
286 for (i
=0;i
<len
;i
++) {
288 if ((c
=='%') && i
<len
-2 ) { //need 2 more characters
289 c1
=toupper(inbuf
[i
+1]); c2
=toupper(inbuf
[i
+2]); // need uppercase chars
291 c1
=129; c2
=129; //not escape chars
294 if( (c
>= 'A' && c
<= 'Z') ||
295 (c
>= 'a' && c
<= 'z') ||
296 (c
>= '0' && c
<= '9') ||
299 } else if ( c
=='%' && ((c1
>= '0' && c1
<= '9') || (c1
>= 'A' && c1
<= 'F')) &&
300 ((c2
>= '0' && c2
<= '9') || (c2
>= 'A' && c2
<= 'F'))) {
301 // check if part of an escape sequence
302 *outbuf
++=c
; // already
305 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_URL_StringAlreadyEscaped
,c
,c1
,c2
);
306 // error as this should not happen against RFC 2396
307 // to escape a string twice
309 /* all others will be escaped */
310 c1
= ((c
& 0xf0) >> 4);
312 if (c1
< 10) c1
+='0';
314 if (c2
< 10) c2
+='0';
324 /* Replace specific characters in the URL string by an escape sequence */
325 /* works like strcpy(), but without return argument */
327 url_escape_string(char *outbuf
, const char *inbuf
) {
329 int i
= 0,j
,len
= strlen(inbuf
);
330 char* tmp
,*unesc
= NULL
, *in
;
332 // Look if we have an ip6 address, if so skip it there is
333 // no need to escape anything in there.
334 tmp
= strstr(inbuf
,"://[");
336 tmp
= strchr(tmp
+4,']');
337 if(tmp
&& (tmp
[1] == '/' || tmp
[1] == ':' ||
340 strncpy(outbuf
,inbuf
,i
);
348 // look for the next char that must be kept
349 for (j
=i
;j
<len
;j
++) {
351 if(c
=='-' || c
=='_' || c
=='.' || c
=='!' || c
=='~' || /* mark characters */
352 c
=='*' || c
=='\'' || c
=='(' || c
==')' || /* do not touch escape character */
353 c
==';' || c
=='/' || c
=='?' || c
==':' || c
=='@' || /* reserved characters */
354 c
=='&' || c
=='=' || c
=='+' || c
=='$' || c
==',') /* see RFC 2396 */
357 // we are on a reserved char, write it out
363 // we found one, take that part of the string
365 if(!tmp
) tmp
= malloc(len
+1);
366 strncpy(tmp
,inbuf
+i
,j
-i
);
369 } else // take the rest of the string
372 if(!unesc
) unesc
= malloc(len
+1);
373 // unescape first to avoid escaping escape
374 url_unescape_string(unesc
,in
);
375 // then escape, including mark and other reserved chars
376 // that can come from escape sequences
377 url_escape_string_part(outbuf
,unesc
);
378 outbuf
+= strlen(outbuf
);
383 if(unesc
) free(unesc
);
388 url_debug(const URL_t
*url
) {
390 printf("URL pointer NULL\n");
393 if( url
->url
!=NULL
) {
394 printf("url=%s\n", url
->url
);
396 if( url
->protocol
!=NULL
) {
397 printf("protocol=%s\n", url
->protocol
);
399 if( url
->hostname
!=NULL
) {
400 printf("hostname=%s\n", url
->hostname
);
402 printf("port=%d\n", url
->port
);
403 if( url
->file
!=NULL
) {
404 printf("file=%s\n", url
->file
);
406 if( url
->username
!=NULL
) {
407 printf("username=%s\n", url
->username
);
409 if( url
->password
!=NULL
) {
410 printf("password=%s\n", url
->password
);
413 #endif /* URL_DEBUG */