Forum: Ruby Help with GServer

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C95664a5c59ccdfb0da2f5910675c62c?d=identicon&s=25 Sudhi Kulkarni (sudhi)
on 2009-03-30 11:22
Hi,

I have a requirement

when user type http://localhost:8000/1.xml ... the server should return
1.xml contents which is stored in server's local file system.

I wrote ruby script using GServer library but in serve() function how do
I get the query_string basically I want to be able to get that the user
has requested 1.xml some how...

Please help...

Thanks,
Sudhi
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2009-03-30 11:43
(Received via mailing list)
2009/3/30 Sudhi Kulkarni <kulkarni.sudhi@gmail.com>:
> Hi,
>
> I have a requirement
>
> when user type http://localhost:8000/1.xml ... the server should return
> 1.xml contents which is stored in server's local file system.
>
> I wrote ruby script using GServer library but in serve() function how do
> I get the query_string basically I want to be able to get that the user
> has requested 1.xml some how...

I don't understand your problem.

GServer is just a threaded TCP listener, it doesn't work at
application level (as an HTTP server does). So the steps you should do
are:

1) Start GServer.
2) For each *TCP* connection parse the TCP data and extract the first
line to get the requested URL.
3) Do anything with it.

But you should use something as Mongrel which allows this kind of stuf
very easy and ellegant.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-03-30 14:25
Iñaki Baz Castillo wrote:
> But you should use something as Mongrel which allows this kind of stuf
> very easy and ellegant.

For a low-level HTTP app, I'd say use Rack. You just write a single
handler function; each incoming request calls that function, and the
response is what your function returns.

http://rack.rubyforge.org/
http://rack.rubyforge.org/doc/files/SPEC.html

It includes a bunch of modules you can plug in, like Rack::File which
does exactly the sort of static file serving you're talking about. And
the nice thing is you can run your application under any webserver you
like without any modification.

For a higher-level HTTP app, I'd say use Sinatra. Your app becomes as
simple as this (minus security checks):

  require 'sinatra'
  get '/:id.xml'
    send_file "/path/to/#{id}.xml"
  end

Sinatra runs on Rack, and you can plugin Rack modules easily.
C95664a5c59ccdfb0da2f5910675c62c?d=identicon&s=25 Sudhi Kulkarni (sudhi)
on 2009-03-30 16:44
Brian Candler wrote:
> Iñaki Baz Castillo wrote:
>> But you should use something as Mongrel which allows this kind of stuf
>> very easy and ellegant.
>
> For a low-level HTTP app, I'd say use Rack. You just write a single
> handler function; each incoming request calls that function, and the
> response is what your function returns.
>
> http://rack.rubyforge.org/
> http://rack.rubyforge.org/doc/files/SPEC.html
>
> It includes a bunch of modules you can plug in, like Rack::File which
> does exactly the sort of static file serving you're talking about. And
> the nice thing is you can run your application under any webserver you
> like without any modification.
>
> For a higher-level HTTP app, I'd say use Sinatra. Your app becomes as
> simple as this (minus security checks):
>
>   require 'sinatra'
>   get '/:id.xml'
>     send_file "/path/to/#{id}.xml"
>   end
>
> Sinatra runs on Rack, and you can plugin Rack modules easily.
Hi,

Thanks for the solutions Sinatra and Rack look good but need a bit of
extra installations which I want to avoid for this basic operation...

I like the idea to parse TCP Stram... should I do it in serve()? All I
get as input in serve is a io object of type TCPSocket how do I parse
lines from it? Snippet would be of big help...

Thanks,
Sudhi
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-03-30 16:51
(Received via mailing list)
On Mar 30, 2009, at 9:44 AM, Sudhi Kulkarni wrote:

> Thanks for the solutions Sinatra and Rack look good but need a bit of
> extra installations which I want to avoid for this basic operation...

Then use the WEBrick standard library that ships with Ruby.

> I like the idea to parse TCP Stram...

No you don't.  Trust me.  ;)

James Edward Gray II
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-03-30 17:22
James Gray wrote:
> On Mar 30, 2009, at 9:44 AM, Sudhi Kulkarni wrote:
>
>> Thanks for the solutions Sinatra and Rack look good but need a bit of
>> extra installations which I want to avoid for this basic operation...
>
> Then use the WEBrick standard library that ships with Ruby.
>
>> I like the idea to parse TCP Stram...
>
> No you don't.  Trust me.  ;)

If you don't trust him: read RFC 2616 from top to bottom. Then you will
trust him :-)
Fa2521c6539342333de9f42502657e5a?d=identicon&s=25 Eleanor McHugh (Guest)
on 2009-03-30 20:42
(Received via mailing list)
On 30 Mar 2009, at 15:51, James Gray wrote:
> On Mar 30, 2009, at 9:44 AM, Sudhi Kulkarni wrote:
>
>> Thanks for the solutions Sinatra and Rack look good but need a bit of
>> extra installations which I want to avoid for this basic operation...
>
> Then use the WEBrick standard library that ships with Ruby.

For an overview of how to use WEBrick grab the Camping presentation
from the link in my .sig and also read the following:

http://blog.segment7.net/articles/category/webrick
http://microjet.ath.cx/webrickguide/html/

but don't expect to get high performance as that's not WEBrick's
strength.


Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net
----
raise ArgumentError unless @reality.responds_to? :reason
This topic is locked and can not be replied to.