Commit 8ec657ab authored by Kenton Varda's avatar Kenton Varda

Add convenience versions of HttpHeaders::set() and add() that take ownership.

parent 2c15b603
......@@ -328,6 +328,11 @@ void HttpHeaders::set(HttpHeaderId id, kj::StringPtr value) {
indexedHeaders[id.id] = value;
}
void HttpHeaders::set(HttpHeaderId id, kj::String&& value) {
set(id, kj::StringPtr(value));
takeOwnership(kj::mv(value));
}
void HttpHeaders::add(kj::StringPtr name, kj::StringPtr value) {
requireValidHeaderName(name);
requireValidHeaderValue(value);
......@@ -336,6 +341,17 @@ void HttpHeaders::add(kj::StringPtr name, kj::StringPtr value) {
"can't set connection-level headers on HttpHeaders", name, value) { break; }
}
void HttpHeaders::add(kj::StringPtr name, kj::String&& value) {
add(name, kj::StringPtr(value));
takeOwnership(kj::mv(value));
}
void HttpHeaders::add(kj::String&& name, kj::String&& value) {
add(kj::StringPtr(name), kj::StringPtr(value));
takeOwnership(kj::mv(name));
takeOwnership(kj::mv(value));
}
kj::Maybe<uint> HttpHeaders::addNoCheck(kj::StringPtr name, kj::StringPtr value) {
KJ_IF_MAYBE(id, table->stringToId(name)) {
if (id->id > CONNECTION_HEADER_THRESHOLD) {
......
......@@ -254,16 +254,23 @@ public:
// to IDs in the header table. Both inputs are of type kj::StringPtr.
void set(HttpHeaderId id, kj::StringPtr value);
void set(HttpHeaderId id, kj::String&& value);
// Sets a header value, overwriting the existing value.
//
// The String&& version is equivalent to calling the other version followed by takeOwnership().
//
// WARNING: It is the caller's responsibility to ensure that `value` remains valid until the
// HttpHeaders object is destroyed. This allows string literals to be passed without making a
// copy, but complicates the use of dynamic values. Hint: Consider using `takeOwnership()`.
void add(kj::StringPtr name, kj::StringPtr value);
void add(kj::StringPtr name, kj::String&& value);
void add(kj::String&& name, kj::String&& value);
// Append a header. `name` will be looked up in the header table, but if it's not mapped, the
// header will be added to the list of unmapped headers.
//
// The String&& versions are equivalent to calling the other version followed by takeOwnership().
//
// WARNING: It is the caller's responsibility to ensure that `name` and `value` remain valid
// until the HttpHeaders object is destroyed. This allows string literals to be passed without
// making a copy, but complicates the use of dynamic values. Hint: Consider using
......@@ -280,8 +287,6 @@ public:
void takeOwnership(HttpHeaders&& otherHeaders);
// Takes overship of a string so that it lives until the HttpHeaders object is destroyed. Useful
// when you've passed a dynamic value to set() or add() or parse*().
//
// TODO(soon): Is takeOwnership() actually needed?
struct ConnectionHeaders {
// These headers govern details of the specific HTTP connection or framing of the content.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment