简体   繁体   English

开源Java HTTP编解码器

[英]Open Source Java HTTP Codecs

I am writing a web server from the scratch. 我正在从头开始编写Web服务器。 There I need a Http codec which can decode a string request (buffer) to an Http object and encode http object into Sting (buffer). 我需要一个Http编解码器,它可以将字符串请求(缓冲区)解码到Http对象,并将http对象编码为Sting(缓冲区)。

I found three Codecs, 我发现了三个编解码器

  1. Apache Codecs (can't use this because this is tightly coupled with their server coding structure) Apache编解码器(不能使用它,因为它与其服务器编码结构紧密结合在一起)
  2. Netty Codes (can't use this because this is tightly coupled with their server coding structure) Netty代码(不能使用此代码,因为这与它们的服务器代码结构紧密结合在一起)
  3. JDrupes Codecs (Has some concurrency issues) JDrupes编解码器(存在一些并发问题)

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 也许这是一个可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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