webrick 1.4.1 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -23,11 +23,11 @@ STDIN.binmode
23
23
 
24
24
  len = sysread(STDIN, 8).to_i
25
25
  out = sysread(STDIN, len)
26
- STDOUT.reopen(open(out, "w"))
26
+ STDOUT.reopen(File.open(out, "w"))
27
27
 
28
28
  len = sysread(STDIN, 8).to_i
29
29
  err = sysread(STDIN, len)
30
- STDERR.reopen(open(err, "w"))
30
+ STDERR.reopen(File.open(err, "w"))
31
31
 
32
32
  len = sysread(STDIN, 8).to_i
33
33
  dump = sysread(STDIN, len)
@@ -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
@@ -28,6 +28,7 @@ module WEBrick
28
28
  class CGIHandler < AbstractServlet
29
29
  Ruby = RbConfig.ruby # :nodoc:
30
30
  CGIRunner = "\"#{Ruby}\" \"#{WEBrick::Config::LIBDIR}/httpservlet/cgi_runner.rb\"" # :nodoc:
31
+ CGIRunnerArray = [Ruby, "#{WEBrick::Config::LIBDIR}/httpservlet/cgi_runner.rb".freeze].freeze # :nodoc:
31
32
 
32
33
  ##
33
34
  # Creates a new CGI script servlet for the script at +name+
@@ -36,7 +37,12 @@ module WEBrick
36
37
  super(server, name)
37
38
  @script_filename = name
38
39
  @tempdir = server[:TempDir]
39
- @cgicmd = "#{CGIRunner} #{server[:CGIInterpreter]}"
40
+ interpreter = server[:CGIInterpreter]
41
+ if interpreter.is_a?(Array)
42
+ @cgicmd = CGIRunnerArray + interpreter
43
+ else
44
+ @cgicmd = "#{CGIRunner} #{interpreter}"
45
+ end
40
46
  end
41
47
 
42
48
  # :stopdoc:
@@ -65,9 +71,7 @@ module WEBrick
65
71
  cgi_in.write("%8d" % dump.bytesize)
66
72
  cgi_in.write(dump)
67
73
 
68
- if req.body and req.body.bytesize > 0
69
- cgi_in.write(req.body)
70
- end
74
+ req.body { |chunk| cgi_in.write(chunk) }
71
75
  ensure
72
76
  cgi_in.close
73
77
  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
 
@@ -53,7 +53,7 @@ module WEBrick
53
53
  raise HTTPStatus::Forbidden, "ERBHandler cannot work."
54
54
  end
55
55
  begin
56
- data = open(@script_filename){|io| io.read }
56
+ data = File.open(@script_filename, &:read)
57
57
  res.body = evaluate(ERB.new(data), req, res)
58
58
  res['content-type'] ||=
59
59
  HTTPUtils::mime_type(@script_filename, @config[:MimeTypes])
@@ -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,9 +55,9 @@ 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
- res.body = open(@local_path, "rb")
60
+ res.body = File.open(@local_path, "rb")
61
61
  end
62
62
  end
63
63
 
@@ -86,47 +86,66 @@ 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'])
92
121
  raise HTTPStatus::BadRequest,
93
122
  "Unrecognized range-spec: \"#{req['range']}\""
94
123
  end
95
- open(filename, "rb"){|io|
124
+ File.open(filename, "rb"){|io|
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
 
@@ -95,6 +95,7 @@ module WEBrick
95
95
  "tif" => "image/tiff",
96
96
  "tiff" => "image/tiff",
97
97
  "txt" => "text/plain",
98
+ "wasm" => "application/wasm",
98
99
  "xbm" => "image/x-xbitmap",
99
100
  "xhtml" => "text/html",
100
101
  "xls" => "application/vnd.ms-excel",
@@ -108,6 +109,8 @@ module WEBrick
108
109
  # Loads Apache-compatible mime.types in +file+.
109
110
 
110
111
  def load_mime_types(file)
112
+ # note: +file+ may be a "| command" for now; some people may
113
+ # rely on this, but currently we do not use this method by default.
111
114
  open(file){ |io|
112
115
  hash = Hash.new
113
116
  io.each{ |line|
@@ -159,10 +162,7 @@ module WEBrick
159
162
  end
160
163
  }
161
164
  header.each{|key, values|
162
- values.each{|value|
163
- value.strip!
164
- value.gsub!(/\s+/, " ")
165
- }
165
+ values.each(&:strip!)
166
166
  }
167
167
  header
168
168
  end
@@ -51,7 +51,7 @@ module WEBrick
51
51
  @level = level || INFO
52
52
  case log_file
53
53
  when String
54
- @log = open(log_file, "a+")
54
+ @log = File.open(log_file, "a+")
55
55
  @log.sync = true
56
56
  @opened = true
57
57
  when NilClass
@@ -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.1"
17
+ VERSION = "1.6.1"
18
18
  end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+ begin
3
+ require_relative 'lib/webrick/version'
4
+ rescue LoadError
5
+ # for Ruby core repository
6
+ require_relative 'version'
7
+ end
8
+
9
+ Gem::Specification.new do |s|
10
+ s.name = "webrick"
11
+ s.version = WEBrick::VERSION
12
+ s.summary = "HTTP server toolkit"
13
+ s.description = "WEBrick is an HTTP server toolkit that can be configured as an HTTPS server, a proxy server, and a virtual-host server."
14
+
15
+ s.require_path = %w{lib}
16
+ s.files = [
17
+ "Gemfile",
18
+ "LICENSE.txt",
19
+ "README.md",
20
+ "Rakefile",
21
+ "bin/console",
22
+ "bin/setup",
23
+ "lib/webrick.rb",
24
+ "lib/webrick/accesslog.rb",
25
+ "lib/webrick/cgi.rb",
26
+ "lib/webrick/compat.rb",
27
+ "lib/webrick/config.rb",
28
+ "lib/webrick/cookie.rb",
29
+ "lib/webrick/htmlutils.rb",
30
+ "lib/webrick/httpauth.rb",
31
+ "lib/webrick/httpauth/authenticator.rb",
32
+ "lib/webrick/httpauth/basicauth.rb",
33
+ "lib/webrick/httpauth/digestauth.rb",
34
+ "lib/webrick/httpauth/htdigest.rb",
35
+ "lib/webrick/httpauth/htgroup.rb",
36
+ "lib/webrick/httpauth/htpasswd.rb",
37
+ "lib/webrick/httpauth/userdb.rb",
38
+ "lib/webrick/httpproxy.rb",
39
+ "lib/webrick/httprequest.rb",
40
+ "lib/webrick/httpresponse.rb",
41
+ "lib/webrick/https.rb",
42
+ "lib/webrick/httpserver.rb",
43
+ "lib/webrick/httpservlet.rb",
44
+ "lib/webrick/httpservlet/abstract.rb",
45
+ "lib/webrick/httpservlet/cgi_runner.rb",
46
+ "lib/webrick/httpservlet/cgihandler.rb",
47
+ "lib/webrick/httpservlet/erbhandler.rb",
48
+ "lib/webrick/httpservlet/filehandler.rb",
49
+ "lib/webrick/httpservlet/prochandler.rb",
50
+ "lib/webrick/httpstatus.rb",
51
+ "lib/webrick/httputils.rb",
52
+ "lib/webrick/httpversion.rb",
53
+ "lib/webrick/log.rb",
54
+ "lib/webrick/server.rb",
55
+ "lib/webrick/ssl.rb",
56
+ "lib/webrick/utils.rb",
57
+ "lib/webrick/version.rb",
58
+ "webrick.gemspec",
59
+ ]
60
+ s.required_ruby_version = ">= 2.3.0"
61
+
62
+ s.authors = ["TAKAHASHI Masayoshi", "GOTOU YUUZOU", "Eric Wong"]
63
+ s.email = [nil, nil, 'normal@ruby-lang.org']
64
+ s.homepage = "https://www.ruby-lang.org"
65
+ s.license = "BSD-2-Clause"
66
+
67
+ if s.respond_to?(:metadata=)
68
+ s.metadata = {
69
+ "bug_tracker_uri" => "https://bugs.ruby-lang.org/projects/ruby-trunk/issues",
70
+ "homepage_uri" => "https://www.ruby-lang.org",
71
+ "source_code_uri" => "https://git.ruby-lang.org/ruby.git/"
72
+ }
73
+ end
74
+
75
+ s.add_development_dependency "rake"
76
+ end