Getting html page fields with webrick

Hi All,

I am playing with Webrick and I tried to change a little the example given
with the packet but when I fill a html form and send it the fields values
are empty (using GET method, Webrick doesn’t accept POST method). How to
fix it?
Thank you!

jilani

#!/usr/local/bin/ruby
require 'webrick’
include WEBrick

s = HTTPServer.new(:Port => 2000, :DocumentRoot => “/xitami/webpages”)

class HelloServlet < HTTPServlet::AbstractServlet
def do_GET(req, res)
name = req[‘fname’]
email = req[‘email’]
st = "Hello #{fname} – #{email} “
res[‘Content-Type’] = “text/html"
res.body = st
end
end
s.mount(”/hello”, HelloServlet)

HTTPServer#mount_proc(path){|req, res| …}

You can mount also a block by `mount_proc’.

This block is called when GET or POST.

s.mount_proc("/hello/again"){|req, res|
res.body = "hello (again)"
res[‘Content-Type’] = “text/html”
}

trap(“INT”){ s.shutdown }
s.start

it gives only "hello – " as answer.

ps.
When I run the servlet, I get a warning:
“WARN TCPServer Error: Address family not supported by protocol - socket
(2)”

Everything under Linux Slackware 9.0 and Ruby 1.8.0

···


http://www.jilani.net

It seems to be a common problem.
check the thread 'Integrated webserver?"

I’d recommend to contact the author (???).

-ronnie.

[2003-08-21 14:44:46] INFO WEBrick 1.3.1
[2003-08-21 14:44:46] INFO ruby 1.8.0 (2003-08-04) [i686-linux]
[2003-08-21 14:44:47] WARN TCPServer Error: Address family not
supported by protocol - socket(2)

Could it be because I’m trying to run it inside a 192.* network?

dunno if it could possibly get confused, this it’s working fine for
me:

require ‘webrick’
include WEBrick

s = HTTPServer.new(
:Port => 2000,
:DocumentRoot => “/”
)

s.mount(“/my”, HTTPServlet::FileHandler, ‘C:\pubs’,true)

s.start

it seems that you could try passing
:BindAddress =>your_chosen_addr

possibly you have multiple net interface and this confuses the script?

or maybe you could contact the author to check if you discovered a bug

JK wrote:

···

il Fri, 22 Aug 2003 07:04:38 +0900, Joel VanderWerf vjoel@PATH.Berkeley.EDU ha scritto::

Hi All,

I am playing with Webrick and I tried to change a little the example
given with the packet but when I fill a html form and send it the
fields values are empty (using GET method, Webrick doesn’t accept POST
method). How to fix it?
Thank you!

jilani

#!/usr/local/bin/ruby
require ‘webrick’
include WEBrick

s = HTTPServer.new(:Port => 2000, :DocumentRoot => “/xitami/webpages”)

class HelloServlet < HTTPServlet::AbstractServlet
def do_GET(req, res)
name = req[‘fname’]
email = req[‘email’]
st = “Hello #{fname} – #{email} ”
res[‘Content-Type’] = “text/html”
res.body = st
end
end
s.mount(“/hello”, HelloServlet)

HTTPServer#mount_proc(path){|req, res| …}

You can mount also a block by `mount_proc’.

This block is called when GET or POST.

s.mount_proc(“/hello/again”){|req, res|
res.body = “hello (again)”
res[‘Content-Type’] = “text/html”
}

trap(“INT”){ s.shutdown }
s.start

it gives only "hello – " as answer.

ps.
When I run the servlet, I get a warning:
“WARN TCPServer Error: Address family not supported by protocol -
socket (2)”

Everything under Linux Slackware 9.0 and Ruby 1.8.0

I am playing with Webrick and I tried to change a little the example given
with the packet but when I fill a html form and send it the fields values
are empty (using GET method, Webrick doesn’t accept POST method). How to

Webrick accepts POST method. Just define a method do_POST(req, res).

[…]

name = req[‘fname’]

use req.query[‘fname’].

HTTPRequest# returns values from the request header. For example:

req['Connection'] # => keep-alive

The values you are searching are in the array returned by HTTPRequest#query.

Hi,

In message oprubif80ymwmsst@out.virgilio.it,

I am playing with Webrick and I tried to change a little the example given
with the packet but when I fill a html form and send it the fields values
are empty (using GET method, Webrick doesn’t accept POST method). How to
fix it?

HTTPRequest#query returns a Hash of parsed query.

def do_GET(req, res)
q = req.query
fname = q[‘fname’]
email = q[‘email’]
st = “Hello #{fname} – #{email} ”
res[‘Content-Type’] = “text/html”
res.body = st
end

In addition, do_POST should be defined for POST request.

···

`JK j.khaldi@virgilio.it’ wrote:


gotoyuzo

HTTPRequest#query returns a Hash of parsed query.

def do_GET(req, res)
q = req.query
fname = q[‘fname’]
email = q[‘email’]
st = “Hello #{fname} – #{email} ”
res[‘Content-Type’] = “text/html”
res.body = st
end

In addition, do_POST should be defined for POST request.
That’s ok thank you.
Well, now instead of having the output of results (server side) to
“$stderr” I want to have it in a “log_file”. How to initialize the BasicLog
class?

jilani

···


http://www.jilani.net

Hi,

In message oprucvpwswmwmsst@out.virgilio.it,

Well, now instead of having the output of results (server side) to
“$stderr” I want to have it in a “log_file”. How to initialize the BasicLog
class?

Here is a sample code:

require ‘webrick’

logger = WEBrick::Log::new(“log_file”, WEBrick::Log::DEBUG)
accesslog = WEBrick::BasicLog::new(“accesslog”)
referer = WEBrick::BasicLog::new(“referer”)
agent = WEBrick::BasicLog::new(“agent”)
customlog = WEBrick::BasicLog::new(“customlog”)

s = WEBrick::HTTPServer.new(
:BindAddress => “0.0.0.0”,
:Port => 2000,
:DocumentRoot => File::join(ENV[“HOME”], “public_html”),
:Logger => logger,
:AccessLog => [
[ accesslog, WEBrick::AccessLog::COMMON_LOG_FORMAT ],
[ referer, WEBrick::AccessLog::REFERER_LOG_FORMAT ],
[ agent, WEBrick::AccessLog::AGENT_LOG_FORMAT ],
[ customlog, “%a %U %T” ] # peeraddr, Request-URI, process time
]
)
trap(:INT){ s.shutdown }
s.start

There are two logging options; :Logger is server’s trace log
and :AccessLog is an array of access logs.
Each entry of :AccessLog includes BasicLog object and logging
format string. The specification of format string is a subset
of Apache’s LogFormat.

···

`JK j.khaldi@virgilio.it’ wrote:


gotoyuzo