webrick 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of webrick might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/webrick/cgi.rb +7 -3
- data/lib/webrick/config.rb +5 -5
- data/lib/webrick/cookie.rb +1 -1
- data/lib/webrick/httpauth/basicauth.rb +13 -5
- data/lib/webrick/httpauth/digestauth.rb +10 -23
- data/lib/webrick/httpauth/htdigest.rb +4 -4
- data/lib/webrick/httpauth/htgroup.rb +9 -6
- data/lib/webrick/httpauth/htpasswd.rb +39 -6
- data/lib/webrick/httpauth.rb +5 -5
- data/lib/webrick/httpproxy.rb +44 -32
- data/lib/webrick/httprequest.rb +50 -11
- data/lib/webrick/httpresponse.rb +95 -50
- data/lib/webrick/https.rb +2 -2
- data/lib/webrick/httpserver.rb +23 -9
- data/lib/webrick/httpservlet/abstract.rb +3 -3
- data/lib/webrick/httpservlet/cgi_runner.rb +2 -2
- data/lib/webrick/httpservlet/cgihandler.rb +3 -5
- data/lib/webrick/httpservlet/erbhandler.rb +2 -2
- data/lib/webrick/httpservlet/filehandler.rb +49 -30
- data/lib/webrick/httpservlet/prochandler.rb +1 -1
- data/lib/webrick/httpservlet.rb +5 -5
- data/lib/webrick/httpstatus.rb +1 -1
- data/lib/webrick/httputils.rb +3 -0
- data/lib/webrick/log.rb +1 -1
- data/lib/webrick/server.rb +2 -2
- data/lib/webrick/ssl.rb +2 -2
- data/lib/webrick/version.rb +1 -1
- data/lib/webrick.rb +1 -1
- data/webrick.gemspec +76 -0
- metadata +12 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97da95d5faf45f62560954f75b5fb1536ffdb804a991adbfc1a8da2f312282fb
|
4
|
+
data.tar.gz: c4b305ec0e615913868e1707c42656faa197f6f7a26af17505d7b5b38cbaf9dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62878410914483e24c84430af518e417f985e2471b8907655d2a096966afdd5f872c29f2d65dc6ee88ab4df216f7077678b5cdf04ba021185f1b40402e565114
|
7
|
+
data.tar.gz: ec3e25caa3db02dcf9e7d8dc916cab302c571ba6acb1810681e2051ab7fb35eae06752189a0701c6dee9cd4b5e91f7361c78ceb36e779ed82488702c87d305d9
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Webrick
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/ruby/webrick.svg?branch=master)](https://travis-ci.org/ruby/webrick)
|
4
|
+
|
5
|
+
WEBrick is an HTTP server toolkit that can be configured as an HTTPS server, a proxy server, and a virtual-host server.
|
6
|
+
|
7
|
+
WEBrick features complete logging of both server operations and HTTP access.
|
8
|
+
|
9
|
+
WEBrick supports both basic and digest authentication in addition to algorithms not in RFC 2617.
|
10
|
+
|
11
|
+
A WEBrick server can be composed of multiple WEBrick servers or servlets to provide differing behavior on a per-host or per-path basis. WEBrick includes servlets for handling CGI scripts, ERB pages, Ruby blocks and directory listings.
|
12
|
+
|
13
|
+
WEBrick also includes tools for daemonizing a process and starting a process at a higher privilege level and dropping permissions.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'webrick'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install webrick
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
To create a new WEBrick::HTTPServer that will listen to connections on port 8000 and serve documents from the current user's public_html folder:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'webrick'
|
37
|
+
|
38
|
+
root = File.expand_path '~/public_html'
|
39
|
+
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root
|
40
|
+
```
|
41
|
+
|
42
|
+
To run the server you will need to provide a suitable shutdown hook as
|
43
|
+
starting the server blocks the current thread:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
trap 'INT' do server.shutdown end
|
47
|
+
|
48
|
+
server.start
|
49
|
+
```
|
50
|
+
|
51
|
+
## Development
|
52
|
+
|
53
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
54
|
+
|
55
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
Bug reports and Patch are welcome on https://bugs.ruby-lang.org/.
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
The gem is available as open source under the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "webrick"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/webrick/cgi.rb
CHANGED
@@ -8,9 +8,9 @@
|
|
8
8
|
#
|
9
9
|
# $Id$
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
require_relative "httprequest"
|
12
|
+
require_relative "httpresponse"
|
13
|
+
require_relative "config"
|
14
14
|
require "stringio"
|
15
15
|
|
16
16
|
module WEBrick
|
@@ -265,6 +265,10 @@ module WEBrick
|
|
265
265
|
@out_port << data
|
266
266
|
end
|
267
267
|
|
268
|
+
def write(data)
|
269
|
+
@out_port.write(data)
|
270
|
+
end
|
271
|
+
|
268
272
|
def cert
|
269
273
|
return nil unless defined?(OpenSSL)
|
270
274
|
if pem = @env["SSL_SERVER_CERT"]
|
data/lib/webrick/config.rb
CHANGED
@@ -9,11 +9,11 @@
|
|
9
9
|
#
|
10
10
|
# $IPR: config.rb,v 1.52 2003/07/22 19:20:42 gotoyuzo Exp $
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
require_relative 'version'
|
13
|
+
require_relative 'httpversion'
|
14
|
+
require_relative 'httputils'
|
15
|
+
require_relative 'utils'
|
16
|
+
require_relative 'log'
|
17
17
|
|
18
18
|
module WEBrick
|
19
19
|
module Config
|
data/lib/webrick/cookie.rb
CHANGED
@@ -8,9 +8,9 @@
|
|
8
8
|
#
|
9
9
|
# $IPR: basicauth.rb,v 1.5 2003/02/20 07:15:47 gotoyuzo Exp $
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
require_relative '../config'
|
12
|
+
require_relative '../httpstatus'
|
13
|
+
require_relative 'authenticator'
|
14
14
|
|
15
15
|
module WEBrick
|
16
16
|
module HTTPAuth
|
@@ -24,7 +24,7 @@ module WEBrick
|
|
24
24
|
#
|
25
25
|
# config = { :Realm => 'BasicAuth example realm' }
|
26
26
|
#
|
27
|
-
# htpasswd = WEBrick::HTTPAuth::Htpasswd.new 'my_password_file'
|
27
|
+
# htpasswd = WEBrick::HTTPAuth::Htpasswd.new 'my_password_file', password_hash: :bcrypt
|
28
28
|
# htpasswd.set_passwd config[:Realm], 'username', 'password'
|
29
29
|
# htpasswd.flush
|
30
30
|
#
|
@@ -81,7 +81,15 @@ module WEBrick
|
|
81
81
|
error("%s: the user is not allowed.", userid)
|
82
82
|
challenge(req, res)
|
83
83
|
end
|
84
|
-
|
84
|
+
|
85
|
+
case encpass
|
86
|
+
when /\A\$2[aby]\$/
|
87
|
+
password_matches = BCrypt::Password.new(encpass.sub(/\A\$2[aby]\$/, '$2a$')) == password
|
88
|
+
else
|
89
|
+
password_matches = password.crypt(encpass) == encpass
|
90
|
+
end
|
91
|
+
|
92
|
+
unless password_matches
|
85
93
|
error("%s: password unmatch.", userid)
|
86
94
|
challenge(req, res)
|
87
95
|
end
|
@@ -12,9 +12,9 @@
|
|
12
12
|
#
|
13
13
|
# $IPR: digestauth.rb,v 1.5 2003/02/20 07:15:47 gotoyuzo Exp $
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
require_relative '../config'
|
16
|
+
require_relative '../httpstatus'
|
17
|
+
require_relative 'authenticator'
|
18
18
|
require 'digest/md5'
|
19
19
|
require 'digest/sha1'
|
20
20
|
|
@@ -235,9 +235,11 @@ module WEBrick
|
|
235
235
|
ha2 = hexdigest(req.request_method, auth_req['uri'])
|
236
236
|
ha2_res = hexdigest("", auth_req['uri'])
|
237
237
|
elsif auth_req['qop'] == "auth-int"
|
238
|
-
|
239
|
-
|
240
|
-
|
238
|
+
body_digest = @h.new
|
239
|
+
req.body { |chunk| body_digest.update(chunk) }
|
240
|
+
body_digest = body_digest.hexdigest
|
241
|
+
ha2 = hexdigest(req.request_method, auth_req['uri'], body_digest)
|
242
|
+
ha2_res = hexdigest("", auth_req['uri'], body_digest)
|
241
243
|
end
|
242
244
|
|
243
245
|
if auth_req['qop'] == "auth" || auth_req['qop'] == "auth-int"
|
@@ -288,23 +290,8 @@ module WEBrick
|
|
288
290
|
|
289
291
|
def split_param_value(string)
|
290
292
|
ret = {}
|
291
|
-
|
292
|
-
|
293
|
-
when /^\s*([\w\-\.\*\%\!]+)=\s*\"((\\.|[^\"])*)\"\s*,?/
|
294
|
-
key = $1
|
295
|
-
matched = $2
|
296
|
-
string = $'
|
297
|
-
ret[key] = matched.gsub(/\\(.)/, "\\1")
|
298
|
-
when /^\s*([\w\-\.\*\%\!]+)=\s*([^,\"]*),?/
|
299
|
-
key = $1
|
300
|
-
matched = $2
|
301
|
-
string = $'
|
302
|
-
ret[key] = matched.clone
|
303
|
-
when /^s*^,/
|
304
|
-
string = $'
|
305
|
-
else
|
306
|
-
break
|
307
|
-
end
|
293
|
+
string.scan(/\G\s*([\w\-.*%!]+)=\s*(?:\"((?>\\.|[^\"])*)\"|([^,\"]*))\s*,?/) do
|
294
|
+
ret[$1] = $3 || $2.gsub(/\\(.)/, "\\1")
|
308
295
|
end
|
309
296
|
ret
|
310
297
|
end
|
@@ -8,8 +8,8 @@
|
|
8
8
|
#
|
9
9
|
# $IPR: htdigest.rb,v 1.4 2003/07/22 19:20:45 gotoyuzo Exp $
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
require_relative 'userdb'
|
12
|
+
require_relative 'digestauth'
|
13
13
|
require 'tempfile'
|
14
14
|
|
15
15
|
module WEBrick
|
@@ -40,7 +40,7 @@ module WEBrick
|
|
40
40
|
@digest = Hash.new
|
41
41
|
@mutex = Thread::Mutex::new
|
42
42
|
@auth_type = DigestAuth
|
43
|
-
open(@path,"a").close unless File
|
43
|
+
File.open(@path,"a").close unless File.exist?(@path)
|
44
44
|
reload
|
45
45
|
end
|
46
46
|
|
@@ -51,7 +51,7 @@ module WEBrick
|
|
51
51
|
mtime = File::mtime(@path)
|
52
52
|
if mtime > @mtime
|
53
53
|
@digest.clear
|
54
|
-
open(@path){|io|
|
54
|
+
File.open(@path){|io|
|
55
55
|
while line = io.gets
|
56
56
|
line.chomp!
|
57
57
|
user, realm, pass = line.split(/:/, 3)
|
@@ -36,7 +36,7 @@ module WEBrick
|
|
36
36
|
@path = path
|
37
37
|
@mtime = Time.at(0)
|
38
38
|
@group = Hash.new
|
39
|
-
open(@path,"a").close unless File
|
39
|
+
File.open(@path,"a").close unless File.exist?(@path)
|
40
40
|
reload
|
41
41
|
end
|
42
42
|
|
@@ -46,7 +46,7 @@ module WEBrick
|
|
46
46
|
def reload
|
47
47
|
if (mtime = File::mtime(@path)) > @mtime
|
48
48
|
@group.clear
|
49
|
-
open(@path){|io|
|
49
|
+
File.open(@path){|io|
|
50
50
|
while line = io.gets
|
51
51
|
line.chomp!
|
52
52
|
group, members = line.split(/:\s*/)
|
@@ -63,15 +63,18 @@ module WEBrick
|
|
63
63
|
|
64
64
|
def flush(output=nil)
|
65
65
|
output ||= @path
|
66
|
-
tmp = Tempfile.
|
66
|
+
tmp = Tempfile.create("htgroup", File::dirname(output))
|
67
67
|
begin
|
68
68
|
@group.keys.sort.each{|group|
|
69
69
|
tmp.puts(format("%s: %s", group, self.members(group).join(" ")))
|
70
70
|
}
|
71
|
+
ensure
|
71
72
|
tmp.close
|
72
|
-
|
73
|
-
|
74
|
-
|
73
|
+
if $!
|
74
|
+
File.unlink(tmp.path)
|
75
|
+
else
|
76
|
+
return File.rename(tmp.path, output)
|
77
|
+
end
|
75
78
|
end
|
76
79
|
end
|
77
80
|
|
@@ -8,8 +8,8 @@
|
|
8
8
|
#
|
9
9
|
# $IPR: htpasswd.rb,v 1.4 2003/07/22 19:20:45 gotoyuzo Exp $
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
require_relative 'userdb'
|
12
|
+
require_relative 'basicauth'
|
13
13
|
require 'tempfile'
|
14
14
|
|
15
15
|
module WEBrick
|
@@ -35,12 +35,30 @@ module WEBrick
|
|
35
35
|
##
|
36
36
|
# Open a password database at +path+
|
37
37
|
|
38
|
-
def initialize(path)
|
38
|
+
def initialize(path, password_hash: nil)
|
39
39
|
@path = path
|
40
40
|
@mtime = Time.at(0)
|
41
41
|
@passwd = Hash.new
|
42
42
|
@auth_type = BasicAuth
|
43
|
-
|
43
|
+
@password_hash = password_hash
|
44
|
+
|
45
|
+
case @password_hash
|
46
|
+
when nil
|
47
|
+
# begin
|
48
|
+
# require "string/crypt"
|
49
|
+
# rescue LoadError
|
50
|
+
# warn("Unable to load string/crypt, proceeding with deprecated use of String#crypt, consider using password_hash: :bcrypt")
|
51
|
+
# end
|
52
|
+
@password_hash = :crypt
|
53
|
+
when :crypt
|
54
|
+
# require "string/crypt"
|
55
|
+
when :bcrypt
|
56
|
+
require "bcrypt"
|
57
|
+
else
|
58
|
+
raise ArgumentError, "only :crypt and :bcrypt are supported for password_hash keyword argument"
|
59
|
+
end
|
60
|
+
|
61
|
+
File.open(@path,"a").close unless File.exist?(@path)
|
44
62
|
reload
|
45
63
|
end
|
46
64
|
|
@@ -51,11 +69,19 @@ module WEBrick
|
|
51
69
|
mtime = File::mtime(@path)
|
52
70
|
if mtime > @mtime
|
53
71
|
@passwd.clear
|
54
|
-
open(@path){|io|
|
72
|
+
File.open(@path){|io|
|
55
73
|
while line = io.gets
|
56
74
|
line.chomp!
|
57
75
|
case line
|
58
76
|
when %r!\A[^:]+:[a-zA-Z0-9./]{13}\z!
|
77
|
+
if @password_hash == :bcrypt
|
78
|
+
raise StandardError, ".htpasswd file contains crypt password, only bcrypt passwords supported"
|
79
|
+
end
|
80
|
+
user, pass = line.split(":")
|
81
|
+
when %r!\A[^:]+:\$2[aby]\$\d{2}\$.{53}\z!
|
82
|
+
if @password_hash == :crypt
|
83
|
+
raise StandardError, ".htpasswd file contains bcrypt password, only crypt passwords supported"
|
84
|
+
end
|
59
85
|
user, pass = line.split(":")
|
60
86
|
when /:\$/, /:{SHA}/
|
61
87
|
raise NotImplementedError,
|
@@ -102,7 +128,14 @@ module WEBrick
|
|
102
128
|
# Sets a password in the database for +user+ in +realm+ to +pass+.
|
103
129
|
|
104
130
|
def set_passwd(realm, user, pass)
|
105
|
-
@
|
131
|
+
if @password_hash == :bcrypt
|
132
|
+
# Cost of 5 to match Apache default, and because the
|
133
|
+
# bcrypt default of 10 will introduce significant delays
|
134
|
+
# for every request.
|
135
|
+
@passwd[user] = BCrypt::Password.create(pass, :cost=>5)
|
136
|
+
else
|
137
|
+
@passwd[user] = make_passwd(realm, user, pass)
|
138
|
+
end
|
106
139
|
end
|
107
140
|
|
108
141
|
##
|
data/lib/webrick/httpauth.rb
CHANGED
@@ -9,11 +9,11 @@
|
|
9
9
|
#
|
10
10
|
# $IPR: httpauth.rb,v 1.14 2003/07/22 19:20:42 gotoyuzo Exp $
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
require_relative 'httpauth/basicauth'
|
13
|
+
require_relative 'httpauth/digestauth'
|
14
|
+
require_relative 'httpauth/htpasswd'
|
15
|
+
require_relative 'httpauth/htdigest'
|
16
|
+
require_relative 'httpauth/htgroup'
|
17
17
|
|
18
18
|
module WEBrick
|
19
19
|
|
data/lib/webrick/httpproxy.rb
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
# $IPR: httpproxy.rb,v 1.18 2003/03/08 18:58:10 gotoyuzo Exp $
|
11
11
|
# $kNotwork: straw.rb,v 1.3 2002/02/12 15:13:07 gotoken Exp $
|
12
12
|
|
13
|
-
|
13
|
+
require_relative "httpserver"
|
14
14
|
require "net/http"
|
15
15
|
|
16
16
|
module WEBrick
|
@@ -211,21 +211,15 @@ module WEBrick
|
|
211
211
|
end
|
212
212
|
|
213
213
|
def do_GET(req, res)
|
214
|
-
perform_proxy_request(req, res
|
215
|
-
http.get(path, header)
|
216
|
-
end
|
214
|
+
perform_proxy_request(req, res, Net::HTTP::Get)
|
217
215
|
end
|
218
216
|
|
219
217
|
def do_HEAD(req, res)
|
220
|
-
perform_proxy_request(req, res
|
221
|
-
http.head(path, header)
|
222
|
-
end
|
218
|
+
perform_proxy_request(req, res, Net::HTTP::Head)
|
223
219
|
end
|
224
220
|
|
225
221
|
def do_POST(req, res)
|
226
|
-
perform_proxy_request(req, res
|
227
|
-
http.post(path, req.body || "", header)
|
228
|
-
end
|
222
|
+
perform_proxy_request(req, res, Net::HTTP::Post, req.body_reader)
|
229
223
|
end
|
230
224
|
|
231
225
|
def do_OPTIONS(req, res)
|
@@ -301,38 +295,56 @@ module WEBrick
|
|
301
295
|
return FakeProxyURI
|
302
296
|
end
|
303
297
|
|
304
|
-
def perform_proxy_request(req, res)
|
298
|
+
def perform_proxy_request(req, res, req_class, body_stream = nil)
|
305
299
|
uri = req.request_uri
|
306
300
|
path = uri.path.dup
|
307
301
|
path << "?" << uri.query if uri.query
|
308
302
|
header = setup_proxy_header(req, res)
|
309
303
|
upstream = setup_upstream_proxy_authentication(req, res, header)
|
310
|
-
response = nil
|
311
304
|
|
305
|
+
body_tmp = []
|
312
306
|
http = Net::HTTP.new(uri.host, uri.port, upstream.host, upstream.port)
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
307
|
+
req_fib = Fiber.new do
|
308
|
+
http.start do
|
309
|
+
if @config[:ProxyTimeout]
|
310
|
+
################################## these issues are
|
311
|
+
http.open_timeout = 30 # secs # necessary (maybe because
|
312
|
+
http.read_timeout = 60 # secs # Ruby's bug, but why?)
|
313
|
+
##################################
|
314
|
+
end
|
315
|
+
if body_stream && req['transfer-encoding'] =~ /\bchunked\b/i
|
316
|
+
header['Transfer-Encoding'] = 'chunked'
|
317
|
+
end
|
318
|
+
http_req = req_class.new(path, header)
|
319
|
+
http_req.body_stream = body_stream if body_stream
|
320
|
+
http.request(http_req) do |response|
|
321
|
+
# Persistent connection requirements are mysterious for me.
|
322
|
+
# So I will close the connection in every response.
|
323
|
+
res['proxy-connection'] = "close"
|
324
|
+
res['connection'] = "close"
|
325
|
+
|
326
|
+
# stream Net::HTTP::HTTPResponse to WEBrick::HTTPResponse
|
327
|
+
res.status = response.code.to_i
|
328
|
+
res.chunked = response.chunked?
|
329
|
+
choose_header(response, res)
|
330
|
+
set_cookie(response, res)
|
331
|
+
set_via(res)
|
332
|
+
response.read_body do |buf|
|
333
|
+
body_tmp << buf
|
334
|
+
Fiber.yield # wait for res.body Proc#call
|
335
|
+
end
|
336
|
+
end # http.request
|
337
|
+
end
|
338
|
+
end
|
339
|
+
req_fib.resume # read HTTP response headers and first chunk of the body
|
340
|
+
res.body = ->(socket) do
|
341
|
+
while buf = body_tmp.shift
|
342
|
+
socket.write(buf)
|
343
|
+
buf.clear
|
344
|
+
req_fib.resume # continue response.read_body
|
319
345
|
end
|
320
|
-
response = yield(http, path, header)
|
321
346
|
end
|
322
|
-
|
323
|
-
# Persistent connection requirements are mysterious for me.
|
324
|
-
# So I will close the connection in every response.
|
325
|
-
res['proxy-connection'] = "close"
|
326
|
-
res['connection'] = "close"
|
327
|
-
|
328
|
-
# Convert Net::HTTP::HTTPResponse to WEBrick::HTTPResponse
|
329
|
-
res.status = response.code.to_i
|
330
|
-
choose_header(response, res)
|
331
|
-
set_cookie(response, res)
|
332
|
-
set_via(res)
|
333
|
-
res.body = response.body
|
334
347
|
end
|
335
|
-
|
336
348
|
# :stopdoc:
|
337
349
|
end
|
338
350
|
end
|
data/lib/webrick/httprequest.rb
CHANGED
@@ -10,10 +10,10 @@
|
|
10
10
|
# $IPR: httprequest.rb,v 1.64 2003/07/13 17:18:22 gotoyuzo Exp $
|
11
11
|
|
12
12
|
require 'uri'
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
require_relative 'httpversion'
|
14
|
+
require_relative 'httpstatus'
|
15
|
+
require_relative 'httputils'
|
16
|
+
require_relative 'cookie'
|
17
17
|
|
18
18
|
module WEBrick
|
19
19
|
|
@@ -257,6 +257,32 @@ module WEBrick
|
|
257
257
|
@body.empty? ? nil : @body
|
258
258
|
end
|
259
259
|
|
260
|
+
##
|
261
|
+
# Prepares the HTTPRequest object for use as the
|
262
|
+
# source for IO.copy_stream
|
263
|
+
|
264
|
+
def body_reader
|
265
|
+
@body_tmp = []
|
266
|
+
@body_rd = Fiber.new do
|
267
|
+
body do |buf|
|
268
|
+
@body_tmp << buf
|
269
|
+
Fiber.yield
|
270
|
+
end
|
271
|
+
end
|
272
|
+
@body_rd.resume # grab the first chunk and yield
|
273
|
+
self
|
274
|
+
end
|
275
|
+
|
276
|
+
# for IO.copy_stream. Note: we may return a larger string than +size+
|
277
|
+
# here; but IO.copy_stream does not care.
|
278
|
+
def readpartial(size, buf = ''.b) # :nodoc
|
279
|
+
res = @body_tmp.shift or raise EOFError, 'end of file reached'
|
280
|
+
buf.replace(res)
|
281
|
+
res.clear
|
282
|
+
@body_rd.resume # get more chunks
|
283
|
+
buf
|
284
|
+
end
|
285
|
+
|
260
286
|
##
|
261
287
|
# Request query as a Hash
|
262
288
|
|
@@ -414,13 +440,19 @@ module WEBrick
|
|
414
440
|
|
415
441
|
MAX_URI_LENGTH = 2083 # :nodoc:
|
416
442
|
|
443
|
+
# same as Mongrel, Thin and Puma
|
444
|
+
MAX_HEADER_LENGTH = (112 * 1024) # :nodoc:
|
445
|
+
|
417
446
|
def read_request_line(socket)
|
418
447
|
@request_line = read_line(socket, MAX_URI_LENGTH) if socket
|
419
|
-
|
448
|
+
raise HTTPStatus::EOFError unless @request_line
|
449
|
+
|
450
|
+
@request_bytes = @request_line.bytesize
|
451
|
+
if @request_bytes >= MAX_URI_LENGTH and @request_line[-1, 1] != LF
|
420
452
|
raise HTTPStatus::RequestURITooLarge
|
421
453
|
end
|
454
|
+
|
422
455
|
@request_time = Time.now
|
423
|
-
raise HTTPStatus::EOFError unless @request_line
|
424
456
|
if /^(\S+)\s+(\S++)(?:\s+HTTP\/(\d+\.\d+))?\r?\n/mo =~ @request_line
|
425
457
|
@request_method = $1
|
426
458
|
@unparsed_uri = $2
|
@@ -435,6 +467,9 @@ module WEBrick
|
|
435
467
|
if socket
|
436
468
|
while line = read_line(socket)
|
437
469
|
break if /\A(#{CRLF}|#{LF})\z/om =~ line
|
470
|
+
if (@request_bytes += line.bytesize) > MAX_HEADER_LENGTH
|
471
|
+
raise HTTPStatus::RequestEntityTooLarge, 'headers too large'
|
472
|
+
end
|
438
473
|
@raw_header << line
|
439
474
|
end
|
440
475
|
end
|
@@ -502,12 +537,16 @@ module WEBrick
|
|
502
537
|
def read_chunked(socket, block)
|
503
538
|
chunk_size, = read_chunk_size(socket)
|
504
539
|
while chunk_size > 0
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
540
|
+
begin
|
541
|
+
sz = [ chunk_size, @buffer_size ].min
|
542
|
+
data = read_data(socket, sz) # read chunk-data
|
543
|
+
if data.nil? || data.bytesize != sz
|
544
|
+
raise HTTPStatus::BadRequest, "bad chunk data size."
|
545
|
+
end
|
546
|
+
block.call(data)
|
547
|
+
end while (chunk_size -= sz) > 0
|
548
|
+
|
509
549
|
read_line(socket) # skip CRLF
|
510
|
-
block.call(data)
|
511
550
|
chunk_size, = read_chunk_size(socket)
|
512
551
|
end
|
513
552
|
read_header(socket) # trailer + CRLF
|