10
Philip Repsher October 29 th , 2008 Or Maybe November 3 rd , 2008

Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

Embed Size (px)

Citation preview

Page 1: Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

Philip RepsherOctober 29th, 2008

Or Maybe

November 3rd, 2008

Page 2: Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

In this presentation, I will guide you through how to create a simple ruby web server and a simple ruby web client to get information from the server.

Webrick Simplified web server library included in ruby

since Ruby 1.8.0 net/http class

Basic Ruby framework for HTTP interaction, we will use it for our client.

Page 3: Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

Webrick is a library included in the Ruby standard library dedicated to creating HTTP servers

Webrick is a very easy way to create HTTP servers.

A few commands - Require “webrick” WEBrick::HTTPServer.new(address, port) Trap “INT” Let’s go make a simple web server now

Page 4: Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

The server we just made doesn’t truly do anything

After creating a server, servlets are mounted onto the server as subdirectories (www.server.com/servlet) to provide more functionality

Each time a servlet is called, Ruby creates a new instance of the servlet in its own thread and runs it.

Page 5: Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

Mount – after you make a servlet, it must be mounted on the server before the server is started.

do_GET – A method with this name will service all GET requests.

do_GET methods have two arguments, the request and the response, or req and resp.

Let’s make some servlets!

Page 6: Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

Webrick comes installed with several useful Servlets

Filehandler – if the server is fed a path when starting it automatically sets up a filehandler to serve that directory

CGIhandler-If the directory a filehandler is set up for contains any CGI files, the server will automatically set up a CGIhandler to handle the execution of these files.

Page 7: Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

Net/http is a ruby class that provides access to World Wide Web documents and information via HTTP.

As such, it is the class to use to make a client that will access the web server and report back.

Require ‘net/http’

Page 8: Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

Next we need to set up the client. This client will send a request to the server for info, and then print the info out when it is received.

This client also needs to include the uri library to parse the URL for the net/http command.

We could also add some time commands to see how long the get takes.

Page 9: Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

There are many more advanced Ruby server frameworks out there

In Particular, Ruby on Rails is very popular

Ruby on Rails is a Ruby development framework set up to assist in the making of ruby web applications, particularly database driven ones.

Page 10: Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008

http://segment7.net/projects/ruby/WEBrick/servlets.html

http://microjet.ath.cx/webrickguide/html/html_webrick.html

http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html

http://www.webrick.org/ http://www.ruby-doc.org/docs/

ProgrammingRuby/