1 /*
2  * lftp - file transfer program
3  *
4  * Copyright (c) 1996-2017 by Alexander V. Lukyanov (lav@yars.free.net)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef URL_H
21 #define URL_H
22 
23 #include "xstring.h"
24 
25 class ParsedURL
26 {
27 public:
28    xstring_c proto;
29    xstring_c user;
30    xstring_c pass;
31    xstring_c host;
32    xstring_c port;
33    xstring path;
34 
35    xstring_c orig_url;
36 
ParsedURL()37    ParsedURL() {}
38    ParsedURL(const char *url,bool proto_required=false,bool use_rfc1738=true);
39    void parse(const char *url,bool proto_required=false,bool use_rfc1738=true);
40    ~ParsedURL();
41 
42    xstring& CombineTo(xstring &buf,const char *home=0,bool use_rfc1738=true) const;
43    const char *CombineTo(xstring_c &buf,const char *home=0,bool use_rfc1738=true) const {
44       xstring tmp; tmp.move_here(buf);
45       return buf.move_here(CombineTo(tmp,home,use_rfc1738));
46    }
47    // returns allocated memory
48    char *Combine(const char *home=0,bool use_rfc1738=true);
49 };
50 
51 # define URL_UNSAFE " <>\"'%{}|\\^[]`"
52 # define URL_PATH_UNSAFE URL_UNSAFE"#;?&+"
53 # define URL_HOST_UNSAFE URL_UNSAFE":/"
54 # define URL_PORT_UNSAFE URL_UNSAFE"/"
55 # define URL_USER_UNSAFE URL_UNSAFE"/:@"
56 # define URL_PASS_UNSAFE URL_UNSAFE"/:@"
57 class url
58 {
59 public:
60    char	 *proto;
61    char  *user;
62    char	 *pass;
63    char  *host;
64    char	 *port;
65    char  *path;
66 
67    url(const char *u);
68    url();
69    ~url();
70 
71    void SetPath(const char *p,const char *q=URL_PATH_UNSAFE);
72    char *Combine();
73 
74    // encode unsafe chars as %XY
75    static const xstring& encode(const char *s,int len,const char *unsafe,unsigned flags=0);
76    static const xstring& encode(const xstring &s,const char *unsafe,unsigned flags=0) { return encode(s,s.length(),unsafe,flags); }
77    static const xstring& encode(const char *s,const char *unsafe,unsigned flags=0) { return encode(s,strlen(s),unsafe,flags); }
78    static const xstring& decode(const char *) __attribute__((warn_unused_result));
79 
80    static bool is_url(const char *p);
81    static int path_index(const char *p);
82    static const char *path_ptr(const char *p);
83    static bool dir_needs_trailing_slash(const char *proto);
84    static bool find_password_pos(const char *url,int *start,int *len);
85    static const char *remove_password(const char *url);
86    static const char *hide_password(const char *url);
87 };
88 
89 #endif//URL_H
90