Apache -> malformed header from script

Discussion in 'Ruby' started by Douglas Livingstone, Jan 28, 2005.

  1. Hi all,

    I'm on windows xp trying, playing with Ruby on Apache. I've put this
    in httpd.conf:

    AddHandler cgi-script .rb

    And this in index.rb:

    #!ruby

    print "HTTP/1.0 200 OK\r\n"
    print "Content-type: text/html\r\n\r\n"
    print "<html><body>Hello World!</body></html>\r\n"

    This comes up as a 500 error, with this in the log file:

    malformed header from script. Bad header=HTTP/1.0 200 OK: index.rb

    Any idea what I'm missing?

    Thanks,
    Douglas
     
    Douglas Livingstone, Jan 28, 2005
    #1
    1. Advertising

  2. Douglas Livingstone

    Jim Menard Guest

    Douglas Livingstone wrote:
    > Hi all,
    >
    > I'm on windows xp trying, playing with Ruby on Apache. I've put this
    > in httpd.conf:
    >
    > AddHandler cgi-script .rb
    >
    > And this in index.rb:
    >
    > #!ruby
    >
    > print "HTTP/1.0 200 OK\r\n"
    > print "Content-type: text/html\r\n\r\n"
    > print "<html><body>Hello World!</body></html>\r\n"
    >
    > This comes up as a 500 error, with this in the log file:
    >
    > malformed header from script. Bad header=HTTP/1.0 200 OK: index.rb
    >
    > Any idea what I'm missing?


    Try just "\n" instead of "\r\n". Since you're on Windows, I think Ruby will
    output "\r\n" when you use "\n". You may be sending too many newlines.

    (I've also just used "\n" on Unix for this kind of thing before, and it always
    worked fine.)

    Jim
    --
    Jim Menard, , http://www.io.com/~jimm
     
    Jim Menard, Jan 28, 2005
    #2
    1. Advertising

  3. > Try just "\n" instead of "\r\n". Since you're on Windows, I think Ruby will
    > output "\r\n" when you use "\n". You may be sending too many newlines.


    No joy, but, I have solved it! The working code is:

    #!ruby

    print "Content-type: text/html\n\n"
    print "<html><body>Hello World!</body></html>\n"

    Turns out ruby or apache or something else is sending the 200 for me,
    which is nice :) (though I wonder how to change it then... hmm... back
    to google...)

    All that is needed is

    print "\n\n"

    to get output, though need the text/html for html naturally :)

    Thanks,
    Douglas
     
    Douglas Livingstone, Jan 28, 2005
    #3
  4. On Sat, 29 Jan 2005 05:33:29 +0900, Douglas Livingstone
    <> wrote:
    > > Try just "\n" instead of "\r\n". Since you're on Windows, I think Ruby will
    > > output "\r\n" when you use "\n". You may be sending too many newlines.

    >
    > No joy, but, I have solved it! The working code is:
    >
    > #!ruby
    >
    > print "Content-type: text/html\n\n"
    > print "<html><body>Hello World!</body></html>\n"
    >
    > Turns out ruby or apache or something else is sending the 200 for me,
    > which is nice :) (though I wonder how to change it then... hmm... back
    > to google...)


    puts <<-EOS
    Status: 302
    Content-type: text/html

    <html>
    <body>
    <p>Hello World!</p>
    </body>
    </html>
    EOS

    -austin
    --
    Austin Ziegler *
    * Alternate:
     
    Austin Ziegler, Jan 28, 2005
    #4
  5. Douglas Livingstone

    Aredridel Guest

    On Sat, 29 Jan 2005 05:33:29 +0900, Douglas Livingstone
    <> wrote:
    > > Try just "\n" instead of "\r\n". Since you're on Windows, I think Ruby will
    > > output "\r\n" when you use "\n". You may be sending too many newlines.

    >
    > No joy, but, I have solved it! The working code is:
    >
    > #!ruby
    >
    > print "Content-type: text/html\n\n"
    > print "<html><body>Hello World!</body></html>\n"
    >
    > Turns out ruby or apache or something else is sending the 200 for me,
    > which is nice :) (though I wonder how to change it then... hmm... back
    > to google...)
    >
    > All that is needed is
    >
    > print "\n\n"
    >
    > to get output, though need the text/html for html naturally :)
    >


    Hehe. You send Status: 200 for setting the status line; you have to
    tell Apache that it's an "NPH" CGI if you want to send the HTTP line
    yourself.

    Also, you use just \n, not \r\n, because you're speaking CGI, not
    HTTP. The CGI spec declares that \n is the line ending for headers,
    and the web server does the translation.
     
    Aredridel, Jan 29, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kiran B.

    Malformed header error... pls help!

    Kiran B., Nov 2, 2003, in forum: Python
    Replies:
    1
    Views:
    444
    Derrick 'dman' Hudson
    Nov 6, 2003
  2. Eric Wichterich

    Re: Malformed Header

    Eric Wichterich, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    408
    Eric Wichterich
    Nov 3, 2003
  3. Kiran Budhrani

    Re: Malformed Header

    Kiran Budhrani, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    440
    Kiran Budhrani
    Nov 3, 2003
  4. Eric Wichterich

    Fwd: Malformed Header

    Eric Wichterich, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    328
    Eric Wichterich
    Nov 3, 2003
  5. Kiran B.
    Replies:
    0
    Views:
    317
    Kiran B.
    Nov 5, 2003
Loading...

Share This Page