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
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.