private void parseRequest(SocketInputStream input, OutputStream output) throws IOException, ServletException { // Parse the incoming request line input.readRequestLine(requestLine); String method = new String(requestLine.method, 0, requestLine.methodEnd); String uri = null; String protocol = new String(requestLine.protocol, 0, requestLine.protocolEnd); // Validate the incoming request line if (method.length() < 1) { throw new ServletException("Missing HTTP request method"); } else if (requestLine.uriEnd < 1) { throw new ServletException("Missing HTTP request URI"); } // Parse any query parameters out of the request URI int question = requestLine.indexOf("?"); if (question >= 0) { request.setQueryString( new String(requestLine.uri, question + 1, requestLine.uriEnd - question - 1)); uri = new String(requestLine.uri, 0, question); } else { request.setQueryString(null); uri = new String(requestLine.uri, 0, requestLine.uriEnd); } // Checking for an absolute URI (with the HTTP protocol) if (!uri.startsWith("/")) { int pos = uri.indexOf("://"); // Parsing out protocol and host name if (pos != -1) { pos = uri.indexOf('/', pos + 3); if (pos == -1) { uri = ""; } else { uri = uri.substring(pos); } } } // Parse any requested session ID out of the request URI String match = ";jsessionid="; int semicolon = uri.indexOf(match); if (semicolon >= 0) { String rest = uri.substring(semicolon + match.length()); int semicolon2 = rest.indexOf(';'); if (semicolon2 >= 0) { request.setRequestedSessionId(rest.substring(0, semicolon2)); rest = rest.substring(semicolon2); } else { request.setRequestedSessionId(rest); rest = ""; } request.setRequestedSessionURL(true); uri = uri.substring(0, semicolon) + rest; } else { request.setRequestedSessionId(null); request.setRequestedSessionURL(false); } // Normalize URI (using String operations at the moment) String normalizedUri = normalize(uri); // Set the corresponding request properties ((HttpRequest) request).setMethod(method); request.setProtocol(protocol); if (normalizedUri != null) { ((HttpRequest) request).setRequestURI(normalizedUri); } else { ((HttpRequest) request).setRequestURI(uri); } if (normalizedUri == null) { throw new ServletException("Invalid URI: " + uri + "'"); } }
/** * Parse the incoming HTTP request and set the corresponding HTTP request properties. * * @param input The input stream attached to our socket * @param output The output stream of the socket * @exception IOException if an input/output error occurs * @exception ServletException if a parsing error occurs */ private void parseRequest(SocketInputStream input, OutputStream output) throws IOException, ServletException { // Parse the incoming request line input.readRequestLine(requestLine); // When the previous method returns, we're actually processing a // request status = Constants.PROCESSOR_ACTIVE; String method = new String(requestLine.method, 0, requestLine.methodEnd); String uri = null; String protocol = new String(requestLine.protocol, 0, requestLine.protocolEnd); // System.out.println(" Method:" + method + "_ Uri:" + uri // + "_ Protocol:" + protocol); if (protocol.length() == 0) protocol = "HTTP/0.9"; // Now check if the connection should be kept alive after parsing the // request. if (protocol.equals("HTTP/1.1")) { http11 = true; sendAck = false; } else { http11 = false; sendAck = false; // For HTTP/1.0, connection are not persistent by default, // unless specified with a Connection: Keep-Alive header. keepAlive = false; } // Validate the incoming request line if (method.length() < 1) { throw new ServletException(sm.getString("httpProcessor.parseRequest.method")); } else if (requestLine.uriEnd < 1) { throw new ServletException(sm.getString("httpProcessor.parseRequest.uri")); } // Parse any query parameters out of the request URI int question = requestLine.indexOf("?"); if (question >= 0) { request.setQueryString( new String(requestLine.uri, question + 1, requestLine.uriEnd - question - 1)); if (debug >= 1) log(" Query string is " + ((HttpServletRequest) request.getRequest()).getQueryString()); uri = new String(requestLine.uri, 0, question); } else { request.setQueryString(null); uri = new String(requestLine.uri, 0, requestLine.uriEnd); } // Checking for an absolute URI (with the HTTP protocol) if (!uri.startsWith("/")) { int pos = uri.indexOf("://"); // Parsing out protocol and host name if (pos != -1) { pos = uri.indexOf('/', pos + 3); if (pos == -1) { uri = ""; } else { uri = uri.substring(pos); } } } // Parse any requested session ID out of the request URI int semicolon = uri.indexOf(match); if (semicolon >= 0) { String rest = uri.substring(semicolon + match.length()); int semicolon2 = rest.indexOf(';'); if (semicolon2 >= 0) { request.setRequestedSessionId(rest.substring(0, semicolon2)); rest = rest.substring(semicolon2); } else { request.setRequestedSessionId(rest); rest = ""; } request.setRequestedSessionURL(true); uri = uri.substring(0, semicolon) + rest; if (debug >= 1) log( " Requested URL session id is " + ((HttpServletRequest) request.getRequest()).getRequestedSessionId()); } else { request.setRequestedSessionId(null); request.setRequestedSessionURL(false); } // Normalize URI (using String operations at the moment) String normalizedUri = normalize(uri); if (debug >= 1) log("Normalized: '" + uri + "' to '" + normalizedUri + "'"); // Set the corresponding request properties ((HttpRequest) request).setMethod(method); request.setProtocol(protocol); if (normalizedUri != null) { ((HttpRequest) request).setRequestURI(normalizedUri); } else { ((HttpRequest) request).setRequestURI(uri); } request.setSecure(connector.getSecure()); request.setScheme(connector.getScheme()); if (normalizedUri == null) { log(" Invalid request URI: '" + uri + "'"); throw new ServletException("Invalid URI: " + uri + "'"); } if (debug >= 1) log(" Request is '" + method + "' for '" + uri + "' with protocol '" + protocol + "'"); }