GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_http_common-inl.h Lines: 63 82 76.8 %
Date: 2020-06-24 22:13:30 Branches: 15 26 57.7 %

Line Branch Exec Source
1
#ifndef SRC_NODE_HTTP_COMMON_INL_H_
2
#define SRC_NODE_HTTP_COMMON_INL_H_
3
4
#include "node_http_common.h"
5
#include "node.h"
6
#include "node_mem-inl.h"
7
#include "env-inl.h"
8
#include "v8.h"
9
10
#include <algorithm>
11
12
namespace node {
13
14
template <typename T>
15
23518
NgHeaders<T>::NgHeaders(Environment* env, v8::Local<v8::Array> headers) {
16
  v8::Local<v8::Value> header_string =
17
70554
      headers->Get(env->context(), 0).ToLocalChecked();
18
  v8::Local<v8::Value> header_count =
19
70554
      headers->Get(env->context(), 1).ToLocalChecked();
20
23518
  CHECK(header_count->IsUint32());
21
47036
  CHECK(header_string->IsString());
22
47036
  count_ = header_count.As<v8::Uint32>()->Value();
23
47036
  int header_string_len = header_string.As<v8::String>()->Length();
24
25
23518
  if (count_ == 0) {
26
24
    CHECK_EQ(header_string_len, 0);
27
49
    return;
28
  }
29
30
23494
  buf_.AllocateSufficientStorage((alignof(nv_t) - 1) +
31
23494
                                 count_ * sizeof(nv_t) +
32
                                 header_string_len);
33
34
23494
  char* start = AlignUp(buf_.out(), alignof(nv_t));
35
23494
  char* header_contents = start + (count_ * sizeof(nv_t));
36
23494
  nv_t* const nva = reinterpret_cast<nv_t*>(start);
37
38
23494
  CHECK_LE(header_contents + header_string_len, *buf_ + buf_.length());
39
70482
  CHECK_EQ(header_string.As<v8::String>()->WriteOneByte(
40
               env->isolate(),
41
               reinterpret_cast<uint8_t*>(header_contents),
42
               0,
43
               header_string_len,
44
               v8::String::NO_NULL_TERMINATION),
45
           header_string_len);
46
47
23494
  size_t n = 0;
48
  char* p;
49
95402
  for (p = header_contents; p < header_contents + header_string_len; n++) {
50
71909
    if (n >= count_) {
51
      static uint8_t zero = '\0';
52
1
      nva[0].name = nva[0].value = &zero;
53
1
      nva[0].namelen = nva[0].valuelen = 1;
54
1
      count_ = 1;
55
1
      return;
56
    }
57
58
71908
    nva[n].flags = T::kNoneFlag;
59
71908
    nva[n].name = reinterpret_cast<uint8_t*>(p);
60
71908
    nva[n].namelen = strlen(p);
61
71908
    p += nva[n].namelen + 1;
62
71908
    nva[n].value = reinterpret_cast<uint8_t*>(p);
63
71908
    nva[n].valuelen = strlen(p);
64
71908
    p += nva[n].valuelen + 1;
65
  }
66
}
67
68
299
size_t GetClientMaxHeaderPairs(size_t max_header_pairs) {
69
  static constexpr size_t min_header_pairs = 1;
70
299
  return std::max(max_header_pairs, min_header_pairs);
71
}
72
73
316
size_t GetServerMaxHeaderPairs(size_t max_header_pairs) {
74
  static constexpr size_t min_header_pairs = 4;
75
316
  return std::max(max_header_pairs, min_header_pairs);
76
}
77
78
template <typename allocator_t>
79
std::string NgHeaderBase<allocator_t>::ToString() const {
80
  std::string ret = name();
81
  ret += " = ";
82
  ret += value();
83
  return ret;
84
}
85
86
template <typename T>
87
bool NgHeader<T>::IsZeroLength(
88
    NgHeader<T>::rcbuf_t* name,
89
    NgHeader<T>::rcbuf_t* value) {
90
  return IsZeroLength(-1, name, value);
91
}
92
93
template <typename T>
94
bool NgHeader<T>::IsZeroLength(
95
    int32_t token,
96
    NgHeader<T>::rcbuf_t* name,
97
    NgHeader<T>::rcbuf_t* value) {
98
99
  if (NgHeader<T>::rcbufferpointer_t::IsZeroLength(value))
100
    return true;
101
102
  const char* header_name = T::ToHttpHeaderName(token);
103
  return header_name != nullptr ||
104
      NgHeader<T>::rcbufferpointer_t::IsZeroLength(name);
105
}
106
107
template <typename T>
108
71843
NgHeader<T>::NgHeader(
109
    Environment* env,
110
    NgHeader<T>::rcbuf_t* name,
111
    NgHeader<T>::rcbuf_t* value,
112
    uint8_t flags)
113
71843
    : NgHeader<T>(env, -1, name, value, flags) {}
114
115
template <typename T>
116
71843
NgHeader<T>::NgHeader(
117
    Environment* env,
118
    int32_t token,
119
    NgHeader<T>::rcbuf_t* name,
120
    NgHeader<T>::rcbuf_t* value,
121
71843
    uint8_t flags) : env_(env), token_(token), flags_(flags) {
122
71843
  if (token == -1) {
123
71843
    CHECK_NOT_NULL(name);
124
71843
    name_.reset(name, true);  // Internalizable
125
  }
126
71843
  CHECK_NOT_NULL(value);
127
71843
  name_.reset(name, true);  // Internalizable
128
71843
  value_.reset(value);
129
71843
}
130
131
template <typename T>
132
71865
NgHeader<T>::NgHeader(NgHeader<T>&& other) noexcept
133
71865
    : name_(std::move(other.name_)),
134
71865
      value_(std::move(other.value_)),
135
71865
      token_(other.token_),
136
287460
      flags_(other.flags_) {
137
71865
  other.token_ = -1;
138
71865
  other.flags_ = 0;
139
71865
  other.env_ = nullptr;
140
71865
}
141
142
template <typename T>
143
void NgHeader<T>::MemoryInfo(MemoryTracker* tracker) const {
144
  tracker->TrackField("name", name_);
145
  tracker->TrackField("value", value_);
146
}
147
148
template <typename T>
149
71732
v8::MaybeLocal<v8::String> NgHeader<T>::GetName(
150
    NgHeader<T>::allocator_t* allocator) const {
151
152
  // Not all instances will support using token id's for header names.
153
  // HTTP/2 specifically does not support it.
154
71732
  const char* header_name = T::ToHttpHeaderName(token_);
155
156
  // If header_name is not nullptr, then it is a known header with
157
  // a statically defined name. We can safely internalize it here.
158
71732
  if (header_name != nullptr) {
159
    auto& static_str_map = env_->isolate_data()->static_str_map;
160
    v8::Eternal<v8::String> eternal = static_str_map[header_name];
161
    if (eternal.IsEmpty()) {
162
      v8::Local<v8::String> str = OneByteString(env_->isolate(), header_name);
163
      eternal.Set(env_->isolate(), str);
164
      return str;
165
    }
166
    return eternal.Get(env_->isolate());
167
  }
168
71732
  return rcbufferpointer_t::External::New(allocator, name_);
169
}
170
171
template <typename T>
172
71732
v8::MaybeLocal<v8::String> NgHeader<T>::GetValue(
173
    NgHeader<T>::allocator_t* allocator) const {
174
71732
  return rcbufferpointer_t::External::New(allocator, value_);
175
}
176
177
template <typename T>
178
std::string NgHeader<T>::name() const {
179
  return name_.str();
180
}
181
182
template <typename T>
183
std::string NgHeader<T>::value() const {
184
  return value_.str();
185
}
186
187
template <typename T>
188
71843
size_t NgHeader<T>::length() const {
189
71843
  return name_.len() + value_.len();
190
}
191
192
}  // namespace node
193
194
#endif  // SRC_NODE_HTTP_COMMON_INL_H_