OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/core/spdy_utils.h" | 5 #include "net/quic/core/spdy_utils.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "net/quic/platform/api/quic_logging.h" | 10 #include "net/quic/platform/api/quic_logging.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { | 25 string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { |
26 size_t length = SpdyFramer::GetSerializedLength(&headers); | 26 size_t length = SpdyFramer::GetSerializedLength(&headers); |
27 SpdyFrameBuilder builder(length); | 27 SpdyFrameBuilder builder(length); |
28 SpdyFramer framer(SpdyFramer::DISABLE_COMPRESSION); | 28 SpdyFramer framer(SpdyFramer::DISABLE_COMPRESSION); |
29 framer.SerializeHeaderBlockWithoutCompression(&builder, headers); | 29 framer.SerializeHeaderBlockWithoutCompression(&builder, headers); |
30 SpdySerializedFrame block(builder.take()); | 30 SpdySerializedFrame block(builder.take()); |
31 return string(block.data(), length); | 31 return string(block.data(), length); |
32 } | 32 } |
33 | 33 |
34 // static | 34 // static |
35 bool SpdyUtils::ParseHeaders(const char* data, | |
36 uint32_t data_len, | |
37 int64_t* content_length, | |
38 SpdyHeaderBlock* headers) { | |
39 SpdyFramer framer(SpdyFramer::ENABLE_COMPRESSION); | |
40 if (!framer.ParseHeaderBlockInBuffer(data, data_len, headers) || | |
41 headers->empty()) { | |
42 return false; // Headers were invalid. | |
43 } | |
44 | |
45 if (!QuicContainsKey(*headers, "content-length")) { | |
46 return true; | |
47 } | |
48 | |
49 return ExtractContentLengthFromHeaders(content_length, headers); | |
50 } | |
51 | |
52 // static | |
53 bool SpdyUtils::ExtractContentLengthFromHeaders(int64_t* content_length, | 35 bool SpdyUtils::ExtractContentLengthFromHeaders(int64_t* content_length, |
54 SpdyHeaderBlock* headers) { | 36 SpdyHeaderBlock* headers) { |
55 auto it = headers->find("content-length"); | 37 auto it = headers->find("content-length"); |
56 if (it == headers->end()) { | 38 if (it == headers->end()) { |
57 return false; | 39 return false; |
58 } else { | 40 } else { |
59 // Check whether multiple values are consistent. | 41 // Check whether multiple values are consistent. |
60 QuicStringPiece content_length_header = it->second; | 42 QuicStringPiece content_length_header = it->second; |
61 std::vector<QuicStringPiece> values = | 43 std::vector<QuicStringPiece> values = |
62 QuicTextUtils::Split(content_length_header, '\0'); | 44 QuicTextUtils::Split(content_length_header, '\0'); |
(...skipping 13 matching lines...) Expand all Loading... |
76 << "Parsed content length " << new_value << " is " | 58 << "Parsed content length " << new_value << " is " |
77 << "inconsistent with previously detected content length " | 59 << "inconsistent with previously detected content length " |
78 << *content_length; | 60 << *content_length; |
79 return false; | 61 return false; |
80 } | 62 } |
81 } | 63 } |
82 return true; | 64 return true; |
83 } | 65 } |
84 } | 66 } |
85 | 67 |
86 // static | |
87 bool SpdyUtils::ParseTrailers(const char* data, | |
88 uint32_t data_len, | |
89 size_t* final_byte_offset, | |
90 SpdyHeaderBlock* trailers) { | |
91 SpdyFramer framer(SpdyFramer::ENABLE_COMPRESSION); | |
92 if (!framer.ParseHeaderBlockInBuffer(data, data_len, trailers) || | |
93 trailers->empty()) { | |
94 QUIC_DVLOG(1) << "Request Trailers are invalid."; | |
95 return false; // Trailers were invalid. | |
96 } | |
97 | |
98 // Pull out the final offset pseudo header which indicates the number of | |
99 // response body bytes expected. | |
100 auto it = trailers->find(kFinalOffsetHeaderKey); | |
101 if (it == trailers->end() || | |
102 !QuicTextUtils::StringToSizeT(it->second, final_byte_offset)) { | |
103 QUIC_DLOG(ERROR) << "Required key '" << kFinalOffsetHeaderKey | |
104 << "' not present"; | |
105 return false; | |
106 } | |
107 // The final offset header is no longer needed. | |
108 trailers->erase(it->first); | |
109 | |
110 // Trailers must not have empty keys, and must not contain pseudo headers. | |
111 for (const auto& trailer : *trailers) { | |
112 QuicStringPiece key = trailer.first; | |
113 QuicStringPiece value = trailer.second; | |
114 if (QuicTextUtils::StartsWith(key, ":")) { | |
115 QUIC_DVLOG(1) << "Trailers must not contain pseudo-header: '" << key | |
116 << "','" << value << "'."; | |
117 return false; | |
118 } | |
119 | |
120 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec. | |
121 } | |
122 | |
123 QUIC_DVLOG(1) << "Successfully parsed Trailers: " << trailers->DebugString(); | |
124 return true; | |
125 } | |
126 | |
127 bool SpdyUtils::CopyAndValidateHeaders(const QuicHeaderList& header_list, | 68 bool SpdyUtils::CopyAndValidateHeaders(const QuicHeaderList& header_list, |
128 int64_t* content_length, | 69 int64_t* content_length, |
129 SpdyHeaderBlock* headers) { | 70 SpdyHeaderBlock* headers) { |
130 for (const auto& p : header_list) { | 71 for (const auto& p : header_list) { |
131 const string& name = p.first; | 72 const string& name = p.first; |
132 if (name.empty()) { | 73 if (name.empty()) { |
133 QUIC_DLOG(ERROR) << "Header name must not be empty."; | 74 QUIC_DLOG(ERROR) << "Header name must not be empty."; |
134 return false; | 75 return false; |
135 } | 76 } |
136 | 77 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 (*headers)[":authority"] = url.substr(start); | 194 (*headers)[":authority"] = url.substr(start); |
254 (*headers)[":path"] = "/"; | 195 (*headers)[":path"] = "/"; |
255 return true; | 196 return true; |
256 } | 197 } |
257 (*headers)[":authority"] = url.substr(start, pos - start); | 198 (*headers)[":authority"] = url.substr(start, pos - start); |
258 (*headers)[":path"] = url.substr(pos); | 199 (*headers)[":path"] = url.substr(pos); |
259 return true; | 200 return true; |
260 } | 201 } |
261 | 202 |
262 } // namespace net | 203 } // namespace net |
OLD | NEW |