codecs.conf: add ffmpeg g722 audio codec
[mplayer/glamo.git] / stream / url.c
blob92ea0588316cba7e4a041da5818ad20982e5c799
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 URL_t*
71 url_new(const char* url) {
72 int pos1, pos2,v6addr = 0, noauth_len;
73 URL_t* Curl = NULL;
74 char *escfilename=NULL;
75 char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL;
76 int jumpSize = 3;
78 if( url==NULL ) return NULL;
80 if (strlen(url) > (SIZE_MAX / 3 - 1)) {
81 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
82 goto err_out;
84 escfilename=malloc(strlen(url)*3+1);
85 if (!escfilename ) {
86 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
87 goto err_out;
90 // Create the URL container
91 Curl = malloc(sizeof(URL_t));
92 if( Curl==NULL ) {
93 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
94 goto err_out;
97 // Initialisation of the URL container members
98 memset( Curl, 0, sizeof(URL_t) );
100 url_escape_string(escfilename,url);
102 // Copy the url in the URL container
103 Curl->url = strdup(escfilename);
104 if( Curl->url==NULL ) {
105 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
106 goto err_out;
108 mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename);
110 // extract the protocol
111 ptr1 = strstr(escfilename, "://");
112 if( ptr1==NULL ) {
113 // Check for a special case: "sip:" (without "//"):
114 if (strstr(escfilename, "sip:") == escfilename) {
115 ptr1 = (char *)&url[3]; // points to ':'
116 jumpSize = 1;
117 } else {
118 mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n");
119 goto err_out;
122 pos1 = ptr1-escfilename;
123 Curl->protocol = malloc(pos1+1);
124 if( Curl->protocol==NULL ) {
125 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
126 goto err_out;
128 strncpy(Curl->protocol, escfilename, pos1);
129 Curl->protocol[pos1] = '\0';
131 // jump the "://"
132 ptr1 += jumpSize;
133 pos1 += jumpSize;
135 // check if a username:password is given
136 ptr2 = strstr(ptr1, "@");
137 ptr3 = strstr(ptr1, "/");
138 if( ptr3!=NULL && ptr3<ptr2 ) {
139 // it isn't really a username but rather a part of the path
140 ptr2 = NULL;
142 if( ptr2!=NULL ) {
143 // We got something, at least a username...
144 int len = ptr2-ptr1;
145 Curl->username = malloc(len+1);
146 if( Curl->username==NULL ) {
147 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
148 goto err_out;
150 strncpy(Curl->username, ptr1, len);
151 Curl->username[len] = '\0';
153 ptr3 = strstr(ptr1, ":");
154 if( ptr3!=NULL && ptr3<ptr2 ) {
155 // We also have a password
156 int len2 = ptr2-ptr3-1;
157 Curl->username[ptr3-ptr1]='\0';
158 Curl->password = malloc(len2+1);
159 if( Curl->password==NULL ) {
160 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
161 goto err_out;
163 strncpy( Curl->password, ptr3+1, len2);
164 Curl->password[len2]='\0';
165 url_unescape_string(Curl->password, Curl->password);
167 url_unescape_string(Curl->username, Curl->username);
168 ptr1 = ptr2+1;
169 pos1 = ptr1-escfilename;
172 // before looking for a port number check if we have an IPv6 type numeric address
173 // in IPv6 URL the numeric address should be inside square braces.
174 ptr2 = strstr(ptr1, "[");
175 ptr3 = strstr(ptr1, "]");
176 ptr4 = strstr(ptr1, "/");
177 if( ptr2!=NULL && ptr3!=NULL && ptr2 < ptr3 && (!ptr4 || ptr4 > ptr3)) {
178 // we have an IPv6 numeric address
179 ptr1++;
180 pos1++;
181 ptr2 = ptr3;
182 v6addr = 1;
183 } else {
184 ptr2 = ptr1;
188 // look if the port is given
189 ptr2 = strstr(ptr2, ":");
190 // If the : is after the first / it isn't the port
191 ptr3 = strstr(ptr1, "/");
192 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL;
193 if( ptr2==NULL ) {
194 // No port is given
195 // Look if a path is given
196 if( ptr3==NULL ) {
197 // No path/filename
198 // So we have an URL like http://www.hostname.com
199 pos2 = strlen(escfilename);
200 } else {
201 // We have an URL like http://www.hostname.com/file.txt
202 pos2 = ptr3-escfilename;
204 } else {
205 // We have an URL beginning like http://www.hostname.com:1212
206 // Get the port number
207 Curl->port = atoi(ptr2+1);
208 pos2 = ptr2-escfilename;
210 if( v6addr ) pos2--;
211 // copy the hostname in the URL container
212 Curl->hostname = malloc(pos2-pos1+1);
213 if( Curl->hostname==NULL ) {
214 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
215 goto err_out;
217 strncpy(Curl->hostname, ptr1, pos2-pos1);
218 Curl->hostname[pos2-pos1] = '\0';
220 // Look if a path is given
221 ptr2 = strstr(ptr1, "/");
222 if( ptr2!=NULL ) {
223 // A path/filename is given
224 // check if it's not a trailing '/'
225 if( strlen(ptr2)>1 ) {
226 // copy the path/filename in the URL container
227 Curl->file = strdup(ptr2);
228 if( Curl->file==NULL ) {
229 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
230 goto err_out;
234 // Check if a filename was given or set, else set it with '/'
235 if( Curl->file==NULL ) {
236 Curl->file = malloc(2);
237 if( Curl->file==NULL ) {
238 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
239 goto err_out;
241 strcpy(Curl->file, "/");
244 noauth_len = make_noauth_url(Curl, NULL, 0);
245 if (noauth_len > 0) {
246 noauth_len++;
247 Curl->noauth_url = malloc(noauth_len);
248 if (!Curl->noauth_url) {
249 mp_msg(MSGT_NETWORK, MSGL_FATAL, "Memory allocation failed.\n");
250 goto err_out;
252 make_noauth_url(Curl, Curl->noauth_url, noauth_len);
255 free(escfilename);
256 return Curl;
257 err_out:
258 free(escfilename);
259 if (Curl) url_free(Curl);
260 return NULL;
263 void
264 url_free(URL_t* url) {
265 if(!url) return;
266 free(url->url);
267 free(url->protocol);
268 free(url->hostname);
269 free(url->file);
270 free(url->username);
271 free(url->password);
272 free(url);
276 /* Replace escape sequences in an URL (or a part of an URL) */
277 /* works like strcpy(), but without return argument,
278 except that outbuf == inbuf is allowed */
279 void
280 url_unescape_string(char *outbuf, const char *inbuf)
282 unsigned char c,c1,c2;
283 int i,len=strlen(inbuf);
284 for (i=0;i<len;i++){
285 c = inbuf[i];
286 if (c == '%' && i<len-2) { //must have 2 more chars
287 c1 = toupper(inbuf[i+1]); // we need uppercase characters
288 c2 = toupper(inbuf[i+2]);
289 if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) &&
290 ((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) {
291 if (c1>='0' && c1<='9') c1-='0';
292 else c1-='A'-10;
293 if (c2>='0' && c2<='9') c2-='0';
294 else c2-='A'-10;
295 c = (c1<<4) + c2;
296 i=i+2; //only skip next 2 chars if valid esc
299 *outbuf++ = c;
301 *outbuf++='\0'; //add nullterm to string
304 static void
305 url_escape_string_part(char *outbuf, const char *inbuf) {
306 unsigned char c,c1,c2;
307 int i,len=strlen(inbuf);
309 for (i=0;i<len;i++) {
310 c = inbuf[i];
311 if ((c=='%') && i<len-2 ) { //need 2 more characters
312 c1=toupper(inbuf[i+1]); c2=toupper(inbuf[i+2]); // need uppercase chars
313 } else {
314 c1=129; c2=129; //not escape chars
317 if( (c >= 'A' && c <= 'Z') ||
318 (c >= 'a' && c <= 'z') ||
319 (c >= '0' && c <= '9') ||
320 (c >= 0x7f)) {
321 *outbuf++ = c;
322 } else if ( c=='%' && ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) &&
323 ((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) {
324 // check if part of an escape sequence
325 *outbuf++=c; // already
327 // dont escape again
328 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"String appears to be already escaped in url_escape %c%c1%c2\n",c,c1,c2);
329 // error as this should not happen against RFC 2396
330 // to escape a string twice
331 } else {
332 /* all others will be escaped */
333 c1 = ((c & 0xf0) >> 4);
334 c2 = (c & 0x0f);
335 if (c1 < 10) c1+='0';
336 else c1+='A'-10;
337 if (c2 < 10) c2+='0';
338 else c2+='A'-10;
339 *outbuf++ = '%';
340 *outbuf++ = c1;
341 *outbuf++ = c2;
344 *outbuf++='\0';
347 /* Replace specific characters in the URL string by an escape sequence */
348 /* works like strcpy(), but without return argument */
349 void
350 url_escape_string(char *outbuf, const char *inbuf) {
351 unsigned char c;
352 int i = 0,j,len = strlen(inbuf);
353 char* tmp,*unesc = NULL, *in;
355 // Look if we have an ip6 address, if so skip it there is
356 // no need to escape anything in there.
357 tmp = strstr(inbuf,"://[");
358 if(tmp) {
359 tmp = strchr(tmp+4,']');
360 if(tmp && (tmp[1] == '/' || tmp[1] == ':' ||
361 tmp[1] == '\0')) {
362 i = tmp+1-inbuf;
363 strncpy(outbuf,inbuf,i);
364 outbuf += i;
365 tmp = NULL;
369 tmp = NULL;
370 while(i < len) {
371 // look for the next char that must be kept
372 for (j=i;j<len;j++) {
373 c = inbuf[j];
374 if(c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */
375 c=='*' || c=='\'' || c=='(' || c==')' || /* do not touch escape character */
376 c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */
377 c=='&' || c=='=' || c=='+' || c=='$' || c==',') /* see RFC 2396 */
378 break;
380 // we are on a reserved char, write it out
381 if(j == i) {
382 *outbuf++ = c;
383 i++;
384 continue;
386 // we found one, take that part of the string
387 if(j < len) {
388 if(!tmp) tmp = malloc(len+1);
389 strncpy(tmp,inbuf+i,j-i);
390 tmp[j-i] = '\0';
391 in = tmp;
392 } else // take the rest of the string
393 in = (char*)inbuf+i;
395 if(!unesc) unesc = malloc(len+1);
396 // unescape first to avoid escaping escape
397 url_unescape_string(unesc,in);
398 // then escape, including mark and other reserved chars
399 // that can come from escape sequences
400 url_escape_string_part(outbuf,unesc);
401 outbuf += strlen(outbuf);
402 i += strlen(in);
404 *outbuf = '\0';
405 free(tmp);
406 free(unesc);
409 #ifdef URL_DEBUG
410 void
411 url_debug(const URL_t *url) {
412 if( url==NULL ) {
413 printf("URL pointer NULL\n");
414 return;
416 if( url->url!=NULL ) {
417 printf("url=%s\n", url->url );
419 if( url->protocol!=NULL ) {
420 printf("protocol=%s\n", url->protocol );
422 if( url->hostname!=NULL ) {
423 printf("hostname=%s\n", url->hostname );
425 printf("port=%d\n", url->port );
426 if( url->file!=NULL ) {
427 printf("file=%s\n", url->file );
429 if( url->username!=NULL ) {
430 printf("username=%s\n", url->username );
432 if( url->password!=NULL ) {
433 printf("password=%s\n", url->password );
436 #endif /* URL_DEBUG */