Class: AppHttp

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

Instance Method Summary collapse

Constructor Details

#initialize(app, host: '0.0.0.0', port: '9232', filepath: '.', headings: true, debug: false) ⇒ AppHttp

Returns a new instance of AppHttp.



14
15
16
17
18
19
20
21
22
# File 'lib/apphttp.rb', line 14

def initialize(app, host: '0.0.0.0', port: '9232', filepath: '.', 
               headings: true, debug: false)
  
  @app, @host, @port, @debug = app, host, port, debug
  @filepath, @headings = filepath, headings
  @ah = AppHtmlLayer.new(app, filepath: filepath, headings: headings, 
                    debug: debug)
  
end

Instance Method Details

#startObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/apphttp.rb', line 24

def start

  server = TCPServer.new(@host, @port)

  while (session = server.accept)


    raw_request = session.gets
    request = raw_request[/.[^\s]+(?= HTTP\/1\.\d)/].strip

    puts ('request: ' + request.inspect).debug if @debug
    result, content_type = get(request)
    puts ('content_type: ' + content_type.inspect).debug if @debug

    if result then
      response = result
    else
      response = "404: page not found"
      content_type = 'text/plain'
    end

    session.print "HTTP/1.1 200 OK\r\nContent-type: #{content_type}\r\n" + 
              "Content-Length: #{response.bytesize}\r\n" +
              "Connection: close\r\n"
    session.print "\r\n"
    session.print response
    session.close

  end
end