Search in sources :

Example 46 with ByteString

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);
}
Also used : Buffer(okio.Buffer) ByteString(okio.ByteString) Test(org.junit.Test)

Example 47 with ByteString

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);
            }
        }
    });
}
Also used : Buffer(okio.Buffer) ByteString(okio.ByteString) IOException(java.io.IOException) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 48 with ByteString

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);
        }
    });
}
Also used : ByteString(okio.ByteString) Test(org.junit.Test)

Example 49 with ByteString

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());
}
Also used : StatusLine(okhttp3.internal.http.StatusLine) ProtocolException(java.net.ProtocolException) Headers(okhttp3.Headers) ByteString(okio.ByteString) ByteString(okio.ByteString)

Example 50 with ByteString

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;
}
Also used : Headers(okhttp3.Headers) ByteString(okio.ByteString) ArrayList(java.util.ArrayList) ByteString(okio.ByteString)

Aggregations

ByteString (okio.ByteString)59 Test (org.junit.Test)37 Buffer (okio.Buffer)26 MockResponse (okhttp3.mockwebserver.MockResponse)11 ProtocolException (java.net.ProtocolException)5 IOException (java.io.IOException)4 OneField (com.squareup.wire.protos.edgecases.OneField)3 EOFException (java.io.EOFException)3 BufferedSink (okio.BufferedSink)3 BufferedSource (okio.BufferedSource)3 AllTypes (com.squareup.wire.protos.alltypes.AllTypes)2 Person (com.squareup.wire.protos.person.Person)2 ArrayList (java.util.ArrayList)2 Headers (okhttp3.Headers)2 Request (okhttp3.Request)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 GzipSink (okio.GzipSink)2 Ignore (org.junit.Ignore)2 Phone (retrofit2.converter.protobuf.PhoneProtos.Phone)2 GsonBuilder (com.google.gson.GsonBuilder)1