Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ public class HTTPSession implements IHTTPSession {

private static final int REQUEST_BUFFER_LEN = 512;

private static final int MEMORY_STORE_LIMIT = 1024;

public static final int BUFSIZE = 8192;

public static final int MAX_HEADER_SIZE = 1024;
Expand Down Expand Up @@ -611,7 +609,7 @@ public void parseBody(Map<String, String> files) throws IOException, ResponseExc
DataOutput requestDataOutput = null;

// Store the request in memory or a file, depending on size
if (size < MEMORY_STORE_LIMIT) {
if (size <= httpd.getMemoryStoreLimit()) {
baos = new ByteArrayOutputStream();
requestDataOutput = new DataOutputStream(baos);
} else {
Expand Down
47 changes: 34 additions & 13 deletions core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ public Status getStatus() {
*/
private static final String QUERY_STRING_PARAMETER = "NanoHttpd.QUERY_STRING";

/**
* Default size limit (in bytes) for bodies that are parsed in memory.
*/
private static final int DEFAULT_MEMORY_STORE_LIMIT = 1024;

/**
* logger to log to.
*/
Expand Down Expand Up @@ -226,7 +231,7 @@ private static void loadMimeTypes(Map<String, String> result, String resourceNam
} catch (IOException e) {
LOG.log(Level.INFO, "no mime types available at " + resourceName);
}
};
}

/**
* Creates an SSLSocketFactory for HTTPS. Pass a loaded KeyStore and an
Expand Down Expand Up @@ -284,7 +289,7 @@ public static SSLServerSocketFactory makeSSLSocketFactory(String keyAndTrustStor

/**
* Get MIME type from file name extension, if possible
*
*
* @param uri
* the string representing a file
* @return the connected mime/type
Expand Down Expand Up @@ -344,6 +349,8 @@ public ServerSocket getMyServerSocket() {
*/
private IFactory<ITempFileManager> tempFileManagerFactory;

private int memoryStoreLimit = DEFAULT_MEMORY_STORE_LIMIT;

/**
* Constructs an HTTP server on given port.
*/
Expand Down Expand Up @@ -396,7 +403,7 @@ public synchronized void closeAllConnections() {
/**
* create a instance of the client handler, subclasses can return a subclass
* of the ClientHandler.
*
*
* @param finalAccept
* the socket the cleint is connected to
* @param inputStream
Expand All @@ -410,7 +417,7 @@ protected ClientHandler createClientHandler(final Socket finalAccept, final Inpu
/**
* Instantiate the server runnable, can be overwritten by subclasses to
* provide a subclass of the ServerRunnable.
*
*
* @param timeout
* the socet timeout to use.
* @return the server runnable.
Expand All @@ -423,7 +430,7 @@ protected ServerRunnable createServerRunnable(final int timeout) {
* Decode parameters from a URL, handing the case where a single parameter
* name might have been supplied several times, by return lists of values.
* In general these lists will contain a single element.
*
*
* @param parms
* original <b>NanoHTTPD</b> parameters values, as passed to the
* <code>serve()</code> method.
Expand All @@ -441,7 +448,7 @@ protected static Map<String, List<String>> decodeParameters(Map<String, String>
* Decode parameters from a URL, handing the case where a single parameter
* name might have been supplied several times, by return lists of values.
* In general these lists will contain a single element.
*
*
* @param queryString
* a query string pulled from the URL.
* @return a map of <code>String</code> (parameter name) to
Expand Down Expand Up @@ -469,7 +476,7 @@ protected static Map<String, List<String>> decodeParameters(String queryString)

/**
* Decode percent encoded <code>String</code> values.
*
*
* @param str
* the percent encoded <code>String</code>
* @return expanded form of the input, for example "foo%20bar" becomes
Expand Down Expand Up @@ -509,6 +516,10 @@ public IFactory<ITempFileManager> getTempFileManagerFactory() {
return tempFileManagerFactory;
}

public int getMemoryStoreLimit() {
return memoryStoreLimit;
}

/**
* Call before start() to serve over HTTPS instead of HTTP
*/
Expand All @@ -521,7 +532,7 @@ public void makeSecure(SSLServerSocketFactory sslServerSocketFactory, String[] s
* sure there is a response to every request. You are not supposed to call
* or override this method in any circumstances. But no one will stop you if
* you do. I'm a Javadoc, not Code Police.
*
*
* @param session
* the incoming session
* @return a response to the incoming session
Expand All @@ -540,7 +551,7 @@ public Response handle(IHTTPSession session) {
* <p/>
* <p/>
* (By default, this returns a 404 "Not Found" plain text error response.)
*
*
* @param session
* The HTTP session
* @return HTTP response, see class Response for details
Expand All @@ -552,7 +563,7 @@ protected Response serve(IHTTPSession session) {

/**
* Pluggable strategy for asynchronously executing requests.
*
*
* @param asyncRunner
* new strategy for handling threads.
*/
Expand All @@ -562,17 +573,27 @@ public void setAsyncRunner(IAsyncRunner asyncRunner) {

/**
* Pluggable strategy for creating and cleaning up temporary files.
*
*
* @param tempFileManagerFactory
* new strategy for handling temp files.
*/
public void setTempFileManagerFactory(IFactory<ITempFileManager> tempFileManagerFactory) {
this.tempFileManagerFactory = tempFileManagerFactory;
}

/**
* Sets the size limit for bodies that are parsed in memory.
*
* @param memoryStoreLimit
* The size limit in bytes.
*/
public void setMemoryStoreLimit(final int memoryStoreLimit) {
this.memoryStoreLimit = memoryStoreLimit;
}

/**
* Start the server.
*
*
* @throws IOException
* if the socket is in use.
*/
Expand All @@ -589,7 +610,7 @@ public void start(final int timeout) throws IOException {

/**
* Start the server.
*
*
* @param timeout
* timeout to use for socket connections.
* @param daemon
Expand Down