Search code examples
htmlc++httpserver

why HTML page retured by server is not rendering in the browser


I have created a simple HTTP server, (currently it supports only GET ) which returns an HTML page whenever a GET request is made , but the returned HTML page doesnt render in the browser, I verified that my server is indeed working using CURL and CURL is able to fetch the data . The code can be found below

GITHUB_LINK

Below is the output from the curl , you can see that the server is returning the HTML page.

$ curl -G "192.168.1.188:8080" --verbose
* Rebuilt URL to: 192.168.1.188:8080/
* Hostname was NOT found in DNS cache
*   Trying 192.168.1.188...
* Connected to 192.168.1.188 (192.168.1.188) port 8080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 192.168.1.188:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
<  Content-type: text/html
<  
< <!DOCTYPE html> 
<  <html>
<  <head> 
<  <title>simple server</title> 
<  </head> 
<   <body>
<   <h1>Welcome</h1>
<  <p>Welcome to the server .</p>
<   </body>
<  </html>
* Connection #0 to host 192.168.1.188 left intact

Solution

  • Before sending the HTML page you need to send some headers to the browser. Sending just the content of the file is not enough. Check the structure of a response on the documentation https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages#HTTP_Responses

    Try to access a page from working web-server, look for the headers structure in the browser (using the browser's development tools) and replicate that with your code.