diff -ru webrick-1.1.5/lib/webrick/httpserver
.rb mywebrick/lib/webrick/httpserver.rb
---
webrick-1.1.5/lib/webrick/httpserver.rb Thu F
eb 14 16:10:34 2002
+++ mywebrick/lib/webric
k/httpserver.rb Sat Mar 23 15:15:14 2002
@@
-30,35 +30,36 @@
def run(sock)
peeraddr = sock.peeraddr[3]
- while
true
- res = HTTPResponse.new(@confi
g)
- req = HTTPRequest.new(@config)
- request_line = nil
- begin
- req.parse(sock)
- servic
e(req, res)
- rescue HTTPStatus::EOFE
rror
- break
- rescue HTTPS
tatus::RequestTimeout => ex
- acces
s_log(sock, req, res, ex)
- break
- rescue HTTPStatus::Error => ex
-
res.set_error(ex)
- rescue HTT
PStatus::Status => ex
- res.status
= ex.code
- rescue StandardError, Nam
eError => ex # for Ruby 1.6
- @logg
er.error(ex)
- res.set_error(ex, tr
ue)
- end
- if req.request_me
thod == "HEAD"
- res.send_header(so
ck)
- else
- res.send_respo
nse(sock)
- end
- access_log(
sock, req, res)
- break unless req.ke
ep_alive?
- break unless res.keep_ali
ve?
+
+ res = HTTPResponse.new(@config
)
+ req = HTTPRequest.new(@config)
+
request_line = nil
+ begin
+ req.p
arse(sock)
+ service(req, res)
+ rescu
e HTTPStatus::EOFError
+ return false
+
rescue HTTPStatus::RequestTimeout => ex
+
access_log(sock, req, res, ex)
+ return fals
e
+ rescue HTTPStatus::Error => ex
+ r
es.set_error(ex)
+ rescue HTTPStatus::S
tatus => ex
+ res.status = ex.code
+ r
escue StandardError, NameError => ex # for Ru
by 1.6
+ @logger.error(ex)
+ res.set_error(
ex, true)
end
+ if req.request_method == "HEAD"
+ res.send_header(sock)
+
else
+ res.send_response(sock)
+
end
+ access_log(sock, req, res)
+
+
return false unless req.keep_alive?
+
return false unless res.keep_alive?
+
return true
end
def service(req, res)
diff -ru webrick-1.1.5/lib/webric
k/server.rb mywebrick/lib/webrick/server.rb
--- webrick-1.1.5/lib/webrick/server.rb Thu F
eb 14 11:01:01 2002
+++ mywebrick/lib/webric
k/server.rb Sun Mar 24 13:37:53 2002
@@ -69,
6 +69,9 @@
raise last_error if @listeners.empty?
end
+ StopThread = 0
+ NotifyThread = 1
+
def start
raise ServerError, "already started." i
f status != :Stop
server_type = @config[:ServerType] || SimpleServer
@@ -77,6 +80
,8 @@
server_type.start{
thgroup = Array.new
queue = SizedQueue.new(start_threads)
+ @rqueue = Queue.ne
w
+ sockets = Array.new
@logger.info "#{self.type}#start: pid=#{$$} port=#{@config[:Port]}"
start_hook()
@@ -85,19 +90,27 @@
th = Thread.start(i){ |j|
@logger.info "thread(#{j}) start."
loop do
- sock = queue.pop
+ action,
sock = queue.pop
case sock
when IPSocket
begin
- addr = sock.peera
ddr
- @logger.debug "accept
: #{addr[3]}:#{addr[1]}"
- accept_hook(sock)
- block_g
iven? ? yield(sock) : run(sock)
+ addr =
sock.peeraddr
+ case action
+ when :a
ccept
+ @logger.debug "accept: #{addr[3]}:#{addr[1]}"
+ accept_hook(sock)
+ end
+ continue = (block_given? ? yield(
sock) : run(sock))
rescue Exception => ex
@logger.error ex
ensure
- @logger.debug "close: #{addr[3]}:#{addr[1]}"
- sock.close
+ i
f continue
+ @rqueue.push sock
+
@sockets_w.write(NotifyThread.chr)
+ else
+ @logger.debug "close: #{addr[3]}:#{addr[1]}"
+ sock.close
+ end
end
else
@logger.info "thread(#{j}) exit."
@@ -108,44 +121,72 @@
thgroup.push(th)
}
+ listener_r, @listener_w = IO.pipe
+
+ @listener_thread = Thread.s
tart {
+ loop {
+ if socks = IO.select([listener_r]+@listeners, nil, nil)
+
socks[0].each { |s|
+ if s == listener_r
+ case (listener_r.read 1)[0]
+ when S
topThread
+ @listeners.each {|s| s.close }
+ @listeners.clear
+ Thread.e
xit
+ end
+ else
+ ns = s.accept
+
ns.sync = true
+ queue.push([:accept,
ns])
+ end
+ }
+ end
+ }
+
}
+
+ sockets_r, @sockets_w = IO.pipe
+
@status = :Running
while @status == :Running
- begin
-
if socks = IO.select(@listeners, nil, n
il, 2.0)
- socks[0].each{|s|
- ns = s.accept
-
ns.sync = true
- queue.pus
h(ns)
- }
- end
-
rescue Errno::ECONNRESET, Errno::ECO
NNABORTED => ex
- msg = "#{ex.type}: #{ex.message}\n\t#{ex.backtrace[0]}"
- @logger.error msg
- rescu
e => ex
- @logger.error ex
-
break
- end
- end
+ if socks = IO.select([sockets_r]+sockets,
nil, nil)
+ socks[0].each { |s|
+ if s == sockets_r
+ case (sockets_r.read
1)[0]
+ when NotifyThread
+ sockets.pus
h(@rqueue.pop)
+ when StopThread
+ # do
nothing, will exit outer loop
+ end
+
else
+ sockets.delete s
+ queue.push([
:data, s])
+ end
+ }
+ end
+
end
@logger.info "going to shutdown ..."
- start_threads.times{ queue.push(nil) }
+ start_threads.times{ queue.push([:exit, nil]) }
thgroup.each{|th| th.join }
stop_hook()
@logger.info "#{self.type}#start done."
@status = :Stop
- }
+ }
end
def stop
if @status == :Running
@status = :Shutdown
+ @listener_w.write(StopThread.chr)
+ @liste
ner_thread.join
+ @sockets_w.write(StopThrea
d.chr)
+ # @sockets_thread.join
+ @rqueue.p
ush nil
end
end
def shutdown
stop
- @listeners.each{|s| s.close }
- @listeners.clear
end
def run(sock)