dvdcss: Fix typo in documentation
[mplayer/glamo.git] / stream / url.c
blob8d0176e45f59f8ed373b458226105b7a5dfa5f9a
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';
156 ptr1 = ptr2+1;
157 pos1 = ptr1-escfilename;
160 // before looking for a port number check if we have an IPv6 type numeric address
161 // in IPv6 URL the numeric address should be inside square braces.
162 ptr2 = strstr(ptr1, "[");
163 ptr3 = strstr(ptr1, "]");
164 ptr4 = strstr(ptr1, "/");
165 if( ptr2!=NULL && ptr3!=NULL && ptr2 < ptr3 && (!ptr4 || ptr4 > ptr3)) {
166 // we have an IPv6 numeric address
167 ptr1++;
168 pos1++;
169 ptr2 = ptr3;
170 v6addr = 1;
171 } else {
172 ptr2 = ptr1;
176 // look if the port is given
177 ptr2 = strstr(ptr2, ":");
178 // If the : is after the first / it isn't the port
179 ptr3 = strstr(ptr1, "/");
180 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL;
181 if( ptr2==NULL ) {
182 // No port is given
183 // Look if a path is given
184 if( ptr3==NULL ) {
185 // No path/filename
186 // So we have an URL like http://www.hostname.com
187 pos2 = strlen(escfilename);
188 } else {
189 // We have an URL like http://www.hostname.com/file.txt
190 pos2 = ptr3-escfilename;
192 } else {
193 // We have an URL beginning like http://www.hostname.com:1212
194 // Get the port number
195 Curl->port = atoi(ptr2+1);
196 pos2 = ptr2-escfilename;
198 if( v6addr ) pos2--;
199 // copy the hostname in the URL container
200 Curl->hostname = malloc(pos2-pos1+1);
201 if( Curl->hostname==NULL ) {
202 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
203 goto err_out;
205 strncpy(Curl->hostname, ptr1, pos2-pos1);
206 Curl->hostname[pos2-pos1] = '\0';
208 // Look if a path is given
209 ptr2 = strstr(ptr1, "/");
210 if( ptr2!=NULL ) {
211 // A path/filename is given
212 // check if it's not a trailing '/'
213 if( strlen(ptr2)>1 ) {
214 // copy the path/filename in the URL container
215 Curl->file = strdup(ptr2);
216 if( Curl->file==NULL ) {
217 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
218 goto err_out;
222 // Check if a filename was given or set, else set it with '/'
223 if( Curl->file==NULL ) {
224 Curl->file = malloc(2);
225 if( Curl->file==NULL ) {
226 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
227 goto err_out;
229 strcpy(Curl->file, "/");
232 free(escfilename);
233 return Curl;
234 err_out:
235 if (escfilename) free(escfilename);
236 if (Curl) url_free(Curl);
237 return NULL;
240 void
241 url_free(URL_t* url) {
242 if(!url) return;
243 if(url->url) free(url->url);
244 if(url->protocol) free(url->protocol);
245 if(url->hostname) free(url->hostname);
246 if(url->file) free(url->file);
247 if(url->username) free(url->username);
248 if(url->password) free(url->password);
249 free(url);
253 /* Replace escape sequences in an URL (or a part of an URL) */
254 /* works like strcpy(), but without return argument */
255 void
256 url_unescape_string(char *outbuf, const char *inbuf)
258 unsigned char c,c1,c2;
259 int i,len=strlen(inbuf);
260 for (i=0;i<len;i++){
261 c = inbuf[i];
262 if (c == '%' && i<len-2) { //must have 2 more chars
263 c1 = toupper(inbuf[i+1]); // we need uppercase characters
264 c2 = toupper(inbuf[i+2]);
265 if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) &&
266 ((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) {
267 if (c1>='0' && c1<='9') c1-='0';
268 else c1-='A'-10;
269 if (c2>='0' && c2<='9') c2-='0';
270 else c2-='A'-10;
271 c = (c1<<4) + c2;
272 i=i+2; //only skip next 2 chars if valid esc
275 *outbuf++ = c;
277 *outbuf++='\0'; //add nullterm to string
280 static void
281 url_escape_string_part(char *outbuf, const char *inbuf) {
282 unsigned char c,c1,c2;
283 int i,len=strlen(inbuf);
285 for (i=0;i<len;i++) {
286 c = inbuf[i];
287 if ((c=='%') && i<len-2 ) { //need 2 more characters
288 c1=toupper(inbuf[i+1]); c2=toupper(inbuf[i+2]); // need uppercase chars
289 } else {
290 c1=129; c2=129; //not escape chars
293 if( (c >= 'A' && c <= 'Z') ||
294 (c >= 'a' && c <= 'z') ||
295 (c >= '0' && c <= '9') ||
296 (c >= 0x7f)) {
297 *outbuf++ = c;
298 } else if ( c=='%' && ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) &&
299 ((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) {
300 // check if part of an escape sequence
301 *outbuf++=c; // already
303 // dont escape again
304 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"String appears to be already escaped in url_escape %c%c1%c2\n",c,c1,c2);
305 // error as this should not happen against RFC 2396
306 // to escape a string twice
307 } else {
308 /* all others will be escaped */
309 c1 = ((c & 0xf0) >> 4);
310 c2 = (c & 0x0f);
311 if (c1 < 10) c1+='0';
312 else c1+='A'-10;
313 if (c2 < 10) c2+='0';
314 else c2+='A'-10;
315 *outbuf++ = '%';
316 *outbuf++ = c1;
317 *outbuf++ = c2;
320 *outbuf++='\0';
323 /* Replace specific characters in the URL string by an escape sequence */
324 /* works like strcpy(), but without return argument */
325 void
326 url_escape_string(char *outbuf, const char *inbuf) {
327 unsigned char c;
328 int i = 0,j,len = strlen(inbuf);
329 char* tmp,*unesc = NULL, *in;
331 // Look if we have an ip6 address, if so skip it there is
332 // no need to escape anything in there.
333 tmp = strstr(inbuf,"://[");
334 if(tmp) {
335 tmp = strchr(tmp+4,']');
336 if(tmp && (tmp[1] == '/' || tmp[1] == ':' ||
337 tmp[1] == '\0')) {
338 i = tmp+1-inbuf;
339 strncpy(outbuf,inbuf,i);
340 outbuf += i;
341 tmp = NULL;
345 tmp = NULL;
346 while(i < len) {
347 // look for the next char that must be kept
348 for (j=i;j<len;j++) {
349 c = inbuf[j];
350 if(c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */
351 c=='*' || c=='\'' || c=='(' || c==')' || /* do not touch escape character */
352 c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */
353 c=='&' || c=='=' || c=='+' || c=='$' || c==',') /* see RFC 2396 */
354 break;
356 // we are on a reserved char, write it out
357 if(j == i) {
358 *outbuf++ = c;
359 i++;
360 continue;
362 // we found one, take that part of the string
363 if(j < len) {
364 if(!tmp) tmp = malloc(len+1);
365 strncpy(tmp,inbuf+i,j-i);
366 tmp[j-i] = '\0';
367 in = tmp;
368 } else // take the rest of the string
369 in = (char*)inbuf+i;
371 if(!unesc) unesc = malloc(len+1);
372 // unescape first to avoid escaping escape
373 url_unescape_string(unesc,in);
374 // then escape, including mark and other reserved chars
375 // that can come from escape sequences
376 url_escape_string_part(outbuf,unesc);
377 outbuf += strlen(outbuf);
378 i += strlen(in);
380 *outbuf = '\0';
381 if(tmp) free(tmp);
382 if(unesc) free(unesc);
385 #ifdef URL_DEBUG
386 void
387 url_debug(const URL_t *url) {
388 if( url==NULL ) {
389 printf("URL pointer NULL\n");
390 return;
392 if( url->url!=NULL ) {
393 printf("url=%s\n", url->url );
395 if( url->protocol!=NULL ) {
396 printf("protocol=%s\n", url->protocol );
398 if( url->hostname!=NULL ) {
399 printf("hostname=%s\n", url->hostname );
401 printf("port=%d\n", url->port );
402 if( url->file!=NULL ) {
403 printf("file=%s\n", url->file );
405 if( url->username!=NULL ) {
406 printf("username=%s\n", url->username );
408 if( url->password!=NULL ) {
409 printf("password=%s\n", url->password );
412 #endif /* URL_DEBUG */