annotate stream/url.c @ 32549:cf1f4f5bc096

Set font_fontconfig to -1 if CONFIG_FONTCONFIG is not configured. Without this, -font and -subfont do not work when using -ass.
author komh
date Sat, 20 Nov 2010 11:39:05 +0000
parents 899d7464d246
children c44141d4e443
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
2 * URL Helper
30426
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
3 *
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
4 * Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com>
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
5 *
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
6 * This file is part of MPlayer.
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
7 *
30426
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
8 * MPlayer is free software; you can redistribute it and/or modify
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
9 * it under the terms of the GNU General Public License as published by
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
11 * (at your option) any later version.
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
12 *
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
13 * MPlayer is distributed in the hope that it will be useful,
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
16 * GNU General Public License for more details.
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
17 *
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
18 * You should have received a copy of the GNU General Public License along
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29263
diff changeset
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
21 */
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
22
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
23 #include <string.h>
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
24 #include <stdlib.h>
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
25 #include <stdio.h>
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
26 #include <ctype.h>
18558
4928dd61f136 Fix potential integer overflows in memory allocation.
rtogni
parents: 17048
diff changeset
27 #include <inttypes.h>
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
28
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
29 #include "url.h"
5933
14b46420840b printf to mp_msg
albeu
parents: 4653
diff changeset
30 #include "mp_msg.h"
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 16421
diff changeset
31 #include "help_mp.h"
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
32
18671
8fb542b97815 Protect SIZE_MAX use
rtogni
parents: 18558
diff changeset
33 #ifndef SIZE_MAX
8fb542b97815 Protect SIZE_MAX use
rtogni
parents: 18558
diff changeset
34 #define SIZE_MAX ((size_t)-1)
8fb542b97815 Protect SIZE_MAX use
rtogni
parents: 18558
diff changeset
35 #endif
8fb542b97815 Protect SIZE_MAX use
rtogni
parents: 18558
diff changeset
36
20784
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
37 URL_t *url_redirect(URL_t **url, const char *redir) {
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
38 URL_t *u = *url;
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
39 URL_t *res;
21125
4aff19cc00bb Also support absolute url redirection, e.g. http://www.youtube.com/v/buKaqRG2SFA
reimar
parents: 20784
diff changeset
40 if (!strchr(redir, '/') || *redir == '/') {
20784
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
41 char *tmp;
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
42 char *newurl = malloc(strlen(u->url) + strlen(redir) + 1);
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
43 strcpy(newurl, u->url);
21125
4aff19cc00bb Also support absolute url redirection, e.g. http://www.youtube.com/v/buKaqRG2SFA
reimar
parents: 20784
diff changeset
44 if (*redir == '/') {
4aff19cc00bb Also support absolute url redirection, e.g. http://www.youtube.com/v/buKaqRG2SFA
reimar
parents: 20784
diff changeset
45 redir++;
4aff19cc00bb Also support absolute url redirection, e.g. http://www.youtube.com/v/buKaqRG2SFA
reimar
parents: 20784
diff changeset
46 tmp = strstr(newurl, "://");
4aff19cc00bb Also support absolute url redirection, e.g. http://www.youtube.com/v/buKaqRG2SFA
reimar
parents: 20784
diff changeset
47 if (tmp) tmp = strchr(tmp + 3, '/');
4aff19cc00bb Also support absolute url redirection, e.g. http://www.youtube.com/v/buKaqRG2SFA
reimar
parents: 20784
diff changeset
48 } else
21126
83dc11e40f28 cosmetics
reimar
parents: 21125
diff changeset
49 tmp = strrchr(newurl, '/');
20784
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
50 if (tmp) tmp[1] = 0;
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
51 strcat(newurl, redir);
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
52 res = url_new(newurl);
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
53 free(newurl);
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
54 } else
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
55 res = url_new(redir);
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
56 url_free(u);
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
57 *url = res;
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
58 return res;
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
59 }
720206eef78b Support URL redirections that do not specify full URL.
reimar
parents: 19271
diff changeset
60
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
61 URL_t*
9690
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
62 url_new(const char* url) {
11771
827cae571c5c Fix long standing bug where last (and sometimes first) char of the
albeu
parents: 11412
diff changeset
63 int pos1, pos2,v6addr = 0;
16421
c41e22d77214 memleak fix, escfilename was not freed for an invalid url
reimar
parents: 12637
diff changeset
64 URL_t* Curl = NULL;
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
65 char *escfilename=NULL;
11771
827cae571c5c Fix long standing bug where last (and sometimes first) char of the
albeu
parents: 11412
diff changeset
66 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
67 int jumpSize = 3;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
68
4653
d2a7fcfeec6f Removed the url_copy function , since it was badly implemented,
bertrand
parents: 4119
diff changeset
69 if( url==NULL ) return NULL;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 25795
diff changeset
70
18558
4928dd61f136 Fix potential integer overflows in memory allocation.
rtogni
parents: 17048
diff changeset
71 if (strlen(url) > (SIZE_MAX / 3 - 1)) {
4928dd61f136 Fix potential integer overflows in memory allocation.
rtogni
parents: 17048
diff changeset
72 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
4928dd61f136 Fix potential integer overflows in memory allocation.
rtogni
parents: 17048
diff changeset
73 goto err_out;
4928dd61f136 Fix potential integer overflows in memory allocation.
rtogni
parents: 17048
diff changeset
74 }
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
75 escfilename=malloc(strlen(url)*3+1);
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
76 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
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;
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
79 }
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
80
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
81 // Create the URL container
18879
cc65a585fdcc rm unnecesary casts from void* - part 3
reynaldo
parents: 18671
diff changeset
82 Curl = malloc(sizeof(URL_t));
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
83 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
84 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
85 goto err_out;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
86 }
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
87
840
9141234715a2 Added initialisation of URL pointers.
bertrand
parents: 833
diff changeset
88 // Initialisation of the URL container members
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
89 memset( Curl, 0, sizeof(URL_t) );
840
9141234715a2 Added initialisation of URL pointers.
bertrand
parents: 833
diff changeset
90
17048
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
91 url_escape_string(escfilename,url);
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
92
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
93 // Copy the url in the URL container
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
94 Curl->url = strdup(escfilename);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
95 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
96 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
97 goto err_out;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
98 }
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
99 mp_msg(MSGT_OPEN,MSGL_V,"Filename for url is now %s\n",escfilename);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
100
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
101 // extract the protocol
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
102 ptr1 = strstr(escfilename, "://");
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
103 if( ptr1==NULL ) {
10056
88c855a174f3 Added some special-case code for checking for "sip:" URLs (because they
rsf
parents: 9924
diff changeset
104 // Check for a special case: "sip:" (without "//"):
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
105 if (strstr(escfilename, "sip:") == escfilename) {
11412
ec3dac7d17a0 Warning fixes (approved by A'rpi).
rathann
parents: 10056
diff changeset
106 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
107 jumpSize = 1;
88c855a174f3 Added some special-case code for checking for "sip:" URLs (because they
rsf
parents: 9924
diff changeset
108 } else {
88c855a174f3 Added some special-case code for checking for "sip:" URLs (because they
rsf
parents: 9924
diff changeset
109 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
110 goto err_out;
10056
88c855a174f3 Added some special-case code for checking for "sip:" URLs (because they
rsf
parents: 9924
diff changeset
111 }
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
112 }
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
113 pos1 = ptr1-escfilename;
18879
cc65a585fdcc rm unnecesary casts from void* - part 3
reynaldo
parents: 18671
diff changeset
114 Curl->protocol = malloc(pos1+1);
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
115 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
116 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
117 goto err_out;
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
118 }
12637
5dc89722fad2 uber 10l found by Ilia <chest4l at mail.ru>
alex
parents: 12391
diff changeset
119 strncpy(Curl->protocol, escfilename, pos1);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
120 Curl->protocol[pos1] = '\0';
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
121
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
122 // jump the "://"
10056
88c855a174f3 Added some special-case code for checking for "sip:" URLs (because they
rsf
parents: 9924
diff changeset
123 ptr1 += jumpSize;
88c855a174f3 Added some special-case code for checking for "sip:" URLs (because they
rsf
parents: 9924
diff changeset
124 pos1 += jumpSize;
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
125
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
126 // check if a username:password is given
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
127 ptr2 = strstr(ptr1, "@");
7330
2f3fe8274028 Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents: 6513
diff changeset
128 ptr3 = strstr(ptr1, "/");
2f3fe8274028 Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents: 6513
diff changeset
129 if( ptr3!=NULL && ptr3<ptr2 ) {
2f3fe8274028 Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents: 6513
diff changeset
130 // 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
131 ptr2 = NULL;
2f3fe8274028 Applied patch from Gregory Kovriga <gkovriga@techunix.technion.ac.il>
bertrand
parents: 6513
diff changeset
132 }
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
133 if( ptr2!=NULL ) {
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
134 // We got something, at least a username...
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
135 int len = ptr2-ptr1;
18879
cc65a585fdcc rm unnecesary casts from void* - part 3
reynaldo
parents: 18671
diff changeset
136 Curl->username = malloc(len+1);
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
137 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
138 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
139 goto err_out;
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
140 }
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
141 strncpy(Curl->username, ptr1, len);
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
142 Curl->username[len] = '\0';
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
143
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
144 ptr3 = strstr(ptr1, ":");
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
145 if( ptr3!=NULL && ptr3<ptr2 ) {
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
146 // We also have a password
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
147 int len2 = ptr2-ptr3-1;
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
148 Curl->username[ptr3-ptr1]='\0';
18879
cc65a585fdcc rm unnecesary casts from void* - part 3
reynaldo
parents: 18671
diff changeset
149 Curl->password = malloc(len2+1);
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
150 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
151 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
152 goto err_out;
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
153 }
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
154 strncpy( Curl->password, ptr3+1, len2);
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
155 Curl->password[len2]='\0';
32534
899d7464d246 Unescape username/password when URL-parsing.
reimar
parents: 32511
diff changeset
156 url_unescape_string(Curl->password, Curl->password);
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
157 }
32534
899d7464d246 Unescape username/password when URL-parsing.
reimar
parents: 32511
diff changeset
158 url_unescape_string(Curl->username, Curl->username);
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
159 ptr1 = ptr2+1;
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
160 pos1 = ptr1-escfilename;
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
161 }
9690
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
162
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
163 // 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
164 // 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
165 ptr2 = strstr(ptr1, "[");
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
166 ptr3 = strstr(ptr1, "]");
11771
827cae571c5c Fix long standing bug where last (and sometimes first) char of the
albeu
parents: 11412
diff changeset
167 ptr4 = strstr(ptr1, "/");
827cae571c5c Fix long standing bug where last (and sometimes first) char of the
albeu
parents: 11412
diff changeset
168 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
169 // 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
170 ptr1++;
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
171 pos1++;
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
172 ptr2 = ptr3;
11771
827cae571c5c Fix long standing bug where last (and sometimes first) char of the
albeu
parents: 11412
diff changeset
173 v6addr = 1;
9690
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
174 } else {
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
175 ptr2 = ptr1;
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
176
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
177 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 25795
diff changeset
178
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
179 // 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
180 ptr2 = strstr(ptr2, ":");
3494
fb9de639ed30 Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents: 3040
diff changeset
181 // 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
182 ptr3 = strstr(ptr1, "/");
3494
fb9de639ed30 Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents: 3040
diff changeset
183 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
184 if( ptr2==NULL ) {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
185 // No port is given
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
186 // 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
187 if( ptr3==NULL ) {
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
188 // No path/filename
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
189 // So we have an URL like http://www.hostname.com
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
190 pos2 = strlen(escfilename);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
191 } else {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
192 // We have an URL like http://www.hostname.com/file.txt
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
193 pos2 = ptr3-escfilename;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
194 }
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
195 } else {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
196 // We have an URL beginning like http://www.hostname.com:1212
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
197 // Get the port number
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
198 Curl->port = atoi(ptr2+1);
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
199 pos2 = ptr2-escfilename;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
200 }
11771
827cae571c5c Fix long standing bug where last (and sometimes first) char of the
albeu
parents: 11412
diff changeset
201 if( v6addr ) pos2--;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
202 // copy the hostname in the URL container
18879
cc65a585fdcc rm unnecesary casts from void* - part 3
reynaldo
parents: 18671
diff changeset
203 Curl->hostname = malloc(pos2-pos1+1);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
204 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
205 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
206 goto err_out;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
207 }
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
208 strncpy(Curl->hostname, ptr1, pos2-pos1);
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
209 Curl->hostname[pos2-pos1] = '\0';
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
210
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
211 // Look if a path is given
6513
6e9d7a6b1806 Added support for URLs that contain an username:password
bertrand
parents: 5933
diff changeset
212 ptr2 = strstr(ptr1, "/");
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
213 if( ptr2!=NULL ) {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
214 // A path/filename is given
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
215 // check if it's not a trailing '/'
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
216 if( strlen(ptr2)>1 ) {
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
217 // copy the path/filename in the URL container
3612
a1522fa7728a x = malloc(strlen(s) + c) ... strcpy(x, s)
pl
parents: 3496
diff changeset
218 Curl->file = strdup(ptr2);
840
9141234715a2 Added initialisation of URL pointers.
bertrand
parents: 833
diff changeset
219 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
220 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
221 goto err_out;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
222 }
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
223 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 25795
diff changeset
224 }
9690
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
225 // 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
226 if( Curl->file==NULL ) {
18879
cc65a585fdcc rm unnecesary casts from void* - part 3
reynaldo
parents: 18671
diff changeset
227 Curl->file = malloc(2);
869
e350849ff400 Url given without a filename/path get the filename/path '/'
bertrand
parents: 840
diff changeset
228 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
229 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
230 goto err_out;
869
e350849ff400 Url given without a filename/path get the filename/path '/'
bertrand
parents: 840
diff changeset
231 }
e350849ff400 Url given without a filename/path get the filename/path '/'
bertrand
parents: 840
diff changeset
232 strcpy(Curl->file, "/");
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
233 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 25795
diff changeset
234
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
235 free(escfilename);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
236 return Curl;
16421
c41e22d77214 memleak fix, escfilename was not freed for an invalid url
reimar
parents: 12637
diff changeset
237 err_out:
32511
b39155e98ac3 Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents: 30426
diff changeset
238 free(escfilename);
16421
c41e22d77214 memleak fix, escfilename was not freed for an invalid url
reimar
parents: 12637
diff changeset
239 if (Curl) url_free(Curl);
c41e22d77214 memleak fix, escfilename was not freed for an invalid url
reimar
parents: 12637
diff changeset
240 return NULL;
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
241 }
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
242
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
243 void
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 869
diff changeset
244 url_free(URL_t* url) {
1530
3effaac0ddb7 silly bug fixed - thanx Szekeres Istvan <szekeres@webvilag.com>
arpi
parents: 997
diff changeset
245 if(!url) return;
32511
b39155e98ac3 Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents: 30426
diff changeset
246 free(url->url);
b39155e98ac3 Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents: 30426
diff changeset
247 free(url->protocol);
b39155e98ac3 Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents: 30426
diff changeset
248 free(url->hostname);
b39155e98ac3 Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents: 30426
diff changeset
249 free(url->file);
b39155e98ac3 Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents: 30426
diff changeset
250 free(url->username);
b39155e98ac3 Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents: 30426
diff changeset
251 free(url->password);
833
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
252 free(url);
b8cecdc0c67f Starting implementation of ASF network streaming.
bertrand
parents:
diff changeset
253 }
3496
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
254
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
255
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
256 /* Replace escape sequences in an URL (or a part of an URL) */
32534
899d7464d246 Unescape username/password when URL-parsing.
reimar
parents: 32511
diff changeset
257 /* works like strcpy(), but without return argument,
899d7464d246 Unescape username/password when URL-parsing.
reimar
parents: 32511
diff changeset
258 except that outbuf == inbuf is allowed */
3496
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
259 void
9690
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
260 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
261 {
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
262 unsigned char c,c1,c2;
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
263 int i,len=strlen(inbuf);
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
264 for (i=0;i<len;i++){
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
265 c = inbuf[i];
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
266 if (c == '%' && i<len-2) { //must have 2 more chars
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
267 c1 = toupper(inbuf[i+1]); // we need uppercase characters
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
268 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
269 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
270 ((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
271 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
272 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
273 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
274 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
275 c = (c1<<4) + c2;
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
276 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
277 }
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
278 }
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
279 *outbuf++ = c;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 25795
diff changeset
280 }
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
281 *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
282 }
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
283
17048
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
284 static void
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
285 url_escape_string_part(char *outbuf, const char *inbuf) {
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
286 unsigned char c,c1,c2;
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
287 int i,len=strlen(inbuf);
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
288
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
289 for (i=0;i<len;i++) {
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
290 c = inbuf[i];
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
291 if ((c=='%') && i<len-2 ) { //need 2 more characters
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
292 c1=toupper(inbuf[i+1]); c2=toupper(inbuf[i+2]); // need uppercase chars
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
293 } else {
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
294 c1=129; c2=129; //not escape chars
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
295 }
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
296
3496
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
297 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
298 (c >= 'a' && c <= 'z') ||
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
299 (c >= '0' && c <= '9') ||
17048
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
300 (c >= 0x7f)) {
3496
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
301 *outbuf++ = c;
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
302 } else if ( c=='%' && ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) &&
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
303 ((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) {
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
304 // check if part of an escape sequence
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
305 *outbuf++=c; // already
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 25795
diff changeset
306
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
307 // dont escape again
16882
dfbe8cd0e081 libmpdemux translatables to help_mp part 1 / mp_msg calls / try 2
reynaldo
parents: 16421
diff changeset
308 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_URL_StringAlreadyEscaped,c,c1,c2);
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
309 // error as this should not happen against RFC 2396
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
310 // 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
311 } else {
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
312 /* all others will be escaped */
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
313 c1 = ((c & 0xf0) >> 4);
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
314 c2 = (c & 0x0f);
3496
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
315 if (c1 < 10) c1+='0';
9924
d66ed2c80ddf Fix incorrect URL encoding.
ranma
parents: 9690
diff changeset
316 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
317 if (c2 < 10) c2+='0';
9924
d66ed2c80ddf Fix incorrect URL encoding.
ranma
parents: 9690
diff changeset
318 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
319 *outbuf++ = '%';
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
320 *outbuf++ = c1;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
321 *outbuf++ = c2;
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
322 }
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
323 }
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12153
diff changeset
324 *outbuf++='\0';
3496
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
325 }
5c8a533dfa09 Added 2 functions to escape/unescape the url as described in the RFC 2396.
bertrand
parents: 3494
diff changeset
326
17048
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
327 /* 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
328 /* 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
329 void
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
330 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
331 unsigned char c;
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
332 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
333 char* tmp,*unesc = NULL, *in;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 25795
diff changeset
334
17048
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
335 // 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
336 // 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
337 tmp = strstr(inbuf,"://[");
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
338 if(tmp) {
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
339 tmp = strchr(tmp+4,']');
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
340 if(tmp && (tmp[1] == '/' || tmp[1] == ':' ||
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
341 tmp[1] == '\0')) {
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
342 i = tmp+1-inbuf;
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
343 strncpy(outbuf,inbuf,i);
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
344 outbuf += i;
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
345 tmp = NULL;
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
346 }
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
347 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 25795
diff changeset
348
25795
e96b4070ba66 Clear tmp between ip6 check and string escape to prevent reuse of the
rtogni
parents: 25620
diff changeset
349 tmp = NULL;
17048
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
350 while(i < len) {
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
351 // 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
352 for (j=i;j<len;j++) {
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
353 c = inbuf[j];
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
354 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
355 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
356 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
357 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
358 break;
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
359 }
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
360 // 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
361 if(j == i) {
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
362 *outbuf++ = c;
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
363 i++;
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
364 continue;
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
365 }
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
366 // 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
367 if(j < len) {
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
368 if(!tmp) tmp = malloc(len+1);
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
369 strncpy(tmp,inbuf+i,j-i);
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
370 tmp[j-i] = '\0';
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
371 in = tmp;
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
372 } 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
373 in = (char*)inbuf+i;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 25795
diff changeset
374
17048
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
375 if(!unesc) unesc = malloc(len+1);
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
376 // unescape first to avoid escaping escape
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
377 url_unescape_string(unesc,in);
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
378 // 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
379 // that can come from escape sequences
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
380 url_escape_string_part(outbuf,unesc);
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
381 outbuf += strlen(outbuf);
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
382 i += strlen(in);
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
383 }
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
384 *outbuf = '\0';
32511
b39155e98ac3 Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents: 30426
diff changeset
385 free(tmp);
b39155e98ac3 Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents: 30426
diff changeset
386 free(unesc);
17048
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
387 }
9e02b9cb171b Fix URL escaping to correctly handle URL containing an ip6 address or
albeu
parents: 16908
diff changeset
388
25620
53e5107ea80d Fix illegal identifiers, names starting with __ are reserved for the system.
diego
parents: 21126
diff changeset
389 #ifdef URL_DEBUG
4119
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
390 void
9690
a9b7b6055563 Added support for IPv6 numeric url like: http://[3ffe:400:100::1]:80/file
bertrand
parents: 7809
diff changeset
391 url_debug(const URL_t *url) {
4119
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
392 if( url==NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
393 printf("URL pointer NULL\n");
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
394 return;
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
395 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
396 if( url->url!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
397 printf("url=%s\n", url->url );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
398 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
399 if( url->protocol!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
400 printf("protocol=%s\n", url->protocol );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
401 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
402 if( url->hostname!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
403 printf("hostname=%s\n", url->hostname );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
404 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
405 printf("port=%d\n", url->port );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
406 if( url->file!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
407 printf("file=%s\n", url->file );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
408 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
409 if( url->username!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
410 printf("username=%s\n", url->username );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
411 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
412 if( url->password!=NULL ) {
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
413 printf("password=%s\n", url->password );
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
414 }
639b3b47b138 Added a debug function to print the struct's variables.
bertrand
parents: 3612
diff changeset
415 }
25620
53e5107ea80d Fix illegal identifiers, names starting with __ are reserved for the system.
diego
parents: 21126
diff changeset
416 #endif /* URL_DEBUG */