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