Incorporating a small http server into my C++ program using the dlib library (http://dlib.net/network.html#server_http), my goal was to create a web interface by reading and returning an input HTML file upon request.
class web_server : public server_http
{
const std::string on_request (
const incoming_things& incoming,
outgoing_things& outgoing
)
{
ostringstream sout;
sout<<get_file_contents("Seite.html");
return sout.str();
}
};
The functionality is successful, but when viewed in a browser, only the plain HTML site is displayed without any JavaScript/CSS elements. These are embedded within the HTML file as follows:
<script type="text/javascript" src="scripte.js")>
</script>
<link rel="stylesheet" type="text/css" href="Style.css">
If I open the HTML directly in the browser, it looks fine. Thank you in advance.
Edit: With guidance from Davis King, I managed to get the JavaScript functioning, although the CSS is still not displaying correctly. I modified the response method to now send any requested file as a string: body of on_request:
ostringstream sout;
sout<<get_file_contents("Seite.html");
cout << "request path: " << incoming.path << endl;
string filename=incoming.path.substr(1,incoming.path.length());
if (incoming.path.substr(0,1) == "/" && incoming.path.length()>1) return get_file_contents(filename.c_str());
return sout.str();
Edit again: The issue has been resolved. Chrome indicated that the MIME type of the stylesheet file was text/html, when it should have been text/css. Once I adjusted my response method accordingly, everything worked as intended:
if (incoming.path=="/Style.css") outgoing.headers["Content-Type"] == "text/css";
In follow up, why do the CSS and JS files prompt a request, while the images referenced in the HTML do not? The images appear to be working despite the layout issues. Thanks anyways for the assistance, even though I am unable to upvote at the moment...