Some robustness and cleanup of HTTP code
This commit is contained in:
parent
8cdd25b262
commit
23055d7dec
3 changed files with 76 additions and 47 deletions
|
|
@ -34,29 +34,35 @@ import java.net.URL;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class connects to a HTTP server and
|
* This class connects to a HTTP server and parses the result.
|
||||||
* parses the result
|
|
||||||
*
|
|
||||||
* @author Ziver
|
|
||||||
*/
|
*/
|
||||||
public class HttpClient implements AutoCloseable {
|
public class HttpClient implements AutoCloseable {
|
||||||
public enum HttpRequestType {
|
public enum HttpRequestType {
|
||||||
GET, POST
|
GET,
|
||||||
|
POST
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request variables
|
// Request variables
|
||||||
private HttpURL url;
|
private HttpURL url;
|
||||||
private HttpRequestType type;
|
private String type;
|
||||||
private HashMap<String, String> headers;
|
private HashMap<String, String> headers;
|
||||||
private HashMap<String, String> cookies;
|
private HashMap<String, String> cookies;
|
||||||
private String data;
|
private String data;
|
||||||
|
|
||||||
// Response variables
|
// Response variables
|
||||||
private HttpHeaderParser rspHeader;
|
private HttpHeaderParser responseHeader;
|
||||||
private InputStream rspStream;
|
private InputStream responseStream;
|
||||||
|
|
||||||
|
|
||||||
|
public HttpClient() {
|
||||||
|
this(HttpRequestType.GET);
|
||||||
|
}
|
||||||
|
|
||||||
public HttpClient(HttpRequestType type) {
|
public HttpClient(HttpRequestType type) {
|
||||||
|
this(type.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpClient(String type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
headers = new HashMap<>();
|
headers = new HashMap<>();
|
||||||
cookies = new HashMap<>();
|
cookies = new HashMap<>();
|
||||||
|
|
@ -94,7 +100,7 @@ public class HttpClient implements AutoCloseable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the content data that will be included in the request.
|
* Sets the content data that will be included in the request.
|
||||||
* NOTE: this will override the POST data parameter inclusion.
|
* NOTE: this will disable the POST data parameter inclusion.
|
||||||
*/
|
*/
|
||||||
public void setData(String data) {
|
public void setData(String data) {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
|
|
@ -109,12 +115,12 @@ public class HttpClient implements AutoCloseable {
|
||||||
|
|
||||||
// Request
|
// Request
|
||||||
HttpPrintStream request = new HttpPrintStream(conn.getOutputStream(), HttpMessageType.REQUEST);
|
HttpPrintStream request = new HttpPrintStream(conn.getOutputStream(), HttpMessageType.REQUEST);
|
||||||
request.setRequestType(type.toString());
|
request.setRequestType(type);
|
||||||
request.setRequestURL(url.getHttpURL());
|
request.setRequestURL(url.getHttpURL());
|
||||||
request.setHeaders(headers);
|
request.setHeaders(headers);
|
||||||
request.setCookies(cookies);
|
request.setCookies(cookies);
|
||||||
|
|
||||||
if (type == HttpRequestType.POST) {
|
if (HttpRequestType.POST.toString().equals(type)) {
|
||||||
String postData;
|
String postData;
|
||||||
if (data != null)
|
if (data != null)
|
||||||
postData = data;
|
postData = data;
|
||||||
|
|
@ -128,27 +134,27 @@ public class HttpClient implements AutoCloseable {
|
||||||
request.close();
|
request.close();
|
||||||
|
|
||||||
// Response
|
// Response
|
||||||
if (rspHeader != null || rspStream != null) // Close previous request
|
if (responseHeader != null || responseStream != null) // Close previous request
|
||||||
this.close();
|
this.close();
|
||||||
rspStream = new BufferedInputStream(conn.getInputStream());
|
responseStream = new BufferedInputStream(conn.getInputStream());
|
||||||
rspHeader = new HttpHeaderParser(rspStream);
|
responseHeader = new HttpHeaderParser(responseStream);
|
||||||
|
|
||||||
return rspHeader;
|
return responseHeader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpHeaderParser getResponseHeader() {
|
public HttpHeaderParser getResponseHeader() {
|
||||||
return rspHeader;
|
return responseHeader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getResponseInputStream() {
|
public InputStream getResponseInputStream() {
|
||||||
return rspStream;
|
return responseStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
if (rspStream != null)
|
if (responseStream != null)
|
||||||
rspStream.close();
|
responseStream.close();
|
||||||
rspStream = null;
|
responseStream = null;
|
||||||
rspHeader = null;
|
responseHeader = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -275,30 +275,51 @@ public class HttpHeader {
|
||||||
|
|
||||||
public static String getResponseStatusString(int type) {
|
public static String getResponseStatusString(int type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 100:
|
case 100: return "Continue";
|
||||||
return "Continue";
|
case 200: return "OK";
|
||||||
case 200:
|
case 201: return "Created";
|
||||||
return "OK";
|
case 250: return "Low on Storage Space";
|
||||||
case 301:
|
case 300: return "Multiple Choices";
|
||||||
return "Moved Permanently";
|
case 301: return "Moved Permanently";
|
||||||
case 304:
|
case 302: return "Moved Temporarily";
|
||||||
return "Not Modified";
|
case 303: return "See Other";
|
||||||
case 307:
|
case 304: return "Not Modified";
|
||||||
return "Temporary Redirect";
|
case 305: return "Use Proxy";
|
||||||
case 400:
|
case 307: return "Temporary Redirect";
|
||||||
return "Bad Request";
|
case 400: return "Bad Request";
|
||||||
case 401:
|
case 401: return "Unauthorized";
|
||||||
return "Unauthorized";
|
case 403: return "Forbidden";
|
||||||
case 403:
|
case 404: return "Not Found";
|
||||||
return "Forbidden";
|
case 405: return "Method Not Allowed";
|
||||||
case 404:
|
case 406: return "Not Acceptable";
|
||||||
return "Not Found";
|
case 407: return "Proxy Authentication Required";
|
||||||
case 500:
|
case 408: return "Request Time-out";
|
||||||
return "Internal Server Error";
|
case 410: return "Gone";
|
||||||
case 501:
|
case 411: return "Length Required";
|
||||||
return "Not Implemented";
|
case 412: return "Precondition Failed";
|
||||||
default:
|
case 413: return "Request Entity Too Large";
|
||||||
return "";
|
case 414: return "Request-URI Too Large";
|
||||||
|
case 415: return "Unsupported Media Type";
|
||||||
|
case 451: return "Parameter Not Understood";
|
||||||
|
case 452: return "Conference Not Found";
|
||||||
|
case 453: return "Not Enough Bandwidth";
|
||||||
|
case 454: return "Session Not Found";
|
||||||
|
case 455: return "Method Not Valid in This State";
|
||||||
|
case 456: return "Header Field Not Valid for Resource";
|
||||||
|
case 457: return "Invalid Range";
|
||||||
|
case 458: return "Parameter Is Read-Only";
|
||||||
|
case 459: return "Aggregate operation not allowed";
|
||||||
|
case 460: return "Only aggregate operation allowed";
|
||||||
|
case 461: return "Unsupported transport";
|
||||||
|
case 462: return "Destination unreachable";
|
||||||
|
case 500: return "Internal Server Error";
|
||||||
|
case 501: return "Not Implemented";
|
||||||
|
case 502: return "Bad Gateway";
|
||||||
|
case 503: return "Service Unavailable";
|
||||||
|
case 504: return "Gateway Time-out";
|
||||||
|
case 505: return "RTSP Version not supported";
|
||||||
|
case 551: return "Option not supported";
|
||||||
|
default: return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -146,8 +146,10 @@ public class HttpFilePage implements HttpPage{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void deliverFile(File file, HttpPrintStream out) throws IOException {
|
private void deliverFile(File file, HttpPrintStream out) throws IOException {
|
||||||
out.setHeader("Content-Type",
|
String fileExt = FileUtil.getFileExtension(file);
|
||||||
MimeTypeUtil.getMimeByExtension(FileUtil.getFileExtension(file)).toString());
|
|
||||||
|
if (MimeTypeUtil.getMimeByExtension(fileExt) != null)
|
||||||
|
out.setHeader("Content-Type", MimeTypeUtil.getMimeByExtension(fileExt).toString());
|
||||||
out.setHeader("Content-Length", "" + file.length());
|
out.setHeader("Content-Length", "" + file.length());
|
||||||
out.flush();
|
out.flush();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue