adding support for tempfile sigs, other improvements

This commit is contained in:
HoneyryderChuck 2021-09-29 13:29:26 +01:00
parent 9bd73e5a22
commit 6d33b5e59f
14 changed files with 90 additions and 27 deletions

View File

@ -42,6 +42,7 @@ Naming/FileName:
Exclude:
- Gemfile
- Rakefile
- Steepfile
Layout/EndAlignment:
EnforcedStyleAlignWith: variable

View File

@ -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

View File

@ -64,7 +64,7 @@ module HTTPX
end
def to_f
@interval
Float(@interval)
end
def <<(callback)

View File

@ -40,7 +40,6 @@ module HTTPX::Transcoder
def initialize(buffer, trailers = false)
@buffer = buffer
@chunk_length = nil
@chunk_buffer = "".b
@finished = false
@state = :length

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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