def doActions( self, actions, inEvent ): self._log.debug("doActions %s", actions) if actions : self._log.debug("doActions %s", actions) for action in actions: try: cmd = action["cmd"] adrs = action["address"] uri = action["uri"] # has uri got any % symbols in it, attempt substitution if inEvent.getPayload() and '%' in uri: # the uri can contain string substitutions of the form %(key)s where # the key is a key into other_data, the s is the string specifier. # See String Formatting Operations in the python library ref. uri = uri % inEvent.getPayload() self._log.info("url address : %s command: %s uri: %s" % (adrs, cmd, uri) ) if ( cmd == "GET" ): # TODO rate limit requests to a webbrick at one time url = str("http://%s%s" % (adrs,uri)) # list operations are thread safe http_sin.addQueueEntry( adrs, action, url ) else: # TODO shift to twisted for POST resp = DoHTTPRequest(adrs, cmd, uri) if resp and (resp.status != 200) and resp.read(): self._log.error("HTTP error %i : %s in targetUrl" % (resp.status,resp.reason) ) except Exception, ex: self._log.exception( ex )
def doActions( self, actions, inEvent ): self._log.debug("doActions %s", actions) if actions : self._log.debug("doActions %s", actions) for action in actions: try: cmd = action["cmd"] adrs = action["address"] uri = action["uri"] # has uri got any % symbols in it, attempt substitution if inEvent.getPayload() and '%' in uri: # the uri can contain string substitutions of the form %(key)s where # the key is a key into other_data, the s is the string specifier. # See String Formatting Operations in the python library ref. uri = uri % inEvent.getPayload() if inEvent.getPayload() and '%' in adrs: # the address can contain string substitutions of the form %(key)s where # the key is a key into other_data, the s is the string specifier. # See String Formatting Operations in the python library ref. adrs = adrs % inEvent.getPayload() self._log.info("url address : %s command: %s uri: %s" % (adrs, cmd, uri) ) if ( cmd == "GET" ): # TODO rate limit requests to a webbrick at one time url = str("http://%s%s" % (adrs,uri)) # list operations are thread safe self.addQueueEntry( adrs, (action,url) ) # we need to handle this on the reactor thread as it will # make network calls twisted is not thread safe! # defer import to here so we can install alternate reactors. from twisted.internet import reactor reactor.callFromThread( self.actionStart, adrs ) else: # TODO shift to twisted for POST resp = DoHTTPRequest(adrs, cmd, uri) if resp and (resp.status != 200) and resp.read(): self._log.error("HTTP error %i : %s in targetUrl" % (resp.status,resp.reason) ) except Exception, ex: self._log.exception( ex )
def default(self, *args): # and request parameters as well. self._log.debug( 'WebBrickProxy args (%i) Uri %s' % (len(args), args ) ) if len(args) == 0: ## no webbrick to handle. pass elif len(args) == 1: # no local part, redirect to index page raise cherrypy.HTTPRedirect( "%s/index.htm" % args[0] ) else: wbaddr = args[0] # Ip or DNS address reqUri = string.join( args[1:], '/' ) # TODO should we use twisted or this is already worker thread so no issue self._log.debug( 'WebBrickProxy Address %s Uri %s' % (wbaddr, reqUri) ) if not self._webbrickVers.has_key(wbaddr): # retrieve interface version string. r = DoHTTPRequest(wbaddr, "GET", "/ver") if r and (r.status == 200): self._webbrickVers[wbaddr] = r.read().strip() else: self._webbrickVers[wbaddr] = 'default' # is wbUri in local directory? # first look in default webbrick group. # TODO allow configuring webbrick UI by address/name # TODO list files that must be proxied to webbrick, i.e. .spi files localdir = os.path.join( self.proxyDir, self._webbrickVers[wbaddr] ) if not os.path.exists( localdir ): os.makedirs(localdir) localpath = os.path.join( localdir, reqUri ) if os.path.exists( localpath ): # yes then return. return cherrypy.lib.cptools.serveFile(localpath) else: # do not cache stuff from webbricks. cherrypy.response.headerMap["cache-control"] = "no-cache" # retrieve from remote webbrick and serve wbUri = '/' + reqUri + '?' + cherrypy.request.query_string self._log.debug( 'WebBrickProxy Retrieve %s Uri %s' % (wbaddr, wbUri) ) r = DoHTTPRequest(wbaddr, "GET", wbUri) if r: if (r.status == 200): txt = r.read() self._log.debug( 'WebBrickProxy %s return (%s)' % (wbaddr, txt) ) # if extension is not .inc, .xml then cache locally. if not localpath.endswith('.inc') and not localpath.endswith('.xml'): f = open(localpath,"wb") f.write(txt) f.close() return txt elif (r.status == 302): # redirect by webbrick self._log.debug("HTTP redirect %s" % ( r.getheaders() ) ) raise cherrypy.HTTPRedirect( r.getheader('Location') ) else: self._log.debug("HTTP error %i : %s %s " % ( r.status,r.reason,r.getheaders() ) ) # TODO error handling # TODO error return {}