cainiaocome / proxpy

Automatically exported from code.google.com/p/proxpy
0 stars 0 forks source link

Hook for internal response #8

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I want to trigger a response in my proxy_mangle_request, this is what I came up 
with to support it. I'm used to working with F5 iRules, so far this seems to be 
the most light weight yet powerful open source alternative for that sort of 
thing.

I don't want to clutter up your beautiful code with my hacks, just looking for 
some advice. Is there any side effect that I may be missing? I'm a python 
newbie. Literally, zero exposure to python until now. This library is so tidy, 
even I (think) I can understand how it works though.

So in "class HTTPRequest", "def __init__" block, I added...

         self.response_hook = False

In "class ProxyHandler", end of "def handle" block...

        if req.response_hook == False:
          # if you need a persistent connection set the flag in order to save the status
          if req.isKeepAlive():
              self.keepalive = True
          else:
              self.keepalive = False

          # Target server host and port
          host, port = ProxyState.getTargetHost(req)

          if req.getMethod() == HTTPRequest.METHOD_GET:
              res = self.doGET(host, port, req)
              self.sendResponse(res)
          elif req.getMethod() == HTTPRequest.METHOD_POST:
              res = self.doPOST(host, port, req)
              self.sendResponse(res)
          elif req.getMethod() == HTTPRequest.METHOD_CONNECT:
              res = self.doCONNECT(host, port, req)
        else:
          self.sendResponse(req.response_hook)

This allows me to do stuff like this in my proxy_mangle_request function, which 
seems to work well...

        hdr = ["Content-Type: text/html", "Server: ProxPy"]
        res = HTTPResponse("HTTP/1.1", 200, "OK", hdr,"<h1>HELLOWORLD</h1>")
        req.response_hook = res.serialize()

I probably need to stick some more headers in there, but other than that have I 
missed anything?

Original issue reported on code.google.com by Krustov...@gmail.com on 10 Jun 2014 at 1:00