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 bool kDefaultCompressed = false; 13 14 SpdyWebSocketTestUtil::SpdyWebSocketTestUtil( 15 NextProto protocol) : spdy_util_(protocol) {} 16 17 std::string SpdyWebSocketTestUtil::GetHeader(const SpdyHeaderBlock& headers, 18 const std::string& key) const { 19 SpdyHeaderBlock::const_iterator it = headers.find(GetHeaderKey(key)); 20 return (it == headers.end()) ? "" : it->second; 21 } 22 23 void SpdyWebSocketTestUtil::SetHeader( 24 const std::string& key, 25 const std::string& value, 26 SpdyHeaderBlock* headers) const { 27 (*headers)[GetHeaderKey(key)] = value; 28 } 29 30 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynStream( 31 int stream_id, 32 const char* path, 33 const char* host, 34 const char* origin) { 35 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); 36 SetHeader("path", path, headers.get()); 37 SetHeader("host", host, headers.get()); 38 SetHeader("version", "WebSocket/13", headers.get()); 39 SetHeader("scheme", "ws", headers.get()); 40 SetHeader("origin", origin, headers.get()); 41 return spdy_util_.ConstructSpdySyn( 42 stream_id, *headers, HIGHEST, false, false); 43 } 44 45 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynReply( 46 int stream_id) { 47 SpdyHeaderBlock block; 48 SetHeader("status", "101", &block); 49 return spdy_util_.ConstructSpdyReply(stream_id, block); 50 } 51 52 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeRequestFrame( 53 scoped_ptr<SpdyHeaderBlock> headers, 54 SpdyStreamId stream_id, 55 RequestPriority request_priority) { 56 return spdy_util_.ConstructSpdySyn( 57 stream_id, *headers, request_priority, kDefaultCompressed, false); 58 } 59 60 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeResponseFrame( 61 scoped_ptr<SpdyHeaderBlock> headers, 62 SpdyStreamId stream_id, 63 RequestPriority request_priority) { 64 return spdy_util_.ConstructSpdyReply(stream_id, *headers); 65 } 66 67 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHeadersFrame( 68 int stream_id, 69 const char* length, 70 bool fin) { 71 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); 72 SetHeader("opcode", "1", headers.get()); // text frame 73 SetHeader("length", length, headers.get()); 74 SetHeader("fin", fin ? "1" : "0", headers.get()); 75 return spdy_util_.ConstructSpdySyn(stream_id, *headers, LOWEST, false, false); 76 } 77 78 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdyWebSocketDataFrame( 79 const char* data, 80 int len, 81 SpdyStreamId stream_id, 82 bool fin) { 83 84 // Construct SPDY data frame. 85 BufferedSpdyFramer framer(spdy_util_.spdy_version(), false); 86 return framer.CreateDataFrame( 87 stream_id, 88 data, 89 len, 90 fin ? DATA_FLAG_FIN : DATA_FLAG_NONE); 91 } 92 93 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdySettings( 94 const SettingsMap& settings) const { 95 return spdy_util_.ConstructSpdySettings(settings); 96 } 97 98 SpdyFrame* SpdyWebSocketTestUtil::ConstructSpdySettingsAck() const { 99 return spdy_util_.ConstructSpdySettingsAck(); 100 } 101 102 SpdyMajorVersion SpdyWebSocketTestUtil::spdy_version() const { 103 return spdy_util_.spdy_version(); 104 } 105 106 std::string SpdyWebSocketTestUtil::GetHeaderKey( 107 const std::string& key) const { 108 return (spdy_util_.is_spdy2() ? "" : ":") + key; 109 } 110 111 } // namespace net 112