3

I am trying to write a CGI Program which is invoked from a HTML page . The CGI code snippet is as follows:

 #!/bin/bash
 echo "Content-type: text/html"
 echo  "<html>"
 echo "<body>"
 echo "Hi"
 echo "</body>"
 echo "</html>"

But after executing the script I can see the 500:Internal Server Error in browser page. .

Following can be seen in error log file

[Wed Apr 09 18:36:59 2014] [error] [client 127.0.0.1] malformed header   
from script. Bad header=<html>: image.sh
[Wed Apr 09 18:37:14 2014] [error] [client 127.0.0.1] malformed header 
from script. Bad header=<html>: image.sh
1
  • Are you sure your script is executed ? could you show use some config of your webserver ?
    – Kiwy
    Apr 9, 2014 at 8:12

1 Answer 1

13

As the HTTP spec (and the error message from your HTTP server) specify, you need one blank line between the HTTP headers and the body, otherwise the server doesn't know where the headers end and the body begins. From RFC 2616 (emphasis my own):

Request (section 5) and Response (section 6) messages use the generic message format of RFC 822 [9] for transferring entities (the payload of the message). Both types of message consist of a start-line, zero or more header fields (also known as "headers"), an empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields, and possibly a message-body.

To fix the problem, put another echo before echo "<html>", so that the headers are clearly separated from the body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.