GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_url.h Lines: 41 43 95.3 %
Date: 2021-09-30 04:12:30 Branches: 5 8 62.5 %

Line Branch Exec Source
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
  std::string href;
75
};
76
77
namespace table_data {
78
extern const char hex[1024];
79
extern const uint8_t C0_CONTROL_ENCODE_SET[32];
80
extern const uint8_t FRAGMENT_ENCODE_SET[32];
81
extern const uint8_t PATH_ENCODE_SET[32];
82
extern const uint8_t USERINFO_ENCODE_SET[32];
83
extern const uint8_t QUERY_ENCODE_SET_NONSPECIAL[32];
84
extern const uint8_t QUERY_ENCODE_SET_SPECIAL[32];
85
}
86
87
3
class URL {
88
 public:
89
  static void Parse(const char* input,
90
                    size_t len,
91
                    enum url_parse_state state_override,
92
                    struct url_data* url,
93
                    bool has_url,
94
                    const struct url_data* base,
95
                    bool has_base);
96
97
  static std::string SerializeURL(const struct url_data* url, bool exclude);
98
99
33824
  URL(const char* input, const size_t len) {
100
33824
    Parse(input, len, kUnknownState, &context_, false, nullptr, false);
101
33824
  }
102
103
1
  URL(const char* input, const size_t len, const URL* base) {
104
1
    if (base != nullptr)
105
1
      Parse(input, len, kUnknownState,
106
            &context_, false,
107
            &(base->context_), true);
108
    else
109
      Parse(input, len, kUnknownState, &context_, false, nullptr, false);
110
1
  }
111
112
6
  URL(const char* input, const size_t len,
113
6
      const char* base, const size_t baselen) {
114

6
    if (base != nullptr && baselen > 0) {
115
12
      URL _base(base, baselen);
116
6
      Parse(input, len, kUnknownState,
117
            &context_, false,
118
6
            &(_base.context_), true);
119
    } else {
120
      Parse(input, len, kUnknownState, &context_, false, nullptr, false);
121
    }
122
6
  }
123
124
33815
  explicit URL(const std::string& input) :
125
33815
      URL(input.c_str(), input.length()) {}
126
127
1
  URL(const std::string& input, const URL* base) :
128
1
      URL(input.c_str(), input.length(), base) {}
129
130
  URL(const std::string& input, const URL& base) :
131
      URL(input.c_str(), input.length(), &base) {}
132
133
1
  URL(const std::string& input, const std::string& base) :
134
1
      URL(input.c_str(), input.length(), base.c_str(), base.length()) {}
135
136
14
  int32_t flags() const {
137
14
    return context_.flags;
138
  }
139
140
2
  int port() const {
141
2
    return context_.port;
142
  }
143
144
14
  const std::string& protocol() const {
145
14
    return context_.scheme;
146
  }
147
148
  const std::string& username() const {
149
    return context_.username;
150
  }
151
152
  const std::string& password() const {
153
    return context_.password;
154
  }
155
156
11
  const std::string& host() const {
157
11
    return context_.host;
158
  }
159
160
2
  const std::string& query() const {
161
2
    return context_.query;
162
  }
163
164
2
  const std::string& fragment() const {
165
2
    return context_.fragment;
166
  }
167
168
12
  std::string path() const {
169
12
    std::string ret;
170
33
    for (const std::string& element : context_.path) {
171
21
      ret += '/' + element;
172
    }
173
12
    return ret;
174
  }
175
176
33802
  std::string href() const {
177
33802
    return SerializeURL(&context_, false);
178
  }
179
180
  // Get the path of the file: URL in a format consumable by native file system
181
  // APIs. Returns an empty string if something went wrong.
182
  std::string ToFilePath() const;
183
  // Get the file URL from native file system path.
184
  static URL FromFilePath(const std::string& file_path);
185
186
  v8::MaybeLocal<v8::Value> ToObject(Environment* env) const;
187
188
  URL(const URL&) = default;
189
  URL& operator=(const URL&) = default;
190
  URL(URL&&) = default;
191
  URL& operator=(URL&&) = default;
192
193
1
  URL() : URL("") {}
194
195
 private:
196
  struct url_data context_;
197
};
198
199
}  // namespace url
200
201
}  // namespace node
202
203
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
204
205
#endif  // SRC_NODE_URL_H_