/*********************************************************************
* HttpRequestParser.java
* created on 02.11.2006 by netseeker
* $Id: HttpRequestParser.java,v 1.1 2006/11/06 08:51:38 netseeker Exp $
* $Log: HttpRequestParser.java,v $
* Revision 1.1 2006/11/06 08:51:38 netseeker
* fixed java 1.4 support
* moved HTTP support classes to package de.netseeker.ejoe.http
*
* Revision 1.2 2006/11/05 23:56:27 netseeker
* added support of gzip encoding in case of http if the HTTP header does request it but the header byte did not (usually browser requests)
* added support of persistent http connections
*
* Revision 1.1 2006/11/05 16:30:37 netseeker
* finished the partial HTTP support
*
*
* ====================================================================
*
* Copyright 2005-2006 netseeker aka Michael Manske
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This file is part of the EJOE framework.
* For more information on the author, please see
* <http://www.manskes.de/>.
*
*********************************************************************/
package de.netseeker.ejoe.http;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Parser class for HTTP requests
*
* @author netseeker
* @since 0.3.9.1
*/
public class HttpRequestParser extends HttpHeaderParser
{
private static final Logger logger = Logger.getLogger( HttpRequestParser.class.getName() );
private static Pattern pUri = Pattern.compile( "\\A[A-Z]*+ +([^ ]+) +HTTP/[0-9\\.]+$.*",
Pattern.MULTILINE | Pattern.DOTALL
| Pattern.CASE_INSENSITIVE );
private static Pattern pAcceptEncoding = Pattern.compile( ".*^Accept-Encoding:\\s(.+)+$.*",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
| Pattern.DOTALL );
private String uri;
/**
* Constructs a new HTTP request parser using the given ByteBuffer
*
* @param buf ByteBuffer containing a HTTP request (HTTP header [ + optional preread content]
*/
public HttpRequestParser(ByteBuffer buf)
{
super( buf );
uri = extractURI();
Matcher matcher = pAcceptEncoding.matcher( getCharHeader() );
setCompression( (matcher.matches() && (matcher.group( 1 ).indexOf( "gzip" ) > -1)) );
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.http.HttpHeaderParser#isValid()
*/
public boolean isValid()
{
return super.isValid() && (uri != null) && (uri.length() >= 9);
}
/**
* Returns the requested URI
*
* @return the uri the requested URI
*/
public String getUri()
{
return uri;
}
/**
* Extracts the requested URI from the underlying buffer
*
* @return the requested URI
*/
protected String extractURI()
{
try
{
Matcher m = pUri.matcher( getCharHeader() );
if ( m.matches() )
{
return m.group( 1 ).trim();
}
}
catch ( Exception e )
{
logger.log( Level.WARNING, "Failed to determine requested URI!", e );
}
return null;
}
}