1 /**********************************************************************
2 * HttpRequestParser.java
3 * created on 02.11.2006 by netseeker
4 * $Id: HttpRequestParser.java,v 1.1 2006/11/06 08:51:38 netseeker Exp $
5 * $Log: HttpRequestParser.java,v $
6 * Revision 1.1 2006/11/06 08:51:38 netseeker
7 * fixed java 1.4 support
8 * moved HTTP support classes to package de.netseeker.ejoe.http
9 *
10 * Revision 1.2 2006/11/05 23:56:27 netseeker
11 * 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)
12 * added support of persistent http connections
13 *
14 * Revision 1.1 2006/11/05 16:30:37 netseeker
15 * finished the partial HTTP support
16 *
17 *
18 * ====================================================================
19 *
20 * Copyright 2005-2006 netseeker aka Michael Manske
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 * ====================================================================
34 *
35 * This file is part of the EJOE framework.
36 * For more information on the author, please see
37 * <http://www.manskes.de/>.
38 *
39 *********************************************************************/
40 package de.netseeker.ejoe.http;
41
42 import java.nio.ByteBuffer;
43 import java.util.logging.Level;
44 import java.util.logging.Logger;
45 import java.util.regex.Matcher;
46 import java.util.regex.Pattern;
47
48 /***
49 * Parser class for HTTP requests
50 *
51 * @author netseeker
52 * @since 0.3.9.1
53 */
54 public class HttpRequestParser extends HttpHeaderParser
55 {
56 private static final Logger logger = Logger.getLogger( HttpRequestParser.class.getName() );
57
58 private static Pattern pUri = Pattern.compile( "//A[A-Z]*+ +([^ ]+) +HTTP/[0-9//.]+$.*",
59 Pattern.MULTILINE | Pattern.DOTALL
60 | Pattern.CASE_INSENSITIVE );
61
62 private static Pattern pAcceptEncoding = Pattern.compile( ".*^Accept-Encoding://s(.+)+$.*",
63 Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
64 | Pattern.DOTALL );
65
66 private String uri;
67
68 /***
69 * Constructs a new HTTP request parser using the given ByteBuffer
70 *
71 * @param buf ByteBuffer containing a HTTP request (HTTP header [ + optional preread content]
72 */
73 public HttpRequestParser(ByteBuffer buf)
74 {
75 super( buf );
76 uri = extractURI();
77 Matcher matcher = pAcceptEncoding.matcher( getCharHeader() );
78 setCompression( (matcher.matches() && (matcher.group( 1 ).indexOf( "gzip" ) > -1)) );
79 }
80
81
82
83
84
85
86 public boolean isValid()
87 {
88 return super.isValid() && (uri != null) && (uri.length() >= 9);
89 }
90
91 /***
92 * Returns the requested URI
93 *
94 * @return the uri the requested URI
95 */
96 public String getUri()
97 {
98 return uri;
99 }
100
101 /***
102 * Extracts the requested URI from the underlying buffer
103 *
104 * @return the requested URI
105 */
106 protected String extractURI()
107 {
108 try
109 {
110 Matcher m = pUri.matcher( getCharHeader() );
111 if ( m.matches() )
112 {
113 return m.group( 1 ).trim();
114 }
115 }
116 catch ( Exception e )
117 {
118 logger.log( Level.WARNING, "Failed to determine requested URI!", e );
119 }
120
121 return null;
122 }
123 }