mirror of
https://github.com/square/okhttp.git
synced 2026-01-25 16:01:38 +03:00
Merge pull request #283 from square/jwilson/hide_more_flags
Hide more flags from SPDY APIs.
This commit is contained in:
@@ -22,21 +22,20 @@ import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/** Reads transport frames for SPDY/3 or HTTP/2.0. */
|
||||
public interface SpdyReader extends Closeable {
|
||||
public interface FrameReader extends Closeable {
|
||||
boolean nextFrame(Handler handler) throws IOException;
|
||||
|
||||
public interface Handler {
|
||||
void data(boolean inFinished, int streamId, InputStream in, int length)
|
||||
throws IOException;
|
||||
void data(boolean inFinished, int streamId, InputStream in, int length) throws IOException;
|
||||
void synStream(boolean outFinished, boolean inFinished, int streamId, int associatedStreamId,
|
||||
int priority, int slot, List<String> nameValueBlock);
|
||||
void synReply(boolean inFinished, int streamId, List<String> nameValueBlock) throws IOException;
|
||||
void headers(int flags, int streamId, List<String> nameValueBlock) throws IOException;
|
||||
void rstStream(int flags, int streamId, int statusCode);
|
||||
void settings(int flags, Settings settings);
|
||||
void headers(int streamId, List<String> nameValueBlock) throws IOException;
|
||||
void rstStream(int streamId, int statusCode);
|
||||
void settings(boolean clearPrevious, Settings settings);
|
||||
void noop();
|
||||
void ping(int flags, int streamId);
|
||||
void goAway(int flags, int lastGoodStreamId, int statusCode);
|
||||
void windowUpdate(int flags, int streamId, int deltaWindowSize);
|
||||
void ping(int streamId);
|
||||
void goAway(int lastGoodStreamId, int statusCode);
|
||||
void windowUpdate(int streamId, int deltaWindowSize);
|
||||
}
|
||||
}
|
||||
@@ -21,20 +21,23 @@ import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/** Writes transport frames for SPDY/3 or HTTP/2.0. */
|
||||
public interface SpdyWriter extends Closeable {
|
||||
public interface FrameWriter extends Closeable {
|
||||
/** HTTP/2.0 only. */
|
||||
void connectionHeader();
|
||||
|
||||
/** SPDY/3 only. */
|
||||
void flush() throws IOException;
|
||||
void synStream(boolean outFinished, boolean inFinished, int streamId, int associatedStreamId,
|
||||
int priority, int slot, List<String> nameValueBlock) throws IOException;
|
||||
void synReply(boolean outFinished, int streamId, List<String> nameValueBlock) throws IOException;
|
||||
void headers(int flags, int streamId, List<String> nameValueBlock) throws IOException;
|
||||
void headers(int streamId, List<String> nameValueBlock) throws IOException;
|
||||
void rstStream(int streamId, int statusCode) throws IOException;
|
||||
void data(boolean outFinished, int streamId, byte[] data) throws IOException;
|
||||
void data(boolean outFinished, int streamId, byte[] data, int offset, int byteCount)
|
||||
throws IOException;
|
||||
void settings(int flags, Settings settings) throws IOException;
|
||||
void settings(Settings settings) throws IOException;
|
||||
void noop() throws IOException;
|
||||
void ping(int flags, int id) throws IOException;
|
||||
void goAway(int flags, int lastGoodStreamId, int statusCode) throws IOException;
|
||||
void ping(int id) throws IOException;
|
||||
void goAway(int lastGoodStreamId, int statusCode) throws IOException;
|
||||
void windowUpdate(int streamId, int deltaWindowSize) throws IOException;
|
||||
}
|
||||
@@ -23,15 +23,15 @@ import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
|
||||
final class Http20Draft04 implements Variant {
|
||||
@Override public SpdyReader newReader(InputStream in) {
|
||||
@Override public FrameReader newReader(InputStream in) {
|
||||
return new Reader(in);
|
||||
}
|
||||
|
||||
@Override public SpdyWriter newWriter(OutputStream out) {
|
||||
@Override public FrameWriter newWriter(OutputStream out) {
|
||||
return new Writer(out);
|
||||
}
|
||||
|
||||
static final class Reader implements SpdyReader {
|
||||
static final class Reader implements FrameReader {
|
||||
private final DataInputStream in;
|
||||
|
||||
Reader(InputStream in) {
|
||||
@@ -61,7 +61,7 @@ final class Http20Draft04 implements Variant {
|
||||
}
|
||||
}
|
||||
|
||||
static final class Writer implements SpdyWriter {
|
||||
static final class Writer implements FrameWriter {
|
||||
private final DataOutputStream out;
|
||||
|
||||
Writer(OutputStream out) {
|
||||
@@ -87,7 +87,7 @@ final class Http20Draft04 implements Variant {
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
}
|
||||
|
||||
@Override public synchronized void headers(int flags, int streamId, List<String> nameValueBlock)
|
||||
@Override public synchronized void headers(int streamId, List<String> nameValueBlock)
|
||||
throws IOException {
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
}
|
||||
@@ -106,7 +106,7 @@ final class Http20Draft04 implements Variant {
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
}
|
||||
|
||||
@Override public synchronized void settings(int flags, Settings settings) throws IOException {
|
||||
@Override public synchronized void settings(Settings settings) throws IOException {
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
}
|
||||
|
||||
@@ -114,11 +114,11 @@ final class Http20Draft04 implements Variant {
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
}
|
||||
|
||||
@Override public synchronized void ping(int flags, int id) throws IOException {
|
||||
@Override public synchronized void ping(int id) throws IOException {
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
}
|
||||
|
||||
@Override public synchronized void goAway(int flags, int lastGoodStreamId, int statusCode)
|
||||
@Override public synchronized void goAway(int lastGoodStreamId, int statusCode)
|
||||
throws IOException {
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
}
|
||||
|
||||
@@ -92,16 +92,16 @@ final class Spdy3 implements Variant {
|
||||
}
|
||||
}
|
||||
|
||||
@Override public SpdyReader newReader(InputStream in) {
|
||||
@Override public FrameReader newReader(InputStream in) {
|
||||
return new Reader(in);
|
||||
}
|
||||
|
||||
@Override public SpdyWriter newWriter(OutputStream out) {
|
||||
@Override public FrameWriter newWriter(OutputStream out) {
|
||||
return new Writer(out);
|
||||
}
|
||||
|
||||
/** Read spdy/3 frames. */
|
||||
static final class Reader implements SpdyReader {
|
||||
static final class Reader implements FrameReader {
|
||||
private final DataInputStream in;
|
||||
private final DataInputStream nameValueBlockIn;
|
||||
private int compressedLimit;
|
||||
@@ -217,14 +217,14 @@ final class Spdy3 implements Variant {
|
||||
if (length != 8) throw ioException("TYPE_RST_STREAM length: %d != 8", length);
|
||||
int streamId = in.readInt() & 0x7fffffff;
|
||||
int statusCode = in.readInt();
|
||||
handler.rstStream(flags, streamId, statusCode);
|
||||
handler.rstStream(streamId, statusCode);
|
||||
}
|
||||
|
||||
private void readHeaders(Handler handler, int flags, int length) throws IOException {
|
||||
int w1 = in.readInt();
|
||||
int streamId = w1 & 0x7fffffff;
|
||||
List<String> nameValueBlock = readNameValueBlock(length - 4);
|
||||
handler.headers(flags, streamId, nameValueBlock);
|
||||
handler.headers(streamId, nameValueBlock);
|
||||
}
|
||||
|
||||
private void readWindowUpdate(Handler handler, int flags, int length) throws IOException {
|
||||
@@ -233,7 +233,7 @@ final class Spdy3 implements Variant {
|
||||
int w2 = in.readInt();
|
||||
int streamId = w1 & 0x7fffffff;
|
||||
int deltaWindowSize = w2 & 0x7fffffff;
|
||||
handler.windowUpdate(flags, streamId, deltaWindowSize);
|
||||
handler.windowUpdate(streamId, deltaWindowSize);
|
||||
}
|
||||
|
||||
private DataInputStream newNameValueBlockStream() {
|
||||
@@ -308,14 +308,14 @@ final class Spdy3 implements Variant {
|
||||
private void readPing(Handler handler, int flags, int length) throws IOException {
|
||||
if (length != 4) throw ioException("TYPE_PING length: %d != 4", length);
|
||||
int id = in.readInt();
|
||||
handler.ping(flags, id);
|
||||
handler.ping(id);
|
||||
}
|
||||
|
||||
private void readGoAway(Handler handler, int flags, int length) throws IOException {
|
||||
if (length != 8) throw ioException("TYPE_GOAWAY length: %d != 8", length);
|
||||
int lastGoodStreamId = in.readInt() & 0x7fffffff;
|
||||
int statusCode = in.readInt();
|
||||
handler.goAway(flags, lastGoodStreamId, statusCode);
|
||||
handler.goAway(lastGoodStreamId, statusCode);
|
||||
}
|
||||
|
||||
private void readSettings(Handler handler, int flags, int length) throws IOException {
|
||||
@@ -331,7 +331,8 @@ final class Spdy3 implements Variant {
|
||||
int id = w1 & 0xffffff;
|
||||
settings.set(id, idFlags, value);
|
||||
}
|
||||
handler.settings(flags, settings);
|
||||
boolean clearPrevious = (flags & Settings.FLAG_CLEAR_PREVIOUSLY_PERSISTED_SETTINGS) != 0;
|
||||
handler.settings(clearPrevious, settings);
|
||||
}
|
||||
|
||||
private static IOException ioException(String message, Object... args) throws IOException {
|
||||
@@ -344,7 +345,7 @@ final class Spdy3 implements Variant {
|
||||
}
|
||||
|
||||
/** Write spdy/3 frames. */
|
||||
static final class Writer implements SpdyWriter {
|
||||
static final class Writer implements FrameWriter {
|
||||
private final DataOutputStream out;
|
||||
private final ByteArrayOutputStream nameValueBlockBuffer;
|
||||
private final DataOutputStream nameValueBlockOut;
|
||||
@@ -399,9 +400,10 @@ final class Spdy3 implements Variant {
|
||||
out.flush();
|
||||
}
|
||||
|
||||
@Override public synchronized void headers(int flags, int streamId, List<String> nameValueBlock)
|
||||
@Override public synchronized void headers(int streamId, List<String> nameValueBlock)
|
||||
throws IOException {
|
||||
writeNameValueBlockToBuffer(nameValueBlock);
|
||||
int flags = 0;
|
||||
int type = TYPE_HEADERS;
|
||||
int length = nameValueBlockBuffer.size() + 4;
|
||||
|
||||
@@ -447,8 +449,9 @@ final class Spdy3 implements Variant {
|
||||
nameValueBlockOut.flush();
|
||||
}
|
||||
|
||||
@Override public synchronized void settings(int flags, Settings settings) throws IOException {
|
||||
@Override public synchronized void settings(Settings settings) throws IOException {
|
||||
int type = TYPE_SETTINGS;
|
||||
int flags = 0;
|
||||
int size = settings.size();
|
||||
int length = 4 + size * 8;
|
||||
out.writeInt(0x80000000 | (VERSION & 0x7fff) << 16 | type & 0xffff);
|
||||
@@ -472,8 +475,9 @@ final class Spdy3 implements Variant {
|
||||
out.flush();
|
||||
}
|
||||
|
||||
@Override public synchronized void ping(int flags, int id) throws IOException {
|
||||
@Override public synchronized void ping(int id) throws IOException {
|
||||
int type = TYPE_PING;
|
||||
int flags = 0;
|
||||
int length = 4;
|
||||
out.writeInt(0x80000000 | (VERSION & 0x7fff) << 16 | type & 0xffff);
|
||||
out.writeInt((flags & 0xff) << 24 | length & 0xffffff);
|
||||
@@ -481,9 +485,10 @@ final class Spdy3 implements Variant {
|
||||
out.flush();
|
||||
}
|
||||
|
||||
@Override public synchronized void goAway(int flags, int lastGoodStreamId, int statusCode)
|
||||
@Override public synchronized void goAway(int lastGoodStreamId, int statusCode)
|
||||
throws IOException {
|
||||
int type = TYPE_GOAWAY;
|
||||
int flags = 0;
|
||||
int length = 8;
|
||||
out.writeInt(0x80000000 | (VERSION & 0x7fff) << 16 | type & 0xffff);
|
||||
out.writeInt((flags & 0xff) << 24 | length & 0xffffff);
|
||||
|
||||
@@ -46,12 +46,12 @@ public final class SpdyConnection implements Closeable {
|
||||
// Internal state of this connection is guarded by 'this'. No blocking
|
||||
// operations may be performed while holding this lock!
|
||||
//
|
||||
// Socket writes are guarded by spdyWriter.
|
||||
// Socket writes are guarded by frameWriter.
|
||||
//
|
||||
// Socket reads are unguarded but are only made by the reader thread.
|
||||
//
|
||||
// Certain operations (like SYN_STREAM) need to synchronize on both the
|
||||
// spdyWriter (to do blocking I/O) and this (to create streams). Such
|
||||
// frameWriter (to do blocking I/O) and this (to create streams). Such
|
||||
// operations must synchronize on 'this' last. This ensures that we never
|
||||
// wait for a blocking operation while holding 'this'.
|
||||
|
||||
@@ -74,8 +74,8 @@ public final class SpdyConnection implements Closeable {
|
||||
* run on the callback executor.
|
||||
*/
|
||||
private final IncomingStreamHandler handler;
|
||||
private final SpdyReader spdyReader;
|
||||
private final SpdyWriter spdyWriter;
|
||||
private final FrameReader frameReader;
|
||||
private final FrameWriter frameWriter;
|
||||
|
||||
private final Map<Integer, SpdyStream> streams = new HashMap<Integer, SpdyStream>();
|
||||
private final String hostName;
|
||||
@@ -95,8 +95,8 @@ public final class SpdyConnection implements Closeable {
|
||||
variant = builder.variant;
|
||||
client = builder.client;
|
||||
handler = builder.handler;
|
||||
spdyReader = variant.newReader(builder.in);
|
||||
spdyWriter = variant.newWriter(builder.out);
|
||||
frameReader = variant.newReader(builder.in);
|
||||
frameWriter = variant.newWriter(builder.out);
|
||||
nextStreamId = builder.client ? 1 : 2;
|
||||
nextPingId = builder.client ? 1 : 2;
|
||||
|
||||
@@ -157,7 +157,7 @@ public final class SpdyConnection implements Closeable {
|
||||
SpdyStream stream;
|
||||
int streamId;
|
||||
|
||||
synchronized (spdyWriter) {
|
||||
synchronized (frameWriter) {
|
||||
synchronized (this) {
|
||||
if (shutdown) {
|
||||
throw new IOException("shutdown");
|
||||
@@ -172,7 +172,7 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
spdyWriter.synStream(outFinished, inFinished, streamId, associatedStreamId, priority, slot,
|
||||
frameWriter.synStream(outFinished, inFinished, streamId, associatedStreamId, priority, slot,
|
||||
requestHeaders);
|
||||
}
|
||||
|
||||
@@ -181,12 +181,12 @@ public final class SpdyConnection implements Closeable {
|
||||
|
||||
void writeSynReply(int streamId, boolean outFinished, List<String> alternating)
|
||||
throws IOException {
|
||||
spdyWriter.synReply(outFinished, streamId, alternating);
|
||||
frameWriter.synReply(outFinished, streamId, alternating);
|
||||
}
|
||||
|
||||
public void writeData(int streamId, boolean outFinished, byte[] buffer, int offset, int byteCount)
|
||||
throws IOException {
|
||||
spdyWriter.data(outFinished, streamId, buffer, offset, byteCount);
|
||||
frameWriter.data(outFinished, streamId, buffer, offset, byteCount);
|
||||
}
|
||||
|
||||
void writeSynResetLater(final int streamId, final int statusCode) {
|
||||
@@ -201,7 +201,7 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
|
||||
void writeSynReset(int streamId, int statusCode) throws IOException {
|
||||
spdyWriter.rstStream(streamId, statusCode);
|
||||
frameWriter.rstStream(streamId, statusCode);
|
||||
}
|
||||
|
||||
void writeWindowUpdateLater(final int streamId, final int deltaWindowSize) {
|
||||
@@ -216,7 +216,7 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
|
||||
void writeWindowUpdate(int streamId, int deltaWindowSize) throws IOException {
|
||||
spdyWriter.windowUpdate(streamId, deltaWindowSize);
|
||||
frameWriter.windowUpdate(streamId, deltaWindowSize);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -251,10 +251,10 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
|
||||
private void writePing(int id, Ping ping) throws IOException {
|
||||
synchronized (spdyWriter) {
|
||||
synchronized (frameWriter) {
|
||||
// Observe the sent time immediately before performing I/O.
|
||||
if (ping != null) ping.send();
|
||||
spdyWriter.ping(0, id);
|
||||
frameWriter.ping(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,11 +264,11 @@ public final class SpdyConnection implements Closeable {
|
||||
|
||||
/** Sends a noop frame to the peer. */
|
||||
public void noop() throws IOException {
|
||||
spdyWriter.noop();
|
||||
frameWriter.noop();
|
||||
}
|
||||
|
||||
public void flush() throws IOException {
|
||||
spdyWriter.flush();
|
||||
frameWriter.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -281,7 +281,7 @@ public final class SpdyConnection implements Closeable {
|
||||
* #GOAWAY_INTERNAL_ERROR} or {@link #GOAWAY_PROTOCOL_ERROR}.
|
||||
*/
|
||||
public void shutdown(int statusCode) throws IOException {
|
||||
synchronized (spdyWriter) {
|
||||
synchronized (frameWriter) {
|
||||
int lastGoodStreamId;
|
||||
synchronized (this) {
|
||||
if (shutdown) {
|
||||
@@ -290,7 +290,7 @@ public final class SpdyConnection implements Closeable {
|
||||
shutdown = true;
|
||||
lastGoodStreamId = this.lastGoodStreamId;
|
||||
}
|
||||
spdyWriter.goAway(0, lastGoodStreamId, statusCode);
|
||||
frameWriter.goAway(lastGoodStreamId, statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,12 +343,12 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
|
||||
try {
|
||||
spdyReader.close();
|
||||
frameReader.close();
|
||||
} catch (IOException e) {
|
||||
thrown = e;
|
||||
}
|
||||
try {
|
||||
spdyWriter.close();
|
||||
frameWriter.close();
|
||||
} catch (IOException e) {
|
||||
if (thrown == null) thrown = e;
|
||||
}
|
||||
@@ -361,7 +361,7 @@ public final class SpdyConnection implements Closeable {
|
||||
* be called after {@link Builder#build} for all new connections.
|
||||
*/
|
||||
public void sendConnectionHeader() {
|
||||
spdyWriter.connectionHeader();
|
||||
frameWriter.connectionHeader();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
@@ -419,12 +419,12 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
private class Reader implements Runnable, SpdyReader.Handler {
|
||||
private class Reader implements Runnable, FrameReader.Handler {
|
||||
@Override public void run() {
|
||||
int shutdownStatusCode = GOAWAY_INTERNAL_ERROR;
|
||||
int rstStatusCode = SpdyStream.RST_INTERNAL_ERROR;
|
||||
try {
|
||||
while (spdyReader.nextFrame(this)) {
|
||||
while (frameReader.nextFrame(this)) {
|
||||
}
|
||||
shutdownStatusCode = GOAWAY_OK;
|
||||
rstStatusCode = SpdyStream.RST_CANCEL;
|
||||
@@ -496,7 +496,7 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void headers(int flags, int streamId, List<String> nameValueBlock)
|
||||
@Override public void headers(int streamId, List<String> nameValueBlock)
|
||||
throws IOException {
|
||||
SpdyStream replyStream = getStream(streamId);
|
||||
if (replyStream != null) {
|
||||
@@ -504,17 +504,17 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void rstStream(int flags, int streamId, int statusCode) {
|
||||
@Override public void rstStream(int streamId, int statusCode) {
|
||||
SpdyStream rstStream = removeStream(streamId);
|
||||
if (rstStream != null) {
|
||||
rstStream.receiveRstStream(statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void settings(int flags, Settings newSettings) {
|
||||
@Override public void settings(boolean clearPrevious, Settings newSettings) {
|
||||
SpdyStream[] streamsToNotify = null;
|
||||
synchronized (SpdyConnection.this) {
|
||||
if (settings == null || (flags & Settings.FLAG_CLEAR_PREVIOUSLY_PERSISTED_SETTINGS) != 0) {
|
||||
if (settings == null || clearPrevious) {
|
||||
settings = newSettings;
|
||||
} else {
|
||||
settings.merge(newSettings);
|
||||
@@ -540,7 +540,7 @@ public final class SpdyConnection implements Closeable {
|
||||
@Override public void noop() {
|
||||
}
|
||||
|
||||
@Override public void ping(int flags, int streamId) {
|
||||
@Override public void ping(int streamId) {
|
||||
if (client != (streamId % 2 == 1)) {
|
||||
// Respond to a client ping if this is a server and vice versa.
|
||||
writePingLater(streamId, null);
|
||||
@@ -552,7 +552,7 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void goAway(int flags, int lastGoodStreamId, int statusCode) {
|
||||
@Override public void goAway(int lastGoodStreamId, int statusCode) {
|
||||
synchronized (SpdyConnection.this) {
|
||||
shutdown = true;
|
||||
|
||||
@@ -569,7 +569,7 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void windowUpdate(int flags, int streamId, int deltaWindowSize) {
|
||||
@Override public void windowUpdate(int streamId, int deltaWindowSize) {
|
||||
SpdyStream stream = getStream(streamId);
|
||||
if (stream != null) {
|
||||
stream.receiveWindowUpdate(deltaWindowSize);
|
||||
|
||||
@@ -23,6 +23,6 @@ interface Variant {
|
||||
Variant SPDY3 = new Spdy3();
|
||||
Variant HTTP_20_DRAFT_04 = new Http20Draft04();
|
||||
|
||||
SpdyReader newReader(InputStream in);
|
||||
SpdyWriter newWriter(OutputStream out);
|
||||
FrameReader newReader(InputStream in);
|
||||
FrameWriter newWriter(OutputStream out);
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public final class MockSpdyPeer implements Closeable {
|
||||
private int frameCount = 0;
|
||||
private final boolean client;
|
||||
private final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
|
||||
private final SpdyWriter spdyWriter;
|
||||
private final FrameWriter frameWriter;
|
||||
private final List<OutFrame> outFrames = new ArrayList<OutFrame>();
|
||||
private final BlockingQueue<InFrame> inFrames = new LinkedBlockingQueue<InFrame>();
|
||||
private int port;
|
||||
@@ -49,16 +49,16 @@ public final class MockSpdyPeer implements Closeable {
|
||||
|
||||
public MockSpdyPeer(boolean client) {
|
||||
this.client = client;
|
||||
this.spdyWriter = Variant.SPDY3.newWriter(bytesOut);
|
||||
this.frameWriter = Variant.SPDY3.newWriter(bytesOut);
|
||||
}
|
||||
|
||||
public void acceptFrame() {
|
||||
frameCount++;
|
||||
}
|
||||
|
||||
public SpdyWriter sendFrame() {
|
||||
public FrameWriter sendFrame() {
|
||||
outFrames.add(new OutFrame(frameCount++, bytesOut.size(), Integer.MAX_VALUE));
|
||||
return spdyWriter;
|
||||
return frameWriter;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,9 +66,9 @@ public final class MockSpdyPeer implements Closeable {
|
||||
* useful for testing error handling as the truncated frame will be
|
||||
* malformed.
|
||||
*/
|
||||
public SpdyWriter sendTruncatedFrame(int truncateToLength) {
|
||||
public FrameWriter sendTruncatedFrame(int truncateToLength) {
|
||||
outFrames.add(new OutFrame(frameCount++, bytesOut.size(), truncateToLength));
|
||||
return spdyWriter;
|
||||
return frameWriter;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
@@ -100,7 +100,7 @@ public final class MockSpdyPeer implements Closeable {
|
||||
socket = serverSocket.accept();
|
||||
OutputStream out = socket.getOutputStream();
|
||||
InputStream in = socket.getInputStream();
|
||||
SpdyReader reader = Variant.SPDY3.newReader(in);
|
||||
FrameReader reader = Variant.SPDY3.newReader(in);
|
||||
|
||||
Iterator<OutFrame> outFramesIterator = outFrames.iterator();
|
||||
byte[] outBytes = bytesOut.toByteArray();
|
||||
@@ -164,11 +164,12 @@ public final class MockSpdyPeer implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
public static class InFrame implements SpdyReader.Handler {
|
||||
public static class InFrame implements FrameReader.Handler {
|
||||
public final int sequence;
|
||||
public final SpdyReader reader;
|
||||
public final FrameReader reader;
|
||||
public int type = -1;
|
||||
public int flags = -1;
|
||||
public boolean clearPrevious;
|
||||
public boolean outFinished;
|
||||
public boolean inFinished;
|
||||
public int streamId;
|
||||
@@ -181,15 +182,15 @@ public final class MockSpdyPeer implements Closeable {
|
||||
public byte[] data;
|
||||
public Settings settings;
|
||||
|
||||
public InFrame(int sequence, SpdyReader reader) {
|
||||
public InFrame(int sequence, FrameReader reader) {
|
||||
this.sequence = sequence;
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
@Override public void settings(int flags, Settings settings) {
|
||||
@Override public void settings(boolean clearPrevious, Settings settings) {
|
||||
if (this.type != -1) throw new IllegalStateException();
|
||||
this.type = Spdy3.TYPE_SETTINGS;
|
||||
this.flags = flags;
|
||||
this.clearPrevious = clearPrevious;
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
@@ -214,11 +215,10 @@ public final class MockSpdyPeer implements Closeable {
|
||||
this.nameValueBlock = nameValueBlock;
|
||||
}
|
||||
|
||||
@Override public void headers(int flags, int streamId, List<String> nameValueBlock) {
|
||||
@Override public void headers(int streamId, List<String> nameValueBlock) {
|
||||
if (this.type != -1) throw new IllegalStateException();
|
||||
this.type = Spdy3.TYPE_HEADERS;
|
||||
this.streamId = streamId;
|
||||
this.flags = flags;
|
||||
this.nameValueBlock = nameValueBlock;
|
||||
}
|
||||
|
||||
@@ -232,18 +232,16 @@ public final class MockSpdyPeer implements Closeable {
|
||||
Util.readFully(in, this.data);
|
||||
}
|
||||
|
||||
@Override public void rstStream(int flags, int streamId, int statusCode) {
|
||||
@Override public void rstStream(int streamId, int statusCode) {
|
||||
if (this.type != -1) throw new IllegalStateException();
|
||||
this.type = Spdy3.TYPE_RST_STREAM;
|
||||
this.flags = flags;
|
||||
this.streamId = streamId;
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
@Override public void ping(int flags, int streamId) {
|
||||
@Override public void ping(int streamId) {
|
||||
if (this.type != -1) throw new IllegalStateException();
|
||||
this.type = Spdy3.TYPE_PING;
|
||||
this.flags = flags;
|
||||
this.streamId = streamId;
|
||||
}
|
||||
|
||||
@@ -252,18 +250,16 @@ public final class MockSpdyPeer implements Closeable {
|
||||
this.type = Spdy3.TYPE_NOOP;
|
||||
}
|
||||
|
||||
@Override public void goAway(int flags, int lastGoodStreamId, int statusCode) {
|
||||
@Override public void goAway(int lastGoodStreamId, int statusCode) {
|
||||
if (this.type != -1) throw new IllegalStateException();
|
||||
this.type = Spdy3.TYPE_GOAWAY;
|
||||
this.flags = flags;
|
||||
this.streamId = lastGoodStreamId;
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
@Override public void windowUpdate(int flags, int streamId, int deltaWindowSize) {
|
||||
@Override public void windowUpdate(int streamId, int deltaWindowSize) {
|
||||
if (this.type != -1) throw new IllegalStateException();
|
||||
this.type = Spdy3.TYPE_WINDOW_UPDATE;
|
||||
this.flags = flags;
|
||||
this.streamId = streamId;
|
||||
this.deltaWindowSize = deltaWindowSize;
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ public final class SpdyConnectionTest {
|
||||
peer.acceptFrame(); // SYN_STREAM
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().synReply(true, 1, Arrays.asList("a", "android"));
|
||||
peer.sendFrame().ping(0, 1);
|
||||
peer.sendFrame().ping(1);
|
||||
peer.play();
|
||||
|
||||
// play it back
|
||||
@@ -196,7 +196,7 @@ public final class SpdyConnectionTest {
|
||||
|
||||
@Test public void serverPingsClient() throws Exception {
|
||||
// write the mocking script
|
||||
peer.sendFrame().ping(0, 2);
|
||||
peer.sendFrame().ping(2);
|
||||
peer.acceptFrame(); // PING
|
||||
peer.play();
|
||||
|
||||
@@ -206,14 +206,13 @@ public final class SpdyConnectionTest {
|
||||
// verify the peer received what was expected
|
||||
MockSpdyPeer.InFrame ping = peer.takeFrame();
|
||||
assertEquals(TYPE_PING, ping.type);
|
||||
assertEquals(0, ping.flags);
|
||||
assertEquals(2, ping.streamId);
|
||||
}
|
||||
|
||||
@Test public void clientPingsServer() throws Exception {
|
||||
// write the mocking script
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().ping(0, 1);
|
||||
peer.sendFrame().ping(1);
|
||||
peer.play();
|
||||
|
||||
// play it back
|
||||
@@ -227,16 +226,15 @@ public final class SpdyConnectionTest {
|
||||
// verify the peer received what was expected
|
||||
MockSpdyPeer.InFrame pingFrame = peer.takeFrame();
|
||||
assertEquals(TYPE_PING, pingFrame.type);
|
||||
assertEquals(0, pingFrame.flags);
|
||||
assertEquals(1, pingFrame.streamId);
|
||||
}
|
||||
|
||||
@Test public void unexpectedPingIsNotReturned() throws Exception {
|
||||
// write the mocking script
|
||||
peer.sendFrame().ping(0, 2);
|
||||
peer.sendFrame().ping(2);
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().ping(0, 3); // This ping will not be returned.
|
||||
peer.sendFrame().ping(0, 4);
|
||||
peer.sendFrame().ping(3); // This ping will not be returned.
|
||||
peer.sendFrame().ping(4);
|
||||
peer.acceptFrame(); // PING
|
||||
peer.play();
|
||||
|
||||
@@ -254,8 +252,8 @@ public final class SpdyConnectionTest {
|
||||
// write the mocking script
|
||||
Settings settings = new Settings();
|
||||
settings.set(Settings.MAX_CONCURRENT_STREAMS, PERSIST_VALUE, 10);
|
||||
peer.sendFrame().settings(Settings.FLAG_CLEAR_PREVIOUSLY_PERSISTED_SETTINGS, settings);
|
||||
peer.sendFrame().ping(0, 2);
|
||||
peer.sendFrame().settings(settings);
|
||||
peer.sendFrame().ping(2);
|
||||
peer.acceptFrame(); // PING
|
||||
peer.play();
|
||||
|
||||
@@ -276,13 +274,13 @@ public final class SpdyConnectionTest {
|
||||
settings1.set(Settings.UPLOAD_BANDWIDTH, PERSIST_VALUE, 100);
|
||||
settings1.set(Settings.DOWNLOAD_BANDWIDTH, PERSIST_VALUE, 200);
|
||||
settings1.set(Settings.DOWNLOAD_RETRANS_RATE, 0, 300);
|
||||
peer.sendFrame().settings(0, settings1);
|
||||
peer.sendFrame().settings(settings1);
|
||||
Settings settings2 = new Settings();
|
||||
settings2.set(Settings.DOWNLOAD_BANDWIDTH, 0, 400);
|
||||
settings2.set(Settings.DOWNLOAD_RETRANS_RATE, PERSIST_VALUE, 500);
|
||||
settings2.set(Settings.MAX_CONCURRENT_STREAMS, PERSIST_VALUE, 600);
|
||||
peer.sendFrame().settings(0, settings2);
|
||||
peer.sendFrame().ping(0, 2);
|
||||
peer.sendFrame().settings(settings2);
|
||||
peer.sendFrame().ping(2);
|
||||
peer.acceptFrame();
|
||||
peer.play();
|
||||
|
||||
@@ -308,7 +306,7 @@ public final class SpdyConnectionTest {
|
||||
// write the mocking script
|
||||
peer.sendFrame().data(true, 42, "bogus".getBytes("UTF-8"));
|
||||
peer.acceptFrame(); // RST_STREAM
|
||||
peer.sendFrame().ping(0, 2);
|
||||
peer.sendFrame().ping(2);
|
||||
peer.acceptFrame(); // PING
|
||||
peer.play();
|
||||
|
||||
@@ -318,7 +316,6 @@ public final class SpdyConnectionTest {
|
||||
// verify the peer received what was expected
|
||||
MockSpdyPeer.InFrame rstStream = peer.takeFrame();
|
||||
assertEquals(TYPE_RST_STREAM, rstStream.type);
|
||||
assertEquals(0, rstStream.flags);
|
||||
assertEquals(42, rstStream.streamId);
|
||||
assertEquals(RST_INVALID_STREAM, rstStream.statusCode);
|
||||
MockSpdyPeer.InFrame ping = peer.takeFrame();
|
||||
@@ -329,7 +326,7 @@ public final class SpdyConnectionTest {
|
||||
// write the mocking script
|
||||
peer.sendFrame().synReply(false, 42, Arrays.asList("a", "android"));
|
||||
peer.acceptFrame(); // RST_STREAM
|
||||
peer.sendFrame().ping(0, 2);
|
||||
peer.sendFrame().ping(2);
|
||||
peer.acceptFrame(); // PING
|
||||
peer.play();
|
||||
|
||||
@@ -339,7 +336,6 @@ public final class SpdyConnectionTest {
|
||||
// verify the peer received what was expected
|
||||
MockSpdyPeer.InFrame rstStream = peer.takeFrame();
|
||||
assertEquals(TYPE_RST_STREAM, rstStream.type);
|
||||
assertEquals(0, rstStream.flags);
|
||||
assertEquals(42, rstStream.streamId);
|
||||
assertEquals(RST_INVALID_STREAM, rstStream.statusCode);
|
||||
MockSpdyPeer.InFrame ping = peer.takeFrame();
|
||||
@@ -353,7 +349,7 @@ public final class SpdyConnectionTest {
|
||||
peer.acceptFrame(); // TYPE_DATA
|
||||
peer.acceptFrame(); // TYPE_DATA with FLAG_FIN
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().ping(0, 1);
|
||||
peer.sendFrame().ping(1);
|
||||
peer.play();
|
||||
|
||||
// play it back
|
||||
@@ -397,7 +393,7 @@ public final class SpdyConnectionTest {
|
||||
peer.acceptFrame(); // SYN_STREAM
|
||||
peer.sendFrame().rstStream(1, SpdyStream.RST_CANCEL);
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().ping(0, 1);
|
||||
peer.sendFrame().ping(1);
|
||||
peer.acceptFrame(); // DATA
|
||||
peer.play();
|
||||
|
||||
@@ -551,7 +547,7 @@ public final class SpdyConnectionTest {
|
||||
peer.sendFrame().synReply(false, 1, Arrays.asList("a", "android"));
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().synReply(false, 1, Arrays.asList("b", "banana"));
|
||||
peer.sendFrame().ping(0, 1);
|
||||
peer.sendFrame().ping(1);
|
||||
peer.acceptFrame(); // RST_STREAM
|
||||
peer.play();
|
||||
|
||||
@@ -575,7 +571,6 @@ public final class SpdyConnectionTest {
|
||||
MockSpdyPeer.InFrame rstStream = peer.takeFrame();
|
||||
assertEquals(TYPE_RST_STREAM, rstStream.type);
|
||||
assertEquals(1, rstStream.streamId);
|
||||
assertEquals(0, rstStream.flags);
|
||||
assertEquals(RST_STREAM_IN_USE, rstStream.statusCode);
|
||||
}
|
||||
|
||||
@@ -605,7 +600,6 @@ public final class SpdyConnectionTest {
|
||||
MockSpdyPeer.InFrame rstStream = peer.takeFrame();
|
||||
assertEquals(TYPE_RST_STREAM, rstStream.type);
|
||||
assertEquals(2, rstStream.streamId);
|
||||
assertEquals(0, rstStream.flags);
|
||||
assertEquals(RST_PROTOCOL_ERROR, rstStream.statusCode);
|
||||
assertEquals(1, receiveCount.intValue());
|
||||
}
|
||||
@@ -616,7 +610,7 @@ public final class SpdyConnectionTest {
|
||||
peer.sendFrame().synReply(false, 1, Arrays.asList("a", "android"));
|
||||
peer.sendFrame().data(true, 1, "robot".getBytes("UTF-8"));
|
||||
peer.sendFrame().data(true, 1, "c3po".getBytes("UTF-8")); // Ignored.
|
||||
peer.sendFrame().ping(0, 2); // Ping just to make sure the stream was fastforwarded.
|
||||
peer.sendFrame().ping(2); // Ping just to make sure the stream was fastforwarded.
|
||||
peer.acceptFrame(); // PING
|
||||
peer.play();
|
||||
|
||||
@@ -632,7 +626,6 @@ public final class SpdyConnectionTest {
|
||||
MockSpdyPeer.InFrame ping = peer.takeFrame();
|
||||
assertEquals(TYPE_PING, ping.type);
|
||||
assertEquals(2, ping.streamId);
|
||||
assertEquals(0, ping.flags);
|
||||
}
|
||||
|
||||
@Test public void remoteSendsTooMuchData() throws Exception {
|
||||
@@ -641,7 +634,7 @@ public final class SpdyConnectionTest {
|
||||
peer.sendFrame().synReply(false, 1, Arrays.asList("b", "banana"));
|
||||
peer.sendFrame().data(false, 1, new byte[64 * 1024 + 1]);
|
||||
peer.acceptFrame(); // RST_STREAM
|
||||
peer.sendFrame().ping(0, 2); // Ping just to make sure the stream was fastforwarded.
|
||||
peer.sendFrame().ping(2); // Ping just to make sure the stream was fastforwarded.
|
||||
peer.acceptFrame(); // PING
|
||||
peer.play();
|
||||
|
||||
@@ -656,7 +649,6 @@ public final class SpdyConnectionTest {
|
||||
MockSpdyPeer.InFrame rstStream = peer.takeFrame();
|
||||
assertEquals(TYPE_RST_STREAM, rstStream.type);
|
||||
assertEquals(1, rstStream.streamId);
|
||||
assertEquals(0, rstStream.flags);
|
||||
assertEquals(RST_FLOW_CONTROL_ERROR, rstStream.statusCode);
|
||||
MockSpdyPeer.InFrame ping = peer.takeFrame();
|
||||
assertEquals(TYPE_PING, ping.type);
|
||||
@@ -667,7 +659,7 @@ public final class SpdyConnectionTest {
|
||||
// write the mocking script
|
||||
peer.acceptFrame(); // SYN_STREAM
|
||||
peer.sendFrame().rstStream(1, RST_REFUSED_STREAM);
|
||||
peer.sendFrame().ping(0, 2);
|
||||
peer.sendFrame().ping(2);
|
||||
peer.acceptFrame(); // PING
|
||||
peer.play();
|
||||
|
||||
@@ -688,16 +680,15 @@ public final class SpdyConnectionTest {
|
||||
MockSpdyPeer.InFrame ping = peer.takeFrame();
|
||||
assertEquals(TYPE_PING, ping.type);
|
||||
assertEquals(2, ping.streamId);
|
||||
assertEquals(0, ping.flags);
|
||||
}
|
||||
|
||||
@Test public void receiveGoAway() throws Exception {
|
||||
// write the mocking script
|
||||
peer.acceptFrame(); // SYN_STREAM 1
|
||||
peer.acceptFrame(); // SYN_STREAM 3
|
||||
peer.sendFrame().goAway(0, 1, GOAWAY_PROTOCOL_ERROR);
|
||||
peer.sendFrame().goAway(1, GOAWAY_PROTOCOL_ERROR);
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().ping(0, 1);
|
||||
peer.sendFrame().ping(1);
|
||||
peer.acceptFrame(); // DATA STREAM 1
|
||||
peer.play();
|
||||
|
||||
@@ -742,7 +733,7 @@ public final class SpdyConnectionTest {
|
||||
peer.acceptFrame(); // GOAWAY
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().synStream(false, false, 2, 0, 0, 0, Arrays.asList("b", "b")); // Should be ignored!
|
||||
peer.sendFrame().ping(0, 1);
|
||||
peer.sendFrame().ping(1);
|
||||
peer.play();
|
||||
|
||||
// play it back
|
||||
@@ -845,7 +836,7 @@ public final class SpdyConnectionTest {
|
||||
peer.acceptFrame(); // SYN_STREAM
|
||||
peer.sendFrame().synReply(false, 1, Arrays.asList("a", "android"));
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().ping(0, 1);
|
||||
peer.sendFrame().ping(1);
|
||||
peer.play();
|
||||
|
||||
// play it back
|
||||
@@ -874,8 +865,8 @@ public final class SpdyConnectionTest {
|
||||
peer.acceptFrame(); // SYN_STREAM
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().synReply(false, 1, Arrays.asList("a", "android"));
|
||||
peer.sendFrame().headers(0, 1, Arrays.asList("c", "c3po"));
|
||||
peer.sendFrame().ping(0, 1);
|
||||
peer.sendFrame().headers(1, Arrays.asList("c", "c3po"));
|
||||
peer.sendFrame().ping(1);
|
||||
peer.play();
|
||||
|
||||
// play it back
|
||||
@@ -895,9 +886,9 @@ public final class SpdyConnectionTest {
|
||||
// write the mocking script
|
||||
peer.acceptFrame(); // SYN_STREAM
|
||||
peer.acceptFrame(); // PING
|
||||
peer.sendFrame().headers(0, 1, Arrays.asList("c", "c3po"));
|
||||
peer.sendFrame().headers(1, Arrays.asList("c", "c3po"));
|
||||
peer.acceptFrame(); // RST_STREAM
|
||||
peer.sendFrame().ping(0, 1);
|
||||
peer.sendFrame().ping(1);
|
||||
peer.play();
|
||||
|
||||
// play it back
|
||||
|
||||
Reference in New Issue
Block a user