def queryPage(url): try: curl = pycurl.Curl() curl.setopt(pycurl.URL, url) buff = StringIO.StringIO() curl.setopt(pycurl.CONNECTTIMEOUT, CONNECTTIMEOUT) curl.setopt(pycurl.TIMEOUT, TIMEOUT) curl.setopt(pycurl.WRITEFUNCTION, buff.write) curl.setopt(curl.HEADER, True) curl.setopt(curl.HTTPHEADER, default_header) curl.setopt(pycurl.CONNECTTIMEOUT, 10) curl.setopt(pycurl.TIMEOUT, 10) #curl.setopt(curl.PROXY, 'http://119.188.115.23:8088') #curl.setopt(pycurl.USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) #AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 #Safari/537.36") curl.perform() d_buff = buff.getvalue() curl.close() httpresponse = HTTPResponse(d_buff) httpresponse.begin() return httpresponse except: return None
def request(self, httphandler, path, data): if not path: data = "<html><body><h1>Available WSDL:s</h1><ul>" for url in self.server.api_handler.get_all_wsdl_urls(): data += '<li><a href="%s">%s</a></li>' % (url, url) data += "</ul></body></html>" return HTTPResponse(data, encoding="utf-8", ctype="text/html") try: data = self.server.api_handler.get_wsdl(path) return HTTPResponse(data, encoding='utf-8', ctype='text/xml') except: raise return HTTPResponse("404 Not Found.", ctype="text/plain", code=404)
def request(self, httphandler, path, data): response = None try: call = json.loads(data, encoding="utf-8") function = call['function'] params = call['params'] if 'session' in call: params = [call['session']] + params except: response = {'error': exterror.ExtMalformedJSONError().struct()} if not response: mo = re.search("\?v([0-9]+)", path) if mo: api_version = int(mo.groups()[0]) else: api_version = 0 response = self.server.call_rpc(httphandler, function, tuple(params), api_version) try: retdata = json.dumps(response, separators=(',', ':'), encoding="utf-8") except: sys.stderr.write("Exception caught when encoding JSON response for %s#%d(%s)\n" % (function, api_version, params)) raise return HTTPResponse(data=retdata, encoding='utf-8', ctype='application/json')
def request(self, httphandler, path, data): if 'authorization' not in httphandler.headers: return HTTPResponse(code=401, data="<h1>401 Authentication Required</h1>", headers=[('WWW-Authentication', 'Negotiate')], ctype="text/html") return ApacheXMLRPCProtocol.request(self, httphandler, path, data)
def request(self, httphandler, path, data): response = None try: params, function = xmlrpclib.loads(data) if function is None: raise ValueError except: traceback.print_exc() response = ({'error': exterror.ExtMalformedXMLRPCError().struct()},) if not response: mo = re.search("\?v([0-9]+)", path) if mo: api_version = int(mo.groups()[0]) else: api_version = 0 response = self.server.call_rpc(httphandler, function, params, api_version) response = (response,) try: retdata = xmlrpclib.dumps(response, methodresponse=1, allow_none=1, encoding='ISO-8859-1') except: sys.stderr.write("Exception caught when serializing response for %s#%d(%s)\n" % (function, api_version, params)) raise return HTTPResponse(data=retdata, encoding='iso-8859-1', ctype='application/xml+rpc')
def proxyqueryPage(url): global index try: #发送前先检测是否需要转码 if '?' in url: line, params = url.split('?', 1) params = Common.urlencode(params) url = "%s?%s" % (line, params) curl = pycurl.Curl() curl.setopt(pycurl.URL, url) buff = StringIO.StringIO() curl.setopt(pycurl.CONNECTTIMEOUT, CONNECTTIMEOUT) curl.setopt(pycurl.TIMEOUT, TIMEOUT) curl.setopt(pycurl.WRITEFUNCTION, buff.write) curl.setopt(curl.HEADER, True) curl.setopt(curl.HTTPHEADER, default_header) curl.setopt(pycurl.CONNECTTIMEOUT, 10) curl.setopt(pycurl.TIMEOUT, 10) #index = random.randint(0, len(proxy_list) - 1) if(len(proxy_list) > 0): proxyvalue = proxy_list[index] curl.setopt(curl.PROXY,proxyvalue) #curl.setopt(pycurl.USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) #AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 #Safari/537.36") curl.perform() d_buff = buff.getvalue() curl.close() httpresponse = HTTPResponse(d_buff) httpresponse.begin() index = index + 1 if(index > len(proxy_list) - 1): index = 0 return httpresponse except: return None
def request(self, httphandler, path, data): # [/api/]3/funname #print "PATH=", path, " DATA=", data pathcomp = path.split("/") if len(pathcomp) == 1 and pathcomp[0] == "api.css": s = self.server.documentation.css_for_html() return HTTPResponse(s.encode("utf-8"), ctype="text/css", encoding="utf-8") if len(pathcomp) == 2: apivers = int(pathcomp[0]) funname = pathcomp[1] s = self.server.documentation.function_as_html(apivers, funname) return HTTPResponse(s.encode("utf-8"), ctype="text/html", encoding="utf-8") # Geterate function list apivers = int(pathcomp[0]) s = self.server.documentation.function_list_as_html(apivers) return HTTPResponse(s.encode("utf-8"), ctype="text/html", encoding="utf-8")
def handle_request(self, request): """ Build up the proper http response depending on the request: - status line - headers - body """ body_string = None try: body_string = self.get_body_string(request) except: pass # We could handle different type of errors here obviously # Our handling is very simple and only handles 200 (OK) and 404 (Not Found) if body_string: response = HTTPResponse(200, body_string) else: response = HTTPResponse(404, "404 Not Found") return response.raw_response()
def handle_response(self, resp): """Translate a RPCHTTPResponse object into an actual HTTP response.""" if isinstance(resp, str): resp = HTTPResponse(resp, "text/plain") if resp.code is None: self.send_response(200) else: self.send_response(resp.code) if resp.ctype is not None: self.send_header("Content-type", resp.ctype) else: self.send_header("Content-type", "text/plain") if resp.encoding: self.send_header("Content-encoding", resp.encoding) if resp.data: self.send_header("Content-length", str(len(resp.data))) if resp.headers: for (name, value) in resp.headers: self.send_header(name, value) self.end_headers() self.wfile.write(resp.data) sys.stdout.flush() if False: print type(resp.data) l = 0 for c in resp.data: if ord(c) < 32: print " ", else: print c, print hex(ord(c))[-2:], l += 1 if (l % 16) == 0: print print # shut down the connection self.wfile.flush() if self.server.ssl_enabled: self.connection.shutdown(socket.SHUT_WR)
class HTTPWebHandler(): default_contents = { 301: '<h1>301 - Moved Permanently</h1>', 404: '<h1>404 - Path Not Found</h1>', 405: '<h1>405 - Method Not Allowed</h1>' } web_root = str(pathlib.Path("www").resolve()) + os.sep def __init__(self, request: HTTPRequest): self.request = request self.response = HTTPResponse() # Go ahead and handle the request self.handle_request() def handle_request(self): """ Handles the request and prepares the response :return: """ if self.request.method != 'GET': self.response.code = 405 return self.get_path() def get_path(self): """ Gets the path requested and places in the response, setting the appropriate headers :return: """ # Since the Path class always removes trailing slashes, we need to store the slash trailing_slash = os.sep if self.request.uri[-1] == '/' else '' request_path = pathlib.Path(self.request.uri) # Normalize this path relative to the web root norm_path = pathlib.Path(self.web_root + str(request_path)).resolve() # Then check if the path is within the web root try: norm_path.relative_to(pathlib.Path(self.web_root)) except: # We do not authorize paths outside of web root # But a 403 Forbidden should state why the request was not authorized # and I don't want to encourage whoever is trying to hack my root # So we do a 404 instead self.response.code = 404 return if norm_path.exists() and norm_path.is_file(): with norm_path.open() as f: self.response.contents = f.read() self.response.code = 200 # Information about this header was gathered from # MDN Web Docs # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type # Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ self.response.add_header( 'Content-Type', f'text/{norm_path.suffix[1:]}; charset=UTF-8') elif (norm_path / 'index.html').exists() and norm_path.is_dir(): if trailing_slash == os.sep: # Serve the index.html file for this directory with (norm_path / 'index.html').open() as f: self.response.contents = f.read() self.response.code = 200 # Information about this header was gathered from # MDN Web Docs # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type # Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ self.response.add_header('Content-Type', 'text/html; charset=UTF-8') else: # The path is wrong, they are attempting to access the directory without a slash self.response.code = 301 self.response.add_header('Location', self.request.uri + '/') else: self.response.code = 404
def __init__(self, request: HTTPRequest): self.request = request self.response = HTTPResponse() # Go ahead and handle the request self.handle_request()
async def test(request): return HTTPResponse(200)("aquarius")
def request(self, httphandler, path, data): """Returns a document from the docroot, replacing special tags with formatted internal information (function definitions for example). Not a fully featured Web-server, just a very, very simple way of getting documentation as XHTML over HTTP from the live server. Special tags: <:function:server_documentation:> Inserts XHTML for the complete documentation of the "server_documentation" function. """ if not self.docroot: doc = '<html><head><title>XML RPC Server Error</title>' doc += '</head><body>' doc += '<h1>No doc root set.</h1>' doc += '</body></html>' return HTTPResponse(ctype='text/html', data=doc) if path: if ('..' in path or '/.' in path or './' in path or path[0] == '/'): return HTTPResponse(ctype='text/html', data='<h1>Invalid path</h1>') if path[-1] == '/': path += 'index.html' else: path = 'index.html' #for suff in ['.py', '.pm', '.patch']: # if path.endswith(suff): # mime_type, enc = ('text/plain', 'UTF-8') # break #else: mime_type, enc = mimetypes.guess_type(path) #if path.startswith("client/"): # path = '../../' + path error404 = ('text/html', None, '<h1>Not Found</h1>') try: path = os.path.join(self.docroot, path) if os.path.exists(path): data = file(path).read() else: mime_type, enc, data = error404 except Exception as _e: mime_type, enc, data = error404 if mime_type == 'text/html' and not enc: # HTML documents can contain special <:fun:> tags which # are automatically expanded. ls = re.split('<:([^>]+):>', data) data = "" while ls: if len(ls) == 1: data += ls[0] break (raw, special) = ls[:2] ls = ls[2:] data += raw arglist = special.split(':') function = arglist[0] if function == 'function': data += self.html_function_definition(arglist[1]) if function == 'category': catname = arglist[1] try: cat = self.server.get_category(catname) flist = self.server.functions_with_category(cat) data += '<div class="catlist">' data += '<div class="catdesc">%s</div>' % (cat.desc,) data += '<ul class="catlist">' for funname in [f.rpcname for f in flist]: data += '<li><a href="/functions/%s" class="funlink">%s</a></li>' % (funname, funname) data += '</ul></div>' except KeyError: data += "INVALID CATEGORY" else: data += "INVALID COLON FUNCTION" return HTTPResponse(ctype=mime_type, encoding=enc, data=data)
retelem.set_namespace("m", namespace) env = XMLNode("Envelope") header = env.new("Header") body = env.new("Body") env.set_namespace("env", "http://schemas.xmlsoap.org/soap/envelope/") body.add(retelem) except exterror.ExtSOAPError, e: env = e.get_envelope(namespace) except Exception, e: traceback.print_exc() srverr = exterror.ExtSOAPServerError("Internal server error, contact developer") env = srverr.get_envelope(namespace) retxml = "<?xml version='1.0' encoding='UTF-8'?>\n" + env.xml() return HTTPResponse(data=retxml, encoding="UTF-8", ctype="text/xml") class KRB5SOAPProtocol(SOAPProtocol): def request(self, httphandler, path, data): if 'authorization' not in httphandler.headers: return HTTPResponse(code=401, data="<h1>401 Authentication Required</h1>", headers=[('WWW-Authentication', 'Negotiate')], ctype="text/html") path = path.replace("spnego+", "") return SOAPProtocol.request(self, httphandler, path, data)