On Mon, 29 Jan 2001, Michael Schuerig wrote:

> I'm trying to write a simplistic server that's launched by inetd. From a
> user perspective it looks like this
>
> http://localhost:60000/searchdir?pattern=ruby+dir=/usr/doc/ruby/manual
>
> The point of this is to put a button in the toolbar of Netscape that
> does some JavaScript twiddling (similar to what the Google buttons do)
> and in effect connects to a "server" (my script) that searches the local
> directory I'm currently viewing. (Yes, my firewall blocks port 60000!)
>
> Now, I'd like to use the cgi class to parse the request for me. I read
> the request from stdin with
>
>     rawrequest = $stdin.sysread(1024) # ordinary read seems to block
>
> and feed it to
>
>     request = CGI.parse(rawrequest)
>
> it doesn't produce a sensible result.
>
> Michael
>
> --
> Michael Schuerig
> mailto:schuerig / acm.org
> http://www.schuerig.de/michael/

Here's some code, I use to parse HTTP requests:


  def serve(io)
    command = io.gets     # something like GET /cgi-bin HTTP/1.0

    # I have not checked this
    command =~ /^(GET|POST|HEAD)\s+([^\s]+)\s+/
    method = $1
    path = $2


    hash = {}

    while (line=io.gets) !~ /^(\n|\r)/
      if line =~ /^([\w-]+):\s*(.*)$/
	hash[$1.upcase] = $2.strip
      end
    end

    # hash contains all headers


    # I assume it is a POST request, so if it is a GET, you
    # don't need to do that
    body = io.read(hash["CONTENT-LENGTH"].to_i)

    resp = .... # create response

    io.puts "HTTP/1.0 200 OK"
    io.puts "Connection: close"
    io.puts "Content-Length: #{resp.size}"
    io.puts "Content-type: text/xml"
    io.puts "Server: XMLRPC::Server (Ruby #{RUBY_VERSION})"
    io.puts
    io.puts resp
  end


Regards

Michael Neumann