aboutsummaryrefslogtreecommitdiffstats
path: root/lib/plum/server/http_connection.rb
blob: 8f91ca44bffca23a88de6a31c99dcab8c3044e93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
# -*- frozen-string-literal: true -*-
using Plum::BinaryString

module Plum
  class HTTPServerConnection < ServerConnection
    attr_reader :sock

    def initialize(sock, local_settings = {})
      require "http/parser"
      @_headers = nil
      @_body = String.new
      @_http_parser = setup_parser
      @sock = sock
      super(@sock.method(:write), local_settings)
    end

    # Closes the socket.
    def close
      super
      @sock.close
    end

    private
    def negotiate!
      super
    rescue RemoteConnectionError
      # Upgrade from HTTP/1.1
      offset = @_http_parser << @buffer
      @buffer.byteshift(offset)
    end

    def setup_parser
      parser = HTTP::Parser.new
      parser.on_headers_complete = proc {|_headers|
        @_headers = _headers.map {|n, v| [n.downcase, v] }.to_h
      }
      parser.on_body = proc {|chunk| @_body << chunk }
      parser.on_message_complete = proc {|env|
        connection = @_headers["connection"] || ""
        upgrade = @_headers["upgrade"] || ""
        settings = @_headers["http2-settings"]

        if (connection.split(", ").sort == ["Upgrade", "HTTP2-Settings"].sort &&
            upgrade.split(", ").include?("h2c") &&
            settings != nil)
          switch_protocol(settings)
        else
          raise LegacyHTTPError.new(@_headers, @_body, parser)
        end
      }

      parser
    end

    def switch_protocol(settings)
      self.on(:negotiated) {
        _frame = Frame.new(type: :settings, stream_id: 0, payload: Base64.urlsafe_decode64(settings))
        receive_settings(_frame, send_ack: false) # HTTP2-Settings
        process_first_request
      }

      resp = "HTTP/1.1 101 Switching Protocols\r\n" +
             "Connection: Upgrade\r\n" +
             "Upgrade: h2c\r\n" +
             "Server: plum/#{Plum::VERSION}\r\n" +
             "\r\n"

      @sock.write(resp)
    end

    def process_first_request
      encoder = HPACK::Encoder.new(0, indexing: false) # don't pollute connection's HPACK context
      stream = stream(1)
      max_frame_size = local_settings[:max_frame_size]
      headers = @_headers.merge({ ":method" => @_http_parser.http_method,
                                  ":path" => @_http_parser.request_url,
                                  ":authority" => @_headers["host"] })
                         .reject {|n, v| ["connection", "http2-settings", "upgrade", "host"].include?(n) }

      stream.receive_frame Frame.headers(1, encoder.encode(headers), end_headers: true)
      stream.receive_frame Frame.data(1, @_body, end_stream: true)
    end
  end
end