• CGIHandler: how to handle post requests

    From hypnotortoise@gmail.com@21:1/5 to All on Mon May 23 09:41:24 2016
    I'm writing this simple CGI handler for an endpoint directly in the webserver using the ruby cgi library. So, I want to respond with 405 when it's not a POST, and 200 otherwise.

    # upload.cgi
    require 'cgi'

    cgi = CGI.new

    if cgi.request_method != "POST"
    cgi.out("type" => "text/plain",
    "connection" => "close",
    "status" =>"METHOD_NOT_ALLOWED") { "Method Not Allowed" }

    else
    cgi.out { "hurray" }
    end

    # server.rb
    server = WEBrick::HTTPServer.new :Port => ENV["PORT"] || 8989
    server.mount "/upload", WEBrick::HTTPServlet::CGIHandler , './upload.cgi' trap('INT') { server.stop }
    server.start

    the server.rb is only to test it locally. I'm using curl to test it. So, when I do:

    curl http://localhost:8989/upload -I
    HTTP/1.1 405 Method Not Allowed
    ....

    But when I do:

    curl -X POST http://localhost:8989/upload -I
    => HTTP/1.1 411 Length Required
    ....

    It was my understanding according to the docs that the length would be filled automatically, and if the cgi handles GET requests, in fact it does. So I assume this is a POST "problem". How can I respond successfully?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)