Class: Tanker::CTanker::CHttpResponse

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/tanker/c_tanker/c_http.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_msg, status_code, headers, body) ⇒ CHttpResponse

Returns a new instance of CHttpResponse.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tanker/c_tanker/c_http.rb', line 47

def initialize(error_msg, status_code, headers, body)
  super()

  if error_msg
    @error_msg = CTanker.new_cstring(error_msg)
    self[:error_msg] = @error_msg
  else
    raise TypeError, 'headers argument is not an Array[HttpHeader]' unless headers.is_a?(Array)

    @body = FFI::MemoryPointer.from_string(body)

    self[:error_msg] = nil
    self[:num_headers] = headers.length
    self[:body] = @body
    self[:body_size] = body.bytesize
    self[:status_code] = status_code

    @headers = []
    self[:headers] = FFI::MemoryPointer.new(CHttpResponseHeader, self[:num_headers])
    headers.each_with_index do |header, idx|
      @headers.push(CHttpResponseHeader.new(header.name, header.value))
      # NOTE: memcopy
      str = @headers[idx].pointer.read_bytes CHttpResponseHeader.size
      self[:headers].put_bytes(idx * CHttpResponseHeader.size, str, 0, CHttpResponseHeader.size)
    end
  end
end

Class Method Details

.new_error(msg) ⇒ Object



43
44
45
# File 'lib/tanker/c_tanker/c_http.rb', line 43

def self.new_error(msg)
  new msg, nil, nil, nil
end

.new_ok(status_code:, headers:, body:) ⇒ Object



39
40
41
# File 'lib/tanker/c_tanker/c_http.rb', line 39

def self.new_ok(status_code:, headers:, body:)
  new nil, status_code, headers, body
end