Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Witness the power of this fully functional HTTP toolkit.
- Loading branch information
1 parent
5f4da1b
commit 11051db
Showing
7 changed files
with
140 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'webmachine' | ||
|
||
class HelloResource < Webmachine::Resource | ||
def last_modified | ||
File.mtime(__FILE__) | ||
end | ||
|
||
def encodings_provided | ||
{ "gzip" => :encode_gzip, "identity" => :encode_identity } | ||
end | ||
|
||
def to_html | ||
"<html><head><title>Hello from Webmachine</title></head><body>Hello, world!</body></html>" | ||
end | ||
end | ||
|
||
Webmachine::Dispatcher.add_route([], HelloResource) | ||
|
||
Webmachine.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'webmachine/adapters/webrick' | ||
|
||
module Webmachine | ||
# Contains classes and modules that connect Webmachine to Ruby | ||
# application servers. | ||
module Adapters | ||
end | ||
|
||
class << self | ||
# @return [Symbol] the current webserver adapter | ||
attr_accessor :adapter | ||
end | ||
|
||
self.adapter = :WEBrick | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'webrick' | ||
require 'webmachine/version' | ||
require 'webmachine/headers' | ||
require 'webmachine/request' | ||
require 'webmachine/response' | ||
require 'webmachine/dispatcher' | ||
|
||
module Webmachine | ||
module Adapters | ||
# Connects Webmachine to WEBrick. | ||
module WEBrick | ||
# Starts the WEBrick adapter | ||
def self.run | ||
server = Webmachine::Adapters::WEBrick::Server.new :Port => 3000 | ||
trap("INT"){ server.shutdown } | ||
Thread.new { server.start }.join | ||
end | ||
|
||
class Server < ::WEBrick::HTTPServer | ||
def service(wreq, wres) | ||
header = Webmachine::Headers.new | ||
wreq.each {|k,v| header[k] = v } | ||
request = Webmachine::Request.new(wreq.request_method, | ||
wreq.request_uri, | ||
header, | ||
RequestBody.new(wreq)) | ||
response = Webmachine::Response.new | ||
Webmachine::Dispatcher.dispatch(request, response) | ||
wres.status = response.code.to_i | ||
response.headers.each do |k,v| | ||
wres[k] = v | ||
end | ||
wres['Server'] = [Webmachine::SERVER_STRING, wres.config[:ServerSoftware]].join(" ") | ||
case response.body | ||
when String | ||
wres.body << response.body | ||
when Enumerable | ||
response.body.each {|part| wres.body << part } | ||
when response.body.respond_to?(:call) | ||
wres.body << response.body.call | ||
end | ||
end | ||
end | ||
|
||
# Wraps the WEBrick request body so that it can be passed to | ||
# {Request} while still lazily evaluating the body. | ||
class RequestBody | ||
def initialize(request) | ||
@request = request | ||
end | ||
|
||
# Converts the body to a String so you can work with the entire | ||
# thing. | ||
def to_s | ||
@value ? @value.join : @request.body | ||
end | ||
|
||
# Iterates over the body in chunks. If the body has previously | ||
# been read, this method can be called again and get the same | ||
# sequence of chunks. | ||
# @yield [chunk] | ||
# @yieldparam [String] chunk a chunk of the request body | ||
def each | ||
if @value | ||
@value.each {|chunk| yield chunk } | ||
else | ||
@value = [] | ||
@request.body {|chunk| @value << chunk; yield chunk } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Webmachine | ||
VERSION = "0.1.0" | ||
SERVER_STRING = "Webmachine.rb/#{VERSION}" | ||
SERVER_STRING = "Webmachine-Ruby/#{VERSION}" | ||
end |
This file was deleted.
Oops, something went wrong.