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/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::CompressHeadersWithPriority( | |
24 QuicPriority priority, | |
25 const SpdyHeaderBlock& headers) { | |
26 return CompressHeadersInternal(priority, headers, true); | |
27 } | |
28 | |
29 string QuicSpdyCompressor::CompressHeaders( | |
30 const SpdyHeaderBlock& headers) { | |
31 // CompressHeadersInternal ignores priority when write_priority is false. | |
32 return CompressHeadersInternal(0 /* ignored */, headers, false); | |
33 } | |
34 | |
35 string QuicSpdyCompressor::CompressHeadersInternal( | |
36 QuicPriority priority, | |
37 const SpdyHeaderBlock& headers, | |
38 bool write_priority) { | |
39 // TODO(rch): Modify the SpdyFramer to expose a | |
40 // CreateCompressedHeaderBlock method, or some such. | |
41 SpdyStreamId stream_id = 3; // unused. | |
42 | |
43 SpdyHeadersIR headers_ir(stream_id); | |
44 headers_ir.set_name_value_block(headers); | |
45 scoped_ptr<SpdyFrame> frame(spdy_framer_.SerializeHeaders(headers_ir)); | |
46 | |
47 // The size of the spdy HEADER frame's fixed prefix which | |
48 // needs to be stripped off from the resulting frame. | |
49 const size_t header_frame_prefix_len = 12; | |
50 string serialized = string(frame->data() + header_frame_prefix_len, | |
51 frame->size() - header_frame_prefix_len); | |
52 uint32 serialized_len = serialized.length(); | |
53 char priority_str[sizeof(priority)]; | |
54 memcpy(&priority_str, &priority, sizeof(priority)); | |
55 char id_str[sizeof(header_sequence_id_)]; | |
56 memcpy(&id_str, &header_sequence_id_, sizeof(header_sequence_id_)); | |
57 char len_str[sizeof(serialized_len)]; | |
58 memcpy(&len_str, &serialized_len, sizeof(serialized_len)); | |
59 string compressed; | |
60 int priority_len = write_priority ? arraysize(priority_str) : 0; | |
61 compressed.reserve( | |
62 priority_len + arraysize(id_str) + arraysize(len_str) + serialized_len); | |
63 if (write_priority) { | |
64 compressed.append(priority_str, arraysize(priority_str)); | |
65 } | |
66 compressed.append(id_str, arraysize(id_str)); | |
67 compressed.append(len_str, arraysize(len_str)); | |
68 compressed.append(serialized); | |
69 ++header_sequence_id_; | |
70 return compressed; | |
71 } | |
72 | |
73 } // namespace net | |
OLD | NEW |