Skip to content

Commit 30c1925

Browse files
hanidamlajmeta-codesync[bot]
authored andcommitted
HTTPHeaders replace folly::StringPiece w/ std::string_view
Summary: as title indicates, this diff migrates all folly::StringPiece decls in HTTPHeaders w/ std::string_view Reviewed By: sharmafb Differential Revision: D95121210 fbshipit-source-id: a88c48815bc99a07ff283fd7de5f710031305fba
1 parent f48cab3 commit 30c1925

3 files changed

Lines changed: 34 additions & 46 deletions

File tree

proxygen/lib/http/HTTPHeaders.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ HTTPHeaders::HTTPHeaders() : deletedCount_(0) {
8686
resize(kInitialVectorReserve);
8787
}
8888

89-
void HTTPHeaders::add(folly::StringPiece name, std::string&& value) {
89+
void HTTPHeaders::add(std::string_view name, std::string&& value) {
9090
assert(name.size());
9191
const HTTPHeaderCode code = HTTPCommonHeaders::hash(name.data(), name.size());
9292
auto namePtr =
@@ -108,7 +108,7 @@ void HTTPHeaders::add(HTTPHeaders::headers_initializer_list l) {
108108
}
109109
}
110110

111-
bool HTTPHeaders::exists(folly::StringPiece name) const {
111+
bool HTTPHeaders::exists(std::string_view name) const {
112112
const HTTPHeaderCode code = HTTPCommonHeaders::hash(name.data(), name.size());
113113
if (code != HTTP_HEADER_OTHER) {
114114
return exists(code);
@@ -130,16 +130,16 @@ size_t HTTPHeaders::getNumberOfValues(HTTPHeaderCode code) const {
130130
return count;
131131
}
132132

133-
size_t HTTPHeaders::getNumberOfValues(folly::StringPiece name) const {
133+
size_t HTTPHeaders::getNumberOfValues(std::string_view name) const {
134134
size_t count = 0;
135-
forEachValueOfHeader(name, [&](folly::StringPiece /*value*/) -> bool {
135+
forEachValueOfHeader(name, [&](std::string_view) -> bool {
136136
++count;
137137
return false;
138138
});
139139
return count;
140140
}
141141

142-
bool HTTPHeaders::remove(folly::StringPiece name) {
142+
bool HTTPHeaders::remove(std::string_view name) {
143143
const HTTPHeaderCode code = HTTPCommonHeaders::hash(name.data(), name.size());
144144
if (code != HTTP_HEADER_OTHER) {
145145
return remove(code);
@@ -165,7 +165,7 @@ bool HTTPHeaders::remove(HTTPHeaderCode code) {
165165
}
166166

167167
bool HTTPHeaders::removeAllVersions(HTTPHeaderCode code,
168-
folly::StringPiece name) {
168+
std::string_view name) {
169169
bool removed = false;
170170
if (code != HTTP_HEADER_OTHER) {
171171
removed = remove(code);
@@ -261,7 +261,7 @@ size_t HTTPHeaders::size() const {
261261
return length_ - deletedCount_;
262262
}
263263

264-
bool HTTPHeaders::transferHeaderIfPresent(folly::StringPiece name,
264+
bool HTTPHeaders::transferHeaderIfPresent(std::string_view name,
265265
HTTPHeaders& strippedHeaders) {
266266
bool transferred = false;
267267
const HTTPHeaderCode code = HTTPCommonHeaders::hash(name.data(), name.size());
@@ -388,7 +388,7 @@ HTTPHeaders::SingleOrNullptrResult HTTPHeaders::getSingleOrNullptr(
388388
}
389389

390390
HTTPHeaders::SingleOrNullptrResult HTTPHeaders::getSingleOrNullptr(
391-
folly::StringPiece name) const noexcept {
391+
std::string_view name) const noexcept {
392392
SingleOrNullptrResult res;
393393
forEachValueOfHeader(name, [&](const std::string& value) -> bool {
394394
res.value = (res.value == nullptr) ? &value : nullptr;
@@ -401,8 +401,7 @@ HTTPHeaders::SingleOrNullptrResult HTTPHeaders::getSingleOrNullptr(
401401
const std::string& HTTPHeaders::getSingleOrEmpty(HTTPHeaderCode code) const {
402402
return *getSingleOrNullptr(code);
403403
}
404-
const std::string& HTTPHeaders::getSingleOrEmpty(
405-
folly::StringPiece name) const {
404+
const std::string& HTTPHeaders::getSingleOrEmpty(std::string_view name) const {
406405
return *getSingleOrNullptr(name);
407406
}
408407

@@ -507,7 +506,7 @@ void HTTPHeaders::forEachWithCode(const ForEachWithCodeFnT& func) const {
507506
}
508507

509508
bool HTTPHeaders::forEachValueOfHeader(
510-
folly::StringPiece name, const ForEachValueOfHeaderFnT& func) const {
509+
std::string_view name, const ForEachValueOfHeaderFnT& func) const {
511510
const HTTPHeaderCode code = HTTPCommonHeaders::hash(name.data(), name.size());
512511
if (code != HTTPHeaderCode::HTTP_HEADER_OTHER) {
513512
return forEachValueOfHeader(code, func);

proxygen/lib/http/HTTPHeaders.h

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class HTTPHeaders {
7272
struct HTTPHeaderName {
7373
enum Type { CODE, STRING };
7474
union {
75-
folly::StringPiece name_;
75+
std::string_view name_;
7676
HTTPHeaderCode code_;
7777
};
7878
Type type_;
@@ -82,13 +82,13 @@ class HTTPHeaders {
8282
/* implicit */ HTTPHeaderName(const char* name)
8383
: name_(name), type_(STRING) {
8484
}
85-
/* implicit */ HTTPHeaderName(folly::StringPiece name)
85+
/* implicit */ HTTPHeaderName(std::string_view name)
8686
: name_(name), type_(STRING) {
8787
}
8888
};
8989

9090
using headers_initializer_list =
91-
std::initializer_list<std::pair<HTTPHeaderName, folly::StringPiece>>;
91+
std::initializer_list<std::pair<HTTPHeaderName, std::string_view>>;
9292

9393
/*
9494
* separator used to concatenate multiple values of the same header
@@ -107,28 +107,20 @@ class HTTPHeaders {
107107
* Add the header 'name' with value 'value'; if other instances of this
108108
* header name exist, they will be retained.
109109
*/
110-
void add(folly::StringPiece name, std::string&& value);
111-
void add(folly::StringPiece name, const std::string& value) {
110+
void add(std::string_view name, std::string&& value);
111+
void add(std::string_view name, std::string_view value) {
112112
add(name, std::string(value));
113113
}
114-
void add(folly::StringPiece name, folly::StringPiece value) {
115-
add(name, std::string(value));
116-
}
117-
// TODO(@damlaj): remove this fn & patch up all callsites
118-
void add(folly::StringPiece name, const char* value) {
119-
return add(name, std::string(value));
114+
FOLLY_ALWAYS_INLINE void add(std::string_view name, const char* value) {
115+
return add(name, std::string_view{value});
120116
}
121117

122118
void add(HTTPHeaderCode code, std::string&& value);
123-
void add(HTTPHeaderCode code, const std::string& value) {
119+
void add(HTTPHeaderCode code, std::string_view value) {
124120
add(code, std::string(value));
125121
}
126-
void add(HTTPHeaderCode code, folly::StringPiece value) {
127-
add(code, std::string(value));
128-
}
129-
// TODO(@damlaj): remove this fn & patch up all callsites
130-
void add(HTTPHeaderCode code, const char* value) {
131-
return add(code, std::string(value));
122+
FOLLY_ALWAYS_INLINE void add(HTTPHeaderCode code, const char* value) {
123+
return add(code, std::string_view{value});
132124
}
133125

134126
void add(headers_initializer_list l);
@@ -137,12 +129,12 @@ class HTTPHeaders {
137129
* For the header 'name', set its value to the single header 'value',
138130
* removing any other instances of this header.
139131
*/
140-
void set(folly::StringPiece name, const std::string& value) {
132+
void set(std::string_view name, std::string_view value) {
141133
// this could be somewhat optimized but probably not an issue yet
142134
remove(name);
143135
add(name, value);
144136
}
145-
void set(HTTPHeaderCode code, const std::string& value) {
137+
void set(HTTPHeaderCode code, std::string_view value) {
146138
remove(code);
147139
add(code, value);
148140
}
@@ -153,21 +145,18 @@ class HTTPHeaders {
153145
* argument it will remove x-y_z, x_y-z and x_y_z too and then set the given
154146
* header name, value.
155147
*/
156-
void setOneVersion(folly::StringPiece name,
148+
void setOneVersion(std::string_view name,
157149
HTTPHeaderCode code,
158-
const std::string& value) {
150+
std::string_view value) {
159151
removeAllVersions(code, name);
160152
add(name, value);
161153
}
162154

163155
/**
164156
* Do we have an instance of the given header?
165157
*/
166-
[[nodiscard]] bool exists(folly::StringPiece name) const;
158+
[[nodiscard]] bool exists(std::string_view name) const;
167159
[[nodiscard]] bool exists(HTTPHeaderCode code) const;
168-
bool rawExists(std::string& name) const {
169-
return exists(name);
170-
}
171160

172161
/**
173162
* combine all the value for this header into a string
@@ -253,7 +242,7 @@ class HTTPHeaders {
253242
[[nodiscard]] SingleOrNullptrResult getSingleOrNullptr(
254243
HTTPHeaderCode code) const noexcept;
255244
[[nodiscard]] SingleOrNullptrResult getSingleOrNullptr(
256-
folly::StringPiece name) const noexcept;
245+
std::string_view name) const noexcept;
257246

258247
/**
259248
* Returns the value of the header if it's found in the message and is the
@@ -262,7 +251,7 @@ class HTTPHeaders {
262251
*/
263252
[[nodiscard]] const std::string& getSingleOrEmpty(HTTPHeaderCode code) const;
264253
[[nodiscard]] const std::string& getSingleOrEmpty(
265-
folly::StringPiece name) const;
254+
std::string_view name) const;
266255
[[nodiscard]] const std::string rawGet(const std::string& header) const {
267256
return getSingleOrEmpty(header);
268257
}
@@ -271,7 +260,7 @@ class HTTPHeaders {
271260
* Get the number of values corresponding to a given header name.
272261
*/
273262
[[nodiscard]] size_t getNumberOfValues(HTTPHeaderCode code) const;
274-
[[nodiscard]] size_t getNumberOfValues(folly::StringPiece name) const;
263+
[[nodiscard]] size_t getNumberOfValues(std::string_view name) const;
275264

276265
/**
277266
* Process the ordered list of values for the given header name:
@@ -286,7 +275,7 @@ class HTTPHeaders {
286275
* true), and false otherwise.
287276
*/
288277
using ForEachValueOfHeaderFnT = std::function<bool(const std::string&)>;
289-
bool forEachValueOfHeader(folly::StringPiece name,
278+
bool forEachValueOfHeader(std::string_view name,
290279
const ForEachValueOfHeaderFnT& func) const;
291280
bool forEachValueOfHeader(HTTPHeaderCode code,
292281
const ForEachValueOfHeaderFnT& func) const;
@@ -295,7 +284,7 @@ class HTTPHeaders {
295284
* Remove all instances of the given header, returning true if anything was
296285
* removed and false if this header didn't exist in our set.
297286
*/
298-
bool remove(folly::StringPiece name);
287+
bool remove(std::string_view name);
299288
bool remove(HTTPHeaderCode code);
300289
void rawRemove(const std::string& name) {
301290
remove(name);
@@ -305,7 +294,7 @@ class HTTPHeaders {
305294
* Remove all possible versions of header eg. if x-y-z is the
306295
* argument it will remove x-y_z, x_y-z and x_y_z too.
307296
*/
308-
bool removeAllVersions(HTTPHeaderCode code, folly::StringPiece name);
297+
bool removeAllVersions(HTTPHeaderCode code, std::string_view name);
309298

310299
/**
311300
* Remove all headers.
@@ -365,7 +354,7 @@ class HTTPHeaders {
365354
* group. No-op if the header doesn't exist. Returns true if header(s) were
366355
* moved.
367356
*/
368-
bool transferHeaderIfPresent(folly::StringPiece name, HTTPHeaders& dest);
357+
bool transferHeaderIfPresent(std::string_view name, HTTPHeaders& dest);
369358

370359
// deletes the strings in headerNames_ that we own
371360
void disposeOfHeaderNames();

proxygen/lib/http/test/HTTPMessageTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,12 +560,12 @@ TEST(HTTPHeaders, InitializerList) {
560560
EXPECT_EQ("x", hdrs.getSingleOrEmpty(HTTP_HEADER_SERVER));
561561
}
562562

563-
TEST(HTTPHeaders, InitializerListStringPiece) {
563+
TEST(HTTPHeaders, InitializerListStringView) {
564564
HTTPHeaders hdrs;
565565

566566
const char* foo = "name:value";
567567
folly::StringPiece str(foo);
568-
folly::StringPiece name = str.split_step(':');
568+
std::string_view name = str.split_step(':');
569569
hdrs.add({{name, str}, {HTTP_HEADER_CONNECTION, str}});
570570

571571
EXPECT_EQ("value", hdrs.getSingleOrEmpty("name"));

0 commit comments

Comments
 (0)