Module: Ethon::Easy::Callbacks Private

Included in:
Ethon::Easy
Defined in:
lib/ethon/easy/callbacks.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

This module contains all the logic around the callbacks, which are needed to interact with libcurl.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:


12
13
14
# File 'lib/ethon/easy/callbacks.rb', line 12

def self.included(base)
  base.send(:attr_accessor, *[:response_body, :response_headers, :debug_info])
end

Instance Method Details

#body_write_callbackProc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the body write callback.

Examples:

Return the callback.

easy.body_write_callback

Returns:

  • (Proc)

    The callback.


38
39
40
41
42
43
44
45
# File 'lib/ethon/easy/callbacks.rb', line 38

def body_write_callback
  @body_write_callback ||= proc do |stream, size, num, object|
    headers
    result = body(chunk = stream.read_string(size * num))
    @response_body << chunk if result == :unyielded
    result != :abort ? size * num : -1
  end
end

#debug_callbackProc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the debug callback. This callback is currently used write the raw http request headers.

Examples:

Return the callback.

easy.debug_callback

Returns:

  • (Proc)

    The callback.


67
68
69
70
71
72
73
74
# File 'lib/ethon/easy/callbacks.rb', line 67

def debug_callback
  @debug_callback ||= proc {|handle, type, data, size, udata|
    message = data.read_string(size)
    @debug_info.add type, message
    print message unless [:data_in, :data_out].include?(type)
    0
  }
end

#header_write_callbackProc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the header write callback.

Examples:

Return the callback.

easy.header_write_callback

Returns:

  • (Proc)

    The callback.


53
54
55
56
57
58
# File 'lib/ethon/easy/callbacks.rb', line 53

def header_write_callback
  @header_write_callback ||= proc {|stream, size, num, object|
    @response_headers << stream.read_string(size * num)
    size * num
  }
end

#progress_callbackProc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the progress callback.

Examples:

Return the callback.

easy.progress_callback

Returns:

  • (Proc)

    The callback.


90
91
92
93
94
95
# File 'lib/ethon/easy/callbacks.rb', line 90

def progress_callback
  @progress_callback ||= proc { |_, dltotal, dlnow, ultotal, ulnow|
    progress(dltotal, dlnow, ultotal, ulnow)
    0
  }
end

#read_callbackProc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the body read callback.

Examples:

Return the callback.

easy.read_callback

Returns:

  • (Proc)

    The callback.


143
144
145
# File 'lib/ethon/easy/callbacks.rb', line 143

def read_callback
  @read_callback
end

#set_callbacksObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set writefunction and headerfunction callback. They are called by libcurl in order to provide the header and the body from the request.

Examples:

Set callbacks.

easy.set_callbacks

22
23
24
25
26
27
28
29
30
# File 'lib/ethon/easy/callbacks.rb', line 22

def set_callbacks
  Curl.set_option(:writefunction, body_write_callback, handle)
  Curl.set_option(:headerfunction, header_write_callback, handle)
  Curl.set_option(:debugfunction, debug_callback, handle)
  @response_body = String.new
  @response_headers = String.new
  @headers_called = false
  @debug_info = Ethon::Easy::DebugInfo.new
end

#set_progress_callbackObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


76
77
78
79
80
81
82
# File 'lib/ethon/easy/callbacks.rb', line 76

def set_progress_callback
  if Curl.version_info[:version] >= "7.32.0"
    Curl.set_option(:xferinfofunction, progress_callback, handle)
  else
    Curl.set_option(:progressfunction, progress_callback, handle)
  end
end

#set_read_callback(body) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set the read callback. This callback is used by libcurl to read data when performing a PUT request.

Examples:

Set the callback.

easy.set_read_callback("a=1")

Parameters:

  • body (String)

    The body.


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ethon/easy/callbacks.rb', line 104

def set_read_callback(body)
  @request_body_read = 0
  readfunction do |stream, size, num, object|
    size = size * num
    body_size = if body.respond_to?(:bytesize)
      body.bytesize
    elsif body.respond_to?(:size)
      body.size
    elsif body.is_a?(File)
      File.size(body.path)
    end

    left = body_size - @request_body_read
    size = left if size > left

    if size > 0
      chunk = if body.respond_to?(:byteslice)
        body.byteslice(@request_body_read, size)
      elsif body.respond_to?(:read)
        body.read(size)
      else
        body[@request_body_read, size]
      end

      stream.write_string(
        chunk, size
      )
      @request_body_read += size
    end
    size
  end
end