Many bugfixes and improvements
This commit is contained in:
parent
c3e3bbf787
commit
363e0c6cfc
52 changed files with 2021 additions and 982 deletions
98
src/zutil/net/http/HttpClient.java
Normal file
98
src/zutil/net/http/HttpClient.java
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package zutil.net.http;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.Socket;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
|
||||
import zutil.net.http.HttpPrintStream.HttpMessageType;
|
||||
|
||||
/**
|
||||
* This class connects to a HTTP server and
|
||||
* parses the result
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class HttpClient {
|
||||
public static enum HttpRequestType{
|
||||
GET, POST
|
||||
}
|
||||
|
||||
private HttpURL url;
|
||||
private HttpRequestType type;
|
||||
private HashMap<String,String> headers;
|
||||
private HashMap<String,String> cookies;
|
||||
|
||||
|
||||
public static HttpClient POST(){
|
||||
return new HttpClient( HttpRequestType.POST );
|
||||
}
|
||||
|
||||
public static HttpClient GET(){
|
||||
return new HttpClient( HttpRequestType.GET );
|
||||
}
|
||||
|
||||
|
||||
private HttpClient(HttpRequestType type){
|
||||
this.type = type;
|
||||
headers = new HashMap<String,String>();
|
||||
cookies = new HashMap<String,String>();
|
||||
}
|
||||
|
||||
|
||||
public void setURL( URL url){
|
||||
this.url = new HttpURL( url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a parameter to the request
|
||||
*/
|
||||
public void setParameter( String key, String value ){
|
||||
url.setParameter(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a cookie to the request
|
||||
*/
|
||||
public void setCookie( String key, String value ){
|
||||
cookies.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a header value to the request
|
||||
*/
|
||||
public void setHeader( String key, String value ){
|
||||
headers.put(key, value);
|
||||
}
|
||||
|
||||
public HttpHeaderParser send() throws IOException{
|
||||
Socket conn = new Socket( url.getHost(), url.getPort());
|
||||
|
||||
// Request
|
||||
HttpPrintStream request = new HttpPrintStream( conn.getOutputStream(), HttpMessageType.REQUEST );
|
||||
request.setRequestType( type.toString() );
|
||||
request.setRequestURL( url.getHttpURL() );
|
||||
request.setHeaders( headers );
|
||||
request.setCookies( cookies );
|
||||
|
||||
if( type == HttpRequestType.POST ){
|
||||
String data = url.getParameterString();
|
||||
request.setHeader("Content-Length", data);
|
||||
request.println();
|
||||
request.print( data );
|
||||
|
||||
}
|
||||
else
|
||||
request.println("");
|
||||
request.flush();
|
||||
|
||||
// Response
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
HttpHeaderParser response = new HttpHeaderParser( in );
|
||||
conn.close();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ import java.util.HashMap;
|
|||
import java.util.Scanner;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class HTTPHeaderParser {
|
||||
public class HttpHeaderParser {
|
||||
// Some Cached regex's
|
||||
private static final Pattern colonPattern = Pattern.compile(":");
|
||||
private static final Pattern equalPattern = Pattern.compile("=");
|
||||
|
|
@ -30,7 +30,7 @@ public class HTTPHeaderParser {
|
|||
* @param in is the stream
|
||||
* @throws IOException
|
||||
*/
|
||||
public HTTPHeaderParser(BufferedReader in) throws IOException{
|
||||
public HttpHeaderParser(BufferedReader in) throws IOException{
|
||||
url_attr = new HashMap<String, String>();
|
||||
headers = new HashMap<String, String>();
|
||||
cookies = new HashMap<String, String>();
|
||||
|
|
@ -50,7 +50,7 @@ public class HTTPHeaderParser {
|
|||
*
|
||||
* @param in is the string
|
||||
*/
|
||||
public HTTPHeaderParser(String in){
|
||||
public HttpHeaderParser(String in){
|
||||
url_attr = new HashMap<String, String>();
|
||||
headers = new HashMap<String, String>();
|
||||
cookies = new HashMap<String, String>();
|
||||
|
|
@ -92,7 +92,7 @@ public class HTTPHeaderParser {
|
|||
if(index > -1){
|
||||
url = line.substring(0, index );
|
||||
line = line.substring( index+1, line.length());
|
||||
parseUrlAttributes(line, url_attr);
|
||||
parseURLParameters(line, url_attr);
|
||||
}
|
||||
else{
|
||||
url = line;
|
||||
|
|
@ -108,9 +108,9 @@ public class HTTPHeaderParser {
|
|||
*
|
||||
* @param attributes is the String containing all the attributes
|
||||
*/
|
||||
public static HashMap<String, String> parseUrlAttributes( String attributes ){
|
||||
public static HashMap<String, String> parseURLParameters( String attributes ){
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
parseUrlAttributes(attributes, map);
|
||||
parseURLParameters(attributes, map);
|
||||
return map;
|
||||
}
|
||||
|
||||
|
|
@ -121,7 +121,7 @@ public class HTTPHeaderParser {
|
|||
* @param attributes is the String containing all the attributes
|
||||
* @param map is the HashMap to put all the values into
|
||||
*/
|
||||
public static void parseUrlAttributes(String attributes, HashMap<String, String> map){
|
||||
public static void parseURLParameters(String attributes, HashMap<String, String> map){
|
||||
String[] tmp;
|
||||
// get the variables
|
||||
String[] data = andPattern.split( attributes );
|
||||
|
|
@ -13,13 +13,13 @@ import java.util.HashMap;
|
|||
*/
|
||||
public class HttpPrintStream extends PrintStream{
|
||||
// Defines the type of message
|
||||
public enum HTTPMessageType{
|
||||
public enum HttpMessageType{
|
||||
REQUEST,
|
||||
RESPONSE
|
||||
}
|
||||
|
||||
// This defines the type of message that will be generated
|
||||
private HTTPMessageType message_type;
|
||||
private HttpMessageType message_type;
|
||||
// The status code of the message, ONLY for response
|
||||
private Integer res_status_code;
|
||||
// The request type of the message ONLY for request
|
||||
|
|
@ -27,9 +27,9 @@ public class HttpPrintStream extends PrintStream{
|
|||
// The requesting url ONLY for request
|
||||
private String req_url;
|
||||
// An Map of all the header values
|
||||
private HashMap<String, String> header;
|
||||
private HashMap<String, String> headers;
|
||||
// An Map of all the cookies
|
||||
private HashMap<String, String> cookie;
|
||||
private HashMap<String, String> cookies;
|
||||
// The buffered header
|
||||
private StringBuffer buffer;
|
||||
// If the header buffering is enabled
|
||||
|
|
@ -42,7 +42,7 @@ public class HttpPrintStream extends PrintStream{
|
|||
* @param out is the OutputStream to send the message
|
||||
*/
|
||||
public HttpPrintStream(OutputStream out) {
|
||||
this( out, HTTPMessageType.RESPONSE );
|
||||
this( out, HttpMessageType.RESPONSE );
|
||||
}
|
||||
/**
|
||||
* Creates an new instance of HttpPrintStream with
|
||||
|
|
@ -51,13 +51,13 @@ public class HttpPrintStream extends PrintStream{
|
|||
* @param out is the OutputStream to send the message
|
||||
* @param type is the type of message
|
||||
*/
|
||||
public HttpPrintStream(OutputStream out, HTTPMessageType type) {
|
||||
public HttpPrintStream(OutputStream out, HttpMessageType type) {
|
||||
super(out);
|
||||
|
||||
this.message_type = type;
|
||||
res_status_code = 0;
|
||||
header = new HashMap<String, String>();
|
||||
cookie = new HashMap<String, String>();
|
||||
headers = new HashMap<String, String>();
|
||||
cookies = new HashMap<String, String>();
|
||||
buffer = new StringBuffer();
|
||||
buffer_enabled = false;
|
||||
}
|
||||
|
|
@ -84,9 +84,9 @@ public class HttpPrintStream extends PrintStream{
|
|||
* @throws Exception Throws exception if the header has already been sent
|
||||
*/
|
||||
public void setCookie(String key, String value) throws RuntimeException{
|
||||
if(cookie == null)
|
||||
if(cookies == null)
|
||||
throw new RuntimeException("Header already sent!!!");
|
||||
cookie.put(key, value);
|
||||
cookies.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -97,9 +97,9 @@ public class HttpPrintStream extends PrintStream{
|
|||
* @throws Exception Throws exception if the header has already been sent
|
||||
*/
|
||||
public void setHeader(String key, String value) throws RuntimeException{
|
||||
if(header == null)
|
||||
if(headers == null)
|
||||
throw new RuntimeException("Header already sent!!!");
|
||||
header.put(key, value);
|
||||
headers.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -111,7 +111,7 @@ public class HttpPrintStream extends PrintStream{
|
|||
public void setStatusCode(int code) throws RuntimeException{
|
||||
if( res_status_code == null )
|
||||
throw new RuntimeException("Header already sent!!!");
|
||||
if( message_type != HTTPMessageType.RESPONSE )
|
||||
if( message_type != HttpMessageType.RESPONSE )
|
||||
throw new RuntimeException("Status Code is only available in HTTP RESPONSE!!!");
|
||||
res_status_code = code;
|
||||
}
|
||||
|
|
@ -125,7 +125,7 @@ public class HttpPrintStream extends PrintStream{
|
|||
public void setRequestType(String req_type) throws RuntimeException{
|
||||
if( req_type == null )
|
||||
throw new RuntimeException("Header already sent!!!");
|
||||
if( message_type != HTTPMessageType.REQUEST )
|
||||
if( message_type != HttpMessageType.REQUEST )
|
||||
throw new RuntimeException("Request Message Type is only available in HTTP REQUEST!!!");
|
||||
this.req_type = req_type;
|
||||
}
|
||||
|
|
@ -138,11 +138,18 @@ public class HttpPrintStream extends PrintStream{
|
|||
public void setRequestURL(String req_url) throws RuntimeException{
|
||||
if( req_url == null )
|
||||
throw new RuntimeException("Header already sent!!!");
|
||||
if( message_type != HTTPMessageType.REQUEST )
|
||||
if( message_type != HttpMessageType.REQUEST )
|
||||
throw new RuntimeException("Request URL is only available in HTTP REQUEST!!!");
|
||||
this.req_url = req_url;
|
||||
}
|
||||
|
||||
protected void setHeaders( HashMap<String,String> map ){
|
||||
headers = map;
|
||||
}
|
||||
protected void setCookies( HashMap<String,String> map ){
|
||||
cookies = map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints with a new line
|
||||
*/
|
||||
|
|
@ -166,40 +173,40 @@ public class HttpPrintStream extends PrintStream{
|
|||
}
|
||||
else{
|
||||
if(res_status_code != null){
|
||||
if( message_type==HTTPMessageType.REQUEST )
|
||||
super.print(req_type+" "+req_url+" HTTP/1.1");
|
||||
if( message_type==HttpMessageType.REQUEST )
|
||||
super.print(req_type+" "+req_url+" HTTP/1.0");
|
||||
else
|
||||
super.print("HTTP/1.1 "+res_status_code+" "+getStatusString(res_status_code));
|
||||
super.print("HTTP/1.0 "+res_status_code+" "+getStatusString(res_status_code));
|
||||
super.println();
|
||||
res_status_code = null;
|
||||
req_type = null;
|
||||
req_url = null;
|
||||
}
|
||||
if(header != null){
|
||||
for(String key : header.keySet()){
|
||||
super.print(key+": "+header.get(key));
|
||||
if(headers != null){
|
||||
for(String key : headers.keySet()){
|
||||
super.print(key+": "+headers.get(key));
|
||||
super.println();
|
||||
}
|
||||
header = null;
|
||||
headers = null;
|
||||
}
|
||||
if(cookie != null){
|
||||
if( !cookie.isEmpty() ){
|
||||
if( message_type==HTTPMessageType.REQUEST ){
|
||||
if(cookies != null){
|
||||
if( !cookies.isEmpty() ){
|
||||
if( message_type==HttpMessageType.REQUEST ){
|
||||
super.print("Cookie: ");
|
||||
for(String key : cookie.keySet()){
|
||||
super.print(key+"="+cookie.get(key)+"; ");
|
||||
for(String key : cookies.keySet()){
|
||||
super.print(key+"="+cookies.get(key)+"; ");
|
||||
}
|
||||
super.println();
|
||||
}
|
||||
else{
|
||||
for(String key : cookie.keySet()){
|
||||
super.print("Set-Cookie: "+key+"="+cookie.get(key)+";");
|
||||
for(String key : cookies.keySet()){
|
||||
super.print("Set-Cookie: "+key+"="+cookies.get(key)+";");
|
||||
super.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
super.println();
|
||||
cookie = null;
|
||||
cookies = null;
|
||||
}
|
||||
super.print(s);
|
||||
}
|
||||
|
|
@ -215,7 +222,7 @@ public class HttpPrintStream extends PrintStream{
|
|||
buffer.delete(0, buffer.length());
|
||||
buffer_enabled = true;
|
||||
}
|
||||
else if(res_status_code != null || header != null || cookie != null){
|
||||
else if(res_status_code != null || headers != null || cookies != null){
|
||||
printOrBuffer("");
|
||||
}
|
||||
super.flush();
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ public class HttpServer extends ThreadedTCPNetworkServer{
|
|||
try {
|
||||
logger.finer("Reciving Http Request!!!");
|
||||
|
||||
HTTPHeaderParser parser = new HTTPHeaderParser(in);
|
||||
HttpHeaderParser parser = new HttpHeaderParser(in);
|
||||
logger.finest(parser.toString());
|
||||
client_info = parser.getHeaders();
|
||||
request = parser.getURLAttributes();
|
||||
|
|
@ -173,7 +173,7 @@ public class HttpServer extends ThreadedTCPNetworkServer{
|
|||
tmp = parser.getHeader("Content-Type");
|
||||
if( tmp.contains("application/x-www-form-urlencoded") ){
|
||||
// get the variables
|
||||
HTTPHeaderParser.parseUrlAttributes( tmpb.toString(), request );
|
||||
HttpHeaderParser.parseURLParameters( tmpb.toString(), request );
|
||||
}
|
||||
else if( tmp.contains("application/soap+xml" ) ||
|
||||
tmp.contains("text/xml") ||
|
||||
|
|
|
|||
139
src/zutil/net/http/HttpURL.java
Normal file
139
src/zutil/net/http/HttpURL.java
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
package zutil.net.http;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Handles URLs in the HTTP protocol
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class HttpURL {
|
||||
public static final String PROTOCOL_SEPARATOR = "://";
|
||||
public static final String PORT_SEPARATOR = ":";
|
||||
public static final String PATH_SEPARATOR = "/";
|
||||
public static final String PARAMETER_SEPARATOR = "?";
|
||||
public static final String ANCHOR_SEPARATOR = "#";
|
||||
|
||||
private String protocol = "";
|
||||
private String host = "127.0.0.1";
|
||||
private int port = -1;
|
||||
private String path;
|
||||
private String anchor;
|
||||
|
||||
private HashMap<String,String> parameters = new HashMap<String,String>();
|
||||
|
||||
|
||||
public HttpURL(){}
|
||||
|
||||
public HttpURL( URL url ){
|
||||
this.setProtocol( url.getProtocol() );
|
||||
this.setHost( url.getHost() );
|
||||
this.setPort( url.getPort() );
|
||||
this.setPath( url.getPath() );
|
||||
}
|
||||
|
||||
|
||||
public String getProtocol( ){
|
||||
return protocol;
|
||||
}
|
||||
public String getHost( ){
|
||||
return host;
|
||||
}
|
||||
public int getPort( ){
|
||||
return port;
|
||||
}
|
||||
public String getPath( ){
|
||||
return path;
|
||||
}
|
||||
public String getAnchor( ){
|
||||
return anchor;
|
||||
}
|
||||
|
||||
public void setProtocol( String prot ){
|
||||
this.protocol = prot;
|
||||
}
|
||||
public void setHost( String host ){
|
||||
this.host = host;
|
||||
}
|
||||
public void setPort( int port ){
|
||||
this.port = port;
|
||||
}
|
||||
public void setPath( String path ){
|
||||
if( path.length() >= 1 && !path.startsWith(PATH_SEPARATOR))
|
||||
path = PATH_SEPARATOR + path;
|
||||
this.path = path;
|
||||
}
|
||||
public void setAnchor( String anch ){
|
||||
this.anchor = anch;
|
||||
}
|
||||
public void setParameter( String key, String value ){
|
||||
this.parameters.put(key, value);
|
||||
}
|
||||
|
||||
protected void setParameters( HashMap<String,String> pars ){
|
||||
this.parameters = pars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the parameter string in a URL.
|
||||
*
|
||||
* e.g.
|
||||
* "key=value&key2=value&..."
|
||||
*/
|
||||
public String getParameterString(){
|
||||
StringBuilder param = new StringBuilder();
|
||||
for(String key : parameters.keySet()){
|
||||
param.append(key);
|
||||
param.append('=');
|
||||
param.append( parameters.get(key) );
|
||||
param.append('&');
|
||||
}
|
||||
if( param.length() > 0 )
|
||||
param.deleteCharAt( param.length()-1 );
|
||||
return param.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a path that are used in the HTTP header
|
||||
*/
|
||||
public String getHttpURL(){
|
||||
StringBuilder url = new StringBuilder();
|
||||
url.append( path );
|
||||
if( !parameters.isEmpty() )
|
||||
url.append( PARAMETER_SEPARATOR ).append( getParameterString() );
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a full URL
|
||||
*/
|
||||
public String getURL(){
|
||||
return toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the whole URL
|
||||
*/
|
||||
public String toString(){
|
||||
StringBuilder url = new StringBuilder();
|
||||
url.append( protocol );
|
||||
url.append( PROTOCOL_SEPARATOR );
|
||||
url.append( host );
|
||||
if( port > 0 )
|
||||
url.append( PORT_SEPARATOR ).append( port );
|
||||
|
||||
if( path != null )
|
||||
url.append( path );
|
||||
else
|
||||
url.append( PATH_SEPARATOR );
|
||||
|
||||
if( !parameters.isEmpty() )
|
||||
url.append( PARAMETER_SEPARATOR ).append( getParameterString() );
|
||||
if( anchor != null )
|
||||
url.append( ANCHOR_SEPARATOR ).append( anchor );
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ import java.util.List;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import zutil.ProgressListener;
|
||||
import zutil.net.http.HTTPHeaderParser;
|
||||
import zutil.net.http.HttpHeaderParser;
|
||||
|
||||
/**
|
||||
* Parses a multipart/form-data http request,
|
||||
|
|
@ -35,11 +35,11 @@ public class MultipartParser {
|
|||
private BufferedReader in;
|
||||
|
||||
/** This is the listener that will listen on the progress */
|
||||
private ProgressListener<MultipartField> listener;
|
||||
private ProgressListener<MultipartParser,MultipartField> listener;
|
||||
|
||||
|
||||
|
||||
public MultipartParser(BufferedReader in, HTTPHeaderParser header){
|
||||
public MultipartParser(BufferedReader in, HttpHeaderParser header){
|
||||
this.in = in;
|
||||
|
||||
String cotype = header.getHeader("Content-type");
|
||||
|
|
@ -69,7 +69,7 @@ public class MultipartParser {
|
|||
/**
|
||||
* @param listener is the listener that will be called for progress
|
||||
*/
|
||||
public void setListener(ProgressListener<MultipartField> listener){
|
||||
public void setListener(ProgressListener<MultipartParser,MultipartField> listener){
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue