Class: Puma::Client
- Inherits:
-
Object
- Object
- Puma::Client
- Extended by:
- Delegation
- Includes:
- Const
- Defined in:
- lib/puma/client.rb
Constant Summary collapse
- EmptyBody =
The object used for a request with no body. All requests with no body share this one object since it has no state.
NullIO.new
Constants included from Const
Puma::Const::CGI_VER, Puma::Const::CHUNK_SIZE, Puma::Const::CLOSE, Puma::Const::CLOSE_CHUNKED, Puma::Const::CODE_NAME, Puma::Const::COLON, Puma::Const::CONNECTION_CLOSE, Puma::Const::CONNECTION_KEEP_ALIVE, Puma::Const::CONTENT_LENGTH, Puma::Const::CONTENT_LENGTH2, Puma::Const::CONTENT_LENGTH_S, Puma::Const::CONTENT_TYPE, Puma::Const::DATE, Puma::Const::ERROR_400_RESPONSE, Puma::Const::ERROR_404_RESPONSE, Puma::Const::ERROR_408_RESPONSE, Puma::Const::ERROR_500_RESPONSE, Puma::Const::ERROR_503_RESPONSE, Puma::Const::ETAG, Puma::Const::ETAG_FORMAT, Puma::Const::FAST_TRACK_KA_TIMEOUT, Puma::Const::FIRST_DATA_TIMEOUT, Puma::Const::GATEWAY_INTERFACE, Puma::Const::GET, Puma::Const::HALT_COMMAND, Puma::Const::HEAD, Puma::Const::HIJACK, Puma::Const::HIJACK_IO, Puma::Const::HIJACK_P, Puma::Const::HOST, Puma::Const::HTTP, Puma::Const::HTTPS, Puma::Const::HTTPS_KEY, Puma::Const::HTTP_10, Puma::Const::HTTP_10_200, Puma::Const::HTTP_11, Puma::Const::HTTP_11_200, Puma::Const::HTTP_CONNECTION, Puma::Const::HTTP_HOST, Puma::Const::HTTP_IF_MODIFIED_SINCE, Puma::Const::HTTP_IF_NONE_MATCH, Puma::Const::HTTP_VERSION, Puma::Const::HTTP_X_FORWARDED_FOR, Puma::Const::KEEP_ALIVE, Puma::Const::LAST_MODIFIED, Puma::Const::LINE_END, Puma::Const::LOCALHOST, Puma::Const::LOCALHOST_ADDR, Puma::Const::LOCALHOST_IP, Puma::Const::MAX_BODY, Puma::Const::MAX_HEADER, Puma::Const::NEWLINE, Puma::Const::PATH_INFO, Puma::Const::PERSISTENT_TIMEOUT, Puma::Const::PORT_443, Puma::Const::PORT_80, Puma::Const::PUMA_CONFIG, Puma::Const::PUMA_PEERCERT, Puma::Const::PUMA_SERVER_STRING, Puma::Const::PUMA_SOCKET, Puma::Const::PUMA_TMP_BASE, Puma::Const::PUMA_VERSION, Puma::Const::QUERY_STRING, Puma::Const::RACK_AFTER_REPLY, Puma::Const::RACK_INPUT, Puma::Const::RACK_URL_SCHEME, Puma::Const::REDIRECT, Puma::Const::REMOTE_ADDR, Puma::Const::REQUEST_METHOD, Puma::Const::REQUEST_PATH, Puma::Const::REQUEST_URI, Puma::Const::RESTART_COMMAND, Puma::Const::SCRIPT_NAME, Puma::Const::SERVER_NAME, Puma::Const::SERVER_PORT, Puma::Const::SERVER_PROTOCOL, Puma::Const::SERVER_SOFTWARE, Puma::Const::SHUTDOWN_GRACE_TIME, Puma::Const::SLASH, Puma::Const::STATUS_FORMAT, Puma::Const::STOP_COMMAND, Puma::Const::TRANSFER_ENCODING, Puma::Const::TRANSFER_ENCODING_CHUNKED, Puma::Const::WRITE_TIMEOUT
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#hijacked ⇒ Object
readonly
Returns the value of attribute hijacked.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
- #peerip ⇒ Object
-
#ready ⇒ Object
readonly
Returns the value of attribute ready.
-
#remote_addr_header ⇒ Object
Returns the value of attribute remote_addr_header.
-
#tempfile ⇒ Object
readonly
Returns the value of attribute tempfile.
-
#timeout_at ⇒ Object
readonly
Returns the value of attribute timeout_at.
-
#to_io ⇒ Object
readonly
Returns the value of attribute to_io.
Instance Method Summary collapse
-
#call ⇒ Object
For the hijack protocol (allows us to just put the Client object into the env).
- #close ⇒ Object
- #eagerly_finish ⇒ Object
-
#finish ⇒ Object
IS_JRUBY.
- #in_data_phase ⇒ Object
-
#initialize(io, env = nil) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
- #jruby_start_try_to_finish ⇒ Object
- #read_body ⇒ Object
- #reset(fast_check = true) ⇒ Object
- #set_timeout(val) ⇒ Object
- #setup_body ⇒ Object
- #try_to_finish ⇒ Object
- #write_400 ⇒ Object
- #write_408 ⇒ Object
- #write_500 ⇒ Object
Methods included from Delegation
Constructor Details
#initialize(io, env = nil) ⇒ Client
Returns a new instance of Client.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/puma/client.rb', line 27 def initialize(io, env=nil) @io = io @to_io = io.to_io @proto_env = env if !env @env = nil else @env = env.dup end @parser = HttpParser.new @parsed_bytes = 0 @read_header = true @ready = false @body = nil @buffer = nil @tempfile = nil @timeout_at = nil @requests_served = 0 @hijacked = false @peerip = nil @remote_addr_header = nil end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
55 56 57 |
# File 'lib/puma/client.rb', line 55 def body @body end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
55 56 57 |
# File 'lib/puma/client.rb', line 55 def env @env end |
#hijacked ⇒ Object (readonly)
Returns the value of attribute hijacked.
55 56 57 |
# File 'lib/puma/client.rb', line 55 def hijacked @hijacked end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
55 56 57 |
# File 'lib/puma/client.rb', line 55 def io @io end |
#peerip ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/puma/client.rb', line 312 def peerip return @peerip if @peerip if @remote_addr_header hdr = (@env[@remote_addr_header] || LOCALHOST_ADDR).split(/[\s,]/).first @peerip = hdr return hdr end @peerip ||= @io.peeraddr.last end |
#ready ⇒ Object (readonly)
Returns the value of attribute ready.
55 56 57 |
# File 'lib/puma/client.rb', line 55 def ready @ready end |
#remote_addr_header ⇒ Object
Returns the value of attribute remote_addr_header.
60 61 62 |
# File 'lib/puma/client.rb', line 60 def remote_addr_header @remote_addr_header end |
#tempfile ⇒ Object (readonly)
Returns the value of attribute tempfile.
55 56 57 |
# File 'lib/puma/client.rb', line 55 def tempfile @tempfile end |
#timeout_at ⇒ Object (readonly)
Returns the value of attribute timeout_at.
55 56 57 |
# File 'lib/puma/client.rb', line 55 def timeout_at @timeout_at end |
#to_io ⇒ Object (readonly)
Returns the value of attribute to_io.
55 56 57 |
# File 'lib/puma/client.rb', line 55 def to_io @to_io end |
Instance Method Details
#call ⇒ Object
For the hijack protocol (allows us to just put the Client object into the env)
70 71 72 73 |
# File 'lib/puma/client.rb', line 70 def call @hijacked = true env[HIJACK_IO] ||= @io end |
#close ⇒ Object
109 110 111 112 113 114 |
# File 'lib/puma/client.rb', line 109 def close begin @io.close rescue IOError end end |
#eagerly_finish ⇒ Object
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/puma/client.rb', line 220 def eagerly_finish return true if @ready if @io.kind_of? OpenSSL::SSL::SSLSocket return true if jruby_start_try_to_finish end return false unless IO.select([@to_io], nil, nil, 0) try_to_finish end |
#finish ⇒ Object
IS_JRUBY
240 241 242 243 244 245 246 |
# File 'lib/puma/client.rb', line 240 def finish return true if @ready until try_to_finish IO.select([@to_io], nil, nil) end true end |
#in_data_phase ⇒ Object
75 76 77 |
# File 'lib/puma/client.rb', line 75 def in_data_phase !@read_header end |
#inspect ⇒ Object
64 65 66 |
# File 'lib/puma/client.rb', line 64 def inspect "#<Puma::Client:0x#{object_id.to_s(16)} @ready=#{@ready.inspect}>" end |
#jruby_start_try_to_finish ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/puma/client.rb', line 192 def jruby_start_try_to_finish return read_body unless @read_header begin data = @io.sysread_nonblock(CHUNK_SIZE) rescue OpenSSL::SSL::SSLError => e return false if e.kind_of? IO::WaitReadable raise e end if @buffer @buffer << data else @buffer = data end @parsed_bytes = @parser.execute(@env, @buffer, @parsed_bytes) if @parser.finished? return setup_body elsif @parsed_bytes >= MAX_HEADER raise HttpParserError, "HEADER is longer than allowed, aborting client early." end false end |
#read_body ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/puma/client.rb', line 248 def read_body # Read an odd sized chunk so we can read even sized ones # after this remain = @body_remain if remain > CHUNK_SIZE want = CHUNK_SIZE else want = remain end begin chunk = @io.read_nonblock(want) rescue Errno::EAGAIN return false rescue SystemCallError, IOError raise ConnectionError, "Connection error detected during read" end # No chunk means a closed socket unless chunk @body.close @buffer = nil @requests_served += 1 @ready = true raise EOFError end remain -= @body.write(chunk) if remain <= 0 @body.rewind @buffer = nil @requests_served += 1 @ready = true return true end @body_remain = remain false end |
#reset(fast_check = true) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/puma/client.rb', line 83 def reset(fast_check=true) @parser.reset @read_header = true @env = @proto_env.dup @body = nil @tempfile = nil @parsed_bytes = 0 @ready = false if @buffer @parsed_bytes = @parser.execute(@env, @buffer, @parsed_bytes) if @parser.finished? return setup_body elsif @parsed_bytes >= MAX_HEADER raise HttpParserError, "HEADER is longer than allowed, aborting client early." end return false elsif fast_check && IO.select([@to_io], nil, nil, FAST_TRACK_KA_TIMEOUT) return try_to_finish end end |
#set_timeout(val) ⇒ Object
79 80 81 |
# File 'lib/puma/client.rb', line 79 def set_timeout(val) @timeout_at = Time.now + val end |
#setup_body ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/puma/client.rb', line 120 def setup_body @in_data_phase = true body = @parser.body cl = @env[CONTENT_LENGTH] unless cl @buffer = body.empty? ? nil : body @body = EmptyBody @requests_served += 1 @ready = true return true end remain = cl.to_i - body.bytesize if remain <= 0 @body = StringIO.new(body) @buffer = nil @requests_served += 1 @ready = true return true end if remain > MAX_BODY @body = Tempfile.new(Const::PUMA_TMP_BASE) @body.binmode @tempfile = @body else # The body[0,0] trick is to get an empty string in the same # encoding as body. @body = StringIO.new body[0,0] end @body.write body @body_remain = remain @read_header = false return false end |
#try_to_finish ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/puma/client.rb', line 162 def try_to_finish return read_body unless @read_header begin data = @io.read_nonblock(CHUNK_SIZE) rescue Errno::EAGAIN return false rescue SystemCallError, IOError raise ConnectionError, "Connection error detected during read" end if @buffer @buffer << data else @buffer = data end @parsed_bytes = @parser.execute(@env, @buffer, @parsed_bytes) if @parser.finished? return setup_body elsif @parsed_bytes >= MAX_HEADER raise HttpParserError, "HEADER is longer than allowed, aborting client early." end false end |
#write_400 ⇒ Object
291 292 293 294 295 296 |
# File 'lib/puma/client.rb', line 291 def write_400 begin @io << ERROR_400_RESPONSE rescue StandardError end end |
#write_408 ⇒ Object
298 299 300 301 302 303 |
# File 'lib/puma/client.rb', line 298 def write_408 begin @io << ERROR_408_RESPONSE rescue StandardError end end |
#write_500 ⇒ Object
305 306 307 308 309 310 |
# File 'lib/puma/client.rb', line 305 def write_500 begin @io << ERROR_500_RESPONSE rescue StandardError end end |