Giter VIP home page Giter VIP logo

jscgi's Introduction

jSCGI

This project is an implementation of the SCGI protocol.

Description

It uses Java ServerSocket to handle incoming requests and the java CachedThreadPool executor to handle them. The main entry point is the JSCGIServer class. It requires an integer as the listen port and a SCGIRequestHandler interface, which should be able to handle all expected SCGI requests.

The SCGIRequestHandler is a functional interface. It accepts an SCGIMessage instance and an OutputStream, into which the created response should be written.

The SCGIMessage is used to pass requests around. It is responsible for parsing the SCGI message from an InputStream, but it is also capable of serializing the object to an OutputStream. It holds the implementation for the SCGI protocol.

There is also an SCGIClient available which should be used to send requests to the SCGIServer. It offers several variants, how to handle the response data. It accepts either an OutputStream or can return a byte array or an SCGIMessage instance. Returning the SCGIMessage instance isn't in line with the SCGI protocol, which should return the HTTP response directly. However, using the SCGIMessage for bidirectional communication it offers a better application logic separation thus be available for custom protocol implementations as well.

Running the server

The server can be created by passing the listen port and an SCGIRequestHandler implementation, where the request is an SCGIMessage instance and the response is an OutputStream.

new SCGIServer(port, (req, res) -> {
  if (req.isBodyAvailable()) {
    String name = new String(req.getBody());
    try {
      res.write(("Hello, " + name).getBytes());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
});

Running the client

The client is as simple as creating an SCGIClient instance and sending the SCGIMessage parameter to it.

SCGIClient client = new SCGIClient("localhost", port);
String response = new String(client.sendAndReceiveAsByteArray(new SCGIMessage(new HashMap<>(), "World!".getBytes()))); // Should return "Hello, World!"

jscgi's People

Contributors

szabogabriel avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.