1

Is there a way to get POST data from G-WAN Ruby?

I've tried:

ENV.each do |k,v|
  puts "#{k} => #{v} <br/>"
end
exit(200)

and test it using:

curl -d 'test2=1' 'http://127.0.0.1:8080/?test.rb&test=1' | gunzip -

but there are no post data (test2) shown:

GATEWAY_INTERFACE => CGI/1.1 <br/>
CONTENT_TYPE => urlencoded <br/>
REMOTE_HOST => 127.0.0.1 <br/>
USER => lab-hci-48 <br/>
REMOTE_ADDR => 127.0.0.1 <br/>
QUERY_STRING => test.rb&test=1 <br/>
CONTENT_LENGTH => 7 <br/>
PATH_TRANSLATED => /home/lab-hci-48/gwan/0.0.0.0_8080/#0.0.0.0/csp <br/>
REQUEST_URI => POST /?test.rb&test=1 <br/>
SERVER_SOFTWARE => G-WAN <br/>
PATH => /home/lab-hci-48/.rvm/gems/ruby-2.1.1/bin:/home/lab-hci-48/.rvm/gems/ruby-2.1.1@global/bin:/home/lab-hci-48/.rvm/rubies/ruby-2.1.1/bin:/home/lab-hci-48/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games <br/>
LANG => en_US.UTF8 <br/>
SERVER_PROTOCOL => HTTP/1.1 <br/>
PATH_INFO => / <br/>
SHELL => /bin/bash <br/>
REQUEST_METHOD => POST <br/>
PWD => /home/lab-hci-48/gwan <br/>
SERVER_PORT => 8080 <br/>
SCRIPT_NAME => test.rb <br/>
SERVER_NAME => 0.0.0.0 <br/>

Is there any other variable that I should check to get the POST data?

Kokizzu
  • 24,974
  • 37
  • 137
  • 233

2 Answers2

1

In my ruby script i use this code to get data:

# Get arguments
k = ""
@params = {}
ARGV.each do|a|
  if a.split("=").size > 1    
    a = a.split("=")
    @params[a[0]] = a[1]
  else
    if k == ""      
      k = a
    else
      @params[k] = a
      k = ""
    end
  end
end

It will create a params variable just like in Rails ;)

solisoft
  • 669
  • 4
  • 12
  • oh, from `ARGV` variable? '___' how come you know about that? (from which website/documentation?) – Kokizzu May 08 '14 at 12:20
  • 1
    Actually I don't remember where I read it ... I use the 4.11.20 version by the way – solisoft May 08 '14 at 12:22
  • Yes, it works XD, I'm using 4.11.19, did you know how to set headers/cookie? – Kokizzu May 08 '14 at 12:26
  • ah I think i found it.. http://stackoverflow.com/questions/14457373/how-to-set-http-response-header-when-php-is-used-with-g-wan – Kokizzu May 08 '14 at 12:31
  • 1
    You can check my little script https://gist.github.com/solisoft/50a5d83b204963c9952c (not sure it works for reading cookies) but it set headers – solisoft May 08 '14 at 12:33
0

Is there any other variable that I should check to get the POST data?

When the REQUEST_METHOD is HTTP_POST, both CONTENT_LENGTH and CONTENT_TYPE will let you find the attributes of the POST entity (same story with HTTP_PUT).

The X_HTTP_HEADERS value (added last year) makes it easier and faster to find the end of the HTTP headers (where the POST entity resides) - but finding the end of the HTTP headers (a double CRLF) can be done in a Ruby script by starting from the REQUEST_URI or the QUERY_STRING.

The /csp/hello.rb G-WAN example shows how to list query parameters:

   # list QUERY PARAMETERS, if any:
   #
   # GET http://10.10.200.80/?hello.rb&arg1=123&arg2=456

   arg: arg1=123
   arg: arg2=456

   ARGV.each do|a|
     puts "arg: #{a}<br>"
   end
Gil
  • 3,279
  • 1
  • 15
  • 25
  • so, which variable that I should check to get `test2=1`? – Kokizzu May 08 '14 at 11:40
  • The hello.rb shows several ways (argv, get_env, etc.). Sometimes, looking at the *.c examples can help you to understand how things work. – Gil May 09 '14 at 14:00