Code Monkey home page Code Monkey logo

simplehttpserver's Introduction

icon

SimpleHttpServer

A simple and efficient HTTP server for Java

⚠️ simplehttpserver5 is not compatible with any previous version of simplehttpserver.

Simplified httpserver experience for Java 8. Includes extensible servers and handlers for complex operations.

Installation

This library requires at least Java 8. No additional dependencies are required.

Compiled binaries can be installed from:

Refer to the documentation to learn how to use servers and handlers.

Features

Complicated tasks made easy

Simplified exchange methods for:

  • Parsing GET/POST requests, including multipart/form-data support.
  • Accessing cookies.
  • Sending byte arrays, strings, and files to clients.
  • Sending gzip compressed responses.
SimpleHttpHandler handler = new SimpleHttpHandler(){
    @Override
    public void handle(SimpleHttpExchange exchange){
        Map POST = exchange.getPostMap();
        MultipartFormData form = exchange.getMultipartFormData();
        Record record = form.getRecord("record");
        FileRecord file = form.getRecord("file").asFile();
        exchange.send(new File("OK.png"), true);
    }
};

More Features

Features not included with a regular HTTP server:

  • Cookies
  • Sessions
  • Multithreaded Servers
SimpleHttpServer server = new SimpleHttpServer(8080);
server.setHttpSessionHandler(new HttpSessionHandler());
SimpleHttpHandler handler = new SimpleHttpHandler(){
    @Override
    public void handle(SimpleHttpExchange exchange){
        HttpSession session = server.getHttpSession(exchange);
        String session_id = session.getSessionID();
        Map<String,String> cookies = exchange.getCookies();
        exchange.close();
    }
};

Simplified Handlers

Simple and extensible request handlers:

  • Redirect Handler
  • Predicate Handler
  • Root / Handler
  • File Handler
  • Server-Sent-Events (SSE) Handler
  • Temporary Handler
  • Timeout Handler
  • Throttled Handler
RedirectHandler redirect = new RedirectHandler("https://github.com/");
FileHandler fileHandler = new FileHandler();
fileHandler.addFile(new File("index.html"));
fileHandler.addDirectory(new File("/site"));
SSEHandler SSE = new SSEHandler();
SSE.push("Server sent events!");
ThrottledHandler throttled = new ThrottledHandler(new ServerExchangeThrottler(), new HttpHandler());

Contributing

Running Tests Locally

For local tests you can use Java 8+, however only methods in the Java 8 API may be used. The src/main/java9 folder should not be marked as a source root.

Running Tests using GitHub Actions

Each commit automatically triggers the Java CI workflow, make sure you have actions enabled on your forks.

 

This library is released under the GNU General Public License (GPL) v2.0.

simplehttpserver's People

Contributors

dependabot[bot] avatar imgbot[bot] avatar katsute avatar kdevbot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

simplehttpserver's Issues

Timeout handler

Feature

Add handler that times out after set time (make sure correct error code)

Reason

Useful in limiting requests.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.