example/upgrade_server.rb in http-2-0.8.0 vs example/upgrade_server.rb in http-2-0.8.1

- old
+ new

@@ -26,60 +26,62 @@ server = OpenSSL::SSL::SSLServer.new(server, ctx) end class UpgradeHandler + VALID_UPGRADE_METHODS = %w(GET OPTIONS) + UPGRADE_RESPONSE = <<-RESP +HTTP/1.1 101 Switching Protocols +Connection: Upgrade +Upgrade: h2c - VALID_UPGRADE_METHODS = %w[GET OPTIONS] - UPGRADE_RESPONSE = ("HTTP/1.1 101 Switching Protocols\n" + - "Connection: Upgrade\n" + - "Upgrade: h2c\n\n").freeze +RESP attr_reader :complete, :headers, :body, :parsing - def initialize conn, sock + def initialize(conn, sock) @conn, @sock = conn, sock @complete, @parsing = false, false @body = '' @parser = ::HTTP::Parser.new(self) end def <<(data) @parsing ||= true @parser << data - if complete + return unless complete - @sock.write UPGRADE_RESPONSE + @sock.write UPGRADE_RESPONSE - settings = headers['http2-settings'] - request = { - ':scheme' => 'http', - ':method' => @parser.http_method, - ':authority' => headers['Host'], - ':path' => @parser.request_url - }.merge(headers) + settings = headers['http2-settings'] + request = { + ':scheme' => 'http', + ':method' => @parser.http_method, + ':authority' => headers['Host'], + ':path' => @parser.request_url, + }.merge(headers) - @conn.upgrade(settings, request, @body) - end + @conn.upgrade(settings, request, @body) end - def complete!; @complete = true; end + def complete! + @complete = true + end def on_headers_complete(headers) @headers = headers end def on_body(chunk) @body << chunk end def on_message_complete - raise unless VALID_UPGRADE_METHODS.include?(@parser.http_method) + fail unless VALID_UPGRADE_METHODS.include?(@parser.http_method) @parsing = false complete! end - end loop do sock = server.accept puts 'New TCP connection!' @@ -120,10 +122,10 @@ if req['Upgrade'] log.info "Processing h2c Upgrade request: #{req}" # Don't respond to OPTIONS... - if req[':method'] != "OPTIONS" + if req[':method'] != 'OPTIONS' response = 'Hello h2c world!' stream.headers({ ':status' => '200', 'content-length' => response.bytesize.to_s, 'content-type' => 'text/plain',