Class: SFML::Network::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/network/http.rb

Overview

CSFML's tiny HTTP/1.x client. Useful when an SFML 3 game wants to talk to a leaderboard / asset server without pulling in Net::HTTP. For anything more involved (TLS, redirects, streaming, retries, JSON), Ruby's stdlib Net::HTTP is the better tool — this wrapper exists for parity with CSFML, not because we recommend it.

http = SFML::Network::Http.new("http://localhost", port: 8080)
resp = http.send_request(method: :get, uri: "/")
resp.status   #=> 200
resp.body     #=> "Hello world\n"

send_request accepts:

method:       :get / :post / :head / :put / :delete (default :get)
uri:          path string (default "/")
fields:       Hash of header name → value
body:         String body (POST/PUT)
http_version: [major, minor] (default [1, 0])
timeout:      SFML::Time or seconds (default 0 = no timeout)

Defined Under Namespace

Classes: Response

Constant Summary collapse

DEFAULT_TIMEOUT =

Default per-call timeout.

SFML::Time.zero
DEFAULT_VERSION =
[1, 0].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port: 0) ⇒ Http

Build an HTTP client pinned to host (URL prefix). Pass port: 0 to let CSFML pick the protocol's default port.

Raises:



29
30
31
32
33
34
35
# File 'lib/sfml/network/http.rb', line 29

def initialize(host, port: 0)
  ptr = C::Network.sfHttp_create
  raise NetworkError, "sfHttp_create returned NULL" if ptr.null?

  @handle = FFI::AutoPointer.new(ptr, C::Network.method(:sfHttp_destroy))
  C::Network.sfHttp_setHost(@handle, host.to_s, Integer(port))
end

Instance Attribute Details

#handle ⇒ Object (readonly)

:nodoc:



66
67
68
# File 'lib/sfml/network/http.rb', line 66

def handle
  @handle
end

Instance Method Details

#send_request(method: :get, uri: "/", fields: nil, body: nil, http_version: DEFAULT_VERSION, timeout: DEFAULT_TIMEOUT) ⇒ Object

Send a request and wait for the response. See the class doc for all the kwargs.

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sfml/network/http.rb', line 39

def send_request(method: :get, uri: "/", fields: nil, body: nil,
                 http_version: DEFAULT_VERSION, timeout: DEFAULT_TIMEOUT)
  request_ptr = C::Network.sfHttpRequest_create
  raise NetworkError, "sfHttpRequest_create returned NULL" if request_ptr.null?

  begin
    method_idx = C::Network::HTTP_METHODS.index(method) ||
      raise(ArgumentError, "Unknown HTTP method: #{method.inspect} " \
                           "(expected one of #{C::Network::HTTP_METHODS.inspect})")
    C::Network.sfHttpRequest_setMethod(request_ptr, method_idx)
    C::Network.sfHttpRequest_setUri(request_ptr, uri.to_s)
    C::Network.sfHttpRequest_setHttpVersion(request_ptr, Integer(http_version[0]), Integer(http_version[1]))
    C::Network.sfHttpRequest_setBody(request_ptr, body.to_s) if body
    fields&.each_pair do |name, value|
      C::Network.sfHttpRequest_setField(request_ptr, name.to_s, value.to_s)
    end

    t = timeout.is_a?(Time) ? timeout : Time.seconds(timeout.to_f)
    response_ptr = C::Network.sfHttp_sendRequest(@handle, request_ptr, t.to_native)
    raise NetworkError, "sfHttp_sendRequest returned NULL" if response_ptr.null?

    Response.send(:_take_ownership, response_ptr)
  ensure
    C::Network.sfHttpRequest_destroy(request_ptr)
  end
end