pax_global_header00006660000000000000000000000064141110647350014514gustar00rootroot0000000000000052 comment=73f2459b0f188366ad024a8299426a1c561742cf ruby-protocol-http1-0.14.2/000077500000000000000000000000001411106473500154565ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/.editorconfig000066400000000000000000000000641411106473500201330ustar00rootroot00000000000000root = true [*] indent_style = tab indent_size = 2 ruby-protocol-http1-0.14.2/.github/000077500000000000000000000000001411106473500170165ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/.github/workflows/000077500000000000000000000000001411106473500210535ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/.github/workflows/development.yml000066400000000000000000000015561411106473500241270ustar00rootroot00000000000000name: Development on: [push, pull_request] jobs: test: runs-on: ${{matrix.os}}-latest continue-on-error: ${{matrix.experimental}} strategy: matrix: os: - ubuntu - macos ruby: - "2.6" - "2.7" - "3.0" experimental: [false] env: [""] include: - os: ubuntu ruby: truffleruby experimental: true - os: ubuntu ruby: jruby experimental: true - os: ubuntu ruby: head experimental: true steps: - uses: actions/checkout@v2 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{matrix.ruby}} bundler-cache: true - name: Run tests timeout-minutes: 5 run: ${{matrix.env}} bundle exec rspec ruby-protocol-http1-0.14.2/.github/workflows/documentation.yml000066400000000000000000000011671411106473500244540ustar00rootroot00000000000000name: Documentation on: push: branches: - master jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: ruby/setup-ruby@v1 env: BUNDLE_WITH: maintenance with: ruby-version: 2.7 bundler-cache: true - name: Installing packages run: sudo apt-get install wget - name: Generate documentation timeout-minutes: 5 run: bundle exec bake utopia:project:static - name: Deploy documentation uses: JamesIves/github-pages-deploy-action@4.0.0 with: branch: docs folder: docs ruby-protocol-http1-0.14.2/.gitignore000066400000000000000000000002401411106473500174420ustar00rootroot00000000000000/.bundle/ /.yardoc /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ # rspec failure tracking .rspec_status /gems.locked .covered.db /fuzz/request/output ruby-protocol-http1-0.14.2/.rspec000066400000000000000000000000671411106473500165760ustar00rootroot00000000000000--format documentation --warnings --require spec_helperruby-protocol-http1-0.14.2/README.md000066400000000000000000000052771411106473500167500ustar00rootroot00000000000000# Protocol::HTTP1 Provides a low-level implementation of the HTTP/1 protocol. [![Development Status](https://github.com/socketry/protocol-http1/workflows/Development/badge.svg)](https://github.com/socketry/protocol-http1/actions?workflow=Development) ## Installation Add this line to your application's Gemfile: ``` ruby gem 'protocol-http1' ``` And then execute: $ bundle Or install it yourself as: $ gem install protocol-http1 ## Usage Here is a basic HTTP/1.1 client: ``` ruby require 'async' require 'async/io/stream' require 'async/http/endpoint' require 'protocol/http1/connection' Async do endpoint = Async::HTTP::Endpoint.parse("https://www.google.com/search?q=kittens", alpn_protocols: ["http/1.1"]) peer = endpoint.connect puts "Connected to #{peer} #{peer.remote_address.inspect}" # IO Buffering... stream = Async::IO::Stream.new(peer) client = Protocol::HTTP1::Connection.new(stream) def client.read_line @stream.read_until(Protocol::HTTP1::Connection::CRLF) or raise EOFError end puts "Writing request..." client.write_request("www.google.com", "GET", "/search?q=kittens", "HTTP/1.1", [["Accept", "*/*"]]) client.write_body(nil) puts "Reading response..." response = client.read_response("GET") puts "Got response: #{response.inspect}" puts "Closing client..." client.close end ``` ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request ## License Released under the MIT license. Copyright, 2019, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams). Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ruby-protocol-http1-0.14.2/examples/000077500000000000000000000000001411106473500172745ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/examples/http1/000077500000000000000000000000001411106473500203345ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/examples/http1/request.rb000066400000000000000000000015541411106473500223560ustar00rootroot00000000000000# frozen_string_literal: true $LOAD_PATH.unshift File.expand_path("../../lib", __dir__) require 'async' require 'async/io/stream' require 'async/http/endpoint' require 'protocol/http1/connection' require 'pry' Async do endpoint = Async::HTTP::Endpoint.parse("https://www.google.com/search?q=kittens", alpn_protocols: ["http/1.1"]) peer = endpoint.connect puts "Connected to #{peer} #{peer.remote_address.inspect}" # IO Buffering... stream = Async::IO::Stream.new(peer) client = Protocol::HTTP1::Connection.new(stream) puts "Writing request..." client.write_request("www.google.com", "GET", "/search?q=kittens", "HTTP/1.1", [["Accept", "*/*"]]) client.write_body("HTTP/1.1", nil) puts "Reading response..." response = client.read_response("GET") puts "Got response: #{response.inspect}" puts "Closing client..." client.close end puts "Exiting." ruby-protocol-http1-0.14.2/fuzz/000077500000000000000000000000001411106473500164545ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/fuzz/request/000077500000000000000000000000001411106473500201445ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/fuzz/request/bake.rb000066400000000000000000000002031411106473500213660ustar00rootroot00000000000000 # Run the fuzz test. def run system("AFL_SKIP_BIN_CHECK=1 afl-fuzz -i input/ -o output/ -t 1000 -m 1000 -- ruby script.rb") end ruby-protocol-http1-0.14.2/fuzz/request/input/000077500000000000000000000000001411106473500213035ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/fuzz/request/input/body.txt000066400000000000000000000001311411106473500227740ustar00rootroot00000000000000POST /upload HTTP/1.1 Host: example.com Accept: */* Content-Length: 10 0123456789 ruby-protocol-http1-0.14.2/fuzz/request/input/simple.txt000066400000000000000000000000221411106473500233270ustar00rootroot00000000000000GET / HTTP/1.1 ruby-protocol-http1-0.14.2/fuzz/request/script.rb000077500000000000000000000011161411106473500217770ustar00rootroot00000000000000#!/usr/bin/env ruby require 'socket' require_relative '../../lib/protocol/http1' def test # input, output = Socket.pair(Socket::PF_UNIX, Socket::SOCK_STREAM) server = Protocol::HTTP1::Connection.new($stdin) # input.write($stdin.read) # input.close begin host, method, path, version, headers, body = server.read_request body = server.read_request_body(method, headers) rescue Protocol::HTTP1::InvalidRequest # Ignore. end end if ENV["_"] =~ /afl/ require 'kisaten' Kisaten.crash_at [], [], Signal.list['USR1'] while Kisaten.loop 10000 test end else test end ruby-protocol-http1-0.14.2/gems.rb000066400000000000000000000002271411106473500167370ustar00rootroot00000000000000# frozen_string_literal: true source "https://rubygems.org" gemspec group :maintenance, optional: true do gem "bake-modernize" gem "bake-gem" end ruby-protocol-http1-0.14.2/lib/000077500000000000000000000000001411106473500162245ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/lib/protocol/000077500000000000000000000000001411106473500200655ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/lib/protocol/http1.rb000066400000000000000000000023211411106473500214500ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2019, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require_relative 'http1/version' require_relative 'http1/connection' ruby-protocol-http1-0.14.2/lib/protocol/http1/000077500000000000000000000000001411106473500211255ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/lib/protocol/http1/body/000077500000000000000000000000001411106473500220625ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/lib/protocol/http1/body/chunked.rb000066400000000000000000000055451411106473500240410ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http/body/readable' module Protocol module HTTP1 module Body class Chunked < HTTP::Body::Readable CRLF = "\r\n" def initialize(stream, headers) @stream = stream @finished = false @headers = headers @length = 0 @count = 0 end def empty? @finished end def close(error = nil) # We only close the connection if we haven't completed reading the entire body: unless @finished @stream.close @finished = true end super end # Follows the procedure outlined in https://tools.ietf.org/html/rfc7230#section-4.1.3 def read return nil if @finished # It is possible this line contains chunk extension, so we use `to_i` to only consider the initial integral part: length = read_line.to_i(16) if length == 0 @finished = true read_trailer return nil end # Read trailing CRLF: chunk = @stream.read(length + 2) # ...and chomp it off: chunk.chomp!(CRLF) @length += length @count += 1 return chunk end def inspect "\#<#{self.class} #{@length} bytes read in #{@count} chunks>" end private def read_line? @stream.gets(CRLF, chomp: true) end def read_line read_line? or raise EOFError end def read_trailer while line = read_line? # Empty line indicates end of trailer: break if line.empty? if match = line.match(HEADER) @headers.add(match[1], match[2]) else raise BadHeader, "Could not parse header: #{line.dump}" end end end end end end end ruby-protocol-http1-0.14.2/lib/protocol/http1/body/fixed.rb000066400000000000000000000040631411106473500235110ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http/body/readable' module Protocol module HTTP1 module Body class Fixed < HTTP::Body::Readable def initialize(stream, length) @stream = stream @length = length @remaining = length end attr :length attr :remaining def empty? @remaining == 0 end def close(error = nil) if @remaining != 0 @stream.close end super end def read if @remaining > 0 if chunk = @stream.readpartial(@remaining) @remaining -= chunk.bytesize return chunk else raise EOFError, "Stream closed with #{@remaining} bytes remaining!" end end end def join buffer = @stream.read(@remaining) @remaining = 0 return buffer end def inspect "\#<#{self.class} length=#{@length} remaining=#{@remaining}>" end end end end end ruby-protocol-http1-0.14.2/lib/protocol/http1/body/remainder.rb000066400000000000000000000041321411106473500243550ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http/body/readable' module Protocol module HTTP1 module Body class Remainder < HTTP::Body::Readable BLOCK_SIZE = 1024 * 64 # block_size may be removed in the future. It is better managed by stream. def initialize(stream) @stream = stream end def empty? @stream.eof? or @stream.closed? end def close(error = nil) # We can't really do anything in this case except close the connection. @stream.close super end # TODO this is a bit less efficient in order to maintain compatibility with `IO`. def read @stream.readpartial(BLOCK_SIZE) rescue EOFError return nil end def call(stream) self.each do |chunk| stream.write(chunk) end stream.flush end def join @stream.read end def inspect "\#<#{self.class} #{@stream.closed? ? 'closed' : 'open'}>" end end end end end ruby-protocol-http1-0.14.2/lib/protocol/http1/connection.rb000066400000000000000000000345731411106473500236250ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http/headers' require_relative 'reason' require_relative 'error' require_relative 'body/chunked' require_relative 'body/fixed' require_relative 'body/remainder' require 'protocol/http/body/head' require 'protocol/http/methods' module Protocol module HTTP1 CONTENT_LENGTH = 'content-length' TRANSFER_ENCODING = 'transfer-encoding' CHUNKED = 'chunked' CONNECTION = 'connection' CLOSE = 'close' KEEP_ALIVE = 'keep-alive' HOST = 'host' UPGRADE = 'upgrade' # HTTP/1.x request line parser: TOKEN = /[!#$%&'*+-\.^_`|~0-9a-zA-Z]+/.freeze REQUEST_LINE = /\A(#{TOKEN}) ([^\s]+) (HTTP\/\d.\d)\z/.freeze # HTTP/1.x header parser: FIELD_NAME = TOKEN FIELD_VALUE = /[^\000-\037]*/.freeze HEADER = /\A(#{FIELD_NAME}):\s*(#{FIELD_VALUE})\s*\z/.freeze VALID_FIELD_NAME = /\A#{FIELD_NAME}\z/.freeze VALID_FIELD_VALUE = /\A#{FIELD_VALUE}\z/.freeze class Connection CRLF = "\r\n" HTTP10 = "HTTP/1.0" HTTP11 = "HTTP/1.1" def initialize(stream, persistent = true) @stream = stream @persistent = persistent @count = 0 end attr :stream # Whether the connection is persistent. attr :persistent # The number of requests processed. attr :count def upgrade?(headers) if upgrade = headers[UPGRADE] return upgrade end end def persistent?(version, method, headers) if method == HTTP::Methods::CONNECT return false end if version == HTTP10 if connection = headers[CONNECTION] return connection.keep_alive? else return false end else if connection = headers[CONNECTION] return !connection.close? else return true end end end # Write the appropriate header for connection persistence. def write_connection_header(version) if version == HTTP10 @stream.write("connection: keep-alive\r\n") if @persistent else @stream.write("connection: close\r\n") unless @persistent end end def write_upgrade_header(upgrade) @stream.write("connection: upgrade\r\nupgrade: #{upgrade}\r\n") end # Effectively close the connection and return the underlying IO. # @return [IO] the underlying non-blocking IO. def hijack! @persistent = false stream = @stream @stream.flush @stream = nil return stream end # Close the connection and underlying stream. def close @stream&.close end def write_request(authority, method, path, version, headers) @stream.write("#{method} #{path} #{version}\r\n") @stream.write("host: #{authority}\r\n") write_headers(headers) end def write_response(version, status, headers, reason = Reason::DESCRIPTIONS[status]) # Safari WebSockets break if no reason is given: @stream.write("#{version} #{status} #{reason}\r\n") write_headers(headers) end def write_headers(headers) headers.each do |name, value| # Convert it to a string: name = name.to_s value = value.to_s # Validate it: unless name.match?(VALID_FIELD_NAME) raise BadHeader, "Invalid header name: #{name.inspect}" end unless value.match?(VALID_FIELD_VALUE) raise BadHeader, "Invalid header value for #{name}: #{value.inspect}" end # Write it: @stream.write("#{name}: #{value}\r\n") end end def read_line? @stream.gets(CRLF, chomp: true) end def read_line read_line? or raise EOFError end def read_request return unless line = read_line? if match = line.match(REQUEST_LINE) _, method, path, version = *match else raise InvalidRequest, line.inspect end headers = read_headers @persistent = persistent?(version, method, headers) body = read_request_body(method, headers) @count += 1 return headers.delete(HOST), method, path, version, headers, body end def read_response(method) version, status, reason = read_line.split(/\s+/, 3) status = Integer(status) headers = read_headers @persistent = persistent?(version, method, headers) body = read_response_body(method, status, headers) @count += 1 return version, status, reason, headers, body end def read_headers fields = [] while line = read_line # Empty line indicates end of headers: break if line.empty? if match = line.match(HEADER) fields << [match[1], match[2]] else raise BadHeader, "Could not parse header: #{line.dump}" end end return HTTP::Headers.new(fields) end # @param protocol [String] the protocol to upgrade to. def write_upgrade_body(protocol, body = nil) # Once we upgrade the connection, it can no longer handle other requests: @persistent = false write_upgrade_header(protocol) @stream.write("\r\n") @stream.flush # Don't remove me! if body body.each do |chunk| @stream.write(chunk) @stream.flush end @stream.close_write end return @stream end def write_tunnel_body(version, body = nil) @persistent = false write_connection_header(version) @stream.write("\r\n") @stream.flush # Don't remove me! if body body.each do |chunk| @stream.write(chunk) @stream.flush end @stream.close_write end return @stream end def write_empty_body(body) @stream.write("content-length: 0\r\n\r\n") @stream.flush body&.close end def write_fixed_length_body(body, length, head) @stream.write("content-length: #{length}\r\n\r\n") if head @stream.flush body.close return end @stream.flush unless body.ready? chunk_length = 0 body.each do |chunk| chunk_length += chunk.bytesize if chunk_length > length raise Error, "Trying to write #{chunk_length} bytes, but content length was #{length} bytes!" end @stream.write(chunk) @stream.flush unless body.ready? end @stream.flush if chunk_length != length raise Error, "Wrote #{chunk_length} bytes, but content length was #{length} bytes!" end end def write_chunked_body(body, head, trailer = nil) @stream.write("transfer-encoding: chunked\r\n\r\n") if head @stream.flush body.close return end @stream.flush unless body.ready? body.each do |chunk| next if chunk.size == 0 @stream.write("#{chunk.bytesize.to_s(16).upcase}\r\n") @stream.write(chunk) @stream.write(CRLF) @stream.flush unless body.ready? end if trailer @stream.write("0\r\n") write_headers(trailer) @stream.write("\r\n") else @stream.write("0\r\n\r\n") end @stream.flush end def write_body_and_close(body, head) # We can't be persistent because we don't know the data length: @persistent = false @stream.write("\r\n") @stream.flush unless body.ready? if head body.close else body.each do |chunk| @stream.write(chunk) @stream.flush unless body.ready? end end @stream.close_write end def write_body(version, body, head = false, trailer = nil) if body.nil? write_connection_header(version) write_empty_body(body) elsif length = body.length and trailer.nil? write_connection_header(version) write_fixed_length_body(body, length, head) elsif body.empty? # Even thought this code is the same as the first clause `body.nil?`, HEAD responses have an empty body but still carry a content length. `write_fixed_length_body` takes care of this appropriately. write_connection_header(version) write_empty_body(body) elsif version == HTTP11 write_connection_header(version) # We specifically ensure that non-persistent connections do not use chunked response, so that hijacking works as expected. write_chunked_body(body, head, trailer) else @persistent = false write_connection_header(version) write_body_and_close(body, head) end end def read_chunked_body(headers) Body::Chunked.new(@stream, headers) end def read_fixed_body(length) Body::Fixed.new(@stream, length) end def read_remainder_body Body::Remainder.new(@stream) end def read_head_body(length) Protocol::HTTP::Body::Head.new(length) end def read_tunnel_body read_remainder_body end def read_upgrade_body(protocol) read_remainder_body end HEAD = "HEAD" CONNECT = "CONNECT" def read_response_body(method, status, headers) # RFC 7230 3.3.3 # 1. Any response to a HEAD request and any response with a 1xx # (Informational), 204 (No Content), or 304 (Not Modified) status # code is always terminated by the first empty line after the # header fields, regardless of the header fields present in the # message, and thus cannot contain a message body. if method == HTTP::Methods::HEAD if content_length = headers.delete(CONTENT_LENGTH) length = Integer(content_length) if length > 0 return read_head_body(length) elsif length == 0 return nil else raise BadRequest, "Invalid content length: #{content_length}" end else return nil end end if (status >= 100 and status < 200) or status == 204 or status == 304 return nil end # 2. Any 2xx (Successful) response to a CONNECT request implies that # the connection will become a tunnel immediately after the empty # line that concludes the header fields. A client MUST ignore any # Content-Length or Transfer-Encoding header fields received in # such a message. if method == HTTP::Methods::CONNECT and status == 200 return read_tunnel_body end return read_body(headers, true) end def read_request_body(method, headers) # 2. Any 2xx (Successful) response to a CONNECT request implies that # the connection will become a tunnel immediately after the empty # line that concludes the header fields. A client MUST ignore any # Content-Length or Transfer-Encoding header fields received in # such a message. if method == HTTP::Methods::CONNECT return read_tunnel_body end # 6. If this is a request message and none of the above are true, then # the message body length is zero (no message body is present). return read_body(headers) end def read_body(headers, remainder = false) # 3. If a Transfer-Encoding header field is present and the chunked # transfer coding (Section 4.1) is the final encoding, the message # body length is determined by reading and decoding the chunked # data until the transfer coding indicates the data is complete. if transfer_encoding = headers.delete(TRANSFER_ENCODING) # If a message is received with both a Transfer-Encoding and a # Content-Length header field, the Transfer-Encoding overrides the # Content-Length. Such a message might indicate an attempt to # perform request smuggling (Section 9.5) or response splitting # (Section 9.4) and ought to be handled as an error. A sender MUST # remove the received Content-Length field prior to forwarding such # a message downstream. if headers[CONTENT_LENGTH] raise BadRequest, "Message contains both transfer encoding and content length!" end if transfer_encoding.last == CHUNKED return read_chunked_body(headers) else # If a Transfer-Encoding header field is present in a response and # the chunked transfer coding is not the final encoding, the # message body length is determined by reading the connection until # it is closed by the server. If a Transfer-Encoding header field # is present in a request and the chunked transfer coding is not # the final encoding, the message body length cannot be determined # reliably; the server MUST respond with the 400 (Bad Request) # status code and then close the connection. return read_remainder_body end end # 5. If a valid Content-Length header field is present without # Transfer-Encoding, its decimal value defines the expected message # body length in octets. If the sender closes the connection or # the recipient times out before the indicated number of octets are # received, the recipient MUST consider the message to be # incomplete and close the connection. if content_length = headers.delete(CONTENT_LENGTH) length = Integer(content_length) if length > 0 return read_fixed_body(length) elsif length == 0 return nil else raise BadRequest, "Invalid content length: #{content_length}" end end # http://tools.ietf.org/html/rfc2068#section-19.7.1.1 if remainder # 7. Otherwise, this is a response message without a declared message # body length, so the message body length is determined by the # number of octets received prior to the server closing the # connection. return read_remainder_body end end end end end ruby-protocol-http1-0.14.2/lib/protocol/http1/error.rb000066400000000000000000000027251411106473500226110ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2019, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http/error' module Protocol module HTTP1 class Error < HTTP::Error end class InvalidRequest < Error end # The request was parsed correctly, but was invalid for some other reason. class BadRequest < Error end class BadHeader < Error end class BadResponse < Error end end end ruby-protocol-http1-0.14.2/lib/protocol/http1/reason.rb000066400000000000000000000061751411106473500227520ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2019, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http/error' module Protocol module HTTP1 module Reason DESCRIPTIONS = { 100 => "Continue", 101 => "Switching Protocols", 102 => "Processing", 103 => "Early Hints", 200 => "OK", 201 => "Created", 202 => "Accepted", 203 => "Non-Authoritative Information", 204 => "No Content", 205 => "Reset Content", 206 => "Partial Content", 207 => "Multi-Status", 208 => "Already Reported", 226 => "IM Used", 300 => "Multiple Choices", 301 => "Moved Permanently", 302 => "Found", 303 => "See Other", 304 => "Not Modified", 305 => "Use Proxy", # no longer used, but included for completeness 306 => "Switch Proxy", 307 => "Temporary Redirect", 308 => "Permanent Redirect", 400 => "Bad Request", 401 => "Unauthorized", 402 => "Payment Required", 403 => "Forbidden", 404 => "Not Found", 405 => "Method Not Allowed", 406 => "Not Acceptable", 407 => "Proxy Authentication Required", 408 => "Request Timeout", 409 => "Conflict", 410 => "Gone", 411 => "Length Required", 412 => "Precondition Failed", 413 => "Payload Too Large", 414 => "URI Too Long", 415 => "Unsupported Media Type", 416 => "Range Not Satisfiable", 417 => "Expectation Failed", 421 => "Misdirected Request", 422 => "Unprocessable Entity", 423 => "Locked", 424 => "Failed Dependency", 426 => "Upgrade Required", 428 => "Precondition Required", 429 => "Too Many Requests", 431 => "Request Header Fields Too Large", 451 => "Unavailable for Legal Reasons", 500 => "Internal Server Error", 501 => "Not Implemented", 502 => "Bad Gateway", 503 => "Service Unavailable", 504 => "Gateway Timeout", 505 => "HTTP Version Not Supported", 506 => "Variant Also Negotiates", 507 => "Insufficient Storage", 508 => "Loop Detected", 510 => "Not Extended", 511 => "Network Authentication Required" }.freeze end end end ruby-protocol-http1-0.14.2/lib/protocol/http1/version.rb000066400000000000000000000023101411106473500231330ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2019, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. module Protocol module HTTP1 VERSION = "0.14.2" end end ruby-protocol-http1-0.14.2/protocol-http1.gemspec000066400000000000000000000015421411106473500217240ustar00rootroot00000000000000 require_relative "lib/protocol/http1/version" Gem::Specification.new do |spec| spec.name = "protocol-http1" spec.version = Protocol::HTTP1::VERSION spec.summary = "A low level implementation of the HTTP/1 protocol." spec.authors = ["Samuel Williams"] spec.license = "MIT" spec.cert_chain = ['release.cert'] spec.signing_key = File.expand_path('~/.gem/release.pem') spec.homepage = "https://github.com/socketry/protocol-http1" spec.files = Dir.glob('{lib}/**/*', File::FNM_DOTMATCH, base: __dir__) spec.required_ruby_version = ">= 2.4" spec.add_dependency "protocol-http", "~> 0.22" spec.add_development_dependency "bundler" spec.add_development_dependency "covered" spec.add_development_dependency "rspec", "~> 3.0" spec.add_development_dependency "rspec-files", "~> 1.0" spec.add_development_dependency "rspec-memory", "~> 1.0" end ruby-protocol-http1-0.14.2/release.cert000066400000000000000000000031331411106473500177550ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIEhDCCAuygAwIBAgIBATANBgkqhkiG9w0BAQsFADA3MTUwMwYDVQQDDCxzYW11 ZWwud2lsbGlhbXMvREM9b3Jpb250cmFuc2Zlci9EQz1jby9EQz1uejAeFw0yMTA4 MTYwNjMzNDRaFw0yMjA4MTYwNjMzNDRaMDcxNTAzBgNVBAMMLHNhbXVlbC53aWxs aWFtcy9EQz1vcmlvbnRyYW5zZmVyL0RDPWNvL0RDPW56MIIBojANBgkqhkiG9w0B AQEFAAOCAY8AMIIBigKCAYEAyXLSS/cw+fXJ5e7hi+U/TeChPWeYdwJojDsFY1xr xvtqbTTL8gbLHz5LW3QD2nfwCv3qTlw0qI3Ie7a9VMJMbSvgVEGEfQirqIgJXWMj eNMDgKsMJtC7u/43abRKx7TCURW3iWyR19NRngsJJmaR51yGGGm2Kfsr+JtKKLtL L188Wm3f13KAx7QJU8qyuBnj1/gWem076hzdA7xi1DbrZrch9GCRz62xymJlrJHn 9iZEZ7AxrS7vokhMlzSr/XMUihx/8aFKtk+tMLClqxZSmBWIErWdicCGTULXCBNb E/mljo4zEVKhlTWpJklMIhr55ZRrSarKFuW7en0+tpJrfsYiAmXMJNi4XAYJH7uL rgJuJwSaa/dMz+VmUoo7VKtSfCoOI+6v5/z0sK3oT6sG6ZwyI47DBq2XqNC6tnAj w+XmCywiTQrFzMMAvcA7rPI4F0nU1rZId51rOvvfxaONp+wgTi4P8owZLw0/j0m4 8C20DYi6EYx4AHDXiLpElWh3AgMBAAGjgZowgZcwCQYDVR0TBAIwADALBgNVHQ8E BAMCBLAwHQYDVR0OBBYEFB6ZaeWKxQjGTI+pmz7cKRmMIywwMC4GA1UdEQQnMCWB I3NhbXVlbC53aWxsaWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MC4GA1UdEgQnMCWB I3NhbXVlbC53aWxsaWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MA0GCSqGSIb3DQEB CwUAA4IBgQBVoM+pu3dpdUhZM1w051iw5GfiqclAr1Psypf16Tiod/ho//4oAu6T 9fj3DPX/acWV9P/FScvqo4Qgv6g4VWO5ZU7z2JmPoTXZtYMunRAmQPFL/gSUc6aK vszMHIyhtyzRc6DnfW2AiVOjMBjaYv8xXZc9bduniRVPrLR4J7ozmGLh4o4uJp7w x9KCFaR8Lvn/r0oJWJOqb/DMAYI83YeN2Dlt3jpwrsmsONrtC5S3gOUle5afSGos bYt5ocnEpKSomR9ZtnCGljds/aeO1Xgpn2r9HHcjwnH346iNrnHmMlC7BtHUFPDg Ts92S47PTOXzwPBDsrFiq3VLbRjHSwf8rpqybQBH9MfzxGGxTaETQYOd6b4e4Ag6 y92abGna0bmIEb4+Tx9rQ10Uijh1POzvr/VTH4bbIPy9FbKrRsIQ24qDbNJRtOpE RAOsIl+HOBTb252nx1kIRN5hqQx272AJCbCjKx8egcUQKffFVVCI0nye09v5CK+a HiLJ8VOFx6w= -----END CERTIFICATE----- ruby-protocol-http1-0.14.2/spec/000077500000000000000000000000001411106473500164105ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/spec/protocol/000077500000000000000000000000001411106473500202515ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/spec/protocol/http1/000077500000000000000000000000001411106473500213115ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/spec/protocol/http1/body/000077500000000000000000000000001411106473500222465ustar00rootroot00000000000000ruby-protocol-http1-0.14.2/spec/protocol/http1/body/chunked_spec.rb000066400000000000000000000055761411106473500252430ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require_relative '../connection_context' require 'protocol/http1/body/chunked' RSpec.describe Protocol::HTTP1::Body::Chunked do include_context RSpec::Memory include_context RSpec::Files::Buffer let(:content) {"Hello World"} let(:postfix) {nil} let(:headers) {Protocol::HTTP::Headers.new} subject {described_class.new(buffer, headers)} before do buffer.write "#{content.bytesize.to_s(16)}\r\n#{content}\r\n0\r\n#{postfix}\r\n" buffer.seek(0) end describe "#empty?" do it "returns whether EOF was reached" do expect(subject.empty?).to be == false end end describe "#stop" do it "closes the stream" do subject.close(EOFError) expect(buffer).to be_closed end it "marks body as finished" do subject.close(EOFError) expect(subject).to be_empty end end describe "#read" do it "retrieves chunks of content" do expect(subject.read).to be == "Hello World" expect(subject.read).to be == nil expect(subject.read).to be == nil end it "updates number of bytes retrieved" do subject.read subject.read # realizes there are no more chunks expect(subject).to be_empty end context "with large stream" do let!(:content) {"a" * 1024 * 10} xit "allocates expected amount of memory" do subject expect do while chunk = subject.read chunk.clear end end.to limit_allocations.of(String, size: 0).of(Hash, count: 8) end end context "with trailer" do let(:postfix) {"ETag: abcd\r\n"} it "can read trailing etag" do headers.add('trailer', 'etag') expect(subject.read).to be == "Hello World" expect(headers['etag']).to be_nil expect(subject.read).to be == nil expect(headers['etag']).to be == 'abcd' end end end end ruby-protocol-http1-0.14.2/spec/protocol/http1/body/fixed_spec.rb000066400000000000000000000062161411106473500247110ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http1/body/fixed' RSpec.describe Protocol::HTTP1::Body::Fixed do include_context RSpec::Memory include_context RSpec::Files::Buffer let(:content) {"Hello World"} subject! {described_class.new(buffer, content.bytesize)} before do buffer.write content buffer.seek(0) end describe "#empty?" do it "returns whether EOF was reached" do expect(subject.empty?).to be == false end end describe "#stop" do it "closes the stream" do subject.close(EOFError) expect(buffer).to be_closed end it "doesn't close the stream when EOF was reached" do subject.read subject.close(EOFError) expect(buffer).not_to be_closed end end describe "#read" do it "retrieves chunks of content" do expect(subject.read).to be == "Hello World" expect(subject.read).to be == nil end it "updates number of bytes retrieved" do subject.read expect(subject).to be_empty end context "when provided length is smaller than stream size" do subject {described_class.new(buffer, 5)} it "retrieves content up to provided length" do expect(subject.read).to be == "Hello" expect(subject.read).to be == nil end it "updates number of bytes retrieved" do subject.read expect(subject).to be_empty end end context "when provided lengthis larger than stream size" do subject {described_class.new(buffer, 20)} it "retrieves content up to provided length" do expect do subject.read subject.read end.to raise_error(EOFError) end end context "with large stream" do let(:content) {"a" * 5*1024*1024} it "allocates expected amount of memory" do expect do subject.read.clear until subject.empty? end.to limit_allocations(size: 0) end end end describe "#join" do it "returns all content" do expect(subject.join).to be == "Hello World" expect(subject.join).to be == "" end it "updates number of bytes retrieved" do subject.read expect(subject).to be_empty end end end ruby-protocol-http1-0.14.2/spec/protocol/http1/body/remainder_spec.rb000066400000000000000000000050101411106473500255470ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http1/body/remainder' RSpec.describe Protocol::HTTP1::Body::Remainder do include_context RSpec::Memory include_context RSpec::Files::Buffer let(:content) {"Hello World"} subject! {described_class.new(buffer)} before do buffer.write content buffer.seek(0) end describe "#empty?" do it "returns whether EOF was reached" do expect(subject.empty?).to be == false end end describe "#stop" do it "closes the stream" do subject.close(EOFError) expect(buffer).to be_closed end it "closes the stream when EOF was reached" do subject.read subject.close(EOFError) expect(buffer).to be_closed end end describe "#read" do it "retrieves chunks of content" do expect(subject.read).to be == "Hello World" expect(subject.read).to be == nil end it "updates number of bytes retrieved" do subject.read expect(subject).to be_empty end context "with large stream" do let(:content) {"a" * 5*1024*1024} it "allocates expected amount of memory" do expect do subject.read.clear until subject.empty? end.to limit_allocations(size: 0) end end end describe "#join" do it "returns all content" do expect(subject.join).to be == "Hello World" expect(subject.join).to be == "" end it "updates number of bytes retrieved" do subject.read expect(subject).to be_empty end end end ruby-protocol-http1-0.14.2/spec/protocol/http1/connection_context.rb000066400000000000000000000026751411106473500255530ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http1/connection' require 'socket' RSpec.shared_context Protocol::HTTP1::Connection do let(:sockets) {Socket.pair(Socket::PF_UNIX, Socket::SOCK_STREAM)} let(:client) {Protocol::HTTP1::Connection.new(sockets.first)} let(:server) {Protocol::HTTP1::Connection.new(sockets.last)} end ruby-protocol-http1-0.14.2/spec/protocol/http1/connection_spec.rb000066400000000000000000000230561411106473500250150ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http1/connection' require 'protocol/http/body/buffered' require_relative 'connection_context' RSpec.describe Protocol::HTTP1::Connection do include_context Protocol::HTTP1::Connection describe '#read_request' do it "reads request without body" do client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n" client.stream.close authority, method, target, version, headers, body = server.read_request expect(authority).to be == 'localhost' expect(method).to be == 'GET' expect(target).to be == '/' expect(version).to be == 'HTTP/1.1' expect(headers).to be == {} expect(body).to be_nil end it "reads request without body after closing connection" do client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nAccept: */*\r\nHeader-0: value 1\r\n\r\n" client.stream.close authority, method, target, version, headers, body = server.read_request expect(authority).to be == 'localhost' expect(method).to be == 'GET' expect(target).to be == '/' expect(version).to be == 'HTTP/1.1' expect(headers).to be == {'accept' => ['*/*'], 'header-0' => ["value 1"]} expect(body).to be_nil end it "reads request with fixed body" do client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 11\r\n\r\nHello World" client.stream.close authority, method, target, version, headers, body = server.read_request expect(authority).to be == 'localhost' expect(method).to be == 'GET' expect(target).to be == '/' expect(version).to be == 'HTTP/1.1' expect(headers).to be == {} expect(body.join).to be == "Hello World" end it "reads request with chunked body" do client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n" client.stream.close authority, method, target, version, headers, body = server.read_request expect(authority).to be == 'localhost' expect(method).to be == 'GET' expect(target).to be == '/' expect(version).to be == 'HTTP/1.1' expect(headers).to be == {} expect(body.join).to be == "Hello World" expect(server).to be_persistent(version, method, headers) end it "fails with broken request" do client.stream.write "Accept: */*\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n" client.stream.close expect do server.read_request end.to raise_error(Protocol::HTTP1::InvalidRequest) end it "fails with missing version" do client.stream.write "GET foo\r\n" client.stream.close expect do server.read_request end.to raise_error(Protocol::HTTP1::InvalidRequest) end end describe '#persistent?' do describe "HTTP 1.0" do it "should not be persistent by default" do expect(server).not_to be_persistent("HTTP/1.0", "GET", {}) end it "should be persistent if connection: keep-alive is set" do headers = Protocol::HTTP::Headers[ "connection" => "keep-alive" ] expect(server).to be_persistent("HTTP/1.0", "GET", headers) end it "should allow case-insensitive 'connection' value" do headers = Protocol::HTTP::Headers[ "connection" => "Keep-Alive" ] expect(server).to be_persistent("HTTP/1.0", "GET", headers) end end describe "HTTP 1.1" do it "should be persistent by default" do expect(server).to be_persistent("HTTP/1.1", "GET", {}) end it "should not be persistent if connection: close is set" do headers = Protocol::HTTP::Headers[ "connection" => "close" ] expect(server).not_to be_persistent("HTTP/1.1", "GET", headers) end it "should allow case-insensitive 'connection' value" do headers = Protocol::HTTP::Headers[ "connection" => "Close" ] expect(server).not_to be_persistent("HTTP/1.1", "GET", headers) end end end describe '#read_response' do it "should read successful response" do server.stream.write("HTTP/1.1 200 Hello\r\nContent-Length: 0\r\n\r\n") server.stream.close version, status, reason, headers, body = client.read_response("GET") expect(version).to be == 'HTTP/1.1' expect(status).to be == 200 expect(reason).to be == "Hello" expect(headers).to be == {} expect(body).to be_nil end end describe '#read_response_body' do context "with GET" do it "should ignore body for informational responses" do expect(client.read_response_body("GET", 100, {'content-length' => '10'})).to be_nil end end context "with HEAD" do it "can read length of head response" do body = client.read_response_body("HEAD", 200, {'content-length' => 3773}) expect(body).to be_kind_of ::Protocol::HTTP::Body::Head expect(body.length).to be == 3773 expect(body.read).to be nil end it "ignores zero length body" do body = client.read_response_body("HEAD", 200, {'content-length' => 0}) expect(body).to be_nil end end end describe '#write_chunked_body' do let(:chunks) {["Hello", "World"]} let(:body) {::Protocol::HTTP::Body::Buffered.wrap(chunks)} it "can generate and read chunked response" do server.write_chunked_body(body, false) server.close headers = client.read_headers expect(headers).to be == [['transfer-encoding', 'chunked']] body = client.read_body(headers, false) expect(body.join).to be == chunks.join end it "can generate and read trailer" do chunks = ["Hello", "World"] server.write_headers({'trailer' => 'etag'}) server.write_chunked_body(body, false, {'etag' => 'abcd'}) server.close headers = client.read_headers expect(headers).to be == [['trailer', 'etag'], ['transfer-encoding', 'chunked']] body = client.read_body(headers, false) expect(body.join).to be == chunks.join expect(headers).to include('etag') end end describe '#write_fixed_length_body' do let(:chunks) {["Hello", "World"]} let(:body) {::Protocol::HTTP::Body::Buffered.wrap(chunks)} it "can generate and read chunked response" do server.write_fixed_length_body(body, 10, false) server.close headers = client.read_headers expect(headers).to be == [['content-length', '10']] body = client.read_body(headers, false) expect(body.join).to be == chunks.join end end describe '#write_body' do let(:body) {double} it "can write empty body" do expect(body).to receive(:empty?).and_return(true) expect(body).to receive(:length).and_return(false) expect(server).to receive(:write_empty_body) server.write_body("HTTP/1.0", body) end it "can write fixed length body" do expect(body).to receive(:length).and_return(1024) expect(server).to receive(:write_fixed_length_body) server.write_body("HTTP/1.0", body) end it "can write chunked body" do expect(server.persistent).to be true expect(body).to receive(:empty?).and_return(false) expect(body).to receive(:length).and_return(nil) expect(server).to receive(:write_chunked_body) server.write_body("HTTP/1.1", body) end it "can write fixed length body for HTTP/1.1" do expect(body).to receive(:length).and_return(1024) expect(server).to receive(:write_fixed_length_body) server.write_body("HTTP/1.1", body) end it "can write closed body" do expect(server.persistent).to be true expect(body).to receive(:empty?).and_return(false) expect(body).to receive(:length).and_return(nil) expect(server).to receive(:write_body_and_close) server.write_body("HTTP/1.0", body) end end context 'bad requests' do it "should fail with negative content length" do client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: -1\r\n\r\nHello World" client.stream.close expect do server.read_request end.to raise_error(Protocol::HTTP1::BadRequest) end it "should fail with invalid headers" do client.stream.write "GET / HTTP/1.1\r\nHost: \000localhost\r\n\r\nHello World" client.stream.close expect do server.read_request end.to raise_error(Protocol::HTTP1::BadHeader) end end context 'bad responses' do it 'should fail if headers contain \r characters' do expect do server.write_headers( [["id", "5\rSet-Cookie: foo-bar"]] ) end.to raise_error(Protocol::HTTP1::BadHeader) end it 'should fail if headers contain \n characters' do expect do server.write_headers( [["id", "5\nSet-Cookie: foo-bar"]] ) end.to raise_error(Protocol::HTTP1::BadHeader) end end end ruby-protocol-http1-0.14.2/spec/protocol/http1/hijack_spec.rb000066400000000000000000000047611411106473500241110ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2019, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http1/connection' require_relative 'connection_context' RSpec.describe Protocol::HTTP1::Connection do include_context Protocol::HTTP1::Connection describe '#hijack' do let(:response_version) {Protocol::HTTP1::Connection::HTTP10} let(:response_headers) {Hash.new('upgrade' => 'websocket')} let(:body) {double} let(:text) {"Hello World!"} it "should not be persistent after hijack" do server_wrapper = server.hijack! expect(server.persistent).to be false end it "should use non-chunked output" do expect(body).to receive(:ready?).and_return(false) expect(body).to receive(:empty?).and_return(false) expect(body).to receive(:length).and_return(nil) expect(body).to receive(:each).and_return(nil) expect(server).to receive(:write_body_and_close).and_call_original server.write_response(response_version, 101, response_headers) server.write_body(response_version, body) server_stream = server.hijack! version, status, reason, headers, body = client.read_response("GET") expect(version).to be == response_version expect(status).to be == 101 expect(headers).to be == response_headers expect(body).to be_nil # due to 101 status client_stream = client.hijack! client_stream.write(text) client_stream.close expect(server_stream.read).to be == text end end end ruby-protocol-http1-0.14.2/spec/protocol/http1/upgrade_spec.rb000066400000000000000000000036421411106473500243040ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2019, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http1/connection' require_relative 'connection_context' RSpec.describe Protocol::HTTP1::Connection do include_context Protocol::HTTP1::Connection describe '#upgrade' do let(:protocol) {'binary'} let(:request_version) {Protocol::HTTP1::Connection::HTTP10} it "should upgrade connection" do client.write_request("testing.com", "GET", "/", request_version, []) stream = client.write_upgrade_body(protocol) stream.write "Hello World" stream.close_write authority, method, path, version, headers, body = server.read_request expect(version).to be == request_version expect(headers['upgrade']).to be == [protocol] expect(body).to be_nil stream = server.hijack! expect(stream.read).to be == "Hello World" end end end ruby-protocol-http1-0.14.2/spec/protocol/http1_spec.rb000066400000000000000000000024501411106473500226510ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2019, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http1/version' RSpec.describe Protocol::HTTP1 do it "has a version number" do expect(Protocol::HTTP1::VERSION).not_to be nil end end ruby-protocol-http1-0.14.2/spec/spec_helper.rb000066400000000000000000000030131411106473500212230ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2019, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'rspec/memory' require 'rspec/files' require 'covered/rspec' RSpec.configure do |config| # Enable flags like --only-failures and --next-failure config.example_status_persistence_file_path = ".rspec_status" # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! config.expect_with :rspec do |c| c.syntax = :expect end end