Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Getting the code into better shape / maven ready
- Loading branch information
1 parent
4373da8
commit 55941c4
Showing
21 changed files
with
283 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Jonathan Giles | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<!-- Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the MIT License. --> | ||
<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/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>net.jonathangiles.tools</groupId> | ||
<artifactId>teenyhttpd</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
|
||
<name>TeenyHttpd</name> | ||
<description>TeenyHttpd is an extremely basic HTTP server.</description> | ||
<url>https://github.com/JonathanGiles/TeenyHttpd</url> | ||
|
||
<distributionManagement> | ||
<snapshotRepository> | ||
<id>ossrh</id> | ||
<url>https://oss.sonatype.org/content/repositories/snapshots</url> | ||
</snapshotRepository> | ||
<repository> | ||
<id>ossrh</id> | ||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> | ||
</repository> | ||
</distributionManagement> | ||
|
||
<scm> | ||
<connection>scm:git:git://github.com/JonathanGiles/TeenyHttpd.git</connection> | ||
<developerConnection>scm:git:git@github.com:JonathanGiles/TeenyHttpd.git</developerConnection> | ||
<url>https://github.com/JonathanGiles/TeenyHttpd</url> | ||
<tag>HEAD</tag> | ||
</scm> | ||
|
||
<issueManagement> | ||
<system>GitHub</system> | ||
<url>https://github.com/JonathanGiles/TeenyHttpd/issues</url> | ||
</issueManagement> | ||
|
||
<licenses> | ||
<license> | ||
<name>MIT License</name> | ||
<url>http://www.opensource.org/licenses/mit-license.php</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
<developers> | ||
<developer> | ||
<id>jonathangiles</id> | ||
<name>Jonathan Giles</name> | ||
<url>http://jonathangiles.net</url> | ||
</developer> | ||
</developers> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
</properties> | ||
|
||
<profiles> | ||
<profile> | ||
<id>release</id> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-gpg-plugin</artifactId> | ||
<version>1.6</version> | ||
<executions> | ||
<execution> | ||
<id>sign-artifacts</id> | ||
<phase>verify</phase> | ||
<goals> | ||
<goal>sign</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.sonatype.plugins</groupId> | ||
<artifactId>nexus-staging-maven-plugin</artifactId> | ||
<version>1.6.8</version> | ||
<extensions>true</extensions> | ||
<configuration> | ||
<serverId>ossrh</serverId> | ||
<nexusUrl>https://oss.sonatype.org/</nexusUrl> | ||
<autoReleaseAfterClose>true</autoReleaseAfterClose> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# TeenyHttpd | ||
|
||
TeenyHttpd is an extremely basic HTTP server. It is implemented in plain Java 8 with no external dependencies, making it lightweight and applicable in situations where a basic, small HTTP server is all that is required. | ||
|
||
## Examples | ||
|
||
### Starting TeenyHttpd | ||
|
||
You can start a new instance of TeenyHttpd up simply using the following code: | ||
|
||
```java | ||
final int PORT = 80; | ||
TeenyHttpd server = new TeenyHttpd(PORT); | ||
server.start(); | ||
``` | ||
|
||
By default, TeenyHttpd will serve files from within its own library, so you will see placeholder files. To configure the webroot from where files should be served, do the following: | ||
|
||
```java | ||
final int PORT = 80; | ||
TeenyHttpd server = new TeenyHttpd(PORT); | ||
server.setWebroot(new File("/Users/jonathan/Code/jonathangiles.net")); | ||
server.start(); | ||
``` | ||
|
||
With this configured, all file requests will served from this base directory. | ||
|
||
### Stopping TeenyHttpd | ||
|
||
You stop a running instance as follows: | ||
|
||
```java | ||
final int PORT = 80; | ||
TeenyHttpd server = new TeenyHttpd(PORT); | ||
server.start(); | ||
|
||
// some time later... | ||
server.stop(); | ||
``` | ||
|
||
### Overriding Response Processing | ||
|
||
In some cases, we don't want to just serve up files from a web root directory - we want to do something more custom when a request is received in the server. To do this, we override the `serve` method, as demonstrated below: | ||
|
||
```java | ||
final int PORT = 80; | ||
TeenyHttpd server = new TeenyHttpd(PORT) { | ||
@Override | ||
public Response serve(final Request request) { | ||
String path = request.getPath(); | ||
|
||
// do work... | ||
|
||
// return response to user | ||
return new StringResponse(request, StatusCode.OK, "Hello!"); | ||
} | ||
}; | ||
server.start(); | ||
``` | ||
|
||
In the above code sample, we return a `StringResponse`. TeenyHttpd also has `ByteResponse` and `FileResponse` types. `ByteResponse` simply returns a `byte[]` to the caller, and `FileResponse` parses the path information from the request to return a file (which is what the default behavior of TeenyHttpd uses to do file hosting). | ||
|
||
## Project Management | ||
|
||
Releases are performed using `mvn clean deploy -Prelease`. |
2 changes: 1 addition & 1 deletion
2
src/net/jonathangiles/teenyhttpd/Header.java → ...onathangiles/tools/teenyhttpd/Header.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...athangiles/teenyhttpd/request/Method.java → ...iles/tools/teenyhttpd/request/Method.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...giles/teenyhttpd/request/QueryParams.java → ...tools/teenyhttpd/request/QueryParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...thangiles/teenyhttpd/request/Request.java → ...les/tools/teenyhttpd/request/Request.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...les/teenyhttpd/response/ByteResponse.java → ...ols/teenyhttpd/response/ByteResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...angiles/teenyhttpd/response/Response.java → ...s/tools/teenyhttpd/response/Response.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...giles/teenyhttpd/response/StatusCode.java → ...tools/teenyhttpd/response/StatusCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...s/teenyhttpd/response/StringResponse.java → ...s/teenyhttpd/response/StringResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
404 - File or Directory Not Found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello from TeenyHttpd |
Oops, something went wrong.