46 lines
1.4 KiB
Java
46 lines
1.4 KiB
Java
package com.spookyinternet.cobweb;
|
|
|
|
import java.io.*;
|
|
import java.util.*;
|
|
|
|
public class HttpRequest {
|
|
public HttpConnection conn;
|
|
public String method;
|
|
public String path;
|
|
public Map<String, String> headers;
|
|
public String[] params;
|
|
|
|
public HttpRequest(BufferedReader reader, String ip, int port) {
|
|
this.headers = new HashMap<String, String>();
|
|
System.out.println(ip + "-" + port);
|
|
HttpTokenStream tokens = new HttpTokenStream(reader);
|
|
boolean processed = false;
|
|
String header = null;
|
|
StringBuilder value = new StringBuilder();
|
|
|
|
while(tokens.hasNext() && !processed) {
|
|
HttpToken token = tokens.next();
|
|
|
|
if(token.line == 1 && token.sol) {
|
|
this.method = token.value.toLowerCase();
|
|
} else if(token.line == 1 && token.seq == 3) {
|
|
this.path = token.value;
|
|
} else if(token.seq == 1 && token.value.endsWith(":")) {
|
|
header = token.value.substring(0, token.value.length() - 1);
|
|
} else if(token.eol && header != null) {
|
|
this.headers.put(header.toLowerCase(), value.toString());
|
|
header = null;
|
|
value = new StringBuilder();
|
|
} else if(header != null && token.seq > 2) {
|
|
value.append(token.value);
|
|
}
|
|
|
|
processed = token.value == "\r\n" && token.complete();
|
|
}
|
|
}
|
|
|
|
public String url() {
|
|
return "http://" + this.headers.get("host") + this.path;
|
|
}
|
|
}
|