Bug fixes
This commit is contained in:
parent
a28d2b219e
commit
553f8da549
13 changed files with 547 additions and 252 deletions
|
|
@ -11,7 +11,9 @@ import java.util.HashMap;
|
|||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public class HttpPrintStream extends PrintStream{
|
||||
public class HttpPrintStream extends PrintStream{
|
||||
private Integer status_code;
|
||||
private HashMap<String, String> header;
|
||||
private HashMap<String, String> cookie;
|
||||
private StringBuffer buffer;
|
||||
private boolean buffer_enabled;
|
||||
|
|
@ -19,6 +21,8 @@ public class HttpPrintStream extends PrintStream{
|
|||
public HttpPrintStream(OutputStream out) {
|
||||
super(out);
|
||||
|
||||
status_code = 0;
|
||||
header = new HashMap<String, String>();
|
||||
cookie = new HashMap<String, String>();
|
||||
buffer = new StringBuffer();
|
||||
buffer_enabled = false;
|
||||
|
|
@ -27,12 +31,15 @@ public class HttpPrintStream extends PrintStream{
|
|||
/**
|
||||
* Enable the buffering capability of the PrintStream.
|
||||
* Nothing will be sent to the client when buffering
|
||||
* is enabled until you close or flush the stream.
|
||||
* is enabled until you close or flush the stream.
|
||||
* This function will flush the stream if buffering is
|
||||
* disabled.
|
||||
*
|
||||
* @param b
|
||||
*/
|
||||
public void enableBuffering(boolean b){
|
||||
buffer_enabled = b;
|
||||
if(!buffer_enabled) flush();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -49,16 +56,28 @@ public class HttpPrintStream extends PrintStream{
|
|||
}
|
||||
|
||||
/**
|
||||
* Sends the given header directly to the client.
|
||||
* No buffering involved.
|
||||
* Adds an header value
|
||||
*
|
||||
* @param header is the header to send
|
||||
* @param key is the header name
|
||||
* @param value is the value of the header
|
||||
* @throws Exception Throws exception if the header has already been sent
|
||||
*/
|
||||
public void sendHeader(String header) throws Exception{
|
||||
if(cookie == null)
|
||||
public void setHeader(String key, String value) throws Exception{
|
||||
if(header == null)
|
||||
throw new Exception("Header already sent!!!");
|
||||
super.print(header+"\n");
|
||||
header.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the return status code
|
||||
*
|
||||
* @param code the code from 100 up to 599
|
||||
* @throws Exception Throws exception if the header has already been sent
|
||||
*/
|
||||
public void setStatusCode(int code) throws Exception{
|
||||
if(status_code == null)
|
||||
throw new Exception("Header already sent!!!");
|
||||
status_code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -74,7 +93,7 @@ public class HttpPrintStream extends PrintStream{
|
|||
public void print(String s){
|
||||
printOrBuffer(s);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* prints to all
|
||||
*/
|
||||
|
|
@ -83,11 +102,24 @@ public class HttpPrintStream extends PrintStream{
|
|||
buffer.append(s);
|
||||
}
|
||||
else{
|
||||
if(status_code != null){
|
||||
super.print("HTTP/1.0 "+status_code+" "+getStatusString(status_code));
|
||||
super.println();
|
||||
status_code = null;
|
||||
}
|
||||
if(header != null){
|
||||
for(String key : header.keySet()){
|
||||
super.print(key+": "+header.get(key));
|
||||
super.println();
|
||||
}
|
||||
header = null;
|
||||
}
|
||||
if(cookie != null){
|
||||
for(String key : cookie.keySet()){
|
||||
super.print("Set-Cookie: "+key+"="+cookie.get(key)+"; \n");
|
||||
super.print("Set-Cookie: "+key+"="+cookie.get(key)+";");
|
||||
super.println();
|
||||
}
|
||||
super.print(" \n");
|
||||
super.println();
|
||||
cookie = null;
|
||||
}
|
||||
super.print(s);
|
||||
|
|
@ -103,15 +135,18 @@ public class HttpPrintStream extends PrintStream{
|
|||
printOrBuffer(buffer.toString());
|
||||
buffer.delete(0, buffer.length());
|
||||
buffer_enabled = true;
|
||||
}
|
||||
}
|
||||
else if(status_code != null || header != null || cookie != null){
|
||||
printOrBuffer("");
|
||||
}
|
||||
super.flush();
|
||||
}
|
||||
|
||||
|
||||
public void close(){
|
||||
flush();
|
||||
super.close();
|
||||
}
|
||||
|
||||
|
||||
public void println(){ println("");}
|
||||
public void println(boolean x){ println(String.valueOf(x));}
|
||||
public void println(char x){ println(String.valueOf(x));}
|
||||
|
|
@ -121,7 +156,7 @@ public class HttpPrintStream extends PrintStream{
|
|||
public void println(int x){ println(String.valueOf(x));}
|
||||
public void println(long x){ println(String.valueOf(x));}
|
||||
public void println(Object x){ println(String.valueOf(x));}
|
||||
|
||||
|
||||
public void print(boolean x){ printOrBuffer(String.valueOf(x));}
|
||||
public void print(char x){ printOrBuffer(String.valueOf(x));}
|
||||
public void print(char[] x){ printOrBuffer(new String(x));}
|
||||
|
|
@ -130,4 +165,25 @@ public class HttpPrintStream extends PrintStream{
|
|||
public void print(int x){ printOrBuffer(String.valueOf(x));}
|
||||
public void print(long x){ printOrBuffer(String.valueOf(x));}
|
||||
public void print(Object x){ printOrBuffer(String.valueOf(x));}
|
||||
|
||||
/*
|
||||
public void write(int b) { print((char)b);}
|
||||
public void write(byte buf[], int off, int len){
|
||||
print(new String(buf, off, len));}
|
||||
*/
|
||||
private String getStatusString(int type){
|
||||
switch(type){
|
||||
case 100: return "Continue";
|
||||
case 200: return "OK";
|
||||
case 301: return "Moved Permanently";
|
||||
case 307: return "Temporary Redirect";
|
||||
case 400: return "Bad Request";
|
||||
case 401: return "Unauthorized";
|
||||
case 403: return "Forbidden";
|
||||
case 404: return "Not Found";
|
||||
case 500: return "Internal Server Error";
|
||||
case 501: return "Not Implemented";
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue