def create(self, request): # URI to be called if request.POST.has_key('url'): url = request.POST['url'] else: return HttpResponse( string_concat(['<error>', _(u"Url not specified"), '</error>'])) # HTTP method, by default is GET if request.POST.has_key('method'): method = request.POST['method'] else: method = "GET" # Params if request.POST.has_key('params'): params = request.POST['params'] # HTTP call try: # Request creation req = Request(url) # Add POST parameters if (method == "POST"): req.add_data(params) # Add original request Headers to the request for header in request.META.items(): req.add_header(header[0], header[1]) # The same with cookies cookies = '' for cookie in request.COOKIES.items(): cookies = cookies + cookie[0] + '=' + cookie[1] + '; ' req.add_header('Cookie', cookies) # Open the request file = urlopen(req) # Add content-type header to the response if file.info().has_key('content-type'): response = HttpResponse(file.read(), mimetype=file.info()['content-type']) else: response = HttpResponse(file.read()) # Add all the headers recieved to the response headers = file.info().items() for header in headers: if is_valid_header(string.lower(header[0])): response[header[0]] = header[1] return response except Exception, e: return HttpResponseServerError( "<html><head><title>Error</title></head><body>%s</body></html>" % e, mimetype='text/html; charset=UTF-8')
def create(self,request): # URI to be called if request.POST.has_key('url'): url = request.POST['url'] else: return HttpResponse(string_concat(['<error>',_(u"Url not specified"),'</error>'])) # HTTP method, by default is GET if request.POST.has_key('method'): method = request.POST['method'] else: method = "GET" # Params if request.POST.has_key('params'): params = request.POST['params'] # HTTP call try: # Request creation req = Request (url) # Add POST parameters if (method=="POST"): req.add_data(params) # Add original request Headers to the request for header in request.META.items(): req.add_header(header[0], header[1]) # The same with cookies cookies = '' for cookie in request.COOKIES.items(): cookies = cookies + cookie[0] + '=' + cookie[1] + '; ' req.add_header('Cookie', cookies) # Open the request file = urlopen(req) # Add content-type header to the response if file.info().has_key('content-type'): response = HttpResponse (file.read(), mimetype=file.info()['content-type']) else: response = HttpResponse (file.read()) # Add all the headers recieved to the response headers = file.info().items() for header in headers: if is_valid_header (string.lower(header[0])): response[header[0]] = header[1] return response except Exception, e: return HttpResponseServerError("<html><head><title>Error</title></head><body>%s</body></html>" % e, mimetype='text/html; charset=UTF-8')
def do_request(self, request, url, method, data): # HTTP call try: url = iri_to_uri(url) # Request creation proto, host, cgi, param, query = urlparse.urlparse(url)[:5] # manage proxies with authentication (get it from environment) proxy = None for proxy_name in settings.NOT_PROXY_FOR: if host.startswith(proxy_name): proxy = urllib2.ProxyHandler({}) # no proxy break if not proxy: #Host is not included in the NOT_PROXY_FOR list => proxy is needed! proxy = urllib2.ProxyHandler() # proxies from environment opener = urllib2.build_opener(proxy) # Adds original request Headers to the request headers = {} for header in request.META.items(): header_name = header[0].lower() if header_name == 'content_type' and header[1]: headers["content-type"] = header[1] elif header_name == 'cookie' or header_name == 'http_cookie': cookie_parser = Cookie.SimpleCookie(header[1]) # Remove EzWeb cookies if hasattr(settings, 'SESSION_COOKIE_NAME'): del cookie_parser[settings.SESSION_COOKIE_NAME] if hasattr(settings, 'CSRF_COOKIE_NAME') and settings.CSRF_COOKIE_NAME in cookie_parser: del cookie_parser[settings.CSRF_COOKIE_NAME] content = ', '.join([cookie_parser[key].OutputString() for key in cookie_parser]) if content != '': headers['Cookie'] = content elif self.http_headerRE.match(header_name) and not header_name in self.blacklisted_http_headers: fixed_name = header_name.replace("http_", "", 1).replace('_', '-') headers[fixed_name] = header[1] protocolVersion = self.protocolRE.match(request.META['SERVER_PROTOCOL']) if protocolVersion != None: protocolVersion = protocolVersion.group(1) else: protocolVersion = '1.1' hostName = self.hostRE.match(request.META['HTTP_HOST']) if hostName != None: hostName = hostName.group(1) else: hostName = socket.gethostname() via_header = "%s %s (EzWeb-python-Proxy/1.1)" % (protocolVersion, hostName) headers["Via"] = via_header if (method == 'POST' or method == 'PUT') and not 'content-type' in headers: # Add Content-Type (Servlets bug) headers['content-type'] = "application/x-www-form-urlencoded" # Remote user header if not request.user.is_anonymous(): headers['Remote-User'] = request.user.username # Open the request try: res = self._do_request(opener, method, url, data, headers) except urllib2.URLError, e: if e.reason[0] == errno.ECONNREFUSED: return HttpResponse(status=504) else: return HttpResponseNotFound(e.reason) # Add content-type header to the response res_info = res.info() if ('Content-Type' in res_info): response = HttpResponse(res.read(), mimetype=res_info['Content-Type']) else: response = HttpResponse(res.read()) # Set status code to the response response.status_code = res.code # Add all the headers received from the response headers = res.headers for header in headers: header_lower = header.lower() if header_lower == 'set-cookie': cookie_parser = Cookie.SimpleCookie() cookies = res.headers.getheaders(header) for i in range(len(cookies)): cookie_parser.load(cookies[i]) for key in cookie_parser: response.set_cookie(key, cookie_parser[key].value, expires=cookie_parser[key]['expires'], path=cookie_parser[key]['path'], domain=cookie_parser[key]['domain']) elif header_lower == 'via': via_header = via_header + ', ' + headers[header] elif is_valid_header(header_lower): response[header] = headers[header] response['Via'] = via_header return response
def create(self, request): # URI to be called if request.POST.has_key('url'): url = request.POST['url'] else: return HttpResponse( string_concat(['<error>', _(u"Url not specified"), '</error>'])) # HTTP method, by default is GET if request.POST.has_key('method'): method = request.POST['method'] else: method = "GET" # Params if request.POST.has_key('params'): params = encode_query(request.POST['params']) else: params = '' # HTTP call try: # Request creation proto, host, cgi, param, query = urlparse.urlparse(url)[:5] query = encode_query(query) # Proxy support proxy = "" try: proxy = settings.PROXY_SERVER except Exception: proxy = "" conn = getConnection(proto, proxy, host) # Adds original request Headers to the request (and modifies Content-Type for Servlets) headers = {} has_content_type = False http_content_type_value = '' for header in request.META.items(): if (header[0].lower() == 'content-type'): if (header[1] != None and header[1].lower() != 'none'): has_content_type = True if (header[0].lower() == 'http_content_type'): http_content_type_value = header[1] headers[header[0]] = header[1] # Add Content-Type (Servlets bug) if ((method == 'POST' or method == 'PUT') and not has_content_type): if (http_content_type_value != ''): headers['Content-Type'] = http_content_type_value else: headers[ 'Content-Type'] = 'application/x-www-form-urlencoded' # The same with cookies cookies = '' for cookie in request.COOKIES.items(): cookies = cookies + cookie[0] + '=' + cookie[1] + '; ' headers['Cookie'] = cookies # Open the request if query != '': cgi = cgi + '?%s' % query if (proxy != ""): conn.request(method, url, params, headers) else: conn.request(method, cgi, params, headers) res = conn.getresponse() # Redirect resolution MAX_REDIRECTS = 50 index_redirects = 0 while (res.status >= 300) and (res.status < 400): if (index_redirects >= MAX_REDIRECTS): return HttpResponse( '<error>Redirect limit has been exceeded</error>') index_redirects = index_redirects + 1 url = res.getheader('Location') proto, host, cgi, param, auxquery = urlparse.urlparse(url)[:5] conn = getConnection(proto, proxy, host) auxquery = encode_query(auxquery) if query != '': query = query + "&" + auxquery else: query = auxquery if query != '': cgi = cgi + '?%s' % query if (proxy != ""): conn.request(method, url, params, headers) else: conn.request(method, cgi, params, headers) res = conn.getresponse() # Add content-type header to the response if res.getheader('content-type'): response = HttpResponse(res.read(), mimetype=res.getheader('content-type')) else: response = HttpResponse(res.read()) # Set status code to the response response.status_code = res.status # Add all the headers recieved to the response headers = res.getheaders() for header in headers: if is_valid_header(string.lower(header[0])): response[header[0]] = header[1] return response except Exception, e: return HttpResponseServerError( "<html><head><title>Error HTTP 500</title></head><body>%s</body></html>" % e, mimetype='text/html; charset=UTF-8')
def do_request(self, request, url, method, data): # HTTP call try: url = iri_to_uri(url) # Request creation proto, host, cgi, param, query = urlparse.urlparse(url)[:5] # manage proxies with authentication (get it from environment) proxy = None for proxy_name in settings.NOT_PROXY_FOR: if host.startswith(proxy_name): proxy = urllib2.ProxyHandler({}) # no proxy break if not proxy: #Host is not included in the NOT_PROXY_FOR list => proxy is needed! proxy = urllib2.ProxyHandler() # proxies from environment opener = urllib2.build_opener(proxy) # Adds original request Headers to the request headers = {} for header in request.META.items(): header_name = header[0].lower() if header_name == 'content_type' and header[1]: headers["content-type"] = header[1] elif header_name == 'cookie' or header_name == 'http_cookie': cookie_parser = Cookie.SimpleCookie(header[1]) # Remove EzWeb cookies if hasattr(settings, 'SESSION_COOKIE_NAME'): del cookie_parser[settings.SESSION_COOKIE_NAME] if hasattr( settings, 'CSRF_COOKIE_NAME' ) and settings.CSRF_COOKIE_NAME in cookie_parser: del cookie_parser[settings.CSRF_COOKIE_NAME] content = ', '.join([ cookie_parser[key].OutputString() for key in cookie_parser ]) if content != '': headers['Cookie'] = content elif self.http_headerRE.match( header_name ) and not header_name in self.blacklisted_http_headers: fixed_name = header_name.replace("http_", "", 1).replace('_', '-') headers[fixed_name] = header[1] protocolVersion = self.protocolRE.match( request.META['SERVER_PROTOCOL']) if protocolVersion != None: protocolVersion = protocolVersion.group(1) else: protocolVersion = '1.1' hostName = self.hostRE.match(request.META['HTTP_HOST']) if hostName != None: hostName = hostName.group(1) else: hostName = socket.gethostname() via_header = "%s %s (EzWeb-python-Proxy/1.1)" % (protocolVersion, hostName) headers["Via"] = via_header if (method == 'POST' or method == 'PUT') and not 'content-type' in headers: # Add Content-Type (Servlets bug) headers['content-type'] = "application/x-www-form-urlencoded" # Remote user header if not request.user.is_anonymous(): headers['Remote-User'] = request.user.username # Open the request try: res = self._do_request(opener, method, url, data, headers) except urllib2.URLError, e: if e.reason[0] == errno.ECONNREFUSED: return HttpResponse(status=504) else: return HttpResponseNotFound(e.reason) # Add content-type header to the response res_info = res.info() if ('Content-Type' in res_info): response = HttpResponse(res.read(), mimetype=res_info['Content-Type']) else: response = HttpResponse(res.read()) # Set status code to the response response.status_code = res.code # Add all the headers received from the response headers = res.headers for header in headers: header_lower = header.lower() if header_lower == 'set-cookie': cookie_parser = Cookie.SimpleCookie() cookies = res.headers.getheaders(header) for i in range(len(cookies)): cookie_parser.load(cookies[i]) for key in cookie_parser: response.set_cookie( key, cookie_parser[key].value, expires=cookie_parser[key]['expires'], path=cookie_parser[key]['path'], domain=cookie_parser[key]['domain']) elif header_lower == 'via': via_header = via_header + ', ' + headers[header] elif is_valid_header(header_lower): response[header] = headers[header] response['Via'] = via_header return response
# # if query != '': # cgi = cgi + '?%s' % query # # if (proxy != ""): # conn.request(method, url, params, headers) # else: # conn.request(method, cgi, params, headers) # # res = conn.getresponse() # Add content-type header to the response try: response = HttpResponse (res.read(), mimetype=res.info()['Content-Type']) except: response = HttpResponse (res.read()) # Set status code to the response response.status_code = res.code # Add all the headers recieved to the response headers = res.headers for header in headers: if is_valid_header (string.lower(header)): response[header] = headers[header] return response except Exception, e: return HttpResponseServerError("<html><head><title>Error HTTP 500</title></head><body>%s</body></html>" % e, mimetype='text/html; charset=UTF-8')
def create(self,request): # URI to be called if request.POST.has_key('url'): url = request.POST['url'] else: return HttpResponse(string_concat(['<error>',_(u"Url not specified"),'</error>'])) # HTTP method, by default is GET if request.POST.has_key('method'): method = request.POST['method'] else: method = "GET" # Params if request.POST.has_key('params'): params = request.POST['params'] else: params = '' # HTTP call try: # Request creation url = unquote(url) proto, host, cgi, param, query = urlparse.urlparse(url)[:5] # Proxy support proxy = "" try: proxy = settings.PROXY_SERVER except Exception: proxy = "" if (proxy != ""): conn = httplib.HTTPConnection(proxy) else: conn = httplib.HTTPConnection(host) # Add original request Headers to the request headers = {} for header in request.META.items(): headers[header[0]] = header[1] # The same with cookies cookies = '' for cookie in request.COOKIES.items(): cookies = cookies + cookie[0] + '=' + cookie[1] + '; ' headers['Cookie'] = cookies # Open the request if query != '': cgi = cgi + '?%s' % query if (proxy != ""): conn.request(method, url, params, headers) else: conn.request(method, cgi, params, headers) res = conn.getresponse() # Redirect resolution while (res.status >= 300) and (res.status < 400): url = unquote(res.getheader('Location')) proto, host, cgi, param, auxquery = urlparse.urlparse(url)[:5] conn = httplib.HTTPConnection(host) if query != '': query = query + "&" + auxquery else: query = auxquery if query != '': cgi = cgi + '?%s' % query if (proxy != ""): conn.request(method, url, params, headers) else: conn.request(method, cgi, params, headers) res = conn.getresponse() # Add content-type header to the response if res.getheader('content-type'): response = HttpResponse (res.read(), mimetype=res.getheader('content-type')) else: response = HttpResponse (res.read()) # Set status code to the response response.status_code = res.status # Add all the headers recieved to the response headers = res.getheaders() for header in headers: if is_valid_header (string.lower(header[0])): response[header[0]] = header[1] return response except Exception, e: return HttpResponseServerError("<html><head><title>Error HTTP 500</title></head><body>%s</body></html>" % e, mimetype='text/html; charset=UTF-8')
def create(self,request): # URI to be called if request.POST.has_key('url'): url = request.POST['url'] else: return HttpResponse(string_concat(['<error>',_(u"Url not specified"),'</error>'])) # HTTP method, by default is GET if request.POST.has_key('method'): method = request.POST['method'] else: method = "GET" # Params if request.POST.has_key('params'): params = encode_query(request.POST['params']) else: params = '' # HTTP call try: # Request creation proto, host, cgi, param, query = urlparse.urlparse(url)[:5] query = encode_query(query) # Proxy support proxy = "" try: proxy = settings.PROXY_SERVER except Exception: proxy = "" conn = getConnection(proto, proxy, host) # Adds original request Headers to the request (and modifies Content-Type for Servlets) headers = {} has_content_type = False http_content_type_value = '' for header in request.META.items(): if (header[0].lower() == 'content-type'): if (header[1] != None and header[1].lower() != 'none'): has_content_type = True if (header[0].lower() == 'http_content_type'): http_content_type_value = header[1] headers[header[0]] = header[1] # Add Content-Type (Servlets bug) if ((method == 'POST' or method == 'PUT') and not has_content_type): if (http_content_type_value != ''): headers['Content-Type'] = http_content_type_value else: headers['Content-Type'] = 'application/x-www-form-urlencoded' # The same with cookies cookies = '' for cookie in request.COOKIES.items(): cookies = cookies + cookie[0] + '=' + cookie[1] + '; ' headers['Cookie'] = cookies # Open the request if query != '': cgi = cgi + '?%s' % query if (proxy != ""): conn.request(method, url, params, headers) else: conn.request(method, cgi, params, headers) res = conn.getresponse() # Redirect resolution MAX_REDIRECTS = 50 index_redirects = 0 while (res.status >= 300) and (res.status < 400): if (index_redirects >= MAX_REDIRECTS): return HttpResponse('<error>Redirect limit has been exceeded</error>') index_redirects = index_redirects + 1 url = res.getheader('Location') proto, host, cgi, param, auxquery = urlparse.urlparse(url)[:5] conn = getConnection(proto, proxy, host) auxquery = encode_query(auxquery) if query != '': query = query + "&" + auxquery else: query = auxquery if query != '': cgi = cgi + '?%s' % query if (proxy != ""): conn.request(method, url, params, headers) else: conn.request(method, cgi, params, headers) res = conn.getresponse() # Add content-type header to the response if res.getheader('content-type'): response = HttpResponse (res.read(), mimetype=res.getheader('content-type')) else: response = HttpResponse (res.read()) # Set status code to the response response.status_code = res.status # Add all the headers recieved to the response headers = res.getheaders() for header in headers: if is_valid_header (string.lower(header[0])): response[header[0]] = header[1] return response except Exception, e: return HttpResponseServerError("<html><head><title>Error HTTP 500</title></head><body>%s</body></html>" % e, mimetype='text/html; charset=UTF-8')