Class: Caldecott::Client::HttpTunnel

Inherits:
Tunnel
  • Object
show all
Defined in:
lib/caldecott-client/tunnel/http_tunnel.rb

Constant Summary collapse

MAX_RETRIES =
10

Instance Attribute Summary collapse

Attributes inherited from Tunnel

#dst_host, #dst_port, #logger, #token, #url

Instance Method Summary collapse

Methods inherited from Tunnel

#close, #closed?, for_url

Constructor Details

#initialize(url, options) ⇒ HttpTunnel

Returns a new instance of HttpTunnel.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/caldecott-client/tunnel/http_tunnel.rb', line 14

def initialize(url, options)
  super

  begin
    @tun_url = URI.parse(url)
  rescue URI::Error
    raise ClientError, "Parsing tunnel URL failed"
  end

  @path_in = nil
  @path_out = nil
  @write_seq = nil
  @read_seq = nil
end

Instance Attribute Details

#path_inObject (readonly)

Returns the value of attribute path_in.



12
13
14
# File 'lib/caldecott-client/tunnel/http_tunnel.rb', line 12

def path_in
  @path_in
end

#path_outObject (readonly)

Returns the value of attribute path_out.



12
13
14
# File 'lib/caldecott-client/tunnel/http_tunnel.rb', line 12

def path_out
  @path_out
end

#read_seqObject (readonly)

Returns the value of attribute read_seq.



12
13
14
# File 'lib/caldecott-client/tunnel/http_tunnel.rb', line 12

def read_seq
  @read_seq
end

#write_seqObject (readonly)

Returns the value of attribute write_seq.



12
13
14
# File 'lib/caldecott-client/tunnel/http_tunnel.rb', line 12

def write_seq
  @write_seq
end

Instance Method Details

#readObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/caldecott-client/tunnel/http_tunnel.rb', line 71

def read
  if @path_out.nil? || @read_seq.nil?
    raise ClientError, "Cannot read, tunnel isn't ready"
  end

  req = Net::HTTP::Get.new(@path_out + "/#{@read_seq}")
  resp = request(req, "reading data", [200, 404, 410])

  @read_seq += 1

  if [404, 410].include?(resp.code.to_i)
    close
    return ""
  end

  resp.body
end

#request(req, msg, success_codes) ⇒ Object



96
97
98
99
100
101
102
103
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
# File 'lib/caldecott-client/tunnel/http_tunnel.rb', line 96

def request(req, msg, success_codes)
  req["Auth-Token"] = token
  retries = 0
  resp = nil

  loop do
    retries += 1
    logger.info("#{msg.capitalize} (retries=#{retries})")

    begin
      http = Net::HTTP.new(@tun_url.host, @tun_url.port)

      if @tun_url.scheme == "https"
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end

      resp = http.request(req)

      break if success_codes.include?(resp.code.to_i)

      logger.debug("Failed #{msg}: HTTP #{resp.code.to_i}")
    rescue Timeout::Error
      logger.error("Failed #{msg}: Timeout error")
    rescue EOFError # HTTP quirk?
      logger.debug("Failed #{msg}: #{e.message}")
    rescue StandardError => e # To satisfy current specs, do we need this really?
      logger.error("Failed #{msg}: #{e.message}")
    end

    if retries >= MAX_RETRIES
      raise ServerError, "Failed #{msg}"
    end
  end

  resp
end

#startObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/caldecott-client/tunnel/http_tunnel.rb', line 29

def start
  req = Net::HTTP::Post.new("/tunnels")
  req.body = JSON.generate(:host => dst_host, :port => dst_port)
  req["Content-Type"] = "application/json"

  resp = request(req, "creating tunnel remotely", [200, 201, 204])

  begin
    payload = JSON.parse(resp.body)
  rescue
    raise ClientError, "Parsing response data failed"
  end

  @tunnel_path = payload["path"]
  @path_in = payload["path_in"]
  @path_out = payload["path_out"]

  logger.info("Init success: tunnel_path=#{@tunnel_path}, " +
                  "path_in=#{@path_in}, path_out=#{@path_out}")
  @read_seq = 1
  @write_seq = 1
end

#stopObject



89
90
91
92
93
94
# File 'lib/caldecott-client/tunnel/http_tunnel.rb', line 89

def stop
  return unless @tunnel_path # failed to start
  req = Net::HTTP::Delete.new(@tunnel_path)
  request(req, "closing remote tunnel", [200, 202, 204, 404])
  close
end

#write(data) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/caldecott-client/tunnel/http_tunnel.rb', line 52

def write(data)
  if @path_in.nil? || @write_seq.nil?
    raise ClientError, "Cannot write, tunnel isn't ready"
  end

  req = Net::HTTP::Put.new(@path_in + "/#{@write_seq}")
  req.body = data
  logger.debug("Sending #{data.bytesize} bytes")

  resp = request(req, "sending data", [200, 202, 204, 404, 410])

  if [404, 410].include?(resp.code.to_i)
    close
    return
  end

  @write_seq += 1
end