def _dispatchSoapRequest(self, request): try: try: envelope = XML(request.soap_data) body = envelope.find("{http://schemas.xmlsoap.org/soap/envelope/}Body") # determine UPnP action action = body.find("{%s}%s" % (request.soap_ns, request.soap_action)) # look up the action in the service upnp_action = self.service._actions[request.soap_action] # build a list of the action arguments in_args = {} for arg in action: in_args[arg.tag] = arg.text # execute the UPnP action logger.log_debug("executing %s#%s" % (self.service.serviceID, request.soap_action)) out_args = upnp_action(request, self.service, in_args) # return the action response env = Element("s:Envelope") env.attrib['xmlns:s'] = "http://schemas.xmlsoap.org/soap/envelope/" env.attrib['s:encodingStyle'] = "http://schemas.xmlsoap.org/soap/encoding/" env.attrib['xmlns:i'] = "http://www.w3.org/1999/XMLSchema-instance" body = SubElement(env, "s:Body") resp = SubElement(body, "u:%sResponse" % request.soap_action) resp.attrib['xmlns:u'] = request.soap_ns for (name,type,value) in out_args: arg = SubElement(resp, name) arg.attrib["i:type"] = type arg.text = value output = xmlprint(env) return HttpResponse(200, headers={'EXT': ''}, stream=output) except UPNPError, e: raise e except Exception, e: logger.log_error("caught unhandled exception: %s" % e) raise UPNPError(500, "Internal server error")
def http_SUBSCRIBE(self, request): try: # if there is no SID header, then this is a subscribe if not request.headers.hasHeader('sid'): # check that the NT header exists and is 'upnp:event' if not request.headers.hasHeader('nt'): return Response(400) nt = request.headers.getHeader('nt') if not nt == 'upnp:event': return Response(412) # get subscription timeout if request.headers.hasHeader('timeout'): timeout = request.headers.getHeader('timeout') else: timeout = 1800 # get callbacks if not request.headers.hasHeader('callback'): return Response(412) callbacks = request.headers.getHeader('callback') # create new subscription s = self.service._subscribe(callbacks, timeout) # send the SID and timeout back in the response return Response(200, {'sid': s.id, 'timeout': s.timeout}) # otherwise this is a renewal else: if request.headers.hasHeader('nt'): return Response(400) if request.headers.hasHeader('callback'): return Response(400) sid = request.headers.getHeader('sid') self.service._renew(sid) return Response(200) except Exception, e: logger.log_error("SUBSCRIBE failed: %s" % e) return Response(500)
arg = a.pop(0) try: arg_value = arguments[arg.name] parsed_value = arg.parse(arg_value) logger.log_debug("parsed %s => '%s'" % (arg.name, arg_value)) parsed_args.append(parsed_value) except KeyError: raise Exception("missing required InArgument %s" % arg.name) except Exception, e: raise e try: out_args = self.action(service, request, *parsed_args) except UPNPError, e: raise e except Exception, e: logger.log_error("caught exception executing %s: %s" % (arg.name, e)) raise UPNPError(500, "Internal server error") a = self.out_args[:] parsed_args = [] while not a == []: arg = a.pop(0) try: arg_value = out_args[arg.name] parsed_value = arg.write(arg_value) logger.log_debug("wrote %s => %s" % (arg.name,parsed_value)) parsed_args.append((arg.name, arg.type, parsed_value)) except KeyError: raise Exception("missing required OutArgument %s" % arg.name) except Exception, e: raise e return parsed_args