Class: Syntropy::HTTP::ClientConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/syntropy/http/client_connection.rb

Overview

ClientConnection represents an HTTP client connection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, fd, io_mode: :socket) ⇒ void

Initializes a ClientConnection.

Parameters:

  • machine (UringMachine)

    machine instance

  • fd (Integer)

    file descriptor

  • io_mode (Symbol) (defaults to: :socket)

    IO mode



18
19
20
21
22
# File 'lib/syntropy/http/client_connection.rb', line 18

def initialize(machine, fd, io_mode: :socket)
  @machine = machine
  @fd = fd
  @io = machine.io(fd, io_mode)
end

Instance Attribute Details

#fdObject (readonly)

Returns the value of attribute fd.



10
11
12
# File 'lib/syntropy/http/client_connection.rb', line 10

def fd
  @fd
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/syntropy/http/client_connection.rb', line 10

def logger
  @logger
end

#response_headersObject (readonly)

Returns the value of attribute response_headers.



10
11
12
# File 'lib/syntropy/http/client_connection.rb', line 10

def response_headers
  @response_headers
end

Instance Method Details

#get_response_body(headers) ⇒ String?

Returns the response body

Parameters:

  • headers (Hash)

    response headers

Returns:

  • (String, nil)

    response body



47
48
49
# File 'lib/syntropy/http/client_connection.rb', line 47

def get_response_body(headers)
  @io.http_read_body(headers)
end

#req(body: nil, **headers) ⇒ Hash

Performs a request with the given body and headers.

Parameters:

  • body (String, nil) (defaults to: nil)

    request body

  • headers (Hash)

    request headers

Returns:

  • (Hash)

    response headers



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/syntropy/http/client_connection.rb', line 29

def req(body: nil, **headers)
  if body
    headers = headers.merge(
      'Content-Length' => body.bytesize
    )
  end
  @io.http_write_request_headers(**headers)
  if body
    @io.write(body)
  end

  @io.http_read_response_headers
end