43 import java.nio.charset.*;
44 import java.util.regex.*;
63 private Action(String name) { this.name = name; }
64 public String toString() {
return name; }
66 static Action GET =
new Action(
"GET");
67 static Action PUT =
new Action(
"PUT");
68 static Action POST =
new Action(
"POST");
69 static Action HEAD =
new Action(
"HEAD");
71 static Action parse(String s) {
80 throw new IllegalArgumentException(s);
84 private Action action;
85 private String version;
88 Action action() {
return action; }
89 String version() {
return version; }
90 URI uri() {
return uri; }
92 private Request(Action a, String v, URI u) {
98 public String toString() {
99 return (action +
" " + version +
" " + uri);
102 static boolean isComplete(ByteBuffer bb) {
103 int p = bb.position() - 4;
106 return (((bb.get(p + 0) ==
'\r') &&
107 (bb.get(p + 1) ==
'\n') &&
108 (bb.get(p + 2) ==
'\r') &&
109 (bb.get(p + 3) ==
'\n')));
112 private static Charset ascii = Charset.forName(
"US-ASCII");
135 private static Pattern requestPattern
136 = Pattern.compile(
"\\A([A-Z]+) +([^ ]+) +HTTP/([0-9\\.]+)$"
137 +
".*^Host: ([^ ]+)$.*\r\n\r\n\\z",
138 Pattern.MULTILINE | Pattern.DOTALL);
140 static Request parse(ByteBuffer bb)
throws MalformedRequestException {
142 CharBuffer cb = ascii.decode(bb);
143 Matcher m = requestPattern.matcher(cb);
145 throw new MalformedRequestException();
148 a = Action.parse(m.group(1));
149 }
catch (IllegalArgumentException x) {
150 throw new MalformedRequestException();
154 u =
new URI(
"http://"
157 }
catch (URISyntaxException x) {
158 throw new MalformedRequestException();
160 return new Request(a, m.group(3), u);