webrick 1.4.2 → 1.4.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 679fbeaf42fd0e9df98934eacf8573238f0b32a5bdcab66cb6ded69c3031ca8d
4
- data.tar.gz: 77811219317b65fdbc1a3f703d98dc63c9c6d936ef29775cbef7e1aa1f707007
3
+ metadata.gz: f5e6d9bdc4f99de4e0488649df6d801d9c4ccef823d2d74916548b9eca6652a8
4
+ data.tar.gz: 7cbac41b577d94a74fd451e4181489ffccaa9ce2bf54fcbfde2a81cf28c4728f
5
5
  SHA512:
6
- metadata.gz: 4bce582ecef05a51ae39d331f6cf2a3a1f35bd5bde35ea69be01374ec9dc851a536fbb37ea276d37f137d86d8af2258b0caf34b30dce83f3a79d36e88b2cfefc
7
- data.tar.gz: 1c4fa49ececffec80b3d3ab8013960379d157202ba4b81959d6bcd91f0dcc978fcc55d60628d2383b8b80c7d1c4ce6b4dc5ca067ea548d0e20ce643d3cb26067
6
+ metadata.gz: aafcd2d775674d2d987492130119eb6b3f497443b1a0c702b1717ed94514b9ea7ce166c2937035ce465ae8e582312896e8b317973a826d4cd5de79809a626710
7
+ data.tar.gz: 7a5657cb4868485516c660a0d65aa937522d5f8db923cd72e029b227f35b324967b4347265d1b80dd273f7cf98670e3328c3ebb0e08e02c1b6e0f638b175cf26
@@ -8,9 +8,9 @@
8
8
  #
9
9
  # $Id$
10
10
 
11
- require "webrick/httprequest"
12
- require "webrick/httpresponse"
13
- require "webrick/config"
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"]
@@ -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
- require 'webrick/version'
13
- require 'webrick/httpversion'
14
- require 'webrick/httputils'
15
- require 'webrick/utils'
16
- require 'webrick/log'
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
@@ -10,7 +10,7 @@
10
10
  # $IPR: cookie.rb,v 1.16 2002/09/21 12:23:35 gotoyuzo Exp $
11
11
 
12
12
  require 'time'
13
- require 'webrick/httputils'
13
+ require_relative 'httputils'
14
14
 
15
15
  module WEBrick
16
16
 
@@ -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
- require 'webrick/httpauth/basicauth'
13
- require 'webrick/httpauth/digestauth'
14
- require 'webrick/httpauth/htpasswd'
15
- require 'webrick/httpauth/htdigest'
16
- require 'webrick/httpauth/htgroup'
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
 
@@ -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
- require 'webrick/config'
12
- require 'webrick/httpstatus'
13
- require 'webrick/httpauth/authenticator'
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
- if password.crypt(encpass) != encpass
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
- require 'webrick/config'
16
- require 'webrick/httpstatus'
17
- require 'webrick/httpauth/authenticator'
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
- ha2 = hexdigest(req.request_method, auth_req['uri'],
239
- hexdigest(req.body))
240
- ha2_res = hexdigest("", auth_req['uri'], hexdigest(res.body))
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
- while string.bytesize != 0
292
- case string
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
- require 'webrick/httpauth/userdb'
12
- require 'webrick/httpauth/digestauth'
11
+ require_relative 'userdb'
12
+ require_relative 'digestauth'
13
13
  require 'tempfile'
14
14
 
15
15
  module WEBrick
@@ -63,15 +63,18 @@ module WEBrick
63
63
 
64
64
  def flush(output=nil)
65
65
  output ||= @path
66
- tmp = Tempfile.new("htgroup", File::dirname(output))
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
- File::rename(tmp.path, output)
73
- rescue
74
- tmp.close(true)
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
- require 'webrick/httpauth/userdb'
12
- require 'webrick/httpauth/basicauth'
11
+ require_relative 'userdb'
12
+ require_relative 'basicauth'
13
13
  require 'tempfile'
14
14
 
15
15
  module WEBrick
@@ -35,11 +35,29 @@ 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
+ @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
+
43
61
  File.open(@path,"a").close unless File.exist?(@path)
44
62
  reload
45
63
  end
@@ -56,6 +74,14 @@ module WEBrick
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
- @passwd[user] = make_passwd(realm, user, pass)
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
  ##
@@ -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
- require "webrick/httpserver"
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) do |http, path, header|
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) do |http, path, header|
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) do |http, path, header|
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
- http.start do
314
- if @config[:ProxyTimeout]
315
- ################################## these issues are
316
- http.open_timeout = 30 # secs # necessary (maybe because
317
- http.read_timeout = 60 # secs # Ruby's bug, but why?)
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
@@ -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
- require 'webrick/httpversion'
14
- require 'webrick/httpstatus'
15
- require 'webrick/httputils'
16
- require 'webrick/cookie'
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
- if @request_line.bytesize >= MAX_URI_LENGTH and @request_line[-1, 1] != LF
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
- data = read_data(socket, chunk_size) # read chunk-data
506
- if data.nil? || data.bytesize != chunk_size
507
- raise BadRequest, "bad chunk data size."
508
- end
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
@@ -10,10 +10,11 @@
10
10
  # $IPR: httpresponse.rb,v 1.45 2003/07/11 11:02:25 gotoyuzo Exp $
11
11
 
12
12
  require 'time'
13
- require 'webrick/httpversion'
14
- require 'webrick/htmlutils'
15
- require 'webrick/httputils'
16
- require 'webrick/httpstatus'
13
+ require 'uri'
14
+ require_relative 'httpversion'
15
+ require_relative 'htmlutils'
16
+ require_relative 'httputils'
17
+ require_relative 'httpstatus'
17
18
 
18
19
  module WEBrick
19
20
  ##
@@ -21,6 +22,8 @@ module WEBrick
21
22
  # WEBrick HTTP Servlet.
22
23
 
23
24
  class HTTPResponse
25
+ class InvalidHeader < StandardError
26
+ end
24
27
 
25
28
  ##
26
29
  # HTTP Response version
@@ -251,7 +254,7 @@ module WEBrick
251
254
  @header.delete('content-length')
252
255
  elsif @header['content-length'].nil?
253
256
  unless @body.is_a?(IO)
254
- @header['content-length'] = @body ? @body.bytesize : 0
257
+ @header['content-length'] = (@body ? @body.bytesize : 0).to_s
255
258
  end
256
259
  end
257
260
 
@@ -274,7 +277,7 @@ module WEBrick
274
277
  # Location is a single absoluteURI.
275
278
  if location = @header['location']
276
279
  if @request_uri
277
- @header['location'] = @request_uri.merge(location)
280
+ @header['location'] = @request_uri.merge(location).to_s
278
281
  end
279
282
  end
280
283
  end
@@ -287,14 +290,19 @@ module WEBrick
287
290
  data = status_line()
288
291
  @header.each{|key, value|
289
292
  tmp = key.gsub(/\bwww|^te$|\b\w/){ $&.upcase }
290
- data << "#{tmp}: #{value}" << CRLF
293
+ data << "#{tmp}: #{check_header(value)}" << CRLF
291
294
  }
292
295
  @cookies.each{|cookie|
293
- data << "Set-Cookie: " << cookie.to_s << CRLF
296
+ data << "Set-Cookie: " << check_header(cookie.to_s) << CRLF
294
297
  }
295
298
  data << CRLF
296
- _write_data(socket, data)
299
+ socket.write(data)
297
300
  end
301
+ rescue InvalidHeader => e
302
+ @header.clear
303
+ @cookies.clear
304
+ set_error e
305
+ retry
298
306
  end
299
307
 
300
308
  ##
@@ -324,8 +332,9 @@ module WEBrick
324
332
  # res.set_redirect WEBrick::HTTPStatus::TemporaryRedirect
325
333
 
326
334
  def set_redirect(status, url)
335
+ url = URI(url).to_s
327
336
  @body = "<HTML><A HREF=\"#{url}\">#{url}</A>.</HTML>\n"
328
- @header['location'] = url.to_s
337
+ @header['location'] = url
329
338
  raise status
330
339
  end
331
340
 
@@ -359,6 +368,15 @@ module WEBrick
359
368
 
360
369
  private
361
370
 
371
+ def check_header(header_value)
372
+ header_value = header_value.to_s
373
+ if /[\r\n]/ =~ header_value
374
+ raise InvalidHeader
375
+ else
376
+ header_value
377
+ end
378
+ end
379
+
362
380
  # :stopdoc:
363
381
 
364
382
  def error_body(backtrace, ex, host, port)
@@ -401,18 +419,29 @@ module WEBrick
401
419
  @body.readpartial(@buffer_size, buf)
402
420
  size = buf.bytesize
403
421
  data = "#{size.to_s(16)}#{CRLF}#{buf}#{CRLF}"
404
- _write_data(socket, data)
422
+ socket.write(data)
405
423
  data.clear
406
424
  @sent_size += size
407
425
  rescue EOFError
408
426
  break
409
427
  end while true
410
428
  buf.clear
411
- _write_data(socket, "0#{CRLF}#{CRLF}")
429
+ socket.write("0#{CRLF}#{CRLF}")
412
430
  else
413
- size = @header['content-length'].to_i
414
- _send_file(socket, @body, 0, size)
415
- @sent_size = size
431
+ if %r{\Abytes (\d+)-(\d+)/\d+\z} =~ @header['content-range']
432
+ offset = $1.to_i
433
+ size = $2.to_i - offset + 1
434
+ else
435
+ offset = nil
436
+ size = @header['content-length']
437
+ size = size.to_i if size
438
+ end
439
+ begin
440
+ @sent_size = IO.copy_stream(@body, socket, size, offset)
441
+ rescue NotImplementedError
442
+ @body.seek(offset, IO::SEEK_SET)
443
+ @sent_size = IO.copy_stream(@body, socket, size)
444
+ end
416
445
  end
417
446
  ensure
418
447
  @body.close
@@ -429,13 +458,13 @@ module WEBrick
429
458
  size = buf.bytesize
430
459
  data = "#{size.to_s(16)}#{CRLF}#{buf}#{CRLF}"
431
460
  buf.clear
432
- _write_data(socket, data)
461
+ socket.write(data)
433
462
  @sent_size += size
434
463
  end
435
- _write_data(socket, "0#{CRLF}#{CRLF}")
464
+ socket.write("0#{CRLF}#{CRLF}")
436
465
  else
437
466
  if @body && @body.bytesize > 0
438
- _write_data(socket, @body)
467
+ socket.write(@body)
439
468
  @sent_size = @body.bytesize
440
469
  end
441
470
  end
@@ -446,7 +475,7 @@ module WEBrick
446
475
  # do nothing
447
476
  elsif chunked?
448
477
  @body.call(ChunkedWrapper.new(socket, self))
449
- _write_data(socket, "0#{CRLF}#{CRLF}")
478
+ socket.write("0#{CRLF}#{CRLF}")
450
479
  else
451
480
  size = @header['content-length'].to_i
452
481
  @body.call(socket)
@@ -461,40 +490,25 @@ module WEBrick
461
490
  end
462
491
 
463
492
  def write(buf)
464
- return if buf.empty?
493
+ return 0 if buf.empty?
465
494
  socket = @socket
466
495
  @resp.instance_eval {
467
496
  size = buf.bytesize
468
497
  data = "#{size.to_s(16)}#{CRLF}#{buf}#{CRLF}"
469
- _write_data(socket, data)
498
+ socket.write(data)
470
499
  data.clear
471
500
  @sent_size += size
501
+ size
472
502
  }
473
503
  end
474
- alias :<< :write
475
- end
476
-
477
- def _send_file(output, input, offset, size)
478
- while offset > 0
479
- sz = @buffer_size < size ? @buffer_size : size
480
- buf = input.read(sz)
481
- offset -= buf.bytesize
482
- end
483
504
 
484
- if size == 0
485
- while buf = input.read(@buffer_size)
486
- _write_data(output, buf)
487
- end
488
- else
489
- while size > 0
490
- sz = @buffer_size < size ? @buffer_size : size
491
- buf = input.read(sz)
492
- _write_data(output, buf)
493
- size -= buf.bytesize
494
- end
505
+ def <<(*buf)
506
+ write(buf)
507
+ self
495
508
  end
496
509
  end
497
510
 
511
+ # preserved for compatibility with some 3rd-party handlers
498
512
  def _write_data(socket, data)
499
513
  socket << data
500
514
  end
@@ -9,8 +9,8 @@
9
9
  #
10
10
  # $IPR: https.rb,v 1.15 2003/07/22 19:20:42 gotoyuzo Exp $
11
11
 
12
- require 'webrick/ssl'
13
- require 'webrick/httpserver'
12
+ require_relative 'ssl'
13
+ require_relative 'httpserver'
14
14
 
15
15
  module WEBrick
16
16
  module Config
@@ -10,13 +10,13 @@
10
10
  # $IPR: httpserver.rb,v 1.63 2002/10/01 17:16:32 gotoyuzo Exp $
11
11
 
12
12
  require 'io/wait'
13
- require 'webrick/server'
14
- require 'webrick/httputils'
15
- require 'webrick/httpstatus'
16
- require 'webrick/httprequest'
17
- require 'webrick/httpresponse'
18
- require 'webrick/httpservlet'
19
- require 'webrick/accesslog'
13
+ require_relative 'server'
14
+ require_relative 'httputils'
15
+ require_relative 'httpstatus'
16
+ require_relative 'httprequest'
17
+ require_relative 'httpresponse'
18
+ require_relative 'httpservlet'
19
+ require_relative 'accesslog'
20
20
 
21
21
  module WEBrick
22
22
  class HTTPServerError < ServerError; end
@@ -68,8 +68,8 @@ module WEBrick
68
68
 
69
69
  def run(sock)
70
70
  while true
71
- res = HTTPResponse.new(@config)
72
- req = HTTPRequest.new(@config)
71
+ req = create_request(@config)
72
+ res = create_response(@config)
73
73
  server = self
74
74
  begin
75
75
  timeout = @config[:RequestTimeout]
@@ -224,6 +224,20 @@ module WEBrick
224
224
  }
225
225
  end
226
226
 
227
+ ##
228
+ # Creates the HTTPRequest used when handling the HTTP
229
+ # request. Can be overridden by subclasses.
230
+ def create_request(with_webrick_config)
231
+ HTTPRequest.new(with_webrick_config)
232
+ end
233
+
234
+ ##
235
+ # Creates the HTTPResponse used when handling the HTTP
236
+ # request. Can be overridden by subclasses.
237
+ def create_response(with_webrick_config)
238
+ HTTPResponse.new(with_webrick_config)
239
+ end
240
+
227
241
  ##
228
242
  # Mount table for the path a servlet is mounted on in the directory space
229
243
  # of the server. Users of WEBrick can only access this indirectly via
@@ -9,11 +9,11 @@
9
9
  #
10
10
  # $IPR: httpservlet.rb,v 1.21 2003/02/23 12:24:46 gotoyuzo Exp $
11
11
 
12
- require 'webrick/httpservlet/abstract'
13
- require 'webrick/httpservlet/filehandler'
14
- require 'webrick/httpservlet/cgihandler'
15
- require 'webrick/httpservlet/erbhandler'
16
- require 'webrick/httpservlet/prochandler'
12
+ require_relative 'httpservlet/abstract'
13
+ require_relative 'httpservlet/filehandler'
14
+ require_relative 'httpservlet/cgihandler'
15
+ require_relative 'httpservlet/erbhandler'
16
+ require_relative 'httpservlet/prochandler'
17
17
 
18
18
  module WEBrick
19
19
  module HTTPServlet
@@ -9,9 +9,9 @@
9
9
  #
10
10
  # $IPR: abstract.rb,v 1.24 2003/07/11 11:16:46 gotoyuzo Exp $
11
11
 
12
- require 'webrick/htmlutils'
13
- require 'webrick/httputils'
14
- require 'webrick/httpstatus'
12
+ require_relative '../htmlutils'
13
+ require_relative '../httputils'
14
+ require_relative '../httpstatus'
15
15
 
16
16
  module WEBrick
17
17
  module HTTPServlet
@@ -11,8 +11,8 @@
11
11
 
12
12
  require 'rbconfig'
13
13
  require 'tempfile'
14
- require 'webrick/config'
15
- require 'webrick/httpservlet/abstract'
14
+ require_relative '../config'
15
+ require_relative 'abstract'
16
16
 
17
17
  module WEBrick
18
18
  module HTTPServlet
@@ -65,9 +65,7 @@ module WEBrick
65
65
  cgi_in.write("%8d" % dump.bytesize)
66
66
  cgi_in.write(dump)
67
67
 
68
- if req.body and req.body.bytesize > 0
69
- cgi_in.write(req.body)
70
- end
68
+ req.body { |chunk| cgi_in.write(chunk) }
71
69
  ensure
72
70
  cgi_in.close
73
71
  status = $?.exitstatus
@@ -9,7 +9,7 @@
9
9
  #
10
10
  # $IPR: erbhandler.rb,v 1.25 2003/02/24 19:25:31 gotoyuzo Exp $
11
11
 
12
- require 'webrick/httpservlet/abstract.rb'
12
+ require_relative 'abstract'
13
13
 
14
14
  require 'erb'
15
15
 
@@ -11,9 +11,9 @@
11
11
 
12
12
  require 'time'
13
13
 
14
- require 'webrick/htmlutils'
15
- require 'webrick/httputils'
16
- require 'webrick/httpstatus'
14
+ require_relative '../htmlutils'
15
+ require_relative '../httputils'
16
+ require_relative '../httpstatus'
17
17
 
18
18
  module WEBrick
19
19
  module HTTPServlet
@@ -55,7 +55,7 @@ module WEBrick
55
55
  else
56
56
  mtype = HTTPUtils::mime_type(@local_path, @config[:MimeTypes])
57
57
  res['content-type'] = mtype
58
- res['content-length'] = st.size
58
+ res['content-length'] = st.size.to_s
59
59
  res['last-modified'] = mtime.httpdate
60
60
  res.body = File.open(@local_path, "rb")
61
61
  end
@@ -86,6 +86,35 @@ module WEBrick
86
86
  return false
87
87
  end
88
88
 
89
+ # returns a lambda for webrick/httpresponse.rb send_body_proc
90
+ def multipart_body(body, parts, boundary, mtype, filesize)
91
+ lambda do |socket|
92
+ begin
93
+ begin
94
+ first = parts.shift
95
+ last = parts.shift
96
+ socket.write(
97
+ "--#{boundary}#{CRLF}" \
98
+ "Content-Type: #{mtype}#{CRLF}" \
99
+ "Content-Range: bytes #{first}-#{last}/#{filesize}#{CRLF}" \
100
+ "#{CRLF}"
101
+ )
102
+
103
+ begin
104
+ IO.copy_stream(body, socket, last - first + 1, first)
105
+ rescue NotImplementedError
106
+ body.seek(first, IO::SEEK_SET)
107
+ IO.copy_stream(body, socket, last - first + 1)
108
+ end
109
+ socket.write(CRLF)
110
+ end while parts[0]
111
+ socket.write("--#{boundary}--#{CRLF}")
112
+ ensure
113
+ body.close
114
+ end
115
+ end
116
+ end
117
+
89
118
  def make_partial_content(req, res, filename, filesize)
90
119
  mtype = HTTPUtils::mime_type(filename, @config[:MimeTypes])
91
120
  unless ranges = HTTPUtils::parse_range_header(req['range'])
@@ -96,37 +125,27 @@ module WEBrick
96
125
  if ranges.size > 1
97
126
  time = Time.now
98
127
  boundary = "#{time.sec}_#{time.usec}_#{Process::pid}"
99
- body = ''
100
- ranges.each{|range|
101
- first, last = prepare_range(range, filesize)
102
- next if first < 0
103
- io.pos = first
104
- content = io.read(last-first+1)
105
- body << "--" << boundary << CRLF
106
- body << "Content-Type: #{mtype}" << CRLF
107
- body << "Content-Range: bytes #{first}-#{last}/#{filesize}" << CRLF
108
- body << CRLF
109
- body << content
110
- body << CRLF
128
+ parts = []
129
+ ranges.each {|range|
130
+ prange = prepare_range(range, filesize)
131
+ next if prange[0] < 0
132
+ parts.concat(prange)
111
133
  }
112
- raise HTTPStatus::RequestRangeNotSatisfiable if body.empty?
113
- body << "--" << boundary << "--" << CRLF
134
+ raise HTTPStatus::RequestRangeNotSatisfiable if parts.empty?
114
135
  res["content-type"] = "multipart/byteranges; boundary=#{boundary}"
115
- res.body = body
136
+ if req.http_version < '1.1'
137
+ res['connection'] = 'close'
138
+ else
139
+ res.chunked = true
140
+ end
141
+ res.body = multipart_body(io.dup, parts, boundary, mtype, filesize)
116
142
  elsif range = ranges[0]
117
143
  first, last = prepare_range(range, filesize)
118
144
  raise HTTPStatus::RequestRangeNotSatisfiable if first < 0
119
- if last == filesize - 1
120
- content = io.dup
121
- content.pos = first
122
- else
123
- io.pos = first
124
- content = io.read(last-first+1)
125
- end
126
145
  res['content-type'] = mtype
127
146
  res['content-range'] = "bytes #{first}-#{last}/#{filesize}"
128
- res['content-length'] = last - first + 1
129
- res.body = content
147
+ res['content-length'] = (last - first + 1).to_s
148
+ res.body = io.dup
130
149
  else
131
150
  raise HTTPStatus::BadRequest
132
151
  end
@@ -9,7 +9,7 @@
9
9
  #
10
10
  # $IPR: prochandler.rb,v 1.7 2002/09/21 12:23:42 gotoyuzo Exp $
11
11
 
12
- require 'webrick/httpservlet/abstract.rb'
12
+ require_relative 'abstract'
13
13
 
14
14
  module WEBrick
15
15
  module HTTPServlet
@@ -9,7 +9,7 @@
9
9
  #
10
10
  # $IPR: httpstatus.rb,v 1.11 2003/03/24 20:18:55 gotoyuzo Exp $
11
11
 
12
- require 'webrick/accesslog'
12
+ require_relative 'accesslog'
13
13
 
14
14
  module WEBrick
15
15
 
@@ -10,8 +10,8 @@
10
10
  # $IPR: server.rb,v 1.62 2003/07/22 19:20:43 gotoyuzo Exp $
11
11
 
12
12
  require 'socket'
13
- require 'webrick/config'
14
- require 'webrick/log'
13
+ require_relative 'config'
14
+ require_relative 'log'
15
15
 
16
16
  module WEBrick
17
17
 
@@ -130,7 +130,7 @@ module WEBrick
130
130
  aki = ef.create_extension("authorityKeyIdentifier",
131
131
  "keyid:always,issuer:always")
132
132
  cert.add_extension(aki)
133
- cert.sign(rsa, OpenSSL::Digest::SHA1.new)
133
+ cert.sign(rsa, OpenSSL::Digest::SHA256.new)
134
134
 
135
135
  return [ cert, rsa ]
136
136
  end
@@ -181,7 +181,7 @@ module WEBrick
181
181
  unless config[:SSLCertificate]
182
182
  cn = config[:SSLCertName]
183
183
  comment = config[:SSLCertComment]
184
- cert, key = Utils::create_self_signed_cert(1024, cn, comment)
184
+ cert, key = Utils::create_self_signed_cert(2048, cn, comment)
185
185
  config[:SSLCertificate] = cert
186
186
  config[:SSLPrivateKey] = key
187
187
  end
@@ -14,5 +14,5 @@ module WEBrick
14
14
  ##
15
15
  # The WEBrick version
16
16
 
17
- VERSION = "1.4.2"
17
+ VERSION = "1.4.3"
18
18
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webrick
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - TAKAHASHI Masayoshi
8
8
  - GOTOU YUUZOU
9
9
  - Eric Wong
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-12-24 00:00:00.000000000 Z
13
+ date: 2020-08-31 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -29,8 +29,8 @@ dependencies:
29
29
  description: WEBrick is an HTTP server toolkit that can be configured as an HTTPS
30
30
  server, a proxy server, and a virtual-host server.
31
31
  email:
32
- -
33
- -
32
+ -
33
+ -
34
34
  - normal@ruby-lang.org
35
35
  executables: []
36
36
  extensions: []
@@ -78,7 +78,7 @@ metadata:
78
78
  bug_tracker_uri: https://bugs.ruby-lang.org/projects/ruby-trunk/issues
79
79
  homepage_uri: https://www.ruby-lang.org
80
80
  source_code_uri: https://svn.ruby-lang.org/repos/ruby
81
- post_install_message:
81
+ post_install_message:
82
82
  rdoc_options: []
83
83
  require_paths:
84
84
  - lib
@@ -93,9 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubyforge_project:
97
- rubygems_version: 2.7.3
98
- signing_key:
96
+ rubygems_version: 3.1.2
97
+ signing_key:
99
98
  specification_version: 4
100
99
  summary: HTTP server toolkit
101
100
  test_files: []