Removes unused Request class

This commit is contained in:
Fuzzlemann 2017-08-12 15:19:44 +02:00
parent 7906459fc8
commit 5996f7cba4

View File

@ -1,151 +0,0 @@
package main.java.com.djrapitops.plan.ui.webserver;
import main.java.com.djrapitops.plan.Log;
import java.io.*;
import java.util.Arrays;
import java.util.Optional;
/**
* Represents a HTTP Request.
* <p>
* Request is read from the given InputStream.
* <p>
* Closing the Request closes the InputStream. (Closing Socket InputStream
* closes the socket.)
* <p>
* Request Strings should not be logged because they may contain base64 encoded
* user:password Authorization combinations.
*
* @author Rsl1122
* @since 3.5.2
*/
public class Request implements Closeable {
private final InputStream input;
private String request;
private String target;
private String authorization;
private Closeable close;
/**
* Creates a new Request object.
*
* @param input InputStream to read the socket.
*/
public Request(InputStream input) {
this.input = input;
}
/**
* Reads the information in the Request and parses required information.
* <p>
* Parses Request (GET, POST etc.)
* <p>
* Parses Target (/home/etc)
* <p>
* Parses Authorization (Authorization header).
*
* @throws java.io.IOException if InputStream can not be read.
*/
public void parse() throws IOException {
StringBuilder headerB = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
close = in;
while (true) {
String line = in.readLine();
if (line == null || line.isEmpty()) {
break;
}
headerB.append(line);
headerB.append(":::");
}
final String requestHeader = headerB.toString();
String[] header = requestHeader.split(":::");
parseRequestAndTarget(header);
parseAuthorization(header);
}
/**
* Check if the request has Authorization: Basic header.
*
* @return true/false
*/
public boolean hasAuthorization() {
return authorization != null;
}
/**
* Returns the base64 encoded Authorization if present.
*
* @return base64 encoded user:password or null.
*/
public String getAuthorization() {
return authorization;
}
private void parseAuthorization(String[] header) {
Optional<String> auth = Arrays.stream(header)
.filter(l -> l.contains("Authorization: Basic "))
.findFirst();
if (auth.isPresent()) {
Log.debug("Found Authorization.");
authorization = auth.get().replace("Authorization: Basic ", "");
} else {
Log.debug("Not Authorized.");
}
}
private void parseRequestAndTarget(String[] header) {
String req = header[0];
String[] reqLine = req.split(" ");
if (reqLine.length >= 2) {
request = reqLine[0];
target = reqLine[1].replace("%20", " ")
.replace("%2E", ".");
} else {
request = "GET";
target = "/";
}
}
/**
* Used to get the request type.
*
* @return GET, POST, etc.
*/
public String getRequest() {
return request;
}
/**
* Used to get the target.
*
* @return for example '/home/etc'
*/
public String getTarget() {
return target;
}
/**
* Closes the Request.
* <p>
* Closes the InputStream.
*
* @throws IOException if the stream can not be closed.
*/
@Override
public void close() throws IOException {
close.close();
}
@Override
public String toString() {
return "Request{" +
"request='" + request + '\'' +
", target='" + target + '\'' +
", authorization='" + authorization + '\'' +
'}';
}
}