OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/spdy/http2_compressor.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace net { |
| 10 |
| 11 Http2Encoder::Http2Encoder() {} |
| 12 |
| 13 void Http2Encoder::EncodeOctet(uint8 octet) { |
| 14 buffer_.append(1, octet); |
| 15 } |
| 16 |
| 17 void Http2Encoder::EncodeInteger(uint8 op_code, uint8 N, uint32 I) { |
| 18 DCHECK_LT(N, 8u); |
| 19 DCHECK_LT(static_cast<uint16>(op_code), 1u << (8 - N)); |
| 20 |
| 21 uint8 next_marker = (1 << N) - 1; |
| 22 |
| 23 if (I < next_marker) { |
| 24 EncodeOctet((op_code << N) | I); |
| 25 return; |
| 26 } |
| 27 |
| 28 if (N > 0) { |
| 29 EncodeOctet((op_code << N) | next_marker); |
| 30 } |
| 31 |
| 32 I -= next_marker; |
| 33 while (I >= 128) { |
| 34 EncodeOctet(I % 128 | 128); |
| 35 I /= 128; |
| 36 } |
| 37 EncodeOctet(I); |
| 38 } |
| 39 |
| 40 void Http2Encoder::EncodeOctetSequence(base::StringPiece str) { |
| 41 uint32 size_to_encode = static_cast<uint32>(str.size()); |
| 42 DCHECK_EQ(static_cast<size_t>(size_to_encode), str.size()); |
| 43 EncodeInteger(0, 0, size_to_encode); |
| 44 buffer_.append(str.data(), str.size()); |
| 45 } |
| 46 |
| 47 void Http2Encoder::EncodeIndexedHeader(uint32 index) { |
| 48 EncodeInteger(kIndexOpcode, kIndexN, index); |
| 49 } |
| 50 |
| 51 void Http2Encoder::EncodeLiteralHeaderNoIndexingWithIndex( |
| 52 uint32 index, |
| 53 base::StringPiece value) { |
| 54 EncodeInteger(kLiteralNoIndexOpcode, kLiteralNoIndexN, index + 1); |
| 55 EncodeOctetSequence(value); |
| 56 } |
| 57 |
| 58 void Http2Encoder::EncodeLiteralHeaderNoIndexingWithName( |
| 59 base::StringPiece name, |
| 60 base::StringPiece value) { |
| 61 EncodeInteger(kLiteralNoIndexOpcode, kLiteralNoIndexN, 0); |
| 62 EncodeOctetSequence(name); |
| 63 EncodeOctetSequence(value); |
| 64 } |
| 65 |
| 66 void Http2Encoder::EncodeLiteralHeaderWithIncrementalIndexingWithIndex( |
| 67 uint32 index, |
| 68 base::StringPiece value) { |
| 69 EncodeInteger(kLiteralIncrementalOpcode, kLiteralIncrementalN, index + 1); |
| 70 EncodeOctetSequence(value); |
| 71 } |
| 72 |
| 73 void Http2Encoder::EncodeLiteralHeaderWithIncrementalIndexingWithName( |
| 74 base::StringPiece name, |
| 75 base::StringPiece value) { |
| 76 EncodeInteger(kLiteralIncrementalOpcode, kLiteralIncrementalN, 0); |
| 77 EncodeOctetSequence(name); |
| 78 EncodeOctetSequence(value); |
| 79 } |
| 80 |
| 81 void Http2Encoder::EncodeLiteralHeaderWithSubstitutionIndexingWithIndex( |
| 82 uint32 index, |
| 83 base::StringPiece value) { |
| 84 EncodeInteger(kLiteralSubstitutionOpcode, kLiteralSubstitutionN, index + 1); |
| 85 EncodeOctetSequence(value); |
| 86 } |
| 87 |
| 88 void Http2Encoder::EncodeLiteralHeaderWithSubstitutionIndexingWithName( |
| 89 base::StringPiece name, |
| 90 base::StringPiece value) { |
| 91 EncodeInteger(kLiteralSubstitutionOpcode, kLiteralSubstitutionN, 0); |
| 92 EncodeOctetSequence(name); |
| 93 EncodeOctetSequence(value); |
| 94 } |
| 95 |
| 96 std::string Http2Encoder::Flush() { |
| 97 std::string out; |
| 98 out.swap(buffer_); |
| 99 return out; |
| 100 } |
| 101 |
| 102 Http2Compressor::Http2Compressor() : encoding_context_(HTTP2_REQUEST) {} |
| 103 |
| 104 Http2Compressor::~Http2Compressor() {} |
| 105 |
| 106 std::string Http2Compressor::EncodeNameValueBlock( |
| 107 const SpdyNameValueBlock& name_value_block) { |
| 108 Http2Encoder encoder; |
| 109 for (SpdyNameValueBlock::const_iterator it = name_value_block.begin(); |
| 110 it != name_value_block.end(); ++it) { |
| 111 encoder.EncodeLiteralHeaderNoIndexingWithName(it->first, it->second); |
| 112 } |
| 113 |
| 114 // TODO(akalin): Process unemitted entries in reference set. |
| 115 |
| 116 return encoder.Flush(); |
| 117 } |
| 118 |
| 119 } // namespace net |
OLD | NEW |