blob: 56d3b19899198f065eea3b37b0f562329a6dff60 [file] [log] [blame]
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/third_party/quiche/src/quic/core/http/spdy_utils.h"
#include <memory>
#include <string>
#include <vector>
#include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"
#include "net/third_party/quiche/src/spdy/core/spdy_protocol.h"
using spdy::SpdyHeaderBlock;
namespace quic {
// static
bool SpdyUtils::ExtractContentLengthFromHeaders(int64_t* content_length,
SpdyHeaderBlock* headers) {
auto it = headers->find("content-length");
if (it == headers->end()) {
return false;
} else {
// Check whether multiple values are consistent.
QuicStringPiece content_length_header = it->second;
std::vector<QuicStringPiece> values =
QuicTextUtils::Split(content_length_header, '\0');
for (const QuicStringPiece& value : values) {
uint64_t new_value;
if (!QuicTextUtils::StringToUint64(value, &new_value)) {
QUIC_DLOG(ERROR)
<< "Content length was either unparseable or negative.";
return false;
}
if (*content_length < 0) {
*content_length = new_value;
continue;
}
if (new_value != static_cast<uint64_t>(*content_length)) {
QUIC_DLOG(ERROR)
<< "Parsed content length " << new_value << " is "
<< "inconsistent with previously detected content length "
<< *content_length;
return false;
}
}
return true;
}
}
bool SpdyUtils::CopyAndValidateHeaders(const QuicHeaderList& header_list,
int64_t* content_length,
SpdyHeaderBlock* headers) {
for (const auto& p : header_list) {
const std::string& name = p.first;
if (name.empty()) {
QUIC_DLOG(ERROR) << "Header name must not be empty.";
return false;
}
if (QuicTextUtils::ContainsUpperCase(name)) {
QUIC_DLOG(ERROR) << "Malformed header: Header name " << name
<< " contains upper-case characters.";
return false;
}
headers->AppendValueOrAddHeader(name, p.second);
}
if (QuicContainsKey(*headers, "content-length") &&
!ExtractContentLengthFromHeaders(content_length, headers)) {
return false;
}
QUIC_DVLOG(1) << "Successfully parsed headers: " << headers->DebugString();
return true;
}
bool SpdyUtils::CopyAndValidateTrailers(const QuicHeaderList& header_list,
bool expect_final_byte_offset,
size_t* final_byte_offset,
SpdyHeaderBlock* trailers) {
bool found_final_byte_offset = false;
for (const auto& p : header_list) {
const std::string& name = p.first;
// Pull out the final offset pseudo header which indicates the number of
// response body bytes expected.
if (expect_final_byte_offset && !found_final_byte_offset &&
name == kFinalOffsetHeaderKey &&
QuicTextUtils::StringToSizeT(p.second, final_byte_offset)) {
found_final_byte_offset = true;
continue;
}
if (name.empty() || name[0] == ':') {
QUIC_DLOG(ERROR)
<< "Trailers must not be empty, and must not contain pseudo-"
<< "headers. Found: '" << name << "'";
return false;
}
if (QuicTextUtils::ContainsUpperCase(name)) {
QUIC_DLOG(ERROR) << "Malformed header: Header name " << name
<< " contains upper-case characters.";
return false;
}
trailers->AppendValueOrAddHeader(name, p.second);
}
if (expect_final_byte_offset && !found_final_byte_offset) {
QUIC_DLOG(ERROR) << "Required key '" << kFinalOffsetHeaderKey
<< "' not present";
return false;
}
// TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec.
QUIC_DVLOG(1) << "Successfully parsed Trailers: " << trailers->DebugString();
return true;
}
// static
// TODO(danzh): Move it to quic/tools/ and switch to use GURL.
bool SpdyUtils::PopulateHeaderBlockFromUrl(const std::string url,
SpdyHeaderBlock* headers) {
(*headers)[":method"] = "GET";
size_t pos = url.find("://");
if (pos == std::string::npos) {
return false;
}
(*headers)[":scheme"] = url.substr(0, pos);
size_t start = pos + 3;
pos = url.find("/", start);
if (pos == std::string::npos) {
(*headers)[":authority"] = url.substr(start);
(*headers)[":path"] = "/";
return true;
}
(*headers)[":authority"] = url.substr(start, pos - start);
(*headers)[":path"] = url.substr(pos);
return true;
}
#define RETURN_STRING_LITERAL(x) \
case x: \
return #x;
// static
std::string SpdyUtils::H3SettingsToString(
Http3AndQpackSettingsIdentifiers identifier) {
switch (identifier) {
RETURN_STRING_LITERAL(SETTINGS_QPACK_MAX_TABLE_CAPACITY);
RETURN_STRING_LITERAL(SETTINGS_MAX_HEADER_LIST_SIZE);
RETURN_STRING_LITERAL(SETTINGS_QPACK_BLOCKED_STREAMS);
}
return QuicStrCat("UNSUPPORTED_SETTINGS_TYPE(", identifier, ")");
}
#undef RETURN_STRING_LITERAL // undef for jumbo builds
} // namespace quic