/* * RequestParser.java * * Created on 3.2.2009 * */ package netukar.tinybrowser.request; import java.net.MalformedURLException; import netukar.tinybrowser.exceptions.RequestSyntaxException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.URI; import org.apache.commons.httpclient.URIException; import org.apache.commons.httpclient.methods.DeleteMethod; import org.apache.commons.httpclient.methods.EntityEnclosingMethod; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.HeadMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PutMethod; import org.apache.commons.httpclient.methods.StringRequestEntity; /** * * @author Radovan Netuka */ public class RequestParser { /** * Creates a new instance of RequestParser. */ private RequestParser() { } public static HttpMethod parse(String text) throws RequestSyntaxException, MalformedURLException { return parse(null, text); } public static HttpMethod parse(String url, String text) throws RequestSyntaxException, MalformedURLException { HttpMethod result; String overridenUrl = null; String[] lines = text.split("\n"); // Try both styles here, ie. "GET" or "GET /index.html HTTP/1.1" String[] keywords = lines[0].split(" "); String method; String path = null; String httpVersion = null; if (keywords.length == 1) { // contains only method name method = keywords[0]; } else if (keywords.length == 3) { // contains all 3 keywords - method, path and http version method = keywords[0]; path = keywords[1]; httpVersion = keywords[2]; } else { throw new RequestSyntaxException("Invalid keywords count for the first line"); } if (method.equals("GET")) { result = new GetMethod(); } else if (method.equals("POST")) { result = new PostMethod(); } else if (method.equals("PUT")) { result = new PutMethod(); } else if (method.equals("DELETE")) { result = new DeleteMethod(); } else if (method.equals("HEAD")) { result = new HeadMethod(); } else { throw new RequestSyntaxException("Invalid method name for request"); } if (path != null) { } if (httpVersion != null) { if (! (httpVersion.equals("HTTP/1.0") || httpVersion.equals("HTTP/1.1"))) { throw new RequestSyntaxException("Invalid HTTP version"); } } // HTTP header fields int i; for (i = 1; i < lines.length; i++) { if (lines[i].length() == 0) { // empty line found - end of headers break; } else if (lines[i].startsWith("Host: ")) { String host = lines[i].substring("Host: ".length()); if (path != null) { overridenUrl = "http://" + host + path; } } else { try { int colonIndex = lines[i].indexOf(':'); String name = lines[i].substring(0, colonIndex); String value = lines[i].substring(colonIndex + 2); result.setRequestHeader(name, value); } catch (IndexOutOfBoundsException ex) { throw new RequestSyntaxException("Invalid syntax for request header fields"); } } } // HTTP entity StringBuilder b = new StringBuilder(); for (i = i + 1; i < lines.length; i++) { b.append(lines[i]).append('\n'); } String body = b.toString(); if (! body.isEmpty()) { if (result instanceof EntityEnclosingMethod) { ((EntityEnclosingMethod) result).setRequestEntity(new StringRequestEntity(body)); } } try { if (overridenUrl != null) { result.setURI(new URI(overridenUrl, false)); } else { result.setURI(new URI(url, false)); } } catch (URIException ex) { throw new RequestSyntaxException("Invalid URI"); } return result; } }