private String validateRequest(HttpExchangeImpl x) { if (x.rVerb.isEmpty()) { return "HTTP verb cannot be empty!"; } if (!BytesUtil.isValidURI(x.input().bytes(), x.rUri)) { return "Invalid HTTP URI!"; } if (x.isGet.value && !x.rBody.isEmpty()) { return "Body is NOT allowed in HTTP GET requests!"; } return null; // OK, not error }
@SuppressWarnings("unused") private void serveStaticFileIfExists(HttpExchangeImpl x, Buf buf, Range path) { if (x.isGetReq() && BytesUtil.find(buf.bytes(), path.start + 1, path.limit(), (byte) '.', true) >= 0) { if (x.serveStaticFile()) { return; } } }
public synchronized void setSubpath(int start, int end) { subpathRange.setInterval(start, end); }
public synchronized HttpExchangeImpl setHome(String home) { String uriPath = path(); U.must(uriPath.startsWith(home)); this.path = uriPath.substring(home.length()); if (U.isEmpty(this.path)) { this.path = "/"; this.rPath.length = 1; } else { this.rPath.strip(home.length(), 0); } pathSegments = null; // re-calculate path segments pathSegments(); this.home = home; return this; }
private Handler findHandler(HttpExchangeImpl x) { Buf buf = x.input(); Range action = x.verb_().range(); Range path = x.path_().range(); // serveStaticFileIfExists(x, buf, path); long hash = hash(buf.bytes(), action, path); 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()); return genericHandler; } } throw x.notFound(); }
public Handler findHandler(Buf buf, Range action, Range path) { long hash = hash(buf.bytes(), 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) == '/') { return route.handler; } } } } return null; }
@Override public synchronized MultiData params_() { if (!parsedParams) { if (!rQuery.isEmpty()) { PARSER.parseParams(input(), params, query_().range()); } parsedParams = true; } return _params; }