sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / stream / url.c
blob62e3bdcf2a9acabf75d61111d8d0ec5334ad0705
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"
31 #include "help_mp.h"
33 #ifndef SIZE_MAX
34 #define SIZE_MAX ((size_t)-1)
35 #endif
37 URL_t *url_redirect(URL_t **url, const char *redir) {
38 URL_t *u = *url;
39 URL_t *res;
40 if (!strchr(redir, '/') || *redir == '/') {
41 char *tmp;
42 char *newurl = malloc(strlen(u->url) + strlen(redir) + 1);
43 strcpy(newurl, u->url);
44 if (*redir == '/') {
45 redir++;
46 tmp = strstr(newurl, "://");
47 if (tmp) tmp = strchr(tmp + 3, '/');
48 } else
49 tmp = strrchr(newurl, '/');
50 if (tmp) tmp[1] = 0;
51 strcat(newurl, redir);
52 res = url_new(newurl);
53 free(newurl);
54 } else
55 res = url_new(redir);
56 url_free(u);
57 *url = res;
58 return res;
61 URL_t*
62 url_new(const char* url) {
63 int pos1, pos2,v6addr = 0;
64 URL_t* Curl = NULL;
65 char *escfilename=NULL;
66 char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL;
67 int jumpSize = 3;
69 if( url==NULL ) return NULL;
71 if (strlen(url) > (SIZE_MAX / 3 - 1)) {
72 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
73 goto err_out;
75 escfilename=malloc(strlen(url)*3+1);
76 if (!escfilename ) {
77 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
78 goto err_out;
81 // Create the URL container
82 Curl = malloc(sizeof(URL_t));
83 if( Curl==NULL ) {
84 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
85 goto err_out;
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);
97 goto err_out;
99 mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename);
101 // extract the protocol
102 ptr1 = strstr(escfilename, "://");
103 if( ptr1==NULL ) {
104 // Check for a special case: "sip:" (without "//"):
105 if (strstr(escfilename, "sip:") == escfilename) {
106 ptr1 = (char *)&url[3]; // points to ':'
107 jumpSize = 1;
108 } else {
109 mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n");
110 goto err_out;
113 pos1 = ptr1-escfilename;
114 Curl->protocol = malloc(pos1+1);
115 if( Curl->protocol==NULL ) {
116 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
117 goto err_out;
119 strncpy(Curl->protocol, escfilename, pos1);
120 Curl->protocol[pos1] = '\0';
122 // jump the "://"
123 ptr1 += jumpSize;
124 pos1 += jumpSize;
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
131 ptr2 = NULL;
133 if( ptr2!=NULL ) {
134 // We got something, at least a username...
135 int len = ptr2-ptr1;
136 Curl->username = malloc(len+1);
137 if( Curl->username==NULL ) {
138 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
139 goto err_out;
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);
152 goto err_out;
154 strncpy( Curl->password, ptr3+1, len2);
155 Curl->password[len2]='\0';
157 ptr1 = ptr2+1;
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
168 ptr1++;
169 pos1++;
170 ptr2 = ptr3;
171 v6addr = 1;
172 } else {
173 ptr2 = ptr1;
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;
182 if( ptr2==NULL ) {
183 // No port is given
184 // Look if a path is given
185 if( ptr3==NULL ) {
186 // No path/filename
187 // So we have an URL like http://www.hostname.com
188 pos2 = strlen(escfilename);
189 } else {
190 // We have an URL like http://www.hostname.com/file.txt
191 pos2 = ptr3-escfilename;
193 } else {
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;
199 if( v6addr ) pos2--;
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);
204 goto err_out;
206 strncpy(Curl->hostname, ptr1, pos2-pos1);
207 Curl->hostname[pos2-pos1] = '\0';
209 // Look if a path is given
210 ptr2 = strstr(ptr1, "/");
211 if( ptr2!=NULL ) {
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);
219 goto err_out;
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);
228 goto err_out;
230 strcpy(Curl->file, "/");
233 free(escfilename);
234 return Curl;
235 err_out:
236 if (escfilename) free(escfilename);
237 if (Curl) url_free(Curl);
238 return NULL;
241 void
242 url_free(URL_t* url) {
243 if(!url) return;
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);
250 free(url);
254 /* Replace escape sequences in an URL (or a part of an URL) */
255 /* works like strcpy(), but without return argument */
256 void
257 url_unescape_string(char *outbuf, const char *inbuf)
259 unsigned char c,c1,c2;
260 int i,len=strlen(inbuf);
261 for (i=0;i<len;i++){
262 c = inbuf[i];
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';
269 else c1-='A'-10;
270 if (c2>='0' && c2<='9') c2-='0';
271 else c2-='A'-10;
272 c = (c1<<4) + c2;
273 i=i+2; //only skip next 2 chars if valid esc
276 *outbuf++ = c;
278 *outbuf++='\0'; //add nullterm to string
281 static void
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++) {
287 c = inbuf[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
290 } else {
291 c1=129; c2=129; //not escape chars
294 if( (c >= 'A' && c <= 'Z') ||
295 (c >= 'a' && c <= 'z') ||
296 (c >= '0' && c <= '9') ||
297 (c >= 0x7f)) {
298 *outbuf++ = c;
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
304 // dont escape again
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
308 } else {
309 /* all others will be escaped */
310 c1 = ((c & 0xf0) >> 4);
311 c2 = (c & 0x0f);
312 if (c1 < 10) c1+='0';
313 else c1+='A'-10;
314 if (c2 < 10) c2+='0';
315 else c2+='A'-10;
316 *outbuf++ = '%';
317 *outbuf++ = c1;
318 *outbuf++ = c2;
321 *outbuf++='\0';
324 /* Replace specific characters in the URL string by an escape sequence */
325 /* works like strcpy(), but without return argument */
326 void
327 url_escape_string(char *outbuf, const char *inbuf) {
328 unsigned char c;
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,"://[");
335 if(tmp) {
336 tmp = strchr(tmp+4,']');
337 if(tmp && (tmp[1] == '/' || tmp[1] == ':' ||
338 tmp[1] == '\0')) {
339 i = tmp+1-inbuf;
340 strncpy(outbuf,inbuf,i);
341 outbuf += i;
342 tmp = NULL;
346 tmp = NULL;
347 while(i < len) {
348 // look for the next char that must be kept
349 for (j=i;j<len;j++) {
350 c = inbuf[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 */
355 break;
357 // we are on a reserved char, write it out
358 if(j == i) {
359 *outbuf++ = c;
360 i++;
361 continue;
363 // we found one, take that part of the string
364 if(j < len) {
365 if(!tmp) tmp = malloc(len+1);
366 strncpy(tmp,inbuf+i,j-i);
367 tmp[j-i] = '\0';
368 in = tmp;
369 } else // take the rest of the string
370 in = (char*)inbuf+i;
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);
379 i += strlen(in);
381 *outbuf = '\0';
382 if(tmp) free(tmp);
383 if(unesc) free(unesc);
386 #ifdef URL_DEBUG
387 void
388 url_debug(const URL_t *url) {
389 if( url==NULL ) {
390 printf("URL pointer NULL\n");
391 return;
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 */