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