1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef nsURLParsers_h__
7 #define nsURLParsers_h__
8 
9 #include "nsIURLParser.h"
10 #include "mozilla/Attributes.h"
11 
12 //----------------------------------------------------------------------------
13 // base class for url parsers
14 //----------------------------------------------------------------------------
15 
16 class nsBaseURLParser : public nsIURLParser {
17  public:
18   NS_DECL_NSIURLPARSER
19 
nsBaseURLParser()20   nsBaseURLParser() {}
21 
22  protected:
23   // implemented by subclasses
24   virtual void ParseAfterScheme(const char *spec, int32_t specLen,
25                                 uint32_t *authPos, int32_t *authLen,
26                                 uint32_t *pathPos, int32_t *pathLen) = 0;
27 };
28 
29 //----------------------------------------------------------------------------
30 // an url parser for urls that do not have an authority section
31 //
32 // eg. file:foo/bar.txt
33 //     file:/foo/bar.txt      (treated equivalently)
34 //     file:///foo/bar.txt
35 //
36 // eg. file:////foo/bar.txt   (UNC-filepath = \\foo\bar.txt)
37 //
38 // XXX except in this case:
39 //     file://foo/bar.txt     (the authority "foo"  is ignored)
40 //----------------------------------------------------------------------------
41 
42 class nsNoAuthURLParser final : public nsBaseURLParser {
~nsNoAuthURLParser()43   ~nsNoAuthURLParser() {}
44 
45  public:
46   NS_DECL_THREADSAFE_ISUPPORTS
47 
48 #if defined(XP_WIN)
49   NS_IMETHOD ParseFilePath(const char *, int32_t, uint32_t *, int32_t *,
50                            uint32_t *, int32_t *, uint32_t *,
51                            int32_t *) override;
52 #endif
53 
54   NS_IMETHOD ParseAuthority(const char *auth, int32_t authLen,
55                             uint32_t *usernamePos, int32_t *usernameLen,
56                             uint32_t *passwordPos, int32_t *passwordLen,
57                             uint32_t *hostnamePos, int32_t *hostnameLen,
58                             int32_t *port) override;
59 
60   void ParseAfterScheme(const char *spec, int32_t specLen, uint32_t *authPos,
61                         int32_t *authLen, uint32_t *pathPos,
62                         int32_t *pathLen) override;
63 };
64 
65 //----------------------------------------------------------------------------
66 // an url parser for urls that must have an authority section
67 //
68 // eg. http:www.foo.com/bar.html
69 //     http:/www.foo.com/bar.html
70 //     http://www.foo.com/bar.html    (treated equivalently)
71 //     http:///www.foo.com/bar.html
72 //----------------------------------------------------------------------------
73 
74 class nsAuthURLParser : public nsBaseURLParser {
75  protected:
~nsAuthURLParser()76   virtual ~nsAuthURLParser() {}
77 
78  public:
79   NS_DECL_THREADSAFE_ISUPPORTS
80 
81   NS_IMETHOD ParseAuthority(const char *auth, int32_t authLen,
82                             uint32_t *usernamePos, int32_t *usernameLen,
83                             uint32_t *passwordPos, int32_t *passwordLen,
84                             uint32_t *hostnamePos, int32_t *hostnameLen,
85                             int32_t *port) override;
86 
87   NS_IMETHOD ParseUserInfo(const char *userinfo, int32_t userinfoLen,
88                            uint32_t *usernamePos, int32_t *usernameLen,
89                            uint32_t *passwordPos,
90                            int32_t *passwordLen) override;
91 
92   NS_IMETHOD ParseServerInfo(const char *serverinfo, int32_t serverinfoLen,
93                              uint32_t *hostnamePos, int32_t *hostnameLen,
94                              int32_t *port) override;
95 
96   void ParseAfterScheme(const char *spec, int32_t specLen, uint32_t *authPos,
97                         int32_t *authLen, uint32_t *pathPos,
98                         int32_t *pathLen) override;
99 };
100 
101 //----------------------------------------------------------------------------
102 // an url parser for urls that may or may not have an authority section
103 //
104 // eg. http:www.foo.com              (www.foo.com is authority)
105 //     http:www.foo.com/bar.html     (www.foo.com is authority)
106 //     http:/www.foo.com/bar.html    (www.foo.com is part of file path)
107 //     http://www.foo.com/bar.html   (www.foo.com is authority)
108 //     http:///www.foo.com/bar.html  (www.foo.com is part of file path)
109 //----------------------------------------------------------------------------
110 
111 class nsStdURLParser : public nsAuthURLParser {
~nsStdURLParser()112   virtual ~nsStdURLParser() {}
113 
114  public:
115   void ParseAfterScheme(const char *spec, int32_t specLen, uint32_t *authPos,
116                         int32_t *authLen, uint32_t *pathPos,
117                         int32_t *pathLen) override;
118 };
119 
120 #endif  // nsURLParsers_h__
121