Class: Rakie::HttpServer

Inherits:
Object
  • Object
show all
Defined in:
lib/rakie/http_server.rb

Defined Under Namespace

Classes: Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: '127.0.0.1', port: 10086, delegate: nil) ⇒ HttpServer

Returns a new instance of HttpServer.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rakie/http_server.rb', line 18

def initialize(host: '127.0.0.1', port: 10086, delegate: nil)
  @host = host
  @port = port

  @channel = TCPServerChannel.new(host, port, self)

  # @type [Hash{Channel=>Session}]
  @sessions = {}
  @delegate = delegate
  @opt = {}
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



16
17
18
# File 'lib/rakie/http_server.rb', line 16

def channel
  @channel
end

#hostObject (readonly)

Returns the value of attribute host.



16
17
18
# File 'lib/rakie/http_server.rb', line 16

def host
  @host
end

#optObject (readonly)

Returns the value of attribute opt.



16
17
18
# File 'lib/rakie/http_server.rb', line 16

def opt
  @opt
end

#portObject (readonly)

Returns the value of attribute port.



16
17
18
# File 'lib/rakie/http_server.rb', line 16

def port
  @port
end

Instance Method Details

#on_accept(channel) ⇒ Object



30
31
32
33
34
# File 'lib/rakie/http_server.rb', line 30

def on_accept(channel)
  channel.delegate = self
  @sessions[channel] = Session.new
  Log.debug("Rakie::HTTPServer accept client: #{channel}")
end

#on_close(channel) ⇒ Object



130
131
132
# File 'lib/rakie/http_server.rb', line 130

def on_close(channel)
  @sessions.delete(channel)
end

#on_recv(channel, data) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rakie/http_server.rb', line 36

def on_recv(channel, data)
  # Log.debug("Rakie::HTTPServer recv: #{data}")
  session = @sessions[channel]

  if session == nil
    return 0
  end

  # @type [HttpRequest] request
  request = session.request

  if request.parse_status == ParseStatus::COMPLETE
    request = HttpRequest.new
    session.request = request
  end

  len = request.parse(data)

  Log.debug("Rakie::HttpServer receive request: #{request.to_s}")

  if request.parse_status == ParseStatus::COMPLETE
    response = HttpResponse.new

    if upgrade = request.headers["upgrade"]
      if websocket_delegate = @opt[:websocket_delegate]
        websocket_delegate.upgrade(request, response)
        Log.debug("Rakie::HttpServer upgrade protocol")
      end

    elsif @delegate != nil
      @delegate.handle(request, response)

    else
      response.headers["content-type"] = HttpMIME::HTML
      response.content = "<html><body><h1>Rakie!</h1></body></html>"
    end
    
    if header_connection = request.headers["connection"]
      response.headers["connection"] = header_connection
    end

    if response.content.length > 0
      response.headers["content-length"] = response.content.length
    end

    response.headers["server"] = Rakie.full_version_s
    session.responses << response
    response_data = response.to_s

    Log.debug("Rakie::HttpServer response: #{response_data}")

    channel.write(response_data) # Response data

  elsif request.parse_status == ParseStatus::ERROR
    channel.close
    @sessions.delete(channel)

    Log.debug("Rakie::HttpServer: Illegal request")
    return len
  end

  return len
end

#on_send(channel) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rakie/http_server.rb', line 100

def on_send(channel)
  session = @sessions[channel]
  # @type [HttpRequest]
  last_response = session.responses.shift

  if last_response
    if connect_status = last_response.headers["connection"]
      if connect_status.downcase == "close"
        Log.debug("Rakie::HttpServer: send finish and close channel")
        channel.close
        @sessions.delete(channel)
      end
    end

    if upgrade = last_response.headers["upgrade"]
      websocket_delegate = @opt[:websocket_delegate]

      if websocket_delegate
        websocket_delegate.on_accept(channel)
        @sessions.delete(channel)
        return
      end

      Log.debug("Rakie::HttpServer: no websocket delegate and close channel")
      channel.close
      @sessions.delete(channel)
    end
  end
end