Class: HTTPClient::Session
- Inherits:
-
Object
- Object
- HTTPClient::Session
- 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
Defined Under Namespace
Classes: BadResponse, Error, InvalidState, KeepAliveDisconnected
Instance Attribute Summary collapse
-
#connect_retry ⇒ Object
Returns the value of attribute connect_retry.
-
#connect_timeout ⇒ Object
These session parameters are not used now…
-
#debug_dev ⇒ Object
Device for dumping log for debugging.
-
#dest ⇒ Object
readonly
Destination site.
-
#proxy ⇒ Object
Proxy site.
-
#read_block_size ⇒ Object
Returns the value of attribute read_block_size.
-
#receive_timeout ⇒ Object
Returns the value of attribute receive_timeout.
-
#requested_version ⇒ Object
Requested protocol version.
-
#send_timeout ⇒ Object
Returns the value of attribute send_timeout.
-
#socket_sync ⇒ Object
Boolean value for Socket#sync.
-
#src ⇒ Object
readonly
Source site.
-
#ssl_config ⇒ Object
Returns the value of attribute ssl_config.
-
#ssl_peer_cert ⇒ Object
readonly
Returns the value of attribute ssl_peer_cert.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #eof? ⇒ Boolean
- #get_data(&block) ⇒ Object
- #get_header(&block) ⇒ Object
- #get_status ⇒ Object
-
#initialize(dest, user_agent, from) ⇒ Session
constructor
A new instance of Session.
-
#query(req) ⇒ Object
Send a request to the server.
Constructor Details
#initialize(dest, user_agent, from) ⇒ Session
Returns a new instance of Session.
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 |
# File 'lib/httpclient.rb', line 1128 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 @user_agent = user_agent @from = from @state = :INIT @requests = [] @status = nil @reason = nil @headers = [] @socket = nil end |
Instance Attribute Details
#connect_retry ⇒ Object
Returns the value of attribute connect_retry.
1120 1121 1122 |
# File 'lib/httpclient.rb', line 1120 def connect_retry @connect_retry end |
#connect_timeout ⇒ Object
These session parameters are not used now…
1119 1120 1121 |
# File 'lib/httpclient.rb', line 1119 def connect_timeout @connect_timeout end |
#debug_dev ⇒ Object
Device for dumping log for debugging
1116 1117 1118 |
# File 'lib/httpclient.rb', line 1116 def debug_dev @debug_dev end |
#dest ⇒ Object (readonly)
Destination site
1109 1110 1111 |
# File 'lib/httpclient.rb', line 1109 def dest @dest end |
#proxy ⇒ Object
Proxy site
1111 1112 1113 |
# File 'lib/httpclient.rb', line 1111 def proxy @proxy end |
#read_block_size ⇒ Object
Returns the value of attribute read_block_size.
1123 1124 1125 |
# File 'lib/httpclient.rb', line 1123 def read_block_size @read_block_size end |
#receive_timeout ⇒ Object
Returns the value of attribute receive_timeout.
1122 1123 1124 |
# File 'lib/httpclient.rb', line 1122 def receive_timeout @receive_timeout end |
#requested_version ⇒ Object
Requested protocol version
1114 1115 1116 |
# File 'lib/httpclient.rb', line 1114 def requested_version @requested_version end |
#send_timeout ⇒ Object
Returns the value of attribute send_timeout.
1121 1122 1123 |
# File 'lib/httpclient.rb', line 1121 def send_timeout @send_timeout end |
#socket_sync ⇒ Object
Boolean value for Socket#sync
1112 1113 1114 |
# File 'lib/httpclient.rb', line 1112 def socket_sync @socket_sync end |
#src ⇒ Object (readonly)
Source site
1110 1111 1112 |
# File 'lib/httpclient.rb', line 1110 def src @src end |
#ssl_config ⇒ Object
Returns the value of attribute ssl_config.
1125 1126 1127 |
# File 'lib/httpclient.rb', line 1125 def ssl_config @ssl_config end |
#ssl_peer_cert ⇒ Object (readonly)
Returns the value of attribute ssl_peer_cert.
1126 1127 1128 |
# File 'lib/httpclient.rb', line 1126 def ssl_peer_cert @ssl_peer_cert end |
Instance Method Details
#close ⇒ Object
1188 1189 1190 1191 1192 1193 1194 |
# File 'lib/httpclient.rb', line 1188 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
1196 1197 1198 |
# File 'lib/httpclient.rb', line 1196 def closed? @state == :INIT end |
#eof? ⇒ Boolean
1230 1231 1232 1233 1234 1235 1236 1237 1238 |
# File 'lib/httpclient.rb', line 1230 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
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 |
# File 'lib/httpclient.rb', line 1240 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
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 |
# File 'lib/httpclient.rb', line 1214 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_status ⇒ Object
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 |
# File 'lib/httpclient.rb', line 1200 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
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 |
# File 'lib/httpclient.rb', line 1160 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 |