Class: DevelopmentServer

Inherits:
Object
  • Object
show all
Defined in:
lib/yodel/middleware/development_server.rb

Defined Under Namespace

Classes: Message

Constant Summary collapse

SEPARATORS =
Regexp.union(*[::File::SEPARATOR, ::File::ALT_SEPARATOR].compact)

Instance Method Summary collapse

Constructor Details

#initializeDevelopmentServer

Returns a new instance of DevelopmentServer.



77
78
79
80
81
82
83
84
85
# File 'lib/yodel/middleware/development_server.rb', line 77

def initialize
  spawn_server
  @mutex = Mutex.new
  @pid = nil
  Signal.trap("SIGTERM") do
    self.kill_child
    Process.kill("SIGINT", Process.pid)
  end
end

Instance Method Details

#call(env) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/yodel/middleware/development_server.rb', line 96

def call(env)
  @mutex.lock
  parts = Rack::Utils.unescape(env["PATH_INFO"]).split(SEPARATORS)
  return [403, {"Content-Type" => "text/plain"}, "Forbidden"] if parts.include? ".."
  
  # pass the request through to a yodel server. if the server
  # responds with a restart message (yodel source files have
  # changed and need to be re-loaded) spawn a new server and
  # write the request again (assume the request is successful)
  request = Message.new(:request)
  request.env = env
  request.write(@client_socket)
  response = Message.read(@client_socket)
  
  if response.restart?
    spawn_server
    request.write(@client_socket)
    response = Message.read(@client_socket)
  end
  
  # response data is a valid rack response
  response.data
ensure
  @mutex.unlock
end

#kill_childObject



87
88
89
90
91
92
93
94
# File 'lib/yodel/middleware/development_server.rb', line 87

def kill_child
  begin
    Process.kill("SIGTERM", @pid) if @pid
    @pid = nil
    @client_socket.close
  rescue
  end
end