Mercurial > mplayer.hg
annotate libmpdemux/url.c @ 6069:8e88e92fe331
Initial support for dxr2. Based on patch from Tobias Diedrich <ranma@gmx.at>.
author | albeu |
---|---|
date | Mon, 13 May 2002 13:15:40 +0000 |
parents | 14b46420840b |
children | 6e9d7a6b1806 |
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" | |
5933 | 15 #include "mp_msg.h" |
833 | 16 |
17 URL_t* | |
902 | 18 url_new(char* url) { |
833 | 19 int pos1, pos2; |
20 URL_t* Curl; | |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
21 char *ptr1, *ptr2, *ptr3; |
833 | 22 |
4653
d2a7fcfeec6f
Removed the url_copy function , since it was badly implemented,
bertrand
parents:
4119
diff
changeset
|
23 if( url==NULL ) return NULL; |
d2a7fcfeec6f
Removed the url_copy function , since it was badly implemented,
bertrand
parents:
4119
diff
changeset
|
24 |
833 | 25 // Create the URL container |
26 Curl = (URL_t*)malloc(sizeof(URL_t)); | |
27 if( Curl==NULL ) { | |
5933 | 28 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
1631 | 29 return NULL; |
833 | 30 } |
840 | 31 // Initialisation of the URL container members |
902 | 32 memset( Curl, 0, sizeof(URL_t) ); |
840 | 33 |
833 | 34 // Copy the url in the URL container |
3612 | 35 Curl->url = strdup(url); |
833 | 36 if( Curl->url==NULL ) { |
5933 | 37 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
1631 | 38 return NULL; |
833 | 39 } |
40 | |
41 // extract the protocol | |
42 ptr1 = strstr(url, "://"); | |
43 if( ptr1==NULL ) { | |
5933 | 44 mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n"); |
833 | 45 return NULL; |
46 } | |
47 pos1 = ptr1-url; | |
48 Curl->protocol = (char*)malloc(pos1+1); | |
49 strncpy(Curl->protocol, url, pos1); | |
50 Curl->protocol[pos1] = '\0'; | |
51 | |
52 // look if the port is given | |
53 ptr2 = strstr(ptr1+3, ":"); | |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
54 // 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
|
55 ptr3 = strstr(ptr1+3, "/"); |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
56 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL; |
833 | 57 if( ptr2==NULL ) { |
58 // No port is given | |
59 // Look if a path is given | |
60 ptr2 = strstr(ptr1+3, "/"); | |
61 if( ptr2==NULL ) { | |
62 // No path/filename | |
63 // So we have an URL like http://www.hostname.com | |
64 pos2 = strlen(url); | |
65 } else { | |
66 // We have an URL like http://www.hostname.com/file.txt | |
67 pos2 = ptr2-url; | |
68 } | |
69 } else { | |
70 // We have an URL beginning like http://www.hostname.com:1212 | |
71 // Get the port number | |
72 Curl->port = atoi(ptr2+1); | |
73 pos2 = ptr2-url; | |
74 } | |
75 // copy the hostname in the URL container | |
997 | 76 Curl->hostname = (char*)malloc(pos2-pos1-3+1); |
833 | 77 if( Curl->hostname==NULL ) { |
5933 | 78 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
1631 | 79 return NULL; |
833 | 80 } |
81 strncpy(Curl->hostname, ptr1+3, pos2-pos1-3); | |
840 | 82 Curl->hostname[pos2-pos1-3] = '\0'; |
833 | 83 |
84 // Look if a path is given | |
85 ptr2 = strstr(ptr1+3, "/"); | |
86 if( ptr2!=NULL ) { | |
87 // A path/filename is given | |
88 // check if it's not a trailing '/' | |
89 if( strlen(ptr2)>1 ) { | |
90 // copy the path/filename in the URL container | |
3612 | 91 Curl->file = strdup(ptr2); |
840 | 92 if( Curl->file==NULL ) { |
5933 | 93 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
1631 | 94 return NULL; |
833 | 95 } |
96 } | |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
97 } |
997 | 98 // 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
|
99 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
100 Curl->file = (char*)malloc(2); |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
101 if( Curl->file==NULL ) { |
5933 | 102 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
1631 | 103 return NULL; |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
104 } |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
105 strcpy(Curl->file, "/"); |
833 | 106 } |
107 | |
108 return Curl; | |
109 } | |
110 | |
111 void | |
902 | 112 url_free(URL_t* url) { |
1530
3effaac0ddb7
silly bug fixed - thanx Szekeres Istvan <szekeres@webvilag.com>
arpi
parents:
997
diff
changeset
|
113 if(!url) return; |
833 | 114 if(url->url) free(url->url); |
115 if(url->protocol) free(url->protocol); | |
116 if(url->hostname) free(url->hostname); | |
840 | 117 if(url->file) free(url->file); |
902 | 118 if(url->username) free(url->username); |
119 if(url->password) free(url->password); | |
833 | 120 free(url); |
121 } | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
122 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
123 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
124 /* 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
|
125 /* 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
|
126 /* 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
|
127 void |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
128 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
|
129 { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
130 unsigned char c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
131 do { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
132 c = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
133 if (c == '%') { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
134 unsigned char c1 = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
135 unsigned char c2 = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
136 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
|
137 ((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
|
138 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
|
139 else c1-='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
140 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
|
141 else c2-='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
142 c = (c1<<4) + c2; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
143 } |
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 *outbuf++ = c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
146 } while (c != '\0'); |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
147 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
148 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
149 /* 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
|
150 /* 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
|
151 /* 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
|
152 void |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
153 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
|
154 unsigned char c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
155 do { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
156 c = *inbuf++; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
157 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
|
158 (c >= 'a' && c <= 'z') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
159 (c >= '0' && c <= '9') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
160 (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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 c=='\0' ) { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
166 *outbuf++ = c; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
167 } else { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
168 /* 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
|
169 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
|
170 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
|
171 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
|
172 else c1+='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
173 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
|
174 else c2+='A'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
175 *outbuf++ = '%'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
176 *outbuf++ = c1; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
177 *outbuf++ = c2; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
178 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
179 } while (c != '\0'); |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
180 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
181 |
4119
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
182 #ifdef __URL_DEBUG |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
183 void |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
184 url_debug(URL_t *url) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
185 if( url==NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
186 printf("URL pointer NULL\n"); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
187 return; |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
188 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
189 if( url->url!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
190 printf("url=%s\n", url->url ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
191 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
192 if( url->protocol!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
193 printf("protocol=%s\n", url->protocol ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
194 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
195 if( url->hostname!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
196 printf("hostname=%s\n", url->hostname ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
197 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
198 printf("port=%d\n", url->port ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
199 if( url->file!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
200 printf("file=%s\n", url->file ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
201 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
202 if( url->username!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
203 printf("username=%s\n", url->username ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
204 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
205 if( url->password!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
206 printf("password=%s\n", url->password ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
207 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
208 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
209 #endif //__URL_DEBUG |