【问题标题】:Is my HTTP protocol design correctly implemented in the coding?我的 HTTP 协议设计是否在编码中正确实现?
【发布时间】:2015-05-15 11:24:28
【问题描述】:

这是我非常简单的客户端-服务器应用程序。客户端向服务器发送一些命令,服务器将输出返回给客户端。但是,我特别关心的是发送到服务器的GET 命令。客户端请求GET filename 下载命名文件。正如我设计的协议一样,该文件最终会下载到带有 HTTP 响应标头的客户端目录中。

现在我担心我的编码是否准确地遵循协议。特别是带有换行符的 HTTP 响应标头(在客户端和服务器端)。

协议设计:

客户:

syntax: GET  namedfile CRLF
CRLF
meaning: downloading the named file from the server
representation: text file

服务器:

syntax: Status: ok CRLF
Length: 20 bytes CRLF
CRLF
File contents
meaning: The file exist in the server and ready to 
download
representation: text file

代码:

服务器端:

                          .................
                          .................
else if (request.startsWith("GET")) {
                        System.out.println("");
                        String filename = request.substring(4);
                        File file = new File(System.getProperty("user.dir"));
                        File[] files = file.listFiles();

                        if (fileExists(files, filename)) {
                            file = new File(filename);
                            int fileSize = (int) file.length();
                            outputToClient.print("Status OK\r\n"
                                    + "Size " + fileSize + "KB" + "\r\n"
                                    + "\r\n"
                                    + "File " + filename + " Download was successfully\r\n");
                            outputToClient.flush();
                            // reading files
                            fis = new FileInputStream(file);
                            os = socket.getOutputStream();
                            byte[] buffer = new byte[2^7-1];
                            int bytesRead = 0;
                            while ((bytesRead = fis.read(buffer))!= -1) {
                                os.write(buffer, 0, bytesRead);
                            }
                            os.close();
                            fis.close();
                        } else {
                            outputToClient.print("Status 400\r\n"
                                    + "File " + filename + " not found\r\n"
                                    + "\r\n");
                            outputToClient.flush();
                        }
                    }
                    outputToClient.flush();
                }
                           .................
                           .................

客户端:

       ............
                 ............
                if (request.startsWith("GET")) {
                File file = new File(request.substring(4));
                is = socket.getInputStream();
                fos = new FileOutputStream(file);

                byte[] buffer = new byte[socket.getReceiveBufferSize()];
                int bytesReceived = 0;

                while ((bytesReceived = is.read(buffer)) >=0) {
                    //while ((bytesReceived = is.read(buffer))>=buffer) {
                    fos.write(buffer, 0, bytesReceived);
                }
                request = "";
                fos.close();
                is.close();
            }
               .................
               .................

【问题讨论】:

  • 重新发明轮子?更好的提问地点可能是 codereview。
  • johnnnnn,什么?
  • 问题应该问stack review
  • @Mast 所以这是重复的。好的,将标记。

标签: java http file-io http-headers client-server


【解决方案1】:

我不太清楚你在问什么,但正如 john 指出的那样 - 你为什么不直接采用像码头服务器这样的现有解决方案并根据你的喜好进行调整? 给出答案:例如,缺少 HTTP 状态代码。 直接“映射”到服务器文件系统并不是最佳实践。

【讨论】:

  • 梅奥,非常感谢。我想你明白我的意思。你能给我更多的建议吗,你能在编码中具体告诉我,缺少状态码的地方以及更好的映射方式是什么。请。我会接受你的回答,并为你在这个论坛上发布的每一个答案给予更多的支持。
  • 查看en.wikipedia.org/wiki/Hypertext_Transfer_Protocol 和“示例会话”部分。每个客户端请求至少包含文章中给出的最小示例。重要的响应头字段是状态(响应的第一行)、内容类型(您的浏览器需要知道即将到来的数据编码类型)和连接。剩下的就看你了。对于文件映射问题:您需要了解可以通过 GET /foo.html HTTP/1.1 请求文件,但服务器可能会以本地存储的文件 bar.html 进行响应。这样你就隐藏了真正的文件结构。
猜你喜欢
  • 2018-04-15
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 2011-10-19
相关资源
最近更新 更多