126 lines
No EOL
3.6 KiB
Java
126 lines
No EOL
3.6 KiB
Java
package dev.peerat.framework;
|
|
|
|
import java.io.BufferedWriter;
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStreamWriter;
|
|
import java.net.Socket;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
public class HttpWriter{
|
|
|
|
private OutputStream out;
|
|
private BufferedWriter writer;
|
|
|
|
public HttpWriter(Socket socket) throws Exception{
|
|
this.out = socket.getOutputStream();
|
|
this.writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
|
|
}
|
|
|
|
public HttpWriter(HttpWriter origin) throws Exception{
|
|
this.out = origin.out;
|
|
this.writer = origin.writer;
|
|
}
|
|
|
|
public HttpWriter(OutputStream out, BufferedWriter writer){
|
|
this.out = out;
|
|
this.writer = writer;
|
|
}
|
|
|
|
public void write(byte[] buffer) throws IOException{
|
|
this.out.write(buffer);
|
|
this.out.flush();
|
|
}
|
|
|
|
public void write(String message) throws IOException{
|
|
this.writer.write(message);
|
|
}
|
|
|
|
public void flush() throws IOException{
|
|
this.writer.flush();
|
|
}
|
|
|
|
public void close() throws IOException{
|
|
this.writer.close();
|
|
}
|
|
|
|
void response(int code, String... headers) throws Exception{
|
|
write("HTTP/1.1 "+code+codeMessage(code)+"\n");
|
|
for(String header : headers) write(header+"\n");
|
|
write("\n");
|
|
flush();
|
|
}
|
|
|
|
private static String[] HEIGHTBITS = new String[9];
|
|
private static String[] NINEBITS = new String[212];
|
|
|
|
static {
|
|
HEIGHTBITS[0] = " OK";
|
|
HEIGHTBITS[1] = " Created";
|
|
HEIGHTBITS[2] = " Accepted";
|
|
HEIGHTBITS[3] = " Non-Authoritative Information";
|
|
HEIGHTBITS[4] = " No Content";
|
|
HEIGHTBITS[5] = " Reset Content";
|
|
HEIGHTBITS[6] = " Partial Content";
|
|
HEIGHTBITS[7] = " Mutli-Status";
|
|
HEIGHTBITS[8] = " Already Reported";
|
|
|
|
NINEBITS[0] = " Multiple Choices";
|
|
NINEBITS[1] = " Moved Permanently";
|
|
NINEBITS[2] = " Found";
|
|
NINEBITS[3] = " See Other";
|
|
NINEBITS[4] = " Not Modified";
|
|
NINEBITS[5] = " Use Proxy";
|
|
NINEBITS[7] = " Temporary Redirect";
|
|
NINEBITS[8] = " Permanent Redirect";
|
|
|
|
NINEBITS[100] = " Bad Request";
|
|
NINEBITS[101] = " Unauthorized";
|
|
NINEBITS[102] = " Payment Required";
|
|
NINEBITS[103] = " Forbidden";
|
|
NINEBITS[104] = " Not Found";
|
|
NINEBITS[105] = " Method Not Allowed";
|
|
NINEBITS[106] = " Not Acceptable";
|
|
NINEBITS[107] = " Proxy Authentication Required";
|
|
NINEBITS[108] = " Request Time-Out";
|
|
NINEBITS[109] = " Conflict";
|
|
NINEBITS[110] = " Gone";
|
|
NINEBITS[111] = " Length Required";
|
|
NINEBITS[112] = " Precondition Failed";
|
|
NINEBITS[113] = " Payload Too Large";
|
|
NINEBITS[114] = " URI Too Long";
|
|
NINEBITS[115] = " Unsupported Media Type";
|
|
NINEBITS[116] = " Range Not Satisfiable";
|
|
NINEBITS[117] = " Expectation Failed";
|
|
NINEBITS[118] = " I'm a Teapot";
|
|
NINEBITS[121] = " Misdirected Request";
|
|
NINEBITS[122] = " Unprocessable Content";
|
|
NINEBITS[123] = " Locked";
|
|
NINEBITS[124] = " Failed Dependency";
|
|
NINEBITS[125] = " Too Early";
|
|
NINEBITS[126] = " Upgrade Required";
|
|
NINEBITS[128] = " Precondition Required";
|
|
NINEBITS[129] = " Too Many Requests";
|
|
NINEBITS[131] = " Request Header Fields Too Large";
|
|
NINEBITS[151] = " Unavailable For Legal Reasons";
|
|
|
|
NINEBITS[200] = " Internal Server Error";
|
|
NINEBITS[201] = " Not Implemented";
|
|
NINEBITS[202] = " Bad Gateway";
|
|
NINEBITS[203] = " Service Unavailable";
|
|
NINEBITS[204] = " Gateway Timeout";
|
|
NINEBITS[205] = " HTTP Version Not Supported";
|
|
NINEBITS[206] = " Variant Also Negotiates";
|
|
NINEBITS[207] = " Insufficient Storage";
|
|
NINEBITS[208] = " Loop Detected";
|
|
NINEBITS[210] = " Not Extended";
|
|
NINEBITS[211] = " Network Authentication Required";
|
|
}
|
|
|
|
private static String codeMessage(int code){
|
|
if(code == 100) return " Continue";
|
|
if(code >> 8 == 0) return HEIGHTBITS[code-200];
|
|
return NINEBITS[code-300];
|
|
}
|
|
|
|
} |