Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode

by rizzo (Curate)
on Mar 09, 2018 at 12:19 UTC ( [id://1210561]=note: print w/replies, xml ) Need Help??


in reply to CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode

"\n\n" is the correct way to end the header.
If your script fails due to an incorrect header declaration, you get an error like:"premature end of script headers".
  • Comment on Re: CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode

Replies are listed 'Best First'.
Re: CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode
by 7stud (Deacon) on Mar 09, 2018 at 13:10 UTC

    "\n\n" is the correct way to end the header.

    How do you reconcile that statement with:

    1. The HTTP 1.1 spec which requires the last header be followed by two CRLF sequences.
    2. The fact that $cgi->header; returns the string: "Content-Type: text/html; charset=ISO-8859-1" terminated by "\x0D\x0A\x0D\x0A"?
      The fact that $cgi->header; returns the string: "Content-Type: text/html; charset=ISO-8859-1" terminated by "\x0D\x0A\x0D\x0A"?

      UPDATE: In addition:
      What is the correct form of response from a CGI script? ... The CGI specification allows any of the usual newline representations +in the CGI response (it's the server's job to create an accurate HTTP + response based on it). So "\n" written in text mode is technically c +orrect, and recommended. NPH scripts are more tricky: they must put o +ut a complete and accurate set of HTTP transaction response headers; +the HTTP specification calls for records to be terminated with carria +ge-return and line-feed, i.e ASCII \015\012 written in binary mode. Using CGI.pm gives excellent platform independence, including EBCDIC s +ystems. CGI.pm selects an appropriate newline representation ($CGI::C +RLF) and sets binmode as appropriate.
      perlfaq9 - Networking

      /Update


      This article sheds some light on the topic:
      The End-of-Line Story
      Few people today are aware of the EOL issue, because systems generally (but not always!) make it transparent. For example, the RFC Editor stores the official RFC archive on a Unix system whose native EOL is a single LF. When you click on a link for an RFC from the RFC Editor We +b page, your browser uses an FTP client to retrieve the ASCII text. The RFC's FTP server atranslates the LF in each text line into CR LF for transmission across the Internet, and your FTP client in turn translates each CR LF into whatever the EOL convention of your system.

        Hi

        The great thing about CGI.pm is that it was written by guy who knew the RFC like the back of his hand and then some 25 years ago ...

        Talking rfc/newlines 25 years after CGI.pm gave the world header() ... extreme waste of time, I stopped doing that 15 years ago :) (ok I start and stop every few years)

      The HTTP 1.1 spec which requires the last header be followed by two CRLF sequences.

      Jep. From RFC 2616
      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 h +eader fields, and possibly a message-body. generic-message = start-line *(message-header CRLF) CRLF [ message-body ] start-line = Request-Line | Status-Line
      In short, the header is terminated by an empty line. Maybe I'm missing the point, but that's exactly what I get with:

      print "$lastlineofheader\n\n;"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1210561]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-24 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found