Class: LogStash::Outputs::Http

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/http.rb

Instance Method Summary collapse

Instance Method Details

#encode(hash) ⇒ Object

def receive



138
139
140
141
142
# File 'lib/logstash/outputs/http.rb', line 138

def encode(hash)
  return hash.collect do |key, value|
    CGI.escape(key) + "=" + CGI.escape(value)
  end.join("&")
end

#receive(event) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/logstash/outputs/http.rb', line 86

def receive(event)
  return unless output?(event)

  if @mapping
    evt = Hash.new
    @mapping.each do |k,v|
      evt[k] = event.sprintf(v)
    end
  else
    evt = event.to_hash
  end

  case @http_method
  when "put"
    request = @agent.put(event.sprintf(@url))
  when "post"
    request = @agent.post(event.sprintf(@url))
  else
    @logger.error("Unknown verb:", :verb => @http_method)
  end

  if @headers
    @headers.each do |k,v|
      request.headers[k] = event.sprintf(v)
    end
  end

  request["Content-Type"] = @content_type

  begin
    if @format == "json"
      request.body = LogStash::Json.dump(evt)
    elsif @format == "message"
      request.body = event.sprintf(@message)
    else
      request.body = encode(evt)
    end
    #puts "#{request.port} / #{request.protocol}"
    #puts request
    #puts
    #puts request.body
    response = @agent.execute(request)

    # Consume body to let this connection be reused
    rbody = ""
    response.read_body { |c| rbody << c }
    #puts rbody
  rescue Exception => e
    @logger.warn("Unhandled exception", :request => request, :response => response, :exception => e, :stacktrace => e.backtrace)
  end
end

#registerObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/logstash/outputs/http.rb', line 60

def register
  require "ftw"
  require "uri"
  @agent = FTW::Agent.new
  # TODO(sissel): SSL verify mode?

  if @content_type.nil?
    case @format
      when "form" ; @content_type = "application/x-www-form-urlencoded"
      when "json" ; @content_type = "application/json"
    end
  end
  if @format == "message"
    if @message.nil?
      raise "message must be set if message format is used"
    end
    if @content_type.nil?
      raise "content_type must be set if message format is used"
    end
    unless @mapping.nil?
      @logger.warn "mapping is not supported and will be ignored if message format is used"
    end
  end
end