Class: OMGF::VerifyPaths::HeadSock

Inherits:
Kgio::Socket
  • Object
show all
Defined in:
lib/omgf/verify_paths.rb

Overview

private class

Constant Summary collapse

VERSION =

:nodoc:

'1.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#retry_ok=(value) ⇒ Object (writeonly)

Sets the attribute retry_ok

Parameters:

  • value

    the value to set the attribute retry_ok to.



22
23
24
# File 'lib/omgf/verify_paths.rb', line 22

def retry_ok=(value)
  @retry_ok = value
end

#uriObject (readonly)

Returns the value of attribute uri.



21
22
23
# File 'lib/omgf/verify_paths.rb', line 21

def uri
  @uri
end

Class Method Details

.start(uri) ⇒ Object



24
25
26
# File 'lib/omgf/verify_paths.rb', line 24

def self.start(uri)
  super(Socket.pack_sockaddr_in(uri.port, uri.host))
end

Instance Method Details

#http_init(uri, retry_ok = true) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/omgf/verify_paths.rb', line 39

def http_init(uri, retry_ok = true)
  @uri = uri
  unless defined?(@kcar)
    @kcar = Kcar::Parser.new
    @headers = {}
    @buf = ""
  end
  @retry_ok = retry_ok

  # prepare the HTTP request
  @req = "HEAD #{@uri.request_uri} HTTP/1.1\r\n" \
         "User-Agent: #{self.class}/#{VERSION}\r\n" \
         "Host: #{@uri.host}:#{@uri.port}\r\n" \
         "\r\n"
end

#http_reusable?Boolean

returns true if the HTTP connection is reusable

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
# File 'lib/omgf/verify_paths.rb', line 29

def http_reusable?
  rv = @kcar.keepalive?
  @kcar.reset
  @headers.clear
  @buf.clear
  @uri = nil
  rv ? setsockopt(:SOL_SOCKET, :SO_KEEPALIVE, 1) : close
  rv
end

#maybe_retry(err, pollset) ⇒ Object

returns the err object if we’ve already retried, nil otherwise



85
86
87
88
89
90
91
92
93
# File 'lib/omgf/verify_paths.rb', line 85

def maybe_retry(err, pollset)
  pollset.delete(self)
  return err unless @retry_ok

  # always start a fresh connection on socket errors
  sock = self.class.start(@uri)
  sock.http_init(@uri, false)
  pollset[sock] = :wait_writable
end

#poll_iter(pollset) ⇒ Object

returns an array result if successful returns :wait_readable or :wait_writable if incomplete returns a subclass of Exception on errors returns nil on premature EOF (twice)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/omgf/verify_paths.rb', line 59

def poll_iter(pollset)
  case rv = kgio_trywrite(@req)
  when nil, # done writing, start reading
       String # partial write
    @req = rv # continue looping
  when Symbol
    return pollset[self] = rv # busy
  end while @req

  case rv = kgio_tryread(666)
  when String
    if ary = @kcar.headers(@headers, @buf << rv)
      pollset.delete(self)
      return ary # success
    end
    # continue looping if incomplete
  when Symbol
    return pollset[self] = rv # busy
  when nil # EOF (premature)
    return maybe_retry(nil, pollset)
  end while true
rescue => err
  maybe_retry(err, pollset)
end