pax_global_header00006660000000000000000000000064143030375370014517gustar00rootroot0000000000000052 comment=e6a9235102986a7a5462aea251f2fc9cdc00d65b ruby-protocol-http1-0.14.6/000077500000000000000000000000001430303753700154655ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/.editorconfig000066400000000000000000000000641430303753700201420ustar00rootroot00000000000000root = true [*] indent_style = tab indent_size = 2 ruby-protocol-http1-0.14.6/.github/000077500000000000000000000000001430303753700170255ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/.github/workflows/000077500000000000000000000000001430303753700210625ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/.github/workflows/documentation.yaml000066400000000000000000000023171430303753700246220ustar00rootroot00000000000000name: Documentation on: push: branches: - main # Allows you to run this workflow manually from the Actions tab: workflow_dispatch: # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages: permissions: contents: read pages: write id-token: write # Allow one concurrent deployment: concurrency: group: "pages" cancel-in-progress: true env: CONSOLE_OUTPUT: XTerm BUNDLE_WITH: maintenance jobs: generate: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: ruby/setup-ruby@v1 with: ruby-version: "3.1" 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 --force no - name: Upload documentation artifact uses: actions/upload-pages-artifact@v1 with: path: docs deploy: runs-on: ubuntu-latest environment: name: github-pages url: ${{steps.deployment.outputs.page_url}} needs: generate steps: - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v1 ruby-protocol-http1-0.14.6/.github/workflows/test-external.yaml000066400000000000000000000011461430303753700245470ustar00rootroot00000000000000name: Test External on: [push, pull_request] permissions: contents: read env: CONSOLE_OUTPUT: XTerm jobs: test: name: ${{matrix.ruby}} on ${{matrix.os}} runs-on: ${{matrix.os}}-latest strategy: matrix: os: - ubuntu - macos ruby: - "2.7" - "3.0" - "3.1" steps: - uses: actions/checkout@v3 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{matrix.ruby}} bundler-cache: true - name: Run tests timeout-minutes: 10 run: bundle exec bake test:external ruby-protocol-http1-0.14.6/.github/workflows/test.yaml000066400000000000000000000016641430303753700227340ustar00rootroot00000000000000name: Test on: [push, pull_request] permissions: contents: read env: CONSOLE_OUTPUT: XTerm jobs: test: name: ${{matrix.ruby}} on ${{matrix.os}} runs-on: ${{matrix.os}}-latest continue-on-error: ${{matrix.experimental}} strategy: matrix: os: - ubuntu - macos ruby: - "2.7" - "3.0" - "3.1" experimental: [false] include: - os: ubuntu ruby: truffleruby experimental: true - os: ubuntu ruby: jruby experimental: true - os: ubuntu ruby: head experimental: true steps: - uses: actions/checkout@v3 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{matrix.ruby}} bundler-cache: true - name: Run tests timeout-minutes: 10 run: bundle exec bake test ruby-protocol-http1-0.14.6/.gitignore000066400000000000000000000001631430303753700174550ustar00rootroot00000000000000/.bundle/ /pkg/ /gems.locked /.covered.db /external # rspec failure tracking /.rspec_status /fuzz/request/output ruby-protocol-http1-0.14.6/.rspec000066400000000000000000000000671430303753700166050ustar00rootroot00000000000000--format documentation --warnings --require spec_helperruby-protocol-http1-0.14.6/examples/000077500000000000000000000000001430303753700173035ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/examples/early-hints/000077500000000000000000000000001430303753700215425ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/examples/early-hints/server.rb000077500000000000000000000011711430303753700234000ustar00rootroot00000000000000#!/usr/bin/env ruby # frozen_string_literal: true # Released under the MIT License. # Copyright, 2022, by Samuel Williams. $LOAD_PATH.unshift File.expand_path("../../lib", __dir__) require 'async' require 'async/http/endpoint' RESPONSE = <<~HTTP.split(/\r?\n/).join("\r\n") HTTP/1.1 103 Early Hints Link: ; rel=preload; as=style Link: ; rel=preload; as=script HTTP/1.1 200 OK Content-Length: 11 Connection: close Content-Type: text/plain Hello World HTTP p RESPONSE Async do endpoint = Async::HTTP::Endpoint.parse("http://localhost:3000") endpoint.accept do |peer| peer.write(RESPONSE) end end ruby-protocol-http1-0.14.6/examples/http1/000077500000000000000000000000001430303753700203435ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/examples/http1/request.rb000066400000000000000000000016731430303753700223670ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. $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.6/fuzz/000077500000000000000000000000001430303753700164635ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/fuzz/request/000077500000000000000000000000001430303753700201535ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/fuzz/request/bake.rb000066400000000000000000000003601430303753700214010ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2020-2022, by Samuel Williams. # 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.6/fuzz/request/input/000077500000000000000000000000001430303753700213125ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/fuzz/request/input/body.txt000066400000000000000000000001311430303753700230030ustar00rootroot00000000000000POST /upload HTTP/1.1 Host: example.com Accept: */* Content-Length: 10 0123456789 ruby-protocol-http1-0.14.6/fuzz/request/input/simple.txt000066400000000000000000000000221430303753700233360ustar00rootroot00000000000000GET / HTTP/1.1 ruby-protocol-http1-0.14.6/fuzz/request/script.rb000077500000000000000000000012731430303753700220120ustar00rootroot00000000000000#!/usr/bin/env ruby # frozen_string_literal: true # Released under the MIT License. # Copyright, 2020-2022, by Samuel Williams. 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.6/gems.rb000066400000000000000000000004271430303753700167500ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2020-2022, by Samuel Williams. source "https://rubygems.org" gemspec group :maintenance, optional: true do gem "bake-modernize" gem "bake-gem" gem "bake-github-pages" gem "utopia-project" end ruby-protocol-http1-0.14.6/lib/000077500000000000000000000000001430303753700162335ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/lib/protocol/000077500000000000000000000000001430303753700200745ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/lib/protocol/http1.rb000066400000000000000000000002631430303753700214620ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. require_relative 'http1/version' require_relative 'http1/connection' ruby-protocol-http1-0.14.6/lib/protocol/http1/000077500000000000000000000000001430303753700211345ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/lib/protocol/http1/body/000077500000000000000000000000001430303753700220715ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/lib/protocol/http1/body/chunked.rb000066400000000000000000000035071430303753700240440ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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.6/lib/protocol/http1/body/fixed.rb000066400000000000000000000020251430303753700235140ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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.6/lib/protocol/http1/body/remainder.rb000066400000000000000000000022351430303753700243660ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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, IOError # I noticed that in some cases you will get EOFError, and in other cases IOError!? 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.6/lib/protocol/http1/connection.rb000066400000000000000000000335741430303753700236340ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. # Copyright, 2020, by Bruno Sutic. 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&.any? @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) # HTTP/1.0 cannot in any case handle trailers. if version == HTTP10 # or te: trailers was not present (strictly speaking not required.) trailer = nil end # While writing the body, we don't know if trailers will be added. We must choose a different body format depending on whether there is the chance of trailers, even if trailer.any? is currently false. # # Below you notice `and trailer.nil?`. I tried this but content-length is more important than trailers. 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.6/lib/protocol/http1/error.rb000066400000000000000000000006671430303753700226230ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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.6/lib/protocol/http1/reason.rb000066400000000000000000000041371430303753700227550ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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.6/lib/protocol/http1/version.rb000066400000000000000000000002521430303753700231450ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. module Protocol module HTTP1 VERSION = "0.14.6" end end ruby-protocol-http1-0.14.6/license.md000066400000000000000000000021361430303753700174330ustar00rootroot00000000000000# MIT License Copyright, 2019-2022, by Samuel Williams. Copyright, 2020, by Bruno Sutic. 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.6/protocol-http1.gemspec000066400000000000000000000016731430303753700217400ustar00rootroot00000000000000# frozen_string_literal: true 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", "Brian Morearty", "Bruno Sutic", "Olle Jonsson"] 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}/**/*', '*.md'], 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.6/readme.md000066400000000000000000000052611430303753700172500ustar00rootroot00000000000000# Protocol::HTTP1 Provides a low-level implementation of the HTTP/1 protocol. [![Development Status](https://github.com/socketry/protocol-http1/workflows/Test/badge.svg)](https://github.com/socketry/protocol-http1/actions?workflow=Test) ## 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.6/release.cert000066400000000000000000000033141430303753700177650ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIE2DCCA0CgAwIBAgIBATANBgkqhkiG9w0BAQsFADBhMRgwFgYDVQQDDA9zYW11 ZWwud2lsbGlhbXMxHTAbBgoJkiaJk/IsZAEZFg1vcmlvbnRyYW5zZmVyMRIwEAYK CZImiZPyLGQBGRYCY28xEjAQBgoJkiaJk/IsZAEZFgJuejAeFw0yMjA4MDYwNDUz MjRaFw0zMjA4MDMwNDUzMjRaMGExGDAWBgNVBAMMD3NhbXVlbC53aWxsaWFtczEd MBsGCgmSJomT8ixkARkWDW9yaW9udHJhbnNmZXIxEjAQBgoJkiaJk/IsZAEZFgJj bzESMBAGCgmSJomT8ixkARkWAm56MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIB igKCAYEAomvSopQXQ24+9DBB6I6jxRI2auu3VVb4nOjmmHq7XWM4u3HL+pni63X2 9qZdoq9xt7H+RPbwL28LDpDNflYQXoOhoVhQ37Pjn9YDjl8/4/9xa9+NUpl9XDIW sGkaOY0eqsQm1pEWkHJr3zn/fxoKPZPfaJOglovdxf7dgsHz67Xgd/ka+Wo1YqoE e5AUKRwUuvaUaumAKgPH+4E4oiLXI4T1Ff5Q7xxv6yXvHuYtlMHhYfgNn8iiW8WN XibYXPNP7NtieSQqwR/xM6IRSoyXKuS+ZNGDPUUGk8RoiV/xvVN4LrVm9upSc0ss RZ6qwOQmXCo/lLcDUxJAgG95cPw//sI00tZan75VgsGzSWAOdjQpFM0l4dxvKwHn tUeT3ZsAgt0JnGqNm2Bkz81kG4A2hSyFZTFA8vZGhp+hz+8Q573tAR89y9YJBdYM zp0FM4zwMNEUwgfRzv1tEVVUEXmoFCyhzonUUw4nE4CFu/sE3ffhjKcXcY//qiSW xm4erY3XAgMBAAGjgZowgZcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0O BBYEFO9t7XWuFf2SKLmuijgqR4sGDlRsMC4GA1UdEQQnMCWBI3NhbXVlbC53aWxs aWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MC4GA1UdEgQnMCWBI3NhbXVlbC53aWxs aWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MA0GCSqGSIb3DQEBCwUAA4IBgQB5sxkE cBsSYwK6fYpM+hA5B5yZY2+L0Z+27jF1pWGgbhPH8/FjjBLVn+VFok3CDpRqwXCl xCO40JEkKdznNy2avOMra6PFiQyOE74kCtv7P+Fdc+FhgqI5lMon6tt9rNeXmnW/ c1NaMRdxy999hmRGzUSFjozcCwxpy/LwabxtdXwXgSay4mQ32EDjqR1TixS1+smp 8C/NCWgpIfzpHGJsjvmH2wAfKtTTqB9CVKLCWEnCHyCaRVuKkrKjqhYCdmMBqCws JkxfQWC+jBVeG9ZtPhQgZpfhvh+6hMhraUYRQ6XGyvBqEUe+yo6DKIT3MtGE2+CP eX9i9ZWBydWb8/rvmwmX2kkcBbX0hZS1rcR593hGc61JR6lvkGYQ2MYskBveyaxt Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8 voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg= -----END CERTIFICATE----- ruby-protocol-http1-0.14.6/spec/000077500000000000000000000000001430303753700164175ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/spec/protocol/000077500000000000000000000000001430303753700202605ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/spec/protocol/http1/000077500000000000000000000000001430303753700213205ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/spec/protocol/http1/body/000077500000000000000000000000001430303753700222555ustar00rootroot00000000000000ruby-protocol-http1-0.14.6/spec/protocol/http1/body/chunked_spec.rb000066400000000000000000000030711430303753700252360ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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 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.6/spec/protocol/http1/body/fixed_spec.rb000066400000000000000000000036061430303753700247200ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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 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.6/spec/protocol/http1/body/remainder_spec.rb000066400000000000000000000024001430303753700255560ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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 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.6/spec/protocol/http1/connection_context.rb000066400000000000000000000006371430303753700255560ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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.6/spec/protocol/http1/connection_spec.rb000066400000000000000000000210631430303753700250200ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. # Copyright, 2020, by Bruno Sutic. 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.6/spec/protocol/http1/hijack_spec.rb000066400000000000000000000027231430303753700241140ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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.6/spec/protocol/http1/trailer_spec.rb000066400000000000000000000025631430303753700243270ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2022, by Samuel Williams. require 'protocol/http1/connection' require 'protocol/http/body/buffered' require_relative 'connection_context' RSpec.describe Protocol::HTTP1::Connection do include_context Protocol::HTTP1::Connection let(:chunks) {["Hello", "World"]} let(:body) {::Protocol::HTTP::Body::Buffered.wrap(chunks)} let(:trailer) {Hash.new} context 'with trailers' do it "ignores trailers with HTTP/1.0" do expect(server).to receive(:write_fixed_length_body) server.write_body("HTTP/1.0", body, false, trailer) end it "ignores trailers with content length" do expect(server).to receive(:write_fixed_length_body) server.write_body("HTTP/1.1", body, false, trailer) end it "uses chunked encoding when given trailers without content length" do expect(body).to receive(:length).and_return(nil) trailer['foo'] = 'bar' server.write_response("HTTP/1.1", 200, {}) server.write_body("HTTP/1.1", body, false, trailer) version, status, reason, headers, body = client.read_response("GET") expect(version).to be == 'HTTP/1.1' expect(status).to be == 200 expect(headers).to be == {} # Read all of the response body, including trailers: body.join # Headers are updated: expect(headers).to be == {'foo' => ['bar']} end end end ruby-protocol-http1-0.14.6/spec/protocol/http1/upgrade_spec.rb000066400000000000000000000016041430303753700243070ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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.6/spec/protocol/http1_spec.rb000066400000000000000000000004121430303753700226540ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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.6/spec/spec_helper.rb000066400000000000000000000007551430303753700212440ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. 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