• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_NODE_URL_H_
2 #define SRC_NODE_URL_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "node.h"
7 
8 #include <string>
9 
10 namespace node {
11 namespace url {
12 
13 #define PARSESTATES(XX)                                                       \
14   XX(kSchemeStart)                                                            \
15   XX(kScheme)                                                                 \
16   XX(kNoScheme)                                                               \
17   XX(kSpecialRelativeOrAuthority)                                             \
18   XX(kPathOrAuthority)                                                        \
19   XX(kRelative)                                                               \
20   XX(kRelativeSlash)                                                          \
21   XX(kSpecialAuthoritySlashes)                                                \
22   XX(kSpecialAuthorityIgnoreSlashes)                                          \
23   XX(kAuthority)                                                              \
24   XX(kHost)                                                                   \
25   XX(kHostname)                                                               \
26   XX(kPort)                                                                   \
27   XX(kFile)                                                                   \
28   XX(kFileSlash)                                                              \
29   XX(kFileHost)                                                               \
30   XX(kPathStart)                                                              \
31   XX(kPath)                                                                   \
32   XX(kCannotBeBase)                                                           \
33   XX(kQuery)                                                                  \
34   XX(kFragment)
35 
36 #define FLAGS(XX)                                                             \
37   XX(URL_FLAGS_NONE, 0)                                                       \
38   XX(URL_FLAGS_FAILED, 0x01)                                                  \
39   XX(URL_FLAGS_CANNOT_BE_BASE, 0x02)                                          \
40   XX(URL_FLAGS_INVALID_PARSE_STATE, 0x04)                                     \
41   XX(URL_FLAGS_TERMINATED, 0x08)                                              \
42   XX(URL_FLAGS_SPECIAL, 0x10)                                                 \
43   XX(URL_FLAGS_HAS_USERNAME, 0x20)                                            \
44   XX(URL_FLAGS_HAS_PASSWORD, 0x40)                                            \
45   XX(URL_FLAGS_HAS_HOST, 0x80)                                                \
46   XX(URL_FLAGS_HAS_PATH, 0x100)                                               \
47   XX(URL_FLAGS_HAS_QUERY, 0x200)                                              \
48   XX(URL_FLAGS_HAS_FRAGMENT, 0x400)                                           \
49   XX(URL_FLAGS_IS_DEFAULT_SCHEME_PORT, 0x800)                                 \
50 
51 enum url_parse_state {
52   kUnknownState = -1,
53 #define XX(name) name,
54   PARSESTATES(XX)
55 #undef XX
56 };
57 
58 enum url_flags {
59 #define XX(name, val) name = val,
60   FLAGS(XX)
61 #undef XX
62 };
63 
64 struct url_data {
65   int32_t flags = URL_FLAGS_NONE;
66   int port = -1;
67   std::string scheme;
68   std::string username;
69   std::string password;
70   std::string host;
71   std::string query;
72   std::string fragment;
73   std::vector<std::string> path;
74 };
75 
76 class URL {
77  public:
78   static void Parse(const char* input,
79                     size_t len,
80                     enum url_parse_state state_override,
81                     struct url_data* url,
82                     bool has_url,
83                     const struct url_data* base,
84                     bool has_base);
85 
URL(const char * input,const size_t len)86   URL(const char* input, const size_t len) {
87     Parse(input, len, kUnknownState, &context_, false, nullptr, false);
88   }
89 
URL(const char * input,const size_t len,const URL * base)90   URL(const char* input, const size_t len, const URL* base) {
91     if (base != nullptr)
92       Parse(input, len, kUnknownState,
93             &context_, false,
94             &(base->context_), true);
95     else
96       Parse(input, len, kUnknownState, &context_, false, nullptr, false);
97   }
98 
URL(const char * input,const size_t len,const char * base,const size_t baselen)99   URL(const char* input, const size_t len,
100       const char* base, const size_t baselen) {
101     if (base != nullptr && baselen > 0) {
102       URL _base(base, baselen);
103       Parse(input, len, kUnknownState,
104             &context_, false,
105             &(_base.context_), true);
106     } else {
107       Parse(input, len, kUnknownState, &context_, false, nullptr, false);
108     }
109   }
110 
URL(const std::string & input)111   explicit URL(const std::string& input) :
112       URL(input.c_str(), input.length()) {}
113 
URL(const std::string & input,const URL * base)114   URL(const std::string& input, const URL* base) :
115       URL(input.c_str(), input.length(), base) {}
116 
URL(const std::string & input,const URL & base)117   URL(const std::string& input, const URL& base) :
118       URL(input.c_str(), input.length(), &base) {}
119 
URL(const std::string & input,const std::string & base)120   URL(const std::string& input, const std::string& base) :
121       URL(input.c_str(), input.length(), base.c_str(), base.length()) {}
122 
flags()123   int32_t flags() const {
124     return context_.flags;
125   }
126 
port()127   int port() const {
128     return context_.port;
129   }
130 
protocol()131   const std::string& protocol() const {
132     return context_.scheme;
133   }
134 
username()135   const std::string& username() const {
136     return context_.username;
137   }
138 
password()139   const std::string& password() const {
140     return context_.password;
141   }
142 
host()143   const std::string& host() const {
144     return context_.host;
145   }
146 
query()147   const std::string& query() const {
148     return context_.query;
149   }
150 
fragment()151   const std::string& fragment() const {
152     return context_.fragment;
153   }
154 
path()155   std::string path() const {
156     std::string ret;
157     for (const std::string& element : context_.path) {
158       ret += '/' + element;
159     }
160     return ret;
161   }
162 
163   // Get the path of the file: URL in a format consumable by native file system
164   // APIs. Returns an empty string if something went wrong.
165   std::string ToFilePath() const;
166   // Get the file URL from native file system path.
167   static URL FromFilePath(const std::string& file_path);
168 
169   v8::MaybeLocal<v8::Value> ToObject(Environment* env) const;
170 
171   URL(const URL&) = default;
172   URL& operator=(const URL&) = default;
173   URL(URL&&) = default;
174   URL& operator=(URL&&) = default;
175 
URL()176   URL() : URL("") {}
177 
178  private:
179   struct url_data context_;
180 };
181 
182 }  // namespace url
183 
184 }  // namespace node
185 
186 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
187 
188 #endif  // SRC_NODE_URL_H_
189