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/quic/quic_spdy_compressor.h" 6 7 #include "base/basictypes.h" 8 #include "base/memory/scoped_ptr.h" 9 10 using std::string; 11 12 namespace net { 13 14 QuicSpdyCompressor::QuicSpdyCompressor() 15 : spdy_framer_(SPDY3), 16 header_sequence_id_(1) { 17 spdy_framer_.set_enable_compression(true); 18 } 19 20 QuicSpdyCompressor::~QuicSpdyCompressor() { 21 } 22 23 string QuicSpdyCompressor::CompressHeaders( 24 const SpdyHeaderBlock& headers) { 25 // TODO(rch): Modify the SpdyFramer to expose a 26 // CreateCompressedHeaderBlock method, or some such. 27 SpdyStreamId stream_id = 3; // unused. 28 scoped_ptr<SpdyFrame> frame(spdy_framer_.CreateHeaders( 29 stream_id, CONTROL_FLAG_NONE, true, &headers)); 30 31 // The size of the spdy HEADER frame's fixed prefix which 32 // needs to be stripped off from the resulting frame. 33 const size_t header_frame_prefix_len = 12; 34 string serialized = string(frame->data() + header_frame_prefix_len, 35 frame->size() - header_frame_prefix_len); 36 uint32 serialized_len = serialized.length(); 37 char id_str[sizeof(header_sequence_id_)]; 38 memcpy(&id_str, &header_sequence_id_, sizeof(header_sequence_id_)); 39 char len_str[sizeof(serialized_len)]; 40 memcpy(&len_str, &serialized_len, sizeof(serialized_len)); 41 string compressed; 42 compressed.reserve(arraysize(id_str) + arraysize(len_str) + serialized_len); 43 compressed.append(id_str, arraysize(id_str)); 44 compressed.append(len_str, arraysize(len_str)); 45 compressed.append(serialized); 46 ++header_sequence_id_; 47 return compressed; 48 } 49 50 } // namespace net 51