Class: HTTPClient::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/httpclient.rb

Overview

HTTPClient::Session – manage http session with one site.

One or more TCP sessions with the site may be created.
Only 1 TCP session is live at the same time.

Direct Known Subclasses

HTTPAccess2::Session

Defined Under Namespace

Classes: BadResponse, Error, InvalidState, KeepAliveDisconnected

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dest, user_agent, from) ⇒ Session

Returns a new instance of Session.



1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
# File 'lib/httpclient.rb', line 1193

def initialize(dest, user_agent, from)
  @dest = dest
  @src = Site.new
  @proxy = nil
  @socket_sync = true
  @requested_version = nil

  @debug_dev = nil

  @connect_timeout = nil
  @connect_retry = 1
  @send_timeout = nil
  @receive_timeout = nil
  @read_block_size = nil

  @ssl_config = nil
  @ssl_peer_cert = nil

  @test_loopback_http_response = nil

  @user_agent = user_agent
  @from = from
  @state = :INIT

  @requests = []

  @status = nil
  @reason = nil
  @headers = []

  @socket = nil
end

Instance Attribute Details

#connect_retryObject

Returns the value of attribute connect_retry.



1184
1185
1186
# File 'lib/httpclient.rb', line 1184

def connect_retry
  @connect_retry
end

#connect_timeoutObject

These session parameters are not used now…



1183
1184
1185
# File 'lib/httpclient.rb', line 1183

def connect_timeout
  @connect_timeout
end

#debug_devObject

Device for dumping log for debugging



1180
1181
1182
# File 'lib/httpclient.rb', line 1180

def debug_dev
  @debug_dev
end

#destObject (readonly)

Destination site



1173
1174
1175
# File 'lib/httpclient.rb', line 1173

def dest
  @dest
end

#proxyObject

Proxy site



1175
1176
1177
# File 'lib/httpclient.rb', line 1175

def proxy
  @proxy
end

#read_block_sizeObject

Returns the value of attribute read_block_size.



1187
1188
1189
# File 'lib/httpclient.rb', line 1187

def read_block_size
  @read_block_size
end

#receive_timeoutObject

Returns the value of attribute receive_timeout.



1186
1187
1188
# File 'lib/httpclient.rb', line 1186

def receive_timeout
  @receive_timeout
end

#requested_versionObject

Requested protocol version



1178
1179
1180
# File 'lib/httpclient.rb', line 1178

def requested_version
  @requested_version
end

#send_timeoutObject

Returns the value of attribute send_timeout.



1185
1186
1187
# File 'lib/httpclient.rb', line 1185

def send_timeout
  @send_timeout
end

#socket_syncObject

Boolean value for Socket#sync



1176
1177
1178
# File 'lib/httpclient.rb', line 1176

def socket_sync
  @socket_sync
end

#srcObject (readonly)

Source site



1174
1175
1176
# File 'lib/httpclient.rb', line 1174

def src
  @src
end

#ssl_configObject

Returns the value of attribute ssl_config.



1189
1190
1191
# File 'lib/httpclient.rb', line 1189

def ssl_config
  @ssl_config
end

#ssl_peer_certObject (readonly)

Returns the value of attribute ssl_peer_cert.



1190
1191
1192
# File 'lib/httpclient.rb', line 1190

def ssl_peer_cert
  @ssl_peer_cert
end

#test_loopback_http_responseObject

Returns the value of attribute test_loopback_http_response.



1191
1192
1193
# File 'lib/httpclient.rb', line 1191

def test_loopback_http_response
  @test_loopback_http_response
end

Instance Method Details

#closeObject



1255
1256
1257
1258
1259
1260
1261
# File 'lib/httpclient.rb', line 1255

def close
  if !@socket.nil? and !@socket.closed?
    @socket.flush rescue nil  # try to rescue OpenSSL::SSL::SSLError: cf. #120
    @socket.close
  end
  @state = :INIT
end

#closed?Boolean

Returns:

  • (Boolean)


1263
1264
1265
# File 'lib/httpclient.rb', line 1263

def closed?
  @state == :INIT
end

#eof?Boolean

Returns:

  • (Boolean)


1297
1298
1299
1300
1301
1302
1303
1304
1305
# File 'lib/httpclient.rb', line 1297

def eof?
  if !@content_length.nil?
    @content_length == 0
  elsif @readbuf.length > 0
    false
  else
    @socket.closed? or @socket.eof?
  end
end

#get_data(&block) ⇒ Object



1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
# File 'lib/httpclient.rb', line 1307

def get_data(&block)
  begin
    read_header() if @state == :META
    return nil if @state != :DATA
    unless @state == :DATA
      raise InvalidState.new('state != DATA')
    end
    data = nil
    if block
      while true
        begin
          timeout(@receive_timeout) do
            data = read_body()
          end
        rescue TimeoutError
          raise
        end
        block.call(data) if data
        break if eof?
      end
      data = nil      # Calling with block returns nil.
    else
      begin
        timeout(@receive_timeout) do
          data = read_body()
        end
      rescue TimeoutError
        raise
      end
    end
  rescue
    close
    raise
  end
  if eof?
    if @next_connection
      @state = :WAIT
    else
      close
    end
  end
  data
end

#get_header(&block) ⇒ Object



1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
# File 'lib/httpclient.rb', line 1281

def get_header(&block)
  begin
    read_header() if @state == :META
  rescue
    close
    raise
  end
  if block
    @headers.each do |line|
      block.call(line)
    end
  else
    @headers
  end
end

#get_statusObject



1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
# File 'lib/httpclient.rb', line 1267

def get_status
  version = status = reason = nil
  begin
    if @state != :META
      raise RuntimeError.new("get_status must be called at the beginning of a session.")
    end
    version, status, reason = read_header()
  rescue
    close
    raise
  end
  return version, status, reason
end

#query(req) ⇒ Object

Send a request to the server



1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
# File 'lib/httpclient.rb', line 1227

def query(req)
  connect() if @state == :INIT
  begin
    timeout(@send_timeout) do
      set_header(req)
      req.dump(@socket)
      # flush the IO stream as IO::sync mode is false
      @socket.flush unless @socket_sync
    end
  rescue Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EPIPE
    close
    raise KeepAliveDisconnected.new
  rescue
    if SSLEnabled and $!.is_a?(OpenSSL::SSL::SSLError)
      raise KeepAliveDisconnected.new
    elsif $!.is_a?(TimeoutError)
      close
      raise
    else
      raise
    end
  end

  @state = :META if @state == :WAIT
  @next_connection = nil
  @requests.push(req)
end