Skip to content

Commit

Permalink
initial commit \
Browse files Browse the repository at this point in the history
  • Loading branch information
alexDickhans committed Sep 17, 2020
0 parents commit 3f782c2
Show file tree
Hide file tree
Showing 31 changed files with 916 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/azureSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions http-server.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
56 changes: 56 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>dev.hdprojects</groupId>
<artifactId>http-server</artifactId>
<version>1.0-SNAPSHOT</version>


<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<!-- Dependencies -->
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- LOGGING -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

<!-- JUNIT -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
13 changes: 13 additions & 0 deletions request.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.30 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
40 changes: 40 additions & 0 deletions src/main/java/dev/hdprojects/HttpServer/HttpServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.hdprojects.HttpServer;

import dev.hdprojects.HttpServer.config.Configuration;
import dev.hdprojects.HttpServer.config.ConfigurationManager;
import dev.hdprojects.HttpServer.core.ServerListenerThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
* Driver class for the HTTP Server
*/
public class HttpServer {

private final static Logger LOGGER = LoggerFactory.getLogger(HttpServer.class);

public static void main(String[] args){

LOGGER.info("Server Starting ... ");

ConfigurationManager.getInstance().loadConfigurationFile("src/main/resources/http.json");

Configuration conf = ConfigurationManager.getInstance().getCurrentConfiguration();

LOGGER.info("Using port: " + conf.getPort());
LOGGER.info("Using webroot: " + conf.getWebroot());

ServerListenerThread serverListenerThread = null;
try {
serverListenerThread = new ServerListenerThread(conf.getPort(), conf.getWebroot());
serverListenerThread.start();
} catch (IOException e) {
e.printStackTrace();
// TODO Handle later
}


}
}
41 changes: 41 additions & 0 deletions src/main/java/dev/hdprojects/HttpServer/config/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.hdprojects.HttpServer.config;

public class Configuration {

private int port;
private String webroot;
private String[] css;
private String[] js;

public String[] getCss() {
return css;
}

public void setCss(String[] css) {
this.css = css;
}

public String[] getJs() {
return js;
}

public void setJs(String[] js) {
this.js = js;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getWebroot() {
return webroot;
}

public void setWebroot(String webroot) {
this.webroot = webroot;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dev.hdprojects.HttpServer.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import dev.hdprojects.HttpServer.util.json;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ConfigurationManager {

private static ConfigurationManager myConfigurationManager;
private static Configuration myCurrentConfiguration;

private ConfigurationManager() {
}

public static ConfigurationManager getInstance() {
if (myConfigurationManager==null)
myConfigurationManager = new ConfigurationManager();
return myConfigurationManager;
}

/**
* Used to load a configuration file by the path provided
*/
public void loadConfigurationFile(String filePath) {
FileReader fileReader = null;
try {
fileReader = new FileReader(filePath);
} catch (FileNotFoundException e) {
throw new HttpConfigurationException(e);
}
StringBuffer sb = new StringBuffer();
int i ;
try {
while ( ( i = fileReader.read()) != -1) {
sb.append((char)i);
}
} catch (IOException e) {
throw new HttpConfigurationException(e);
}
JsonNode conf = null;
try {
conf = json.parse(sb.toString());
} catch (IOException e) {
throw new HttpConfigurationException("Error parsing the Configuration File", e);
}
try {
myCurrentConfiguration = json.fromJson(conf, Configuration.class);
} catch (JsonProcessingException e) {
throw new HttpConfigurationException("Error parsing the Configuration file, internal",e);
}
}

/**
* Returns the Current loaded Configuration
*/
public Configuration getCurrentConfiguration() {
if ( myCurrentConfiguration == null) {
throw new HttpConfigurationException("No Current Configuration Set.");
}
return myCurrentConfiguration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.hdprojects.HttpServer.config;

public class HttpConfigurationException extends RuntimeException {
public HttpConfigurationException() {
}

public HttpConfigurationException(String message) {
super(message);
}

public HttpConfigurationException(String message, Throwable cause) {
super(message, cause);
}

public HttpConfigurationException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dev.hdprojects.HttpServer.core;

import dev.hdprojects.http.GenerateHttpHeader;
import dev.hdprojects.http.HttpParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class HttpConnectionWorkerThread extends Thread{

private final static Logger LOGGER = LoggerFactory.getLogger(HttpConnectionWorkerThread.class);

private Socket socket;

public HttpConnectionWorkerThread(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();

HttpParser httpParser = new HttpParser(inputStream);

httpParser.parseHttpRequest();

String html = "<html><head> <title>Simple Java HTTP Server</title></head><body><h1>Build this server with java HTTP</h1></body></html>";

final String CRLF = "\r\n"; // 13, 10 ASCII

GenerateHttpHeader httpHeader = new GenerateHttpHeader(200, html.length(), "text/html", "hd");

String response = httpHeader + html + CRLF + CRLF;

outputStream.write(response.getBytes());

LOGGER.info("Connection processing finished");
} catch (IOException e) {
LOGGER.error("Problem with communication", e);
} finally {
if(inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {}
}

if(outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {}
}

if(socket != null) {
try {
socket.close();
} catch (IOException e) {}
}
}
}
}
Loading

0 comments on commit 3f782c2

Please sign in to comment.