core: fix audio-only + framestep weird behavior
[mplayer/glamo.git] / stream / url.c
blobc898d092b5ebcff6378ab7bc2dbcbf6b2714f1c8
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 = malloc(sizeof(URL_t));
105 if( Curl==NULL ) {
106 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
107 goto err_out;
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");
119 goto err_out;
121 mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename);
123 // extract the protocol
124 ptr1 = strstr(escfilename, "://");
125 if( ptr1==NULL ) {
126 // Check for a special case: "sip:" (without "//"):
127 if (strstr(escfilename, "sip:") == escfilename) {
128 ptr1 = (char *)&url[3]; // points to ':'
129 jumpSize = 1;
130 } else {
131 mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n");
132 goto err_out;
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");
139 goto err_out;
141 strncpy(Curl->protocol, escfilename, pos1);
142 Curl->protocol[pos1] = '\0';
144 // jump the "://"
145 ptr1 += jumpSize;
146 pos1 += jumpSize;
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
153 ptr2 = NULL;
155 if( ptr2!=NULL ) {
156 // We got something, at least a username...
157 int len = ptr2-ptr1;
158 Curl->username = malloc(len+1);
159 if( Curl->username==NULL ) {
160 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
161 goto err_out;
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");
174 goto err_out;
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);
181 ptr1 = ptr2+1;
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
192 ptr1++;
193 pos1++;
194 ptr2 = ptr3;
195 v6addr = 1;
196 } else {
197 ptr2 = ptr1;
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;
206 if( ptr2==NULL ) {
207 // No port is given
208 // Look if a path is given
209 if( ptr3==NULL ) {
210 // No path/filename
211 // So we have an URL like http://www.hostname.com
212 pos2 = strlen(escfilename);
213 } else {
214 // We have an URL like http://www.hostname.com/file.txt
215 pos2 = ptr3-escfilename;
217 } else {
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;
223 if( v6addr ) pos2--;
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");
228 goto err_out;
230 strncpy(Curl->hostname, ptr1, pos2-pos1);
231 Curl->hostname[pos2-pos1] = '\0';
233 // Look if a path is given
234 ptr2 = strstr(ptr1, "/");
235 if( ptr2!=NULL ) {
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");
243 goto err_out;
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");
252 goto err_out;
254 strcpy(Curl->file, "/");
257 noauth_len = make_noauth_url(Curl, NULL, 0);
258 if (noauth_len > 0) {
259 noauth_len++;
260 Curl->noauth_url = malloc(noauth_len);
261 if (!Curl->noauth_url) {
262 mp_msg(MSGT_NETWORK, MSGL_FATAL, "Memory allocation failed.\n");
263 goto err_out;
265 make_noauth_url(Curl, Curl->noauth_url, noauth_len);
268 free(escfilename);
269 return Curl;
270 err_out:
271 free(escfilename);
272 if (Curl) url_free(Curl);
273 return NULL;
276 void
277 url_free(URL_t* url) {
278 if(!url) return;
279 free(url->url);
280 free(url->protocol);
281 free(url->hostname);
282 free(url->file);
283 free(url->username);
284 free(url->password);
285 free(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 */
292 void
293 url_unescape_string(char *outbuf, const char *inbuf)
295 unsigned char c,c1,c2;
296 int i,len=strlen(inbuf);
297 for (i=0;i<len;i++){
298 c = inbuf[i];
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';
305 else c1-='A'-10;
306 if (c2>='0' && c2<='9') c2-='0';
307 else c2-='A'-10;
308 c = (c1<<4) + c2;
309 i=i+2; //only skip next 2 chars if valid esc
312 *outbuf++ = c;
314 *outbuf++='\0'; //add nullterm to string
317 static void
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++) {
323 c = inbuf[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
326 } else {
327 c1=129; c2=129; //not escape chars
330 if( (c >= 'A' && c <= 'Z') ||
331 (c >= 'a' && c <= 'z') ||
332 (c >= '0' && c <= '9') ||
333 (c >= 0x7f)) {
334 *outbuf++ = c;
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
340 // dont escape again
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
344 } else {
345 /* all others will be escaped */
346 c1 = ((c & 0xf0) >> 4);
347 c2 = (c & 0x0f);
348 if (c1 < 10) c1+='0';
349 else c1+='A'-10;
350 if (c2 < 10) c2+='0';
351 else c2+='A'-10;
352 *outbuf++ = '%';
353 *outbuf++ = c1;
354 *outbuf++ = c2;
357 *outbuf++='\0';
360 /* Replace specific characters in the URL string by an escape sequence */
361 /* works like strcpy(), but without return argument */
362 void
363 url_escape_string(char *outbuf, const char *inbuf) {
364 unsigned char c;
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,"://[");
371 if(tmp) {
372 tmp = strchr(tmp+4,']');
373 if(tmp && (tmp[1] == '/' || tmp[1] == ':' ||
374 tmp[1] == '\0')) {
375 i = tmp+1-inbuf;
376 strncpy(outbuf,inbuf,i);
377 outbuf += i;
378 tmp = NULL;
382 tmp = NULL;
383 while(i < len) {
384 // look for the next char that must be kept
385 for (j=i;j<len;j++) {
386 c = inbuf[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 */
391 break;
393 // we are on a reserved char, write it out
394 if(j == i) {
395 *outbuf++ = c;
396 i++;
397 continue;
399 // we found one, take that part of the string
400 if(j < len) {
401 if(!tmp) tmp = malloc(len+1);
402 strncpy(tmp,inbuf+i,j-i);
403 tmp[j-i] = '\0';
404 in = tmp;
405 } else // take the rest of the string
406 in = (char*)inbuf+i;
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);
415 i += strlen(in);
417 *outbuf = '\0';
418 free(tmp);
419 free(unesc);
422 #ifdef URL_DEBUG
423 void
424 url_debug(const URL_t *url) {
425 if( url==NULL ) {
426 printf("URL pointer NULL\n");
427 return;
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 */