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.



1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
# File 'lib/httpclient.rb', line 1276

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.



1267
1268
1269
# File 'lib/httpclient.rb', line 1267

def connect_retry
  @connect_retry
end

#connect_timeoutObject

These session parameters are not used now…



1266
1267
1268
# File 'lib/httpclient.rb', line 1266

def connect_timeout
  @connect_timeout
end

#debug_devObject

Device for dumping log for debugging



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

def debug_dev
  @debug_dev
end

#destObject (readonly)

Destination site



1256
1257
1258
# File 'lib/httpclient.rb', line 1256

def dest
  @dest
end

#proxyObject

Proxy site



1258
1259
1260
# File 'lib/httpclient.rb', line 1258

def proxy
  @proxy
end

#read_block_sizeObject

Returns the value of attribute read_block_size.



1270
1271
1272
# File 'lib/httpclient.rb', line 1270

def read_block_size
  @read_block_size
end

#receive_timeoutObject

Returns the value of attribute receive_timeout.



1269
1270
1271
# File 'lib/httpclient.rb', line 1269

def receive_timeout
  @receive_timeout
end

#requested_versionObject

Requested protocol version



1261
1262
1263
# File 'lib/httpclient.rb', line 1261

def requested_version
  @requested_version
end

#send_timeoutObject

Returns the value of attribute send_timeout.



1268
1269
1270
# File 'lib/httpclient.rb', line 1268

def send_timeout
  @send_timeout
end

#socket_syncObject

Boolean value for Socket#sync



1259
1260
1261
# File 'lib/httpclient.rb', line 1259

def socket_sync
  @socket_sync
end

#srcObject (readonly)

Source site



1257
1258
1259
# File 'lib/httpclient.rb', line 1257

def src
  @src
end

#ssl_configObject

Returns the value of attribute ssl_config.



1272
1273
1274
# File 'lib/httpclient.rb', line 1272

def ssl_config
  @ssl_config
end

#ssl_peer_certObject (readonly)

Returns the value of attribute ssl_peer_cert.



1273
1274
1275
# File 'lib/httpclient.rb', line 1273

def ssl_peer_cert
  @ssl_peer_cert
end

#test_loopback_http_responseObject

Returns the value of attribute test_loopback_http_response.



1274
1275
1276
# File 'lib/httpclient.rb', line 1274

def test_loopback_http_response
  @test_loopback_http_response
end

Instance Method Details

#closeObject



1338
1339
1340
1341
1342
1343
1344
# File 'lib/httpclient.rb', line 1338

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)


1346
1347
1348
# File 'lib/httpclient.rb', line 1346

def closed?
  @state == :INIT
end

#eof?Boolean

Returns:

  • (Boolean)


1380
1381
1382
1383
1384
1385
1386
1387
1388
# File 'lib/httpclient.rb', line 1380

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



1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
# File 'lib/httpclient.rb', line 1390

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



1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
# File 'lib/httpclient.rb', line 1364

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



1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
# File 'lib/httpclient.rb', line 1350

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



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
# File 'lib/httpclient.rb', line 1310

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