176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanpackage fi.iki.elonen; 276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.io.*; 476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.net.InetAddress; 54dc8a2aec63e4fb5ee2688544c4de323ed5de3efWichert Akkermanimport java.net.InetSocketAddress; 676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.net.ServerSocket; 776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.net.Socket; 876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.net.SocketException; 976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.net.SocketTimeoutException; 1076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.net.URLDecoder; 1176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.nio.ByteBuffer; 1276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.nio.channels.FileChannel; 1376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.text.SimpleDateFormat; 1476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.ArrayList; 1576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.Calendar; 1676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.Date; 1776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.HashMap; 1876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.HashSet; 1976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.Iterator; 2076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.List; 2176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.Locale; 2276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.Map; 2376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.Set; 2476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.StringTokenizer; 2576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanimport java.util.TimeZone; 2676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 2776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman/** 2876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * A simple, tiny, nicely embeddable HTTP server in Java 2976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 3076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 3176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * NanoHTTPD 3276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p></p>Copyright (c) 2012-2013 by Paul S. Hawke, 2001,2005-2013 by Jarno Elonen, 2010 by Konstantinos Togias</p> 332ee6e45f36566e8735b35ffad40bfcc626a25a98Wichert Akkerman * <p/> 3476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 3576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <b>Features + limitations: </b> 3676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <ul> 3776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 3876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>Only one Java file</li> 3976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>Java 5 compatible</li> 4076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>Released as open source, Modified BSD licence</li> 4176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>No fixed config files, logging, authorization etc. (Implement yourself if you need them.)</li> 4276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>Supports parameter parsing of GET and POST methods (+ rudimentary PUT support in 1.25)</li> 4376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>Supports both dynamic content and file serving</li> 4476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>Supports file upload (since version 1.2, 2010)</li> 4576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>Supports partial content (streaming)</li> 4619e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes * <li>Supports ETags</li> 4776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>Never caches anything</li> 487b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman * <li>Doesn't limit bandwidth, request time or simultaneous connections</li> 497b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman * <li>Default code serves files and shows all HTTP parameters and headers</li> 507b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman * <li>File server supports directory listing, index.html and index.htm</li> 517b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman * <li>File server supports partial content (streaming)</li> 52bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * <li>File server supports ETags</li> 53bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * <li>File server does the 301 redirection trick for directories without '/'</li> 54bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * <li>File server supports simple skipping for files (continue download)</li> 55bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * <li>File server serves also very long files without memory overhead</li> 5676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>Contains a built-in list of most common mime types</li> 5776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>All header names are converted lowercase so they don't vary between browsers/clients</li> 58ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman * <p/> 591d08dcf46dd4b2cfe3e27723c05b0aec9955c591John Hughes * </ul> 609ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman * <p/> 619ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman * <p/> 6276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <b>How to use: </b> 631d08dcf46dd4b2cfe3e27723c05b0aec9955c591John Hughes * <ul> 6476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 6576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <li>Subclass and implement serve() and embed to your own program</li> 6676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 6776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * </ul> 6876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 6976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * See the separate "LICENSE.md" file for the distribution license (Modified BSD licence) 7076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 7176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanpublic abstract class NanoHTTPD { 7276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 7376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Maximum time to wait on Socket.getInputStream().read() (in milliseconds) 7476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * This is required as the Keep-Alive HTTP connections would otherwise 7576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * block the socket reading thread forever (or as long the browser is open). 7676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 7776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public static final int SOCKET_READ_TIMEOUT = 5000; 7876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 7976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Common mime type for dynamic content: plain text 8076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 8176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public static final String MIME_PLAINTEXT = "text/plain"; 8276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 8376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Common mime type for dynamic content: html 8476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 8576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public static final String MIME_HTML = "text/html"; 8676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 8776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Pseudo-Parameter to use to store the actual query string in the parameters map for later re-processing. 8876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 8976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private static final String QUERY_STRING_PARAMETER = "NanoHttpd.QUERY_STRING"; 9076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private final String hostname; 9176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private final int myPort; 9276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private ServerSocket myServerSocket; 9376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private Set<Socket> openConnections = new HashSet<Socket>(); 9476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private Thread myThread; 9576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 9676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Pluggable strategy for asynchronously executing requests. 9776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 9876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private AsyncRunner asyncRunner; 9976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 100bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * Pluggable strategy for creating and cleaning up temporary files. 10176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 10276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private TempFileManagerFactory tempFileManagerFactory; 10376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 10476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 105e68d61c7d6dcf986d32c462e533ea46f85183db8Wichert Akkerman * Constructs an HTTP server on given port. 10676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 10776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public NanoHTTPD(int port) { 10876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this(null, port); 10976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 11076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 11176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 11276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Constructs an HTTP server on given hostname and port. 11376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 11476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public NanoHTTPD(String hostname, int port) { 11576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.hostname = hostname; 11676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.myPort = port; 11776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman setTempFileManagerFactory(new DefaultTempFileManagerFactory()); 11876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman setAsyncRunner(new DefaultAsyncRunner()); 11976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 12076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 12176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private static final void safeClose(Closeable closeable) { 12276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (closeable != null) { 123ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman try { 1249ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman closeable.close(); 1259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } catch (IOException e) { 1269ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 1279ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 128bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 12976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 13076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private static final void safeClose(Socket closeable) { 13176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (closeable != null) { 13276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 13376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman closeable.close(); 13476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (IOException e) { 13576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 13676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 13776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 13876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 13976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private static final void safeClose(ServerSocket closeable) { 14076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (closeable != null) { 14176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 14276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman closeable.close(); 14376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (IOException e) { 14476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 14576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 14676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 14776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 14876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 14976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Start the server. 15076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 15176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @throws IOException if the socket is in use. 15276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 15376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void start() throws IOException { 15476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman myServerSocket = new ServerSocket(); 15576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman myServerSocket.bind((hostname != null) ? new InetSocketAddress(hostname, myPort) : new InetSocketAddress(myPort)); 15676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 15776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman myThread = new Thread(new Runnable() { 15876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 15976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void run() { 16076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman do { 16176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 16276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman final Socket finalAccept = myServerSocket.accept(); 16376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman registerConnection(finalAccept); 16476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman finalAccept.setSoTimeout(SOCKET_READ_TIMEOUT); 16576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman final InputStream inputStream = finalAccept.getInputStream(); 16676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman asyncRunner.exec(new Runnable() { 16776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 16876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void run() { 16976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman OutputStream outputStream = null; 17076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 17176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman outputStream = finalAccept.getOutputStream(); 17276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman TempFileManager tempFileManager = tempFileManagerFactory.create(); 17376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman HTTPSession session = new HTTPSession(tempFileManager, inputStream, outputStream, finalAccept.getInetAddress()); 17476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (!finalAccept.isClosed()) { 17576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman session.execute(); 17676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 17776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (Exception e) { 17876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // When the socket is closed by the client, we throw our own SocketException 17976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // to break the "keep alive" loop above. 18076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!(e instanceof SocketException && "NanoHttpd Shutdown".equals(e.getMessage()))) { 18176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman e.printStackTrace(); 18276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 18376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } finally { 18476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(outputStream); 18576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(inputStream); 18676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(finalAccept); 18776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman unRegisterConnection(finalAccept); 18876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 18976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 19076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman }); 19176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (IOException e) { 19276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 19376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } while (!myServerSocket.isClosed()); 19476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 19576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman }); 19676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman myThread.setDaemon(true); 19776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman myThread.setName("NanoHttpd Main Listener"); 19876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman myThread.start(); 19976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 20076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 20176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 20276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Stop the server. 20376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 20476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void stop() { 20576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 20676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(myServerSocket); 20776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman closeAllConnections(); 20876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (myThread != null) { 20976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman myThread.join(); 21076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 21176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (Exception e) { 21276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman e.printStackTrace(); 21376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 21476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 21576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 21676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 21776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Registers that a new connection has been set up. 21876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 21976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param socket the {@link Socket} for the connection. 22076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 22176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public synchronized void registerConnection(Socket socket) { 22276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman openConnections.add(socket); 22376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 22476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 22576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 22676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Registers that a connection has been closed 22776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 22876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param socket 22976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * the {@link Socket} for the connection. 23076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 23176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public synchronized void unRegisterConnection(Socket socket) { 23276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman openConnections.remove(socket); 23376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 23476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 23576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 23676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Forcibly closes all connections that are open. 23776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 23876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public synchronized void closeAllConnections() { 23976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (Socket socket : openConnections) { 24076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(socket); 24176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 24276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 24376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 24476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public final int getListeningPort() { 24576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return myServerSocket == null ? -1 : myServerSocket.getLocalPort(); 24676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 24776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 24876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public final boolean wasStarted() { 24976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return myServerSocket != null && myThread != null; 25076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 25176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 25276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public final boolean isAlive() { 25376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return wasStarted() && !myServerSocket.isClosed() && myThread.isAlive(); 25476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 25576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 25654a4767f8609abfe2d7cb1802bc9e8dca97dd08fWichert Akkerman /** 25776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Override this to customize the server. 25876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 25976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 26076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * (By default, this delegates to serveFile() and allows directory listing.) 26176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 26276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param uri Percent-decoded URI without parameters, for example "/index.cgi" 26376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param method "GET", "POST" etc. 26476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param parms Parsed, percent decoded parameters from URI and, in case of POST, data. 26576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param headers Header entries, percent decoded 26676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @return HTTP response, see class Response for details 26776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 26876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Deprecated 26976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> parms, 27076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Map<String, String> files) { 27176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found"); 27276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 27376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 27476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 27576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Override this to customize the server. 27676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 27776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 27876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * (By default, this delegates to serveFile() and allows directory listing.) 27976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 28076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param session The HTTP session 28176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @return HTTP response, see class Response for details 28276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 28376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public Response serve(IHTTPSession session) { 28476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Map<String, String> files = new HashMap<String, String>(); 28576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Method method = session.getMethod(); 28676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (Method.PUT.equals(method) || Method.POST.equals(method)) { 28776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 28876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman session.parseBody(files); 28976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (IOException ioe) { 29076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); 29176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (ResponseException re) { 29276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); 29376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 29476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 29576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 29676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Map<String, String> parms = session.getParms(); 29776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman parms.put(QUERY_STRING_PARAMETER, session.getQueryParameterString()); 29876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return serve(session.getUri(), method, session.getHeaders(), parms, files); 29976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 30076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 30176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 30276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Decode percent encoded <code>String</code> values. 30376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 30476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param str the percent encoded <code>String</code> 30576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @return expanded form of the input, for example "foo%20bar" becomes "foo bar" 30676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 30776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman protected String decodePercent(String str) { 30876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String decoded = null; 30976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 31076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman decoded = URLDecoder.decode(str, "UTF8"); 31176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (UnsupportedEncodingException ignored) { 31276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 31376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return decoded; 31476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 31576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 31676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 31776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Decode parameters from a URL, handing the case where a single parameter name might have been 31876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * supplied several times, by return lists of values. In general these lists will contain a single 31976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * element. 32076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 32176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param parms original <b>NanoHttpd</b> parameters values, as passed to the <code>serve()</code> method. 32254b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman * @return a map of <code>String</code> (parameter name) to <code>List<String></code> (a list of the values supplied). 32354b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman */ 32476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman protected Map<String, List<String>> decodeParameters(Map<String, String> parms) { 32576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return this.decodeParameters(parms.get(QUERY_STRING_PARAMETER)); 32676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 32776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 32876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 32954b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman * Decode parameters from a URL, handing the case where a single parameter name might have been 33054b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman * supplied several times, by return lists of values. In general these lists will contain a single 33154b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman * element. 33254b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman * 33354b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman * @param queryString a query string pulled from the URL. 33454b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman * @return a map of <code>String</code> (parameter name) to <code>List<String></code> (a list of the values supplied). 33554b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman */ 33654b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman protected Map<String, List<String>> decodeParameters(String queryString) { 33754b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman Map<String, List<String>> parms = new HashMap<String, List<String>>(); 33854b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman if (queryString != null) { 33976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman StringTokenizer st = new StringTokenizer(queryString, "&"); 34076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (st.hasMoreTokens()) { 34176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String e = st.nextToken(); 34276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int sep = e.indexOf('='); 34376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String propertyName = (sep >= 0) ? decodePercent(e.substring(0, sep)).trim() : decodePercent(e).trim(); 34476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!parms.containsKey(propertyName)) { 34576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman parms.put(propertyName, new ArrayList<String>()); 34676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 34776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String propertyValue = (sep >= 0) ? decodePercent(e.substring(sep + 1)) : null; 34876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (propertyValue != null) { 34976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman parms.get(propertyName).add(propertyValue); 35076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 35176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 35276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 35376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return parms; 35476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 35576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 35676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // ------------------------------------------------------------------------------- // 35776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // 35876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Threading Strategy. 359bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman // 36076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // ------------------------------------------------------------------------------- // 36176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 36276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 36376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Pluggable strategy for asynchronously executing requests. 36476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 365bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * @param asyncRunner new strategy for handling threads. 36676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 36776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void setAsyncRunner(AsyncRunner asyncRunner) { 36876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.asyncRunner = asyncRunner; 36976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 37076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 371bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman // ------------------------------------------------------------------------------- // 37276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // 37376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Temp file handling strategy. 37476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // 37576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // ------------------------------------------------------------------------------- // 37676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 37776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 37876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Pluggable strategy for creating and cleaning up temporary files. 37976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 38076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param tempFileManagerFactory new strategy for handling temp files. 38176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 38276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void setTempFileManagerFactory(TempFileManagerFactory tempFileManagerFactory) { 38376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.tempFileManagerFactory = tempFileManagerFactory; 38476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 38576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 38676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 38776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * HTTP Request methods, with the ability to decode a <code>String</code> back to its enum value. 38876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 38976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public enum Method { 39076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman GET, PUT, POST, DELETE, HEAD, OPTIONS; 39176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 39276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman static Method lookup(String method) { 39376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (Method m : Method.values()) { 39476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (m.toString().equalsIgnoreCase(method)) { 39576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return m; 39676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 39776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 39876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return null; 39976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 40076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 40176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 40276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 40376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Pluggable strategy for asynchronously executing requests. 40476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 40576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public interface AsyncRunner { 40676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman void exec(Runnable code); 40776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 40876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 40976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 41076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Factory to create temp file managers. 41176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 41276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public interface TempFileManagerFactory { 41376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman TempFileManager create(); 41476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 41576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 41676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // ------------------------------------------------------------------------------- // 41776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 41876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 41976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Temp file manager. 42076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 42176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p>Temp file managers are created 1-to-1 with incoming requests, to create and cleanup 42276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * temporary files created as a result of handling the request.</p> 42376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 42476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public interface TempFileManager { 42576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman TempFile createTempFile() throws Exception; 42676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 42776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman void clear(); 42876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 42976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 43076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 43176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * A temp file. 43276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 433bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * <p>Temp files are responsible for managing the actual temporary storage and cleaning 434bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * themselves up when no longer needed.</p> 43576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 43676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public interface TempFile { 43776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman OutputStream open() throws Exception; 43876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 43976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman void delete() throws Exception; 44076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 44176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String getName(); 442bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 44376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 444bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman /** 445bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * Default threading strategy for NanoHttpd. 446bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * <p/> 447bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * <p>By default, the server spawns a new Thread for every incoming request. These are set 448bd4125c6bcd20e8f014b682b31d6fc5d0fef3ed0Wichert Akkerman * to <i>daemon</i> status, and named according to the request number. The name is 4497987cdf192632516d6ba493b0a9943f5a3a7c362Wichert Akkerman * useful when profiling the application.</p> 450bd4125c6bcd20e8f014b682b31d6fc5d0fef3ed0Wichert Akkerman */ 45176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public static class DefaultAsyncRunner implements AsyncRunner { 45276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private long requestCount; 45376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 45476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 45576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void exec(Runnable code) { 45676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ++requestCount; 45776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Thread t = new Thread(code); 45876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman t.setDaemon(true); 45976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman t.setName("NanoHttpd Request Processor (#" + requestCount + ")"); 46076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman t.start(); 46176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 46276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 46376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 46476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 46576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Default strategy for creating and cleaning up temporary files. 46676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 46776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p></p>This class stores its files in the standard location (that is, 46876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * wherever <code>java.io.tmpdir</code> points to). Files are added 46976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * to an internal list, and deleted when no longer needed (that is, 47076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * when <code>clear()</code> is invoked at the end of processing a 4715ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman * request).</p> 4725ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman */ 4735ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman public static class DefaultTempFileManager implements TempFileManager { 4745ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman private final String tmpdir; 4755ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman private final List<TempFile> tempFiles; 4765ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman 4775ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman public DefaultTempFileManager() { 4785ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman tmpdir = System.getProperty("java.io.tmpdir"); 4795ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman tempFiles = new ArrayList<TempFile>(); 4805ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman } 4815ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman 4825ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman @Override 4835ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman public TempFile createTempFile() throws Exception { 48476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman DefaultTempFile tempFile = new DefaultTempFile(tmpdir); 48576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tempFiles.add(tempFile); 48676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return tempFile; 48776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 488bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 48976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 49076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void clear() { 49176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (TempFile file : tempFiles) { 49276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 49376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman file.delete(); 49476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (Exception ignored) { 49576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 49676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 49776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tempFiles.clear(); 49876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 49976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 50076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 501bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman /** 50276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Default strategy for creating and cleaning up temporary files. 50376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p/> 50476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * <p></p></[>By default, files are created by <code>File.createTempFile()</code> in 50576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * the directory specified.</p> 50676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 507bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman public static class DefaultTempFile implements TempFile { 508bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private File file; 50976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private OutputStream fstream; 510bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 51176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public DefaultTempFile(String tempdir) throws IOException { 51276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman file = File.createTempFile("NanoHTTPD-", "", new File(tempdir)); 51376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fstream = new FileOutputStream(file); 51476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 51576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 51676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 51776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public OutputStream open() throws Exception { 51876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return fstream; 51976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 52076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 52176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 52276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void delete() throws Exception { 52376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(fstream); 52476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman file.delete(); 52576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 52676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 52776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 52876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public String getName() { 52976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return file.getAbsolutePath(); 53076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 53176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 53276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 53376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 53476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * HTTP response. Return one of these from serve(). 53576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 53676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public static class Response { 53776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 53876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * HTTP status code after processing, e.g. "200 OK", HTTP_OK 53976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 54076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private IStatus status; 541bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman /** 54276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * MIME type of content, e.g. "text/html" 54376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 544bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private String mimeType; 54576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 54676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Data of the response, may be null. 54776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 54876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private InputStream data; 54976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 55076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Headers for the HTTP response. Use addHeader() to add lines. 55176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 55276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private Map<String, String> header = new HashMap<String, String>(); 55376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 55476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * The request method that spawned this response. 55576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 55676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private Method requestMethod; 55776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 55876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Use chunkedTransfer 55976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 56076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private boolean chunkedTransfer; 56176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 56276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 56376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Default constructor: response = HTTP_OK, mime = MIME_HTML and your supplied message 56476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 56576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public Response(String msg) { 56676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this(Status.OK, MIME_HTML, msg); 56776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 56876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 56976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 57076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Basic constructor. 57176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 57276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public Response(IStatus status, String mimeType, InputStream data) { 57376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.status = status; 57476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.mimeType = mimeType; 57576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.data = data; 57676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 57776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 57876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 57976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Convenience method that makes an InputStream out of given text. 58076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 58176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public Response(IStatus status, String mimeType, String txt) { 58276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.status = status; 58376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.mimeType = mimeType; 58476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 58576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.data = txt != null ? new ByteArrayInputStream(txt.getBytes("UTF-8")) : null; 58676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (java.io.UnsupportedEncodingException uee) { 58776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman uee.printStackTrace(); 58876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 58976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 59076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 59176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 59276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Adds given line to the header. 59376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 59476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void addHeader(String name, String value) { 59576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman header.put(name, value); 59676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 59776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 59876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public String getHeader(String name) { 59976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return header.get(name); 60076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 601bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 60276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 60376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Sends given response to the socket. 60476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 60576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman protected void send(OutputStream outputStream) { 60676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String mime = mimeType; 60776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman SimpleDateFormat gmtFrmt = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss 'GMT'", Locale.US); 60876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman gmtFrmt.setTimeZone(TimeZone.getTimeZone("GMT")); 609bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 61019e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes try { 61119e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes if (status == null) { 61276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new Error("sendResponse(): Status can't be null."); 61376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 614bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman PrintWriter pw = new PrintWriter(outputStream); 61576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pw.print("HTTP/1.1 " + status.getDescription() + " \r\n"); 61676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 61776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (mime != null) { 61876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pw.print("Content-Type: " + mime + "\r\n"); 619ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman } 6209ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 6219ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (header == null || header.get("Date") == null) { 6229ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman pw.print("Date: " + gmtFrmt.format(new Date()) + "\r\n"); 6239ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6249ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 6259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (header != null) { 6269ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman for (String key : header.keySet()) { 6279ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman String value = header.get(key); 6289ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman pw.print(key + ": " + value + "\r\n"); 6299ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6309ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6319ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 6329ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman sendConnectionHeaderIfNotAlreadyPresent(pw, header); 6339ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 6349ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (requestMethod != Method.HEAD && chunkedTransfer) { 6359ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman sendAsChunked(outputStream, pw); 6369ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } else { 6379ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman int pending = data != null ? data.available() : 0; 6389ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman sendContentLengthHeaderIfNotAlreadyPresent(pw, header, pending); 6399ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman pw.print("\r\n"); 6409ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman pw.flush(); 6419ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman sendAsFixedLength(outputStream, pending); 6429ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6439ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman outputStream.flush(); 6449ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman safeClose(data); 6459ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } catch (IOException ioe) { 6469ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman // Couldn't write? No can do. 6479ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6489ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6499ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 6509ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman protected void sendContentLengthHeaderIfNotAlreadyPresent(PrintWriter pw, Map<String, String> header, int size) { 6519ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (!headerAlreadySent(header, "content-length")) { 6529ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman pw.print("Content-Length: "+ size +"\r\n"); 6539ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6549ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6559ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 6569ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman protected void sendConnectionHeaderIfNotAlreadyPresent(PrintWriter pw, Map<String, String> header) { 6579ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (!headerAlreadySent(header, "connection")) { 6589ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman pw.print("Connection: keep-alive\r\n"); 6599ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6609ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 66176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 662bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private boolean headerAlreadySent(Map<String, String> header, String name) { 66376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman boolean alreadySent = false; 66476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (String headerName : header.keySet()) { 665bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman alreadySent |= headerName.equalsIgnoreCase(name); 666bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 667bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman return alreadySent; 668bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 66976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 67076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private void sendAsChunked(OutputStream outputStream, PrintWriter pw) throws IOException { 67176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pw.print("Transfer-Encoding: chunked\r\n"); 6729ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman pw.print("\r\n"); 6739ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman pw.flush(); 6749ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman int BUFFER_SIZE = 16 * 1024; 6759ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman byte[] CRLF = "\r\n".getBytes(); 6769ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman byte[] buff = new byte[BUFFER_SIZE]; 6779ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman int read; 6789ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman while ((read = data.read(buff)) > 0) { 6799ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman outputStream.write(String.format("%x\r\n", read).getBytes()); 6809ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman outputStream.write(buff, 0, read); 681bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman outputStream.write(CRLF); 682bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 683bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman outputStream.write(String.format("0\r\n\r\n").getBytes()); 684bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 685bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 686bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private void sendAsFixedLength(OutputStream outputStream, int pending) throws IOException { 687bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (requestMethod != Method.HEAD && data != null) { 688bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman int BUFFER_SIZE = 16 * 1024; 689bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman byte[] buff = new byte[BUFFER_SIZE]; 690bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman while (pending > 0) { 691bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman int read = data.read(buff, 0, ((pending > BUFFER_SIZE) ? BUFFER_SIZE : pending)); 692bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (read <= 0) { 693bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman break; 694bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 695bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman outputStream.write(buff, 0, read); 69676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pending -= read; 69776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 69876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 69976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 70076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 70176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public IStatus getStatus() { 70276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return status; 7039ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 70476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 70576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void setStatus(Status status) { 70676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.status = status; 7079ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 708bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 70976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public String getMimeType() { 71076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return mimeType; 711bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 71276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 71316a03d2e97415afe6cf34172a0aea97a95a0b160Wichert Akkerman public void setMimeType(String mimeType) { 71476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.mimeType = mimeType; 71576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 71676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 717bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman public InputStream getData() { 71876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return data; 71976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 72076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 7219ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman public void setData(InputStream data) { 72276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.data = data; 72376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 72476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 72576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public Method getRequestMethod() { 72676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return requestMethod; 7279ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 72876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 72976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void setRequestMethod(Method requestMethod) { 73076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.requestMethod = requestMethod; 73176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 732bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 73376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void setChunkedTransfer(boolean chunkedTransfer) { 73476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.chunkedTransfer = chunkedTransfer; 73576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 73676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 73776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public interface IStatus { 73876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int getRequestStatus(); 73976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String getDescription(); 74076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 741bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 742bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman /** 743bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * Some HTTP response status codes 744bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman */ 745bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman public enum Status implements IStatus { 746bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman SWITCH_PROTOCOL(101, "Switching Protocols"), 747bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 748bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman OK(200, "OK"), 749bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman CREATED(201, "Created"), 750bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman ACCEPTED(202, "Accepted"), 751bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman NO_CONTENT(204, "No Content"), 752bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman PARTIAL_CONTENT(206, "Partial Content"), 75376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 754bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman REDIRECT(301, "Moved Permanently"), 75519e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes TEMPORARY_REDIRECT(302, "Moved Temporarily"), 75619e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes NOT_MODIFIED(304, "Not Modified"), 75719e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes 75819e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes BAD_REQUEST(400, "Bad Request"), 75919e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes UNAUTHORIZED(401, "Unauthorized"), 76019e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes FORBIDDEN(403, "Forbidden"), 76119e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes NOT_FOUND(404, "Not Found"), 76219e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes METHOD_NOT_ALLOWED(405, "Method Not Allowed"), 76319e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes RANGE_NOT_SATISFIABLE(416, "Requested Range Not Satisfiable"), 76419e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes 76519e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes INTERNAL_ERROR(500, "Internal Server Error"); 76619e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes 76719e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes private final int requestStatus; 76819e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes private final String description; 76919e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes 77019e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes Status(int requestStatus, String description) { 77119e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes this.requestStatus = requestStatus; 77219e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes this.description = description; 77319e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes } 77419e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes 77519e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes @Override 77619e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes public int getRequestStatus() { 77719e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes return this.requestStatus; 77876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 77976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 78076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 78119e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes public String getDescription() { 78219e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes return "" + this.requestStatus + " " + description; 78376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 78476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 78576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 78619e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes 78719e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes public static final class ResponseException extends Exception { 78819e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes 78919e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes private final Response.Status status; 79019e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes 79119e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes public ResponseException(Response.Status status, String message) { 7929ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman super(message); 79376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman this.status = status; 79476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 79576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 79619e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes public ResponseException(Response.Status status, String message, Exception e) { 79719e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes super(message, e); 79819e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes this.status = status; 79919e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes } 80019e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes 80119e49984acc8f12dfaf0b7835ad17ca24f854c47John Hughes public Response.Status getStatus() { 8029ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return status; 80376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 80476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 80576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 806bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman /** 807bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman * Default strategy for creating and cleaning up temporary files. 808bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman */ 809bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private class DefaultTempFileManagerFactory implements TempFileManagerFactory { 810bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman @Override 811bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman public TempFileManager create() { 812bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman return new DefaultTempFileManager(); 813bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 81476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 81576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 81676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 81776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Handles one session, i.e. parses the HTTP request and returns the response. 81876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 81976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public interface IHTTPSession { 82076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman void execute() throws IOException; 82176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 822bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman Map<String, String> getParms(); 82376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 8249ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman Map<String, String> getHeaders(); 8259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 82676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 82776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @return the path part of the URL. 82876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 829bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman String getUri(); 830bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 831bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman String getQueryParameterString(); 832bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 833bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman Method getMethod(); 834bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 83576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman InputStream getInputStream(); 83676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 8379ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman CookieHandler getCookies(); 83876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 83976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 84076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Adds the files in the request body to the files map. 8419ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman * @arg files - map to modify 842bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman */ 843bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman void parseBody(Map<String, String> files) throws IOException, ResponseException; 844bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 84576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 84676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman protected class HTTPSession implements IHTTPSession { 84776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public static final int BUFSIZE = 8192; 848bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private final TempFileManager tempFileManager; 8499ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman private final OutputStream outputStream; 8509ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman private PushbackInputStream inputStream; 851bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private int splitbyte; 852bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private int rlen; 853bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private String uri; 85476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private Method method; 85576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private Map<String, String> parms; 85676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private Map<String, String> headers; 857bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private CookieHandler cookies; 858bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman private String queryParameterString; 859bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 860bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman public HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) { 861bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman this.tempFileManager = tempFileManager; 862bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman this.inputStream = new PushbackInputStream(inputStream, BUFSIZE); 863bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman this.outputStream = outputStream; 86476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 865bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 86676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { 867bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman this.tempFileManager = tempFileManager; 868bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman this.inputStream = new PushbackInputStream(inputStream, BUFSIZE); 8692e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman this.outputStream = outputStream; 8702e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman String remoteIp = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "127.0.0.1" : inetAddress.getHostAddress().toString(); 8712e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman headers = new HashMap<String, String>(); 8722e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman 8732e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman headers.put("remote-addr", remoteIp); 8742e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman headers.put("http-client-ip", remoteIp); 8752e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman } 8762e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman 8772e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman @Override 8782e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman public void execute() throws IOException { 8792e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman try { 8802e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman // Read the first 8192 bytes. 8812e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman // The full header should fit in here. 8822e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman // Apache's default header limit is 8KB. 8832e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman // Do NOT assume that a single read will get the entire header at once! 8842e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman byte[] buf = new byte[BUFSIZE]; 8852e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman splitbyte = 0; 8862e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman rlen = 0; 8872e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman { 8882e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman int read = -1; 8892e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman try { 8902e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman read = inputStream.read(buf, 0, BUFSIZE); 8912e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman } catch (Exception e) { 8922e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman safeClose(inputStream); 8932e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman safeClose(outputStream); 894bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman throw new SocketException("NanoHttpd Shutdown"); 895bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 89676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (read == -1) { 89776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // socket was been closed 89876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(inputStream); 89976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(outputStream); 90076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new SocketException("NanoHttpd Shutdown"); 90176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 90276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (read > 0) { 90376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman rlen += read; 90476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman splitbyte = findHeaderEnd(buf, rlen); 90576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (splitbyte > 0) 90676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 90776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman read = inputStream.read(buf, rlen, BUFSIZE - rlen); 90876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 909bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 91076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 91176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (splitbyte < rlen) { 91276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman inputStream.unread(buf, splitbyte, rlen - splitbyte); 91376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 91476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 91576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman parms = new HashMap<String, String>(); 91676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if(null == headers) { 91776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman headers = new HashMap<String, String>(); 91876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 91976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 92076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Create a BufferedReader for parsing the header. 92176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman BufferedReader hin = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf, 0, rlen))); 92276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 92376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Decode the header into parms and header java properties 92476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Map<String, String> pre = new HashMap<String, String>(); 92576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman decodeHeader(hin, pre, parms, headers); 92676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 927bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman method = Method.lookup(pre.get("method")); 92876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (method == null) { 92976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Syntax error."); 93076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 93176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 93276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman uri = pre.get("uri"); 93376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 93476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cookies = new CookieHandler(headers); 93576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 93676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Ok, now do the serve() 93776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Response r = serve(this); 93876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (r == null) { 93976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new ResponseException(Response.Status.INTERNAL_ERROR, "SERVER INTERNAL ERROR: Serve() returned a null response."); 94076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else { 94176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cookies.unloadQueue(r); 94276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman r.setRequestMethod(method); 94376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman r.send(outputStream); 94476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 945bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } catch (SocketException e) { 94676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // throw it out to close socket object (finalAccept) 94776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw e; 94876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (SocketTimeoutException ste) { 94976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw ste; 95076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (IOException ioe) { 95176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Response r = new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); 95276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman r.send(outputStream); 95376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(outputStream); 95476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (ResponseException re) { 95576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Response r = new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); 95676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman r.send(outputStream); 95776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(outputStream); 95876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } finally { 959bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman tempFileManager.clear(); 960bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 961bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 962bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 963bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman @Override 964bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman public void parseBody(Map<String, String> files) throws IOException, ResponseException { 965bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman RandomAccessFile randomAccessFile = null; 966bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman BufferedReader in = null; 967bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman try { 968bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 969bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman randomAccessFile = getTmpBucket(); 97076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 97176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman long size; 97276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (headers.containsKey("content-length")) { 97376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman size = Integer.parseInt(headers.get("content-length")); 97476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else if (splitbyte < rlen) { 97576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman size = rlen - splitbyte; 97676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else { 97776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman size = 0; 97876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 97976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 98076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Now read all the body and write it to f 98176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman byte[] buf = new byte[512]; 98276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (rlen >= 0 && size > 0) { 98376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman rlen = inputStream.read(buf, 0, (int)Math.min(size, 512)); 984bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman size -= rlen; 98576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (rlen > 0) { 98676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman randomAccessFile.write(buf, 0, rlen); 98776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 98876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 98976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 99076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Get the raw body as a byte [] 99176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ByteBuffer fbuf = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length()); 99276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman randomAccessFile.seek(0); 99376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 99476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Create a BufferedReader for easily reading it as string. 99576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman InputStream bin = new FileInputStream(randomAccessFile.getFD()); 99676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman in = new BufferedReader(new InputStreamReader(bin)); 99776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 99876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // If the method is POST, there may be parameters 99976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // in data section, too, read it: 100076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (Method.POST.equals(method)) { 100176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String contentType = ""; 100276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String contentTypeHeader = headers.get("content-type"); 100376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 100476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman StringTokenizer st = null; 100576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (contentTypeHeader != null) { 100676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman st = new StringTokenizer(contentTypeHeader, ",; "); 100776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (st.hasMoreTokens()) { 100876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman contentType = st.nextToken(); 1009bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 101076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 101176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 101276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ("multipart/form-data".equalsIgnoreCase(contentType)) { 101376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Handle multipart/form-data 101476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!st.hasMoreTokens()) { 101576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but boundary missing. Usage: GET /example/file.html"); 101676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 101776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 101876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String boundaryStartString = "boundary="; 101976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int boundaryContentStart = contentTypeHeader.indexOf(boundaryStartString) + boundaryStartString.length(); 102076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String boundary = contentTypeHeader.substring(boundaryContentStart, contentTypeHeader.length()); 102176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (boundary.startsWith("\"") && boundary.endsWith("\"")) { 102276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman boundary = boundary.substring(1, boundary.length() - 1); 102376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 102476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 102576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman decodeMultipartData(boundary, fbuf, in, parms, files); 102676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else { 102776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String postLine = ""; 102876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman StringBuilder postLineBuffer = new StringBuilder(); 102976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman char pbuf[] = new char[512]; 103076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int read = in.read(pbuf); 103176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (read >= 0 && !postLine.endsWith("\r\n")) { 1032dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman postLine = String.valueOf(pbuf, 0, read); 1033dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman postLineBuffer.append(postLine); 1034dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman read = in.read(pbuf); 1035dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman } 103676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman postLine = postLineBuffer.toString().trim(); 103776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Handle application/x-www-form-urlencoded 103876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ("application/x-www-form-urlencoded".equalsIgnoreCase(contentType)) { 103976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman decodeParms(postLine, parms); 104076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else if (postLine.length() != 0) { 104176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Special case for raw POST data => create a special files entry "postData" with raw content data 104276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman files.put("postData", postLine); 104376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 104476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 104576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else if (Method.PUT.equals(method)) { 104676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman files.put("content", saveTmpFile(fbuf, 0, fbuf.limit())); 104776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 104876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } finally { 104976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(randomAccessFile); 105076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(in); 105176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 105276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 105376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 105476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 105576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Decodes the sent headers and loads the data into Key/value pairs 105676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 105776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private void decodeHeader(BufferedReader in, Map<String, String> pre, Map<String, String> parms, Map<String, String> headers) 105876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throws ResponseException { 105976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 106076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Read the request line 106176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String inLine = in.readLine(); 106276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (inLine == null) { 106376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return; 106476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 106576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 106676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman StringTokenizer st = new StringTokenizer(inLine); 106776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!st.hasMoreTokens()) { 106876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Syntax error. Usage: GET /example/file.html"); 106976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 107076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 107176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pre.put("method", st.nextToken()); 107276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 107376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!st.hasMoreTokens()) { 107476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Missing URI. Usage: GET /example/file.html"); 107576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 107676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 107776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String uri = st.nextToken(); 107876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 107976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Decode parameters from the URI 108076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int qmi = uri.indexOf('?'); 108176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (qmi >= 0) { 108276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman decodeParms(uri.substring(qmi + 1), parms); 108376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman uri = decodePercent(uri.substring(0, qmi)); 108476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else { 108576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman uri = decodePercent(uri); 108676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 108776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 108876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // If there's another token, it's protocol version, 108976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // followed by HTTP headers. Ignore version but parse headers. 109076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // NOTE: this now forces header names lowercase since they are 1091bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman // case insensitive and vary by client. 109276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (st.hasMoreTokens()) { 109376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String line = in.readLine(); 1094bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman while (line != null && line.trim().length() > 0) { 109576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int p = line.indexOf(':'); 109676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (p >= 0) 109776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman headers.put(line.substring(0, p).trim().toLowerCase(Locale.US), line.substring(p + 1).trim()); 109876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman line = in.readLine(); 109976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 110076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 110176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 110276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pre.put("uri", uri); 1103bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } catch (IOException ioe) { 110476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new ResponseException(Response.Status.INTERNAL_ERROR, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage(), ioe); 110576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 110676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 110776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 110876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 110976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Decodes the Multipart Body data and put it into Key/Value pairs. 111076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 111176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private void decodeMultipartData(String boundary, ByteBuffer fbuf, BufferedReader in, Map<String, String> parms, 111276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Map<String, String> files) throws ResponseException { 111376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 111476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int[] bpositions = getBoundaryPositions(fbuf, boundary.getBytes()); 111576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int boundarycount = 1; 111676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String mpline = in.readLine(); 111776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (mpline != null) { 111876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!mpline.contains(boundary)) { 111976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but next chunk does not start with boundary. Usage: GET /example/file.html"); 112076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 112176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman boundarycount++; 112276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Map<String, String> item = new HashMap<String, String>(); 1123bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman mpline = in.readLine(); 112476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (mpline != null && mpline.trim().length() > 0) { 112576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int p = mpline.indexOf(':'); 112676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (p != -1) { 112776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman item.put(mpline.substring(0, p).trim().toLowerCase(Locale.US), mpline.substring(p + 1).trim()); 112876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 112976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman mpline = in.readLine(); 113076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 113176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (mpline != null) { 113276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String contentDisposition = item.get("content-disposition"); 113376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (contentDisposition == null) { 113476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but no content-disposition info found. Usage: GET /example/file.html"); 113576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 113676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman StringTokenizer st = new StringTokenizer(contentDisposition, ";"); 113776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Map<String, String> disposition = new HashMap<String, String>(); 113876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (st.hasMoreTokens()) { 113976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String token = st.nextToken().trim(); 114076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int p = token.indexOf('='); 114176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (p != -1) { 114276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman disposition.put(token.substring(0, p).trim().toLowerCase(Locale.US), token.substring(p + 1).trim()); 114376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 114476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 114576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String pname = disposition.get("name"); 114676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pname = pname.substring(1, pname.length() - 1); 114776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 114876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String value = ""; 114976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (item.get("content-type") == null) { 115076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (mpline != null && !mpline.contains(boundary)) { 115176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman mpline = in.readLine(); 115276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (mpline != null) { 115376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int d = mpline.indexOf(boundary); 115476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (d == -1) { 115576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman value += mpline; 115676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else { 115776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman value += mpline.substring(0, d - 2); 115876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 115976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 116076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 116176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else { 116276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (boundarycount > bpositions.length) { 116376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new ResponseException(Response.Status.INTERNAL_ERROR, "Error processing request"); 116476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 116576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int offset = stripMultipartHeaders(fbuf, bpositions[boundarycount - 2]); 116676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String path = saveTmpFile(fbuf, offset, bpositions[boundarycount - 1] - offset - 4); 116776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman files.put(pname, path); 116876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman value = disposition.get("filename"); 116976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman value = value.substring(1, value.length() - 1); 117076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman do { 117176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman mpline = in.readLine(); 117276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } while (mpline != null && !mpline.contains(boundary)); 117376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 117476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman parms.put(pname, value); 117576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 117676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 117776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (IOException ioe) { 117876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new ResponseException(Response.Status.INTERNAL_ERROR, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage(), ioe); 117976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 118076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 118176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 118276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 118376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Find byte index separating header from body. It must be the last byte of the first two sequential new lines. 118476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 118576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private int findHeaderEnd(final byte[] buf, int rlen) { 118676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int splitbyte = 0; 118776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (splitbyte + 3 < rlen) { 118876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (buf[splitbyte] == '\r' && buf[splitbyte + 1] == '\n' && buf[splitbyte + 2] == '\r' && buf[splitbyte + 3] == '\n') { 118976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return splitbyte + 4; 119076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 119176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman splitbyte++; 119276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 119376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 119476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 119576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 119676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 119776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Find the byte positions where multipart boundaries start. 119876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 119976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private int[] getBoundaryPositions(ByteBuffer b, byte[] boundary) { 120076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int matchcount = 0; 120176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int matchbyte = -1; 120276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman List<Integer> matchbytes = new ArrayList<Integer>(); 120376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (int i = 0; i < b.limit(); i++) { 120476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (b.get(i) == boundary[matchcount]) { 120576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (matchcount == 0) 120676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman matchbyte = i; 120776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman matchcount++; 120876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (matchcount == boundary.length) { 120976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman matchbytes.add(matchbyte); 121076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman matchcount = 0; 1211bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman matchbyte = -1; 121276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 121376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else { 121476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman i -= matchcount; 121576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman matchcount = 0; 121676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman matchbyte = -1; 121776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 121876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 121976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int[] ret = new int[matchbytes.size()]; 122076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (int i = 0; i < ret.length; i++) { 122176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ret[i] = matchbytes.get(i); 122276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 12239ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return ret; 122476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 122576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 122676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 122776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Retrieves the content of a sent file and saves it to a temporary file. The full path to the saved file is returned. 122876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 122976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private String saveTmpFile(ByteBuffer b, int offset, int len) { 123076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String path = ""; 123176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (len > 0) { 123276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman FileOutputStream fileOutputStream = null; 123376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 123476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman TempFile tempFile = tempFileManager.createTempFile(); 123576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ByteBuffer src = b.duplicate(); 123676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fileOutputStream = new FileOutputStream(tempFile.getName()); 123776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman FileChannel dest = fileOutputStream.getChannel(); 123876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman src.position(offset).limit(offset + len); 123976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman dest.write(src.slice()); 124076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman path = tempFile.getName(); 124176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (Exception e) { // Catch exception if any 124276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new Error(e); // we won't recover, so throw an error 124376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } finally { 124476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman safeClose(fileOutputStream); 124576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 124676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 124776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return path; 124876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 124976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 125076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private RandomAccessFile getTmpBucket() { 125176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman try { 125276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman TempFile tempFile = tempFileManager.createTempFile(); 125376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return new RandomAccessFile(tempFile.getName(), "rw"); 125476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } catch (Exception e) { 125576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman throw new Error(e); // we won't recover, so throw an error 125676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 125776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 125876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 125976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 126076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * It returns the offset separating multipart file headers from the file's data. 126176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 126276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private int stripMultipartHeaders(ByteBuffer b, int offset) { 126376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int i; 126476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (i = offset; i < b.limit(); i++) { 126576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (b.get(i) == '\r' && b.get(++i) == '\n' && b.get(++i) == '\r' && b.get(++i) == '\n') { 126676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 126776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 126876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 126976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return i + 1; 127076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 127176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 127276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 127376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Decodes parameters in percent-encoded URI-format ( e.g. "name=Jack%20Daniels&pass=Single%20Malt" ) and 127476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * adds them to given Map. NOTE: this doesn't support multiple identical keys due to the simplicity of Map. 127576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 127676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private void decodeParms(String parms, Map<String, String> p) { 127776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (parms == null) { 127876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman queryParameterString = ""; 127976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return; 128076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 128176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 128276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman queryParameterString = parms; 128376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman StringTokenizer st = new StringTokenizer(parms, "&"); 128476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (st.hasMoreTokens()) { 128576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String e = st.nextToken(); 128676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int sep = e.indexOf('='); 128776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (sep >= 0) { 128876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman p.put(decodePercent(e.substring(0, sep)).trim(), 128976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman decodePercent(e.substring(sep + 1))); 129076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } else { 129176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman p.put(decodePercent(e).trim(), ""); 129276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 129376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 129476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 129576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 129676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 129776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public final Map<String, String> getParms() { 1298bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman return parms; 1299bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 1300bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 130176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public String getQueryParameterString() { 130276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return queryParameterString; 130376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 130476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 130576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 130676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public final Map<String, String> getHeaders() { 130776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return headers; 130876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 130976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 131076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 131176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public final String getUri() { 131276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return uri; 131376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 131476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 131576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 131676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public final Method getMethod() { 131776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return method; 131876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 131976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 132076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 132176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public final InputStream getInputStream() { 132276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return inputStream; 132376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 132476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 132576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override 132676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public CookieHandler getCookies() { 132776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return cookies; 132876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 132976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 133076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 133176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public static class Cookie { 133276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private String n, v, e; 133376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 133476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public Cookie(String name, String value, String expires) { 133576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman n = name; 133676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman v = value; 133776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman e = expires; 133876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 133976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 1340bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman public Cookie(String name, String value) { 1341bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman this(name, value, 30); 1342bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 1343bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 1344bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman public Cookie(String name, String value, int numDays) { 13459ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman n = name; 134676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman v = value; 134776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman e = getHTTPTime(numDays); 134876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 134976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 135076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public String getHTTPHeader() { 135176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String fmt = "%s=%s; expires=%s"; 135276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return String.format(fmt, n, v, e); 135376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 135476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 135576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public static String getHTTPTime(int days) { 135676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman Calendar calendar = Calendar.getInstance(); 135776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); 135876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 135976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman calendar.add(Calendar.DAY_OF_MONTH, days); 136076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return dateFormat.format(calendar.getTime()); 13619ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 136276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 136376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 136476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 136576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Provides rudimentary support for cookies. 136676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Doesn't support 'path', 'secure' nor 'httpOnly'. 136776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Feel free to improve it and/or add unsupported features. 136876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 136976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @author LordFokas 137076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 137176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public class CookieHandler implements Iterable<String> { 137276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private HashMap<String, String> cookies = new HashMap<String, String>(); 137376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman private ArrayList<Cookie> queue = new ArrayList<Cookie>(); 137476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 137576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public CookieHandler(Map<String, String> httpHeaders) { 137676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String raw = httpHeaders.get("cookie"); 137776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (raw != null) { 13789ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman String[] tokens = raw.split(";"); 137976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (String token : tokens) { 138076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman String[] data = token.trim().split("="); 138176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (data.length == 2) { 138276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cookies.put(data[0], data[1]); 138376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 138476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 138576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 138676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 138776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 138876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman @Override public Iterator<String> iterator() { 138976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return cookies.keySet().iterator(); 139076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 139176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 139276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 139376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Read a cookie from the HTTP Headers. 139476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 139576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param name The cookie's name. 139676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @return The cookie's value if it exists, null otherwise. 139776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 139876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public String read(String name) { 139976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return cookies.get(name); 140076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 14019ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 140276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 140376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Sets a cookie. 140476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 140576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param name The cookie's name. 140676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param value The cookie's value. 140776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param expires How many days until the cookie expires. 140876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 140976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void set(String name, String value, int expires) { 141076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman queue.add(new Cookie(name, value, Cookie.getHTTPTime(expires))); 141176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 141276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 14139dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman public void set(Cookie cookie) { 14149dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman queue.add(cookie); 14159dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman } 141676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 141776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 141876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Set a cookie with an expiration date from a month ago, effectively deleting it on the client side. 141976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 14209ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman * @param name The cookie name. 142176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 142276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void delete(String name) { 142376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman set(name, "-delete-", -30); 142476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 142576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 142676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /** 142776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Internally used by the webserver to add all queued cookies into the Response's HTTP Headers. 142876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 142976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * @param response The Response object to which headers the queued cookies will be added. 143076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 143176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman public void unloadQueue(Response response) { 143276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (Cookie cookie : queue) { 143376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman response.addHeader("Set-Cookie", cookie.getHTTPHeader()); 143476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 143576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 143676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 143776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 143876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman