Class: HTTPAccess2::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/reap/vendor/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.



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
1206
1207
# File 'lib/reap/vendor/http-access2.rb', line 1179

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.



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

def connect_retry
  @connect_retry
end

#connect_timeoutObject

These session parameters are not used now…



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

def connect_timeout
  @connect_timeout
end

#debug_devObject

Device for dumping log for debugging



1168
1169
1170
# File 'lib/reap/vendor/http-access2.rb', line 1168

def debug_dev
  @debug_dev
end

#destObject (readonly)

Destination site



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

def dest
  @dest
end

#proxyObject

Proxy site



1163
1164
1165
# File 'lib/reap/vendor/http-access2.rb', line 1163

def proxy
  @proxy
end

#read_block_sizeObject

Returns the value of attribute read_block_size.



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

def read_block_size
  @read_block_size
end

#receive_timeoutObject

Returns the value of attribute receive_timeout.



1174
1175
1176
# File 'lib/reap/vendor/http-access2.rb', line 1174

def receive_timeout
  @receive_timeout
end

#requested_versionObject

Requested protocol version



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

def requested_version
  @requested_version
end

#send_timeoutObject

Returns the value of attribute send_timeout.



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

def send_timeout
  @send_timeout
end

#socket_syncObject

Boolean value for Socket#sync



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

def socket_sync
  @socket_sync
end

#srcObject (readonly)

Source site



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

def src
  @src
end

#ssl_configObject

Returns the value of attribute ssl_config.



1177
1178
1179
# File 'lib/reap/vendor/http-access2.rb', line 1177

def ssl_config
  @ssl_config
end

Instance Method Details

#closeObject



1238
1239
1240
1241
1242
1243
1244
# File 'lib/reap/vendor/http-access2.rb', line 1238

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

#closed?Boolean

Returns:

  • (Boolean)


1246
1247
1248
# File 'lib/reap/vendor/http-access2.rb', line 1246

def closed?
  @state == :INIT
end

#eof?Boolean

Returns:

  • (Boolean)


1280
1281
1282
1283
1284
1285
1286
1287
1288
# File 'lib/reap/vendor/http-access2.rb', line 1280

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

#get_data(&block) ⇒ Object



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
1330
1331
# File 'lib/reap/vendor/http-access2.rb', line 1290

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



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

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



1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
# File 'lib/reap/vendor/http-access2.rb', line 1250

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



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
1235
1236
# File 'lib/reap/vendor/http-access2.rb', line 1210

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