GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_url.h Lines: 27 28 96.4 %
Date: 2019-09-17 22:33:17 Branches: 3 4 75.0 %

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