Class: Aizuchi::Middleware

Inherits:
Struct
  • Object
show all
Defined in:
lib/aizuchi/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject

Returns the value of attribute app

Returns:

  • (Object)

    the current value of app



8
9
10
# File 'lib/aizuchi/middleware.rb', line 8

def app
  @app
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



8
9
10
# File 'lib/aizuchi/middleware.rb', line 8

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/aizuchi/middleware.rb', line 10

def call(env)
  return app.call(env) unless config['enabled']
  if env["REQUEST_METHOD"] == "POST" and
      env["PATH_INFO"] =~ /^\/aizuchi/
    # proxy
    request = Rack::Request.new env
    res = post(config['target'], request.POST.to_json,
               config['user'], config['password'])
    case res
    when Net::HTTPSuccess
      [ 200, {}, [] ]
    else
      [ 500, {}, [] ]
    end
  else
    # inject
    status, headers, response = app.call(env)
    if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
      body = ""
      response.each { |part| body << part }
      index = body.rindex "</body>"
      if index
        body.insert index, data
        headers["Content-Length"] = body.length.to_s
        response = [ body ]
      end
    end
    [ status, headers, response ]
  end
end