Search in sources :

Example 1 with HeaderMap

use of org.httpkit.HeaderMap in project http-kit by http-kit.

the class SingleThreadHttpServerTest method handle.

public void handle(HttpRequest request, RespCallback cb) {
    HeaderMap map = new HeaderMap();
    map.put("Connection", "Keep-Alive");
    ByteBuffer[] bytes = HttpEncode(200, map, SingleThreadHandler.body);
    cb.run(bytes);
}
Also used : HeaderMap(org.httpkit.HeaderMap) ByteBuffer(java.nio.ByteBuffer)

Example 2 with HeaderMap

use of org.httpkit.HeaderMap in project http-kit by http-kit.

the class AsyncChannel method sendHandshake.

public void sendHandshake(Map<String, Object> headers) {
    HeaderMap map = HeaderMap.camelCase(headers);
    server.tryWrite(key, HttpEncode(101, map, null));
}
Also used : HeaderMap(org.httpkit.HeaderMap)

Example 3 with HeaderMap

use of org.httpkit.HeaderMap in project http-kit by http-kit.

the class AsyncChannel method firstWrite.

// Write first HTTP header and [first chunk data]? to client
private void firstWrite(Object data, boolean close) throws IOException {
    ByteBuffer[] buffers;
    int status = 200;
    Object body = data;
    HeaderMap headers;
    if (data instanceof Map) {
        Map<Keyword, Object> resp = (Map<Keyword, Object>) data;
        headers = HeaderMap.camelCase((Map) resp.get(HEADERS));
        status = getStatus(resp);
        body = resp.get(BODY);
    } else {
        headers = new HeaderMap();
    }
    if (headers.isEmpty()) {
        // default 200 and text/html
        headers.put("Content-Type", "text/html; charset=utf-8");
    }
    if (request.isKeepAlive && request.version == HttpVersion.HTTP_1_0) {
        headers.put("Connection", "Keep-Alive");
    }
    if (close) {
        // normal response, Content-Length. Every http client understand it
        buffers = HttpEncode(status, headers, body);
    } else {
        if (request.version == HttpVersion.HTTP_1_1) {
            // first chunk
            headers.put("Transfer-Encoding", "chunked");
        }
        ByteBuffer[] bb = HttpEncode(status, headers, body);
        if (body == null) {
            buffers = bb;
        } else {
            buffers = new ByteBuffer[] { // header
            bb[0], // chunk size
            chunkSize(bb[1].remaining()), // chunk data
            bb[1], // terminating CRLF sequence
            ByteBuffer.wrap(newLineBytes) };
        }
    }
    if (close) {
        onClose(0);
    }
    server.tryWrite(key, !close, buffers);
}
Also used : HeaderMap(org.httpkit.HeaderMap) Keyword(clojure.lang.Keyword) ByteBuffer(java.nio.ByteBuffer) HeaderMap(org.httpkit.HeaderMap) Map(java.util.Map)

Example 4 with HeaderMap

use of org.httpkit.HeaderMap in project http-kit by http-kit.

the class HttpServer method decodeHttp.

private void decodeHttp(HttpAtta atta, SelectionKey key, SocketChannel ch) {
    try {
        boolean sentContinue = false;
        do {
            AsyncChannel channel = atta.channel;
            HttpRequest request = atta.decoder.decode(buffer);
            if (request != null) {
                channel.reset(request);
                if (request.isWebSocket) {
                    key.attach(new WsAtta(channel, maxWs));
                } else {
                    atta.keepalive = request.isKeepAlive;
                }
                request.channel = channel;
                request.remoteAddr = (InetSocketAddress) ch.socket().getRemoteSocketAddress();
                handler.handle(request, new RespCallback(key, this));
                // pipelining not supported : need queue to ensure order
                atta.decoder.reset();
            } else if (!sentContinue && atta.decoder.requiresContinue()) {
                tryWrite(key, HttpEncode(100, new HeaderMap(), null));
                sentContinue = true;
            }
        } while (// consume all
        buffer.hasRemaining());
    } catch (ProtocolException e) {
        closeKey(key, -1);
    } catch (RequestTooLargeException e) {
        atta.keepalive = false;
        tryWrite(key, HttpEncode(413, new HeaderMap(), e.getMessage()));
    } catch (LineTooLargeException e) {
        // close after write
        atta.keepalive = false;
        tryWrite(key, HttpEncode(414, new HeaderMap(), e.getMessage()));
    }
}
Also used : ProtocolException(org.httpkit.ProtocolException) LineTooLargeException(org.httpkit.LineTooLargeException) HeaderMap(org.httpkit.HeaderMap) RequestTooLargeException(org.httpkit.RequestTooLargeException)

Example 5 with HeaderMap

use of org.httpkit.HeaderMap in project http-kit by http-kit.

the class RingHandler method run.

public void run() {
    try {
        Map resp = (Map) handler.invoke(buildRequestMap(req));
        if (resp == null) {
            // handler return null
            cb.run(HttpEncode(404, new HeaderMap(), null));
        } else {
            Object body = resp.get(BODY);
            if (!(body instanceof AsyncChannel)) {
                // hijacked
                HeaderMap headers = HeaderMap.camelCase((Map) resp.get(HEADERS));
                if (req.version == HTTP_1_0 && req.isKeepAlive) {
                    headers.put("Connection", "Keep-Alive");
                }
                cb.run(HttpEncode(getStatus(resp), headers, body));
            }
        }
    } catch (Throwable e) {
        cb.run(HttpEncode(500, new HeaderMap(), e.getMessage()));
        HttpUtils.printError(req.method + " " + req.uri, e);
    }
}
Also used : HeaderMap(org.httpkit.HeaderMap) HeaderMap(org.httpkit.HeaderMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

HeaderMap (org.httpkit.HeaderMap)5 ByteBuffer (java.nio.ByteBuffer)2 Map (java.util.Map)2 Keyword (clojure.lang.Keyword)1 TreeMap (java.util.TreeMap)1 LineTooLargeException (org.httpkit.LineTooLargeException)1 ProtocolException (org.httpkit.ProtocolException)1 RequestTooLargeException (org.httpkit.RequestTooLargeException)1