XML-RPC "filter"

L

Luigi

Dear all,

I'm writing an XML-RPC server which should be able to modify the
incoming request before dispatching it. In particular I wand to added
two fixed parameters to the method called: one is the client host
address, and the other is the user name provided as for Basic
Authentication (http://[email protected]).

To do this, at the present I've overwritten the do_POST method of
SimpleXMLRPCRequestHandler, including at a certain point this code:

.....
data = ''.join(L)

params, method = xmlrpclib.loads(data)
user = "unknown"
if self.headers.has_key('Authorization'):
# handle Basic authentication
(enctype, encstr) = self.headers.get('Authorization').split()
user, password = base64.standard_b64decode(encstr).split(':')
params = list(params)
params.append(self.address_string())
params.append(user)
params = tuple(params)
data = xmlrpclib.dumps(params, methodname=method)

(I slightly modified it to make it more readable at mail level)

It works, but I don't really like it because it completely overwrites
the do_POST method that in the future Python releases is going to
change (I verified it). Do you know a better way to do this?

Thanks in advance.

Luigi
 
D

Diez B. Roggisch

Luigi said:
Dear all,

I'm writing an XML-RPC server which should be able to modify the
incoming request before dispatching it. In particular I wand to added
two fixed parameters to the method called: one is the client host
address, and the other is the user name provided as for Basic
Authentication (http://[email protected]).

To do this, at the present I've overwritten the do_POST method of
SimpleXMLRPCRequestHandler, including at a certain point this code:

....
data = ''.join(L)

params, method = xmlrpclib.loads(data)
user = "unknown"
if self.headers.has_key('Authorization'):
# handle Basic authentication
(enctype, encstr) = self.headers.get('Authorization').split()
user, password = base64.standard_b64decode(encstr).split(':')
params = list(params)
params.append(self.address_string())
params.append(user)
params = tuple(params)
data = xmlrpclib.dumps(params, methodname=method)

(I slightly modified it to make it more readable at mail level)

It works, but I don't really like it because it completely overwrites
the do_POST method that in the future Python releases is going to
change (I verified it). Do you know a better way to do this?

I would go for a slightly different approach: make your server have a
dispatch-method that delegates the calls to the underlying actual
implementation. But *before* that happens, extract the information as
above, and either

- prepend it to the argument list

- stuff it into threadlocal variables, and only access these if needed in
your implementation.

Diez
 
L

luigi.paioro

I would go for a slightly different approach: make your server have a
dispatch-method that delegates the calls to the underlying actual
implementation. But *before* that happens, extract the information as
above, and either

 - prepend it to the argument list

 - stuff it into threadlocal variables, and only access these if needed in
your implementation.

Diez

Are you suggesting me to overwrite the _dispatch(self, method, params)
method of SimpleXMLRPCDispatcher? I thought to this possibility, but
it only accepts "method" and "params" as arguments, so, as far as I
know, I have no way to get the user and host address to append.

Perhaps I've misunderstood your suggestion... in that case can you
post a short example?

Thank you very much!

Luigi
 
R

Richard Levasseur

Dear all,

I'm writing an XML-RPC server which should be able to modify the
incoming request before dispatching it. In particular I wand to added
two fixed parameters to the method called: one is the client host
address, and the other is the user name provided as for Basic
Authentication (http://[email protected]).

To do this, at the present I've overwritten the do_POST method of
SimpleXMLRPCRequestHandler, including at a certain point this code:

....
data = ''.join(L)

params, method = xmlrpclib.loads(data)
user = "unknown"
if self.headers.has_key('Authorization'):
  # handle Basic authentication
  (enctype, encstr) =  self.headers.get('Authorization').split()
  user, password = base64.standard_b64decode(encstr).split(':')
params = list(params)
params.append(self.address_string())
params.append(user)
params = tuple(params)
data = xmlrpclib.dumps(params, methodname=method)

(I slightly modified it to make it more readable at mail level)

It works, but I don't really like it because it completely overwrites
the do_POST method that in the future Python releases is going to
change (I verified it). Do you know a better way to do this?

Thanks in advance.

Luigi

I actually wrote a wsgi module for almost this -exact- use case
(having to prepend a user/password to the method calls). The simple
rpc server and dispatchers didn't give me enough control over the
behavior, so I had to reimplement all the logic surround the loads/
dumps calls, and eventually that just turned into the bulk of the
whole SimpleXMLRPCServer module. There's a lot of tight coupling in
the _dispatch method, so you'll have to override, monkey patch, or
reimplement it.
 
D

Diez B. Roggisch

Are you suggesting me to overwrite the _dispatch(self, method, params)
method of SimpleXMLRPCDispatcher? I thought to this possibility, but
it only accepts "method" and "params" as arguments, so, as far as I
know, I have no way to get the user and host address to append.

Perhaps I've misunderstood your suggestion... in that case can you
post a short example?

Ah, darn. Yes, you are right of course, the information itself is not
available, as you don't have access to the request. I gotta ponder this
a bit more.

Diez
 
R

Richard Levasseur

(e-mail address removed) schrieb:






Ah, darn. Yes, you are right of course, the information itself is not
available, as you don't have access to the request. I gotta ponder this
a bit more.

Diez

Because he wants to insert parameters at the very start, he can
probably get away with modifying the xml directly. Just find the
position of the <params> (i think thats the tag) and insert the xml
you need after it. Its pretty dirty, but would work. The wire format
isn't that complicated.
 
L

luigi.paioro

Because he wants to insert parameters at the very start, he can
probably get away with modifying the xml directly.  Just find the
position of the <params> (i think thats the tag) and insert the xml
you need after it.  Its pretty dirty, but would work.  The wire format
isn't that complicated.

I think this is exactly what I do... isn't it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top