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/spdy/spdy_websocket_test_util.h" | 5 #include "net/spdy/spdy_websocket_test_util.h" |
6 | 6 |
7 #include "net/spdy/buffered_spdy_framer.h" | 7 #include "net/spdy/buffered_spdy_framer.h" |
8 #include "net/spdy/spdy_http_utils.h" | 8 #include "net/spdy/spdy_http_utils.h" |
9 | 9 |
10 namespace net { | 10 namespace net { |
11 | 11 |
12 const int kDefaultAssociatedStreamId = 0; | |
13 const bool kDefaultCompressed = false; | 12 const bool kDefaultCompressed = false; |
14 const char* const kDefaultDataPointer = NULL; | |
15 const uint32 kDefaultDataLength = 0; | |
16 | 13 |
17 SpdyWebSocketTestUtil::SpdyWebSocketTestUtil( | 14 SpdyWebSocketTestUtil::SpdyWebSocketTestUtil( |
18 NextProto protocol) : spdy_util_(protocol) {} | 15 NextProto protocol) : spdy_util_(protocol) {} |
19 | 16 |
20 std::string SpdyWebSocketTestUtil::GetHeader(const SpdyHeaderBlock& headers, | 17 std::string SpdyWebSocketTestUtil::GetHeader(const SpdyHeaderBlock& headers, |
21 const std::string& key) const { | 18 const std::string& key) const { |
22 SpdyHeaderBlock::const_iterator it = headers.find(GetHeaderKey(key)); | 19 SpdyHeaderBlock::const_iterator it = headers.find(GetHeaderKey(key)); |
23 return (it == headers.end()) ? "" : it->second; | 20 return (it == headers.end()) ? "" : it->second; |
24 } | 21 } |
25 | 22 |
26 void SpdyWebSocketTestUtil::SetHeader( | 23 void SpdyWebSocketTestUtil::SetHeader( |
27 const std::string& key, | 24 const std::string& key, |
28 const std::string& value, | 25 const std::string& value, |
29 SpdyHeaderBlock* headers) const { | 26 SpdyHeaderBlock* headers) const { |
30 (*headers)[GetHeaderKey(key)] = value; | 27 (*headers)[GetHeaderKey(key)] = value; |
31 } | 28 } |
32 | 29 |
33 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynStream( | 30 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynStream( |
34 int stream_id, | 31 int stream_id, |
35 const char* path, | 32 const char* path, |
36 const char* host, | 33 const char* host, |
37 const char* origin) { | 34 const char* origin) { |
38 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); | 35 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); |
39 SetHeader("path", path, headers.get()); | 36 SetHeader("path", path, headers.get()); |
40 SetHeader("host", host, headers.get()); | 37 SetHeader("host", host, headers.get()); |
41 SetHeader("version", "WebSocket/13", headers.get()); | 38 SetHeader("version", "WebSocket/13", headers.get()); |
42 SetHeader("scheme", "ws", headers.get()); | 39 SetHeader("scheme", "ws", headers.get()); |
43 SetHeader("origin", origin, headers.get()); | 40 SetHeader("origin", origin, headers.get()); |
44 return spdy_util_.ConstructSpdyControlFrame(headers.Pass(), | 41 return spdy_util_.ConstructSpdySyn( |
45 /*compressed*/ false, | 42 stream_id, *headers, HIGHEST, false, false); |
46 stream_id, | |
47 HIGHEST, | |
48 SYN_STREAM, | |
49 CONTROL_FLAG_NONE, | |
50 0); | |
51 } | 43 } |
52 | 44 |
53 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynReply( | 45 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynReply( |
54 int stream_id) { | 46 int stream_id) { |
55 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); | 47 SpdyHeaderBlock block; |
56 SetHeader("status", "101", headers.get()); | 48 SetHeader("status", "101", &block); |
57 return spdy_util_.ConstructSpdyControlFrame(headers.Pass(), | 49 return spdy_util_.ConstructSpdyReply(stream_id, block); |
58 false, | |
59 stream_id, | |
60 LOWEST, | |
61 SYN_REPLY, | |
62 CONTROL_FLAG_NONE, | |
63 0); | |
64 } | 50 } |
65 | 51 |
66 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeRequestFrame( | 52 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeRequestFrame( |
67 scoped_ptr<SpdyHeaderBlock> headers, | 53 scoped_ptr<SpdyHeaderBlock> headers, |
68 SpdyStreamId stream_id, | 54 SpdyStreamId stream_id, |
69 RequestPriority request_priority) { | 55 RequestPriority request_priority) { |
70 // SPDY SYN_STREAM control frame header. | 56 return spdy_util_.ConstructSpdySyn( |
71 const SpdyHeaderInfo kSynStreamHeader = { | 57 stream_id, *headers, request_priority, kDefaultCompressed, false); |
72 SYN_STREAM, | |
73 stream_id, | |
74 kDefaultAssociatedStreamId, | |
75 ConvertRequestPriorityToSpdyPriority(request_priority, SPDY2), | |
76 kSpdyCredentialSlotUnused, | |
77 CONTROL_FLAG_NONE, | |
78 kDefaultCompressed, | |
79 RST_STREAM_INVALID, | |
80 kDefaultDataPointer, | |
81 kDefaultDataLength, | |
82 DATA_FLAG_NONE | |
83 }; | |
84 | |
85 // Construct SPDY SYN_STREAM control frame. | |
86 return spdy_util_.ConstructSpdyFrame( | |
87 kSynStreamHeader, | |
88 headers.Pass()); | |
89 } | 58 } |
90 | 59 |
91 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeResponseFrame( | 60 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeResponseFrame( |
92 scoped_ptr<SpdyHeaderBlock> headers, | 61 scoped_ptr<SpdyHeaderBlock> headers, |
93 SpdyStreamId stream_id, | 62 SpdyStreamId stream_id, |
94 RequestPriority request_priority) { | 63 RequestPriority request_priority) { |
95 // SPDY SYN_REPLY control frame header. | 64 return spdy_util_.ConstructSpdyReply(stream_id, *headers); |
96 const SpdyHeaderInfo kSynReplyHeader = { | |
97 SYN_REPLY, | |
98 stream_id, | |
99 kDefaultAssociatedStreamId, | |
100 ConvertRequestPriorityToSpdyPriority(request_priority, SPDY2), | |
101 kSpdyCredentialSlotUnused, | |
102 CONTROL_FLAG_NONE, | |
103 kDefaultCompressed, | |
104 RST_STREAM_INVALID, | |
105 kDefaultDataPointer, | |
106 kDefaultDataLength, | |
107 DATA_FLAG_NONE | |
108 }; | |
109 | |
110 // Construct SPDY SYN_REPLY control frame. | |
111 return spdy_util_.ConstructSpdyFrame( | |
112 kSynReplyHeader, | |
113 headers.Pass()); | |
114 } | 65 } |
115 | 66 |
116 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHeadersFrame( | 67 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHeadersFrame( |
117 int stream_id, | 68 int stream_id, |
118 const char* length, | 69 const char* length, |
119 bool fin) { | 70 bool fin) { |
120 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); | 71 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); |
121 SetHeader("opcode", "1", headers.get()); // text frame | 72 SetHeader("opcode", "1", headers.get()); // text frame |
122 SetHeader("length", length, headers.get()); | 73 SetHeader("length", length, headers.get()); |
123 SetHeader("fin", fin ? "1" : "0", headers.get()); | 74 SetHeader("fin", fin ? "1" : "0", headers.get()); |
124 return spdy_util_.ConstructSpdyControlFrame(headers.Pass(), | 75 return spdy_util_.ConstructSpdySyn(stream_id, *headers, LOWEST, false, false); |
125 /*compression*/ false, | |
126 stream_id, | |
127 LOWEST, | |
128 HEADERS, | |
129 CONTROL_FLAG_NONE, | |
130 0); | |
131 } | 76 } |
132 | 77 |
133 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketDataFrame( | 78 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketDataFrame( |
134 const char* data, | 79 const char* data, |
135 int len, | 80 int len, |
136 SpdyStreamId stream_id, | 81 SpdyStreamId stream_id, |
137 bool fin) { | 82 bool fin) { |
138 | 83 |
139 // Construct SPDY data frame. | 84 // Construct SPDY data frame. |
140 BufferedSpdyFramer framer(spdy_util_.spdy_version(), false); | 85 BufferedSpdyFramer framer(spdy_util_.spdy_version(), false); |
(...skipping 16 matching lines...) Expand all Loading... |
157 SpdyMajorVersion SpdyWebSocketTestUtil::spdy_version() const { | 102 SpdyMajorVersion SpdyWebSocketTestUtil::spdy_version() const { |
158 return spdy_util_.spdy_version(); | 103 return spdy_util_.spdy_version(); |
159 } | 104 } |
160 | 105 |
161 std::string SpdyWebSocketTestUtil::GetHeaderKey( | 106 std::string SpdyWebSocketTestUtil::GetHeaderKey( |
162 const std::string& key) const { | 107 const std::string& key) const { |
163 return (spdy_util_.is_spdy2() ? "" : ":") + key; | 108 return (spdy_util_.is_spdy2() ? "" : ":") + key; |
164 } | 109 } |
165 | 110 |
166 } // namespace net | 111 } // namespace net |
OLD | NEW |