26288 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Investor Icahn
needs a loan of
$7bn to tick off
Mike Dell
World"s richest
hobo (Apple) has
worked "tax-free"
in Ireland since
"80s
Security
Twitteratti:
Twitter"s 2FA does
sweet FA for biz
Judge: Evidence
will likely show
Apple DID fix ebook
prices
ServiceKey, Oracle
end "grey market"
code spat without
bloodshed
BBC suspends CTO
after it spaffs
£100m on doomed IT
system
Open wide, Google:
Here comes an
advertising
antitrust probe
El Reg drills into
Office 365: Email
migration
Daft tweet by
Speaker Bercow"s
loquacious wife DID
libel lord
Paul Allen buys
lovingly restored
vintage V-2 Nazi
ballistic missile
Slashdot
FiOS User Finds
Limit of
"Unlimited" Data
Plan: 77 TB/Month
Xbox One Used Game
Policy Leaks:
Publishers Get a
Cut of Sale
Possible Collision
Between
Cube-satellite and
Old Space Junk
AT&T Quietly
Adds Charges To All
Contract Cell Plans
Drupalcon Attendees
Come Together To
Build Help4ok.org
In 24 Hours
Twitter"s New
Money-Making Plan:
Lead Generation
Bandages That Can
Turn Off Genes
Encourages Wound
Healing
BT Runs an 800Gbps
Channel On Old
Fiber
Australian Police
Move To Make 3D
Printed Guns
Illegal
Cockroaches
Evolving To Avoid
Roach Motels
Article viewer

Web Server



Written by:dimport
Published by:dimport
Published on:2003-06-21 07:19:46
Topic:Ruby
Search OSI about Ruby.More articles by dimport.
 viewed 6244 times send this article printer friendly

Digg this!
    Rate this article :
Had a few requests to release the code for this, so here it is, a 50 line (ok 49, but 50 sounds better) ruby web server, whilst its hardly apache it does the job.

This has gone through a few incarnations, the first of which could only serve one client at a time, i then overcame this via forking, and then, as my skills progressed i decided to make a separate thread for each client, whilst there's still room for improvement (security, using net::http etc) its fairly respectable given its size methinks.
Quote:

[b]code:[/b]<hr>

 
require 'socket'
 

 
def main
 
    listener = TCPServer.new("localhost", (ARGV[0]||80).to_i)
 
    
 
    loop do
 
        session = listener.accept
 
        Thread.new(session) do |s|
 
        req = nil
 
        session.each do |line|
 
        if line =~ /^GET (.+) HTTP/
 
            req $1
 
        end
 
        break if line.strip.empty?
 
    end
 
    
 
    if req
 
        session.puts http_file("." + req)
 
    else
 
        session.puts err_page("error")
 
    end
 
s.close
 
end
 
end
 
end
 

 
Header = "HTTP/1.1 200/OKnContent-type: text/htmlrnrn"
 

 
def err_page(title, message)
 
    Header+
 
        "<html><head><title>#{title}</title></head>n" +
 
        "<body>[b]Error:[/b]#{message}</body></html>n"
 
end
 

 
def http_file(filename)
 
    if filename =~ /../
 
        return err_page 'Error', '".." in URL not allowed'
 
    else
 
        begin
 
        f = File.open(filename,"r")
 
        contents = f.read
 
        f.close
 
        Header + contents
 
        rescue
 
            err_page 'Error', $!.to_s.gsub(/n/, "
")
 
            end
 
        end
 
    end
 
main
 
 
<hr>


This article was originally written by Pigsbig78

Did you like this article? There are hundreds more.

Comments:
<none>
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
reacher
The Ruby Way on Wed 27th Aug 6pm
I've recently come back to OSIX and taken all the challenges again. This next time around, however, I'm using Ruby to tackle them. I will admit that my old C versions of some solutions ran faster (especially the brute force ones), but coding the Ruby ve
bb
Ruby on Rails on Tue 10th Oct 9am
I installed ruby and wrote my first ruby on rails web application. It was quite an enjoyable experience. Certainly more fun than asp.net (which is bloody unproductive) I suggested we knock some internal application up in it for my company to have ap


     
Your Ad Here
 
Copyright Open Source Institute, 2006