| 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/spdy_utils.h" | 5 #include "net/quic/spdy_utils.h" |
| 6 | 6 |
| 7 #include <vector> |
| 8 |
| 7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" |
| 8 #include "net/spdy/spdy_frame_builder.h" | 13 #include "net/spdy/spdy_frame_builder.h" |
| 9 #include "net/spdy/spdy_framer.h" | 14 #include "net/spdy/spdy_framer.h" |
| 10 #include "net/spdy/spdy_protocol.h" | 15 #include "net/spdy/spdy_protocol.h" |
| 11 | 16 |
| 12 using std::string; | 17 using std::string; |
| 18 using std::vector; |
| 13 | 19 |
| 14 namespace net { | 20 namespace net { |
| 15 | 21 |
| 16 // static | 22 // static |
| 17 string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { | 23 string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { |
| 18 SpdyMajorVersion spdy_version = HTTP2; | 24 SpdyMajorVersion spdy_version = HTTP2; |
| 19 | 25 |
| 20 size_t length = SpdyFramer::GetSerializedLength(spdy_version, &headers); | 26 size_t length = SpdyFramer::GetSerializedLength(spdy_version, &headers); |
| 21 SpdyFrameBuilder builder(length, spdy_version); | 27 SpdyFrameBuilder builder(length, spdy_version); |
| 22 SpdyFramer::WriteHeaderBlock(&builder, spdy_version, &headers); | 28 SpdyFramer::WriteHeaderBlock(&builder, spdy_version, &headers); |
| 23 scoped_ptr<SpdyFrame> block(builder.take()); | 29 scoped_ptr<SpdyFrame> block(builder.take()); |
| 24 return string(block->data(), length); | 30 return string(block->data(), length); |
| 25 } | 31 } |
| 26 | 32 |
| 33 // static |
| 34 bool SpdyUtils::ParseHeaders(const char* data, |
| 35 uint32 data_len, |
| 36 int* content_length, |
| 37 SpdyHeaderBlock* headers) { |
| 38 SpdyFramer framer(HTTP2); |
| 39 if (!framer.ParseHeaderBlockInBuffer(data, data_len, headers) || |
| 40 headers->empty()) { |
| 41 return false; // Headers were invalid. |
| 42 } |
| 43 |
| 44 if (ContainsKey(*headers, "content-length")) { |
| 45 // Check whether multiple values are consistent. |
| 46 base::StringPiece content_length_header = (*headers)["content-length"]; |
| 47 vector<string> values = |
| 48 base::SplitString(content_length_header, base::StringPiece("\0", 1), |
| 49 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 50 for (const string& value : values) { |
| 51 int new_value; |
| 52 if (!base::StringToInt(value, &new_value) || new_value < 0) { |
| 53 return false; |
| 54 } |
| 55 if (*content_length < 0) { |
| 56 *content_length = new_value; |
| 57 continue; |
| 58 } |
| 59 if (new_value != *content_length) { |
| 60 return false; |
| 61 } |
| 62 } |
| 63 } |
| 64 |
| 65 return true; |
| 66 } |
| 67 |
| 27 } // namespace net | 68 } // namespace net |
| OLD | NEW |