简体   繁体   中英

Open Source Java HTTP Codecs

I am writing a web server from the scratch. There I need a Http codec which can decode a string request (buffer) to an Http object and encode http object into Sting (buffer).

I found three Codecs,

  1. Apache Codecs (can't use this because this is tightly coupled with their server coding structure)
  2. Netty Codes (can't use this because this is tightly coupled with their server coding structure)
  3. JDrupes Codecs (Has some concurrency issues)

But non of these can be used for my purpose. Are there any other Codecs I can use?

class SimpleHttpsServer implements Runnable {
    Thread process = new Thread(this);
    private static int port = 3030;
    private String returnMessage;
    private ServerSocket ssocket;

    /************************************************************************************/
    SimpleHttpsServer() {
        try {
            ssocket = new ServerSocket(port);
            System.out.println("port " + port + " Opend");
            process.start();
        } catch (IOException e) {
            System.out.println("port " + port + " not opened due to  " + e);
            System.exit(1);
        }
    }

    /**********************************************************************************/
    public void run() {
        if (ssocket == null)
            return;
        while (true) {
            Socket csocket = null;
            try {
                csocket = ssocket.accept();
                System.out.println("New Connection accepted");
            } catch (IOException e) {
                System.out.println("Accept failed: " + port + ", " + e);
                System.exit(1);
            }
            try {
                DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(csocket.getInputStream()));
                PrintStream printStream = new PrintStream(new BufferedOutputStream(csocket.getOutputStream(), 1024),
                        false);
                this.returnMessage = "";
                InputStream inputStream = csocket.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                // code to read and print headers
                String headerLine = null;
                while ((headerLine = bufferedReader.readLine()).length() != 0) {
                    System.out.println(headerLine);
                }
                // code to read the post payload data
                StringBuilder payload = new StringBuilder();
                while (bufferedReader.ready()) {
                    payload.append((char) bufferedReader.read());
                }

                System.out.println("payload.toString().length() " + payload.toString().length());
                if (payload.toString().length() != 1 || payload.toString().length() != 0) {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(payload.toString());

                       // Handle here your string data and make responce 
                       // returnMessage this can store your responce message



                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + this.returnMessage;
                    printStream.write(httpResponse.getBytes("UTF-8"));
                    printStream.flush();

                }else {
                    /*String httpResponse = "HTTP/1.1 200 OK\r\n\r\n";
                    outStream.write(httpResponse.getBytes("UTF-8"));
                    outStream.flush();*/
                }

                printStream.close();
                dataInputStream.close();
                // csocket.close();
                System.out.println("client disconnected");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /************************************************************************************/
    public static void main(String[] args) {
        new SimpleHttpsServer();
    }
}

may be this one is help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM