1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package net.jot.web.server; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; import java.net.URLDecoder; import java.util.Hashtable; import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * // TODO: deal with anchors in url (#) * // TODO: deal with jsessionID in URL * * * Parse a socket input (web request) into an easy to use JOTWebRequest object * @author thibautc */ public class JOTRequestParser { // Ex: GET /test?toto=3&block=4 HTTP/1.1 private static final Pattern REQUEST_PARSER = Pattern.compile("^(\\S+)\\s+([^\\? ]*)(\\?\\S*)?\\s+(HTTP/\\d+\\.\\d+)"); private static final Pattern HEADER_PATTERN = Pattern.compile("^(\\S+)\\s*:(.*)"); private final static String JSESSIONID_STR=";jsessionid="; public static JOTWebRequest parseRequest(Socket socket) throws Exception { boolean valid=false; JOTWebRequest request = new JOTWebRequest(); request.socket = socket; request.setLocalHost(socket.getLocalAddress().getHostAddress()); request.setLocalPort(socket.getLocalPort()); request.setRemoteHost(socket.getInetAddress().getHostAddress()); request.setRemotePort(socket.getPort()); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = ""; while (reader.ready() && line != null) { line = reader.readLine(); //System.out.println("ln: "+line); // handle the main request line (path/query/protocol) Matcher m2 = HEADER_PATTERN.matcher(line); Matcher m = REQUEST_PARSER.matcher(line); if (m.matches()) { valid=true; parseRequestLine(request, line); } else if (m2.matches()) { String key=m2.group(1); request.addHeader(key, m2.group(2)); } //handler.handleGetRequest(socket, path, params);*/ } if(!valid) throw new IllegalArgumentException("Main Request Line missing or not parseable !"); return request; } /** * Hashtable of key -> Vector of strings (values) * @param parameters * @return * @throws java.lang.Exception */ protected static Hashtable parseParameters(String parameters) throws Exception { //TODO: parameters with multiple values ?? Hashtable hash = new Hashtable(); if (parameters != null) { String[] params = parameters.split("&"); for (int i = 0; i != params.length; i++) { String p = params[i]; int index = p.indexOf("="); if (index != -1) { addParamToHash(hash,URLDecoder.decode(p.substring(0, index), "UTF-8"), URLDecoder.decode(p.substring(index + 1, p.length()), "UTF-8")); } else { addParamToHash(hash,URLDecoder.decode(p, "UTF-8"), ""); } } } return hash; } /** * Deal with params with multiple values * @param hash * @param name * @param value */ private static void addParamToHash(Hashtable hash, String name, String value) { Vector v=new Vector(); if(hash.containsKey(name)) { v=(Vector)hash.get(name); } v.add(value); hash.put(name,v); } /** * Builds a "fake" request for testing. * Does not set any headers, etc.., you can add those manually as needed * @param socket: a scoket for simulating a connection * @param requestLine: ie: something like "GET /test#anchor?toto=3&block=4 HTTP/1.1" * @return */ public static JOTWebRequest getTestRequest(Socket socket, String requestLine) throws Exception { JOTWebRequest request = new JOTWebRequest(); request.socket = socket; request.setLocalHost(socket.getLocalAddress().getHostAddress()); request.setLocalPort(socket.getLocalPort()); request.setRemoteHost(socket.getInetAddress().getHostAddress()); request.setRemotePort(socket.getPort()); Matcher m = REQUEST_PARSER.matcher(requestLine); if (!m.matches()) { throw new IllegalArgumentException("Main Request Line not parseable : "+requestLine); } parseRequestLine(request, requestLine); return request; } private static void parseRequestLine(JOTWebRequest request, String line) throws Exception { Matcher m = REQUEST_PARSER.matcher(line); if (m.matches()) { String method = m.group(1); String path = URLDecoder.decode(m.group(2), "UTF-8"); int index=path.indexOf(JSESSIONID_STR); if(index>=0) { // sessionId in request (probably cookies disabled) request.setRequestedSessionId(path.substring(index+JSESSIONID_STR.length(),path.length()),false); path=path.substring(0,index); } String params = m.group(3); if (params != null && params.length() > 0) { params = params.substring(1, params.length()); } String protocol = m.group(4); Hashtable params2 = parseParameters(params); request.setRawRequestLine(line); request.setMethod(method); request.setRawParameters(params); request.setParameters(params2); request.setPath(path); request.setProtocol(protocol); } } }