Skip to content

Commit

Permalink
Fixed concurrency issues of the HTTP router component.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jul 26, 2015
1 parent 39204b8 commit c3e754e
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions rapidoid-http/src/main/java/org/rapidoid/http/HttpRouter.java
Expand Up @@ -50,11 +50,11 @@ public String toString() {
private Handler genericHandler;

@Override
public void generic(Handler handler) {
public synchronized void generic(Handler handler) {
this.genericHandler = handler;
}

private void addRoute(String action, String path, Handler handler) {
private synchronized void addRoute(String action, String path, Handler handler) {
assert action.length() >= 1;
assert path.length() >= 1;

Expand All @@ -69,17 +69,20 @@ private void addRoute(String action, String path, Handler handler) {
}

private long hash(String action, String path) {
int hash = action.charAt(0) * 17 + action.length() * 19 + path.charAt(0);
return hash;
return action.charAt(0) * 17 + action.length() * 19 + path.charAt(0);
}

private long hash(Buf buf, Range action, Range path) {
int hash = buf.get(action.start) * 17 + action.length * 19 + buf.get(path.start);
return hash;
return buf.get(action.start) * 17 + action.length * 19 + buf.get(path.start);
}

@Override
public void dispatch(HttpExchangeImpl x) {
Handler handler = findHandler(x);
handle(handler, x);
}

private Handler findHandler(HttpExchangeImpl x) {

Buf buf = x.input();
Range action = x.verb_().range();
Expand All @@ -88,28 +91,33 @@ public void dispatch(HttpExchangeImpl x) {
// serveStaticFileIfExists(x, buf, path);

long hash = hash(buf, action, path);
SimpleList<Route> candidates = routes.get(hash);

if (candidates != null) {
for (int i = 0; i < candidates.size(); i++) {
Route route = candidates.get(i);

if (BytesUtil.matches(buf.bytes(), action, route.action, true)
&& BytesUtil.startsWith(buf.bytes(), path, route.path, true)) {
int pos = path.start + route.path.length;
if (path.limit() == pos || buf.get(pos) == '/') {
x.setSubpath(pos, path.limit());
handle(route.handler, x);
return;

synchronized (this) {

SimpleList<Route> candidates = routes.get(hash);
candidates = routes.get(hash);

if (candidates != null) {

for (int i = 0; i < candidates.size(); i++) {
Route route = candidates.get(i);

if (BytesUtil.matches(buf.bytes(), action, route.action, true)
&& BytesUtil.startsWith(buf.bytes(), path, route.path, true)) {

int pos = path.start + route.path.length;
if (path.limit() == pos || buf.get(pos) == '/') {
x.setSubpath(pos, path.limit());
return route.handler;
}
}
}
}
}

if (genericHandler != null) {
x.setSubpath(path.start, path.limit());
handle(genericHandler, x);
return;
if (genericHandler != null) {
x.setSubpath(path.start, path.limit());
return genericHandler;
}
}

throw x.notFound();
Expand Down Expand Up @@ -147,9 +155,7 @@ public HttpRouter route(String action, String url, Handler handler) {
throw new IllegalArgumentException("Invalid url: " + url);
}

if (url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
}
url = U.trimr(url, "/");

if (!url.startsWith("/")) {
url = "/" + url;
Expand Down

0 comments on commit c3e754e

Please sign in to comment.