| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.util.http; |
| 16 |
|
| 17 |
import java.io.IOException; |
| 18 |
import java.io.InputStream; |
| 19 |
|
| 20 |
import org.apache.commons.logging.Log; |
| 21 |
import org.apache.commons.logging.LogFactory; |
| 22 |
|
| 23 |
public abstract class HTTPParser { |
| 24 |
|
| 25 |
private static Log logger = |
| 26 |
LogFactory.getLog(HTTPParser.class.getName()); |
| 27 |
|
| 28 |
protected String _server; |
| 29 |
protected String _host; |
| 30 |
protected String _contentType; |
| 31 |
protected String _connection; |
| 32 |
protected long _contentLength; |
| 33 |
protected boolean _chunked; |
| 34 |
protected LineReader _reader; |
| 35 |
|
| 36 |
public HTTPParser(InputStream is) |
| 37 |
throws IOException { |
| 38 |
_contentLength = -1; |
| 39 |
_chunked = false; |
| 40 |
setInputStream(is); |
| 41 |
parse(); |
| 42 |
} |
| 43 |
|
| 44 |
public String getContentType() { |
| 45 |
return _contentType; |
| 46 |
} |
| 47 |
|
| 48 |
public long getContentLength() { |
| 49 |
return _contentLength; |
| 50 |
} |
| 51 |
|
| 52 |
public boolean isChunked() { |
| 53 |
return _chunked; |
| 54 |
} |
| 55 |
|
| 56 |
public LineReader getReader() { |
| 57 |
return _reader; |
| 58 |
} |
| 59 |
|
| 60 |
public void setInputStream(InputStream in) { |
| 61 |
_reader = new LineReader(in); |
| 62 |
} |
| 63 |
|
| 64 |
public abstract void parseHead(String line) |
| 65 |
throws IOException; |
| 66 |
|
| 67 |
/** |
| 68 |
* Parses the typical HTTP header. |
| 69 |
* @exception IOException if a connection fails or bad/incomplete request |
| 70 |
*/ |
| 71 |
protected void parse() |
| 72 |
throws IOException { |
| 73 |
|
| 74 |
String line; |
| 75 |
line = _reader.readLine(); |
| 76 |
if (logger.isTraceEnabled()) { |
| 77 |
logger.trace(line); |
| 78 |
} |
| 79 |
parseHead(line); |
| 80 |
|
| 81 |
while ( (line = _reader.readLine()).length() != 0 ) { |
| 82 |
if (logger.isTraceEnabled()) { |
| 83 |
logger.trace(line); |
| 84 |
} |
| 85 |
|
| 86 |
if (line.startsWith(HTTPProtocol.CONNECTION)) { |
| 87 |
_connection = getRest(line, HTTPProtocol.CONNECTION.length()); |
| 88 |
} else if (line.startsWith(HTTPProtocol.SERVER)) { |
| 89 |
_server = getRest(line, HTTPProtocol.SERVER.length()); |
| 90 |
} else if (line.startsWith(HTTPProtocol.CONTENT_TYPE)) { |
| 91 |
_contentType = getRest(line, HTTPProtocol.CONTENT_TYPE.length()); |
| 92 |
} else if (line.startsWith(HTTPProtocol.CONTENT_LENGTH)) { |
| 93 |
_contentLength = Long.parseLong(getRest(line, |
| 94 |
HTTPProtocol.CONTENT_LENGTH.length())); |
| 95 |
} else if (line.startsWith(HTTPProtocol.HOST)){ |
| 96 |
_host = getRest(line, HTTPProtocol.HOST.length()); |
| 97 |
} else if (line.startsWith(HTTPProtocol.CHUNKED)) { |
| 98 |
_chunked = true; |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
protected static final String getRest(String line, int index) { |
| 105 |
return line.substring(index).trim(); |
| 106 |
} |
| 107 |
|
| 108 |
} |