Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed blocking bug in TCP server #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rocs/HTTPServ.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/local/bin/ruby
require 'open3'
require 'json'
require './tcpserver'
Expand Down
3 changes: 3 additions & 0 deletions rocs/HttpRequest/HttpRequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def initialize(request_str)
# spoof a socket read on the request string
request = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
socket_spoof = StringIO.open(request_str)
#puts "request_str"
#puts request_str
#puts "end"
request.parse(socket_spoof)
# grab HTTP request parameters
@type = request.request_method
Expand Down
41 changes: 25 additions & 16 deletions rocs/tcpserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,31 @@ def start
@status = :Running
while @status == :Running
begin
puts "listening"
puts "have client"
client_sock = @sock.accept
if client_sock
puts "#{client_sock}"
puts "some client is coming"

request = client_sock.gets
puts "#{request}"
response = request
response = @http.process(request)
@logger.info(response.to_s)
write_data(client_sock, response)

client_sock.close
end
puts "listening"
puts "have client"
client_sock = @sock.accept
if client_sock
puts "#{client_sock}"
puts "some client is coming"
all_data = []
while partial_data = client_sock.gets and partial_data !~ /^\s*$/m
#puts partial_data
all_data << partial_data
end
print "done"
#socket.close
request = all_data.join()
request =~/^Content-Length:\s*(\d*)/
length = $1
length = length.to_i
#puts "length is #{length}\n\n\n"
request = request + "\n" + client_sock.read(length)
response = @http.process(request)
@logger.info(response.to_s)
write_data(client_sock, response)

client_sock.close
end

rescue Errno::EBADF, IOError => ex
# if the listening socket was closed in TcpServer#shutdown,
Expand Down