font_load_ft.c: disable -fontconfig if compiled without libfontconfig
[mplayer/glamo.git] / stream / url.c
blob0d64382e2f2cec705fea6e08f3cee84dc8c69627
1 /*
2 * URL Helper
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.
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <inttypes.h>
29 #include "url.h"
30 #include "mp_msg.h"
32 #ifndef SIZE_MAX
33 #define SIZE_MAX ((size_t)-1)
34 #endif
36 URL_t *url_redirect(URL_t **url, const char *redir) {
37 URL_t *u = *url;
38 URL_t *res;
39 if (!strchr(redir, '/') || *redir == '/') {
40 char *tmp;
41 char *newurl = malloc(strlen(u->url) + strlen(redir) + 1);
42 strcpy(newurl, u->url);
43 if (*redir == '/') {
44 redir++;
45 tmp = strstr(newurl, "://");
46 if (tmp) tmp = strchr(tmp + 3, '/');
47 } else
48 tmp = strrchr(newurl, '/');
49 if (tmp) tmp[1] = 0;
50 strcat(newurl, redir);
51 res = url_new(newurl);
52 free(newurl);
53 } else
54 res = url_new(redir);
55 url_free(u);
56 *url = res;
57 return res;
60 URL_t*
61 url_new(const char* url) {
62 int pos1, pos2,v6addr = 0;
63 URL_t* Curl = NULL;
64 char *escfilename=NULL;
65 char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL;
66 int jumpSize = 3;
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");
72 goto err_out;
74 escfilename=malloc(strlen(url)*3+1);
75 if (!escfilename ) {
76 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
77 goto err_out;
80 // Create the URL container
81 Curl = malloc(sizeof(URL_t));
82 if( Curl==NULL ) {
83 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
84 goto err_out;
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");
96 goto err_out;
98 mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename);
100 // extract the protocol
101 ptr1 = strstr(escfilename, "://");
102 if( ptr1==NULL ) {
103 // Check for a special case: "sip:" (without "//"):
104 if (strstr(escfilename, "sip:") == escfilename) {
105 ptr1 = (char *)&url[3]; // points to ':'
106 jumpSize = 1;
107 } else {
108 mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n");
109 goto err_out;
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");
116 goto err_out;
118 strncpy(Curl->protocol, escfilename, pos1);
119 Curl->protocol[pos1] = '\0';
121 // jump the "://"
122 ptr1 += jumpSize;
123 pos1 += jumpSize;
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
130 ptr2 = NULL;
132 if( ptr2!=NULL ) {
133 // We got something, at least a username...
134 int len = ptr2-ptr1;
135 Curl->username = malloc(len+1);
136 if( Curl->username==NULL ) {
137 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
138 goto err_out;
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");
151 goto err_out;
153 strncpy( Curl->password, ptr3+1, len2);
154 Curl->password[len2]='\0';
155 url_unescape_string(Curl->password, Curl->password);
157 url_unescape_string(Curl->username, Curl->username);
158 ptr1 = ptr2+1;
159 pos1 = ptr1-escfilename;
162 // before looking for a port number check if we have an IPv6 type numeric address
163 // in IPv6 URL the numeric address should be inside square braces.
164 ptr2 = strstr(ptr1, "[");
165 ptr3 = strstr(ptr1, "]");
166 ptr4 = strstr(ptr1, "/");
167 if( ptr2!=NULL && ptr3!=NULL && ptr2 < ptr3 && (!ptr4 || ptr4 > ptr3)) {
168 // we have an IPv6 numeric address
169 ptr1++;
170 pos1++;
171 ptr2 = ptr3;
172 v6addr = 1;
173 } else {
174 ptr2 = ptr1;
178 // look if the port is given
179 ptr2 = strstr(ptr2, ":");
180 // If the : is after the first / it isn't the port
181 ptr3 = strstr(ptr1, "/");
182 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL;
183 if( ptr2==NULL ) {
184 // No port is given
185 // Look if a path is given
186 if( ptr3==NULL ) {
187 // No path/filename
188 // So we have an URL like http://www.hostname.com
189 pos2 = strlen(escfilename);
190 } else {
191 // We have an URL like http://www.hostname.com/file.txt
192 pos2 = ptr3-escfilename;
194 } else {
195 // We have an URL beginning like http://www.hostname.com:1212
196 // Get the port number
197 Curl->port = atoi(ptr2+1);
198 pos2 = ptr2-escfilename;
200 if( v6addr ) pos2--;
201 // copy the hostname in the URL container
202 Curl->hostname = malloc(pos2-pos1+1);
203 if( Curl->hostname==NULL ) {
204 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
205 goto err_out;
207 strncpy(Curl->hostname, ptr1, pos2-pos1);
208 Curl->hostname[pos2-pos1] = '\0';
210 // Look if a path is given
211 ptr2 = strstr(ptr1, "/");
212 if( ptr2!=NULL ) {
213 // A path/filename is given
214 // check if it's not a trailing '/'
215 if( strlen(ptr2)>1 ) {
216 // copy the path/filename in the URL container
217 Curl->file = strdup(ptr2);
218 if( Curl->file==NULL ) {
219 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
220 goto err_out;
224 // Check if a filename was given or set, else set it with '/'
225 if( Curl->file==NULL ) {
226 Curl->file = malloc(2);
227 if( Curl->file==NULL ) {
228 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
229 goto err_out;
231 strcpy(Curl->file, "/");
234 free(escfilename);
235 return Curl;
236 err_out:
237 free(escfilename);
238 if (Curl) url_free(Curl);
239 return NULL;
242 void
243 url_free(URL_t* url) {
244 if(!url) return;
245 free(url->url);
246 free(url->protocol);
247 free(url->hostname);
248 free(url->file);
249 free(url->username);
250 free(url->password);
251 free(url);
255 /* Replace escape sequences in an URL (or a part of an URL) */
256 /* works like strcpy(), but without return argument,
257 except that outbuf == inbuf is allowed */
258 void
259 url_unescape_string(char *outbuf, const char *inbuf)
261 unsigned char c,c1,c2;
262 int i,len=strlen(inbuf);
263 for (i=0;i<len;i++){
264 c = inbuf[i];
265 if (c == '%' && i<len-2) { //must have 2 more chars
266 c1 = toupper(inbuf[i+1]); // we need uppercase characters
267 c2 = toupper(inbuf[i+2]);
268 if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) &&
269 ((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) {
270 if (c1>='0' && c1<='9') c1-='0';
271 else c1-='A'-10;
272 if (c2>='0' && c2<='9') c2-='0';
273 else c2-='A'-10;
274 c = (c1<<4) + c2;
275 i=i+2; //only skip next 2 chars if valid esc
278 *outbuf++ = c;
280 *outbuf++='\0'; //add nullterm to string
283 static void
284 url_escape_string_part(char *outbuf, const char *inbuf) {
285 unsigned char c,c1,c2;
286 int i,len=strlen(inbuf);
288 for (i=0;i<len;i++) {
289 c = inbuf[i];
290 if ((c=='%') && i<len-2 ) { //need 2 more characters
291 c1=toupper(inbuf[i+1]); c2=toupper(inbuf[i+2]); // need uppercase chars
292 } else {
293 c1=129; c2=129; //not escape chars
296 if( (c >= 'A' && c <= 'Z') ||
297 (c >= 'a' && c <= 'z') ||
298 (c >= '0' && c <= '9') ||
299 (c >= 0x7f)) {
300 *outbuf++ = c;
301 } else if ( c=='%' && ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) &&
302 ((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) {
303 // check if part of an escape sequence
304 *outbuf++=c; // already
306 // dont escape again
307 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"String appears to be already escaped in url_escape %c%c1%c2\n",c,c1,c2);
308 // error as this should not happen against RFC 2396
309 // to escape a string twice
310 } else {
311 /* all others will be escaped */
312 c1 = ((c & 0xf0) >> 4);
313 c2 = (c & 0x0f);
314 if (c1 < 10) c1+='0';
315 else c1+='A'-10;
316 if (c2 < 10) c2+='0';
317 else c2+='A'-10;
318 *outbuf++ = '%';
319 *outbuf++ = c1;
320 *outbuf++ = c2;
323 *outbuf++='\0';
326 /* Replace specific characters in the URL string by an escape sequence */
327 /* works like strcpy(), but without return argument */
328 void
329 url_escape_string(char *outbuf, const char *inbuf) {
330 unsigned char c;
331 int i = 0,j,len = strlen(inbuf);
332 char* tmp,*unesc = NULL, *in;
334 // Look if we have an ip6 address, if so skip it there is
335 // no need to escape anything in there.
336 tmp = strstr(inbuf,"://[");
337 if(tmp) {
338 tmp = strchr(tmp+4,']');
339 if(tmp && (tmp[1] == '/' || tmp[1] == ':' ||
340 tmp[1] == '\0')) {
341 i = tmp+1-inbuf;
342 strncpy(outbuf,inbuf,i);
343 outbuf += i;
344 tmp = NULL;
348 tmp = NULL;
349 while(i < len) {
350 // look for the next char that must be kept
351 for (j=i;j<len;j++) {
352 c = inbuf[j];
353 if(c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */
354 c=='*' || c=='\'' || c=='(' || c==')' || /* do not touch escape character */
355 c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */
356 c=='&' || c=='=' || c=='+' || c=='$' || c==',') /* see RFC 2396 */
357 break;
359 // we are on a reserved char, write it out
360 if(j == i) {
361 *outbuf++ = c;
362 i++;
363 continue;
365 // we found one, take that part of the string
366 if(j < len) {
367 if(!tmp) tmp = malloc(len+1);
368 strncpy(tmp,inbuf+i,j-i);
369 tmp[j-i] = '\0';
370 in = tmp;
371 } else // take the rest of the string
372 in = (char*)inbuf+i;
374 if(!unesc) unesc = malloc(len+1);
375 // unescape first to avoid escaping escape
376 url_unescape_string(unesc,in);
377 // then escape, including mark and other reserved chars
378 // that can come from escape sequences
379 url_escape_string_part(outbuf,unesc);
380 outbuf += strlen(outbuf);
381 i += strlen(in);
383 *outbuf = '\0';
384 free(tmp);
385 free(unesc);
388 #ifdef URL_DEBUG
389 void
390 url_debug(const URL_t *url) {
391 if( url==NULL ) {
392 printf("URL pointer NULL\n");
393 return;
395 if( url->url!=NULL ) {
396 printf("url=%s\n", url->url );
398 if( url->protocol!=NULL ) {
399 printf("protocol=%s\n", url->protocol );
401 if( url->hostname!=NULL ) {
402 printf("hostname=%s\n", url->hostname );
404 printf("port=%d\n", url->port );
405 if( url->file!=NULL ) {
406 printf("file=%s\n", url->file );
408 if( url->username!=NULL ) {
409 printf("username=%s\n", url->username );
411 if( url->password!=NULL ) {
412 printf("password=%s\n", url->password );
415 #endif /* URL_DEBUG */