SPDY: code cleanup

This commit is contained in:
Jeff Pinner 2013-10-17 08:21:35 -07:00 committed by Trustin Lee
parent db59b38db3
commit 6447777ac2
5 changed files with 22 additions and 153 deletions

View File

@ -235,35 +235,6 @@ final class SpdyCodecUtil {
0x2c, 0x65, 0x6e, 0x71, 0x3d, 0x30, 0x2e // - e n q - 0 - 0x2c, 0x65, 0x6e, 0x71, 0x3d, 0x30, 0x2e // - e n q - 0 -
}; };
private static final String SPDY2_DICT_S =
"optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-" +
"languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi" +
"f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser" +
"-agent10010120020120220320420520630030130230330430530630740040140240340440" +
"5406407408409410411412413414415416417500501502503504505accept-rangesageeta" +
"glocationproxy-authenticatepublicretry-afterservervarywarningwww-authentic" +
"ateallowcontent-basecontent-encodingcache-controlconnectiondatetrailertran" +
"sfer-encodingupgradeviawarningcontent-languagecontent-lengthcontent-locati" +
"oncontent-md5content-rangecontent-typeetagexpireslast-modifiedset-cookieMo" +
"ndayTuesdayWednesdayThursdayFridaySaturdaySundayJanFebMarAprMayJunJulAugSe" +
"pOctNovDecchunkedtext/htmlimage/pngimage/jpgimage/gifapplication/xmlapplic" +
"ation/xhtmltext/plainpublicmax-agecharset=iso-8859-1utf-8gzipdeflateHTTP/1" +
".1statusversionurl ";
static final byte[] SPDY2_DICT;
static {
byte[] SPDY2_DICT_;
try {
SPDY2_DICT_ = SPDY2_DICT_S.getBytes(CharsetUtil.US_ASCII);
// dictionary is null terminated
SPDY2_DICT_[SPDY2_DICT_.length - 1] = 0;
} catch (Exception e) {
SPDY2_DICT_ = new byte[1];
}
SPDY2_DICT = SPDY2_DICT_;
}
private SpdyCodecUtil() { private SpdyCodecUtil() {
} }

View File

@ -51,12 +51,8 @@ class SpdyHeaderBlockJZlibEncoder extends SpdyHeaderBlockRawEncoder {
if (resultCode != JZlib.Z_OK) { if (resultCode != JZlib.Z_OK) {
throw new CompressionException( throw new CompressionException(
"failed to initialize an SPDY header block deflater: " + resultCode); "failed to initialize an SPDY header block deflater: " + resultCode);
} else {
if (version.getVersion() < 3) {
resultCode = z.deflateSetDictionary(SPDY2_DICT, SPDY2_DICT.length);
} else { } else {
resultCode = z.deflateSetDictionary(SPDY_DICT, SPDY_DICT.length); resultCode = z.deflateSetDictionary(SPDY_DICT, SPDY_DICT.length);
}
if (resultCode != JZlib.Z_OK) { if (resultCode != JZlib.Z_OK) {
throw new CompressionException( throw new CompressionException(
"failed to set the SPDY dictionary: " + resultCode); "failed to set the SPDY dictionary: " + resultCode);

View File

@ -25,7 +25,6 @@ import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
class SpdyHeaderBlockZlibDecoder extends SpdyHeaderBlockRawDecoder { class SpdyHeaderBlockZlibDecoder extends SpdyHeaderBlockRawDecoder {
private final int version;
private final byte[] out = new byte[8192]; private final byte[] out = new byte[8192];
private final Inflater decompressor = new Inflater(); private final Inflater decompressor = new Inflater();
@ -33,7 +32,6 @@ class SpdyHeaderBlockZlibDecoder extends SpdyHeaderBlockRawDecoder {
public SpdyHeaderBlockZlibDecoder(SpdyVersion version, int maxHeaderSize) { public SpdyHeaderBlockZlibDecoder(SpdyVersion version, int maxHeaderSize) {
super(version, maxHeaderSize); super(version, maxHeaderSize);
this.version = version.getVersion();
} }
@Override @Override
@ -59,11 +57,7 @@ class SpdyHeaderBlockZlibDecoder extends SpdyHeaderBlockRawDecoder {
try { try {
int numBytes = decompressor.inflate(out); int numBytes = decompressor.inflate(out);
if (numBytes == 0 && decompressor.needsDictionary()) { if (numBytes == 0 && decompressor.needsDictionary()) {
if (version < 3) {
decompressor.setDictionary(SPDY2_DICT);
} else {
decompressor.setDictionary(SPDY_DICT); decompressor.setDictionary(SPDY_DICT);
}
numBytes = decompressor.inflate(out); numBytes = decompressor.inflate(out);
} }
if (frame != null) { if (frame != null) {

View File

@ -37,12 +37,8 @@ class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder {
"compressionLevel: " + compressionLevel + " (expected: 0-9)"); "compressionLevel: " + compressionLevel + " (expected: 0-9)");
} }
compressor = new Deflater(compressionLevel); compressor = new Deflater(compressionLevel);
if (version.getVersion() < 3) {
compressor.setDictionary(SPDY2_DICT);
} else {
compressor.setDictionary(SPDY_DICT); compressor.setDictionary(SPDY_DICT);
} }
}
private void setInput(ByteBuf decompressed) { private void setInput(ByteBuf decompressed) {
byte[] in = new byte[decompressed.readableBytes()]; byte[] in = new byte[decompressed.readableBytes()];

View File

@ -131,34 +131,6 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
private HttpNames() { } private HttpNames() { }
} }
/**
* SPDY/2 HTTP header names
*/
public static final class Spdy2HttpNames {
/**
* {@code "method"}
*/
public static final String METHOD = "method";
/**
* {@code "scheme"}
*/
public static final String SCHEME = "scheme";
/**
* {@code "status"}
*/
public static final String STATUS = "status";
/**
* {@code "url"}
*/
public static final String URL = "url";
/**
* {@code "version"}
*/
public static final String VERSION = "version";
private Spdy2HttpNames() { }
}
/** /**
* Returns the header value with the specified header name. If there are * Returns the header value with the specified header name. If there are
* more than one header value for the specified header name, the first * more than one header value for the specified header name, the first
@ -234,23 +206,15 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
* Removes the HTTP method header. * Removes the HTTP method header.
*/ */
public static void removeMethod(int spdyVersion, SpdyHeadersFrame frame) { public static void removeMethod(int spdyVersion, SpdyHeadersFrame frame) {
if (spdyVersion < 3) {
frame.headers().remove(Spdy2HttpNames.METHOD);
} else {
frame.headers().remove(HttpNames.METHOD); frame.headers().remove(HttpNames.METHOD);
} }
}
/** /**
* Returns the {@link HttpMethod} represented by the HTTP method header. * Returns the {@link HttpMethod} represented by the HTTP method header.
*/ */
public static HttpMethod getMethod(int spdyVersion, SpdyHeadersFrame frame) { public static HttpMethod getMethod(int spdyVersion, SpdyHeadersFrame frame) {
try { try {
if (spdyVersion < 3) {
return HttpMethod.valueOf(frame.headers().get(Spdy2HttpNames.METHOD));
} else {
return HttpMethod.valueOf(frame.headers().get(HttpNames.METHOD)); return HttpMethod.valueOf(frame.headers().get(HttpNames.METHOD));
}
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
@ -260,68 +224,43 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
* Sets the HTTP method header. * Sets the HTTP method header.
*/ */
public static void setMethod(int spdyVersion, SpdyHeadersFrame frame, HttpMethod method) { public static void setMethod(int spdyVersion, SpdyHeadersFrame frame, HttpMethod method) {
if (spdyVersion < 3) {
frame.headers().set(Spdy2HttpNames.METHOD, method.name());
} else {
frame.headers().set(HttpNames.METHOD, method.name()); frame.headers().set(HttpNames.METHOD, method.name());
} }
}
/** /**
* Removes the URL scheme header. * Removes the URL scheme header.
*/ */
public static void removeScheme(int spdyVersion, SpdyHeadersFrame frame) { public static void removeScheme(int spdyVersion, SpdyHeadersFrame frame) {
if (spdyVersion < 2) {
frame.headers().remove(Spdy2HttpNames.SCHEME);
} else {
frame.headers().remove(HttpNames.SCHEME); frame.headers().remove(HttpNames.SCHEME);
} }
}
/** /**
* Returns the value of the URL scheme header. * Returns the value of the URL scheme header.
*/ */
public static String getScheme(int spdyVersion, SpdyHeadersFrame frame) { public static String getScheme(int spdyVersion, SpdyHeadersFrame frame) {
if (spdyVersion < 3) {
return frame.headers().get(Spdy2HttpNames.SCHEME);
} else {
return frame.headers().get(HttpNames.SCHEME); return frame.headers().get(HttpNames.SCHEME);
} }
}
/** /**
* Sets the URL scheme header. * Sets the URL scheme header.
*/ */
public static void setScheme(int spdyVersion, SpdyHeadersFrame frame, String scheme) { public static void setScheme(int spdyVersion, SpdyHeadersFrame frame, String scheme) {
if (spdyVersion < 3) {
frame.headers().set(Spdy2HttpNames.SCHEME, scheme);
} else {
frame.headers().set(HttpNames.SCHEME, scheme); frame.headers().set(HttpNames.SCHEME, scheme);
} }
}
/** /**
* Removes the HTTP response status header. * Removes the HTTP response status header.
*/ */
public static void removeStatus(int spdyVersion, SpdyHeadersFrame frame) { public static void removeStatus(int spdyVersion, SpdyHeadersFrame frame) {
if (spdyVersion < 3) {
frame.headers().remove(Spdy2HttpNames.STATUS);
} else {
frame.headers().remove(HttpNames.STATUS); frame.headers().remove(HttpNames.STATUS);
} }
}
/** /**
* Returns the {@link HttpResponseStatus} represented by the HTTP response status header. * Returns the {@link HttpResponseStatus} represented by the HTTP response status header.
*/ */
public static HttpResponseStatus getStatus(int spdyVersion, SpdyHeadersFrame frame) { public static HttpResponseStatus getStatus(int spdyVersion, SpdyHeadersFrame frame) {
try { try {
String status; String status = frame.headers().get(HttpNames.STATUS);
if (spdyVersion < 3) {
status = frame.headers().get(Spdy2HttpNames.STATUS);
} else {
status = frame.headers().get(HttpNames.STATUS);
}
int space = status.indexOf(' '); int space = status.indexOf(' ');
if (space == -1) { if (space == -1) {
return HttpResponseStatus.valueOf(Integer.parseInt(status)); return HttpResponseStatus.valueOf(Integer.parseInt(status));
@ -344,67 +283,43 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
* Sets the HTTP response status header. * Sets the HTTP response status header.
*/ */
public static void setStatus(int spdyVersion, SpdyHeadersFrame frame, HttpResponseStatus status) { public static void setStatus(int spdyVersion, SpdyHeadersFrame frame, HttpResponseStatus status) {
if (spdyVersion < 3) {
frame.headers().set(Spdy2HttpNames.STATUS, status.toString());
} else {
frame.headers().set(HttpNames.STATUS, status.toString()); frame.headers().set(HttpNames.STATUS, status.toString());
} }
}
/** /**
* Removes the URL path header. * Removes the URL path header.
*/ */
public static void removeUrl(int spdyVersion, SpdyHeadersFrame frame) { public static void removeUrl(int spdyVersion, SpdyHeadersFrame frame) {
if (spdyVersion < 3) {
frame.headers().remove(Spdy2HttpNames.URL);
} else {
frame.headers().remove(HttpNames.PATH); frame.headers().remove(HttpNames.PATH);
} }
}
/** /**
* Returns the value of the URL path header. * Returns the value of the URL path header.
*/ */
public static String getUrl(int spdyVersion, SpdyHeadersFrame frame) { public static String getUrl(int spdyVersion, SpdyHeadersFrame frame) {
if (spdyVersion < 3) {
return frame.headers().get(Spdy2HttpNames.URL);
} else {
return frame.headers().get(HttpNames.PATH); return frame.headers().get(HttpNames.PATH);
} }
}
/** /**
* Sets the URL path header. * Sets the URL path header.
*/ */
public static void setUrl(int spdyVersion, SpdyHeadersFrame frame, String path) { public static void setUrl(int spdyVersion, SpdyHeadersFrame frame, String path) {
if (spdyVersion < 3) {
frame.headers().set(Spdy2HttpNames.URL, path);
} else {
frame.headers().set(HttpNames.PATH, path); frame.headers().set(HttpNames.PATH, path);
} }
}
/** /**
* Removes the HTTP version header. * Removes the HTTP version header.
*/ */
public static void removeVersion(int spdyVersion, SpdyHeadersFrame frame) { public static void removeVersion(int spdyVersion, SpdyHeadersFrame frame) {
if (spdyVersion < 3) {
frame.headers().remove(Spdy2HttpNames.VERSION);
} else {
frame.headers().remove(HttpNames.VERSION); frame.headers().remove(HttpNames.VERSION);
} }
}
/** /**
* Returns the {@link HttpVersion} represented by the HTTP version header. * Returns the {@link HttpVersion} represented by the HTTP version header.
*/ */
public static HttpVersion getVersion(int spdyVersion, SpdyHeadersFrame frame) { public static HttpVersion getVersion(int spdyVersion, SpdyHeadersFrame frame) {
try { try {
if (spdyVersion < 3) {
return HttpVersion.valueOf(frame.headers().get(Spdy2HttpNames.VERSION));
} else {
return HttpVersion.valueOf(frame.headers().get(HttpNames.VERSION)); return HttpVersion.valueOf(frame.headers().get(HttpNames.VERSION));
}
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
@ -414,12 +329,9 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
* Sets the HTTP version header. * Sets the HTTP version header.
*/ */
public static void setVersion(int spdyVersion, SpdyHeadersFrame frame, HttpVersion httpVersion) { public static void setVersion(int spdyVersion, SpdyHeadersFrame frame, HttpVersion httpVersion) {
if (spdyVersion < 3) {
frame.headers().set(Spdy2HttpNames.VERSION, httpVersion.text());
} else {
frame.headers().set(HttpNames.VERSION, httpVersion.text()); frame.headers().set(HttpNames.VERSION, httpVersion.text());
} }
}
@Override @Override
public Iterator<Map.Entry<String, String>> iterator() { public Iterator<Map.Entry<String, String>> iterator() {
return entries().iterator(); return entries().iterator();