def dispatch_wsgi(self): """ WSGI handler """ if hasattr(self, 'response'): return self._write_response() LOGGER.debug('WSGI mode detected') if self.environ['REQUEST_METHOD'] == 'POST': try: request_body_size = int(self.environ.get('CONTENT_LENGTH', 0)) except (ValueError): request_body_size = 0 self.requesttype = 'POST' self.request = self.environ['wsgi.input'].read(request_body_size) LOGGER.debug('Request type: POST. Request:\n%s\n', self.request) else: # it's a GET request self.requesttype = 'GET' self.request = wsgiref.util.request_uri(self.environ) try: query_part = splitquery(self.request)[-1] self.kvp = dict(parse_qsl(query_part, keep_blank_values=True)) except AttributeError as err: LOGGER.exception('Could not parse query string') self.kvp = {} LOGGER.debug('Request type: GET. Request:\n%s\n', self.request) return self.dispatch()
def get_os_hosts(request, context): host, query = parse.splitquery(request.url) zone = 'patron1' service = None if query: qs = parse.parse_qs(query) try: zone = qs['zone'][0] except Exception: pass try: service = qs['service'][0] except Exception: pass return { 'hosts': [ { 'host': 'host1', 'service': service or 'patron-compute', 'zone': zone }, { 'host': 'host1', 'service': service or 'patron-cert', 'zone': zone } ] }
def test_web_summarylist(browser, basic_html): from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver import ActionChains from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException ignore_exc = (NoSuchElementException, StaleElementReferenceException) html_dir, dvcs = basic_html last_change_hash = dvcs.get_hash('master~4') browser.set_window_size(1200, 900) with tools.preview(html_dir) as base_url: get_with_retry(browser, base_url) summarylist_btn = browser.find_element_by_link_text('Benchmark list') summarylist_btn.click() # Check text content in the table base_link = browser.find_element_by_link_text( 'params_examples.track_find_test') cur_row = base_link.find_element_by_xpath('../..') m = re.match( 'params_examples.track_find_test \\([12]\\) 2.00 \u221233.3% \\(-1.00\\).*' + last_change_hash[:8], cur_row.text) assert m, cur_row.text # Check link base_href, qs = splitquery(base_link.get_attribute('href')) base_url, tag = splittag(base_href) assert parse_qs(qs) == { 'ram': ['128GB'], 'cpu': ['Blazingly fast'], 'NUL': ['[none]'] } assert tag == 'params_examples.track_find_test' # Change table sort (sorting is async, so needs waits) sort_th = browser.find_element_by_xpath('//th[text()="Recent change"]') sort_th.click() WebDriverWait(browser, 5).until( EC.text_to_be_present_in_element( ('xpath', '//tbody/tr[1]'), 'params_examples.track_find_test')) # Try to click cpu selector link in the panel cpu_select = browser.find_element_by_link_text('Not /really/ <fast>') cpu_select.click() # For the other CPU, there is no recent change recorded, only # the latest result is available def check(*args): base_link = browser.find_element_by_link_text( 'params_examples.track_find_test') cur_row = base_link.find_element_by_xpath('../..') return cur_row.text in ('params_examples.track_find_test (1) 2.00', 'params_examples.track_find_test (2) 2.00') WebDriverWait(browser, 5, ignored_exceptions=ignore_exc).until(check)
def test_web_summarylist(browser, basic_html): ignore_exc = (NoSuchElementException, StaleElementReferenceException) html_dir, dvcs = basic_html last_change_hash = dvcs.get_hash('master~4') browser.set_window_size(1200, 900) with tools.preview(html_dir) as base_url: get_with_retry(browser, base_url) summarylist_btn = browser.find_element_by_link_text('Benchmark list') summarylist_btn.click() # Check text content in the table base_link = browser.find_element_by_link_text('params_examples.track_find_test') cur_row = base_link.find_element_by_xpath('../..') m = re.match('params_examples.track_find_test \\([12]\\) 2.00 \u221233.3% \\(-1.00\\).*' + last_change_hash[:8], cur_row.text) assert m, cur_row.text # Check link base_href, qs = splitquery(base_link.get_attribute('href')) base_url, tag = splittag(base_href) assert parse_qs(qs) == {'ram': ['128GB'], 'cpu': ['Blazingly fast'], 'NUL': ['[none]']} assert tag == 'params_examples.track_find_test' # Change table sort (sorting is async, so needs waits) sort_th = browser.find_element_by_xpath('//th[text()="Recent change"]') sort_th.click() WebDriverWait(browser, WAIT_TIME).until( EC.text_to_be_present_in_element(('xpath', '//tbody/tr[1]'), 'params_examples.track_find_test')) # Try to click cpu selector link in the panel cpu_select = browser.find_element_by_link_text('Not /really/ <fast>') cpu_select.click() # For the other CPU, there is no recent change recorded, only # the latest result is available def check(*args): links = browser.find_elements_by_link_text('params_examples.track_find_test') visible_links = [item for item in links if item.is_displayed()] row_texts = [link.find_element_by_xpath('../..').text for link in visible_links] row_texts.sort() return row_texts == ['params_examples.track_find_test (1) 2.00', 'params_examples.track_find_test (2) 2.00'] WebDriverWait(browser, WAIT_TIME, ignored_exceptions=ignore_exc).until(check)
def urldecode(url): """ >>> urldecode('http://google.com/search?q=bar&x=y') ('http://google.com/search', {'q': 'bar', 'x': 'y'}) >>> urldecode('http://google.com/') ('http://google.com/', {}) """ base, query = splitquery(url) query = query or "" items = [item.split('=', 1) for item in query.split('&') if '=' in item] d = dict((unquote(k), unquote_plus(v)) for (k, v) in items) return base, d
def get_os_hosts(request, url, headers): host, query = parse.splitquery(url) zone = "nova1" if query: qs = parse.parse_qs(query) try: zone = qs["zone"][0] except Exception: pass data = { "hosts": [ {"host": "host1", "service": "nova-compute", "zone": zone}, {"host": "host1", "service": "nova-cert", "zone": zone}, ] } return 200, headers, jsonutils.dumps(data)
def get_os_hosts(request, url, headers): host, query = parse.splitquery(url) zone = 'nova1' if query: qs = parse.parse_qs(query) try: zone = qs['zone'][0] except Exception: pass data = { 'hosts': [{ 'host': 'host1', 'service': 'nova-compute', 'zone': zone }, { 'host': 'host1', 'service': 'nova-cert', 'zone': zone }] } return 200, headers, jsonutils.dumps(data)
def unpackHttpInfo(self, ngamsCfgObj, httpMethod, path, headers): """ Unpack the information from an HTTP request and set the members of the class accordingly. httpMethod: HTTP request method (GET/POST) (string). path: Path of HTTP request (string). headers: Dictionary containing the information for the headers of the HTTP query (dictionary). Returns: Reference to object itself. """ self.setHttpMethod(httpMethod) # Handle the HTTP headers. for key in headers.keys(): keyTmp = key.lower() val = urlparse.unquote(headers[key]) logger.debug("Parsing HTTP header key: %s with value: %s", key, val) self.__httpHdrDic[key.lower()] = val if (keyTmp == "content-disposition"): pars = ngamsLib.parseHttpHdr(headers[key]) for name, val in pars.items(): val = urlparse.unquote(val) if name == "filename": self.setFileUri(os.path.basename(val)) elif name == "mime_type": if (self.getMimeType() == ""): self.setMimeType(val) elif name.strip(): self.addHttpPar(name, val) elif (keyTmp == "content-type"): if (self.getMimeType() == ""): self.setMimeType(val.split(";")[0].strip(" \"")) elif (keyTmp == "content-length"): self.setSize(val.strip(" \"")) elif (keyTmp == "authorization"): self.setAuthorization(urlparse.unquote(val.strip())) # Handle the information in the path. path, query = urlparse.splitquery(path) self.setCmd(path.lstrip('/')) if (query): parList = urlparse.parse_qsl(query) for name, val in parList: logger.debug("Found parameter: %s with value: %s", name, val) if (httpMethod in [NGAMS_HTTP_GET, NGAMS_HTTP_PUT, NGAMS_HTTP_POST]): self.addHttpPar(name, val) # Subscription file delivery is always POST, but sometimes we want it behave like GET (e.g. proxy qrchive) to pass on parametres in url string. if name == "filename": self.setFileUri(val) elif name == "mime_type": self.setMimeType(val) elif name == "authorization": self.setAuthorization(val) # Small trick to set the mime-type in case not defined by the # Content-Type HTTP header. if ((self.getCmd() == NGAMS_ARCHIVE_CMD) and ((self.getMimeType() == "") or ((self.getMimeType() == NGAMS_ARCH_REQ_MT)))): if (self.getFileUri().strip() == ""): raise Exception( genLog("NGAMS_ER_CMD_EXEC", [NGAMS_ARCHIVE_CMD, "Missing parameter: filename"])) mimeType = ngamsLib.detMimeType(ngamsCfgObj.getMimeTypeMappings(), self.getFileUri(), 1) if (mimeType == NGAMS_UNKNOWN_MT): errMsg = genLog("NGAMS_ER_UNKNOWN_MIME_TYPE1", [self.getFileUri()]) raise Exception(errMsg) else: self.setMimeType(mimeType) # In the case of an Archive Request, check that the mime-type is # known to this NGAS installation. if (self.getCmd() == NGAMS_ARCHIVE_CMD): # - To do this, we check if there is a Stream defined for # this kind of data. if (not ngamsCfgObj.getStreamFromMimeType(self.getMimeType())): errMsg = genLog( "NGAMS_ER_UNKNOWN_MIME_TYPE2", [self.getMimeType(), self.getFileUri()]) raise Exception(errMsg) return self
def test_web_summarylist(browser, basic_html): ignore_exc = (NoSuchElementException, StaleElementReferenceException) html_dir, dvcs = basic_html last_change_hash = dvcs.get_hash('master~4') browser.set_window_size(1200, 900) with tools.preview(html_dir) as base_url: get_with_retry(browser, base_url) summarylist_btn = browser.find_element_by_link_text('Benchmark list') summarylist_btn.click() # Check text content in the table base_link = browser.find_element_by_link_text( 'params_examples.track_find_test') cur_row = base_link.find_element_by_xpath('../..') m = re.match( 'params_examples.track_find_test \\([12]\\) 2.00 \u221233.3% \\(-1.00\\).*' + last_change_hash[:8], cur_row.text) assert m, cur_row.text # Check units in row base_link2 = browser.find_element_by_link_text( 'params_examples.track_bytes') cur_row2 = base_link2.find_element_by_xpath('../..') m = re.match(r'params_examples.track_bytes\s*1.000M', cur_row2.text) assert m, cur_row2.text # Check link base_href, qs = splitquery(base_link.get_attribute('href')) base_url, tag = splittag(base_href) assert parse_qs(qs) == { 'ram': ['128GB'], 'cpu': ['Blazingly fast'], 'NUL': ['[none]'] } assert tag == 'params_examples.track_find_test' # Change table sort (sorting is async, so needs waits) sort_th = browser.find_element_by_xpath('//th[text()="Recent change"]') sort_th.click() WebDriverWait(browser, WAIT_TIME).until( EC.text_to_be_present_in_element( ('xpath', '//tbody/tr[1]'), 'params_examples.track_find_test')) # Try to click cpu selector link in the panel cpu_select = browser.find_element_by_link_text('Not /really/ <fast>') cpu_select.click() # For the other CPU, there is no recent change recorded, only # the latest result is available def check(*args): links = browser.find_elements_by_link_text( 'params_examples.track_find_test') visible_links = [item for item in links if item.is_displayed()] row_texts = [ link.find_element_by_xpath('../..').text for link in visible_links ] row_texts.sort() if len(row_texts) != 2: return False ok = (re.match( r'^params_examples\.track_find_test \(1\) 2\.00 .*\(-1\.00\).*$', row_texts[0] ) and re.match( r'^params_examples\.track_find_test \(2\) 2\.00 .*\(-1\.00\).*$', row_texts[1])) return ok WebDriverWait(browser, WAIT_TIME, ignored_exceptions=ignore_exc).until(check)
def __call__(self, line): d = json.loads(line) d['@timestamp'] = self.timestamp_parser(d.pop('timestamp')) if self.hostname is not None: d['@hostname'] = self.hostname for i in list(d): if d[i] in ('-', ''): del d[i] if 'request_uri' not in d and 'request' in d: s = d['request'].split() if len(s) == 3: _, d['request_uri'], d['server_protocol'] = s del d['request'] if 'request_uri' in d: d['request_path'], d['request_qs'] = splitquery(d['request_uri']) if d['request_qs'] is None: del d['request_qs'] else: d['query'] = parse_qs(d['request_qs']) for i in list(d['query']): if '.' in i: d['query'][i.replace('.', '_')] = d['query'].pop(i) lon_alias = 'lng' if 'lng' in d['query'] else 'lon' if 'lat' in d['query'] and lon_alias in d['query']: try: d['query_geo'] = { 'lat': float(d['query']['lat'][0]), 'lon': float(d['query'][lon_alias][0]), } except ValueError: pass for n, i in enumerate(d['request_path'].split('/')): if i: # skip the empty 0-th and last components d['request_path_%d' % n] = i for i in [ 'request_length', 'connection_requests', 'bytes_sent', 'connection' ]: if i in d: d[i] = int(d[i]) for i in ['request_time', 'gzip_ratio']: if i in d: d[i] = float(d[i]) for i in [ 'forwarded_for', 'upstream_addr', 'upstream_status', 'upstream_response_time', 'upstream_response_length', 'upstream_connect_time', ]: if i not in d: continue d[i] = [j.strip() for j in d[i].replace(', ', ' : ').split(' : ')] d[i] = [j for j in d[i] if j not in ('', '-')] if not d[i]: del d[i] if 'upstream_response_time' in d: d['upstream_response_time'] = [ float(i) for i in d['upstream_response_time'] ] if 'upstream_connect_time' in d: d['upstream_connect_time'] = [ float(i) for i in d['upstream_connect_time'] ] if 'upstream_response_length' in d: d['upstream_response_length'] = [ int(i) for i in d['upstream_response_length'] ] if self.geoip is not None: g = self.geoip.record_by_name(d['remote_addr']) if g is not None: d['geoip'] = { 'lat': g['latitude'], 'lon': g['longitude'], } d['city'] = g['city'] d['region_name'] = g['region_name'] for ext in self.extensions: ext(d) return d
def _get_params_from_stack(self, pos=-1): method, url = self.shell.cs.client.callstack[pos][0:2] path, query = parse.splitquery(url) params = parse.parse_qs(query) return path, params