Class: Carnivore::Source::HttpPaths
- Inherits:
-
HttpSource
- Object
- Carnivore::Source
- HttpSource
- Carnivore::Source::HttpPaths
- 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
-
#http_method ⇒ Symbol
readonly
Http method.
-
#http_path ⇒ String
readonly
End point path.
Attributes inherited from HttpSource
#args, #auth_allowed_origins, #auth_htpasswd, #retry_delivery
Instance Method Summary collapse
-
#connect ⇒ Object
Setup the HTTP listener source.
-
#halt_listener ⇒ Object
Kill listener on shutdown.
- #message_queue ⇒ Queue
-
#message_queues ⇒ Queue
Message queue.
- #queue_key ⇒ String
- #receive(*_) ⇒ Object
-
#setup(*_) ⇒ Object
Setup message queue for source.
-
#start_listener! ⇒ Object
Start the HTTP(S) listener.
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_method ⇒ Symbol (readonly)
Returns http method.
16 17 18 |
# File 'lib/carnivore-http/http_paths.rb', line 16 def http_method @http_method end |
#http_path ⇒ String (readonly)
Returns end point path.
14 15 16 |
# File 'lib/carnivore-http/http_paths.rb', line 14 def http_path @http_path end |
Instance Method Details
#connect ⇒ Object
Setup the HTTP listener source
41 42 43 |
# File 'lib/carnivore-http/http_paths.rb', line 41 def connect start_listener! end |
#halt_listener ⇒ Object
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_queue ⇒ Queue
58 59 60 |
# File 'lib/carnivore-http/http_paths.rb', line 58 def [queue_key] end |
#message_queues ⇒ Queue
Returns Message queue.
46 47 48 49 50 |
# File 'lib/carnivore-http/http_paths.rb', line 46 def memoize("#{args[:bind]}-#{args[:port]}-queues", :global) do Smash.new end end |
#queue_key ⇒ String
53 54 55 |
# File 'lib/carnivore-http/http_paths.rb', line 53 def queue_key "#{http_path}-#{http_method}" end |
#receive(*_) ⇒ 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{ .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([queue_key]) raise ArgumentError.new "Conflicting HTTP path source provided! path: #{http_path} method: #{http_method}" else [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 = (con, req) msg_queue = ["#{req.path}-#{req.method.to_s.downcase}"] if(msg_queue) if((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 |