Skip to content

Commit

Permalink
get v3.0 build test clean #357
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchieGitHub committed Sep 9, 2016
1 parent 0563f1f commit f5c2ec1
Show file tree
Hide file tree
Showing 45 changed files with 1,330 additions and 114 deletions.
48 changes: 41 additions & 7 deletions core/src/main/java/org/nanohttpd/protocols/http/ClientHandler.java
@@ -1,5 +1,38 @@
package org.nanohttpd.protocols.http;

/*
* #%L
* NanoHttpd-Core
* %%
* Copyright (C) 2012 - 2016 nanohttpd
* %%
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the nanohttpd nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* 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.
* #L%
*/

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
Expand All @@ -13,6 +46,7 @@
* The runnable that will be used for every new client connection.
*/
public class ClientHandler implements Runnable {

private final NanoHTTPD httpd;

private final InputStream inputStream;
Expand All @@ -21,13 +55,13 @@ public class ClientHandler implements Runnable {

public ClientHandler(NanoHTTPD httpd, InputStream inputStream, Socket acceptSocket) {
this.httpd = httpd;
this.inputStream = inputStream;
this.inputStream = inputStream;
this.acceptSocket = acceptSocket;
}

public void close() {
NanoHTTPD.safeClose(this.inputStream);
NanoHTTPD.safeClose(this.acceptSocket);
NanoHTTPD.safeClose(this.inputStream);
NanoHTTPD.safeClose(this.acceptSocket);
}

@Override
Expand All @@ -52,10 +86,10 @@ public void run() {
NanoHTTPD.LOG.log(Level.SEVERE, "Communication with the client broken, or an bug in the handler code", e);
}
} finally {
NanoHTTPD.safeClose(outputStream);
NanoHTTPD.safeClose(this.inputStream);
NanoHTTPD.safeClose(this.acceptSocket);
NanoHTTPD.safeClose(outputStream);
NanoHTTPD.safeClose(this.inputStream);
NanoHTTPD.safeClose(this.acceptSocket);
httpd.asyncRunner.closed(this);
}
}
}
}
77 changes: 54 additions & 23 deletions core/src/main/java/org/nanohttpd/protocols/http/HTTPSession.java
@@ -1,5 +1,38 @@
package org.nanohttpd.protocols.http;

/*
* #%L
* NanoHttpd-Core
* %%
* Copyright (C) 2012 - 2016 nanohttpd
* %%
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the nanohttpd nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* 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.
* #L%
*/

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -49,7 +82,7 @@ public class HTTPSession implements IHTTPSession {
public static final int MAX_HEADER_SIZE = 1024;

private final NanoHTTPD httpd;

private final ITempFileManager tempFileManager;

private final OutputStream outputStream;
Expand Down Expand Up @@ -80,14 +113,14 @@ public class HTTPSession implements IHTTPSession {

public HTTPSession(NanoHTTPD httpd, ITempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) {
this.httpd = httpd;
this.tempFileManager = tempFileManager;
this.tempFileManager = tempFileManager;
this.inputStream = new BufferedInputStream(inputStream, HTTPSession.BUFSIZE);
this.outputStream = outputStream;
}

public HTTPSession(NanoHTTPD httpd, ITempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) {
this.httpd = httpd;
this.tempFileManager = tempFileManager;
this.tempFileManager = tempFileManager;
this.inputStream = new BufferedInputStream(inputStream, HTTPSession.BUFSIZE);
this.outputStream = outputStream;
this.remoteIp = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "127.0.0.1" : inetAddress.getHostAddress().toString();
Expand Down Expand Up @@ -269,8 +302,7 @@ private int scipOverNewLine(byte[] partHeaderBuff, int index) {

/**
* Decodes parameters in percent-encoded URI-format ( e.g.
* "name=Jack%20Daniels&pass=Single%20Malt" ) and adds them to given
* Map.
* "name=Jack%20Daniels&pass=Single%20Malt" ) and adds them to given Map.
*/
private void decodeParms(String parms, Map<String, List<String>> p) {
if (parms == null) {
Expand Down Expand Up @@ -324,14 +356,14 @@ public void execute() throws IOException {
} catch (SSLException e) {
throw e;
} catch (IOException e) {
NanoHTTPD.safeClose(this.inputStream);
NanoHTTPD.safeClose(this.outputStream);
NanoHTTPD.safeClose(this.inputStream);
NanoHTTPD.safeClose(this.outputStream);
throw new SocketException("NanoHttpd Shutdown");
}
if (read == -1) {
// socket was been closed
NanoHTTPD.safeClose(this.inputStream);
NanoHTTPD.safeClose(this.outputStream);
NanoHTTPD.safeClose(this.inputStream);
NanoHTTPD.safeClose(this.outputStream);
throw new SocketException("NanoHttpd Shutdown");
}
while (read > 0) {
Expand Down Expand Up @@ -422,14 +454,14 @@ public void execute() throws IOException {
resp.send(this.outputStream);
NanoHTTPD.safeClose(this.outputStream);
} finally {
NanoHTTPD.safeClose(r);
NanoHTTPD.safeClose(r);
this.tempFileManager.clear();
}
}

/**
* Find byte index separating header from body. It must be the last byte
* of the first two sequential new lines.
* Find byte index separating header from body. It must be the last byte of
* the first two sequential new lines.
*/
private int findHeaderEnd(final byte[] buf, int rlen) {
int splitbyte = 0;
Expand All @@ -450,9 +482,9 @@ private int findHeaderEnd(final byte[] buf, int rlen) {
}

/**
* Find the byte positions where multipart boundaries start. This reads
* a large block at a time and uses a temporary buffer to optimize
* (memory mapped) file access.
* Find the byte positions where multipart boundaries start. This reads a
* large block at a time and uses a temporary buffer to optimize (memory
* mapped) file access.
*/
private int[] getBoundaryPositions(ByteBuffer b, byte[] boundary) {
int[] res = new int[0];
Expand Down Expand Up @@ -554,8 +586,8 @@ public final String getUri() {
}

/**
* Deduce body length in bytes. Either from "content-length" header or
* read bytes.
* Deduce body length in bytes. Either from "content-length" header or read
* bytes.
*/
public long getBodySize() {
if (this.headers.containsKey("content-length")) {
Expand Down Expand Up @@ -608,8 +640,7 @@ public void parseBody(Map<String, String> files) throws IOException, ResponseExc
if (contentType.isMultipart()) {
String boundary = contentType.getBoundary();
if (boundary == null) {
throw new ResponseException(Status.BAD_REQUEST,
"BAD REQUEST: Content type is multipart/form-data but boundary missing. Usage: GET /example/file.html");
throw new ResponseException(Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but boundary missing. Usage: GET /example/file.html");
}
decodeMultipartFormData(contentType, fbuf, this.parms, files);
} else {
Expand All @@ -630,13 +661,13 @@ public void parseBody(Map<String, String> files) throws IOException, ResponseExc
files.put("content", saveTmpFile(fbuf, 0, fbuf.limit(), null));
}
} finally {
NanoHTTPD.safeClose(randomAccessFile);
NanoHTTPD.safeClose(randomAccessFile);
}
}

/**
* Retrieves the content of a sent file and saves it to a temporary
* file. The full path to the saved file is returned.
* Retrieves the content of a sent file and saves it to a temporary file.
* The full path to the saved file is returned.
*/
private String saveTmpFile(ByteBuffer b, int offset, int len, String filename_hint) {
String path = "";
Expand All @@ -653,7 +684,7 @@ private String saveTmpFile(ByteBuffer b, int offset, int len, String filename_hi
} catch (Exception e) { // Catch exception if any
throw new Error(e); // we won't recover, so throw an error
} finally {
NanoHTTPD.safeClose(fileOutputStream);
NanoHTTPD.safeClose(fileOutputStream);
}
}
return path;
Expand Down
42 changes: 37 additions & 5 deletions core/src/main/java/org/nanohttpd/protocols/http/IHTTPSession.java
@@ -1,5 +1,38 @@
package org.nanohttpd.protocols.http;

/*
* #%L
* NanoHttpd-Core
* %%
* Copyright (C) 2012 - 2016 nanohttpd
* %%
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the nanohttpd nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* 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.
* #L%
*/

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
Expand All @@ -10,8 +43,7 @@
import org.nanohttpd.protocols.http.request.Method;

/**
* Handles one session, i.e. parses the HTTP request and returns the
* response.
* Handles one session, i.e. parses the HTTP request and returns the response.
*/
public interface IHTTPSession {

Expand All @@ -26,9 +58,9 @@ public interface IHTTPSession {
Method getMethod();

/**
* This method will only return the first value for a given parameter.
* You will want to use getParameters if you expect multiple values for
* a given key.
* This method will only return the first value for a given parameter. You
* will want to use getParameters if you expect multiple values for a given
* key.
*
* @deprecated use {@link #getParameters()} instead.
*/
Expand Down
43 changes: 37 additions & 6 deletions core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java
@@ -1,5 +1,38 @@
package org.nanohttpd.protocols.http;

/*
* #%L
* NanoHttpd-Core
* %%
* Copyright (C) 2012 - 2016 nanohttpd
* %%
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the nanohttpd nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* 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.
* #L%
*/

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -90,6 +123,7 @@
* licence)
*/
public abstract class NanoHTTPD {

public static final String CONTENT_DISPOSITION_REGEX = "([ |\t]*Content-Disposition[ |\t]*:)(.*)";

public static final Pattern CONTENT_DISPOSITION_PATTERN = Pattern.compile(CONTENT_DISPOSITION_REGEX, Pattern.CASE_INSENSITIVE);
Expand All @@ -102,9 +136,6 @@ public abstract class NanoHTTPD {

public static final Pattern CONTENT_DISPOSITION_ATTRIBUTE_PATTERN = Pattern.compile(CONTENT_DISPOSITION_ATTRIBUTE_REGEX);




public static final class ResponseException extends Exception {

private static final long serialVersionUID = 6569838532917408380L;
Expand Down Expand Up @@ -292,10 +323,10 @@ public static final void safeClose(Object closeable) {
private volatile ServerSocket myServerSocket;

public ServerSocket getMyServerSocket() {
return myServerSocket;
}
return myServerSocket;
}

private IFactoryThrowing<ServerSocket, IOException> serverSocketFactory = new DefaultServerSocketFactory();
private IFactoryThrowing<ServerSocket, IOException> serverSocketFactory = new DefaultServerSocketFactory();

private Thread myThread;

Expand Down

0 comments on commit f5c2ec1

Please sign in to comment.