Changed name of method getAttribute to getHeader

This commit is contained in:
Ziver Koc 2010-08-13 22:39:15 +00:00
parent d62dea8139
commit 30c7bd7dd9
2 changed files with 30 additions and 33 deletions

View file

@ -21,7 +21,7 @@ public class HTTPHeaderParser {
private int httpCode; private int httpCode;
// Parameters // Parameters
private HashMap<String, String> attributes; private HashMap<String, String> headers;
private HashMap<String, String> cookies; private HashMap<String, String> cookies;
/** /**
@ -32,12 +32,12 @@ public class HTTPHeaderParser {
*/ */
public HTTPHeaderParser(BufferedReader in) throws IOException{ public HTTPHeaderParser(BufferedReader in) throws IOException{
url_attr = new HashMap<String, String>(); url_attr = new HashMap<String, String>();
attributes = new HashMap<String, String>(); headers = new HashMap<String, String>();
cookies = new HashMap<String, String>(); cookies = new HashMap<String, String>();
String tmp = null; String tmp = null;
if( (tmp=in.readLine()) != null && !tmp.isEmpty() ){ if( (tmp=in.readLine()) != null && !tmp.isEmpty() ){
parseStartLine( tmp ); parseFirstLine( tmp );
while( (tmp=in.readLine()) != null && !tmp.isEmpty() ){ while( (tmp=in.readLine()) != null && !tmp.isEmpty() ){
parseLine( tmp ); parseLine( tmp );
} }
@ -52,14 +52,14 @@ public class HTTPHeaderParser {
*/ */
public HTTPHeaderParser(String in){ public HTTPHeaderParser(String in){
url_attr = new HashMap<String, String>(); url_attr = new HashMap<String, String>();
attributes = new HashMap<String, String>(); headers = new HashMap<String, String>();
cookies = new HashMap<String, String>(); cookies = new HashMap<String, String>();
Scanner sc = new Scanner(in); Scanner sc = new Scanner(in);
sc.useDelimiter("\n"); sc.useDelimiter("\n");
String tmp = null; String tmp = null;
if( sc.hasNext() && !(tmp=sc.next()).isEmpty() ){ if( sc.hasNext() && !(tmp=sc.next()).isEmpty() ){
parseStartLine( tmp ); parseFirstLine( tmp );
while( sc.hasNext() && !(tmp=sc.next()).isEmpty() ){ while( sc.hasNext() && !(tmp=sc.next()).isEmpty() ){
parseLine( tmp ); parseLine( tmp );
} }
@ -75,7 +75,7 @@ public class HTTPHeaderParser {
* @param map The HashMap to put the variables to * @param map The HashMap to put the variables to
* @return The path and file name as a String * @return The path and file name as a String
*/ */
protected void parseStartLine(String line){ protected void parseFirstLine(String line){
// Server Response // Server Response
if( line.startsWith("HTTP/") ){ if( line.startsWith("HTTP/") ){
version = Float.parseFloat( line.substring( 5 , 8) ); version = Float.parseFloat( line.substring( 5 , 8) );
@ -88,9 +88,10 @@ public class HTTPHeaderParser {
line = (line.substring(type.length()+1, line.lastIndexOf("HTTP/"))).trim(); line = (line.substring(type.length()+1, line.lastIndexOf("HTTP/"))).trim();
// parse URL and attributes // parse URL and attributes
if(line.indexOf('?') > -1){ int index = line.indexOf('?');
url = line.substring(0, line.indexOf('?')); if(index > -1){
line = line.substring(line.indexOf('?')+1, line.length()); url = line.substring(0, index );
line = line.substring( index+1, line.length());
parseUrlAttributes(line, url_attr); parseUrlAttributes(line, url_attr);
} }
else{ else{
@ -127,7 +128,7 @@ public class HTTPHeaderParser {
*/ */
protected void parseLine(String line){ protected void parseLine(String line){
String[] data = colonPattern.split( line, 2 ); String[] data = colonPattern.split( line, 2 );
attributes.put( headers.put(
data[0].trim().toUpperCase(), // Key data[0].trim().toUpperCase(), // Key
(data.length>1 ? data[1] : "").trim()); //Value (data.length>1 ? data[1] : "").trim()); //Value
} }
@ -139,8 +140,8 @@ public class HTTPHeaderParser {
* @return a HashMap with cookie values * @return a HashMap with cookie values
*/ */
protected void parseCookies(){ protected void parseCookies(){
if( attributes.containsKey("COOKIE") ){ if( headers.containsKey("COOKIE") ){
String[] tmp = semiColonPattern.split( attributes.get("COOKIE") ); String[] tmp = semiColonPattern.split( headers.get("COOKIE") );
String[] tmp2; String[] tmp2;
for(String cookie : tmp){ for(String cookie : tmp){
tmp2 = equalPattern.split(cookie, 2); tmp2 = equalPattern.split(cookie, 2);
@ -186,8 +187,8 @@ public class HTTPHeaderParser {
* Returns the HTTP attribute value of the given name, * Returns the HTTP attribute value of the given name,
* returns null if there is no such attribute * returns null if there is no such attribute
*/ */
public String getHTTPAttribute(String name){ public String getHeader(String name){
return attributes.get( name.toUpperCase() ); return headers.get( name.toUpperCase() );
} }
/** /**
* Returns the cookie value of the given name, * Returns the cookie value of the given name,
@ -199,34 +200,30 @@ public class HTTPHeaderParser {
/** /**
* @return athe parsed cookies * @return a map of the parsed cookies
*/ */
public HashMap<String, String> getCookies(){ public HashMap<String, String> getCookies(){
return cookies; return cookies;
} }
/** /**
* @return the parsed URL values * @return a map of the parsed URL attributes
*/ */
public HashMap<String, String> getURLAttributes(){ public HashMap<String, String> getURLAttributes(){
return url_attr; return url_attr;
} }
/** /**
* @return the parsed header attributes * @return a map of the parsed headers
*/ */
public HashMap<String, String> getAttributes(){ public HashMap<String, String> getHeaders(){
return attributes; return headers;
} }
public String toString(){ public String toString(){
StringBuffer tmp = new StringBuffer(); StringBuffer tmp = new StringBuffer();
tmp.append("Type: "); tmp.append("Type: ").append(type);
tmp.append(type); tmp.append("\nHTTP Version: HTTP/").append(version);
tmp.append("\nHTTP Version: HTTP/"); tmp.append("\nURL: ").append(url);
tmp.append(version);
tmp.append("\nURL: ");
tmp.append(url);
for( String key : url_attr.keySet() ){ for( String key : url_attr.keySet() ){
tmp.append("\nURL Attr: "); tmp.append("\nURL Attr: ");
@ -235,11 +232,11 @@ public class HTTPHeaderParser {
tmp.append( url_attr.get(key) ); tmp.append( url_attr.get(key) );
} }
for( String key : attributes.keySet() ){ for( String key : headers.keySet() ){
tmp.append("\nHTTP Attr: "); tmp.append("\nHeader: ");
tmp.append(key); tmp.append(key);
tmp.append("="); tmp.append("=");
tmp.append( attributes.get(key) ); tmp.append( headers.get(key) );
} }
for( String key : cookies.keySet() ){ for( String key : cookies.keySet() ){

View file

@ -153,15 +153,15 @@ public class HttpServer extends ThreadedTCPNetworkServer{
HTTPHeaderParser parser = new HTTPHeaderParser(in); HTTPHeaderParser parser = new HTTPHeaderParser(in);
logger.finest(parser.toString()); logger.finest(parser.toString());
client_info = parser.getAttributes(); client_info = parser.getHeaders();
request = parser.getURLAttributes(); request = parser.getURLAttributes();
cookie = parser.getCookies(); cookie = parser.getCookies();
//******* Read in the post data if available //******* Read in the post data if available
if( parser.getHTTPAttribute("Content-Length")!=null ){ if( parser.getHeader("Content-Length")!=null ){
// Reads the post data size // Reads the post data size
tmp = parser.getHTTPAttribute("Content-Length"); tmp = parser.getHeader("Content-Length");
int post_data_length = Integer.parseInt( tmp ); int post_data_length = Integer.parseInt( tmp );
// read the data // read the data
StringBuffer tmpb = new StringBuffer(); StringBuffer tmpb = new StringBuffer();
@ -170,7 +170,7 @@ public class HttpServer extends ThreadedTCPNetworkServer{
tmpb.append((char)in.read()); tmpb.append((char)in.read());
} }
tmp = parser.getHTTPAttribute("Content-Type"); tmp = parser.getHeader("Content-Type");
if( tmp.contains("application/x-www-form-urlencoded") ){ if( tmp.contains("application/x-www-form-urlencoded") ){
// get the variables // get the variables
HTTPHeaderParser.parseUrlAttributes( tmpb.toString(), request ); HTTPHeaderParser.parseUrlAttributes( tmpb.toString(), request );