多部分的HTTP响应从Web服务器下载而不是解析。

huangapple go评论61阅读模式
英文:

Multipart HTTP response from a web server is being downloaded instead of parsed

问题

我正在尝试在Rust中创建一个简单的Web服务器,但遇到了一些问题。

当Web服务器响应网站页面的HTTP请求时,服务器会获取关联的HTML、JS和CSS文件,并将它们编译成一个多部分的HTTP响应。然而,与其说响应由浏览器(在我这里是Firefox)解析并显示HTML和CSS并执行JS,不如说它将多部分HTTP响应作为文件(没有扩展名)下载。

这是发送给客户端的响应(下载的文件不包括前三行):

HTTP/1.1 200 OK
Content-Type: multipart/mixed; boundary=boundary

--boundary
Content-Disposition: inline; filename="index.html"
Content-Type: text/html
Content-Length: 268

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Hello</title>

    <script src="index.js"></script>
    <link rel="stylesheet" href="index.css">
  </head>
  <body>
    <h1>Hi, this is a website!</h1>
  </body>
</html>
--boundary
Content-Type: text/javascript
Content-Disposition: inline; filename="index.js"
Content-Length: 49

function alert(){  
    alert("Hello!");  
}
--boundary
Content-Type: text/css
Content-Disposition: inline; filename="index.css"
Content-Length: 54

body {
    background-color: rgb(212, 193, 145);
  }
--boundary--

我尝试过更改Content-Disposition标头,特别是内联,以及回车符和换行符,希望能够匹配正确的格式,但没有任何效果。

如果您有任何建议或知道我在格式方面出了什么问题,请告诉我。

英文:

I'm trying to create a simple web server in rust, but I'm running into some trouble.

When the web server is responding to an HTTP request for a website page, the server gets the associated HTML, JS, and CSS files and compiles them into a multipart HTTP response. Yet, instead of the response being parsed by the browser (in my case Firefox) and displaying the HTML and CSS and executing the JS, it downloads the multipart HTTP response as a file (no extension).

This is response to the client (the file downloaded excludes the top 3 lines):

HTTP/1.1 200 OK
Content-Type: multipart/mixed; boundary=boundary

--boundary
Content-Disposition: inline; filename="index.html"
Content-Type: text/html
Content-Length: 268

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Hello</title>

    <script src="index.js"></script>
    <link rel="stylesheet" href="index.css">
  </head>
  <body>
    <h1>Hi, this is a website!</h1>
  </body>
</html>
--boundary
Content-Type: text/javascript
Content-Disposition: inline; filename="index.js"
Content-Length: 49

function alert(){  
    alert("Hello!");  
}
--boundary
Content-Type: text/css
Content-Disposition: inline; filename="index.css"
Content-Length: 54

body {
    background-color: rgb(212, 193, 145);
  }
--boundary--

I have tried altering the Content-Dispostion headers, specifically inline, as well the carriage returns and line feeds in an attempt to hopefully match the proper formatting, but nothing has worked.

If you have any suggestions or know where I'm going wrong with the formatting, please let me know

答案1

得分: 1

Browsers do not support parsing and displaying multipart HTTP responses.

It isn't working, not because you are getting the format wrong, but because the format isn't supported at all.

You could get similar behavior by implementing HTTP 2.0 with server push.

英文:

Browsers do not support parsing and displaying multipart HTTP responses.

It isn't working, not because you are getting the format wrong, but because the format isn't supported at all.

You could get similar behaviour by implementing HTTP 2.0 with server push.

huangapple
  • 本文由 发表于 2023年7月3日 05:02:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定