Search in sources :

Example 6 with Utf8StringBuilder

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

the class I2PRequestLog method log.

/* ------------------------------------------------------------ */
public void log(Request request, Response response) {
    if (!isStarted())
        return;
    try {
        if (_ignorePathMap != null && _ignorePathMap.getMatch(request.getRequestURI()) != null)
            return;
        if (_fileOut == null)
            return;
        Utf8StringBuilder u8buf;
        StringBuilder buf;
        synchronized (_writer) {
            int size = _buffers.size();
            u8buf = size == 0 ? new Utf8StringBuilder(160) : _buffers.remove(size - 1);
            buf = u8buf.getStringBuilder();
        }
        synchronized (// for efficiency until we can use StringBuilder
        buf) {
            if (_logServer) {
                buf.append(request.getServerName());
                buf.append(' ');
            }
            String addr = null;
            if (_preferProxiedForAddress) {
                addr = request.getHeader("X-Forwarded-For");
            }
            if (addr == null) {
                if (_b64) {
                    addr = request.getHeader("X-I2P-DestHash");
                    if (addr != null)
                        addr += ".i2p";
                } else {
                    // 52chars.b32.i2p
                    addr = request.getHeader("X-I2P-DestB32");
                }
                if (addr == null)
                    addr = request.getRemoteAddr();
            }
            buf.append(addr);
            buf.append(" - ");
            String user = request.getRemoteUser();
            buf.append((user == null) ? " - " : user);
            buf.append(" [");
            if (_logDateCache != null)
                buf.append(_logDateCache.format(request.getTimeStamp()));
            else
                // buf.append(request.getTimeStampBuffer().toString());
                // TODO SimpleDateFormat or something
                buf.append(request.getTimeStamp());
            buf.append("] \"");
            buf.append(request.getMethod());
            buf.append(' ');
            request.getUri().writeTo(u8buf);
            buf.append(' ');
            buf.append(request.getProtocol());
            buf.append("\" ");
            int status = response.getStatus();
            if (status <= 0)
                status = 404;
            buf.append((char) ('0' + ((status / 100) % 10)));
            buf.append((char) ('0' + ((status / 10) % 10)));
            buf.append((char) ('0' + (status % 10)));
            long responseLength = response.getContentCount();
            // https://dev.eclipse.org/mhonarc/lists/jetty-dev/msg02267.html
            if (responseLength == 0 && status == 200 && !"HEAD".equals(request.getMethod()))
                responseLength = response.getLongContentLength();
            if (responseLength >= 0) {
                buf.append(' ');
                if (responseLength > 99999)
                    buf.append(Long.toString(responseLength));
                else {
                    if (responseLength > 9999)
                        buf.append((char) ('0' + ((responseLength / 10000) % 10)));
                    if (responseLength > 999)
                        buf.append((char) ('0' + ((responseLength / 1000) % 10)));
                    if (responseLength > 99)
                        buf.append((char) ('0' + ((responseLength / 100) % 10)));
                    if (responseLength > 9)
                        buf.append((char) ('0' + ((responseLength / 10) % 10)));
                    buf.append((char) ('0' + (responseLength) % 10));
                }
                buf.append(' ');
            } else
                buf.append(" - ");
        }
        if (!_extended && !_logCookies && !_logLatency) {
            synchronized (_writer) {
                buf.append(System.getProperty("line.separator", "\n"));
                int l = buf.length();
                if (l > _copy.length)
                    l = _copy.length;
                buf.getChars(0, l, _copy, 0);
                _writer.write(_copy, 0, l);
                _writer.flush();
                u8buf.reset();
                _buffers.add(u8buf);
            }
        } else {
            synchronized (_writer) {
                int l = buf.length();
                if (l > _copy.length)
                    l = _copy.length;
                buf.getChars(0, l, _copy, 0);
                _writer.write(_copy, 0, l);
                u8buf.reset();
                _buffers.add(u8buf);
                // TODO do outside synchronized scope
                if (_extended)
                    logExtended(request, response, _writer);
                // TODO do outside synchronized scope
                if (_logCookies) {
                    Cookie[] cookies = request.getCookies();
                    if (cookies == null || cookies.length == 0)
                        _writer.write(" -");
                    else {
                        _writer.write(" \"");
                        for (int i = 0; i < cookies.length; i++) {
                            if (i != 0)
                                _writer.write(';');
                            _writer.write(cookies[i].getName());
                            _writer.write('=');
                            _writer.write(cookies[i].getValue());
                        }
                        _writer.write('\"');
                    }
                }
                if (_logLatency) {
                    _writer.write(' ');
                    _writer.write(Long.toString(System.currentTimeMillis() - request.getTimeStamp()));
                }
                _writer.write(System.getProperty("line.separator", "\n"));
                _writer.flush();
            }
        }
    } catch (IOException e) {
        Log.getLogger((String) null).warn(e);
    }
}
Also used : Cookie(javax.servlet.http.Cookie) Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) IOException(java.io.IOException)

Example 7 with Utf8StringBuilder

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

the class HTTP2CServerTest method testHTTP_1_1_Upgrade.

@Test
public void testHTTP_1_1_Upgrade() throws Exception {
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        output.write(("" + "GET /one HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: something, else, upgrade, HTTP2-Settings\r\n" + "Upgrade: h2c\r\n" + "HTTP2-Settings: \r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
        output.flush();
        InputStream input = client.getInputStream();
        Utf8StringBuilder upgrade = new Utf8StringBuilder();
        int crlfs = 0;
        while (true) {
            int read = input.read();
            if (read == '\r' || read == '\n')
                ++crlfs;
            else
                crlfs = 0;
            upgrade.append((byte) read);
            if (crlfs == 4)
                break;
        }
        assertTrue(upgrade.toString().startsWith("HTTP/1.1 101 "));
        byteBufferPool = new MappedByteBufferPool();
        generator = new Generator(byteBufferPool);
        final AtomicReference<HeadersFrame> headersRef = new AtomicReference<>();
        final AtomicReference<DataFrame> dataRef = new AtomicReference<>();
        final AtomicReference<CountDownLatch> latchRef = new AtomicReference<>(new CountDownLatch(2));
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                headersRef.set(frame);
                latchRef.get().countDown();
            }

            @Override
            public void onData(DataFrame frame) {
                dataRef.set(frame);
                latchRef.get().countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latchRef.get().await(5, TimeUnit.SECONDS));
        HeadersFrame response = headersRef.get();
        Assert.assertNotNull(response);
        MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
        DataFrame responseData = dataRef.get();
        Assert.assertNotNull(responseData);
        String content = BufferUtil.toString(responseData.getData());
        // The upgrade request is seen as HTTP/1.1.
        assertThat(content, containsString("Hello from Jetty using HTTP/1.1"));
        assertThat(content, containsString("uri=/one"));
        // Send a HTTP/2 request.
        headersRef.set(null);
        dataRef.set(null);
        latchRef.set(new CountDownLatch(2));
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        generator.control(lease, new SettingsFrame(new HashMap<>(), false));
        MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, new HostPortHttpField("localhost:" + connector.getLocalPort()), "/two", HttpVersion.HTTP_2, new HttpFields());
        generator.control(lease, new HeadersFrame(3, metaData, null, true));
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        output.flush();
        parseResponse(client, parser);
        Assert.assertTrue(latchRef.get().await(5, TimeUnit.SECONDS));
        response = headersRef.get();
        Assert.assertNotNull(response);
        responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
        responseData = dataRef.get();
        Assert.assertNotNull(responseData);
        content = BufferUtil.toString(responseData.getData());
        assertThat(content, containsString("Hello from Jetty using HTTP/2.0"));
        assertThat(content, containsString("uri=/two"));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) InputStream(java.io.InputStream) Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) Parser(org.eclipse.jetty.http2.parser.Parser) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

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