Skip to content

Instantly share code, notes, and snippets.

@enebo
Created September 28, 2010 01:00
Show Gist options
  • Save enebo/600204 to your computer and use it in GitHub Desktop.
Save enebo/600204 to your computer and use it in GitHub Desktop.
diff --git a/lib/webrick/cgi.rb b/lib/webrick/cgi.rb
index 7099d14..9a94837 100644
--- a/lib/webrick/cgi.rb
+++ b/lib/webrick/cgi.rb
@@ -29,7 +29,7 @@ module WEBrick
end
@config = WEBrick::Config::HTTP.dup.update(
:ServerSoftware => ENV["SERVER_SOFTWARE"] || "null",
- :HTTPVersion => HTTPVersion.new(httpv || "1.0"),
+ :HTTPVersion => (httpv ? HTTPVersion::HTTP_1_0 : HTTPVersion.convert(httpv)),
:RunOnCGI => true, # to detect if it runs on CGI.
:NPH => false # set true to run as NPH script.
)
diff --git a/lib/webrick/config.rb b/lib/webrick/config.rb
index 946312e..1dc70a6 100644
--- a/lib/webrick/config.rb
+++ b/lib/webrick/config.rb
@@ -41,7 +41,7 @@ module WEBrick
HTTP = General.dup.update(
:Port => 80,
:RequestTimeout => 30,
- :HTTPVersion => HTTPVersion.new("1.1"),
+ :HTTPVersion => HTTPVersion::HTTP_1_1,
:AccessLog => nil,
:MimeTypes => HTTPUtils::DefaultMimeTypes,
:DirectoryIndex => ["index.html","index.htm","index.cgi","index.rhtml"],
diff --git a/lib/webrick/httprequest.rb b/lib/webrick/httprequest.rb
index ff9b6d7..ff6e119 100644
--- a/lib/webrick/httprequest.rb
+++ b/lib/webrick/httprequest.rb
@@ -115,7 +115,7 @@ module WEBrick
@keep_alive = false
elsif /keep-alive/io =~ self["connection"]
@keep_alive = true
- elsif @http_version < "1.1"
+ elsif @http_version < HTTPVersion::HTTP_1_1
@keep_alive = false
else
@keep_alive = true
@@ -252,7 +252,7 @@ module WEBrick
if /^(\S+)\s+(\S++)(?:\s+HTTP\/(\d+\.\d+))?\r?\n/mo =~ @request_line
@request_method = $1
@unparsed_uri = $2
- @http_version = HTTPVersion.new($3 ? $3 : "0.9")
+ @http_version = $3 ? HTTPVersion.convert($3) : HTTPVersion::HTTP_0_9
else
rl = @request_line.sub(/\x0d?\x0a\z/o, '')
raise HTTPStatus::BadRequest, "bad Request-Line `#{rl}'."
diff --git a/lib/webrick/httpresponse.rb b/lib/webrick/httpresponse.rb
index 98c4a35..3ca244e 100644
--- a/lib/webrick/httpresponse.rb
+++ b/lib/webrick/httpresponse.rb
@@ -33,7 +33,7 @@ module WEBrick
@header = Hash.new
@status = HTTPStatus::RC_OK
@reason_phrase = nil
- @http_version = HTTPVersion::convert(@config[:HTTPVersion])
+ @http_version = @config[:HTTPVersion]
@body = ''
@keep_alive = true
@cookies = []
@@ -116,13 +116,13 @@ module WEBrick
@header['date'] ||= Time.now.httpdate
# HTTP/0.9 features
- if @request_http_version < "1.0"
- @http_version = HTTPVersion.new("0.9")
+ if @request_http_version < HTTPVersion::HTTP_1_0
+ @http_version = HTTPVersion::HTTP_0_9
@keep_alive = false
end
# HTTP/1.0 features
- if @request_http_version < "1.1"
+ if @request_http_version < HTTPVersion::HTTP_1_1
if chunked?
@chunked = false
ver = @request_http_version.to_s
diff --git a/lib/webrick/httpserver.rb b/lib/webrick/httpserver.rb
index 929d856..b537aef 100644
--- a/lib/webrick/httpserver.rb
+++ b/lib/webrick/httpserver.rb
@@ -22,7 +22,7 @@ module WEBrick
class HTTPServer < ::WEBrick::GenericServer
def initialize(config={}, default=Config::HTTP)
super(config, default)
- @http_version = HTTPVersion::convert(@config[:HTTPVersion])
+ @http_version = @config[:HTTPVersion]
@mount_tab = MountTable.new
if @config[:DocumentRoot]
@@ -87,7 +87,7 @@ module WEBrick
server.access_log(@config, req, res)
end
end
- break if @http_version < "1.1"
+ break if @http_version < HTTPVersion::HTTP_1_1
break unless req.keep_alive?
break unless res.keep_alive?
end
diff --git a/lib/webrick/httpversion.rb b/lib/webrick/httpversion.rb
index 86907a2..a4ef975 100644
--- a/lib/webrick/httpversion.rb
+++ b/lib/webrick/httpversion.rb
@@ -7,43 +7,51 @@
#
# $IPR: httpversion.rb,v 1.5 2002/09/21 12:23:37 gotoyuzo Exp $
+# @compare is a Fixnum which is built by multiplying the major by a large
+# value and then adding the minor value to that. So long as we never have
+# a HTTP minor version >99 this scheme will work fine. The value this scheme
+# is version comparisons is a single Fixnum comparison.
module WEBrick
class HTTPVersion
+ COMPARE_MULTIPLIER = 100
+
include Comparable
- attr_accessor :major, :minor
+ attr_reader :compare
- def self.convert(version)
- version.is_a?(self) ? version : new(version)
+ def self.convert(string)
+ version = HTTP_VERSIONS[string]
+ version ? version : new(string)
end
def initialize(version)
- case version
- when HTTPVersion
- @major, @minor = version.major, version.minor
- when String
- if /^(\d+)\.(\d+)$/ =~ version
- @major, @minor = $1.to_i, $2.to_i
- end
- end
- if @major.nil? || @minor.nil?
- raise ArgumentError,
- format("cannot convert %s into %s", version.class, self.class)
+ if /^(\d+)\.(\d+)$/ !~ version.to_s
+ raise ArgumentError, "cannot convert #{version.class} into HTTPVersion"
end
+
+ major, minor = $1.to_i, $2.to_i
+ @compare = major * COMPARE_MULTIPLIER + minor
+ end
+
+ def major
+ @compare / COMPARE_MULTIPLIER
+ end
+
+ def minor
+ @compare % COMPARE_MULTIPLIER
end
def <=>(other)
- unless other.is_a?(self.class)
- other = self.class.new(other)
- end
- if (ret = @major <=> other.major) == 0
- return @minor <=> other.minor
- end
- return ret
+ @compare <=> other.compare
end
def to_s
- format("%d.%d", @major, @minor)
+ "#{major}.#{minor}"
end
+
+ HTTP_0_9 = HTTPVersion.new('0.9')
+ HTTP_1_0 = HTTPVersion.new('1.0')
+ HTTP_1_1 = HTTPVersion.new('1.1')
+ HTTP_VERSIONS = {'0.9' => HTTP_0_9, '1.0' => HTTP_1_0, '1.1' => HTTP_1_1}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment