All Implemented Interfaces:
Serializable, Servlet, ServletConfig
Direct Known Subclasses:
I2PSnarkServlet

class BasicServletextends HttpServlet
Based on DefaultServlet from Jetty 6.1.26, heavily simplified and modified to remove all dependencies on Jetty libs.

Supports HEAD and GET only, for resources from the .war and local files. Supports files and resource only. Supports MIME types with local overrides and additions. Supports Last-Modified. Supports single request ranges.

Does not support directories or "welcome files". Does not support gzip. Does not support multiple request ranges. Does not cache.

POST returns 405. Directories return 403. Jar resources are sent with a long cache directive.

------------------------------------------------------------

The default servlet. This servlet, normally mapped to /, provides the handling for static content, OPTION and TRACE methods for the context. The following initParameters are supported, these can be set on the servlet itself:


resourceBase  Set to replace the context resource base
 warBase      Path allowed for resource in war

  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      The serialization version of the servlet.
      See Also:
    • _context

      protected final transient I2PAppContext _context
      The global application context, transient so the servlet stays serializable.
    • _log

      protected final transient Log _log
      Log for this servlet's class, transient so the servlet stays serializable.
    • _resourceBase

      protected volatile File _resourceBase
      Base directory for serving static files. Effectively immutable after startup; volatile so request threads never lock to read it.
    • _warBase

      private String _warBase
      Filesystem path of the unpacked war content.
    • _mimeTypes

      private final transient MimeTypes _mimeTypes
      Resolver of the served files' mime types.
    • BUFSIZE

      private static final int BUFSIZE
      Same as PeerState.PARTSIZE.
      See Also:
    • _cache

      private transient ByteCache _cache
      Cache supplying the file transfer buffers.
    • WAR_CACHE_CONTROL_SECS

      private static final int WAR_CACHE_CONTROL_SECS
      Browser cache time of war resources, in seconds.
      See Also:
    • FILE_CACHE_CONTROL_SECS

      private static final int FILE_CACHE_CONTROL_SECS
      Browser cache time of the torrent files, in seconds.
      See Also:
  • Constructor Details

    • BasicServlet

      public BasicServlet()
      Bind to the global context and build a fresh MIME table.
  • Method Details

    • init

      public void init(ServletConfig cfg) throws ServletException
      Initializes the servlet with resource and war base directories.
      Specified by:
      init in interface Servlet
      Overrides:
      init in class GenericServlet
      Parameters:
      cfg - the servlet configuration
      Throws:
      ServletException - if initialization fails
    • setResourceBase

      protected void setResourceBase(File base) throws UnavailableException
      Files are served from here.
      Parameters:
      base - the directory to serve files from
      Throws:
      UnavailableException - if the base directory doesn't exist
    • setWarBase

      protected void setWarBase(String base)
      Only paths starting with this in the path are served.
      Parameters:
      base - the base path that must be matched
    • getResource

      public File getResource(String pathInContext)
      Resource to serve, mapping a path to a resource. The default implementation calls HttpContext.getResource but derived servlets may provide their own mapping.
      Parameters:
      pathInContext - The path to find a resource for.
      Returns:
      The resource to serve or null if not existing
    • getContent

      public BasicServlet.HttpContent getContent(String pathInContext, long limit)
      Resource to serve, mapping a path to a resource. The default implementation calls HttpContext.getResource but derived servlets may provide their own mapping.
      Parameters:
      pathInContext - The path to find a resource for.
      limit - the maximum content length to serve, or 0 for unlimited
      Returns:
      The resource to serve or null. Returns null for directories
    • doGet

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      Overrides:
      doGet in class HttpServlet
      Throws:
      ServletException
      IOException
    • doPost

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      Handles HTTP POST requests. Returns 405 Method Not Allowed.
      Overrides:
      doPost in class HttpServlet
      Parameters:
      request - the HTTP request
      response - the HTTP response
      Throws:
      ServletException - if a servlet error occurs
      IOException - if an I/O error occurs
    • doTrace

      protected void doTrace(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      Handles HTTP TRACE requests. Returns 405 Method Not Allowed.
      Overrides:
      doTrace in class HttpServlet
      Parameters:
      request - the HTTP request
      response - the HTTP response
      Throws:
      ServletException - if a servlet error occurs
      IOException - if an I/O error occurs
    • doOptions

      protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      Handles HTTP OPTIONS requests. Returns 405 Method Not Allowed.
      Overrides:
      doOptions in class HttpServlet
      Parameters:
      request - the HTTP request
      response - the HTTP response
      Throws:
      ServletException - if a servlet error occurs
      IOException - if an I/O error occurs
    • doDelete

      protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      Handles HTTP DELETE requests. Returns 405 Method Not Allowed.
      Overrides:
      doDelete in class HttpServlet
      Parameters:
      request - the HTTP request
      response - the HTTP response
      Throws:
      ServletException - if a servlet error occurs
      IOException - if an I/O error occurs
    • passConditionalHeaders

      protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content) throws IOException
      Check modification date headers.
      Parameters:
      request - the HTTP request
      response - the HTTP response
      content - the body to be served, queried for type, length and modification time
      Returns:
      true to keep going, false if handled here
      Throws:
      IOException - if an I/O error occurs
    • sendData

      protected void sendData(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content) throws IOException
      Sends the content to the client, handling range requests.
      Parameters:
      request - the HTTP request
      response - the HTTP response
      content - the body to be served, whose stream the caller must not reuse
      Throws:
      IOException - if an I/O error occurs
    • writeHeaders

      protected void writeHeaders(HttpServletResponse response, BasicServlet.HttpContent content, long count) throws IOException
      Write response headers for the content.
      Parameters:
      response - the HTTP response
      content - the body being served, consulted for type and cache lifetime
      count - the content length
      Throws:
      IOException - if an I/O error occurs
    • loadMimeMap

      protected void loadMimeMap(String resourcePath)
      Load MIME type mappings from a resource file.
      Parameters:
      resourcePath - the resource path in the classpath, without ".properties" extension
    • getMimeType

      protected String getMimeType(String filename)
      The MIME type by filename extension.
      Parameters:
      filename - A file name
      Returns:
      MIME type matching the longest dot extension of the file name.
    • addMimeMapping

      protected void addMimeMapping(String extension, String type)
      Add a MIME type mapping for an extension.
      Parameters:
      extension - the file extension (without the dot)
      type - the MIME type to associate with the extension
    • addPaths

      protected static String addPaths(String base, String path)
      Simple version of URIUtil.addPaths().
      Parameters:
      base - the base path
      path - the path to add (may be null)
      Returns:
      the combined path
    • decodePath

      protected static String decodePath(String path) throws MalformedURLException
      Simple version of URIUtil.decodePath().
      Parameters:
      path - the path to decode
      Returns:
      the decoded path
      Throws:
      MalformedURLException - if the path contains invalid escape sequences
    • encodePath

      protected static String encodePath(String path)
      Simple version of URIUtil.encodePath(String).
      Parameters:
      path - the path to encode
      Returns:
      the encoded path
    • copy

      private void copy(InputStream in, OutputStream out) throws IOException
      Write from in to out.
      Parameters:
      in - the input stream to read from
      out - the output stream to write to
      Throws:
      IOException - if an I/O error occurs
    • copy

      private void copy(InputStream in, long skip, OutputStream out, long len) throws IOException
      Copy bytes from input stream to output stream.
      Parameters:
      in - the input stream to read from
      skip - the number of bytes to skip before copying
      out - the output stream to write to
      len - the number of bytes to copy, or -1 for all remaining bytes
      Throws:
      IOException - if an I/O error occurs