Class: XmppGateway::HttpInterface

Inherits:
EM::Connection
  • Object
show all
Includes:
EM::HttpServer
Defined in:
lib/xmpp_gateway/http_interface.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(host = '127.0.0.1', port = 8000) ⇒ Object



12
13
14
15
# File 'lib/xmpp_gateway/http_interface.rb', line 12

def self.start(host = '127.0.0.1', port = 8000)
  XmppGateway.logger.info "Starting server at http://#{host}:#{port}"
  EM.start_server(host, port, self)
end

Instance Method Details

#acceptable_method?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/xmpp_gateway/http_interface.rb', line 17

def acceptable_method?
  %w(GET POST).include? @http_request_method
end

#bad_requestObject



116
117
118
119
120
121
122
123
124
# File 'lib/xmpp_gateway/http_interface.rb', line 116

def bad_request
  XmppGateway.logger.debug "Bad request '#{post_params['stanza']}' is not a valid stanza"
  
  response = EM::DelegatedHttpResponse.new(self)
  response.status = 400
  response.content_type 'text/html'
  response.content = "Please send a valid stanza\n"
  return response
end

#basic_authObject



70
71
72
# File 'lib/xmpp_gateway/http_interface.rb', line 70

def basic_auth
  @_basic_auth ||= ( headers['Authorization'] || '' ).match(/\ABasic +(.+)\Z/).to_a[1]
end

#credentialsObject



79
80
81
82
83
# File 'lib/xmpp_gateway/http_interface.rb', line 79

def credentials
  if basic_auth
    Base64.decode64(basic_auth).split(':')
  end
end

#getObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/xmpp_gateway/http_interface.rb', line 126

def get
  XmppGateway.logger.debug "Success, send stanza form"
  
  response = EM::DelegatedHttpResponse.new(self)
  response.status = 200
  response.content_type 'text/html'      
  response.content = 
  "<h1>Enter Stanza</h1>" + 
  "<form method='post'>" +
    "<label for='stanza'>Stanza</label>" +
    "<textarea name='stanza' cols=50 rows=20>" +
      "<iq type='get'\n" +
      "    to='#{@connection.client.jid.domain}'\n" +
      "    id='info1'>\n" +
      "  <query xmlns='http://jabber.org/protocol/disco#info'/>\n" +
      "</iq>" +
    "</textarea>" +
    "<input type='submit' value='Submit'/>" +
  "</form>\n"
  return response
end

#headersObject



66
67
68
# File 'lib/xmpp_gateway/http_interface.rb', line 66

def headers
  @headers ||= Hash[@http_headers.split("\000").map{|kv| kv.split(':',2).map{|v| v.strip} }]
end

#method_not_allowedObject



106
107
108
109
110
111
112
113
114
# File 'lib/xmpp_gateway/http_interface.rb', line 106

def method_not_allowed
  XmppGateway.logger.debug "Method #{@http_request_method} not allowed"
  
  response = EM::DelegatedHttpResponse.new(self)
  response.status = 405
  response.content_type 'text/html'
  response.content = "Method not allowed\n"
  return response
end

#post_paramsObject



74
75
76
77
# File 'lib/xmpp_gateway/http_interface.rb', line 74

def post_params
  string = @http_post_content || ''
  @_post_params ||= Hash[string.split('&').map{|kv| kv.split('=',2).map{|v| CGI.unescape v } }]
end

#process_http_requestObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xmpp_gateway/http_interface.rb', line 21

def process_http_request
  # the http request details are available via the following instance variables:
  #   @http_protocol
  #   @http_request_method
  #   @http_cookie
  #   @http_if_none_match
  #   @http_content_type
  #   @http_path_info
  #   @http_request_uri
  #   @http_query_string
  #   @http_post_content
  #   @http_headers
  
  XmppGateway.logger.info "Received request #{@http_request_method} #{post_params.inspect}"
  
  Fiber.new{        
    if acceptable_method?
      user, password = credentials
      @connection = XmppPool.new( user, password )
      if @connection.connected
        case @http_request_method
        when "GET"
          get.send_response
        when "POST"
          stanza = XmppPool.stanza(post_params['stanza'])
          if stanza
            result = @connection.write stanza
            response(result).send_response
          else
            bad_request.send_response
          end
        else
          # acceptable_method should have stopped us arriving here
          # battle on bravely
          method_not_allowed.send_response
        end
      else
        unauthorized.send_response
      end
    else
      method_not_allowed.send_response 
    end
  }.resume
end

#response(stanza) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/xmpp_gateway/http_interface.rb', line 85

def response(stanza)
  XmppGateway.logger.debug "Success"
  
  response = EM::DelegatedHttpResponse.new(self)
  response.status = 200
  response.content_type 'application/xml'
  response.content = stanza.to_s + "\n"
  return response
end

#unauthorizedObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/xmpp_gateway/http_interface.rb', line 95

def unauthorized
  XmppGateway.logger.debug "Unauthorized, username & password wrong"
  
  response = EM::DelegatedHttpResponse.new(self)
  response.status = 401
  response.content_type 'text/html'
  response.headers = {"WWW-Authenticate" => 'Basic realm="Secure Area"'}
  response.content = "Unauthorised\n"
  return response
end