diff --git a/.rubocop.yml b/.rubocop.yml index dd69bab0..83a79869 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -42,6 +42,7 @@ Naming/FileName: Exclude: - Gemfile - Rakefile + - Steepfile Layout/EndAlignment: EnforcedStyleAlignWith: variable diff --git a/lib/httpx/response.rb b/lib/httpx/response.rb index 66268177..ef30234e 100644 --- a/lib/httpx/response.rb +++ b/lib/httpx/response.rb @@ -173,7 +173,7 @@ module HTTPX rescue ArgumentError @buffer.string end - when Tempfile, File + when Tempfile rewind content = _with_same_buffer_pos { @buffer.read } begin @@ -253,6 +253,7 @@ module HTTPX @buffer = StringIO.new("".b) end when :memory + # @type ivar @buffer: StringIO | Tempfile if @length > @threshold_size aux = @buffer @buffer = Tempfile.new("httpx", encoding: Encoding::BINARY, mode: File::RDWR) @@ -272,11 +273,12 @@ module HTTPX def _with_same_buffer_pos return yield unless @buffer && @buffer.respond_to?(:pos) + # @type ivar @buffer: StringIO | Tempfile current_pos = @buffer.pos @buffer.rewind begin yield - rescue StandardError + ensure @buffer.pos = current_pos end end diff --git a/lib/httpx/timers.rb b/lib/httpx/timers.rb index 532a6738..213d3384 100644 --- a/lib/httpx/timers.rb +++ b/lib/httpx/timers.rb @@ -64,7 +64,7 @@ module HTTPX end def to_f - @interval + Float(@interval) end def <<(callback) diff --git a/lib/httpx/transcoder/chunker.rb b/lib/httpx/transcoder/chunker.rb index ca98076c..16618ff9 100644 --- a/lib/httpx/transcoder/chunker.rb +++ b/lib/httpx/transcoder/chunker.rb @@ -40,7 +40,6 @@ module HTTPX::Transcoder def initialize(buffer, trailers = false) @buffer = buffer - @chunk_length = nil @chunk_buffer = "".b @finished = false @state = :length diff --git a/sig/errors.rbs b/sig/errors.rbs index fd96899c..74c026a7 100644 --- a/sig/errors.rbs +++ b/sig/errors.rbs @@ -26,6 +26,14 @@ module HTTPX class ResolveError < Error end + class HTTPError < Error + attr_reader response: Response + + private + + def initialize: (Response response) -> void + end + class NativeResolveError < ResolveError attr_reader connection: Connection attr_reader host: String diff --git a/sig/parser/http1.rbs b/sig/parser/http1.rbs index d666ff57..37deccab 100644 --- a/sig/parser/http1.rbs +++ b/sig/parser/http1.rbs @@ -1,5 +1,8 @@ module HTTPX module Parser + class Error < HTTPX::Error + end + type parsed_headers = Hash[String, Array[String]] interface _HTTP1Events @@ -14,16 +17,22 @@ module HTTPX class HTTP1 VERSIONS: Array[String] + attr_reader status_code: Integer + attr_reader http_version: [Integer, Integer] + attr_reader headers: parsed_headers + + @observer: _HTTP1Events + @state: Symbol + @buffer: String + @content_type: String? + @content_length: Integer + @_has_trailers: bool + @upgrade: bool + def <<: (String chunk) -> void - def headers: () -> parsed_headers - - def http_version: () -> Array[1 | 0] - def reset!: () -> void - def status_code: () -> Integer - def upgrade?: () -> bool def upgrade_data: () -> String diff --git a/sig/registry.rbs b/sig/registry.rbs index 2148a916..0b4a6adc 100644 --- a/sig/registry.rbs +++ b/sig/registry.rbs @@ -1,4 +1,7 @@ module HTTPX::Registry[T, V] + class Error < HTTPX::Error + end + # type registrable = Symbol | String | Class def self.registry: (T tag) -> Class diff --git a/sig/request.rbs b/sig/request.rbs index 65ccff1f..1c1f30aa 100644 --- a/sig/request.rbs +++ b/sig/request.rbs @@ -15,6 +15,11 @@ module HTTPX attr_reader response: response? attr_reader drain_error: StandardError? + @trailers: Headers? + @informational_status: Integer? + @query: String? + @drainer: Enumerator[String, void]? + def initialize: (Symbol | String, generic_uri, ?options) -> untyped def interests: () -> (:r | :w) @@ -46,6 +51,10 @@ module HTTPX def trailers?: () -> boolish class Body + @headers: Headers + @body: body_encoder? + @unbounded_body: bool + def initialize: (Headers, Options) -> untyped def each: () { (String) -> void } -> void | () -> Enumerable[String] @@ -61,6 +70,8 @@ module HTTPX end class ProcIO + @block: ^(String) -> void + def initialize: (^(String) -> void) -> untyped def write: (String data) -> Integer diff --git a/sig/resolver.rbs b/sig/resolver.rbs index 5e16273b..c54f18e1 100644 --- a/sig/resolver.rbs +++ b/sig/resolver.rbs @@ -2,12 +2,11 @@ module HTTPX type ipaddr = IPAddr | String module Resolver + extend Registry[Symbol, Class] + RESOLVE_TIMEOUT: Integer | Float - def self.registry: (Symbol tag) -> Class - | () -> Hash[Symbol, Class] - - def self.register: (Symbol tag, Class handler) -> void + @lookup_mutex: Thread::Mutex type dns_resource = singleton(Resolv::DNS::Resource) diff --git a/sig/response.rbs b/sig/response.rbs index 235e7bba..712e0262 100644 --- a/sig/response.rbs +++ b/sig/response.rbs @@ -19,6 +19,7 @@ module HTTPX @options: Options @request: Request + @content_type: ContentType def copy_to: (_ToPath | _Writer destination) -> void def close: () -> void @@ -45,9 +46,15 @@ module HTTPX include _ToS include _ToStr + @response: Response + @headers: Headers + @options: Options @state: :idle | :memory | :buffer | :closed @threshold_size: Integer @window_size: Integer + @encoding: String + @length: Integer + @buffer: StringIO | Tempfile | nil def write:(String chunk) -> Integer? @@ -65,6 +72,7 @@ module HTTPX def initialize: (Response, Options) -> untyped def rewind: () -> void def transition: () -> void + def _with_same_buffer_pos: [A] () { () -> A } -> A end end @@ -72,12 +80,13 @@ module HTTPX MIME_TYPE_RE: Regexp CHARSET_RE: Regexp - attr_reader mime_type: String? - attr_reader charset: String? + @header_value: String? + @mime_type: String? + @charset: String? - def self.parse: (_ToS) -> ContentType - def self.mime_type: (_ToS) -> String? - def self.charset: (_ToS) -> String? + def mime_type: () -> String? + + def charset: () -> String? private @@ -89,6 +98,7 @@ module HTTPX include Loggable @options: Options + @error: Exception attr_reader request: Request diff --git a/sig/timers.rbs b/sig/timers.rbs index 77a0598f..ed98fb1e 100644 --- a/sig/timers.rbs +++ b/sig/timers.rbs @@ -1,12 +1,13 @@ module HTTPX class Timers - @interval: Array[Interval] + @intervals: Array[Interval] + @next_interval_at: Numeric def after: (Numeric interval_in_secs) { () -> void } -> void def wait_interval: () -> Numeric? - def fire: (?StandardError error) -> void + def fire: (?TimeoutError error) -> void def cancel: () -> void @@ -19,6 +20,8 @@ module HTTPX attr_reader interval: Numeric + @callbacks: Array[^() -> void] + def to_f: () -> Float def <<: (^() -> void) -> void diff --git a/sig/transcoder.rbs b/sig/transcoder.rbs index d40aad5e..087de6d5 100644 --- a/sig/transcoder.rbs +++ b/sig/transcoder.rbs @@ -1,6 +1,8 @@ module HTTPX type bodyIO = _Reader | _Each[[String, untyped]] | _ToS + type body_encoder = Transcoder::_Encoder | _Each[String] + module Transcoder def self?.registry: (String tag) -> _Encode | () -> Hash[String, _Encode] @@ -12,7 +14,7 @@ module HTTPX def self?.normalize_query: (Hash[String, untyped] params, String name, String v, Integer depth) -> void interface _Encode - def encode: (untyped payload) -> (_Encoder | _Each[String]) + def encode: (untyped payload) -> body_encoder end interface _Encoder diff --git a/sig/transcoder/chunker.rbs b/sig/transcoder/chunker.rbs index c94da421..f148d7ea 100644 --- a/sig/transcoder/chunker.rbs +++ b/sig/transcoder/chunker.rbs @@ -10,27 +10,38 @@ module HTTPX::Transcoder class Encoder @raw: _Each[String] - include _Each[String] + def each: () { (String) -> void } -> void + | () -> Enumerator[String, void] private - def initialize: (_Each[String] chunks) -> untyped + def initialize: (_Each[String] chunks) -> void end class Decoder extend Forwardable include _ToS - include _Each[String] + + @buffer: String + @chunk_length: Integer + @chunk_buffer: String + @finished: bool + @state: Symbol + @trailers: bool + + def each: () { (String) -> void } -> void def finished?: () -> bool + def empty?: () -> bool + def <<: (string) -> void + def clear: () -> void private - def initialize: (String, bool) -> untyped - | (String) -> untyped + def initialize: (String, ?bool) -> void def nextstate: (Symbol) -> void end diff --git a/sig/transcoder/json.rbs b/sig/transcoder/json.rbs index 27fbe209..a53ccf7a 100644 --- a/sig/transcoder/json.rbs +++ b/sig/transcoder/json.rbs @@ -1,5 +1,7 @@ module HTTPX::Transcoder module JSON + JSON_REGEX: Regexp + def self?.encode: (_ToJson json) -> Encoder def self?.decode: (HTTPX::Response response) -> _Decoder @@ -8,6 +10,9 @@ module HTTPX::Transcoder include _Encoder include _ToS + @raw: String + @charset: String + def content_type: () -> String private