Login

AMF Message passing through Middleware

Author:
jayb
Posted:
September 27, 2007
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

Middleware for communicating with Flash Player via Flashticle and Django.

Setup a view at /gateway/math/multiply like so:

def multiply(request, m1, m2): return m1 * m2

Then in your Flex/Flash app you call "math.multiply" on a NetConnection pointing to http://domain.com/gateway/

Does not yet support authentication.

 1
 2
 3
 4
 5
 6
 7
 8
 9
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
40
41
42
43
44
45
46
47
import re

from django.http import HttpResponse, HttpResponseForbidden
from django.conf import settings
from django.core import urlresolvers

from flashticle.remoting import to_remoting, from_remoting
from flashticle.util import StringIO

class AMFMiddleware(object):
  CONTENT_TYPE = 'application/x-amf'

  def __init__(self):
    self.gateway_path = getattr(settings, 'GATEWAY_PATH', '/gateway/')
    self.path_matcher = re.compile(r'^%s.+' % self.gateway_path)

  def process_request(self, request):
    if self.path_matcher.match(request.path):
      return HttpResponseForbidden()
    elif request.method == "POST" and request.path == self.gateway_path and request.META.get('CONTENT_TYPE') == AMFMiddleware.CONTENT_TYPE:
      request_message = from_remoting(StringIO(request.raw_post_data))
      headers, body = self.processAMF(request, request_message)

      io = StringIO()
      to_remoting(headers, body, io)
      response = HttpResponse(io.getvalue(), AMFMiddleware.CONTENT_TYPE)
      response['Content-Length'] = str(io.tell())

      return response

  def processAMF(self, request, request_message):
    headers = request_message.raw_headers
    if not 'DescribeService' in request_message.headers:
      body = [self.processBody(request, *elem) for elem in request_message.raw_body]
    print headers
    print body
    return headers, body

  def processBody(self, request, target, response, params):
    targetPath = target.encode('utf8').replace('.', '/')
    path = request.path.rsplit('/', 1)[0] + '/' + targetPath

    callback, _, _ = urlresolvers.resolve(path)
    result = callback(request, *params)

    response += '/onResult'
    return response, 'null', result

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

pigletto (on September 28, 2007):

Seems to be very interesting. When do you plan to add auth support?

#

Please login first before commenting.