Class: Compp::Default

Inherits:
Connection show all
Defined in:
lib/compp/default.rb

Overview

This is the default Compp::Connection. For each GET request it receives, it extract the credentails from the headers, and connects an XMPP client. The user can define its own xmpp handlers by subclassing this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#initialize, #orbitize

Constructor Details

This class inherits a constructor from Compp::Connection

Instance Attribute Details

#http_headersObject

Returns the value of attribute http_headers.



8
9
10
# File 'lib/compp/default.rb', line 8

def http_headers
  @http_headers
end

Instance Method Details

#extract_jid_and_password_from_headersObject

Helping tool to extract jid and password from headers



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/compp/default.rb', line 12

def extract_jid_and_password_from_headers
  jid, password = nil, nil
  begin
    headers = Hash[@http_headers.split("\000").map() {|h| h.split(": ")}]
    if headers["Authorization"] 
      auth = headers["Authorization"].split(" ")
      if auth[0] == "Basic"
        jid, password = Base64.decode64(auth[1]).split(":")
      end
    end
  rescue
    # Something was wring, w dont care
  end
  [jid, password]
end

#process_http_requestObject

This is the default behavior. When a user sends an HTTP request, the headers are parsed We connect an Blather Client using these headers Upon stanzas, we call on_stanza



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/compp/default.rb', line 33

def process_http_request
  jid, password = extract_jid_and_password_from_headers()
  if jid.nil?
    send_401
  else
    client = Blather::Client.setup(jid, password)
    client.register_handler(:ready) { 
      orbitize("HTTP/1.0 200 OK")
      orbitize("Content-Type: text/xml")
      orbitize("Connection: close\r\n")
      orbitize( "<?xml version='1.0' encoding='utf-8' ?>")
    }
    register_xmpp_handlers(client) # This function should probably be overriden
    client.connect
  end
end

#register_xmpp_handlers(client) ⇒ Object

This should be overiden! If, not, we assume the user just wants to puts the XML content of the message received to the HTTP connection



71
72
73
74
75
# File 'lib/compp/default.rb', line 71

def register_xmpp_handlers(client)
  client.register_handler(:message) do |m|
    orbitize(m.to_xml)
  end
end

#send_401Object

Helping tool that sends a 401



52
53
54
55
56
57
# File 'lib/compp/default.rb', line 52

def send_401
  resp = EventMachine::DelegatedHttpResponse.new( self )
  resp.status = 401
  resp.content = "Please, authenticate with with jid:password\n\n"
  resp.send_response
end

#send_403Object

Helping tool that sends a 403



61
62
63
64
65
66
# File 'lib/compp/default.rb', line 61

def send_403
  resp = EventMachine::DelegatedHttpResponse.new( self )
  resp.status = 403
  resp.content = "Couldn't authenticate XMPP user\n\n"
  resp.send_response
end