Class: HTTPAccess2::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/http-access2.rb

Overview

HTTPAccess2::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.

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.



1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
# File 'lib/http-access2.rb', line 1177

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

  @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.



1170
1171
1172
# File 'lib/http-access2.rb', line 1170

def connect_retry
  @connect_retry
end

#connect_timeoutObject

These session parameters are not used now…



1169
1170
1171
# File 'lib/http-access2.rb', line 1169

def connect_timeout
  @connect_timeout
end

#debug_devObject

Device for dumping log for debugging



1166
1167
1168
# File 'lib/http-access2.rb', line 1166

def debug_dev
  @debug_dev
end

#destObject (readonly)

Destination site



1159
1160
1161
# File 'lib/http-access2.rb', line 1159

def dest
  @dest
end

#proxyObject

Proxy site



1161
1162
1163
# File 'lib/http-access2.rb', line 1161

def proxy
  @proxy
end

#read_block_sizeObject

Returns the value of attribute read_block_size.



1173
1174
1175
# File 'lib/http-access2.rb', line 1173

def read_block_size
  @read_block_size
end

#receive_timeoutObject

Returns the value of attribute receive_timeout.



1172
1173
1174
# File 'lib/http-access2.rb', line 1172

def receive_timeout
  @receive_timeout
end

#requested_versionObject

Requested protocol version



1164
1165
1166
# File 'lib/http-access2.rb', line 1164

def requested_version
  @requested_version
end

#send_timeoutObject

Returns the value of attribute send_timeout.



1171
1172
1173
# File 'lib/http-access2.rb', line 1171

def send_timeout
  @send_timeout
end

#socket_syncObject

Boolean value for Socket#sync



1162
1163
1164
# File 'lib/http-access2.rb', line 1162

def socket_sync
  @socket_sync
end

#srcObject (readonly)

Source site



1160
1161
1162
# File 'lib/http-access2.rb', line 1160

def src
  @src
end

#ssl_configObject

Returns the value of attribute ssl_config.



1175
1176
1177
# File 'lib/http-access2.rb', line 1175

def ssl_config
  @ssl_config
end

Instance Method Details

#closeObject



1236
1237
1238
1239
1240
1241
1242
# File 'lib/http-access2.rb', line 1236

def close
  unless @socket.nil?
    @socket.flush
    @socket.close unless @socket.closed?
  end
  @state = :INIT
end

#closed?Boolean

Returns:

  • (Boolean)


1244
1245
1246
# File 'lib/http-access2.rb', line 1244

def closed?
  @state == :INIT
end

#eof?Boolean

Returns:

  • (Boolean)


1278
1279
1280
1281
1282
1283
1284
1285
1286
# File 'lib/http-access2.rb', line 1278

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

#get_data(&block) ⇒ Object



1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
# File 'lib/http-access2.rb', line 1288

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
	until eof?
 begin
   timeout(@receive_timeout) do
     data = read_body()
   end
 rescue TimeoutError
   raise
 end
 block.call(data) if data
	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



1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
# File 'lib/http-access2.rb', line 1262

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



1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
# File 'lib/http-access2.rb', line 1248

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



1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
# File 'lib/http-access2.rb', line 1208

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
    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