Class: LogStash::Outputs::HttpAuth

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

Instance Method Summary collapse

Instance Method Details

#receive(event) ⇒ Object



115
116
117
118
119
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
161
162
163
164
165
166
167
168
# File 'lib/logstash/outputs/http_auth.rb', line 115

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

    () if @sign_in

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

    @url = URI::HTTP.build({:host => @host, :port=> @port, :path => @path}) if @url.nil?

    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

    set_header(request)

    begin
        if @format == "json"
            request.body = evt.to_json
        elsif @format == "message"
            request.body = event.sprintf(@message)
        else
            request.body = encode(evt)
        end

        response = @agent.execute(request)

        @signed_in = status_success?(response.status)

        @logger.warn("Response Status #{response.status} for request #{@http_method} to #{@url}") if !status_success?(response.status)

    rescue Exception => e
        @logger.warn("Unhandled exception", :request => request, :response => response, :exception => e, :stacktrace => e.backtrace)
    end
end

#registerObject



85
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
# File 'lib/logstash/outputs/http_auth.rb', line 85

def register
    require "ftw"
    require "uri"
    @agent = FTW::Agent.new

    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

    @signed_in = false
    @csrf_token = nil
    @cookies = nil
end