# Help with GServer

**URL:** <https://www.ruby-forum.com/t/help-with-gserver/164244>\
**Category:** Ruby\
**Created:** [March 30, 2009, 11:22am UTC](https://www.ruby-forum.com/t/help-with-gserver/164244 "2009-03-30T11:22:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 30, 2009, 11:22am UTC](https://www.ruby-forum.com/t/help-with-gserver/164244/1 "2009-03-30T11:22:44Z")

</div>

Hi,

I have a requirement

when user type [http://localhost:8000/1.xml](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

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 30, 2009, 11:43am UTC](https://www.ruby-forum.com/t/help-with-gserver/164244/2 "2009-03-30T11:43:22Z")

</div>

2009/3/30 Sudhi K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi,
> 
> I have a requirement
> 
> when user type [http://localhost:8000/1.xml](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.

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 30, 2009, 2:25pm UTC](https://www.ruby-forum.com/t/help-with-gserver/164244/3 "2009-03-30T14:25:00Z")

</div>

IÃ±aki Baz C. 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/)  
[http://rack.rubyforge.org/doc/files/SPEC.html](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.

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 30, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/help-with-gserver/164244/4 "2009-03-30T16:51:52Z")

</div>

On Mar 30, 2009, at 9:44 AM, Sudhi K. 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 G. II

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 30, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/help-with-gserver/164244/5 "2009-03-30T17:22:48Z")

</div>

James G. wrote:

> On Mar 30, 2009, at 9:44 AM, Sudhi K. 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 🙂

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 30, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/help-with-gserver/164244/6 "2009-03-30T16:44:18Z")

</div>

Brian C. wrote:

> IÃ±aki Baz C. 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/)  
> [http://rack.rubyforge.org/doc/files/SPEC.html](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

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 30, 2009, 8:42pm UTC](https://www.ruby-forum.com/t/help-with-gserver/164244/7 "2009-03-30T20:42:31Z")

</div>

On 30 Mar 2009, at 15:51, James G. wrote:

> On Mar 30, 2009, at 9:44 AM, Sudhi K. 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://blog.segment7.net/articles/category/webrick)  
[http://microjet.ath.cx/webrickguide/html/](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](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason
