Mercurial > mplayer.hg
annotate libmpdemux/url.c @ 16447:b2379996cb88
screenshot filter
author | henry |
---|---|
date | Sun, 11 Sep 2005 07:25:50 +0000 |
parents | c41e22d77214 |
children | dfbe8cd0e081 |
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 */ | |
7 | |
833 | 8 #include <string.h> |
9 #include <stdlib.h> | |
10 #include <stdio.h> | |
12391 | 11 #include <ctype.h> |
833 | 12 |
13 #include "url.h" | |
5933 | 14 #include "mp_msg.h" |
833 | 15 |
16 URL_t* | |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
17 url_new(const char* url) { |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
18 int pos1, pos2,v6addr = 0; |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
19 URL_t* Curl = NULL; |
12391 | 20 char *escfilename=NULL; |
21 char *unescfilename=NULL; | |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
22 char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL; |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
23 int jumpSize = 3; |
833 | 24 |
4653
d2a7fcfeec6f
Removed the url_copy function , since it was badly implemented,
bertrand
parents:
4119
diff
changeset
|
25 if( url==NULL ) return NULL; |
d2a7fcfeec6f
Removed the url_copy function , since it was badly implemented,
bertrand
parents:
4119
diff
changeset
|
26 |
12391 | 27 // Create temp filename space |
28 unescfilename=malloc(strlen(url)+1); | |
29 if (!unescfilename ) { | |
30 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); | |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
31 goto err_out; |
12391 | 32 } |
33 escfilename=malloc(strlen(url)*3+1); | |
34 if (!escfilename ) { | |
35 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); | |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
36 goto err_out; |
12391 | 37 } |
38 | |
833 | 39 // Create the URL container |
40 Curl = (URL_t*)malloc(sizeof(URL_t)); | |
41 if( Curl==NULL ) { | |
5933 | 42 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
43 goto err_out; |
833 | 44 } |
12391 | 45 |
840 | 46 // Initialisation of the URL container members |
902 | 47 memset( Curl, 0, sizeof(URL_t) ); |
840 | 48 |
12391 | 49 //create unescaped/escaped versions of url |
50 url_unescape_string(unescfilename,url); // this is to prevent us from escaping an unescaped string | |
51 // violating RFC 2396 | |
52 url_escape_string(escfilename,unescfilename); | |
53 free(unescfilename); | |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
54 unescfilename = NULL; |
12391 | 55 |
833 | 56 // Copy the url in the URL container |
12391 | 57 Curl->url = strdup(escfilename); |
833 | 58 if( Curl->url==NULL ) { |
5933 | 59 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
60 goto err_out; |
833 | 61 } |
12391 | 62 mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename); |
833 | 63 |
64 // extract the protocol | |
12391 | 65 ptr1 = strstr(escfilename, "://"); |
833 | 66 if( ptr1==NULL ) { |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
67 // Check for a special case: "sip:" (without "//"): |
12391 | 68 if (strstr(escfilename, "sip:") == escfilename) { |
11412 | 69 ptr1 = (char *)&url[3]; // points to ':' |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
70 jumpSize = 1; |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
71 } else { |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
72 mp_msg(MSGT_NETWORK,MSGL_V,"Not an URL!\n"); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
73 goto err_out; |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
74 } |
833 | 75 } |
12391 | 76 pos1 = ptr1-escfilename; |
833 | 77 Curl->protocol = (char*)malloc(pos1+1); |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
78 if( Curl->protocol==NULL ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
79 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
80 goto err_out; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
81 } |
12637 | 82 strncpy(Curl->protocol, escfilename, pos1); |
833 | 83 Curl->protocol[pos1] = '\0'; |
84 | |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
85 // jump the "://" |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
86 ptr1 += jumpSize; |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
87 pos1 += jumpSize; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
88 |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
89 // check if a username:password is given |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
90 ptr2 = strstr(ptr1, "@"); |
7330
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
91 ptr3 = strstr(ptr1, "/"); |
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
92 if( ptr3!=NULL && ptr3<ptr2 ) { |
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
93 // it isn't really a username but rather a part of the path |
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
94 ptr2 = NULL; |
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
95 } |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
96 if( ptr2!=NULL ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
97 // We got something, at least a username... |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
98 int len = ptr2-ptr1; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
99 Curl->username = (char*)malloc(len+1); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
100 if( Curl->username==NULL ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
101 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
102 goto err_out; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
103 } |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
104 strncpy(Curl->username, ptr1, len); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
105 Curl->username[len] = '\0'; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
106 |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
107 ptr3 = strstr(ptr1, ":"); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
108 if( ptr3!=NULL && ptr3<ptr2 ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
109 // We also have a password |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
110 int len2 = ptr2-ptr3-1; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
111 Curl->username[ptr3-ptr1]='\0'; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
112 Curl->password = (char*)malloc(len2+1); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
113 if( Curl->password==NULL ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
114 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
115 goto err_out; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
116 } |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
117 strncpy( Curl->password, ptr3+1, len2); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
118 Curl->password[len2]='\0'; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
119 } |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
120 ptr1 = ptr2+1; |
12391 | 121 pos1 = ptr1-escfilename; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
122 } |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
123 |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
124 // before looking for a port number check if we have an IPv6 type numeric address |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
125 // in IPv6 URL the numeric address should be inside square braces. |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
126 ptr2 = strstr(ptr1, "["); |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
127 ptr3 = strstr(ptr1, "]"); |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
128 ptr4 = strstr(ptr1, "/"); |
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
129 if( ptr2!=NULL && ptr3!=NULL && ptr2 < ptr3 && (!ptr4 || ptr4 > ptr3)) { |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
130 // we have an IPv6 numeric address |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
131 ptr1++; |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
132 pos1++; |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
133 ptr2 = ptr3; |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
134 v6addr = 1; |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
135 } else { |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
136 ptr2 = ptr1; |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
137 |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
138 } |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
139 |
833 | 140 // look if the port is given |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
141 ptr2 = strstr(ptr2, ":"); |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
142 // If the : is after the first / it isn't the port |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
143 ptr3 = strstr(ptr1, "/"); |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
144 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL; |
833 | 145 if( ptr2==NULL ) { |
146 // No port is given | |
147 // Look if a path is given | |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
148 if( ptr3==NULL ) { |
833 | 149 // No path/filename |
150 // So we have an URL like http://www.hostname.com | |
12391 | 151 pos2 = strlen(escfilename); |
833 | 152 } else { |
153 // We have an URL like http://www.hostname.com/file.txt | |
12391 | 154 pos2 = ptr3-escfilename; |
833 | 155 } |
156 } else { | |
157 // We have an URL beginning like http://www.hostname.com:1212 | |
158 // Get the port number | |
159 Curl->port = atoi(ptr2+1); | |
12391 | 160 pos2 = ptr2-escfilename; |
833 | 161 } |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
162 if( v6addr ) pos2--; |
833 | 163 // copy the hostname in the URL container |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
164 Curl->hostname = (char*)malloc(pos2-pos1+1); |
833 | 165 if( Curl->hostname==NULL ) { |
5933 | 166 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
167 goto err_out; |
833 | 168 } |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
169 strncpy(Curl->hostname, ptr1, pos2-pos1); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
170 Curl->hostname[pos2-pos1] = '\0'; |
833 | 171 |
172 // Look if a path is given | |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
173 ptr2 = strstr(ptr1, "/"); |
833 | 174 if( ptr2!=NULL ) { |
175 // A path/filename is given | |
176 // check if it's not a trailing '/' | |
177 if( strlen(ptr2)>1 ) { | |
178 // copy the path/filename in the URL container | |
3612 | 179 Curl->file = strdup(ptr2); |
840 | 180 if( Curl->file==NULL ) { |
5933 | 181 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
182 goto err_out; |
833 | 183 } |
184 } | |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
185 } |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
186 // Check if a filename 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
|
187 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
188 Curl->file = (char*)malloc(2); |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
189 if( Curl->file==NULL ) { |
5933 | 190 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n"); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
191 goto err_out; |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
192 } |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
193 strcpy(Curl->file, "/"); |
833 | 194 } |
195 | |
12391 | 196 free(escfilename); |
833 | 197 return Curl; |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
198 err_out: |
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
199 if (escfilename) free(escfilename); |
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
200 if (unescfilename) free(unescfilename); |
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
201 if (Curl) url_free(Curl); |
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
202 return NULL; |
833 | 203 } |
204 | |
205 void | |
902 | 206 url_free(URL_t* url) { |
1530
3effaac0ddb7
silly bug fixed - thanx Szekeres Istvan <szekeres@webvilag.com>
arpi
parents:
997
diff
changeset
|
207 if(!url) return; |
833 | 208 if(url->url) free(url->url); |
209 if(url->protocol) free(url->protocol); | |
210 if(url->hostname) free(url->hostname); | |
840 | 211 if(url->file) free(url->file); |
902 | 212 if(url->username) free(url->username); |
213 if(url->password) free(url->password); | |
833 | 214 free(url); |
215 } | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
216 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
217 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
218 /* 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
|
219 /* 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
|
220 void |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
221 url_unescape_string(char *outbuf, const char *inbuf) |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
222 { |
12391 | 223 unsigned char c,c1,c2; |
224 int i,len=strlen(inbuf); | |
225 for (i=0;i<len;i++){ | |
226 c = inbuf[i]; | |
227 if (c == '%' && i<len-2) { //must have 2 more chars | |
228 c1 = toupper(inbuf[i+1]); // we need uppercase characters | |
229 c2 = toupper(inbuf[i+2]); | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
230 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
|
231 ((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
|
232 if (c1>='0' && c1<='9') c1-='0'; |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
233 else c1-='A'-10; |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
234 if (c2>='0' && c2<='9') c2-='0'; |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
235 else c2-='A'-10; |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
236 c = (c1<<4) + c2; |
12391 | 237 i=i+2; //only skip next 2 chars if valid esc |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
238 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
239 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
240 *outbuf++ = c; |
12391 | 241 } |
242 *outbuf++='\0'; //add nullterm to string | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
243 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
244 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
245 /* 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
|
246 /* 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
|
247 void |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
248 url_escape_string(char *outbuf, const char *inbuf) { |
12391 | 249 unsigned char c,c1,c2; |
250 int i,len=strlen(inbuf); | |
251 | |
252 for (i=0;i<len;i++) { | |
253 c = inbuf[i]; | |
254 if ((c=='%') && i<len-2 ) { //need 2 more characters | |
255 c1=toupper(inbuf[i+1]); c2=toupper(inbuf[i+2]); // need uppercase chars | |
256 } else { | |
257 c1=129; c2=129; //not escape chars | |
258 } | |
259 | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
260 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
|
261 (c >= 'a' && c <= 'z') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
262 (c >= '0' && c <= '9') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
263 (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
|
264 c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */ |
12153 | 265 c=='*' || c=='\'' || c=='(' || c==')' || /* do not touch escape character */ |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
266 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
|
267 c=='&' || c=='=' || c=='+' || c=='$' || c==',' || /* see RFC 2396 */ |
12391 | 268 c=='\0' ) { /* string term char */ |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
269 *outbuf++ = c; |
12391 | 270 } else if ( c=='%' && ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) && |
271 ((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) { | |
272 // check if part of an escape sequence | |
273 *outbuf++=c; // already | |
274 | |
275 // dont escape again | |
276 mp_msg(MSGT_NETWORK,MSGL_ERR,"string appears to be already escaped in url_escape %c%c1%c2\n",c,c1,c2); | |
277 // error as this should not happen against RFC 2396 | |
278 // to escape a string twice | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
279 } else { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
280 /* all others will be escaped */ |
12391 | 281 c1 = ((c & 0xf0) >> 4); |
282 c2 = (c & 0x0f); | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
283 if (c1 < 10) c1+='0'; |
9924 | 284 else c1+='A'-10; |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
285 if (c2 < 10) c2+='0'; |
9924 | 286 else c2+='A'-10; |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
287 *outbuf++ = '%'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
288 *outbuf++ = c1; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
289 *outbuf++ = c2; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
290 } |
12391 | 291 } |
292 *outbuf++='\0'; | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
293 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
294 |
4119
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
295 #ifdef __URL_DEBUG |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
296 void |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
297 url_debug(const URL_t *url) { |
4119
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
298 if( url==NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
299 printf("URL pointer NULL\n"); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
300 return; |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
301 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
302 if( url->url!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
303 printf("url=%s\n", url->url ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
304 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
305 if( url->protocol!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
306 printf("protocol=%s\n", url->protocol ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
307 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
308 if( url->hostname!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
309 printf("hostname=%s\n", url->hostname ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
310 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
311 printf("port=%d\n", url->port ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
312 if( url->file!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
313 printf("file=%s\n", url->file ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
314 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
315 if( url->username!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
316 printf("username=%s\n", url->username ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
317 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
318 if( url->password!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
319 printf("password=%s\n", url->password ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
320 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
321 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
322 #endif //__URL_DEBUG |