mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / stream / url.c
blob2c241ee7ee88ecb0da6f10cc72915fb6e1456ac8
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 static int make_noauth_url(URL_t *url, char *dst, int dst_size)
62 if (url->port)
63 return snprintf(dst, dst_size, "%s://%s:%d%s", url->protocol,
64 url->hostname, url->port, url->file);
65 else
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,
71 int dst_size)
73 if (proxy->username)
74 return snprintf(dst, dst_size, "http_proxy://%s:%s@%s:%d/%s",
75 proxy->username,
76 proxy->password ? proxy->password : "",
77 proxy->hostname, proxy->port, host_url);
78 else
79 return snprintf(dst, dst_size, "http_proxy://%s:%d/%s",
80 proxy->hostname, proxy->port, host_url);
83 URL_t*
84 url_new(const char* url) {
85 int pos1, pos2,v6addr = 0, noauth_len;
86 URL_t* Curl = NULL;
87 char *escfilename=NULL;
88 char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL;
89 int jumpSize = 3;
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");
95 goto err_out;
97 escfilename=malloc(strlen(url)*3+1);
98 if (!escfilename ) {
99 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
100 goto err_out;
103 // Create the URL container
104 Curl = calloc(1, sizeof(*Curl));
105 if( Curl==NULL ) {
106 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
107 goto err_out;
110 url_escape_string(escfilename,url);
112 // Copy the url in the URL container
113 Curl->url = strdup(escfilename);
114 if( Curl->url==NULL ) {
115 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
116 goto err_out;
118 mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename);
120 // extract the protocol
121 ptr1 = strstr(escfilename, "://");
122 if( ptr1==NULL ) {
123 // Check for a special case: "sip:" (without "//"):
124 if (strstr(escfilename, "sip:") == escfilename) {
125 ptr1 = (char *)&url[3]; // points to ':'
126 jumpSize = 1;
127 } else {
128 mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n");
129 goto err_out;
132 pos1 = ptr1-escfilename;
133 Curl->protocol = malloc(pos1+1);
134 if( Curl->protocol==NULL ) {
135 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
136 goto err_out;
138 strncpy(Curl->protocol, escfilename, pos1);
139 Curl->protocol[pos1] = '\0';
141 // jump the "://"
142 ptr1 += jumpSize;
143 pos1 += jumpSize;
145 // check if a username:password is given
146 ptr2 = strstr(ptr1, "@");
147 ptr3 = strstr(ptr1, "/");
148 if( ptr3!=NULL && ptr3<ptr2 ) {
149 // it isn't really a username but rather a part of the path
150 ptr2 = NULL;
152 if( ptr2!=NULL ) {
153 // We got something, at least a username...
154 int len = ptr2-ptr1;
155 Curl->username = malloc(len+1);
156 if( Curl->username==NULL ) {
157 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
158 goto err_out;
160 strncpy(Curl->username, ptr1, len);
161 Curl->username[len] = '\0';
163 ptr3 = strstr(ptr1, ":");
164 if( ptr3!=NULL && ptr3<ptr2 ) {
165 // We also have a password
166 int len2 = ptr2-ptr3-1;
167 Curl->username[ptr3-ptr1]='\0';
168 Curl->password = malloc(len2+1);
169 if( Curl->password==NULL ) {
170 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
171 goto err_out;
173 strncpy( Curl->password, ptr3+1, len2);
174 Curl->password[len2]='\0';
175 url_unescape_string(Curl->password, Curl->password);
177 url_unescape_string(Curl->username, Curl->username);
178 ptr1 = ptr2+1;
179 pos1 = ptr1-escfilename;
182 // before looking for a port number check if we have an IPv6 type numeric address
183 // in IPv6 URL the numeric address should be inside square braces.
184 ptr2 = strstr(ptr1, "[");
185 ptr3 = strstr(ptr1, "]");
186 ptr4 = strstr(ptr1, "/");
187 if( ptr2!=NULL && ptr3!=NULL && ptr2 < ptr3 && (!ptr4 || ptr4 > ptr3)) {
188 // we have an IPv6 numeric address
189 ptr1++;
190 pos1++;
191 ptr2 = ptr3;
192 v6addr = 1;
193 } else {
194 ptr2 = ptr1;
198 // look if the port is given
199 ptr2 = strstr(ptr2, ":");
200 // If the : is after the first / it isn't the port
201 ptr3 = strstr(ptr1, "/");
202 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL;
203 if( ptr2==NULL ) {
204 // No port is given
205 // Look if a path is given
206 if( ptr3==NULL ) {
207 // No path/filename
208 // So we have an URL like http://www.hostname.com
209 pos2 = strlen(escfilename);
210 } else {
211 // We have an URL like http://www.hostname.com/file.txt
212 pos2 = ptr3-escfilename;
214 } else {
215 // We have an URL beginning like http://www.hostname.com:1212
216 // Get the port number
217 Curl->port = atoi(ptr2+1);
218 pos2 = ptr2-escfilename;
220 if( v6addr ) pos2--;
221 // copy the hostname in the URL container
222 Curl->hostname = malloc(pos2-pos1+1);
223 if( Curl->hostname==NULL ) {
224 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
225 goto err_out;
227 strncpy(Curl->hostname, ptr1, pos2-pos1);
228 Curl->hostname[pos2-pos1] = '\0';
230 // Look if a path is given
231 ptr2 = strstr(ptr1, "/");
232 if( ptr2!=NULL ) {
233 // A path/filename is given
234 // check if it's not a trailing '/'
235 if( strlen(ptr2)>1 ) {
236 // copy the path/filename in the URL container
237 Curl->file = strdup(ptr2);
238 if( Curl->file==NULL ) {
239 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
240 goto err_out;
244 // Check if a filename was given or set, else set it with '/'
245 if( Curl->file==NULL ) {
246 Curl->file = malloc(2);
247 if( Curl->file==NULL ) {
248 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
249 goto err_out;
251 strcpy(Curl->file, "/");
254 noauth_len = make_noauth_url(Curl, NULL, 0);
255 if (noauth_len > 0) {
256 noauth_len++;
257 Curl->noauth_url = malloc(noauth_len);
258 if (!Curl->noauth_url) {
259 mp_msg(MSGT_NETWORK, MSGL_FATAL, "Memory allocation failed.\n");
260 goto err_out;
262 make_noauth_url(Curl, Curl->noauth_url, noauth_len);
265 free(escfilename);
266 return Curl;
267 err_out:
268 free(escfilename);
269 if (Curl) url_free(Curl);
270 return NULL;
273 void
274 url_free(URL_t* url) {
275 if(!url) return;
276 free(url->url);
277 free(url->protocol);
278 free(url->hostname);
279 free(url->file);
280 free(url->username);
281 free(url->password);
282 free(url);
286 /* Replace escape sequences in an URL (or a part of an URL) */
287 /* works like strcpy(), but without return argument,
288 except that outbuf == inbuf is allowed */
289 void
290 url_unescape_string(char *outbuf, const char *inbuf)
292 unsigned char c,c1,c2;
293 int i,len=strlen(inbuf);
294 for (i=0;i<len;i++){
295 c = inbuf[i];
296 if (c == '%' && i<len-2) { //must have 2 more chars
297 c1 = toupper(inbuf[i+1]); // we need uppercase characters
298 c2 = toupper(inbuf[i+2]);
299 if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) &&
300 ((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) {
301 if (c1>='0' && c1<='9') c1-='0';
302 else c1-='A'-10;
303 if (c2>='0' && c2<='9') c2-='0';
304 else c2-='A'-10;
305 c = (c1<<4) + c2;
306 i=i+2; //only skip next 2 chars if valid esc
309 *outbuf++ = c;
311 *outbuf++='\0'; //add nullterm to string
314 static void
315 url_escape_string_part(char *outbuf, const char *inbuf) {
316 unsigned char c,c1,c2;
317 int i,len=strlen(inbuf);
319 for (i=0;i<len;i++) {
320 c = inbuf[i];
321 if ((c=='%') && i<len-2 ) { //need 2 more characters
322 c1=toupper(inbuf[i+1]); c2=toupper(inbuf[i+2]); // need uppercase chars
323 } else {
324 c1=129; c2=129; //not escape chars
327 if( (c >= 'A' && c <= 'Z') ||
328 (c >= 'a' && c <= 'z') ||
329 (c >= '0' && c <= '9')) {
330 *outbuf++ = c;
331 } else if ( c=='%' && ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) &&
332 ((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) {
333 // check if part of an escape sequence
334 *outbuf++=c; // already
336 // dont escape again
337 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"String appears to be already escaped in url_escape %c%c1%c2\n",c,c1,c2);
338 // error as this should not happen against RFC 2396
339 // to escape a string twice
340 } else {
341 /* all others will be escaped */
342 c1 = ((c & 0xf0) >> 4);
343 c2 = (c & 0x0f);
344 if (c1 < 10) c1+='0';
345 else c1+='A'-10;
346 if (c2 < 10) c2+='0';
347 else c2+='A'-10;
348 *outbuf++ = '%';
349 *outbuf++ = c1;
350 *outbuf++ = c2;
353 *outbuf++='\0';
356 /* Replace specific characters in the URL string by an escape sequence */
357 /* works like strcpy(), but without return argument */
358 void
359 url_escape_string(char *outbuf, const char *inbuf) {
360 unsigned char c;
361 int i = 0,j,len = strlen(inbuf);
362 char* tmp,*unesc = NULL, *in;
364 // Look if we have an ip6 address, if so skip it there is
365 // no need to escape anything in there.
366 tmp = strstr(inbuf,"://[");
367 if(tmp) {
368 tmp = strchr(tmp+4,']');
369 if(tmp && (tmp[1] == '/' || tmp[1] == ':' ||
370 tmp[1] == '\0')) {
371 i = tmp+1-inbuf;
372 strncpy(outbuf,inbuf,i);
373 outbuf += i;
374 tmp = NULL;
378 tmp = NULL;
379 while(i < len) {
380 // look for the next char that must be kept
381 for (j=i;j<len;j++) {
382 c = inbuf[j];
383 if(c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */
384 c=='*' || c=='\'' || c=='(' || c==')' || /* do not touch escape character */
385 c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */
386 c=='&' || c=='=' || c=='+' || c=='$' || c==',') /* see RFC 2396 */
387 break;
389 // we are on a reserved char, write it out
390 if(j == i) {
391 *outbuf++ = c;
392 i++;
393 continue;
395 // we found one, take that part of the string
396 if(j < len) {
397 if(!tmp) tmp = malloc(len+1);
398 strncpy(tmp,inbuf+i,j-i);
399 tmp[j-i] = '\0';
400 in = tmp;
401 } else // take the rest of the string
402 in = (char*)inbuf+i;
404 if(!unesc) unesc = malloc(len+1);
405 // unescape first to avoid escaping escape
406 url_unescape_string(unesc,in);
407 // then escape, including mark and other reserved chars
408 // that can come from escape sequences
409 url_escape_string_part(outbuf,unesc);
410 outbuf += strlen(outbuf);
411 i += strlen(in);
413 *outbuf = '\0';
414 free(tmp);
415 free(unesc);
418 #ifdef URL_DEBUG
419 void
420 url_debug(const URL_t *url) {
421 if( url==NULL ) {
422 printf("URL pointer NULL\n");
423 return;
425 if( url->url!=NULL ) {
426 printf("url=%s\n", url->url );
428 if( url->protocol!=NULL ) {
429 printf("protocol=%s\n", url->protocol );
431 if( url->hostname!=NULL ) {
432 printf("hostname=%s\n", url->hostname );
434 printf("port=%d\n", url->port );
435 if( url->file!=NULL ) {
436 printf("file=%s\n", url->file );
438 if( url->username!=NULL ) {
439 printf("username=%s\n", url->username );
441 if( url->password!=NULL ) {
442 printf("password=%s\n", url->password );
445 #endif /* URL_DEBUG */