Class: Carnivore::Source::HttpPaths

Inherits:
HttpSource show all
Includes:
Bogo::Memoization
Defined in:
lib/carnivore-http/http_paths.rb

Overview

Carnivore HTTP paths

Constant Summary

Constants inherited from HttpSource

Carnivore::Source::HttpSource::BODY_TO_FILE_SIZE

Instance Attribute Summary collapse

Attributes inherited from HttpSource

#args, #auth_allowed_origins, #auth_htpasswd, #retry_delivery

Instance Method Summary collapse

Methods inherited from HttpSource

#allowed_credentials?, #allowed_htpasswd?, #allowed_origin?, #authorized?, #auto_process?, #build_listener, #build_message, #confirm, #default_args, #perform_transmission, #retry_delivery_failure, #retry_directory, #retry_write_directory, #transmit, #write_for_retry

Methods included from Http::Utils::Params

#format_query_args, #format_query_type, included, #parse_query_string

Instance Attribute Details

#http_methodSymbol (readonly)

Returns http method.

Returns:

  • (Symbol)

    http method



16
17
18
# File 'lib/carnivore-http/http_paths.rb', line 16

def http_method
  @http_method
end

#http_pathString (readonly)

Returns end point path.

Returns:

  • (String)

    end point path



14
15
16
# File 'lib/carnivore-http/http_paths.rb', line 14

def http_path
  @http_path
end

Instance Method Details

#connectObject

Setup the HTTP listener source



41
42
43
# File 'lib/carnivore-http/http_paths.rb', line 41

def connect
  start_listener!
end

#halt_listenerObject

Kill listener on shutdown



19
20
21
22
23
24
25
26
# File 'lib/carnivore-http/http_paths.rb', line 19

def halt_listener
  listener = memoize("#{args[:bind]}-#{args[:port]}", :global){ nil }
  if(listener && listener.alive?)
    listener.terminate
  end
  unmemoize("#{args[:bind]}-#{args[:port]}", :global)
  unmemoize("#{args[:bind]}-#{args[:port]}-queues", :global)
end

#message_queueQueue

Returns:

  • (Queue)


58
59
60
# File 'lib/carnivore-http/http_paths.rb', line 58

def message_queue
  message_queues[queue_key]
end

#message_queuesQueue

Returns Message queue.

Returns:

  • (Queue)

    Message queue



46
47
48
49
50
# File 'lib/carnivore-http/http_paths.rb', line 46

def message_queues
  memoize("#{args[:bind]}-#{args[:port]}-queues", :global) do
    Smash.new
  end
end

#queue_keyString

Returns:

  • (String)


53
54
55
# File 'lib/carnivore-http/http_paths.rb', line 53

def queue_key
  "#{http_path}-#{http_method}"
end

#receive(*_) ⇒ Object

Returns:

  • (Object)


89
90
91
92
93
94
95
# File 'lib/carnivore-http/http_paths.rb', line 89

def receive(*_)
  val = nil
  until(val)
    val = Celluloid::Future.new{ message_queue.pop }.value
  end
  val
end

#setup(*_) ⇒ Object

Setup message queue for source



29
30
31
32
33
34
35
36
37
38
# File 'lib/carnivore-http/http_paths.rb', line 29

def setup(*_)
  @http_path = args.fetch(:path, '/')
  @http_method = args.fetch(:method, 'get').to_s.downcase.to_sym
  if(message_queues[queue_key])
    raise ArgumentError.new "Conflicting HTTP path source provided! path: #{http_path} method: #{http_method}"
  else
    message_queues[queue_key] = Queue.new
  end
  super
end

#start_listener!Object

Start the HTTP(S) listener



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/carnivore-http/http_paths.rb', line 63

def start_listener!
  memoize("#{args[:bind]}-#{args[:port]}", :global) do
    build_listener do |con|
      con.each_request do |req|
        begin
          msg = build_message(con, req)
          msg_queue = message_queues["#{req.path}-#{req.method.to_s.downcase}"]
          if(msg_queue)
            if(authorized?(msg))
              msg_queue << msg
              req.respond(:ok, 'So long and thanks for all the fish!')
            else
              req.respond(:unauthorized, 'You are not authorized to perform requested action!')
            end
          else
            req.respond(:not_found, 'Requested path not found!')
          end
        rescue => e
          req.respond(:bad_request, "Failed to process request -> #{e}")
        end
      end
    end
  end
end