Search in sources :

Example 1 with Utf8StringBuilder

use of org.eclipse.jetty.util.Utf8StringBuilder in project jetty.project by eclipse.

the class ForwardProxyServerTest method testRequestTarget.

@Test
public void testRequestTarget() throws Exception {
    startServer(new AbstractConnectionFactory("http/1.1") {

        @Override
        public Connection newConnection(Connector connector, EndPoint endPoint) {
            return new AbstractConnection(endPoint, connector.getExecutor()) {

                @Override
                public void onOpen() {
                    super.onOpen();
                    fillInterested();
                }

                @Override
                public void onFillable() {
                    try {
                        // When using TLS, multiple reads are required.
                        ByteBuffer buffer = BufferUtil.allocate(1024);
                        int filled = 0;
                        while (filled == 0) filled = getEndPoint().fill(buffer);
                        Utf8StringBuilder builder = new Utf8StringBuilder();
                        builder.append(buffer);
                        String request = builder.toString();
                        // ProxyServlet will receive an absolute URI from
                        // the client, and convert it to a relative URI.
                        // The ConnectHandler won't modify what the client
                        // sent, which must be a relative URI.
                        Assert.assertThat(request.length(), Matchers.greaterThan(0));
                        if (serverSslContextFactory == null)
                            Assert.assertFalse(request.contains("http://"));
                        else
                            Assert.assertFalse(request.contains("https://"));
                        String response = "" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n";
                        getEndPoint().write(Callback.NOOP, ByteBuffer.wrap(response.getBytes(StandardCharsets.UTF_8)));
                    } catch (Throwable x) {
                        x.printStackTrace();
                        close();
                    }
                }
            };
        }
    });
    startProxy();
    HttpClient httpClient = new HttpClient(newSslContextFactory());
    httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
    httpClient.start();
    try {
        ContentResponse response = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(serverSslContextFactory == null ? "http" : "https").method(HttpMethod.GET).path("/test").send();
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    } finally {
        httpClient.stop();
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) AbstractConnectionFactory(org.eclipse.jetty.server.AbstractConnectionFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) HttpClient(org.eclipse.jetty.client.HttpClient) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.io.Connection) EndPoint(org.eclipse.jetty.io.EndPoint) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with Utf8StringBuilder

use of org.eclipse.jetty.util.Utf8StringBuilder in project jetty.project by eclipse.

the class HttpWriterTest method testNotCESU8.

@Test
public void testNotCESU8() throws Exception {
    HttpWriter _writer = new Utf8HttpWriter(_httpOut);
    String data = "xxx𐐀xxx";
    _writer.write(data);
    assertEquals("787878F0909080787878", TypeUtil.toHexString(BufferUtil.toArray(_bytes)));
    assertArrayEquals(data.getBytes(StandardCharsets.UTF_8), BufferUtil.toArray(_bytes));
    assertEquals(3 + 4 + 3, _bytes.remaining());
    Utf8StringBuilder buf = new Utf8StringBuilder();
    buf.append(BufferUtil.toArray(_bytes), 0, _bytes.remaining());
    assertEquals(data, buf.toString());
}
Also used : Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) Test(org.junit.Test)

Example 3 with Utf8StringBuilder

use of org.eclipse.jetty.util.Utf8StringBuilder in project jetty.project by eclipse.

the class SniSslConnectionFactoryTest method response.

private String response(InputStream input) throws IOException {
    Utf8StringBuilder buffer = new Utf8StringBuilder();
    int crlfs = 0;
    while (true) {
        int read = input.read();
        Assert.assertTrue(read >= 0);
        buffer.append((byte) read);
        crlfs = (read == '\r' || read == '\n') ? crlfs + 1 : 0;
        if (crlfs == 4)
            break;
    }
    return buffer.toString();
}
Also used : Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder)

Example 4 with Utf8StringBuilder

use of org.eclipse.jetty.util.Utf8StringBuilder in project jetty.project by eclipse.

the class WebSocketServletRFCTest method testDetectBadUTF8.

@Test(expected = NotUtf8Exception.class)
public void testDetectBadUTF8() {
    byte[] buf = new byte[] { (byte) 0xC2, (byte) 0xC3 };
    Utf8StringBuilder utf = new Utf8StringBuilder();
    utf.append(buf, 0, buf.length);
}
Also used : Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) Test(org.junit.Test)

Example 5 with Utf8StringBuilder

use of org.eclipse.jetty.util.Utf8StringBuilder in project i2p.i2p by i2p.

the class I2PRequestLog method doStart.

/* ------------------------------------------------------------ */
protected void doStart() throws Exception {
    if (_logDateFormat != null) {
        _logDateCache = new DateCache(_logDateFormat, _logLocale, _logTimeZone);
    }
    if (_filename != null) {
        _fileOut = new RolloverFileOutputStream(_filename, _append, _retainDays, TimeZone.getTimeZone(_logTimeZone), _filenameDateFormat, null);
        _closeOut = true;
        Log.getLogger((String) null).info("Opened " + getDatedFilename());
    } else
        _fileOut = System.err;
    _out = _fileOut;
    if (_ignorePaths != null && _ignorePaths.length > 0) {
        _ignorePathMap = new PathMap<String>();
        for (int i = 0; i < _ignorePaths.length; i++) _ignorePathMap.put(_ignorePaths[i], _ignorePaths[i]);
    } else
        _ignorePathMap = null;
    _writer = new OutputStreamWriter(_out, "UTF-8");
    _buffers = new ArrayList<Utf8StringBuilder>();
    _copy = new char[1024];
    super.doStart();
}
Also used : Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) OutputStreamWriter(java.io.OutputStreamWriter) RolloverFileOutputStream(org.eclipse.jetty.util.RolloverFileOutputStream) DateCache(org.eclipse.jetty.util.DateCache)

Aggregations

Utf8StringBuilder (org.eclipse.jetty.util.Utf8StringBuilder)7 Test (org.junit.Test)4 ByteBuffer (java.nio.ByteBuffer)2 EndPoint (org.eclipse.jetty.io.EndPoint)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Socket (java.net.Socket)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Cookie (javax.servlet.http.Cookie)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 HostPortHttpField (org.eclipse.jetty.http.HostPortHttpField)1 HttpFields (org.eclipse.jetty.http.HttpFields)1 MetaData (org.eclipse.jetty.http.MetaData)1 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)1 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)1