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/spdy_websocket_test_util.h" 6 7 #include "net/spdy/buffered_spdy_framer.h" 8 #include "net/spdy/spdy_http_utils.h" 9 10 namespace net { 11 12 const int kDefaultAssociatedStreamId = 0; 13 const bool kDefaultCompressed = false; 14 const char* const kDefaultDataPointer = NULL; 15 const uint32 kDefaultDataLength = 0; 16 17 SpdyWebSocketTestUtil::SpdyWebSocketTestUtil( 18 NextProto protocol) : spdy_util_(protocol) {} 19 20 std::string SpdyWebSocketTestUtil::GetHeader(const SpdyHeaderBlock& headers, 21 const std::string& key) const { 22 SpdyHeaderBlock::const_iterator it = headers.find(GetHeaderKey(key)); 23 return (it == headers.end()) ? "" : it->second; 24 } 25 26 void SpdyWebSocketTestUtil::SetHeader( 27 const std::string& key, 28 const std::string& value, 29 SpdyHeaderBlock* headers) const { 30 (*headers)[GetHeaderKey(key)] = value; 31 } 32 33 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynStream( 34 int stream_id, 35 const char* path, 36 const char* host, 37 const char* origin) { 38 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); 39 SetHeader("path", path, headers.get()); 40 SetHeader("host", host, headers.get()); 41 SetHeader("version", "WebSocket/13", headers.get()); 42 SetHeader("scheme", "ws", headers.get()); 43 SetHeader("origin", origin, headers.get()); 44 return spdy_util_.ConstructSpdyControlFrame(headers.Pass(), 45 /*compressed*/ false, 46 stream_id, 47 HIGHEST, 48 SYN_STREAM, 49 CONTROL_FLAG_NONE, 50 0); 51 } 52 53 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynReply( 54 int stream_id) { 55 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); 56 SetHeader("status", "101", headers.get()); 57 return spdy_util_.ConstructSpdyControlFrame(headers.Pass(), 58 false, 59 stream_id, 60 LOWEST, 61 SYN_REPLY, 62 CONTROL_FLAG_NONE, 63 0); 64 } 65 66 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeRequestFrame( 67 scoped_ptr<SpdyHeaderBlock> headers, 68 SpdyStreamId stream_id, 69 RequestPriority request_priority) { 70 // SPDY SYN_STREAM control frame header. 71 const SpdyHeaderInfo kSynStreamHeader = { 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 } 90 91 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeResponseFrame( 92 scoped_ptr<SpdyHeaderBlock> headers, 93 SpdyStreamId stream_id, 94 RequestPriority request_priority) { 95 // SPDY SYN_REPLY control frame header. 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 } 115 116 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHeadersFrame( 117 int stream_id, 118 const char* length, 119 bool fin) { 120 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); 121 SetHeader("opcode", "1", headers.get()); // text frame 122 SetHeader("length", length, headers.get()); 123 SetHeader("fin", fin ? "1" : "0", headers.get()); 124 return spdy_util_.ConstructSpdyControlFrame(headers.Pass(), 125 /*compression*/ false, 126 stream_id, 127 LOWEST, 128 HEADERS, 129 CONTROL_FLAG_NONE, 130 0); 131 } 132 133 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketDataFrame( 134 const char* data, 135 int len, 136 SpdyStreamId stream_id, 137 bool fin) { 138 139 // Construct SPDY data frame. 140 BufferedSpdyFramer framer(spdy_util_.spdy_version(), false); 141 return framer.CreateDataFrame( 142 stream_id, 143 data, 144 len, 145 fin ? DATA_FLAG_FIN : DATA_FLAG_NONE); 146 } 147 148 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdySettings( 149 const SettingsMap& settings) const { 150 return spdy_util_.ConstructSpdySettings(settings); 151 } 152 153 SpdyMajorVersion SpdyWebSocketTestUtil::spdy_version() const { 154 return spdy_util_.spdy_version(); 155 } 156 157 std::string SpdyWebSocketTestUtil::GetHeaderKey( 158 const std::string& key) const { 159 return (spdy_util_.is_spdy2() ? "" : ":") + key; 160 } 161 162 } // namespace net 163