@Override public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req) { if (!HttpUtils.isGetReq(req)) return HttpStatus.NOT_FOUND; try { String[] staticFilesLocations = customization.staticFilesPath(); if (!U.isEmpty(staticFilesLocations)) { Res res = HttpUtils.staticResource(req, staticFilesLocations); if (res != null) { StaticFilesSecurity staticFilesSecurity = customization.staticFilesSecurity(); if (staticFilesSecurity.canServe(req, res)) { byte[] bytes = res.getBytesOrNull(); if (bytes != null) { MediaType contentType = U.or(MediaType.getByFileName(res.getName()), MediaType.BINARY); HttpIO.INSTANCE.write200(HttpUtils.maybe(req), ctx, isKeepAlive, contentType, bytes); return HttpStatus.DONE; } } } } return HttpStatus.NOT_FOUND; } catch (Exception e) { return HttpIO.INSTANCE.errorAndDone(req, e, LogLevel.ERROR); } }
protected void writeJsonBody(MaybeReq req, Channel ctx, Object value) { writeContentTypeHeader(ctx, MediaType.JSON); ReusableWritable out = Msc.locals().jsonRenderingStream(); JSON.stringify(value, out); HttpIO.INSTANCE.writeContentLengthHeader(ctx, out.size()); HttpIO.INSTANCE.closeHeaders(req, ctx.output()); ctx.write(out.array(), 0, out.size()); }
private void onDone() { if (stopped) { return; } boolean willBeDone = true; if (!rendering) { renderResponseOrError(); willBeDone = false; } if (!completed) { // FIXME is this still required? completed = true; } if (response != null) { response.finish(); } if (willBeDone) { HttpIO.INSTANCE.done(ReqImpl.this); } }
private void handleError(Throwable error, final Req req, final Resp resp, final ProxyMapping mapping, final int attempts, final long since) { if (error instanceof ConnectException || error instanceof IOException) { if (HttpUtils.isGetReq(req) && !U.timedOut(since, timeout())) { Jobs.after(retryDelay()).milliseconds(() -> process(req, resp, mapping, attempts + 1, since)); } else { HttpIO.INSTANCE.errorAndDone(req, U.rte("Couldn't connect to the upstream!", error), LogLevel.DEBUG); } } else { HttpIO.INSTANCE.errorAndDone(req, error, LogLevel.ERROR); } }
private void renderResponse(int code, MediaType contentType, RespBody body) { rendering = true; completed = body != null; HttpIO.INSTANCE.respond( HttpUtils.maybe(this), channel, connId, handle, code, isKeepAlive, contentType, body, response != null ? response.headers() : null, response != null ? response.cookies() : null ); }
BufRange path = data.path; HttpIO.INSTANCE.removeTrailingSlash(buf, path); HttpIO.INSTANCE.removeTrailingSlash(buf, uri); HttpIO.INSTANCE.writeBadRequest(channel); return;
@Override public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req) { HttpIO.INSTANCE.write200(HttpUtils.maybe(req), ctx, isKeepAlive, contentType, response); return HttpStatus.DONE; }
private void handleError(Req req, Throwable e) { req.revert(); req.async(); HttpIO.INSTANCE.error(req, e, LogLevel.ERROR); // the Req object will do the rendering req.done(); }
protected void writeBody(Channel ctx, byte[] body, int offset, int length, MediaType contentType) { writeContentTypeHeader(ctx, contentType); HttpIO.INSTANCE.writeContentLengthHeader(ctx, length); ctx.write(CR_LF); ctx.write(body, offset, length); }
HttpIO.INSTANCE.writeHttpResp(maybeReq, ctx, isKeepAlive, 500, MediaType.PLAIN_TEXT_UTF_8, "Internal server error!".getBytes()); HttpIO.INSTANCE.writeHttpResp(maybeReq, ctx, isKeepAlive, code, ctype, result);
private boolean handleError(Channel channel, boolean isKeepAlive, Req req, Throwable e) { if (req != null) { if (!((ReqImpl) req).isStopped()) { try { HttpIO.INSTANCE.errorAndDone(req, e, LogLevel.ERROR); } catch (Exception e1) { Log.error("HTTP error handler error!", e1); internalServerError(channel, isKeepAlive, req); } } return true; } else { Log.error("Low-level HTTP handler error!", e); internalServerError(channel, isKeepAlive, null); } return false; }
@Override public void onRequest(Channel channel, RapidoidHelper data) { boolean isKeepAlive = data.isKeepAlive.value; RespBody body = new RespBodyBytes("Not found!".getBytes()); HttpIO.INSTANCE.respond(HttpUtils.noReq(), channel, -1, -1, 404, isKeepAlive, MediaType.PLAIN_TEXT_UTF_8, body, null, null); channel.send().closeIf(!isKeepAlive); } }
BufRange path = data.path; HttpIO.INSTANCE.removeTrailingSlash(buf, path); HttpIO.INSTANCE.removeTrailingSlash(buf, uri); HttpIO.INSTANCE.writeBadRequest(channel); return;
@Override public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req) { byte[] bytes = resource.getBytesOrNull(); if (bytes != null) { HttpIO.INSTANCE.write200(HttpUtils.maybe(req), ctx, isKeepAlive, contentType, bytes); return HttpStatus.DONE; } else { return HttpStatus.NOT_FOUND; } }
static RespBody toRespBody(Req req, RespImpl resp) { try { return createRespBodyFromResult(req, resp); } catch (Throwable e) { HttpIO.INSTANCE.error(req, e, LogLevel.ERROR); try { return createRespBodyFromResult(req, resp); } catch (Exception e1) { Log.error("Internal rendering error!", e1); return new RespBodyBytes(HttpUtils.getErrorMessageAndSetCode(resp, e1).getBytes()); } } }
protected void writeBody(Channel ctx, byte[] body, int offset, int length, MediaType contentType) { writeContentTypeHeader(ctx, contentType); HttpIO.INSTANCE.writeContentLengthHeader(ctx, length); ctx.write(CR_LF); ctx.write(body, offset, length); }
HttpIO.INSTANCE.writeHttpResp(maybeReq, ctx, isKeepAlive, 500, MediaType.PLAIN_TEXT_UTF_8, "Internal server error!".getBytes()); HttpIO.INSTANCE.writeHttpResp(maybeReq, ctx, isKeepAlive, code, ctype, result);
@Override public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req) { if (!HttpUtils.isGetReq(req)) return HttpStatus.NOT_FOUND; try { String[] staticFilesLocations = customization.staticFilesPath(); if (!U.isEmpty(staticFilesLocations)) { Res res = HttpUtils.staticResource(req, staticFilesLocations); if (res != null) { StaticFilesSecurity staticFilesSecurity = customization.staticFilesSecurity(); if (staticFilesSecurity.canServe(req, res)) { byte[] bytes = res.getBytesOrNull(); if (bytes != null) { MediaType contentType = U.or(MediaType.getByFileName(res.getName()), MediaType.BINARY); HttpIO.INSTANCE.write200(HttpUtils.maybe(req), ctx, isKeepAlive, contentType, bytes); return HttpStatus.DONE; } } } } return HttpStatus.NOT_FOUND; } catch (Exception e) { return HttpIO.INSTANCE.errorAndDone(req, e, LogLevel.ERROR); } }
private void handleError(Throwable error, final Req req, final Resp resp, final ProxyMapping mapping, final int attempts, final long since) { if (error instanceof ConnectException || error instanceof IOException) { if (HttpUtils.isGetReq(req) && !Msc.timedOut(since, timeout())) { Jobs.after(retryDelay()).milliseconds(new Runnable() { @Override public void run() { process(req, resp, mapping, attempts + 1, since); } }); } else { HttpIO.INSTANCE.errorAndDone(req, U.rte("Couldn't connect to the upstream!", error), LogLevel.DEBUG); } } else { HttpIO.INSTANCE.errorAndDone(req, error, LogLevel.ERROR); } }
private void serveCached(ReqImpl req, CachedResp resp) { Channel channel = req.channel(); req.cached(true); HttpIO.INSTANCE.respond(HttpUtils.req(req), channel, -1, -1, resp.statusCode, req.isKeepAlive(), resp.contentType, new RespBodyBuffer(resp.body.duplicate()), resp.headers, null); channel.send().closeIf(!req.isKeepAlive()); }