Mercurial > mplayer.hg
annotate libmpdemux/url.c @ 18971:ec2f6323fda3
Change SRC_PATH for ffmpeg back to '..' to avoid hardcoding current
directory at configure time. This should work again now that libpostproc
is no longer under libavcodec and all Makefiles included from ffmpeg are
at the same directory level.
The hardcoded paths caused breakage if the build directory was moved or
copied after configure and prevented ccache from sharing compilation
results between directories (different absolute include paths count as
different compiler options).
author | uau |
---|---|
date | Sun, 09 Jul 2006 14:06:13 +0000 |
parents | cc65a585fdcc |
children |
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> |
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
17048
diff
changeset
|
12 #include <inttypes.h> |
833 | 13 |
14 #include "url.h" | |
5933 | 15 #include "mp_msg.h" |
16882
dfbe8cd0e081
libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents:
16421
diff
changeset
|
16 #include "help_mp.h" |
833 | 17 |
18671 | 18 #ifndef SIZE_MAX |
19 #define SIZE_MAX ((size_t)-1) | |
20 #endif | |
21 | |
833 | 22 URL_t* |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
23 url_new(const char* url) { |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
24 int pos1, pos2,v6addr = 0; |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
25 URL_t* Curl = NULL; |
12391 | 26 char *escfilename=NULL; |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
27 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
|
28 int jumpSize = 3; |
833 | 29 |
4653
d2a7fcfeec6f
Removed the url_copy function , since it was badly implemented,
bertrand
parents:
4119
diff
changeset
|
30 if( url==NULL ) return NULL; |
d2a7fcfeec6f
Removed the url_copy function , since it was badly implemented,
bertrand
parents:
4119
diff
changeset
|
31 |
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
17048
diff
changeset
|
32 if (strlen(url) > (SIZE_MAX / 3 - 1)) { |
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
17048
diff
changeset
|
33 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
17048
diff
changeset
|
34 goto err_out; |
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
17048
diff
changeset
|
35 } |
12391 | 36 escfilename=malloc(strlen(url)*3+1); |
37 if (!escfilename ) { | |
16908
bd4cac0d5e0e
Changed MSGTR_MPDEMUX_URL_MallocFailed to MSGTR_MemAllocFailed (msg defined two times in help_mp-en.h)
ptt
parents:
16882
diff
changeset
|
38 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
39 goto err_out; |
12391 | 40 } |
41 | |
833 | 42 // Create the URL container |
18879 | 43 Curl = malloc(sizeof(URL_t)); |
833 | 44 if( Curl==NULL ) { |
16908
bd4cac0d5e0e
Changed MSGTR_MPDEMUX_URL_MallocFailed to MSGTR_MemAllocFailed (msg defined two times in help_mp-en.h)
ptt
parents:
16882
diff
changeset
|
45 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
46 goto err_out; |
833 | 47 } |
12391 | 48 |
840 | 49 // Initialisation of the URL container members |
902 | 50 memset( Curl, 0, sizeof(URL_t) ); |
840 | 51 |
17048
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
52 url_escape_string(escfilename,url); |
12391 | 53 |
833 | 54 // Copy the url in the URL container |
12391 | 55 Curl->url = strdup(escfilename); |
833 | 56 if( Curl->url==NULL ) { |
16908
bd4cac0d5e0e
Changed MSGTR_MPDEMUX_URL_MallocFailed to MSGTR_MemAllocFailed (msg defined two times in help_mp-en.h)
ptt
parents:
16882
diff
changeset
|
57 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
58 goto err_out; |
833 | 59 } |
12391 | 60 mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename); |
833 | 61 |
62 // extract the protocol | |
12391 | 63 ptr1 = strstr(escfilename, "://"); |
833 | 64 if( ptr1==NULL ) { |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
65 // Check for a special case: "sip:" (without "//"): |
12391 | 66 if (strstr(escfilename, "sip:") == escfilename) { |
11412 | 67 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
|
68 jumpSize = 1; |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
69 } else { |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
70 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
|
71 goto err_out; |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
72 } |
833 | 73 } |
12391 | 74 pos1 = ptr1-escfilename; |
18879 | 75 Curl->protocol = malloc(pos1+1); |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
76 if( Curl->protocol==NULL ) { |
16908
bd4cac0d5e0e
Changed MSGTR_MPDEMUX_URL_MallocFailed to MSGTR_MemAllocFailed (msg defined two times in help_mp-en.h)
ptt
parents:
16882
diff
changeset
|
77 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
78 goto err_out; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
79 } |
12637 | 80 strncpy(Curl->protocol, escfilename, pos1); |
833 | 81 Curl->protocol[pos1] = '\0'; |
82 | |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
83 // jump the "://" |
10056
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
84 ptr1 += jumpSize; |
88c855a174f3
Added some special-case code for checking for "sip:" URLs (because they
rsf
parents:
9924
diff
changeset
|
85 pos1 += jumpSize; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
86 |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
87 // check if a username:password is given |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
88 ptr2 = strstr(ptr1, "@"); |
7330
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
89 ptr3 = strstr(ptr1, "/"); |
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
90 if( ptr3!=NULL && ptr3<ptr2 ) { |
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
91 // 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
|
92 ptr2 = NULL; |
2f3fe8274028
Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents:
6513
diff
changeset
|
93 } |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
94 if( ptr2!=NULL ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
95 // We got something, at least a username... |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
96 int len = ptr2-ptr1; |
18879 | 97 Curl->username = malloc(len+1); |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
98 if( Curl->username==NULL ) { |
16908
bd4cac0d5e0e
Changed MSGTR_MPDEMUX_URL_MallocFailed to MSGTR_MemAllocFailed (msg defined two times in help_mp-en.h)
ptt
parents:
16882
diff
changeset
|
99 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
100 goto err_out; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
101 } |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
102 strncpy(Curl->username, ptr1, len); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
103 Curl->username[len] = '\0'; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
104 |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
105 ptr3 = strstr(ptr1, ":"); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
106 if( ptr3!=NULL && ptr3<ptr2 ) { |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
107 // We also have a password |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
108 int len2 = ptr2-ptr3-1; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
109 Curl->username[ptr3-ptr1]='\0'; |
18879 | 110 Curl->password = malloc(len2+1); |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
111 if( Curl->password==NULL ) { |
16908
bd4cac0d5e0e
Changed MSGTR_MPDEMUX_URL_MallocFailed to MSGTR_MemAllocFailed (msg defined two times in help_mp-en.h)
ptt
parents:
16882
diff
changeset
|
112 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
113 goto err_out; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
114 } |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
115 strncpy( Curl->password, ptr3+1, len2); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
116 Curl->password[len2]='\0'; |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
117 } |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
118 ptr1 = ptr2+1; |
12391 | 119 pos1 = ptr1-escfilename; |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
120 } |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
121 |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
122 // 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
|
123 // 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
|
124 ptr2 = strstr(ptr1, "["); |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
125 ptr3 = strstr(ptr1, "]"); |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
126 ptr4 = strstr(ptr1, "/"); |
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
127 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
|
128 // 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
|
129 ptr1++; |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
130 pos1++; |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
131 ptr2 = ptr3; |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
132 v6addr = 1; |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
133 } else { |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
134 ptr2 = ptr1; |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
135 |
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
136 } |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
137 |
833 | 138 // 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
|
139 ptr2 = strstr(ptr2, ":"); |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
140 // 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
|
141 ptr3 = strstr(ptr1, "/"); |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3040
diff
changeset
|
142 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL; |
833 | 143 if( ptr2==NULL ) { |
144 // No port is given | |
145 // 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
|
146 if( ptr3==NULL ) { |
833 | 147 // No path/filename |
148 // So we have an URL like http://www.hostname.com | |
12391 | 149 pos2 = strlen(escfilename); |
833 | 150 } else { |
151 // We have an URL like http://www.hostname.com/file.txt | |
12391 | 152 pos2 = ptr3-escfilename; |
833 | 153 } |
154 } else { | |
155 // We have an URL beginning like http://www.hostname.com:1212 | |
156 // Get the port number | |
157 Curl->port = atoi(ptr2+1); | |
12391 | 158 pos2 = ptr2-escfilename; |
833 | 159 } |
11771
827cae571c5c
Fix long standing bug where last (and sometimes first) char of the
albeu
parents:
11412
diff
changeset
|
160 if( v6addr ) pos2--; |
833 | 161 // copy the hostname in the URL container |
18879 | 162 Curl->hostname = malloc(pos2-pos1+1); |
833 | 163 if( Curl->hostname==NULL ) { |
16908
bd4cac0d5e0e
Changed MSGTR_MPDEMUX_URL_MallocFailed to MSGTR_MemAllocFailed (msg defined two times in help_mp-en.h)
ptt
parents:
16882
diff
changeset
|
164 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
165 goto err_out; |
833 | 166 } |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
167 strncpy(Curl->hostname, ptr1, pos2-pos1); |
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
168 Curl->hostname[pos2-pos1] = '\0'; |
833 | 169 |
170 // Look if a path is given | |
6513
6e9d7a6b1806
Added support for URLs that contain an username:password
bertrand
parents:
5933
diff
changeset
|
171 ptr2 = strstr(ptr1, "/"); |
833 | 172 if( ptr2!=NULL ) { |
173 // A path/filename is given | |
174 // check if it's not a trailing '/' | |
175 if( strlen(ptr2)>1 ) { | |
176 // copy the path/filename in the URL container | |
3612 | 177 Curl->file = strdup(ptr2); |
840 | 178 if( Curl->file==NULL ) { |
16908
bd4cac0d5e0e
Changed MSGTR_MPDEMUX_URL_MallocFailed to MSGTR_MemAllocFailed (msg defined two times in help_mp-en.h)
ptt
parents:
16882
diff
changeset
|
179 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
180 goto err_out; |
833 | 181 } |
182 } | |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
183 } |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
184 // 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
|
185 if( Curl->file==NULL ) { |
18879 | 186 Curl->file = malloc(2); |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
187 if( Curl->file==NULL ) { |
16908
bd4cac0d5e0e
Changed MSGTR_MPDEMUX_URL_MallocFailed to MSGTR_MemAllocFailed (msg defined two times in help_mp-en.h)
ptt
parents:
16882
diff
changeset
|
188 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
189 goto err_out; |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
190 } |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
191 strcpy(Curl->file, "/"); |
833 | 192 } |
193 | |
12391 | 194 free(escfilename); |
833 | 195 return Curl; |
16421
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
196 err_out: |
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
197 if (escfilename) free(escfilename); |
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
198 if (Curl) url_free(Curl); |
c41e22d77214
memleak fix, escfilename was not freed for an invalid url
reimar
parents:
12637
diff
changeset
|
199 return NULL; |
833 | 200 } |
201 | |
202 void | |
902 | 203 url_free(URL_t* url) { |
1530
3effaac0ddb7
silly bug fixed - thanx Szekeres Istvan <szekeres@webvilag.com>
arpi
parents:
997
diff
changeset
|
204 if(!url) return; |
833 | 205 if(url->url) free(url->url); |
206 if(url->protocol) free(url->protocol); | |
207 if(url->hostname) free(url->hostname); | |
840 | 208 if(url->file) free(url->file); |
902 | 209 if(url->username) free(url->username); |
210 if(url->password) free(url->password); | |
833 | 211 free(url); |
212 } | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
213 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
214 |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
215 /* 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
|
216 /* 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
|
217 void |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
218 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
|
219 { |
12391 | 220 unsigned char c,c1,c2; |
221 int i,len=strlen(inbuf); | |
222 for (i=0;i<len;i++){ | |
223 c = inbuf[i]; | |
224 if (c == '%' && i<len-2) { //must have 2 more chars | |
225 c1 = toupper(inbuf[i+1]); // we need uppercase characters | |
226 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
|
227 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
|
228 ((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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 c = (c1<<4) + c2; |
12391 | 234 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
|
235 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
236 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
237 *outbuf++ = c; |
12391 | 238 } |
239 *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
|
240 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
241 |
17048
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
242 static void |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
243 url_escape_string_part(char *outbuf, const char *inbuf) { |
12391 | 244 unsigned char c,c1,c2; |
245 int i,len=strlen(inbuf); | |
246 | |
247 for (i=0;i<len;i++) { | |
248 c = inbuf[i]; | |
249 if ((c=='%') && i<len-2 ) { //need 2 more characters | |
250 c1=toupper(inbuf[i+1]); c2=toupper(inbuf[i+2]); // need uppercase chars | |
251 } else { | |
252 c1=129; c2=129; //not escape chars | |
253 } | |
254 | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
255 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
|
256 (c >= 'a' && c <= 'z') || |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
257 (c >= '0' && c <= '9') || |
17048
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
258 (c >= 0x7f)) { |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
259 *outbuf++ = c; |
12391 | 260 } else if ( c=='%' && ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) && |
261 ((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) { | |
262 // check if part of an escape sequence | |
263 *outbuf++=c; // already | |
264 | |
265 // dont escape again | |
16882
dfbe8cd0e081
libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents:
16421
diff
changeset
|
266 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_URL_StringAlreadyEscaped,c,c1,c2); |
12391 | 267 // error as this should not happen against RFC 2396 |
268 // 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
|
269 } else { |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
270 /* all others will be escaped */ |
12391 | 271 c1 = ((c & 0xf0) >> 4); |
272 c2 = (c & 0x0f); | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
273 if (c1 < 10) c1+='0'; |
9924 | 274 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
|
275 if (c2 < 10) c2+='0'; |
9924 | 276 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
|
277 *outbuf++ = '%'; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
278 *outbuf++ = c1; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
279 *outbuf++ = c2; |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
280 } |
12391 | 281 } |
282 *outbuf++='\0'; | |
3496
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
283 } |
5c8a533dfa09
Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents:
3494
diff
changeset
|
284 |
17048
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
285 /* Replace specific characters in the URL string by an escape sequence */ |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
286 /* works like strcpy(), but without return argument */ |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
287 void |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
288 url_escape_string(char *outbuf, const char *inbuf) { |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
289 unsigned char c; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
290 int i = 0,j,len = strlen(inbuf); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
291 char* tmp,*unesc = NULL, *in; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
292 |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
293 // Look if we have an ip6 address, if so skip it there is |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
294 // no need to escape anything in there. |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
295 tmp = strstr(inbuf,"://["); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
296 if(tmp) { |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
297 tmp = strchr(tmp+4,']'); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
298 if(tmp && (tmp[1] == '/' || tmp[1] == ':' || |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
299 tmp[1] == '\0')) { |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
300 i = tmp+1-inbuf; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
301 strncpy(outbuf,inbuf,i); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
302 outbuf += i; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
303 tmp = NULL; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
304 } |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
305 } |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
306 |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
307 while(i < len) { |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
308 // look for the next char that must be kept |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
309 for (j=i;j<len;j++) { |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
310 c = inbuf[j]; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
311 if(c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */ |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
312 c=='*' || c=='\'' || c=='(' || c==')' || /* do not touch escape character */ |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
313 c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */ |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
314 c=='&' || c=='=' || c=='+' || c=='$' || c==',') /* see RFC 2396 */ |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
315 break; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
316 } |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
317 // we are on a reserved char, write it out |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
318 if(j == i) { |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
319 *outbuf++ = c; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
320 i++; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
321 continue; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
322 } |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
323 // we found one, take that part of the string |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
324 if(j < len) { |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
325 if(!tmp) tmp = malloc(len+1); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
326 strncpy(tmp,inbuf+i,j-i); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
327 tmp[j-i] = '\0'; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
328 in = tmp; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
329 } else // take the rest of the string |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
330 in = (char*)inbuf+i; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
331 |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
332 if(!unesc) unesc = malloc(len+1); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
333 // unescape first to avoid escaping escape |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
334 url_unescape_string(unesc,in); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
335 // then escape, including mark and other reserved chars |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
336 // that can come from escape sequences |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
337 url_escape_string_part(outbuf,unesc); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
338 outbuf += strlen(outbuf); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
339 i += strlen(in); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
340 } |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
341 *outbuf = '\0'; |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
342 if(tmp) free(tmp); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
343 if(unesc) free(unesc); |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
344 } |
9e02b9cb171b
Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents:
16908
diff
changeset
|
345 |
4119
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
346 #ifdef __URL_DEBUG |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
347 void |
9690
a9b7b6055563
Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents:
7809
diff
changeset
|
348 url_debug(const URL_t *url) { |
4119
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
349 if( url==NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
350 printf("URL pointer NULL\n"); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
351 return; |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
352 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
353 if( url->url!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
354 printf("url=%s\n", url->url ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
355 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
356 if( url->protocol!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
357 printf("protocol=%s\n", url->protocol ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
358 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
359 if( url->hostname!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
360 printf("hostname=%s\n", url->hostname ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
361 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
362 printf("port=%d\n", url->port ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
363 if( url->file!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
364 printf("file=%s\n", url->file ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
365 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
366 if( url->username!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
367 printf("username=%s\n", url->username ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
368 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
369 if( url->password!=NULL ) { |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
370 printf("password=%s\n", url->password ); |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
371 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
372 } |
639b3b47b138
Added a debug function to print the struct's variables.
bertrand
parents:
3612
diff
changeset
|
373 #endif //__URL_DEBUG |