Search in sources :

Example 1 with HttpField

use of org.eclipse.jetty.http.HttpField in project jetty.project by eclipse.

the class HttpSenderOverFCGI method sendHeaders.

@Override
protected void sendHeaders(HttpExchange exchange, HttpContent content, Callback callback) {
    Request request = exchange.getRequest();
    // Copy the request headers to be able to convert them properly
    HttpFields headers = new HttpFields();
    for (HttpField field : request.getHeaders()) headers.put(field);
    HttpFields fcgiHeaders = new HttpFields();
    // FastCGI headers based on the URI
    URI uri = request.getURI();
    String path = uri == null ? request.getPath() : uri.getRawPath();
    fcgiHeaders.put(FCGI.Headers.DOCUMENT_URI, path);
    String query = uri == null ? null : uri.getRawQuery();
    fcgiHeaders.put(FCGI.Headers.QUERY_STRING, query == null ? "" : query);
    // FastCGI headers based on HTTP headers
    HttpField httpField = headers.remove(HttpHeader.AUTHORIZATION);
    if (httpField != null)
        fcgiHeaders.put(FCGI.Headers.AUTH_TYPE, httpField.getValue());
    httpField = headers.remove(HttpHeader.CONTENT_LENGTH);
    fcgiHeaders.put(FCGI.Headers.CONTENT_LENGTH, httpField == null ? "" : httpField.getValue());
    httpField = headers.remove(HttpHeader.CONTENT_TYPE);
    fcgiHeaders.put(FCGI.Headers.CONTENT_TYPE, httpField == null ? "" : httpField.getValue());
    // FastCGI headers that are not based on HTTP headers nor URI
    fcgiHeaders.put(FCGI.Headers.REQUEST_METHOD, request.getMethod());
    fcgiHeaders.put(FCGI.Headers.SERVER_PROTOCOL, request.getVersion().asString());
    fcgiHeaders.put(FCGI.Headers.GATEWAY_INTERFACE, "CGI/1.1");
    fcgiHeaders.put(FCGI.Headers.SERVER_SOFTWARE, "Jetty/" + Jetty.VERSION);
    // Translate remaining HTTP header into the HTTP_* format
    for (HttpField field : headers) {
        String name = field.getName();
        String fcgiName = "HTTP_" + name.replaceAll("-", "_").toUpperCase(Locale.ENGLISH);
        fcgiHeaders.add(fcgiName, field.getValue());
    }
    // Give a chance to the transport implementation to customize the FastCGI headers
    HttpClientTransportOverFCGI transport = (HttpClientTransportOverFCGI) getHttpChannel().getHttpDestination().getHttpClient().getTransport();
    transport.customize(request, fcgiHeaders);
    int id = getHttpChannel().getRequest();
    boolean hasContent = content.hasContent();
    Generator.Result headersResult = generator.generateRequestHeaders(id, fcgiHeaders, hasContent ? callback : Callback.NOOP);
    if (hasContent) {
        getHttpChannel().flush(headersResult);
    } else {
        Generator.Result noContentResult = generator.generateRequestContent(id, BufferUtil.EMPTY_BUFFER, true, callback);
        getHttpChannel().flush(headersResult, noContentResult);
    }
}
Also used : HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) Request(org.eclipse.jetty.client.api.Request) URI(java.net.URI) ClientGenerator(org.eclipse.jetty.fcgi.generator.ClientGenerator) Generator(org.eclipse.jetty.fcgi.generator.Generator)

Example 2 with HttpField

use of org.eclipse.jetty.http.HttpField in project jetty.project by eclipse.

the class ServerGenerator method generateResponseHeaders.

public Result generateResponseHeaders(int request, int code, String reason, HttpFields fields, Callback callback) {
    request &= 0xFF_FF;
    final Charset utf8 = StandardCharsets.UTF_8;
    List<byte[]> bytes = new ArrayList<>(fields.size() * 2);
    int length = 0;
    if (code != 200 || sendStatus200) {
        // Special 'Status' header
        bytes.add(STATUS);
        length += STATUS.length + COLON.length;
        if (reason == null)
            reason = HttpStatus.getMessage(code);
        byte[] responseBytes = (code + " " + reason).getBytes(utf8);
        bytes.add(responseBytes);
        length += responseBytes.length + EOL.length;
    }
    // Other headers
    for (HttpField field : fields) {
        String name = field.getName();
        byte[] nameBytes = name.getBytes(utf8);
        bytes.add(nameBytes);
        String value = field.getValue();
        byte[] valueBytes = value.getBytes(utf8);
        bytes.add(valueBytes);
        length += nameBytes.length + COLON.length;
        length += valueBytes.length + EOL.length;
    }
    // End of headers
    length += EOL.length;
    final ByteBuffer buffer = byteBufferPool.acquire(length, true);
    BufferUtil.clearToFill(buffer);
    for (int i = 0; i < bytes.size(); i += 2) buffer.put(bytes.get(i)).put(COLON).put(bytes.get(i + 1)).put(EOL);
    buffer.put(EOL);
    BufferUtil.flipToFlush(buffer, 0);
    return generateContent(request, buffer, true, false, callback, FCGI.FrameType.STDOUT);
}
Also used : HttpField(org.eclipse.jetty.http.HttpField) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer)

Example 3 with HttpField

use of org.eclipse.jetty.http.HttpField in project jetty.project by eclipse.

the class ClientParserTest method testParseResponseHeaders.

@Test
public void testParseResponseHeaders() throws Exception {
    final int id = 13;
    HttpFields fields = new HttpFields();
    final int statusCode = 200;
    final String statusMessage = "OK";
    final String contentTypeName = "Content-Type";
    final String contentTypeValue = "text/html;charset=utf-8";
    fields.put(contentTypeName, contentTypeValue);
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    ServerGenerator generator = new ServerGenerator(byteBufferPool);
    Generator.Result result = generator.generateResponseHeaders(id, statusCode, statusMessage, fields, null);
    // Use the fundamental theorem of arithmetic to test the results.
    // This way we know onHeader() has been called the right number of
    // times with the right arguments, and so onHeaders().
    final int[] primes = new int[] { 2, 3, 5 };
    int value = 1;
    for (int prime : primes) value *= prime;
    final AtomicInteger params = new AtomicInteger(1);
    ClientParser parser = new ClientParser(new ClientParser.Listener.Adapter() {

        @Override
        public void onBegin(int request, int code, String reason) {
            Assert.assertEquals(statusCode, code);
            Assert.assertEquals(statusMessage, reason);
            params.set(params.get() * primes[0]);
        }

        @Override
        public void onHeader(int request, HttpField field) {
            Assert.assertEquals(id, request);
            switch(field.getName()) {
                case contentTypeName:
                    Assert.assertEquals(contentTypeValue, field.getValue());
                    params.set(params.get() * primes[1]);
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onHeaders(int request) {
            Assert.assertEquals(id, request);
            params.set(params.get() * primes[2]);
        }
    });
    for (ByteBuffer buffer : result.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    Assert.assertEquals(value, params.get());
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerGenerator(org.eclipse.jetty.fcgi.generator.ServerGenerator) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) Generator(org.eclipse.jetty.fcgi.generator.Generator) ServerGenerator(org.eclipse.jetty.fcgi.generator.ServerGenerator) Test(org.junit.Test)

Example 4 with HttpField

use of org.eclipse.jetty.http.HttpField in project jetty.project by eclipse.

the class HttpChannelOverFCGI method convertHeader.

private HttpField convertHeader(HttpField field) {
    String name = field.getName();
    if (name.startsWith("HTTP_")) {
        // Converts e.g. "HTTP_ACCEPT_ENCODING" to "Accept-Encoding"
        String[] parts = name.split("_");
        StringBuilder httpName = new StringBuilder();
        for (int i = 1; i < parts.length; ++i) {
            if (i > 1)
                httpName.append("-");
            String part = parts[i];
            httpName.append(Character.toUpperCase(part.charAt(0)));
            httpName.append(part.substring(1).toLowerCase(Locale.ENGLISH));
        }
        String headerName = httpName.toString();
        String value = field.getValue();
        if (HttpHeader.HOST.is(headerName))
            return new HostPortHttpField(value);
        else
            return new HttpField(headerName, value);
    }
    return null;
}
Also used : HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) HttpField(org.eclipse.jetty.http.HttpField) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 5 with HttpField

use of org.eclipse.jetty.http.HttpField in project jetty.project by eclipse.

the class HttpReceiverOverHTTP2 method onHeaders.

@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null)
        return;
    HttpResponse response = exchange.getResponse();
    MetaData.Response metaData = (MetaData.Response) frame.getMetaData();
    response.version(metaData.getHttpVersion()).status(metaData.getStatus()).reason(metaData.getReason());
    if (responseBegin(exchange)) {
        HttpFields headers = metaData.getFields();
        for (HttpField header : headers) {
            if (!responseHeader(exchange, header))
                return;
        }
        if (responseHeaders(exchange)) {
            int status = metaData.getStatus();
            boolean informational = HttpStatus.isInformational(status) && status != HttpStatus.SWITCHING_PROTOCOLS_101;
            if (frame.isEndStream() || informational)
                responseSuccess(exchange);
        }
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpResponse(org.eclipse.jetty.client.HttpResponse) MetaData(org.eclipse.jetty.http.MetaData) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) HttpExchange(org.eclipse.jetty.client.HttpExchange) HttpResponse(org.eclipse.jetty.client.HttpResponse)

Aggregations

HttpField (org.eclipse.jetty.http.HttpField)57 Test (org.junit.Test)29 HttpFields (org.eclipse.jetty.http.HttpFields)19 ByteBuffer (java.nio.ByteBuffer)17 MetaData (org.eclipse.jetty.http.MetaData)16 HostPortHttpField (org.eclipse.jetty.http.HostPortHttpField)12 ArrayList (java.util.ArrayList)8 PreEncodedHttpField (org.eclipse.jetty.http.PreEncodedHttpField)7 Entry (org.eclipse.jetty.http2.hpack.HpackContext.Entry)7 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)7 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)7 HttpHeader (org.eclipse.jetty.http.HttpHeader)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 HeaderGenerator (org.eclipse.jetty.http2.generator.HeaderGenerator)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 HpackEncoder (org.eclipse.jetty.http2.hpack.HpackEncoder)4 Parser (org.eclipse.jetty.http2.parser.Parser)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3