Mercurial > mplayer.hg
annotate libmpdemux/url.c @ 3513:74cf5ea291bc
updated
author | arpi |
---|---|
date | Sun, 16 Dec 2001 03:21:47 +0000 |
parents | 5c8a533dfa09 |
children | a1522fa7728a |
rev | line source |
---|---|
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
1 /* |
902 | 2 * URL Helper |
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 * | |
6 * TODO: | |
7 * Extract the username/password if present | |
8 */ | |
9 | |
833 | 10 #include <string.h> |
11 #include <stdlib.h> | |
12 #include <stdio.h> | |
13 | |
14 #include "url.h" | |
15 | |
16 URL_t* | |
902 | 17 url_new(char* url) { |
833 | 18 int pos1, pos2; |
19 URL_t* Curl; | |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
20 char *ptr1, *ptr2, *ptr3; |
833 | 21 |
22 // Create the URL container | |
23 Curl = (URL_t*)malloc(sizeof(URL_t)); | |
24 if( Curl==NULL ) { | |
25 printf("Memory allocation failed!\n"); | |
1631 | 26 return NULL; |
833 | 27 } |
840 | 28 // Initialisation of the URL container members |
902 | 29 memset( Curl, 0, sizeof(URL_t) ); |
840 | 30 |
833 | 31 // Copy the url in the URL container |
32 Curl->url = (char*)malloc(strlen(url)+1); | |
33 if( Curl->url==NULL ) { | |
34 printf("Memory allocation failed!\n"); | |
1631 | 35 return NULL; |
833 | 36 } |
37 strcpy(Curl->url, url); | |
38 | |
39 // extract the protocol | |
40 ptr1 = strstr(url, "://"); | |
41 if( ptr1==NULL ) { | |
997 | 42 printf("Not an URL!\n"); |
833 | 43 return NULL; |
44 } | |
45 pos1 = ptr1-url; | |
46 Curl->protocol = (char*)malloc(pos1+1); | |
47 strncpy(Curl->protocol, url, pos1); | |
48 Curl->protocol[pos1] = '\0'; | |
49 | |
50 // look if the port is given | |
51 ptr2 = strstr(ptr1+3, ":"); | |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
52 // If the : is after the first / it isn't the port |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
53 ptr3 = strstr(ptr1+3, "/"); |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
54 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL; |
833 | 55 if( ptr2==NULL ) { |
56 // No port is given | |
57 // Look if a path is given | |
58 ptr2 = strstr(ptr1+3, "/"); | |
59 if( ptr2==NULL ) { | |
60 // No path/filename | |
61 // So we have an URL like http://www.hostname.com | |
62 pos2 = strlen(url); | |
63 } else { | |
64 // We have an URL like http://www.hostname.com/file.txt | |
65 pos2 = ptr2-url; | |
66 } | |
67 } else { | |
68 // We have an URL beginning like http://www.hostname.com:1212 | |
69 // Get the port number | |
70 Curl->port = atoi(ptr2+1); | |
71 pos2 = ptr2-url; | |
72 } | |
73 // copy the hostname in the URL container | |
997 | 74 Curl->hostname = (char*)malloc(pos2-pos1-3+1); |
833 | 75 if( Curl->hostname==NULL ) { |
76 printf("Memory allocation failed!\n"); | |
1631 | 77 return NULL; |
833 | 78 } |
79 strncpy(Curl->hostname, ptr1+3, pos2-pos1-3); | |
840 | 80 Curl->hostname[pos2-pos1-3] = '\0'; |
833 | 81 |
82 // Look if a path is given | |
83 ptr2 = strstr(ptr1+3, "/"); | |
84 if( ptr2!=NULL ) { | |
85 // A path/filename is given | |
86 // check if it's not a trailing '/' | |
87 if( strlen(ptr2)>1 ) { | |
88 // copy the path/filename in the URL container | |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
89 Curl->file = (char*)malloc(strlen(ptr2)+1); |
840 | 90 if( Curl->file==NULL ) { |
833 | 91 printf("Memory allocation failed!\n"); |
1631 | 92 return NULL; |
833 | 93 } |
997 | 94 strcpy(Curl->file, ptr2); |
833 | 95 } |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
96 } |
997 | 97 // Check if a filenme was given or set, else set it with '/' |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
98 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
99 Curl->file = (char*)malloc(2); |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
100 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
101 printf("Memory allocation failed!\n"); |
1631 | 102 return NULL; |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
103 } |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
104 strcpy(Curl->file, "/"); |
833 | 105 } |
106 | |
107 return Curl; | |
108 } | |
109 | |
3040 | 110 URL_t * |
111 url_copy(URL_t* url) { | |
112 URL_t *dup_url; | |
113 | |
114 if( url==NULL ) return NULL; | |
115 dup_url = (URL_t*)malloc(sizeof(URL_t)); | |
116 if( dup_url==NULL ) { | |
117 printf("Memory allocation failed!\n"); | |
118 return NULL; | |
119 } | |
120 memcpy( dup_url, url, sizeof(URL_t) ); | |
121 | |
122 return dup_url; | |
123 } | |
124 | |
125 | |
833 | 126 void |
902 | 127 url_free(URL_t* url) { |
1530
3effaac0ddb7
silly bug fixed - thanx Szekeres Istvan <szekeres@webvilag.com>
arpi
parents:
997
diff
changeset
|
128 if(!url) return; |
833 | 129 if(url->url) free(url->url); |
130 if(url->protocol) free(url->protocol); | |
131 if(url->hostname) free(url->hostname); | |
840 | 132 if(url->file) free(url->file); |
902 | 133 if(url->username) free(url->username); |
134 if(url->password) free(url->password); | |
833 | 135 free(url); |
136 } | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
137 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
138 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
139 /* Replace escape sequences in an URL (or a part of an URL) */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
140 /* works like strcpy(), but without return argument */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
141 /* unescape_url_string comes from ASFRecorder */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
142 void |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
143 url_unescape_string(char *outbuf, char *inbuf) |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
144 { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
145 unsigned char c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
146 do { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
147 c = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
148 if (c == '%') { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
149 unsigned char c1 = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
150 unsigned char c2 = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
151 if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) && |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
152 ((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
153 if (c1>='0' && c1<='9') c1-='0'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
154 else c1-='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
155 if (c2>='0' && c2<='9') c2-='0'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
156 else c2-='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
157 c = (c1<<4) + c2; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
158 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
159 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
160 *outbuf++ = c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
161 } while (c != '\0'); |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
162 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
163 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
164 /* Replace specific characters in the URL string by an escape sequence */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
165 /* works like strcpy(), but without return argument */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
166 /* escape_url_string comes from ASFRecorder */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
167 void |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
168 url_escape_string(char *outbuf, char *inbuf) { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
169 unsigned char c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
170 do { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
171 c = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
172 if( (c >= 'A' && c <= 'Z') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
173 (c >= 'a' && c <= 'z') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
174 (c >= '0' && c <= '9') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
175 (c >= 0x7f) || /* fareast languages(Chinese, Korean, Japanese) */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
176 c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
177 c=='*' || c=='\'' || c=='(' || c==')' || c=='%' || /* do not touch escape character */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
178 c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
179 c=='&' || c=='=' || c=='+' || c=='$' || c==',' || /* see RFC 2396 */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
180 c=='\0' ) { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
181 *outbuf++ = c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
182 } else { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
183 /* all others will be escaped */ |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
184 unsigned char c1 = ((c & 0xf0) >> 4); |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
185 unsigned char c2 = (c & 0x0f); |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
186 if (c1 < 10) c1+='0'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
187 else c1+='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
188 if (c2 < 10) c2+='0'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
189 else c2+='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
190 *outbuf++ = '%'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
191 *outbuf++ = c1; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
192 *outbuf++ = c2; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
193 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
194 } while (c != '\0'); |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
195 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
196 |