@Override public void submit(SelectionKey key, HttpMessage msg) { submit(new HttpEncoder(config), key, msg); }
public static void errorResponse(HttpConfig config, SelectionKey key, int status, String message) { HttpEncoder enc = new HttpEncoder(config); enc.response(key, HttpProtocol.HTTP_1_1, status, ""); enc.header(key, H_CONNECTION, "close"); enc.body(key, ByteBuffer.wrap(message.getBytes()), NetCtx.CLOSE); enc.finish(key); } }
@Override public RingHandler finish(SelectionKey key) { if (!bodyParts.isEmpty()) { int sz = 0; for (byte[] b : bodyParts) { sz += b.length; } byte[] body = new byte[sz]; int pos = 0; for (byte[] b : bodyParts) { System.arraycopy(b, 0, body, pos, b.length); pos += b.length; } // TODO implement streaming request = request.assoc(BODY_DATA, new ByteArrayInputStream(body)); bodyParts.clear(); } request = request.assoc(REQ_HEADERS, headers.persistent()); RingTask task = new RingTask(key, fn, new HttpEncoder(config), request.persistent()); executor.execute(task); return this; }
@Override public void sendRequest(SelectionKey key, BufHandler output) { HttpEncoder encoder = new HttpEncoder(config, output); encoder.request(key, HttpProtocol.HTTP_1_1, method, uri, query); for (int i = 0; i < headersPos; i+=2) { encoder.header(key, headers[i], headers[i+1]); } encoder.finish(key); }
@Override public HttpMessage exec(HttpMessage req) { if (socket == null || !socket.isConnected()) { connect(); } Exception e = null; for (int i = 0; i < config.getMaxRetries(); i++) { try { output.submit(new HttpEncoder(config, stream), null, req); socket.getOutputStream().flush(); input.run(); evtCalls.call(); return result; } catch (Exception e1) { e = e1; reconnect(); } } throw new NetException("Error executing HTTP call", e); }
@Override public SvcListener finish(SelectionKey key) { System.out.println("SVR|Finish."); HttpEncoder encoder = new HttpEncoder(config); encoder.response(key, HTTP_1_1, 200, ""); encoder.header(key, "X-Some-Header", "someVal"); encoder.body(key, ByteBuffer.wrap("BAR".getBytes())); encoder.finish(key); return this; }