le-annen.javaserver.0.1.source-code.ReadRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javaserver Show documentation
Show all versions of javaserver Show documentation
A minimal java http server which fulfills small portions of the
http specifications. This is not a full fledged or secure server. It should only be used
for learning purposes and contains no guarantees of any king.
The newest version!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;
class ReadRequest implements ReadInterface {
@Override
public RequestParameters getRequest(Socket socket, String directoryPath) throws IOException {
ArrayList httpMessage = new ArrayList<>();
InputStreamReader inputStreamReader =
new InputStreamReader(socket.getInputStream());
BufferedReader bufferedReader =
new BufferedReader(inputStreamReader);
httpMessage = this.readHttpMessage(bufferedReader, httpMessage);
Boolean containsContent = this.containsContent(httpMessage);
httpMessage = containsContent ?
this.readContentBody(bufferedReader, httpMessage)
: httpMessage;
return new RequestParameters.RequestBuilder(directoryPath)
.setHttpVerb(httpMessage)
.setRequestPath(httpMessage)
.setHost(httpMessage)
.setUserAgent(httpMessage)
.setAccept(httpMessage)
.setBodyContent(httpMessage)
.build();
}
private ArrayList readContentBody(
BufferedReader bufferedReader,
ArrayList httpMessage) throws IOException {
int contentLength = this.getContentLength(httpMessage);
StringBuilder content = new StringBuilder();
int length = 0;
while(length < contentLength) {
int value = bufferedReader.read();
if(value != -1) { content.append((char) value); }
length += 1;
}
httpMessage.add("Body-Content: " + content);
return httpMessage;
}
private Boolean containsContent(ArrayList httpMessage) {
for(String line: httpMessage) {
String headerType = line.split(" ")[0];
if(headerType.equals("Content-Length:")) {
return true;
}
}
return false;
}
private int getContentLength(ArrayList httpMessage) {
int contentLength = 0;
for(String line: httpMessage) {
String headerLabel = line.split(" ")[0];
String headerValue = line.split(" ")[1];
if (headerLabel.equals("Content-Length:")) {
contentLength = Integer.parseInt(headerValue.trim());
}
}
return contentLength;
}
private ArrayList readHttpMessage(
BufferedReader bufferedReader,
ArrayList httpMessage) throws IOException {
Boolean reading = true;
String line;
while(reading) {
line = bufferedReader.readLine();
if(line.equals("")) { reading = false; }
else {
httpMessage.add(line); }
}
return httpMessage;
}
}