├── core ├── .gitignore ├── src │ ├── test │ │ ├── resources │ │ │ ├── keystore.jks │ │ │ ├── META-INF │ │ │ │ └── nanohttpd │ │ │ │ │ └── mimetypes.properties │ │ │ ├── multipart-form-test.htm │ │ │ └── file-upload-test.htm │ │ └── java │ │ │ └── org │ │ │ └── nanohttpd │ │ │ └── junit │ │ │ └── protocols │ │ │ └── http │ │ │ ├── HttpParsingTest.java │ │ │ ├── integration │ │ │ ├── IntegrationTestBase.java │ │ │ ├── ShutdownTest.java │ │ │ └── PutStreamIntegrationTest.java │ │ │ ├── MimeTest.java │ │ │ ├── BadRequestTest.java │ │ │ ├── HttpSessionTest.java │ │ │ ├── HttpSessionHeadersTest.java │ │ │ ├── HttpPutRequestTest.java │ │ │ ├── LoadKeyStoreTest.java │ │ │ ├── JavaIOTempDirExistTest.java │ │ │ ├── InvalidRequestTest.java │ │ │ ├── HttpChunkedResponseTest.java │ │ │ ├── ServerSocketFactoryTest.java │ │ │ ├── CookieTest.java │ │ │ └── HttpSSLServerTest.java │ ├── main │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── nanohttpd │ │ │ │ ├── mimetypes.properties │ │ │ │ └── default-mimetypes.properties │ │ └── java │ │ │ └── org │ │ │ └── nanohttpd │ │ │ ├── protocols │ │ │ └── http │ │ │ │ ├── response │ │ │ │ ├── IStatus.java │ │ │ │ ├── ChunkedOutputStream.java │ │ │ │ └── Status.java │ │ │ │ ├── threading │ │ │ │ ├── IAsyncRunner.java │ │ │ │ └── DefaultAsyncRunner.java │ │ │ │ ├── tempfiles │ │ │ │ ├── DefaultTempFileManagerFactory.java │ │ │ │ ├── ITempFileManager.java │ │ │ │ ├── ITempFile.java │ │ │ │ ├── DefaultTempFile.java │ │ │ │ └── DefaultTempFileManager.java │ │ │ │ ├── sockets │ │ │ │ ├── DefaultServerSocketFactory.java │ │ │ │ └── SecureServerSocketFactory.java │ │ │ │ ├── request │ │ │ │ └── Method.java │ │ │ │ ├── content │ │ │ │ ├── Cookie.java │ │ │ │ └── ContentType.java │ │ │ │ ├── IHTTPSession.java │ │ │ │ ├── ServerRunnable.java │ │ │ │ └── ClientHandler.java │ │ │ └── util │ │ │ ├── IFactory.java │ │ │ ├── IHandler.java │ │ │ ├── IFactoryThrowing.java │ │ │ └── ServerRunner.java │ └── site │ │ └── site.xml ├── build.gradle └── pom.xml ├── nanolets ├── .gitignore ├── src │ └── test │ │ └── resources │ │ ├── exception.html │ │ ├── blabla.html │ │ └── dir │ │ ├── blabla.html │ │ ├── index.htm │ │ └── nanohttpd_logo.png ├── build.gradle └── pom.xml ├── samples ├── .gitignore ├── build.gradle ├── pom.xml └── src │ ├── site │ └── site.xml │ └── main │ └── java │ └── org │ └── nanohttpd │ └── samples │ ├── http │ ├── HelloServer.java │ └── DebugServer.java │ └── tempfiles │ └── TempFilesServer.java ├── webserver ├── .gitignore ├── src │ ├── test │ │ ├── resources │ │ │ ├── testdir │ │ │ │ ├── testdir │ │ │ │ │ ├── different.xml │ │ │ │ │ └── index.html │ │ │ │ ├── testpdf.pdf │ │ │ │ └── test.html │ │ │ └── META-INF │ │ │ │ └── services │ │ │ │ └── org.nanohttpd.webserver.WebServerPluginInfo │ │ └── java │ │ │ └── org │ │ │ └── nanohttpd │ │ │ └── junit │ │ │ └── webserver │ │ │ ├── DummyPluginInfo.java │ │ │ ├── AbstractTestHttpServer.java │ │ │ └── DummyPlugin.java │ ├── site │ │ └── site.xml │ └── main │ │ └── java │ │ └── org │ │ └── nanohttpd │ │ └── webserver │ │ ├── WebServerPluginInfo.java │ │ ├── WebServerPlugin.java │ │ └── InternalRewrite.java ├── build.gradle └── pom.xml ├── websocket ├── .gitignore ├── build.gradle ├── src │ ├── site │ │ └── site.xml │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── nanohttpd │ │ │ ├── protocols │ │ │ └── websockets │ │ │ │ ├── State.java │ │ │ │ ├── OpCode.java │ │ │ │ ├── CloseCode.java │ │ │ │ ├── WebSocketException.java │ │ │ │ └── CloseFrame.java │ │ │ └── samples │ │ │ └── websockets │ │ │ └── EchoSocketSample.java │ └── test │ │ ├── resources │ │ └── echo-test.html │ │ └── java │ │ └── org │ │ └── nanohttpd │ │ └── junit │ │ └── protocols │ │ └── websockets │ │ └── SimpleEchoSocket.java └── pom.xml ├── fileupload ├── .gitignore ├── build.gradle └── pom.xml ├── markdown-plugin ├── .gitignore ├── src │ ├── main │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── services │ │ │ │ └── org.nanohttpd.webserver.WebServerPluginInfo │ │ └── java │ │ │ └── org │ │ │ └── nanohttpd │ │ │ └── markdown │ │ │ └── MarkdownWebServerPluginInfo.java │ └── site │ │ └── site.xml ├── build.gradle └── pom.xml ├── relocation ├── nanohttpd │ ├── src │ │ └── main │ │ │ └── resources │ │ │ └── relocated.txt │ └── pom.xml ├── nanohttpd-samples │ ├── src │ │ └── main │ │ │ └── resources │ │ │ └── relocated.txt │ └── pom.xml ├── nanohttpd-webserver │ ├── src │ │ └── main │ │ │ └── resources │ │ │ └── relocated.txt │ └── pom.xml ├── nanohttpd-websocket │ ├── src │ │ └── main │ │ │ └── resources │ │ │ └── relocated.txt │ └── pom.xml ├── nanohttpd-webserver-markdown-plugin │ ├── src │ │ └── main │ │ │ └── resources │ │ │ └── relocated.txt │ └── pom.xml └── pom.xml ├── src ├── site │ ├── resources │ │ └── images │ │ │ ├── nanohttpd_logo.png │ │ │ └── nanohttpd_logo_text.png │ └── site.xml └── main │ └── checkstyle │ └── checkstyle-suppressions.xml ├── .gitignore ├── settings.xml ├── settings.gradle ├── .travis.yml ├── nanohttpd release perform.launch ├── nanohttpd release prepare.launch └── LICENSE.md /core/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /LICENSE.txt 3 | -------------------------------------------------------------------------------- /nanolets/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /LICENSE.txt 3 | -------------------------------------------------------------------------------- /samples/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /LICENSE.txt 3 | -------------------------------------------------------------------------------- /webserver/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /LICENSE.txt 3 | -------------------------------------------------------------------------------- /websocket/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /LICENSE.txt 3 | -------------------------------------------------------------------------------- /fileupload/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /LICENSE.txt 3 | -------------------------------------------------------------------------------- /markdown-plugin/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /LICENSE.txt 3 | -------------------------------------------------------------------------------- /nanolets/src/test/resources/exception.html: -------------------------------------------------------------------------------- 1 | this will throw an io exception -------------------------------------------------------------------------------- /nanolets/src/test/resources/blabla.html: -------------------------------------------------------------------------------- 1 |

just a page

-------------------------------------------------------------------------------- /nanolets/src/test/resources/dir/blabla.html: -------------------------------------------------------------------------------- 1 |

just an other page

-------------------------------------------------------------------------------- /nanolets/src/test/resources/dir/index.htm: -------------------------------------------------------------------------------- 1 |

just an index page

-------------------------------------------------------------------------------- /webserver/src/test/resources/testdir/testdir/different.xml: -------------------------------------------------------------------------------- 1 | 2 | This sould not show up ;-) 3 | -------------------------------------------------------------------------------- /core/src/test/resources/keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NanoHttpd/nanohttpd/HEAD/core/src/test/resources/keystore.jks -------------------------------------------------------------------------------- /webserver/src/test/resources/testdir/testdir/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Simple index file 4 | 5 | -------------------------------------------------------------------------------- /core/src/main/resources/META-INF/nanohttpd/mimetypes.properties: -------------------------------------------------------------------------------- 1 | #mime types for nanohttpd, use a file like this for user defined mimetypes -------------------------------------------------------------------------------- /relocation/nanohttpd/src/main/resources/relocated.txt: -------------------------------------------------------------------------------- 1 | this project was relocated to the groupid org.nanohttpd starting with version 2.2.0 -------------------------------------------------------------------------------- /webserver/src/test/resources/META-INF/services/org.nanohttpd.webserver.WebServerPluginInfo: -------------------------------------------------------------------------------- 1 | org.nanohttpd.junit.webserver.DummyPluginInfo -------------------------------------------------------------------------------- /src/site/resources/images/nanohttpd_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NanoHttpd/nanohttpd/HEAD/src/site/resources/images/nanohttpd_logo.png -------------------------------------------------------------------------------- /core/src/test/resources/META-INF/nanohttpd/mimetypes.properties: -------------------------------------------------------------------------------- 1 | #test mime types for nanohttpd 2 | blabla=text/blabla 3 | ts=video/wrongOverwrite -------------------------------------------------------------------------------- /markdown-plugin/src/main/resources/META-INF/services/org.nanohttpd.webserver.WebServerPluginInfo: -------------------------------------------------------------------------------- 1 | org.nanohttpd.markdown.MarkdownWebServerPluginInfo -------------------------------------------------------------------------------- /relocation/nanohttpd-samples/src/main/resources/relocated.txt: -------------------------------------------------------------------------------- 1 | this project was relocated to the groupid org.nanohttpd starting with version 2.2.0 -------------------------------------------------------------------------------- /relocation/nanohttpd-webserver/src/main/resources/relocated.txt: -------------------------------------------------------------------------------- 1 | this project was relocated to the groupid org.nanohttpd starting with version 2.2.0 -------------------------------------------------------------------------------- /relocation/nanohttpd-websocket/src/main/resources/relocated.txt: -------------------------------------------------------------------------------- 1 | this project was relocated to the groupid org.nanohttpd starting with version 2.2.0 -------------------------------------------------------------------------------- /src/site/resources/images/nanohttpd_logo_text.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NanoHttpd/nanohttpd/HEAD/src/site/resources/images/nanohttpd_logo_text.png -------------------------------------------------------------------------------- /webserver/src/test/resources/testdir/testpdf.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NanoHttpd/nanohttpd/HEAD/webserver/src/test/resources/testdir/testpdf.pdf -------------------------------------------------------------------------------- /nanolets/src/test/resources/dir/nanohttpd_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NanoHttpd/nanohttpd/HEAD/nanolets/src/test/resources/dir/nanohttpd_logo.png -------------------------------------------------------------------------------- /webserver/src/test/resources/testdir/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | dummy 4 | 5 | 6 |

it works

7 | 8 | -------------------------------------------------------------------------------- /relocation/nanohttpd-webserver-markdown-plugin/src/main/resources/relocated.txt: -------------------------------------------------------------------------------- 1 | this project was relocated to the groupid org.nanohttpd starting with version 2.2.0 -------------------------------------------------------------------------------- /nanolets/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'NanoHttpd-nano application server' 2 | 3 | dependencies { 4 | compile project(':nanohttpd') 5 | 6 | testCompile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.4.1' 7 | } 8 | -------------------------------------------------------------------------------- /samples/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'NanoHttpd-Samples' 2 | 3 | dependencies { 4 | compile project(':nanohttpd') 5 | compile project(':nanohttpd-webserver') 6 | } 7 | 8 | task wrapper(type: Wrapper) { 9 | gradleVersion = "4.4.1" 10 | } 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | out 2 | build 3 | target 4 | 5 | *.iml 6 | *.ipr 7 | *.iws 8 | *.class 9 | 10 | .idea 11 | .classpath 12 | .project 13 | .gradle 14 | gradle/ 15 | gradlew 16 | gradlew.bat 17 | 18 | # Vim Backup/Swap Files 19 | *~ 20 | .swp 21 | .*.swp 22 | 23 | /.settings/ 24 | /LICENSE.txt 25 | -------------------------------------------------------------------------------- /webserver/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'NanoHttpd-Webserver' 2 | 3 | dependencies { 4 | compile project(':nanohttpd') 5 | 6 | testCompile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.4.1' 7 | } 8 | 9 | task wrapper(type: Wrapper) { 10 | gradleVersion = "4.4.1" 11 | } 12 | -------------------------------------------------------------------------------- /core/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'NanoHttpd-Core' 2 | 3 | dependencies { 4 | testCompile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.2.5' 5 | testCompile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.2.5' 6 | } 7 | 8 | task wrapper(type: Wrapper) { 9 | gradleVersion = "4.4.1" 10 | } 11 | -------------------------------------------------------------------------------- /markdown-plugin/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'NanoHttpd-Webserver-Markdown-Plugin' 2 | 3 | dependencies { 4 | compile group: 'org.pegdown', name: 'pegdown', version: '1.4.1' 5 | compileOnly project(':nanohttpd') 6 | compileOnly project(':nanohttpd-webserver') 7 | } 8 | 9 | task wrapper(type: Wrapper) { 10 | gradleVersion = "4.4.1" 11 | } 12 | -------------------------------------------------------------------------------- /settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | sonatype-nexus-staging 7 | ${env.CI_DEPLOY_USERNAME} 8 | ${env.CI_DEPLOY_PASSWORD} 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /websocket/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'NanoHttpd-Websocket' 2 | 3 | dependencies { 4 | compile project(':nanohttpd') 5 | testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5' 6 | testCompile group: 'org.eclipse.jetty.websocket', name: 'websocket-client', version: '9.3.0.M2' 7 | } 8 | 9 | task wrapper(type: Wrapper) { 10 | gradleVersion = "4.4.1" 11 | } 12 | -------------------------------------------------------------------------------- /relocation/nanohttpd/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.nanohttpd 6 | nanohttpd-project 7 | 2.2.0 8 | 9 | nanohttpd 10 | 11 | 12 | org.nanohttpd 13 | 14 | 15 | -------------------------------------------------------------------------------- /relocation/nanohttpd-samples/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.nanohttpd 6 | nanohttpd-project 7 | 2.2.0 8 | 9 | nanohttpd-samples 10 | 11 | 12 | org.nanohttpd 13 | 14 | 15 | -------------------------------------------------------------------------------- /relocation/nanohttpd-webserver/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.nanohttpd 6 | nanohttpd-project 7 | 2.2.0 8 | 9 | nanohttpd-webserver 10 | 11 | 12 | org.nanohttpd 13 | 14 | 15 | -------------------------------------------------------------------------------- /relocation/nanohttpd-websocket/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.nanohttpd 6 | nanohttpd-project 7 | 2.2.0 8 | 9 | nanohttpd-websocket 10 | 11 | 12 | org.nanohttpd 13 | 14 | 15 | -------------------------------------------------------------------------------- /relocation/nanohttpd-webserver-markdown-plugin/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.nanohttpd 6 | nanohttpd-project 7 | 2.2.0 8 | 9 | nanohttpd-webserver-markdown-plugin 10 | 11 | 12 | org.nanohttpd 13 | 14 | 15 | -------------------------------------------------------------------------------- /fileupload/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'NanoHttpd-apache file upload integration' 2 | 3 | dependencies { 4 | compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.1' 5 | 6 | compileOnly project(':nanohttpd') 7 | compileOnly group: 'javax.servlet', name: 'servlet-api', version: '2.5' 8 | 9 | testCompile project(':nanohttpd') 10 | testCompile group: 'javax.servlet', name: 'servlet-api', version: '2.5' 11 | testCompile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.4.1' 12 | testCompile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.4.1' 13 | } 14 | 15 | task wrapper(type: Wrapper) { 16 | gradleVersion = "4.4.1" 17 | } 18 | -------------------------------------------------------------------------------- /core/src/main/resources/META-INF/nanohttpd/default-mimetypes.properties: -------------------------------------------------------------------------------- 1 | #default mime types for nanohttpd, use META-INF/mimetypes.properties for user defined mimetypes 2 | css=text/css 3 | htm=text/html 4 | html=text/html 5 | xml=text/xml 6 | java=text/x-java-source, text/java 7 | md=text/plain 8 | txt=text/plain 9 | asc=text/plain 10 | gif=image/gif 11 | jpg=image/jpeg 12 | jpeg=image/jpeg 13 | png=image/png 14 | svg=image/svg+xml 15 | mp3=audio/mpeg 16 | m3u=audio/mpeg-url 17 | mp4=video/mp4 18 | ogv=video/ogg 19 | flv=video/x-flv 20 | mov=video/quicktime 21 | swf=application/x-shockwave-flash 22 | js=application/javascript 23 | pdf=application/pdf 24 | doc=application/msword 25 | ogg=application/x-ogg 26 | zip=application/octet-stream 27 | exe=application/octet-stream 28 | class=application/octet-stream 29 | m3u8=application/vnd.apple.mpegurl 30 | ts=video/mp2t -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'nanohttpd-project' 2 | include ':nanohttpd' 3 | include ':nanohttpd-samples' 4 | include ':nanohttpd-webserver' 5 | include ':nanohttpd-websocket' 6 | include ':nanohttpd-webserver-markdown-plugin' 7 | include ':nanohttpd-nanolets' 8 | include ':nanohttpd-apache-fileupload' 9 | 10 | project(':nanohttpd').projectDir = "$rootDir/core" as File 11 | project(':nanohttpd-samples').projectDir = "$rootDir/samples" as File 12 | project(':nanohttpd-webserver').projectDir = "$rootDir/webserver" as File 13 | project(':nanohttpd-websocket').projectDir = "$rootDir/websocket" as File 14 | project(':nanohttpd-webserver-markdown-plugin').projectDir = "$rootDir/markdown-plugin" as File 15 | project(':nanohttpd-nanolets').projectDir = "$rootDir/nanolets" as File 16 | project(':nanohttpd-apache-fileupload').projectDir = "$rootDir/fileupload" as File -------------------------------------------------------------------------------- /core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | org.nanohttpd 5 | nanohttpd-project 6 | 2.3.2-SNAPSHOT 7 | 8 | nanohttpd 9 | jar 10 | NanoHttpd-Core 11 | 12 | 13 | org.apache.httpcomponents 14 | httpclient 15 | 4.2.5 16 | test 17 | 18 | 19 | org.apache.httpcomponents 20 | httpmime 21 | 4.2.5 22 | test 23 | 24 | 25 | 26 | 0.82 27 | 28 | 29 | -------------------------------------------------------------------------------- /samples/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | org.nanohttpd 5 | nanohttpd-project 6 | 2.3.2-SNAPSHOT 7 | 8 | nanohttpd-samples 9 | samples for nanohttpd 10 | jar 11 | NanoHttpd-Samples 12 | 13 | 14 | ${project.groupId} 15 | nanohttpd 16 | ${project.version} 17 | 18 | 19 | ${project.groupId} 20 | nanohttpd-webserver 21 | ${project.version} 22 | 23 | 24 | 25 | 0.0 26 | 27 | 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk7 4 | 5 | before_install: 6 | - cat ~/.m2/settings.xml 7 | - rm ~/.m2/settings.xml 8 | 9 | install: 10 | - mvn install -DskipTests=true 11 | 12 | script: 13 | - mvn test; export MAVEN_RESULT=$? 14 | - if [ "$MAVEN_RESULT" -ne 0 ]; then exit 1; fi 15 | - if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then mvn clean deploy --quiet --settings settings.xml; fi 16 | 17 | after_success: 18 | - mvn clean test jacoco:report coveralls:report 19 | 20 | 21 | notifications: 22 | email: 23 | - richard.vannieuwenhoven@adesso.at 24 | - elonen@iki.fi 25 | - diogo.duarte@techie.com 26 | 27 | env: 28 | global: 29 | - secure: "OtD0z3y4/OjSzg8irVD8v/u0TElcw8AiCCXb7a0UQEnTpGxcf5DOdkvHv0hF4xjHKFtlMMHxevW+a4C4NuFR8it8ZJ/i2m24reB28JicDcRQY9nwV/BR/T08CRG9KDz5EuTHgfPJDF0y+5MiVNwJVhHFviBWKuXyIuYouJ5IHgc=" 30 | - secure: "tgTVycNLwYDM3U0EVK1TkffylQHfZihvSDC9QoZEo+wz9aBBLoAtUJP7DWltRQFOkfTeGltGHEfGM2/qkIG6Wz+hNFG/fveHqyI01JWXc64d7yBm7agaCP5uTtt2wjaZ7ZK5Mps5QoufblYu+j9gb2v31t9IdsJ9PUs0+wgE/WU=" 31 | 32 | -------------------------------------------------------------------------------- /nanolets/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | org.nanohttpd 5 | nanohttpd-project 6 | 2.3.2-SNAPSHOT 7 | 8 | nanohttpd-nanolets 9 | jar 10 | NanoHttpd-nano application server 11 | nanohttpd-nanolets add a very easy to use version of servlets into nanohttpd. 12 | 13 | 14 | ${project.groupId} 15 | nanohttpd 16 | ${project.version} 17 | 18 | 19 | org.apache.httpcomponents 20 | httpclient 21 | 4.4.1 22 | test 23 | 24 | 25 | 26 | 0.96 27 | 28 | 29 | -------------------------------------------------------------------------------- /nanohttpd release perform.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /nanohttpd release prepare.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.apache.maven.skins 5 | maven-fluido-skin 6 | 1.3.0 7 | 8 | 9 | images/nanohttpd_logo.png 10 | 11 | 12 | images/nanohttpd_logo_text.png 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | false 22 | true 23 | 24 | Nanohttpd/nanohttpd 25 | right 26 | black 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /core/src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.apache.maven.skins 5 | maven-fluido-skin 6 | 1.3.0 7 | 8 | 9 | ../images/nanohttpd_logo.png 10 | 11 | 12 | ../images/nanohttpd_logo_text.png 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | false 23 | true 24 | 25 | Nanohttpd/nanohttpd 26 | right 27 | black 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /samples/src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.apache.maven.skins 5 | maven-fluido-skin 6 | 1.3.0 7 | 8 | 9 | ../images/nanohttpd_logo.png 10 | 11 | 12 | ../images/nanohttpd_logo_text.png 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | false 23 | true 24 | 25 | Nanohttpd/nanohttpd 26 | right 27 | black 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /webserver/src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.apache.maven.skins 5 | maven-fluido-skin 6 | 1.3.0 7 | 8 | 9 | ../images/nanohttpd_logo.png 10 | 11 | 12 | ../images/nanohttpd_logo_text.png 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | false 23 | true 24 | 25 | Nanohttpd/nanohttpd 26 | right 27 | black 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /websocket/src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.apache.maven.skins 5 | maven-fluido-skin 6 | 1.3.0 7 | 8 | 9 | ../images/nanohttpd_logo.png 10 | 11 | 12 | ../images/nanohttpd_logo_text.png 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | false 23 | true 24 | 25 | Nanohttpd/nanohttpd 26 | right 27 | black 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /markdown-plugin/src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.apache.maven.skins 5 | maven-fluido-skin 6 | 1.3.0 7 | 8 | 9 | ../images/nanohttpd_logo.png 10 | 11 | 12 | ../images/nanohttpd_logo_text.png 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | false 23 | true 24 | 25 | Nanohttpd/nanohttpd 26 | right 27 | black 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/main/checkstyle/checkstyle-suppressions.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2013 by Paul S. Hawke, 2001,2005-2013 by Jarno Elonen, 2010 by Konstantinos Togias 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | * Neither the name of the NanoHttpd organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | -------------------------------------------------------------------------------- /relocation/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | org.sonatype.oss 6 | oss-parent 7 | 7 8 | 9 | com.nanohttpd 10 | nanohttpd-project 11 | 2.2.0 12 | pom 13 | 14 | 15 | sonatype-nexus-staging 16 | nanohttpd sonytype Maven 2 repository 17 | https://oss.sonatype.org/service/local/staging/deploy/maven2 18 | 19 | 20 | org.nanohttpd 21 | 22 | 23 | 24 | 25 | 26 | org.apache.maven.plugins 27 | maven-gpg-plugin 28 | 29 | 30 | sign-artifacts 31 | verify 32 | 33 | sign 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | nanohttpd 43 | nanohttpd-webserver 44 | nanohttpd-webserver-markdown-plugin 45 | nanohttpd-samples 46 | nanohttpd-websocket 47 | 48 | -------------------------------------------------------------------------------- /fileupload/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | nanohttpd-project 5 | org.nanohttpd 6 | 2.3.2-SNAPSHOT 7 | 8 | 4.0.0 9 | nanohttpd-apache-fileupload 10 | NanoHttpd-apache file upload integration 11 | nanohttpd-apache-fileupload integrates the apache file upload framework into nanohttpd 12 | 13 | 14 | org.nanohttpd 15 | nanohttpd 16 | 2.3.2-SNAPSHOT 17 | provided 18 | 19 | 20 | commons-fileupload 21 | commons-fileupload 22 | 1.3.1 23 | 24 | 25 | javax.servlet 26 | servlet-api 27 | 2.5 28 | provided 29 | 30 | 31 | org.apache.httpcomponents 32 | httpclient 33 | 4.4.1 34 | test 35 | 36 | 37 | org.apache.httpcomponents 38 | httpmime 39 | 4.4.1 40 | test 41 | 42 | 43 | 44 | 0.99 45 | 46 | -------------------------------------------------------------------------------- /webserver/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | org.nanohttpd 5 | nanohttpd-project 6 | 2.3.2-SNAPSHOT 7 | 8 | nanohttpd-webserver 9 | jar 10 | NanoHttpd-Webserver 11 | nanohttpd-webserver can serve any local directory as a webserver using nanohttpd. 12 | 13 | 14 | ${project.groupId} 15 | nanohttpd 16 | ${project.version} 17 | 18 | 19 | org.apache.httpcomponents 20 | httpclient 21 | 4.4.1 22 | test 23 | 24 | 25 | 26 | 27 | 28 | org.apache.maven.plugins 29 | maven-assembly-plugin 30 | 2.2-beta-5 31 | 32 | 33 | jar-with-dependencies 34 | 35 | 36 | 37 | fi.iki.elonen.SimpleWebServer 38 | 39 | 40 | 41 | 42 | 43 | package 44 | 45 | single 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 0.75 54 | 55 | 56 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/response/IStatus.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.response; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | public interface IStatus { 37 | 38 | String getDescription(); 39 | 40 | int getRequestStatus(); 41 | } 42 | -------------------------------------------------------------------------------- /websocket/src/main/java/org/nanohttpd/protocols/websockets/State.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.websockets; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Websocket 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | public enum State { 37 | UNCONNECTED, 38 | CONNECTING, 39 | OPEN, 40 | CLOSING, 41 | CLOSED 42 | } 43 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/util/IFactory.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.util; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | /** 37 | * Represents a simple factory 38 | * 39 | * @author LordFokas 40 | * @param 41 | * The Type of object to create 42 | */ 43 | public interface IFactory { 44 | 45 | T create(); 46 | } 47 | -------------------------------------------------------------------------------- /markdown-plugin/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | org.nanohttpd 5 | nanohttpd-project 6 | 2.3.2-SNAPSHOT 7 | 8 | nanohttpd-webserver-markdown-plugin 9 | jar 10 | NanoHttpd-Webserver-Markdown-Plugin 11 | https://github.com/NanoHttpd/nanohttpd 12 | 13 | 14 | ${project.groupId} 15 | nanohttpd 16 | ${project.version} 17 | provided 18 | 19 | 20 | ${project.groupId} 21 | nanohttpd-webserver 22 | ${project.version} 23 | provided 24 | 25 | 26 | org.pegdown 27 | pegdown 28 | 1.4.1 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.apache.maven.plugins 36 | maven-assembly-plugin 37 | 2.2-beta-5 38 | 39 | 40 | jar-with-dependencies 41 | 42 | 43 | 44 | fi.iki.elonen.SimpleWebServer 45 | 46 | 47 | 48 | 49 | 50 | package 51 | 52 | single 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 0.0 61 | 62 | 63 | -------------------------------------------------------------------------------- /webserver/src/main/java/org/nanohttpd/webserver/WebServerPluginInfo.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.webserver; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Webserver 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | /** 37 | * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/14/13 at 8:09 AM 38 | */ 39 | public interface WebServerPluginInfo { 40 | 41 | String[] getIndexFilesForMimeType(String mime); 42 | 43 | String[] getMimeTypes(); 44 | 45 | WebServerPlugin getWebServerPlugin(String mimeType); 46 | } 47 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/util/IHandler.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.util; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | /** 37 | * Defines a generic handler that returns an object of type O when given an 38 | * object of type I. 39 | * 40 | * @author LordFokas 41 | * @param 42 | * The input type. 43 | * @param 44 | * The output type. 45 | */ 46 | public interface IHandler { 47 | 48 | public O handle(I input); 49 | } 50 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/threading/IAsyncRunner.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.threading; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import org.nanohttpd.protocols.http.ClientHandler; 37 | 38 | /** 39 | * Pluggable strategy for asynchronously executing requests. 40 | */ 41 | public interface IAsyncRunner { 42 | 43 | void closeAll(); 44 | 45 | void closed(ClientHandler clientHandler); 46 | 47 | void exec(ClientHandler code); 48 | } 49 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/tempfiles/DefaultTempFileManagerFactory.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.tempfiles; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import org.nanohttpd.util.IFactory; 37 | 38 | /** 39 | * Default strategy for creating and cleaning up temporary files. 40 | */ 41 | public class DefaultTempFileManagerFactory implements IFactory { 42 | 43 | @Override 44 | public ITempFileManager create() { 45 | return new DefaultTempFileManager(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/util/IFactoryThrowing.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.util; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | /** 37 | * Represents a factory that can throw an exception instead of actually creating 38 | * an object 39 | * 40 | * @author LordFokas 41 | * @param 42 | * The Type of object to create 43 | * @param 44 | * The base Type of exceptions that can be thrown 45 | */ 46 | public interface IFactoryThrowing { 47 | 48 | T create() throws E; 49 | } 50 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/tempfiles/ITempFileManager.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.tempfiles; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | /** 37 | * Temp file manager. 38 | *

39 | *

40 | * Temp file managers are created 1-to-1 with incoming requests, to create and 41 | * cleanup temporary files created as a result of handling the request. 42 | *

43 | */ 44 | public interface ITempFileManager { 45 | 46 | void clear(); 47 | 48 | public ITempFile createTempFile(String filename_hint) throws Exception; 49 | } 50 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/tempfiles/ITempFile.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.tempfiles; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.OutputStream; 37 | 38 | /** 39 | * A temp file. 40 | *

41 | *

42 | * Temp files are responsible for managing the actual temporary storage and 43 | * cleaning themselves up when no longer needed. 44 | *

45 | */ 46 | public interface ITempFile { 47 | 48 | public void delete() throws Exception; 49 | 50 | public String getName(); 51 | 52 | public OutputStream open() throws Exception; 53 | } 54 | -------------------------------------------------------------------------------- /websocket/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | org.nanohttpd 5 | nanohttpd-project 6 | 2.3.2-SNAPSHOT 7 | 8 | nanohttpd-websocket 9 | jar 10 | NanoHttpd-Websocket 11 | nanohttpd-websocket is a very low profile websocket server based on nanohttpd. 12 | 13 | 14 | 15 | org.apache.maven.plugins 16 | maven-jar-plugin 17 | 18 | 19 | default-jar 20 | 21 | 22 | **/samples/** 23 | 24 | 25 | 26 | 27 | echo-jar 28 | package 29 | 30 | jar 31 | 32 | 33 | echo 34 | 35 | 36 | true 37 | fi.iki.elonen.samples.echo.EchoSocketSample 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | ${project.groupId} 49 | nanohttpd 50 | ${project.version} 51 | 52 | 53 | org.mockito 54 | mockito-all 55 | 1.9.5 56 | test 57 | 58 | 59 | org.eclipse.jetty.websocket 60 | websocket-client 61 | 9.3.0.M2 62 | test 63 | 64 | 65 | 66 | 0.67 67 | 68 | 69 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/sockets/DefaultServerSocketFactory.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.sockets; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.IOException; 37 | import java.net.ServerSocket; 38 | 39 | import org.nanohttpd.util.IFactoryThrowing; 40 | 41 | /** 42 | * Creates a normal ServerSocket for TCP connections 43 | */ 44 | public class DefaultServerSocketFactory implements IFactoryThrowing { 45 | 46 | @Override 47 | public ServerSocket create() throws IOException { 48 | return new ServerSocket(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /core/src/test/resources/multipart-form-test.htm: -------------------------------------------------------------------------------- 1 | 33 | 34 | 35 |

This is a multipart-form test for NanoHTTPD.

36 |
37 | 38 |
39 | 40 |
41 | 42 |
43 | 44 |
45 | 46 | 47 | -------------------------------------------------------------------------------- /webserver/src/main/java/org/nanohttpd/webserver/WebServerPlugin.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.webserver; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Webserver 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.File; 37 | import java.util.Map; 38 | 39 | import org.nanohttpd.protocols.http.IHTTPSession; 40 | import org.nanohttpd.protocols.http.response.Response; 41 | 42 | /** 43 | * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/14/13 at 8:09 AM 44 | */ 45 | public interface WebServerPlugin { 46 | 47 | boolean canServeUri(String uri, File rootDir); 48 | 49 | void initialize(Map commandLineOptions); 50 | 51 | Response serveFile(String uri, Map headers, IHTTPSession session, File file, String mimeType); 52 | } 53 | -------------------------------------------------------------------------------- /core/src/test/resources/file-upload-test.htm: -------------------------------------------------------------------------------- 1 | 33 | 34 | 35 |

This is a file upload test for NanoHTTPD.

36 |
37 | 38 |
39 | 40 |
41 | 42 |
43 | 44 |
45 | 46 |
47 | 48 | 49 | -------------------------------------------------------------------------------- /webserver/src/test/java/org/nanohttpd/junit/webserver/DummyPluginInfo.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.webserver; 2 | 3 | import org.nanohttpd.webserver.WebServerPlugin; 4 | import org.nanohttpd.webserver.WebServerPluginInfo; 5 | 6 | /* 7 | * #%L 8 | * NanoHttpd-Webserver 9 | * %% 10 | * Copyright (C) 2012 - 2015 nanohttpd 11 | * %% 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * 1. Redistributions of source code must retain the above copyright notice, this 16 | * list of conditions and the following disclaimer. 17 | * 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 22 | * 3. Neither the name of the nanohttpd nor the names of its contributors 23 | * may be used to endorse or promote products derived from this software without 24 | * specific prior written permission. 25 | * 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 28 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 30 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 31 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 34 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 35 | * OF THE POSSIBILITY OF SUCH DAMAGE. 36 | * #L% 37 | */ 38 | 39 | public class DummyPluginInfo implements WebServerPluginInfo { 40 | 41 | @Override 42 | public String[] getIndexFilesForMimeType(String mime) { 43 | 44 | return new String[]{ 45 | "index.xml" 46 | }; 47 | } 48 | 49 | @Override 50 | public String[] getMimeTypes() { 51 | return new String[]{ 52 | "text/xml" 53 | }; 54 | } 55 | 56 | @Override 57 | public WebServerPlugin getWebServerPlugin(String mimeType) { 58 | return new DummyPlugin(); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /websocket/src/main/java/org/nanohttpd/samples/websockets/EchoSocketSample.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.samples.websockets; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Websocket 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.IOException; 37 | 38 | import org.nanohttpd.protocols.websockets.NanoWSD; 39 | 40 | public class EchoSocketSample { 41 | 42 | public static void main(String[] args) throws IOException { 43 | final boolean debugMode = args.length >= 2 && "-d".equals(args[1].toLowerCase()); 44 | NanoWSD ws = new DebugWebSocketServer(args.length > 0 ? Integer.parseInt(args[0]) : 9090, debugMode); 45 | ws.start(); 46 | System.out.println("Server started, hit Enter to stop.\n"); 47 | try { 48 | System.in.read(); 49 | } catch (IOException ignored) { 50 | } 51 | ws.stop(); 52 | System.out.println("Server stopped.\n"); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /websocket/src/main/java/org/nanohttpd/protocols/websockets/OpCode.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.websockets; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Websocket 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | public enum OpCode { 37 | Continuation(0), 38 | Text(1), 39 | Binary(2), 40 | Close(8), 41 | Ping(9), 42 | Pong(10); 43 | 44 | public static OpCode find(byte value) { 45 | for (OpCode opcode : values()) { 46 | if (opcode.getValue() == value) { 47 | return opcode; 48 | } 49 | } 50 | return null; 51 | } 52 | 53 | private final byte code; 54 | 55 | private OpCode(int code) { 56 | this.code = (byte) code; 57 | } 58 | 59 | public byte getValue() { 60 | return this.code; 61 | } 62 | 63 | public boolean isControlFrame() { 64 | return this == Close || this == Ping || this == Pong; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/request/Method.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.request; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | /** 37 | * HTTP Request methods, with the ability to decode a String back 38 | * to its enum value. 39 | */ 40 | public enum Method { 41 | GET, 42 | PUT, 43 | POST, 44 | DELETE, 45 | HEAD, 46 | OPTIONS, 47 | TRACE, 48 | CONNECT, 49 | PATCH, 50 | PROPFIND, 51 | PROPPATCH, 52 | MKCOL, 53 | MOVE, 54 | COPY, 55 | LOCK, 56 | UNLOCK, 57 | NOTIFY, 58 | SUBSCRIBE; 59 | 60 | public static Method lookup(String method) { 61 | if (method == null) 62 | return null; 63 | 64 | try { 65 | return valueOf(method); 66 | } catch (IllegalArgumentException e) { 67 | // TODO: Log it? 68 | return null; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /markdown-plugin/src/main/java/org/nanohttpd/markdown/MarkdownWebServerPluginInfo.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.markdown; 2 | 3 | import org.nanohttpd.webserver.WebServerPlugin; 4 | import org.nanohttpd.webserver.WebServerPluginInfo; 5 | 6 | /* 7 | * #%L 8 | * NanoHttpd-Webserver-Markdown-Plugin 9 | * %% 10 | * Copyright (C) 2012 - 2015 nanohttpd 11 | * %% 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * 1. Redistributions of source code must retain the above copyright notice, this 16 | * list of conditions and the following disclaimer. 17 | * 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, 19 | * this list of conditions and the following disclaimer in the documentation 20 | * and/or other materials provided with the distribution. 21 | * 22 | * 3. Neither the name of the nanohttpd nor the names of its contributors 23 | * may be used to endorse or promote products derived from this software without 24 | * specific prior written permission. 25 | * 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 28 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 30 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 31 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 34 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 35 | * OF THE POSSIBILITY OF SUCH DAMAGE. 36 | * #L% 37 | */ 38 | 39 | /** 40 | * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/13/13 at 4:01 AM 41 | */ 42 | public class MarkdownWebServerPluginInfo implements WebServerPluginInfo { 43 | 44 | @Override 45 | public String[] getIndexFilesForMimeType(String mime) { 46 | return new String[]{ 47 | "index.md" 48 | }; 49 | } 50 | 51 | @Override 52 | public String[] getMimeTypes() { 53 | return new String[]{ 54 | "text/markdown" 55 | }; 56 | } 57 | 58 | @Override 59 | public WebServerPlugin getWebServerPlugin(String mimeType) { 60 | return new MarkdownWebServerPlugin(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /websocket/src/main/java/org/nanohttpd/protocols/websockets/CloseCode.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.websockets; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Websocket 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | public enum CloseCode { 37 | NormalClosure(1000), 38 | GoingAway(1001), 39 | ProtocolError(1002), 40 | UnsupportedData(1003), 41 | NoStatusRcvd(1005), 42 | AbnormalClosure(1006), 43 | InvalidFramePayloadData(1007), 44 | PolicyViolation(1008), 45 | MessageTooBig(1009), 46 | MandatoryExt(1010), 47 | InternalServerError(1011), 48 | TLSHandshake(1015); 49 | 50 | public static CloseCode find(int value) { 51 | for (CloseCode code : values()) { 52 | if (code.getValue() == value) { 53 | return code; 54 | } 55 | } 56 | return null; 57 | } 58 | 59 | private final int code; 60 | 61 | private CloseCode(int code) { 62 | this.code = code; 63 | } 64 | 65 | public int getValue() { 66 | return this.code; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /webserver/src/main/java/org/nanohttpd/webserver/InternalRewrite.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.webserver; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Webserver 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.ByteArrayInputStream; 37 | import java.util.Map; 38 | 39 | import org.nanohttpd.protocols.http.NanoHTTPD; 40 | import org.nanohttpd.protocols.http.response.Response; 41 | import org.nanohttpd.protocols.http.response.Status; 42 | 43 | /** 44 | * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/15/13 at 2:52 PM 45 | */ 46 | public class InternalRewrite extends Response { 47 | 48 | private final String uri; 49 | 50 | private final Map headers; 51 | 52 | public InternalRewrite(Map headers, String uri) { 53 | super(Status.OK, NanoHTTPD.MIME_HTML, new ByteArrayInputStream(new byte[0]), 0); 54 | this.headers = headers; 55 | this.uri = uri; 56 | } 57 | 58 | public Map getHeaders() { 59 | return this.headers; 60 | } 61 | 62 | public String getUri() { 63 | return this.uri; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /websocket/src/main/java/org/nanohttpd/protocols/websockets/WebSocketException.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.websockets; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Websocket 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.IOException; 37 | 38 | public class WebSocketException extends IOException { 39 | 40 | private static final long serialVersionUID = 1L; 41 | 42 | private final CloseCode code; 43 | 44 | private final String reason; 45 | 46 | public WebSocketException(CloseCode code, String reason) { 47 | this(code, reason, null); 48 | } 49 | 50 | public WebSocketException(CloseCode code, String reason, Exception cause) { 51 | super(code + ": " + reason, cause); 52 | this.code = code; 53 | this.reason = reason; 54 | } 55 | 56 | public WebSocketException(Exception cause) { 57 | this(CloseCode.InternalServerError, cause.toString(), cause); 58 | } 59 | 60 | public CloseCode getCode() { 61 | return this.code; 62 | } 63 | 64 | public String getReason() { 65 | return this.reason; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/HttpParsingTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import static junit.framework.Assert.assertEquals; 37 | 38 | import org.junit.Test; 39 | 40 | public class HttpParsingTest extends HttpServerTest { 41 | 42 | @Test 43 | public void testMultibyteCharacterSupport() throws Exception { 44 | String expected = "Chinese \u738b Letters"; 45 | String input = "Chinese+%e7%8e%8b+Letters"; 46 | assertEquals(expected, this.testServer.decodePercent(input)); 47 | } 48 | 49 | @Test 50 | public void testNormalCharacters() throws Exception { 51 | for (int i = 0x20; i < 0x80; i++) { 52 | String hex = Integer.toHexString(i); 53 | String input = "%" + hex; 54 | char expected = (char) i; 55 | assertEquals("" + expected, this.testServer.decodePercent(input)); 56 | } 57 | } 58 | 59 | @Test 60 | public void testPlusInQueryParams() throws Exception { 61 | assertEquals("foo bar", this.testServer.decodePercent("foo+bar")); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/integration/IntegrationTestBase.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http.integration; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.IOException; 37 | 38 | import org.apache.http.impl.client.DefaultHttpClient; 39 | import org.junit.After; 40 | import org.junit.Before; 41 | import org.nanohttpd.protocols.http.NanoHTTPD; 42 | 43 | /** 44 | * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/2/13 at 10:02 PM 45 | */ 46 | public abstract class IntegrationTestBase { 47 | 48 | protected DefaultHttpClient httpclient; 49 | 50 | protected T testServer; 51 | 52 | public abstract T createTestServer(); 53 | 54 | @Before 55 | public void setUp() { 56 | this.testServer = createTestServer(); 57 | this.httpclient = new DefaultHttpClient(); 58 | try { 59 | this.testServer.start(); 60 | } catch (IOException e) { 61 | e.printStackTrace(); 62 | } 63 | } 64 | 65 | @After 66 | public void tearDown() { 67 | this.httpclient.getConnectionManager().shutdown(); 68 | this.testServer.stop(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /webserver/src/test/java/org/nanohttpd/junit/webserver/AbstractTestHttpServer.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.webserver; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Webserver 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.ByteArrayOutputStream; 37 | import java.io.IOException; 38 | import java.io.InputStream; 39 | 40 | import org.apache.http.HttpEntity; 41 | 42 | /** 43 | * @author Matthieu Brouillard [matthieu@brouillard.fr] 44 | */ 45 | public class AbstractTestHttpServer { 46 | 47 | protected byte[] readContents(HttpEntity entity) throws IOException { 48 | InputStream instream = entity.getContent(); 49 | return readContents(instream); 50 | } 51 | 52 | protected byte[] readContents(InputStream instream) throws IOException { 53 | byte[] bytes; 54 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 55 | try { 56 | byte[] buffer = new byte[1024]; 57 | int count; 58 | while ((count = instream.read(buffer)) >= 0) { 59 | out.write(buffer, 0, count); 60 | } 61 | bytes = out.toByteArray(); 62 | } finally { 63 | instream.close(); 64 | } 65 | return bytes; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/MimeTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import org.junit.Assert; 37 | import org.junit.Test; 38 | import org.nanohttpd.protocols.http.NanoHTTPD; 39 | 40 | public class MimeTest { 41 | 42 | @Test 43 | public void testExistingMimeType() throws Exception { 44 | Assert.assertEquals("text/html", NanoHTTPD.getMimeTypeForFile("xxxx.html")); 45 | } 46 | 47 | @Test 48 | public void testNotExistingMimeType() throws Exception { 49 | Assert.assertNull(NanoHTTPD.mimeTypes().get("notExistent")); 50 | Assert.assertEquals("application/octet-stream", NanoHTTPD.getMimeTypeForFile("xxxx.notExistent")); 51 | } 52 | 53 | @Test 54 | public void testOverwritenMimeType() throws Exception { 55 | Assert.assertEquals("video/wrongOverwrite", NanoHTTPD.getMimeTypeForFile("xxxx.ts")); 56 | } 57 | 58 | @Test 59 | public void testManualMimeType() throws Exception { 60 | NanoHTTPD.mimeTypes().put("flv", "video/manualOverwrite"); 61 | Assert.assertEquals("video/manualOverwrite", NanoHTTPD.getMimeTypeForFile("xxxx.flv")); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/BadRequestTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.ByteArrayOutputStream; 37 | import java.io.IOException; 38 | 39 | import org.junit.Test; 40 | 41 | public class BadRequestTest extends HttpServerTest { 42 | 43 | @Test 44 | public void testEmptyRequest() throws IOException { 45 | ByteArrayOutputStream outputStream = invokeServer("\n\n"); 46 | String[] expected = new String[]{ 47 | "HTTP/1.1 400 Bad Request" 48 | }; 49 | assertResponse(outputStream, expected); 50 | } 51 | 52 | @Test 53 | public void testInvalidMethod() throws IOException { 54 | ByteArrayOutputStream outputStream = invokeServer("GETT http://example.com"); 55 | String[] expected = new String[]{ 56 | "HTTP/1.1 400 Bad Request" 57 | }; 58 | assertResponse(outputStream, expected); 59 | } 60 | 61 | @Test 62 | public void testMissingURI() throws IOException { 63 | ByteArrayOutputStream outputStream = invokeServer("GET"); 64 | String[] expected = new String[]{ 65 | "HTTP/1.1 400 Bad Request" 66 | }; 67 | assertResponse(outputStream, expected); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/HttpSessionTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import static org.junit.Assert.assertEquals; 37 | 38 | import java.io.ByteArrayInputStream; 39 | import java.io.ByteArrayOutputStream; 40 | import java.net.InetAddress; 41 | import java.net.UnknownHostException; 42 | 43 | import org.junit.Test; 44 | import org.nanohttpd.protocols.http.HTTPSession; 45 | 46 | public class HttpSessionTest extends HttpServerTest { 47 | 48 | private static final String DUMMY_REQUEST_CONTENT = "dummy request content"; 49 | 50 | private static final TestTempFileManager TEST_TEMP_FILE_MANAGER = new TestTempFileManager(); 51 | 52 | @Test 53 | public void testSessionRemoteIPAddress() throws UnknownHostException { 54 | ByteArrayInputStream inputStream = new ByteArrayInputStream(HttpSessionTest.DUMMY_REQUEST_CONTENT.getBytes()); 55 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 56 | InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); 57 | HTTPSession session = this.testServer.createSession(HttpSessionTest.TEST_TEMP_FILE_MANAGER, inputStream, outputStream, inetAddress); 58 | assertEquals("127.0.0.1", session.getRemoteIpAddress()); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /webserver/src/test/java/org/nanohttpd/junit/webserver/DummyPlugin.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.webserver; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Webserver 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.ByteArrayInputStream; 37 | import java.io.File; 38 | import java.io.InputStream; 39 | import java.util.Map; 40 | 41 | import org.nanohttpd.protocols.http.IHTTPSession; 42 | import org.nanohttpd.protocols.http.response.Response; 43 | import org.nanohttpd.protocols.http.response.Status; 44 | import org.nanohttpd.webserver.InternalRewrite; 45 | import org.nanohttpd.webserver.WebServerPlugin; 46 | 47 | public class DummyPlugin implements WebServerPlugin { 48 | 49 | @Override 50 | public boolean canServeUri(String uri, File rootDir) { 51 | return true; 52 | } 53 | 54 | @Override 55 | public void initialize(Map commandLineOptions) { 56 | } 57 | 58 | @Override 59 | public Response serveFile(String uri, Map headers, IHTTPSession session, File file, String mimeType) { 60 | if (uri.contains("rewrite")) { 61 | return new InternalRewrite(headers, "/testdir/test.html"); 62 | } 63 | byte[] bytes = "".getBytes(); 64 | InputStream data = new ByteArrayInputStream(bytes); 65 | return Response.newFixedLengthResponse(Status.OK, "text/xml", data, bytes.length); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/response/ChunkedOutputStream.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.response; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.FilterOutputStream; 37 | import java.io.IOException; 38 | import java.io.OutputStream; 39 | 40 | /** 41 | * Output stream that will automatically send every write to the wrapped 42 | * OutputStream according to chunked transfer: 43 | * http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1 44 | */ 45 | public class ChunkedOutputStream extends FilterOutputStream { 46 | 47 | public ChunkedOutputStream(OutputStream out) { 48 | super(out); 49 | } 50 | 51 | @Override 52 | public void write(int b) throws IOException { 53 | byte[] data = { 54 | (byte) b 55 | }; 56 | write(data, 0, 1); 57 | } 58 | 59 | @Override 60 | public void write(byte[] b) throws IOException { 61 | write(b, 0, b.length); 62 | } 63 | 64 | @Override 65 | public void write(byte[] b, int off, int len) throws IOException { 66 | if (len == 0) 67 | return; 68 | out.write(String.format("%x\r\n", len).getBytes()); 69 | out.write(b, off, len); 70 | out.write("\r\n".getBytes()); 71 | } 72 | 73 | public void finish() throws IOException { 74 | out.write("0\r\n\r\n".getBytes()); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/util/ServerRunner.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.util; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Webserver 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.IOException; 37 | import java.util.logging.Level; 38 | import java.util.logging.Logger; 39 | 40 | import org.nanohttpd.protocols.http.NanoHTTPD; 41 | 42 | public class ServerRunner { 43 | 44 | /** 45 | * logger to log to. 46 | */ 47 | private static final Logger LOG = Logger.getLogger(ServerRunner.class.getName()); 48 | 49 | public static void executeInstance(NanoHTTPD server) { 50 | try { 51 | server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); 52 | } catch (IOException ioe) { 53 | System.err.println("Couldn't start server:\n" + ioe); 54 | System.exit(-1); 55 | } 56 | 57 | System.out.println("Server started, Hit Enter to stop.\n"); 58 | 59 | try { 60 | System.in.read(); 61 | } catch (Throwable ignored) { 62 | } 63 | 64 | server.stop(); 65 | System.out.println("Server stopped.\n"); 66 | } 67 | 68 | public static void run(Class serverClass) { 69 | try { 70 | executeInstance(serverClass.newInstance()); 71 | } catch (Exception e) { 72 | ServerRunner.LOG.log(Level.SEVERE, "Could not create server", e); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/tempfiles/DefaultTempFile.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.tempfiles; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.File; 37 | import java.io.FileOutputStream; 38 | import java.io.IOException; 39 | import java.io.OutputStream; 40 | 41 | import org.nanohttpd.protocols.http.NanoHTTPD; 42 | 43 | /** 44 | * Default strategy for creating and cleaning up temporary files. 45 | *

46 | *

47 | * By default, files are created by File.createTempFile() in the 48 | * directory specified. 49 | *

50 | */ 51 | public class DefaultTempFile implements ITempFile { 52 | 53 | private final File file; 54 | 55 | private final OutputStream fstream; 56 | 57 | public DefaultTempFile(File tempdir) throws IOException { 58 | this.file = File.createTempFile("NanoHTTPD-", "", tempdir); 59 | this.fstream = new FileOutputStream(this.file); 60 | } 61 | 62 | @Override 63 | public void delete() throws Exception { 64 | NanoHTTPD.safeClose(this.fstream); 65 | if (!this.file.delete()) { 66 | throw new Exception("could not delete temporary file: " + this.file.getAbsolutePath()); 67 | } 68 | } 69 | 70 | @Override 71 | public String getName() { 72 | return this.file.getAbsolutePath(); 73 | } 74 | 75 | @Override 76 | public OutputStream open() throws Exception { 77 | return this.fstream; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/sockets/SecureServerSocketFactory.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.sockets; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.IOException; 37 | import java.net.ServerSocket; 38 | 39 | import javax.net.ssl.SSLServerSocket; 40 | import javax.net.ssl.SSLServerSocketFactory; 41 | 42 | import org.nanohttpd.util.IFactoryThrowing; 43 | 44 | /** 45 | * Creates a new SSLServerSocket 46 | */ 47 | public class SecureServerSocketFactory implements IFactoryThrowing { 48 | 49 | private SSLServerSocketFactory sslServerSocketFactory; 50 | 51 | private String[] sslProtocols; 52 | 53 | public SecureServerSocketFactory(SSLServerSocketFactory sslServerSocketFactory, String[] sslProtocols) { 54 | this.sslServerSocketFactory = sslServerSocketFactory; 55 | this.sslProtocols = sslProtocols; 56 | } 57 | 58 | @Override 59 | public ServerSocket create() throws IOException { 60 | SSLServerSocket ss = null; 61 | ss = (SSLServerSocket) this.sslServerSocketFactory.createServerSocket(); 62 | if (this.sslProtocols != null) { 63 | ss.setEnabledProtocols(this.sslProtocols); 64 | } else { 65 | ss.setEnabledProtocols(ss.getSupportedProtocols()); 66 | } 67 | ss.setUseClientMode(false); 68 | ss.setWantClientAuth(false); 69 | ss.setNeedClientAuth(false); 70 | return ss; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/content/Cookie.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.content; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.text.SimpleDateFormat; 37 | import java.util.Calendar; 38 | import java.util.Locale; 39 | import java.util.TimeZone; 40 | 41 | /** 42 | * A simple cookie representation. This is old code and is flawed in many ways. 43 | * 44 | * @author LordFokas 45 | */ 46 | public class Cookie { 47 | 48 | public static String getHTTPTime(int days) { 49 | Calendar calendar = Calendar.getInstance(); 50 | SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); 51 | dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 52 | calendar.add(Calendar.DAY_OF_MONTH, days); 53 | return dateFormat.format(calendar.getTime()); 54 | } 55 | 56 | private final String n, v, e; 57 | 58 | public Cookie(String name, String value) { 59 | this(name, value, 30); 60 | } 61 | 62 | public Cookie(String name, String value, int numDays) { 63 | this.n = name; 64 | this.v = value; 65 | this.e = getHTTPTime(numDays); 66 | } 67 | 68 | public Cookie(String name, String value, String expires) { 69 | this.n = name; 70 | this.v = value; 71 | this.e = expires; 72 | } 73 | 74 | public String getHTTPHeader() { 75 | String fmt = "%s=%s; expires=%s"; 76 | return String.format(fmt, this.n, this.v, this.e); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/HttpSessionHeadersTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import static org.junit.Assert.assertEquals; 37 | import static org.junit.Assert.assertNotNull; 38 | 39 | import java.io.ByteArrayInputStream; 40 | import java.io.ByteArrayOutputStream; 41 | import java.net.InetAddress; 42 | 43 | import org.junit.Test; 44 | import org.nanohttpd.protocols.http.HTTPSession; 45 | 46 | public class HttpSessionHeadersTest extends HttpServerTest { 47 | 48 | private static final String DUMMY_REQUEST_CONTENT = "dummy request content"; 49 | 50 | private static final TestTempFileManager TEST_TEMP_FILE_MANAGER = new TestTempFileManager(); 51 | 52 | @Test 53 | public void testHeadersRemoteIp() throws Exception { 54 | ByteArrayInputStream inputStream = new ByteArrayInputStream(HttpSessionHeadersTest.DUMMY_REQUEST_CONTENT.getBytes()); 55 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 56 | String[] ipAddresses = { 57 | "127.0.0.1", 58 | "8.8.8.8", 59 | }; 60 | for (String ipAddress : ipAddresses) { 61 | InetAddress inetAddress = InetAddress.getByName(ipAddress); 62 | HTTPSession session = this.testServer.createSession(HttpSessionHeadersTest.TEST_TEMP_FILE_MANAGER, inputStream, outputStream, inetAddress); 63 | assertEquals(ipAddress, session.getRemoteIpAddress()); 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/HttpPutRequestTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import static junit.framework.Assert.assertTrue; 37 | 38 | import java.io.BufferedReader; 39 | import java.io.ByteArrayOutputStream; 40 | import java.io.FileReader; 41 | import java.util.List; 42 | 43 | import org.junit.Test; 44 | 45 | public class HttpPutRequestTest extends HttpServerTest { 46 | 47 | @Test 48 | public void testPutRequestSendsContent() throws Exception { 49 | ByteArrayOutputStream outputStream = invokeServer("PUT " + HttpServerTest.URI + " HTTP/1.1\r\n\r\nBodyData 1\nLine 2"); 50 | 51 | String[] expectedOutput = { 52 | "HTTP/1.1 200 OK", 53 | "Content-Type: text/html", 54 | "Date: .*", 55 | "Connection: keep-alive", 56 | "Content-Length: 0", 57 | "" 58 | }; 59 | 60 | assertResponse(outputStream, expectedOutput); 61 | 62 | assertTrue(this.testServer.files.containsKey("content")); 63 | BufferedReader reader = null; 64 | try { 65 | String[] expectedInputToServeMethodViaFile = { 66 | "BodyData 1", 67 | "Line 2" 68 | }; 69 | reader = new BufferedReader(new FileReader(this.testServer.files.get("content"))); 70 | List lines = readLinesFromFile(reader); 71 | assertLinesOfText(expectedInputToServeMethodViaFile, lines); 72 | } finally { 73 | if (reader != null) { 74 | reader.close(); 75 | } 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /samples/src/main/java/org/nanohttpd/samples/http/HelloServer.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.samples.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Samples 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.util.Map; 37 | import java.util.logging.Logger; 38 | 39 | import org.nanohttpd.protocols.http.IHTTPSession; 40 | import org.nanohttpd.protocols.http.NanoHTTPD; 41 | import org.nanohttpd.protocols.http.request.Method; 42 | import org.nanohttpd.protocols.http.response.Response; 43 | import org.nanohttpd.util.ServerRunner; 44 | 45 | /** 46 | * An example of subclassing NanoHTTPD to make a custom HTTP server. 47 | */ 48 | public class HelloServer extends NanoHTTPD { 49 | 50 | /** 51 | * logger to log to. 52 | */ 53 | private static final Logger LOG = Logger.getLogger(HelloServer.class.getName()); 54 | 55 | public static void main(String[] args) { 56 | ServerRunner.run(HelloServer.class); 57 | } 58 | 59 | public HelloServer() { 60 | super(8080); 61 | } 62 | 63 | @Override 64 | public Response serve(IHTTPSession session) { 65 | Method method = session.getMethod(); 66 | String uri = session.getUri(); 67 | HelloServer.LOG.info(method + " '" + uri + "' "); 68 | 69 | String msg = "

Hello server

\n"; 70 | Map parms = session.getParms(); 71 | if (parms.get("username") == null) { 72 | msg += "
\n" + "

Your name:

\n" + "
\n"; 73 | } else { 74 | msg += "

Hello, " + parms.get("username") + "!

"; 75 | } 76 | 77 | msg += "\n"; 78 | 79 | return Response.newFixedLengthResponse(msg); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/IHTTPSession.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.IOException; 37 | import java.io.InputStream; 38 | import java.util.List; 39 | import java.util.Map; 40 | 41 | import org.nanohttpd.protocols.http.NanoHTTPD.ResponseException; 42 | import org.nanohttpd.protocols.http.content.CookieHandler; 43 | import org.nanohttpd.protocols.http.request.Method; 44 | 45 | /** 46 | * Handles one session, i.e. parses the HTTP request and returns the response. 47 | */ 48 | public interface IHTTPSession { 49 | 50 | void execute() throws IOException; 51 | 52 | CookieHandler getCookies(); 53 | 54 | Map getHeaders(); 55 | 56 | InputStream getInputStream(); 57 | 58 | Method getMethod(); 59 | 60 | /** 61 | * This method will only return the first value for a given parameter. You 62 | * will want to use getParameters if you expect multiple values for a given 63 | * key. 64 | * 65 | * @deprecated use {@link #getParameters()} instead. 66 | */ 67 | @Deprecated 68 | Map getParms(); 69 | 70 | Map> getParameters(); 71 | 72 | String getQueryParameterString(); 73 | 74 | /** 75 | * @return the path part of the URL. 76 | */ 77 | String getUri(); 78 | 79 | /** 80 | * Adds the files in the request body to the files map. 81 | * 82 | * @param files 83 | * map to modify 84 | */ 85 | void parseBody(Map files) throws IOException, ResponseException; 86 | 87 | /** 88 | * Get the remote ip address of the requester. 89 | * 90 | * @return the IP address. 91 | */ 92 | String getRemoteIpAddress(); 93 | } 94 | -------------------------------------------------------------------------------- /websocket/src/main/java/org/nanohttpd/protocols/websockets/CloseFrame.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.websockets; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Websocket 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.nio.charset.CharacterCodingException; 37 | 38 | public class CloseFrame extends WebSocketFrame { 39 | 40 | private static byte[] generatePayload(CloseCode code, String closeReason) throws CharacterCodingException { 41 | if (code != null) { 42 | byte[] reasonBytes = text2Binary(closeReason); 43 | byte[] payload = new byte[reasonBytes.length + 2]; 44 | payload[0] = (byte) (code.getValue() >> 8 & 0xFF); 45 | payload[1] = (byte) (code.getValue() & 0xFF); 46 | System.arraycopy(reasonBytes, 0, payload, 2, reasonBytes.length); 47 | return payload; 48 | } else { 49 | return new byte[0]; 50 | } 51 | } 52 | 53 | private CloseCode _closeCode; 54 | 55 | private String _closeReason; 56 | 57 | public CloseFrame(CloseCode code, String closeReason) throws CharacterCodingException { 58 | super(OpCode.Close, true, generatePayload(code, closeReason)); 59 | } 60 | 61 | public CloseFrame(WebSocketFrame wrap) throws CharacterCodingException { 62 | super(wrap); 63 | assert wrap.getOpCode() == OpCode.Close; 64 | if (wrap.getBinaryPayload().length >= 2) { 65 | this._closeCode = CloseCode.find((wrap.getBinaryPayload()[0] & 0xFF) << 8 | wrap.getBinaryPayload()[1] & 0xFF); 66 | this._closeReason = binary2Text(getBinaryPayload(), 2, getBinaryPayload().length - 2); 67 | } 68 | } 69 | 70 | public CloseCode getCloseCode() { 71 | return this._closeCode; 72 | } 73 | 74 | public String getCloseReason() { 75 | return this._closeReason; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/tempfiles/DefaultTempFileManager.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.tempfiles; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.File; 37 | import java.util.ArrayList; 38 | import java.util.List; 39 | import java.util.logging.Level; 40 | 41 | import org.nanohttpd.protocols.http.NanoHTTPD; 42 | 43 | /** 44 | * Default strategy for creating and cleaning up temporary files. 45 | *

46 | *

47 | * This class stores its files in the standard location (that is, wherever 48 | * java.io.tmpdir points to). Files are added to an internal list, 49 | * and deleted when no longer needed (that is, when clear() is 50 | * invoked at the end of processing a request). 51 | *

52 | */ 53 | public class DefaultTempFileManager implements ITempFileManager { 54 | 55 | private final File tmpdir; 56 | 57 | private final List tempFiles; 58 | 59 | public DefaultTempFileManager() { 60 | this.tmpdir = new File(System.getProperty("java.io.tmpdir")); 61 | if (!tmpdir.exists()) { 62 | tmpdir.mkdirs(); 63 | } 64 | this.tempFiles = new ArrayList(); 65 | } 66 | 67 | @Override 68 | public void clear() { 69 | for (ITempFile file : this.tempFiles) { 70 | try { 71 | file.delete(); 72 | } catch (Exception ignored) { 73 | NanoHTTPD.LOG.log(Level.WARNING, "could not delete file ", ignored); 74 | } 75 | } 76 | this.tempFiles.clear(); 77 | } 78 | 79 | @Override 80 | public ITempFile createTempFile(String filename_hint) throws Exception { 81 | DefaultTempFile tempFile = new DefaultTempFile(this.tmpdir); 82 | this.tempFiles.add(tempFile); 83 | return tempFile; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/integration/ShutdownTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http.integration; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import static org.junit.Assert.fail; 37 | 38 | import java.io.IOException; 39 | import java.io.InputStream; 40 | import java.net.HttpURLConnection; 41 | import java.net.MalformedURLException; 42 | import java.net.URL; 43 | 44 | import org.junit.Test; 45 | import org.nanohttpd.protocols.http.IHTTPSession; 46 | import org.nanohttpd.protocols.http.NanoHTTPD; 47 | import org.nanohttpd.protocols.http.response.Response; 48 | 49 | public class ShutdownTest { 50 | 51 | private class TestServer extends NanoHTTPD { 52 | 53 | public TestServer() { 54 | super(8092); 55 | } 56 | 57 | @Override 58 | public Response serve(IHTTPSession session) { 59 | return Response.newFixedLengthResponse("Whatever"); 60 | } 61 | } 62 | 63 | @Test 64 | public void connectionsAreClosedWhenServerStops() throws IOException { 65 | TestServer server = new TestServer(); 66 | server.start(); 67 | makeRequest(); 68 | server.stop(); 69 | try { 70 | makeRequest(); 71 | fail("Connection should be closed!"); 72 | } catch (IOException e) { 73 | // Expected exception 74 | } 75 | } 76 | 77 | private void makeRequest() throws MalformedURLException, IOException { 78 | HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost:8092/").openConnection(); 79 | // Keep-alive seems to be on by default, but just in case that changes. 80 | connection.addRequestProperty("Connection", "keep-alive"); 81 | InputStream in = connection.getInputStream(); 82 | while (in.available() > 0) { 83 | in.read(); 84 | } 85 | in.close(); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/threading/DefaultAsyncRunner.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.threading; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.util.ArrayList; 37 | import java.util.Collections; 38 | import java.util.List; 39 | 40 | import org.nanohttpd.protocols.http.ClientHandler; 41 | 42 | /** 43 | * Default threading strategy for NanoHTTPD. 44 | *

45 | *

46 | * By default, the server spawns a new Thread for every incoming request. These 47 | * are set to daemon status, and named according to the request number. 48 | * The name is useful when profiling the application. 49 | *

50 | */ 51 | public class DefaultAsyncRunner implements IAsyncRunner { 52 | 53 | protected long requestCount; 54 | 55 | private final List running = Collections.synchronizedList(new ArrayList()); 56 | 57 | /** 58 | * @return a list with currently running clients. 59 | */ 60 | public List getRunning() { 61 | return running; 62 | } 63 | 64 | @Override 65 | public void closeAll() { 66 | // copy of the list for concurrency 67 | for (ClientHandler clientHandler : new ArrayList(this.running)) { 68 | clientHandler.close(); 69 | } 70 | } 71 | 72 | @Override 73 | public void closed(ClientHandler clientHandler) { 74 | this.running.remove(clientHandler); 75 | } 76 | 77 | @Override 78 | public void exec(ClientHandler clientHandler) { 79 | ++this.requestCount; 80 | this.running.add(clientHandler); 81 | createThread(clientHandler).start(); 82 | } 83 | 84 | protected Thread createThread(ClientHandler clientHandler) { 85 | Thread t = new Thread(clientHandler); 86 | t.setDaemon(true); 87 | t.setName("NanoHttpd Request Processor (#" + this.requestCount + ")"); 88 | return t; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/ServerRunnable.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.IOException; 37 | import java.io.InputStream; 38 | import java.net.InetSocketAddress; 39 | import java.net.Socket; 40 | import java.util.logging.Level; 41 | 42 | /** 43 | * The runnable that will be used for the main listening thread. 44 | */ 45 | public class ServerRunnable implements Runnable { 46 | 47 | private NanoHTTPD httpd; 48 | 49 | private final int timeout; 50 | 51 | private IOException bindException; 52 | 53 | private boolean hasBinded = false; 54 | 55 | public ServerRunnable(NanoHTTPD httpd, int timeout) { 56 | this.httpd = httpd; 57 | this.timeout = timeout; 58 | } 59 | 60 | @Override 61 | public void run() { 62 | try { 63 | httpd.getMyServerSocket().bind(httpd.hostname != null ? new InetSocketAddress(httpd.hostname, httpd.myPort) : new InetSocketAddress(httpd.myPort)); 64 | hasBinded = true; 65 | } catch (IOException e) { 66 | this.bindException = e; 67 | return; 68 | } 69 | do { 70 | try { 71 | final Socket finalAccept = httpd.getMyServerSocket().accept(); 72 | if (this.timeout > 0) { 73 | finalAccept.setSoTimeout(this.timeout); 74 | } 75 | final InputStream inputStream = finalAccept.getInputStream(); 76 | httpd.asyncRunner.exec(httpd.createClientHandler(finalAccept, inputStream)); 77 | } catch (IOException e) { 78 | NanoHTTPD.LOG.log(Level.FINE, "Communication with the client broken", e); 79 | } 80 | } while (!httpd.getMyServerSocket().isClosed()); 81 | } 82 | 83 | public IOException getBindException() { 84 | return bindException; 85 | } 86 | 87 | public boolean hasBinded() { 88 | return hasBinded; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/LoadKeyStoreTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | import static org.junit.Assert.assertNotNull; 4 | import static org.junit.Assert.assertNull; 5 | 6 | import java.io.IOException; 7 | import java.io.InputStream; 8 | 9 | import javax.net.ssl.SSLServerSocketFactory; 10 | 11 | /* 12 | * #%L 13 | * NanoHttpd-Core 14 | * %% 15 | * Copyright (C) 2012 - 2015 nanohttpd 16 | * %% 17 | * Redistribution and use in source and binary forms, with or without modification, 18 | * are permitted provided that the following conditions are met: 19 | * 20 | * 1. Redistributions of source code must retain the above copyright notice, this 21 | * list of conditions and the following disclaimer. 22 | * 23 | * 2. Redistributions in binary form must reproduce the above copyright notice, 24 | * this list of conditions and the following disclaimer in the documentation 25 | * and/or other materials provided with the distribution. 26 | * 27 | * 3. Neither the name of the nanohttpd nor the names of its contributors 28 | * may be used to endorse or promote products derived from this software without 29 | * specific prior written permission. 30 | * 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 32 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 33 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 34 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 35 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 36 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 38 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 39 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 40 | * OF THE POSSIBILITY OF SUCH DAMAGE. 41 | * #L% 42 | */ 43 | 44 | import org.junit.Rule; 45 | import org.junit.Test; 46 | import org.junit.rules.ExpectedException; 47 | import org.nanohttpd.protocols.http.NanoHTTPD; 48 | 49 | public class LoadKeyStoreTest { 50 | 51 | @Rule 52 | public ExpectedException thrown = ExpectedException.none(); 53 | 54 | @Test 55 | public void loadKeyStoreFromResources() throws Exception { 56 | String keyStorePath = "/keystore.jks"; 57 | InputStream resourceAsStream = this.getClass().getResourceAsStream(keyStorePath); 58 | assertNotNull(resourceAsStream); 59 | 60 | SSLServerSocketFactory sslServerSocketFactory = NanoHTTPD.makeSSLSocketFactory(keyStorePath, "password".toCharArray()); 61 | assertNotNull(sslServerSocketFactory); 62 | } 63 | 64 | @Test 65 | public void loadKeyStoreFromResourcesWrongPassword() throws Exception { 66 | String keyStorePath = "/keystore.jks"; 67 | InputStream resourceAsStream = this.getClass().getResourceAsStream(keyStorePath); 68 | assertNotNull(resourceAsStream); 69 | 70 | thrown.expect(IOException.class); 71 | NanoHTTPD.makeSSLSocketFactory(keyStorePath, "wrongpassword".toCharArray()); 72 | } 73 | 74 | @Test 75 | public void loadNonExistentKeyStoreFromResources() throws Exception { 76 | String nonExistentPath = "/nokeystorehere.jks"; 77 | InputStream resourceAsStream = this.getClass().getResourceAsStream(nonExistentPath); 78 | assertNull(resourceAsStream); 79 | 80 | thrown.expect(IOException.class); 81 | NanoHTTPD.makeSSLSocketFactory(nonExistentPath, "".toCharArray()); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/JavaIOTempDirExistTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.File; 37 | import java.io.IOException; 38 | import java.util.UUID; 39 | 40 | import org.junit.Assert; 41 | import org.junit.Test; 42 | import org.nanohttpd.protocols.http.tempfiles.DefaultTempFile; 43 | import org.nanohttpd.protocols.http.tempfiles.DefaultTempFileManager; 44 | 45 | /** 46 | * Created by Victor Nikiforov on 10/16/15. 47 | */ 48 | public class JavaIOTempDirExistTest { 49 | 50 | @Test 51 | public void testJavaIoTempDefault() throws Exception { 52 | String tmpdir = System.getProperty("java.io.tmpdir"); 53 | DefaultTempFileManager manager = new DefaultTempFileManager(); 54 | DefaultTempFile tempFile = (DefaultTempFile) manager.createTempFile("xx"); 55 | File tempFileBackRef = new File(tempFile.getName()); 56 | Assert.assertEquals(tempFileBackRef.getParentFile(), new File(tmpdir)); 57 | 58 | // force an exception 59 | tempFileBackRef.delete(); 60 | Exception e = null; 61 | try { 62 | tempFile.delete(); 63 | } catch (Exception ex) { 64 | e = ex; 65 | } 66 | Assert.assertNotNull(e); 67 | manager.clear(); 68 | } 69 | 70 | @Test 71 | public void testJavaIoTempSpecific() throws IOException { 72 | final String tmpdir = System.getProperty("java.io.tmpdir"); 73 | try { 74 | String tempFileName = UUID.randomUUID().toString(); 75 | File newDir = new File("target", tempFileName); 76 | System.setProperty("java.io.tmpdir", newDir.getAbsolutePath()); 77 | Assert.assertEquals(false, newDir.exists()); 78 | new DefaultTempFileManager(); 79 | Assert.assertEquals(true, newDir.exists()); 80 | newDir.delete(); 81 | } finally { 82 | System.setProperty("java.io.tmpdir", tmpdir); 83 | } 84 | 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /websocket/src/test/resources/echo-test.html: -------------------------------------------------------------------------------- 1 | 33 | 34 | 35 | 36 | WebSocket Test 37 | 84 | 85 | 86 |

WebSocket Test

87 | 88 |
89 | 90 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/InvalidRequestTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | import static junit.framework.Assert.assertNotNull; 36 | import static junit.framework.Assert.assertTrue; 37 | 38 | import org.junit.Test; 39 | 40 | public class InvalidRequestTest extends HttpServerTest { 41 | 42 | @Test 43 | public void testGetRequestWithoutProtocol() { 44 | invokeServer("GET " + HttpServerTest.URI + "\r\nX-Important-Header: foo"); 45 | 46 | assertNotNull(this.testServer.parms); 47 | assertNotNull(this.testServer.parameters); 48 | assertTrue(this.testServer.header.size() > 0); 49 | assertNotNull(this.testServer.files); 50 | assertNotNull(this.testServer.uri); 51 | } 52 | 53 | @Test 54 | public void testGetRequestWithProtocol() { 55 | invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1\r\nX-Important-Header: foo"); 56 | 57 | assertNotNull(this.testServer.parms); 58 | assertNotNull(this.testServer.parameters); 59 | assertTrue(this.testServer.header.size() > 0); 60 | assertNotNull(this.testServer.files); 61 | assertNotNull(this.testServer.uri); 62 | } 63 | 64 | @Test 65 | public void testPostRequestWithoutProtocol() { 66 | invokeServer("POST " + HttpServerTest.URI + "\r\nContent-Length: 123"); 67 | assertNotNull(this.testServer.parms); 68 | assertNotNull(this.testServer.parameters); 69 | assertTrue(this.testServer.header.size() > 0); 70 | assertNotNull(this.testServer.files); 71 | assertNotNull(this.testServer.uri); 72 | } 73 | 74 | @Test 75 | public void testPostRequestWithProtocol() { 76 | invokeServer("POST " + HttpServerTest.URI + " HTTP/1.1\r\nContent-Length: 123"); 77 | assertNotNull(this.testServer.parms); 78 | assertNotNull(this.testServer.parameters); 79 | assertTrue(this.testServer.header.size() > 0); 80 | assertNotNull(this.testServer.files); 81 | assertNotNull(this.testServer.uri); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/HttpChunkedResponseTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.ByteArrayOutputStream; 37 | import java.io.IOException; 38 | import java.io.PipedInputStream; 39 | 40 | import org.nanohttpd.protocols.http.response.Response; 41 | import org.nanohttpd.protocols.http.response.Status; 42 | 43 | public class HttpChunkedResponseTest extends HttpServerTest { 44 | 45 | private static class ChunkedInputStream extends PipedInputStream { 46 | 47 | int chunk = 0; 48 | 49 | String[] chunks; 50 | 51 | private ChunkedInputStream(String[] chunks) { 52 | this.chunks = chunks; 53 | } 54 | 55 | @Override 56 | public synchronized int read(byte[] buffer, int off, int len) throws IOException { 57 | // Too implementation-linked, but... 58 | for (int i = 0; i < this.chunks[this.chunk].length(); ++i) { 59 | buffer[i] = (byte) this.chunks[this.chunk].charAt(i); 60 | } 61 | return this.chunks[this.chunk++].length(); 62 | } 63 | } 64 | 65 | @org.junit.Test 66 | public void thatChunkedContentIsChunked() throws Exception { 67 | PipedInputStream pipedInputStream = new ChunkedInputStream(new String[]{ 68 | "some", 69 | "thing which is longer than sixteen characters", 70 | "whee!", 71 | "" 72 | }); 73 | String[] expected = { 74 | "HTTP/1.1 200 OK", 75 | "Content-Type: what/ever", 76 | "Date: .*", 77 | "Connection: keep-alive", 78 | "Transfer-Encoding: chunked", 79 | "", 80 | "4", 81 | "some", 82 | "2d", 83 | "thing which is longer than sixteen characters", 84 | "5", 85 | "whee!", 86 | "0", 87 | "" 88 | }; 89 | this.testServer.response = Response.newChunkedResponse(Status.OK, "what/ever", pipedInputStream); 90 | this.testServer.response.setChunkedTransfer(true); 91 | 92 | ByteArrayOutputStream byteArrayOutputStream = invokeServer("GET / HTTP/1.1"); 93 | 94 | assertResponse(byteArrayOutputStream, expected); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/ServerSocketFactoryTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | import java.io.File; 4 | 5 | /* 6 | * #%L 7 | * NanoHttpd-Core 8 | * %% 9 | * Copyright (C) 2012 - 2015 nanohttpd 10 | * %% 11 | * Redistribution and use in source and binary forms, with or without modification, 12 | * are permitted provided that the following conditions are met: 13 | * 14 | * 1. Redistributions of source code must retain the above copyright notice, this 15 | * list of conditions and the following disclaimer. 16 | * 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 21 | * 3. Neither the name of the nanohttpd nor the names of its contributors 22 | * may be used to endorse or promote products derived from this software without 23 | * specific prior written permission. 24 | * 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 27 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 29 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 30 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 32 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 33 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 34 | * OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * #L% 36 | */ 37 | 38 | import java.io.IOException; 39 | import java.net.ServerSocket; 40 | 41 | import org.junit.Assert; 42 | import org.junit.Test; 43 | import org.nanohttpd.protocols.http.NanoHTTPD; 44 | import org.nanohttpd.protocols.http.sockets.SecureServerSocketFactory; 45 | import org.nanohttpd.util.IFactoryThrowing; 46 | 47 | public class ServerSocketFactoryTest extends NanoHTTPD { 48 | 49 | public static final int PORT = 8192; 50 | 51 | public ServerSocketFactoryTest() { 52 | super(PORT); 53 | 54 | this.setServerSocketFactory(new TestFactory()); 55 | } 56 | 57 | @Test 58 | public void isCustomServerSocketFactory() { 59 | System.out.println("CustomServerSocketFactory test"); 60 | Assert.assertTrue(this.getServerSocketFactory() instanceof TestFactory); 61 | } 62 | 63 | @Test 64 | public void testCreateServerSocket() { 65 | System.out.println("CreateServerSocket test"); 66 | ServerSocket ss = null; 67 | try { 68 | ss = this.getServerSocketFactory().create(); 69 | } catch (IOException e) { 70 | } 71 | Assert.assertTrue(ss != null); 72 | } 73 | 74 | @Test 75 | public void testSSLServerSocketFail() { 76 | String[] protocols = { 77 | "" 78 | }; 79 | System.setProperty("javax.net.ssl.trustStore", new File("src/test/resources/keystore.jks").getAbsolutePath()); 80 | IFactoryThrowing ssFactory = new SecureServerSocketFactory(null, protocols); 81 | ServerSocket ss = null; 82 | try { 83 | ss = ssFactory.create(); 84 | } catch (Exception e) { 85 | } 86 | Assert.assertTrue(ss == null); 87 | 88 | } 89 | 90 | private class TestFactory implements IFactoryThrowing { 91 | 92 | @Override 93 | public ServerSocket create() { 94 | try { 95 | return new ServerSocket(); 96 | } catch (IOException e) { 97 | e.printStackTrace(); 98 | } 99 | return null; 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/CookieTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | import static org.junit.Assert.assertFalse; 36 | import static org.junit.Assert.assertNotNull; 37 | import static org.junit.Assert.assertTrue; 38 | 39 | import java.util.Random; 40 | 41 | import org.junit.Test; 42 | import org.nanohttpd.protocols.http.content.Cookie; 43 | 44 | public class CookieTest { 45 | 46 | @Test 47 | public void testGetHTTPTime() { 48 | Random random = new Random(); 49 | int randomExpirationTime = random.nextInt(100); 50 | assertNotNull("getHTTPTime should return a non-null value for " + randomExpirationTime + " days", Cookie.getHTTPTime(randomExpirationTime)); 51 | } 52 | 53 | @Test 54 | public void testCookieWithNoExplicitExpirationTime() { 55 | Cookie cookie = new Cookie("CookieKey", "CookieValue"); 56 | assertTrue("Cookie header should contain cookie key", cookie.getHTTPHeader().contains("CookieKey")); 57 | assertTrue("Cookie header should contain cookie value", cookie.getHTTPHeader().contains("CookieValue")); 58 | } 59 | 60 | @Test 61 | public void testCookieWithExplicitExpirationTime() { 62 | Cookie cookie = new Cookie("CookieKey", "CookieValue", 40); 63 | assertFalse("The default 30 days expires string should not be avaialbe in the cookie header" + " because the expiry has been specified as 40 days", cookie 64 | .getHTTPHeader().contains(Cookie.getHTTPTime(30))); 65 | assertTrue("Cookie header should contain cookie key", cookie.getHTTPHeader().contains("CookieKey")); 66 | assertTrue("Cookie header should contain cookie value", cookie.getHTTPHeader().contains("CookieValue")); 67 | } 68 | 69 | @Test 70 | public void testCookieWithExpiresString() { 71 | Random random = new Random(); 72 | int randomExpirationTime = random.nextInt(100); 73 | String expiresString = Cookie.getHTTPTime(randomExpirationTime); 74 | Cookie cookie = new Cookie("CookieKey", "CookieValue", expiresString); 75 | assertTrue("Cookie should contain the expirs string passed in the constructor", cookie.getHTTPHeader().contains(expiresString)); 76 | assertTrue("Cookie header should contain cookie key", cookie.getHTTPHeader().contains("CookieKey")); 77 | assertTrue("Cookie header should contain cookie value", cookie.getHTTPHeader().contains("CookieValue")); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/ClientHandler.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.InputStream; 37 | import java.io.OutputStream; 38 | import java.net.Socket; 39 | import java.net.SocketException; 40 | import java.net.SocketTimeoutException; 41 | import java.util.logging.Level; 42 | 43 | import org.nanohttpd.protocols.http.tempfiles.ITempFileManager; 44 | 45 | /** 46 | * The runnable that will be used for every new client connection. 47 | */ 48 | public class ClientHandler implements Runnable { 49 | 50 | private final NanoHTTPD httpd; 51 | 52 | private final InputStream inputStream; 53 | 54 | private final Socket acceptSocket; 55 | 56 | public ClientHandler(NanoHTTPD httpd, InputStream inputStream, Socket acceptSocket) { 57 | this.httpd = httpd; 58 | this.inputStream = inputStream; 59 | this.acceptSocket = acceptSocket; 60 | } 61 | 62 | public void close() { 63 | NanoHTTPD.safeClose(this.inputStream); 64 | NanoHTTPD.safeClose(this.acceptSocket); 65 | } 66 | 67 | @Override 68 | public void run() { 69 | OutputStream outputStream = null; 70 | try { 71 | outputStream = this.acceptSocket.getOutputStream(); 72 | ITempFileManager tempFileManager = httpd.getTempFileManagerFactory().create(); 73 | HTTPSession session = new HTTPSession(httpd, tempFileManager, this.inputStream, outputStream, this.acceptSocket.getInetAddress()); 74 | while (!this.acceptSocket.isClosed()) { 75 | session.execute(); 76 | } 77 | } catch (Exception e) { 78 | // When the socket is closed by the client, 79 | // we throw our own SocketException 80 | // to break the "keep alive" loop above. If 81 | // the exception was anything other 82 | // than the expected SocketException OR a 83 | // SocketTimeoutException, print the 84 | // stacktrace 85 | if (!(e instanceof SocketException && "NanoHttpd Shutdown".equals(e.getMessage())) && !(e instanceof SocketTimeoutException)) { 86 | NanoHTTPD.LOG.log(Level.SEVERE, "Communication with the client broken, or an bug in the handler code", e); 87 | } 88 | } finally { 89 | NanoHTTPD.safeClose(outputStream); 90 | NanoHTTPD.safeClose(this.inputStream); 91 | NanoHTTPD.safeClose(this.acceptSocket); 92 | httpd.asyncRunner.closed(this); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /samples/src/main/java/org/nanohttpd/samples/tempfiles/TempFilesServer.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.samples.tempfiles; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Samples 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.File; 37 | import java.util.ArrayList; 38 | import java.util.List; 39 | 40 | import org.nanohttpd.protocols.http.tempfiles.DefaultTempFile; 41 | import org.nanohttpd.protocols.http.tempfiles.ITempFile; 42 | import org.nanohttpd.protocols.http.tempfiles.ITempFileManager; 43 | import org.nanohttpd.samples.http.DebugServer; 44 | import org.nanohttpd.util.IFactory; 45 | import org.nanohttpd.util.ServerRunner; 46 | 47 | /** 48 | * @author Paul S. Hawke (paul.hawke@gmail.com) On: 3/9/13 at 12:47 AM 49 | */ 50 | public class TempFilesServer extends DebugServer { 51 | 52 | private static class ExampleManager implements ITempFileManager { 53 | 54 | private final File tmpdir; 55 | 56 | private final List tempFiles; 57 | 58 | private ExampleManager() { 59 | this.tmpdir = new File(System.getProperty("java.io.tmpdir")); 60 | this.tempFiles = new ArrayList(); 61 | } 62 | 63 | @Override 64 | public void clear() { 65 | if (!this.tempFiles.isEmpty()) { 66 | System.out.println("Cleaning up:"); 67 | } 68 | for (ITempFile file : this.tempFiles) { 69 | try { 70 | System.out.println(" " + file.getName()); 71 | file.delete(); 72 | } catch (Exception ignored) { 73 | } 74 | } 75 | this.tempFiles.clear(); 76 | } 77 | 78 | @Override 79 | public ITempFile createTempFile(String filename_hint) throws Exception { 80 | DefaultTempFile tempFile = new DefaultTempFile(this.tmpdir); 81 | this.tempFiles.add(tempFile); 82 | System.out.println("Created tempFile: " + tempFile.getName()); 83 | return tempFile; 84 | } 85 | } 86 | 87 | private static class ExampleManagerFactory implements IFactory { 88 | 89 | @Override 90 | public ITempFileManager create() { 91 | return new ExampleManager(); 92 | } 93 | } 94 | 95 | public static void main(String[] args) { 96 | TempFilesServer server = new TempFilesServer(); 97 | server.setTempFileManagerFactory(new ExampleManagerFactory()); 98 | ServerRunner.executeInstance(server); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /websocket/src/test/java/org/nanohttpd/junit/protocols/websockets/SimpleEchoSocket.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.websockets; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Websocket 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.util.ArrayList; 37 | import java.util.List; 38 | import java.util.concurrent.CountDownLatch; 39 | import java.util.concurrent.Future; 40 | import java.util.concurrent.TimeUnit; 41 | 42 | import org.eclipse.jetty.websocket.api.Session; 43 | import org.eclipse.jetty.websocket.api.StatusCode; 44 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; 45 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; 46 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; 47 | import org.eclipse.jetty.websocket.api.annotations.WebSocket; 48 | 49 | /** 50 | * Basic Echo Client Socket 51 | */ 52 | @WebSocket(maxTextMessageSize = 64 * 1024) 53 | public class SimpleEchoSocket { 54 | 55 | private final List receivedMessages = new ArrayList(); 56 | 57 | private final List toSendMessages = new ArrayList(); 58 | 59 | private final CountDownLatch closeLatch; 60 | 61 | public SimpleEchoSocket() { 62 | this.closeLatch = new CountDownLatch(1); 63 | } 64 | 65 | public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException { 66 | return this.closeLatch.await(duration, unit); 67 | } 68 | 69 | public List getReceivedMessages() { 70 | return this.receivedMessages; 71 | } 72 | 73 | public List getToSendMessages() { 74 | return this.toSendMessages; 75 | } 76 | 77 | @OnWebSocketClose 78 | public void onClose(int statusCode, String reason) { 79 | System.out.printf("Connection closed: %d - %s%n", statusCode, reason); 80 | this.closeLatch.countDown(); 81 | } 82 | 83 | @OnWebSocketConnect 84 | public void onConnect(Session session) { 85 | System.out.printf("Got connect: %s%n", session); 86 | try { 87 | Future fut; 88 | 89 | for (String message : this.toSendMessages) { 90 | fut = session.getRemote().sendStringByFuture(message); 91 | fut.get(5, TimeUnit.SECONDS); 92 | } 93 | session.close(StatusCode.NORMAL, "I'm done"); 94 | } catch (Throwable t) { 95 | t.printStackTrace(); 96 | } 97 | } 98 | 99 | @OnWebSocketMessage 100 | public void onMessage(String msg) { 101 | System.out.printf("Got msg: %s%n", msg); 102 | this.receivedMessages.add(msg); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/integration/PutStreamIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http.integration; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import static org.junit.Assert.assertEquals; 37 | 38 | import java.io.DataInputStream; 39 | import java.io.IOException; 40 | import java.util.Map; 41 | 42 | import org.apache.http.client.ResponseHandler; 43 | import org.apache.http.client.methods.HttpPut; 44 | import org.apache.http.entity.ByteArrayEntity; 45 | import org.apache.http.impl.client.BasicResponseHandler; 46 | import org.junit.Test; 47 | import org.nanohttpd.protocols.http.IHTTPSession; 48 | import org.nanohttpd.protocols.http.NanoHTTPD; 49 | import org.nanohttpd.protocols.http.request.Method; 50 | import org.nanohttpd.protocols.http.response.Response; 51 | import org.nanohttpd.protocols.http.response.Status; 52 | 53 | public class PutStreamIntegrationTest extends IntegrationTestBase { 54 | 55 | public static class TestServer extends NanoHTTPD { 56 | 57 | public TestServer() { 58 | super(8192); 59 | } 60 | 61 | @Override 62 | public Response serve(IHTTPSession session) { 63 | Method method = session.getMethod(); 64 | Map headers = session.getHeaders(); 65 | int contentLength = Integer.parseInt(headers.get("content-length")); 66 | 67 | byte[] body; 68 | try { 69 | DataInputStream dataInputStream = new DataInputStream(session.getInputStream()); 70 | body = new byte[contentLength]; 71 | dataInputStream.readFully(body, 0, contentLength); 72 | } catch (IOException e) { 73 | return Response.newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, e.getMessage()); 74 | } 75 | 76 | String response = String.valueOf(method) + ':' + new String(body); 77 | return Response.newFixedLengthResponse(response); 78 | } 79 | } 80 | 81 | @Override 82 | public TestServer createTestServer() { 83 | return new TestServer(); 84 | } 85 | 86 | @Test 87 | public void testSimplePutRequest() throws Exception { 88 | String expected = "This HttpPut request has a content-length of 48."; 89 | 90 | HttpPut httpput = new HttpPut("http://localhost:8192/"); 91 | httpput.setEntity(new ByteArrayEntity(expected.getBytes())); 92 | ResponseHandler responseHandler = new BasicResponseHandler(); 93 | String responseBody = this.httpclient.execute(httpput, responseHandler); 94 | 95 | assertEquals("PUT:" + expected, responseBody); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /core/src/test/java/org/nanohttpd/junit/protocols/http/HttpSSLServerTest.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.junit.protocols.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.File; 37 | import java.io.IOException; 38 | 39 | import org.apache.http.HttpEntity; 40 | import org.apache.http.HttpResponse; 41 | import org.apache.http.client.ClientProtocolException; 42 | import org.apache.http.client.methods.HttpTrace; 43 | import org.apache.http.impl.client.DefaultHttpClient; 44 | import org.junit.After; 45 | import org.junit.Assert; 46 | import org.junit.Before; 47 | import org.junit.Test; 48 | import org.nanohttpd.protocols.http.NanoHTTPD; 49 | 50 | public class HttpSSLServerTest extends HttpServerTest { 51 | 52 | @Test 53 | public void testSSLConnection() throws ClientProtocolException, IOException { 54 | DefaultHttpClient httpclient = new DefaultHttpClient(); 55 | HttpTrace httphead = new HttpTrace("https://localhost:9043/index.html"); 56 | HttpResponse response = httpclient.execute(httphead); 57 | HttpEntity entity = response.getEntity(); 58 | Assert.assertEquals(200, response.getStatusLine().getStatusCode()); 59 | 60 | Assert.assertEquals(9043, this.testServer.getListeningPort()); 61 | Assert.assertTrue(this.testServer.isAlive()); 62 | } 63 | 64 | /** 65 | * using http to connect to https. 66 | * 67 | * @throws ClientProtocolException 68 | * @throws IOException 69 | */ 70 | @Test(expected = ClientProtocolException.class) 71 | public void testHttpOnSSLConnection() throws ClientProtocolException, IOException { 72 | DefaultHttpClient httpclient = new DefaultHttpClient(); 73 | HttpTrace httphead = new HttpTrace("http://localhost:9043/index.html"); 74 | httpclient.execute(httphead); 75 | } 76 | 77 | @Before 78 | public void setUp() throws Exception { 79 | System.setProperty("javax.net.ssl.trustStore", new File("src/test/resources/keystore.jks").getAbsolutePath()); 80 | this.testServer = new TestServer(9043); 81 | this.testServer.makeSecure(NanoHTTPD.makeSSLSocketFactory("/keystore.jks", "password".toCharArray()), null); 82 | this.tempFileManager = new TestTempFileManager(); 83 | this.testServer.start(); 84 | try { 85 | long start = System.currentTimeMillis(); 86 | Thread.sleep(100L); 87 | while (!this.testServer.wasStarted()) { 88 | Thread.sleep(100L); 89 | if (System.currentTimeMillis() - start > 2000) { 90 | Assert.fail("could not start server"); 91 | } 92 | } 93 | } catch (InterruptedException e) { 94 | } 95 | } 96 | 97 | @After 98 | public void tearDown() { 99 | this.testServer.stop(); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/response/Status.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.response; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | /** 37 | * Some HTTP response status codes 38 | */ 39 | public enum Status implements IStatus { 40 | SWITCH_PROTOCOL(101, "Switching Protocols"), 41 | 42 | OK(200, "OK"), 43 | CREATED(201, "Created"), 44 | ACCEPTED(202, "Accepted"), 45 | NO_CONTENT(204, "No Content"), 46 | PARTIAL_CONTENT(206, "Partial Content"), 47 | MULTI_STATUS(207, "Multi-Status"), 48 | 49 | REDIRECT(301, "Moved Permanently"), 50 | /** 51 | * Many user agents mishandle 302 in ways that violate the RFC1945 spec 52 | * (i.e., redirect a POST to a GET). 303 and 307 were added in RFC2616 to 53 | * address this. You should prefer 303 and 307 unless the calling user agent 54 | * does not support 303 and 307 functionality 55 | */ 56 | @Deprecated 57 | FOUND(302, "Found"), 58 | REDIRECT_SEE_OTHER(303, "See Other"), 59 | NOT_MODIFIED(304, "Not Modified"), 60 | TEMPORARY_REDIRECT(307, "Temporary Redirect"), 61 | 62 | BAD_REQUEST(400, "Bad Request"), 63 | UNAUTHORIZED(401, "Unauthorized"), 64 | FORBIDDEN(403, "Forbidden"), 65 | NOT_FOUND(404, "Not Found"), 66 | METHOD_NOT_ALLOWED(405, "Method Not Allowed"), 67 | NOT_ACCEPTABLE(406, "Not Acceptable"), 68 | REQUEST_TIMEOUT(408, "Request Timeout"), 69 | CONFLICT(409, "Conflict"), 70 | GONE(410, "Gone"), 71 | LENGTH_REQUIRED(411, "Length Required"), 72 | PRECONDITION_FAILED(412, "Precondition Failed"), 73 | PAYLOAD_TOO_LARGE(413, "Payload Too Large"), 74 | UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"), 75 | RANGE_NOT_SATISFIABLE(416, "Requested Range Not Satisfiable"), 76 | EXPECTATION_FAILED(417, "Expectation Failed"), 77 | TOO_MANY_REQUESTS(429, "Too Many Requests"), 78 | 79 | INTERNAL_ERROR(500, "Internal Server Error"), 80 | NOT_IMPLEMENTED(501, "Not Implemented"), 81 | SERVICE_UNAVAILABLE(503, "Service Unavailable"), 82 | UNSUPPORTED_HTTP_VERSION(505, "HTTP Version Not Supported"); 83 | 84 | private final int requestStatus; 85 | 86 | private final String description; 87 | 88 | Status(int requestStatus, String description) { 89 | this.requestStatus = requestStatus; 90 | this.description = description; 91 | } 92 | 93 | public static Status lookup(int requestStatus) { 94 | for (Status status : Status.values()) { 95 | if (status.getRequestStatus() == requestStatus) { 96 | return status; 97 | } 98 | } 99 | return null; 100 | } 101 | 102 | @Override 103 | public String getDescription() { 104 | return "" + this.requestStatus + " " + this.description; 105 | } 106 | 107 | @Override 108 | public int getRequestStatus() { 109 | return this.requestStatus; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /core/src/main/java/org/nanohttpd/protocols/http/content/ContentType.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.protocols.http.content; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.util.regex.Matcher; 37 | import java.util.regex.Pattern; 38 | 39 | public class ContentType { 40 | 41 | private static final String ASCII_ENCODING = "US-ASCII"; 42 | 43 | private static final String MULTIPART_FORM_DATA_HEADER = "multipart/form-data"; 44 | 45 | private static final String CONTENT_REGEX = "[ |\t]*([^/^ ^;^,]+/[^ ^;^,]+)"; 46 | 47 | private static final Pattern MIME_PATTERN = Pattern.compile(CONTENT_REGEX, Pattern.CASE_INSENSITIVE); 48 | 49 | private static final String CHARSET_REGEX = "[ |\t]*(charset)[ |\t]*=[ |\t]*['|\"]?([^\"^'^;^,]*)['|\"]?"; 50 | 51 | private static final Pattern CHARSET_PATTERN = Pattern.compile(CHARSET_REGEX, Pattern.CASE_INSENSITIVE); 52 | 53 | private static final String BOUNDARY_REGEX = "[ |\t]*(boundary)[ |\t]*=[ |\t]*['|\"]?([^\"^'^;^,]*)['|\"]?"; 54 | 55 | private static final Pattern BOUNDARY_PATTERN = Pattern.compile(BOUNDARY_REGEX, Pattern.CASE_INSENSITIVE); 56 | 57 | private final String contentTypeHeader; 58 | 59 | private final String contentType; 60 | 61 | private final String encoding; 62 | 63 | private final String boundary; 64 | 65 | public ContentType(String contentTypeHeader) { 66 | this.contentTypeHeader = contentTypeHeader; 67 | if (contentTypeHeader != null) { 68 | contentType = getDetailFromContentHeader(contentTypeHeader, MIME_PATTERN, "", 1); 69 | encoding = getDetailFromContentHeader(contentTypeHeader, CHARSET_PATTERN, null, 2); 70 | } else { 71 | contentType = ""; 72 | encoding = "UTF-8"; 73 | } 74 | if (MULTIPART_FORM_DATA_HEADER.equalsIgnoreCase(contentType)) { 75 | boundary = getDetailFromContentHeader(contentTypeHeader, BOUNDARY_PATTERN, null, 2); 76 | } else { 77 | boundary = null; 78 | } 79 | } 80 | 81 | private String getDetailFromContentHeader(String contentTypeHeader, Pattern pattern, String defaultValue, int group) { 82 | Matcher matcher = pattern.matcher(contentTypeHeader); 83 | return matcher.find() ? matcher.group(group) : defaultValue; 84 | } 85 | 86 | public String getContentTypeHeader() { 87 | return contentTypeHeader; 88 | } 89 | 90 | public String getContentType() { 91 | return contentType; 92 | } 93 | 94 | public String getEncoding() { 95 | return encoding == null ? ASCII_ENCODING : encoding; 96 | } 97 | 98 | public String getBoundary() { 99 | return boundary; 100 | } 101 | 102 | public boolean isMultipart() { 103 | return MULTIPART_FORM_DATA_HEADER.equalsIgnoreCase(contentType); 104 | } 105 | 106 | public ContentType tryUTF8() { 107 | if (encoding == null) { 108 | return new ContentType(this.contentTypeHeader + "; charset=UTF-8"); 109 | } 110 | return this; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /samples/src/main/java/org/nanohttpd/samples/http/DebugServer.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd.samples.http; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Samples 6 | * %% 7 | * Copyright (C) 2012 - 2015 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.util.HashMap; 37 | import java.util.List; 38 | import java.util.Map; 39 | 40 | import org.nanohttpd.protocols.http.IHTTPSession; 41 | import org.nanohttpd.protocols.http.NanoHTTPD; 42 | import org.nanohttpd.protocols.http.response.Response; 43 | import org.nanohttpd.util.ServerRunner; 44 | 45 | public class DebugServer extends NanoHTTPD { 46 | 47 | public static void main(String[] args) { 48 | ServerRunner.run(DebugServer.class); 49 | } 50 | 51 | public DebugServer() { 52 | super(8080); 53 | } 54 | 55 | private void listItem(StringBuilder sb, Map.Entry entry) { 56 | sb.append("
  • ").append(entry.getKey()).append(" = ").append(entry.getValue()).append("
  • "); 57 | } 58 | 59 | @Override 60 | public Response serve(IHTTPSession session) { 61 | Map> decodedQueryParameters = decodeParameters(session.getQueryParameterString()); 62 | 63 | StringBuilder sb = new StringBuilder(); 64 | sb.append(""); 65 | sb.append("Debug Server"); 66 | sb.append(""); 67 | sb.append("

    Debug Server

    "); 68 | 69 | sb.append("

    URI = ").append(String.valueOf(session.getUri())).append("
    "); 70 | 71 | sb.append("Method = ").append(String.valueOf(session.getMethod())).append("

    "); 72 | 73 | sb.append("

    Headers

    ").append(toString(session.getHeaders())).append("

    "); 74 | 75 | sb.append("

    Parms

    ").append(toString(session.getParms())).append("

    "); 76 | 77 | sb.append("

    Parms (multi values?)

    ").append(toString(decodedQueryParameters)).append("

    "); 78 | 79 | try { 80 | Map files = new HashMap(); 81 | session.parseBody(files); 82 | sb.append("

    Files

    ").append(toString(files)).append("

    "); 83 | } catch (Exception e) { 84 | e.printStackTrace(); 85 | } 86 | 87 | sb.append(""); 88 | sb.append(""); 89 | return Response.newFixedLengthResponse(sb.toString()); 90 | } 91 | 92 | private String toString(Map map) { 93 | if (map.size() == 0) { 94 | return ""; 95 | } 96 | return unsortedList(map); 97 | } 98 | 99 | private String unsortedList(Map map) { 100 | StringBuilder sb = new StringBuilder(); 101 | sb.append("
      "); 102 | for (Map.Entry entry : map.entrySet()) { 103 | listItem(sb, entry); 104 | } 105 | sb.append("
    "); 106 | return sb.toString(); 107 | } 108 | } 109 | --------------------------------------------------------------------------------