Skip to content

Commit

Permalink
Merge pull request #277 from square/jwilson/http2_or_spdy3
Browse files Browse the repository at this point in the history
Support multiple variants of the SPDY protocol.
  • Loading branch information
JakeWharton committed Aug 12, 2013
2 parents db3b3f4 + 21dc92f commit 83e77ea
Show file tree
Hide file tree
Showing 9 changed files with 707 additions and 459 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@
*/
public final class MockWebServer {
private static final byte[] NPN_PROTOCOLS = {
// TODO: support HTTP/2.0.
// 17, 'H', 'T', 'T', 'P', '-', 'd', 'r', 'a', 'f', 't', '-', '0', '4', '/', '2', '.', '0',
6, 's', 'p', 'd', 'y', '/', '3',
8, 'h', 't', 't', 'p', '/', '1', '.', '1'
};
private static final byte[] SPDY3 = new byte[] {
's', 'p', 'd', 'y', '/', '3'
};
private static final byte[] HTTP_20_DRAFT_04 = new byte[] {
'H', 'T', 'T', 'P', '-', 'd', 'r', 'a', 'f', 't', '-', '0', '4', '/', '2', '.', '0'
};
private static final byte[] HTTP_11 = new byte[] {
'h', 't', 't', 'p', '/', '1', '.', '1'
};
Expand Down Expand Up @@ -322,6 +327,8 @@ public void processConnection() throws Exception {
byte[] selectedProtocol = Platform.get().getNpnSelectedProtocol(sslSocket);
if (selectedProtocol == null || Arrays.equals(selectedProtocol, HTTP_11)) {
transport = Transport.HTTP_11;
} else if (Arrays.equals(selectedProtocol, HTTP_20_DRAFT_04)) {
transport = Transport.HTTP_20_DRAFT_04;
} else if (Arrays.equals(selectedProtocol, SPDY3)) {
transport = Transport.SPDY_3;
} else {
Expand All @@ -334,13 +341,19 @@ public void processConnection() throws Exception {
socket = raw;
}

if (transport == Transport.SPDY_3) {
if (transport == Transport.SPDY_3 || transport == Transport.HTTP_20_DRAFT_04) {
SpdySocketHandler spdySocketHandler = new SpdySocketHandler(socket);
SpdyConnection spdyConnection = new SpdyConnection.Builder(false, socket)
.handler(spdySocketHandler)
.build();
SpdyConnection.Builder builder = new SpdyConnection.Builder(false, socket)
.handler(spdySocketHandler);
if (transport == Transport.SPDY_3) {
builder.spdy3();
} else {
builder.http20Draft04();
}
SpdyConnection spdyConnection = builder.build();
openSpdyConnections.put(spdyConnection, Boolean.TRUE);
openClientSockets.remove(socket);
spdyConnection.sendConnectionHeader();
return;
}

Expand Down Expand Up @@ -699,6 +712,6 @@ private void writeResponse(SpdyStream stream, MockResponse response) throws IOEx
}

enum Transport {
HTTP_11, SPDY_3
HTTP_11, SPDY_3, HTTP_20_DRAFT_04
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (C) 2013 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.okhttp.internal.spdy;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

final class Http20Draft04 implements Variant {
@Override public SpdyReader newReader(InputStream in) {
return new Reader(in);
}

@Override public SpdyWriter newWriter(OutputStream out) {
return new Writer(out);
}

static final class Reader implements SpdyReader {
private final DataInputStream in;

Reader(InputStream in) {
this.in = new DataInputStream(in);
}

@Override public boolean nextFrame(Handler handler) throws IOException {
return false;
}

@Override public void close() throws IOException {
in.close();
}
}

static final class Writer implements SpdyWriter {
private final DataOutputStream out;

Writer(OutputStream out) {
this.out = new DataOutputStream(out);
}

@Override public synchronized void writeFrame(byte[] data, int offset, int length)
throws IOException {
// TODO: this method no longer makes sense; the raw frame can't support all variants!
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void flush() throws IOException {
out.flush();
}

@Override public synchronized void connectionHeader() {
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void synStream(int flags, int streamId, int associatedStreamId,
int priority, int slot, List<String> nameValueBlock) throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void synReply(int flags, int streamId,
List<String> nameValueBlock) throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void headers(int flags, int streamId, List<String> nameValueBlock)
throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void rstStream(int streamId, int statusCode) throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void data(int flags, int streamId, byte[] data)
throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void settings(int flags, Settings settings) throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void noop() throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void ping(int flags, int id) throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void goAway(int flags, int lastGoodStreamId, int statusCode)
throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override public synchronized void windowUpdate(int streamId, int deltaWindowSize)
throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override public void close() throws IOException {
out.close();
}
}
}
Loading

0 comments on commit 83e77ea

Please sign in to comment.