Give better name to Inverse_Table_6_9
[mplayer/glamo.git] / stream / url.c
blob89d4eaaa7b0c888913e1685a7836ff0b51faaa52
1 /*
2 * URL Helper
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team.
6 */
8 #include <string.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <inttypes.h>
14 #include "url.h"
15 #include "mp_msg.h"
16 #include "help_mp.h"
18 #ifndef SIZE_MAX
19 #define SIZE_MAX ((size_t)-1)
20 #endif
22 URL_t *url_redirect(URL_t **url, const char *redir) {
23 URL_t *u = *url;
24 URL_t *res;
25 if (!strchr(redir, '/') || *redir == '/') {
26 char *tmp;
27 char *newurl = malloc(strlen(u->url) + strlen(redir) + 1);
28 strcpy(newurl, u->url);
29 if (*redir == '/') {
30 redir++;
31 tmp = strstr(newurl, "://");
32 if (tmp) tmp = strchr(tmp + 3, '/');
33 } else
34 tmp = strrchr(newurl, '/');
35 if (tmp) tmp[1] = 0;
36 strcat(newurl, redir);
37 res = url_new(newurl);
38 free(newurl);
39 } else
40 res = url_new(redir);
41 url_free(u);
42 *url = res;
43 return res;
46 URL_t*
47 url_new(const char* url) {
48 int pos1, pos2,v6addr = 0;
49 URL_t* Curl = NULL;
50 char *escfilename=NULL;
51 char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL;
52 int jumpSize = 3;
54 if( url==NULL ) return NULL;
56 if (strlen(url) > (SIZE_MAX / 3 - 1)) {
57 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
58 goto err_out;
60 escfilename=malloc(strlen(url)*3+1);
61 if (!escfilename ) {
62 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
63 goto err_out;
66 // Create the URL container
67 Curl = malloc(sizeof(URL_t));
68 if( Curl==NULL ) {
69 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
70 goto err_out;
73 // Initialisation of the URL container members
74 memset( Curl, 0, sizeof(URL_t) );
76 url_escape_string(escfilename,url);
78 // Copy the url in the URL container
79 Curl->url = strdup(escfilename);
80 if( Curl->url==NULL ) {
81 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
82 goto err_out;
84 mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename);
86 // extract the protocol
87 ptr1 = strstr(escfilename, "://");
88 if( ptr1==NULL ) {
89 // Check for a special case: "sip:" (without "//"):
90 if (strstr(escfilename, "sip:") == escfilename) {
91 ptr1 = (char *)&url[3]; // points to ':'
92 jumpSize = 1;
93 } else {
94 mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n");
95 goto err_out;
98 pos1 = ptr1-escfilename;
99 Curl->protocol = malloc(pos1+1);
100 if( Curl->protocol==NULL ) {
101 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
102 goto err_out;
104 strncpy(Curl->protocol, escfilename, pos1);
105 Curl->protocol[pos1] = '\0';
107 // jump the "://"
108 ptr1 += jumpSize;
109 pos1 += jumpSize;
111 // check if a username:password is given
112 ptr2 = strstr(ptr1, "@");
113 ptr3 = strstr(ptr1, "/");
114 if( ptr3!=NULL && ptr3<ptr2 ) {
115 // it isn't really a username but rather a part of the path
116 ptr2 = NULL;
118 if( ptr2!=NULL ) {
119 // We got something, at least a username...
120 int len = ptr2-ptr1;
121 Curl->username = malloc(len+1);
122 if( Curl->username==NULL ) {
123 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
124 goto err_out;
126 strncpy(Curl->username, ptr1, len);
127 Curl->username[len] = '\0';
129 ptr3 = strstr(ptr1, ":");
130 if( ptr3!=NULL && ptr3<ptr2 ) {
131 // We also have a password
132 int len2 = ptr2-ptr3-1;
133 Curl->username[ptr3-ptr1]='\0';
134 Curl->password = malloc(len2+1);
135 if( Curl->password==NULL ) {
136 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
137 goto err_out;
139 strncpy( Curl->password, ptr3+1, len2);
140 Curl->password[len2]='\0';
142 ptr1 = ptr2+1;
143 pos1 = ptr1-escfilename;
146 // before looking for a port number check if we have an IPv6 type numeric address
147 // in IPv6 URL the numeric address should be inside square braces.
148 ptr2 = strstr(ptr1, "[");
149 ptr3 = strstr(ptr1, "]");
150 ptr4 = strstr(ptr1, "/");
151 if( ptr2!=NULL && ptr3!=NULL && ptr2 < ptr3 && (!ptr4 || ptr4 > ptr3)) {
152 // we have an IPv6 numeric address
153 ptr1++;
154 pos1++;
155 ptr2 = ptr3;
156 v6addr = 1;
157 } else {
158 ptr2 = ptr1;
162 // look if the port is given
163 ptr2 = strstr(ptr2, ":");
164 // If the : is after the first / it isn't the port
165 ptr3 = strstr(ptr1, "/");
166 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL;
167 if( ptr2==NULL ) {
168 // No port is given
169 // Look if a path is given
170 if( ptr3==NULL ) {
171 // No path/filename
172 // So we have an URL like http://www.hostname.com
173 pos2 = strlen(escfilename);
174 } else {
175 // We have an URL like http://www.hostname.com/file.txt
176 pos2 = ptr3-escfilename;
178 } else {
179 // We have an URL beginning like http://www.hostname.com:1212
180 // Get the port number
181 Curl->port = atoi(ptr2+1);
182 pos2 = ptr2-escfilename;
184 if( v6addr ) pos2--;
185 // copy the hostname in the URL container
186 Curl->hostname = malloc(pos2-pos1+1);
187 if( Curl->hostname==NULL ) {
188 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
189 goto err_out;
191 strncpy(Curl->hostname, ptr1, pos2-pos1);
192 Curl->hostname[pos2-pos1] = '\0';
194 // Look if a path is given
195 ptr2 = strstr(ptr1, "/");
196 if( ptr2!=NULL ) {
197 // A path/filename is given
198 // check if it's not a trailing '/'
199 if( strlen(ptr2)>1 ) {
200 // copy the path/filename in the URL container
201 Curl->file = strdup(ptr2);
202 if( Curl->file==NULL ) {
203 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
204 goto err_out;
208 // Check if a filename was given or set, else set it with '/'
209 if( Curl->file==NULL ) {
210 Curl->file = malloc(2);
211 if( Curl->file==NULL ) {
212 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
213 goto err_out;
215 strcpy(Curl->file, "/");
218 free(escfilename);
219 return Curl;
220 err_out:
221 if (escfilename) free(escfilename);
222 if (Curl) url_free(Curl);
223 return NULL;
226 void
227 url_free(URL_t* url) {
228 if(!url) return;
229 if(url->url) free(url->url);
230 if(url->protocol) free(url->protocol);
231 if(url->hostname) free(url->hostname);
232 if(url->file) free(url->file);
233 if(url->username) free(url->username);
234 if(url->password) free(url->password);
235 free(url);
239 /* Replace escape sequences in an URL (or a part of an URL) */
240 /* works like strcpy(), but without return argument */
241 void
242 url_unescape_string(char *outbuf, const char *inbuf)
244 unsigned char c,c1,c2;
245 int i,len=strlen(inbuf);
246 for (i=0;i<len;i++){
247 c = inbuf[i];
248 if (c == '%' && i<len-2) { //must have 2 more chars
249 c1 = toupper(inbuf[i+1]); // we need uppercase characters
250 c2 = toupper(inbuf[i+2]);
251 if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) &&
252 ((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) {
253 if (c1>='0' && c1<='9') c1-='0';
254 else c1-='A'-10;
255 if (c2>='0' && c2<='9') c2-='0';
256 else c2-='A'-10;
257 c = (c1<<4) + c2;
258 i=i+2; //only skip next 2 chars if valid esc
261 *outbuf++ = c;
263 *outbuf++='\0'; //add nullterm to string
266 static void
267 url_escape_string_part(char *outbuf, const char *inbuf) {
268 unsigned char c,c1,c2;
269 int i,len=strlen(inbuf);
271 for (i=0;i<len;i++) {
272 c = inbuf[i];
273 if ((c=='%') && i<len-2 ) { //need 2 more characters
274 c1=toupper(inbuf[i+1]); c2=toupper(inbuf[i+2]); // need uppercase chars
275 } else {
276 c1=129; c2=129; //not escape chars
279 if( (c >= 'A' && c <= 'Z') ||
280 (c >= 'a' && c <= 'z') ||
281 (c >= '0' && c <= '9') ||
282 (c >= 0x7f)) {
283 *outbuf++ = c;
284 } else if ( c=='%' && ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) &&
285 ((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) {
286 // check if part of an escape sequence
287 *outbuf++=c; // already
289 // dont escape again
290 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_URL_StringAlreadyEscaped,c,c1,c2);
291 // error as this should not happen against RFC 2396
292 // to escape a string twice
293 } else {
294 /* all others will be escaped */
295 c1 = ((c & 0xf0) >> 4);
296 c2 = (c & 0x0f);
297 if (c1 < 10) c1+='0';
298 else c1+='A'-10;
299 if (c2 < 10) c2+='0';
300 else c2+='A'-10;
301 *outbuf++ = '%';
302 *outbuf++ = c1;
303 *outbuf++ = c2;
306 *outbuf++='\0';
309 /* Replace specific characters in the URL string by an escape sequence */
310 /* works like strcpy(), but without return argument */
311 void
312 url_escape_string(char *outbuf, const char *inbuf) {
313 unsigned char c;
314 int i = 0,j,len = strlen(inbuf);
315 char* tmp,*unesc = NULL, *in;
317 // Look if we have an ip6 address, if so skip it there is
318 // no need to escape anything in there.
319 tmp = strstr(inbuf,"://[");
320 if(tmp) {
321 tmp = strchr(tmp+4,']');
322 if(tmp && (tmp[1] == '/' || tmp[1] == ':' ||
323 tmp[1] == '\0')) {
324 i = tmp+1-inbuf;
325 strncpy(outbuf,inbuf,i);
326 outbuf += i;
327 tmp = NULL;
331 tmp = NULL;
332 while(i < len) {
333 // look for the next char that must be kept
334 for (j=i;j<len;j++) {
335 c = inbuf[j];
336 if(c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */
337 c=='*' || c=='\'' || c=='(' || c==')' || /* do not touch escape character */
338 c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */
339 c=='&' || c=='=' || c=='+' || c=='$' || c==',') /* see RFC 2396 */
340 break;
342 // we are on a reserved char, write it out
343 if(j == i) {
344 *outbuf++ = c;
345 i++;
346 continue;
348 // we found one, take that part of the string
349 if(j < len) {
350 if(!tmp) tmp = malloc(len+1);
351 strncpy(tmp,inbuf+i,j-i);
352 tmp[j-i] = '\0';
353 in = tmp;
354 } else // take the rest of the string
355 in = (char*)inbuf+i;
357 if(!unesc) unesc = malloc(len+1);
358 // unescape first to avoid escaping escape
359 url_unescape_string(unesc,in);
360 // then escape, including mark and other reserved chars
361 // that can come from escape sequences
362 url_escape_string_part(outbuf,unesc);
363 outbuf += strlen(outbuf);
364 i += strlen(in);
366 *outbuf = '\0';
367 if(tmp) free(tmp);
368 if(unesc) free(unesc);
371 #ifdef URL_DEBUG
372 void
373 url_debug(const URL_t *url) {
374 if( url==NULL ) {
375 printf("URL pointer NULL\n");
376 return;
378 if( url->url!=NULL ) {
379 printf("url=%s\n", url->url );
381 if( url->protocol!=NULL ) {
382 printf("protocol=%s\n", url->protocol );
384 if( url->hostname!=NULL ) {
385 printf("hostname=%s\n", url->hostname );
387 printf("port=%d\n", url->port );
388 if( url->file!=NULL ) {
389 printf("file=%s\n", url->file );
391 if( url->username!=NULL ) {
392 printf("username=%s\n", url->username );
394 if( url->password!=NULL ) {
395 printf("password=%s\n", url->password );
398 #endif /* URL_DEBUG */