private void encodeHeaders(Buffer buffer) { if (headers != null && !headers.isEmpty()) { int headersLengthPos = buffer.length(); buffer.appendInt(0); buffer.appendInt(headers.size()); List<Map.Entry<String, String>> entries = headers.entries(); for (Map.Entry<String, String> entry : entries) { writeString(buffer, entry.getKey()); writeString(buffer, entry.getValue()); } int headersEndPos = buffer.length(); buffer.setInt(headersLengthPos, headersEndPos - headersLengthPos); } else { buffer.appendInt(4); } }
void handleChunk(Buffer data) { synchronized (conn) { if (paused) { if (pausedChunk == null) { pausedChunk = data.copy(); } else { pausedChunk.appendBuffer(data); } } else { request.dataReceived(); if (pausedChunk != null) { data = pausedChunk.appendBuffer(data); pausedChunk = null; } bytesRead += data.length(); if (dataHandler != null) { try { dataHandler.handle(data); } catch (Throwable t) { handleException(t); } } } } }
public Buffer encodeToWire() { int length = 1024; // TODO make this configurable Buffer buffer = Buffer.buffer(length); buffer.appendInt(0); buffer.appendByte(WIRE_PROTOCOL_VERSION); byte systemCodecID = messageCodec.systemCodecID(); buffer.appendByte(systemCodecID); if (systemCodecID == -1) { // User codec writeString(buffer, messageCodec.name()); } buffer.appendByte(send ? (byte) 0 : (byte) 1); writeString(buffer, address); if (replyAddress != null) { writeString(buffer, replyAddress); } else { buffer.appendInt(0); } buffer.appendInt(sender.port); writeString(buffer, sender.host); encodeHeaders(buffer); writeBody(buffer); buffer.setInt(0, buffer.length() - 4); return buffer; }
private void writeToTarget(String sendPacket, String cmd) { logger.debug(name_log + "writeToDCU(), Command: " + cmd); // Writing Data to The Socket Buffer outBuffer = Buffer.buffer(); outBuffer.setString(0, sendPacket); logger.info(name_log + "SEND[" + outBuffer.length() + "]> \n" + outBuffer.toString() + "\n"); netSocket.write(outBuffer); }
@Override public void end(Buffer chunk) { synchronized (getLock()) { checkComplete(); checkResponseHandler(); if (!chunked && !contentLengthSet()) { headers().set(CONTENT_LENGTH, String.valueOf(chunk.length())); } write(chunk.getByteBuf(), true); } }
private void testValidSubProtocol(WebsocketVersion version) throws Exception { String path = "/some/path"; String subProtocol = "myprotocol"; Buffer buff = Buffer.buffer("AAA"); server = vertx .createHttpServer( new HttpServerOptions() .setPort(HttpTestBase.DEFAULT_HTTP_PORT) .setWebsocketSubProtocol(subProtocol)) .websocketHandler( ws -> { assertEquals(path, ws.path()); ws.writeFrame(WebSocketFrame.binaryFrame(buff, true)); }); server.listen( ar -> { assertTrue(ar.succeeded()); client.connectWebsocket( HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, subProtocol, ws -> { final Buffer received = Buffer.buffer(); ws.handler( data -> { received.appendBuffer(data); if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete(); } }); }); }); await(); }
private void testWSWriteStream(WebsocketVersion version) throws Exception { String path = "/some/path"; String query = "foo=bar&wibble=eek"; String uri = path + "?" + query; server = vertx .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)) .websocketHandler( ws -> { assertEquals(uri, ws.uri()); assertEquals(path, ws.path()); assertEquals(query, ws.query()); assertEquals("Upgrade", ws.headers().get("Connection")); ws.handler(data -> ws.write(data)); }); server.listen( ar -> { assertTrue(ar.succeeded()); int bsize = 100; int sends = 10; client.connectWebsocket( HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path + "?" + query, null, version, ws -> { final Buffer received = Buffer.buffer(); ws.handler( data -> { received.appendBuffer(data); if (received.length() == bsize * sends) { ws.close(); testComplete(); } }); final Buffer sent = Buffer.buffer(); for (int i = 0; i < sends; i++) { Buffer buff = Buffer.buffer(TestUtils.randomByteArray(bsize)); ws.write(buff); sent.appendBuffer(buff); } }); }); await(); }
synchronized void handleDataReceived(Buffer data) { checkContext(); if (paused) { if (pendingData == null) { pendingData = data.copy(); } else { pendingData.appendBuffer(data); } return; } if (pendingData != null) { data = pendingData.appendBuffer(data); } reportBytesRead(data.length()); if (dataHandler != null) { dataHandler.handle(data); } }
private void testTLS( KS clientCert, TS clientTrust, KS serverCert, TS serverTrust, boolean requireClientAuth, boolean serverUsesCrl, boolean clientTrustAll, boolean clientUsesCrl, boolean shouldPass, String... enabledCipherSuites) throws Exception { HttpClientOptions options = new HttpClientOptions(); options.setSsl(true); if (clientTrustAll) { options.setTrustAll(true); } if (clientUsesCrl) { options.addCrlPath(findFileOnClasspath("tls/ca/crl.pem")); } options.setTrustStoreOptions(getClientTrustOptions(clientTrust)); options.setKeyStoreOptions(getClientCertOptions(clientCert)); for (String suite : enabledCipherSuites) { options.addEnabledCipherSuite(suite); } client = vertx.createHttpClient(options); HttpServerOptions serverOptions = new HttpServerOptions(); serverOptions.setSsl(true); serverOptions.setTrustStoreOptions(getServerTrustOptions(serverTrust)); serverOptions.setKeyStoreOptions(getServerCertOptions(serverCert)); if (requireClientAuth) { serverOptions.setClientAuthRequired(true); } if (serverUsesCrl) { serverOptions.addCrlPath(findFileOnClasspath("tls/ca/crl.pem")); } for (String suite : enabledCipherSuites) { serverOptions.addEnabledCipherSuite(suite); } server = vertx.createHttpServer(serverOptions.setPort(4043)); server.websocketHandler( ws -> { ws.handler(ws::write); }); server.listen( ar -> { assertTrue(ar.succeeded()); client .websocket(4043, HttpTestBase.DEFAULT_HTTP_HOST, "/") .exceptionHandler( t -> { if (shouldPass) { t.printStackTrace(); fail("Should not throw exception"); } else { testComplete(); } }) .handler( ws -> { int size = 100; Buffer received = Buffer.buffer(); ws.handler( data -> { received.appendBuffer(data); if (received.length() == size) { ws.close(); testComplete(); } }); Buffer buff = Buffer.buffer(TestUtils.randomByteArray(size)); ws.writeFrame(WebSocketFrame.binaryFrame(buff, true)); }); }); await(); }