use of okio.ByteString in project okhttp by square.
the class HpackTest method huffmanEncode.
@Test
public void huffmanEncode() throws IOException {
hpackWriter = new Hpack.Writer(4096, true, bytesOut);
hpackWriter.writeHeaders(headerEntries("foo", "bar"));
ByteString expected = new Buffer().writeByte(// Literal header, new name.
0x40).writeByte(// String literal is Huffman encoded (len = 2).
0x82).writeByte(// 'foo' Huffman encoded.
0x94).writeByte(0xE7).writeByte(// String literal not Huffman encoded (len = 3).
3).writeByte('b').writeByte('a').writeByte('r').readByteString();
ByteString actual = bytesOut.readByteString();
assertEquals(expected, actual);
}
use of okio.ByteString in project okhttp by square.
the class Http2Test method maxLengthDataFrame.
@Test
public void maxLengthDataFrame() throws IOException {
final byte[] expectedData = new byte[Http2.INITIAL_MAX_FRAME_SIZE];
Arrays.fill(expectedData, (byte) 2);
writeMedium(frame, expectedData.length);
frame.writeByte(Http2.TYPE_DATA);
frame.writeByte(Http2.FLAG_NONE);
frame.writeInt(expectedStreamId & 0x7fffffff);
frame.write(expectedData);
// Check writer sends the same bytes.
assertEquals(frame, sendDataFrame(new Buffer().write(expectedData)));
reader.nextFrame(false, new BaseTestHandler() {
@Override
public void data(boolean inFinished, int streamId, BufferedSource source, int length) throws IOException {
assertFalse(inFinished);
assertEquals(expectedStreamId, streamId);
assertEquals(Http2.INITIAL_MAX_FRAME_SIZE, length);
ByteString data = source.readByteString(length);
for (byte b : data.toByteArray()) {
assertEquals(2, b);
}
}
});
}
use of okio.ByteString in project okhttp by square.
the class Http2Test method goAwayWithDebugDataRoundTrip.
@Test
public void goAwayWithDebugDataRoundTrip() throws IOException {
final ErrorCode expectedError = ErrorCode.PROTOCOL_ERROR;
final ByteString expectedData = ByteString.encodeUtf8("abcdefgh");
// Compose the expected GOAWAY frame without debug data.
writeMedium(frame, 8 + expectedData.size());
frame.writeByte(Http2.TYPE_GOAWAY);
frame.writeByte(Http2.FLAG_NONE);
// connection-scope
frame.writeInt(0);
// never read any stream!
frame.writeInt(0);
frame.writeInt(expectedError.httpCode);
frame.write(expectedData.toByteArray());
// Check writer sends the same bytes.
assertEquals(frame, sendGoAway(0, expectedError, expectedData.toByteArray()));
reader.nextFrame(false, new BaseTestHandler() {
@Override
public void goAway(int lastGoodStreamId, ErrorCode errorCode, ByteString debugData) {
assertEquals(0, lastGoodStreamId);
assertEquals(expectedError, errorCode);
assertEquals(expectedData, debugData);
}
});
}
use of okio.ByteString in project okhttp by square.
the class Http2Codec method readHttp2HeadersList.
/** Returns headers for a name value block containing an HTTP/2 response. */
public static Response.Builder readHttp2HeadersList(List<Header> headerBlock) throws IOException {
StatusLine statusLine = null;
Headers.Builder headersBuilder = new Headers.Builder();
for (int i = 0, size = headerBlock.size(); i < size; i++) {
Header header = headerBlock.get(i);
// header blocks if the existing header block is a '100 Continue' intermediate response.
if (header == null) {
if (statusLine != null && statusLine.code == HTTP_CONTINUE) {
statusLine = null;
headersBuilder = new Headers.Builder();
}
continue;
}
ByteString name = header.name;
String value = header.value.utf8();
if (name.equals(RESPONSE_STATUS)) {
statusLine = StatusLine.parse("HTTP/1.1 " + value);
} else if (!HTTP_2_SKIPPED_RESPONSE_HEADERS.contains(name)) {
Internal.instance.addLenient(headersBuilder, name.utf8(), value);
}
}
if (statusLine == null)
throw new ProtocolException("Expected ':status' header not present");
return new Response.Builder().protocol(Protocol.HTTP_2).code(statusLine.code).message(statusLine.message).headers(headersBuilder.build());
}
use of okio.ByteString in project okhttp by square.
the class Http2Codec method http2HeadersList.
public static List<Header> http2HeadersList(Request request) {
Headers headers = request.headers();
List<Header> result = new ArrayList<>(headers.size() + 4);
result.add(new Header(TARGET_METHOD, request.method()));
result.add(new Header(TARGET_PATH, RequestLine.requestPath(request.url())));
String host = request.header("Host");
if (host != null) {
// Optional.
result.add(new Header(TARGET_AUTHORITY, host));
}
result.add(new Header(TARGET_SCHEME, request.url().scheme()));
for (int i = 0, size = headers.size(); i < size; i++) {
// header names must be lowercase.
ByteString name = ByteString.encodeUtf8(headers.name(i).toLowerCase(Locale.US));
if (!HTTP_2_SKIPPED_REQUEST_HEADERS.contains(name)) {
result.add(new Header(name, headers.value(i)));
}
}
return result;
}
Aggregations