Add rough route implementation
This commit is contained in:
parent
3971b249d9
commit
6a77faa6cf
|
@ -0,0 +1,49 @@
|
||||||
|
package com.spookyinternet.cobweb;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class HttpRoute {
|
||||||
|
private List<String> pieces;
|
||||||
|
|
||||||
|
public HttpRoute(String path, HttpInspectFunction fn) throws Exception {
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRoute(String path, HttpRequestFunction fn) throws Exception {
|
||||||
|
this.pieces = new ArrayList<String>();
|
||||||
|
|
||||||
|
if(path.charAt(0) != '/') {
|
||||||
|
throw new Exception("Invalid HTTP route: " + path);
|
||||||
|
}
|
||||||
|
|
||||||
|
int idx = 0;
|
||||||
|
int marker = 0;
|
||||||
|
|
||||||
|
while(idx < path.length()) {
|
||||||
|
char rune = path.charAt(idx);
|
||||||
|
|
||||||
|
if(rune == '/' || rune == '*') {
|
||||||
|
if(idx > marker) {
|
||||||
|
pieces.add(path.substring(marker, idx));
|
||||||
|
}
|
||||||
|
|
||||||
|
pieces.add(String.valueOf(rune));
|
||||||
|
marker = idx + 1;
|
||||||
|
} else if(rune == '{') {
|
||||||
|
if(idx > marker) {
|
||||||
|
pieces.add(path.substring(marker, idx));
|
||||||
|
}
|
||||||
|
|
||||||
|
marker = idx;
|
||||||
|
} else if(rune == '}') {
|
||||||
|
pieces.add(path.substring(marker, idx + 1));
|
||||||
|
marker = idx + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(idx > marker) {
|
||||||
|
pieces.add(path.substring(marker, idx + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue