Skip to content

Commit

Permalink
Refactored the HTTP handlers. Other minor improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Dec 28, 2017
1 parent fa41a1b commit fe51473
Show file tree
Hide file tree
Showing 49 changed files with 716 additions and 422 deletions.
2 changes: 1 addition & 1 deletion asciidoc/examples/httpresp.adoc
Expand Up @@ -15,7 +15,7 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
/* Returning the request or response object means the response was constructed */ /* Returning the request or response object means the response was constructed */
On.get("/").html((Req req) -> { On.get("/").html((Req req) -> {
Resp resp = req.response(); Resp resp = req.response();
Expand Down
1 change: 1 addition & 0 deletions asciidoc/examples/jpascaffold.adoc
Expand Up @@ -40,6 +40,7 @@ import java.util.List;
public class Main extends GUI { public class Main extends GUI {
@SuppressWarnings("unchecked")
public static void main(String[] args) { public static void main(String[] args) {
On.page("/").mvc("Welcome!"); On.page("/").mvc("Welcome!");
Expand Down
Expand Up @@ -363,9 +363,15 @@ org.rapidoid.http.FastHttp
org.rapidoid.http.FastHttpProtocol org.rapidoid.http.FastHttpProtocol
org.rapidoid.http.handler.AbstractDecoratingHttpHandler org.rapidoid.http.handler.AbstractDecoratingHttpHandler
org.rapidoid.http.handler.AbstractHttpHandler org.rapidoid.http.handler.AbstractHttpHandler
org.rapidoid.http.handler.AbstractHttpHandlerDecorator
org.rapidoid.http.handler.HandlerInvocation org.rapidoid.http.handler.HandlerInvocation
org.rapidoid.http.handler.HandlerResultProcessor org.rapidoid.http.handler.HandlerResultProcessor
org.rapidoid.http.handler.HttpAuthWrapper
org.rapidoid.http.handler.HttpHandler org.rapidoid.http.handler.HttpHandler
org.rapidoid.http.handler.HttpManagedHandlerDecorator
org.rapidoid.http.handler.HttpTxWrapper
org.rapidoid.http.handler.HttpUnmanagedHandlerDecorator
org.rapidoid.http.handler.HttpWrappers
org.rapidoid.http.handler.lambda.FiveParamLambdaHandler org.rapidoid.http.handler.lambda.FiveParamLambdaHandler
org.rapidoid.http.handler.lambda.FourParamLambdaHandler org.rapidoid.http.handler.lambda.FourParamLambdaHandler
org.rapidoid.http.handler.lambda.NParamLambdaHandler org.rapidoid.http.handler.lambda.NParamLambdaHandler
Expand Down Expand Up @@ -399,7 +405,6 @@ org.rapidoid.http.handler.PredefinedResponseHandler
org.rapidoid.http.handler.ResourceHttpHandler org.rapidoid.http.handler.ResourceHttpHandler
org.rapidoid.http.handler.StaticHttpHandler org.rapidoid.http.handler.StaticHttpHandler
org.rapidoid.http.handler.StaticResourcesHandler org.rapidoid.http.handler.StaticResourcesHandler
org.rapidoid.http.handler.TxUtil
org.rapidoid.http.Headers org.rapidoid.http.Headers
org.rapidoid.http.HTTP org.rapidoid.http.HTTP
org.rapidoid.http.HttpClient org.rapidoid.http.HttpClient
Expand Down Expand Up @@ -435,6 +440,7 @@ org.rapidoid.http.impl.MVCModelImpl
org.rapidoid.http.impl.PathPattern org.rapidoid.http.impl.PathPattern
org.rapidoid.http.impl.RapidoidReqInfo org.rapidoid.http.impl.RapidoidReqInfo
org.rapidoid.http.impl.ReqImpl org.rapidoid.http.impl.ReqImpl
org.rapidoid.http.impl.RespBodyBuffer
org.rapidoid.http.impl.RespBodyBytes org.rapidoid.http.impl.RespBodyBytes
org.rapidoid.http.impl.RespImpl org.rapidoid.http.impl.RespImpl
org.rapidoid.http.impl.ResponseRenderer org.rapidoid.http.impl.ResponseRenderer
Expand Down
Expand Up @@ -20,11 +20,12 @@


package org.rapidoid.net; package org.rapidoid.net;


import org.rapidoid.RapidoidThing;
import org.rapidoid.config.BasicConfig; import org.rapidoid.config.BasicConfig;
import org.rapidoid.net.impl.DefaultExchange; import org.rapidoid.net.impl.DefaultExchange;
import org.rapidoid.net.impl.RapidoidHelper; import org.rapidoid.net.impl.RapidoidHelper;


public class NetworkingParams { public class NetworkingParams extends RapidoidThing {


private volatile String address; private volatile String address;


Expand Down
Expand Up @@ -20,13 +20,14 @@


package org.rapidoid.net; package org.rapidoid.net;


import org.rapidoid.RapidoidThing;
import org.rapidoid.config.Conf; import org.rapidoid.config.Conf;
import org.rapidoid.net.tls.TLSUtil; import org.rapidoid.net.tls.TLSUtil;
import org.rapidoid.util.MscOpts; import org.rapidoid.util.MscOpts;


import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;


public class TLSParams { public class TLSParams extends RapidoidThing {


// auto-activate if TLS is enabled // auto-activate if TLS is enabled
private volatile boolean tls = MscOpts.isTLSEnabled(); private volatile boolean tls = MscOpts.isTLSEnabled();
Expand Down
Expand Up @@ -339,6 +339,11 @@ public void resume(final long expectedConnId, final long handle, final AsyncLogi
public void run() { public void run() {
resume(expectedConnId, handle, asyncLogic); resume(expectedConnId, handle, asyncLogic);
} }

@Override
public String toString() {
return U.frmt("RapidoidConnection.ResumeJob(handle=%s, expectedConnId=%s, logic=%s)", handle, expectedConnId, asyncLogic);
}
}); });


} else if (seq == handle - 1) { } else if (seq == handle - 1) {
Expand Down
Expand Up @@ -70,6 +70,7 @@ public FastHttp(HttpRoutesImpl routes) {
public FastHttp(HttpRoutesImpl routes, @SuppressWarnings("unused") Config serverConfig) { public FastHttp(HttpRoutesImpl routes, @SuppressWarnings("unused") Config serverConfig) {
super(null); super(null);
this.routes = routes; this.routes = routes;
routes.setHttp(this);
} }


@Override @Override
Expand Down Expand Up @@ -198,7 +199,7 @@ private void serveCached(ReqImpl req, CachedResp resp) {
req.cached(true); req.cached(true);


HttpIO.INSTANCE.respond(HttpUtils.req(req), channel, -1, -1, resp.statusCode, HttpIO.INSTANCE.respond(HttpUtils.req(req), channel, -1, -1, resp.statusCode,
req.isKeepAlive(), resp.contentType, resp.body.duplicate(), resp.headers, null); req.isKeepAlive(), resp.contentType, new RespBodyBuffer(resp.body.duplicate()), resp.headers, null);


channel.send().closeIf(!req.isKeepAlive()); channel.send().closeIf(!req.isKeepAlive());
} }
Expand Down Expand Up @@ -281,7 +282,7 @@ public ReqImpl createReq(Channel channel, boolean isGet, boolean isKeepAlive,


private HttpStatus handleIfFound(Channel channel, boolean isKeepAlive, HttpHandler handler, Req req) { private HttpStatus handleIfFound(Channel channel, boolean isKeepAlive, HttpHandler handler, Req req) {
try { try {
return handler.handle(channel, isKeepAlive, req, null); return handler.handle(channel, isKeepAlive, req);
} catch (NotFound nf) { } catch (NotFound nf) {
return HttpStatus.NOT_FOUND; return HttpStatus.NOT_FOUND;
} }
Expand Down
Expand Up @@ -305,7 +305,7 @@ public static TokenAuthData getAuth(Req req) {
} }


public static String inferRealIpAddress(Req req) { public static String inferRealIpAddress(Req req) {
// // FIXME if CloudFlare is detected, use req.header("cf-connecting-ip") // TODO if CloudFlare is detected, use req.header("cf-connecting-ip")?
return req.clientIpAddress(); return req.clientIpAddress();
} }


Expand Down
Expand Up @@ -22,11 +22,14 @@


import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.net.abstracts.Channel;


@Authors("Nikolche Mihajlovski") @Authors("Nikolche Mihajlovski")
@Since("5.5.1") @Since("5.5.1")
public interface RespBody { public interface RespBody {


byte[] toBytes(); int length();

void writeTo(Channel channel);


} }
Expand Up @@ -125,13 +125,11 @@ public static Customization of(Req req) {
private static boolean inValidContext(Req req) { private static boolean inValidContext(Req req) {
Ctx ctx = Ctxs.get(); Ctx ctx = Ctxs.get();
Req ctxReq = ctx != null ? (Req) ctx.exchange() : null; Req ctxReq = ctx != null ? (Req) ctx.exchange() : null;

U.must(ctx == null || !ctx.isClosed(), "The context for request (%s) is closed!");
U.must(req == ctxReq, "The customization request (%s) doesn't match the context request (%s)!", req, ctxReq); U.must(req == ctxReq, "The customization request (%s) doesn't match the context request (%s)!", req, ctxReq);
return true;
}


public static Customization current() { return true;
Ctx ctx = Ctxs.get();
return of(ctx != null ? (Req) ctx.exchange() : null);
} }


public String name() { public String name() {
Expand Down

0 comments on commit fe51473

Please sign in to comment.