Class: Fluent::HttpListInput::Handler

Inherits:
Coolio::Socket
  • Object
show all
Defined in:
lib/fluent/plugin/in_http_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(io, km, callback, body_size_limit) ⇒ Handler

Returns a new instance of Handler.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fluent/plugin/in_http_list.rb', line 117

def initialize(io, km, callback, body_size_limit)
  super(io)
  @km = km
  @callback = callback
  @body_size_limit = body_size_limit
  @content_type = ""
  @next_close = false

  @idle = 0
  @km.add(self)
end

Instance Method Details

#closing?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/fluent/plugin/in_http_list.rb', line 238

def closing?
  @next_close
end

#on_body(chunk) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/fluent/plugin/in_http_list.rb', line 193

def on_body(chunk)
  if @body.bytesize + chunk.bytesize > @body_size_limit
    unless closing?
      send_response_and_close("413 Request Entity Too Large", {}, "Too large")
    end
    return
  end
  @body << chunk
end

#on_closeObject



133
134
135
# File 'lib/fluent/plugin/in_http_list.rb', line 133

def on_close
  @km.delete(self)
end

#on_connectObject



137
138
139
# File 'lib/fluent/plugin/in_http_list.rb', line 137

def on_connect
  @parser = Http::Parser.new(self)
end

#on_headers_complete(headers) ⇒ Object



154
155
156
157
158
159
160
161
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
190
191
# File 'lib/fluent/plugin/in_http_list.rb', line 154

def on_headers_complete(headers)
  expect = nil
  size = nil
  if @parser.http_version == [1, 1]
    #Modified to always be false - Paul McCann 2012/10/24
    #Changed because it was likely cause of slowness in production
    @keep_alive = false
  else
    @keep_alive = false
  end
  headers.each_pair {|k,v|
    case k
    when /Expect/i
      expect = v
    when /Content-Length/i
      size = v.to_i
    when /Content-Type/i
      @content_type = v
    when /Connection/i
      if v =~ /close/i
        @keep_alive = false
      elsif v =~ /Keep-alive/i
        @keep_alive = true
      end
    end
  }
  if expect
    if expect == '100-continue'
      if !size || size < @body_size_limit
        send_response_nobody("100 Continue", {})
      else
        send_response_and_close("413 Request Entity Too Large", {}, "Too large")
      end
    else
      send_response_and_close("417 Expectation Failed", {}, "")
    end
  end
end

#on_message_beginObject



150
151
152
# File 'lib/fluent/plugin/in_http_list.rb', line 150

def on_message_begin
  @body = ''
end

#on_message_completeObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/fluent/plugin/in_http_list.rb', line 203

def on_message_complete
  return if closing?

  params = WEBrick::HTTPUtils.parse_query(@parser.query_string)

  if @content_type =~ /^application\/x-www-form-urlencoded/
    params.update WEBrick::HTTPUtils.parse_query(@body)
  elsif @content_type =~ /^multipart\/form-data; boundary=(.+)/
    boundary = WEBrick::HTTPUtils.dequote($1)
    params.update WEBrick::HTTPUtils.parse_form_data(@body, boundary)
  elsif @content_type =~ /^application\/json/
    params['json'] = @body
  end
  path_info = @parser.request_path

  code, header, body = *@callback.call(path_info, params)
  body = body.to_s

  if @keep_alive
    header['Connection'] = 'Keep-Alive'
    send_response(code, header, body)
  else
    send_response_and_close(code, header, body)
  end
end

#on_read(data) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/fluent/plugin/in_http_list.rb', line 141

def on_read(data)
  @idle = 0
  @parser << data
rescue
  $log.warn "unexpected error", :error=>$!.to_s
  $log.warn_backtrace
  close
end

#on_write_completeObject



229
230
231
# File 'lib/fluent/plugin/in_http_list.rb', line 229

def on_write_complete
  close if @next_close
end

#send_response(code, header, body) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/fluent/plugin/in_http_list.rb', line 242

def send_response(code, header, body)
  header['Content-length'] ||= body.bytesize
  header['Content-type'] ||= 'text/plain'

  data = %[HTTP/1.1 #{code}\r\n]
  header.each_pair {|k,v|
    data << "#{k}: #{v}\r\n"
  }
  data << "\r\n"
  write data

  write body
end

#send_response_and_close(code, header, body) ⇒ Object



233
234
235
236
# File 'lib/fluent/plugin/in_http_list.rb', line 233

def send_response_and_close(code, header, body)
  send_response(code, header, body)
  @next_close = true
end

#send_response_nobody(code, header) ⇒ Object



256
257
258
259
260
261
262
263
# File 'lib/fluent/plugin/in_http_list.rb', line 256

def send_response_nobody(code, header)
  data = %[HTTP/1.1 #{code}\r\n]
  header.each_pair {|k,v|
    data << "#{k}: #{v}\r\n"
  }
  data << "\r\n"
  write data
end

#step_idleObject



129
130
131
# File 'lib/fluent/plugin/in_http_list.rb', line 129

def step_idle
  @idle += 1
end