public void readFromWire(Buffer buffer, CodecManager codecManager) { int pos = 0; // Overall Length already read when passed in here byte protocolVersion = buffer.getByte(pos); if (protocolVersion > WIRE_PROTOCOL_VERSION) { throw new IllegalStateException( "Invalid wire protocol version " + protocolVersion + " should be <= " + WIRE_PROTOCOL_VERSION); } pos++; byte systemCodecCode = buffer.getByte(pos); pos++; if (systemCodecCode == -1) { // User codec int length = buffer.getInt(pos); pos += 4; byte[] bytes = buffer.getBytes(pos, pos + length); String codecName = new String(bytes, CharsetUtil.UTF_8); messageCodec = codecManager.getCodec(codecName); if (messageCodec == null) { throw new IllegalStateException("No message codec registered with name " + codecName); } pos += length; } else { messageCodec = codecManager.systemCodecs()[systemCodecCode]; } byte bsend = buffer.getByte(pos); send = bsend == 0; pos++; int length = buffer.getInt(pos); pos += 4; byte[] bytes = buffer.getBytes(pos, pos + length); address = new String(bytes, CharsetUtil.UTF_8); pos += length; length = buffer.getInt(pos); pos += 4; if (length != 0) { bytes = buffer.getBytes(pos, pos + length); replyAddress = new String(bytes, CharsetUtil.UTF_8); pos += length; } int senderPort = buffer.getInt(pos); pos += 4; length = buffer.getInt(pos); pos += 4; bytes = buffer.getBytes(pos, pos + length); String senderHost = new String(bytes, CharsetUtil.UTF_8); pos += length; headersPos = pos; int headersLength = buffer.getInt(pos); pos += headersLength; bodyPos = pos; sender = new ServerID(senderPort, senderHost); wireBuffer = buffer; fromWire = true; }
private void testWriteMessage(int size, WebsocketVersion version) { String path = "/some/path"; byte[] expected = TestUtils.randomByteArray(size); server = vertx .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)) .websocketHandler( ws -> { ws.writeMessage(Buffer.buffer(expected)); ws.close(); }); server.listen( ar -> { assertTrue(ar.succeeded()); client.connectWebsocket( HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, ws -> { Buffer actual = Buffer.buffer(); ws.handler(actual::appendBuffer); ws.closeHandler( v -> { assertArrayEquals(expected, actual.getBytes()); testComplete(); }); }); }); await(); }
@Override public String decodeFromWire(int pos, Buffer buffer) { int length = buffer.getInt(pos); pos += 4; byte[] bytes = buffer.getBytes(pos, pos + length); return new String(bytes, CharsetUtil.UTF_8); }
@Override public User decodeFromWire(int pos, Buffer buffer) { int length = buffer.getInt(pos); pos += 4; byte[] encoded = buffer.getBytes(pos, pos + length); String str = new String(encoded, CharsetUtil.UTF_8); return JsonUtils.decode(str, User.class); }
private void decodeHeaders() { int length = wireBuffer.getInt(headersPos); if (length != 4) { headersPos += 4; int numHeaders = wireBuffer.getInt(headersPos); headersPos += 4; headers = new CaseInsensitiveHeaders(); for (int i = 0; i < numHeaders; i++) { int keyLength = wireBuffer.getInt(headersPos); headersPos += 4; byte[] bytes = wireBuffer.getBytes(headersPos, headersPos + keyLength); String key = new String(bytes, CharsetUtil.UTF_8); headersPos += keyLength; int valLength = wireBuffer.getInt(headersPos); headersPos += 4; bytes = wireBuffer.getBytes(headersPos, headersPos + valLength); String val = new String(bytes, CharsetUtil.UTF_8); headersPos += valLength; headers.add(key, val); } } headersPos = 0; }
public HttpRequest createRequest( final HttpServerRequest request, final Buffer buffer, final CopyOnWriteArrayList<HttpResponseDecorator> decorators, final HttpResponseCreator httpResponseCreator) { final MultiMap<String, String> headers = request.headers().size() == 0 ? MultiMap.empty() : new MultiMapWrapper(request.headers()); final String contentType = request.headers().get("Content-Type"); final byte[] body = HttpContentTypes.isFormContentType(contentType) || buffer == null ? new byte[0] : buffer.getBytes(); final MultiMap<String, String> params = buildParams(request, contentType); final HttpRequestBuilder httpRequestBuilder = HttpRequestBuilder.httpRequestBuilder(); final String requestPath = request.path(); httpRequestBuilder .setId(requestId.incrementAndGet()) .setUri(requestPath) .setMethod(request.method().toString()) .setParams(params) .setBodyBytes(body) .setRemoteAddress(request.remoteAddress().toString()) .setResponse( createResponse( requestPath, headers, params, request.response(), decorators, httpResponseCreator)) .setTimestamp(time == 0L ? Timer.timer().now() : time) .setHeaders(headers); return httpRequestBuilder.build(); }
@Override public void handle(Buffer buffer) { tokenizer.process(buffer.getBytes()); }