def extract_response(self, components, js): ret = js for component in components: if component in ret: ret = ret[component] else: raise uhttpd.NotFoundException(components) return ret
def get_path(tree, path, all=False): for c in path: if c in tree: tree = tree[c] else: raise uhttpd.NotFoundException( "Invalid path: {}; '{}' not found.".format(path, c)) return Handler.serialize(tree) if not all else tree
def handle_request(self, http_request): # # We only support GET # verb = http_request['verb'] if verb != 'get': raise uhttpd.BadRequestException( "Unsupported HTTP verb: {}".format(verb)) # the relative path is the path on the HTTP request stripped of the # prefix used to register the file handler relative_path = uhttpd.get_relative_path(http_request) # the effective path is the relative path with the root path # prefixed, and normalized to remove '.' and '..' absolute_path = self.effective_path(relative_path) # # If the path is forbidden, 403 out # remote_addr = http_request['tcp']['remote_addr'] if not self.is_prefix(self._root_path, absolute_path): logging.info("FORBIDDEN {} {}".format(remote_addr, absolute_path)) raise uhttpd.ForbiddenException(absolute_path) # # If the path doesn't exist, 404 out # if not exists(absolute_path): logging.info("NOT_FOUND {} {}".format(remote_addr, absolute_path)) raise uhttpd.NotFoundException(absolute_path) # # Otherwise, generate a file listing or a file # if is_dir(absolute_path): index_path = absolute_path + "/index.html" if exists(index_path): response = self.create_file_response(index_path) logging.info("ACCESS {} {}".format(remote_addr, index_path)) return response else: logging.info("ACCESS {} {}".format(remote_addr, absolute_path)) prefix = http_request['prefix'] return self.create_dir_listing_response(absolute_path) else: logging.info("ACCESS {} {}".format(remote_addr, absolute_path)) return self.create_file_response(absolute_path)
def get(self, api_request): #print(api_request) context = api_request['context'] if len(context) > 0: what_to_return = context[0] if what_to_return == 'query_params': query_params = api_request['query_params'] return query_params elif what_to_return == "nothing": return None elif what_to_return == "empty": return b'' elif what_to_return == "something": return b'something' elif what_to_return == "bad_request_excetion": raise uhttpd.BadRequestException("derp") elif what_to_return == "not_found_excetion": raise uhttpd.NotFoundException("what you were looking for") elif what_to_return == "forbidden_excetion": raise uhttpd.ForbiddenException("tsktsk") return {'action': 'get'}
def get(self, api_request): #print(api_request) context = api_request['context'] if len(context) > 0: what_to_return = context[0] if what_to_return == 'query_params': query_params = api_request['query_params'] return query_params elif what_to_return == "nothing": return None elif what_to_return == "empty": return b'' elif what_to_return == "something": return b'something' elif what_to_return == "html": return "<html><body><h1>HTML</h1></body></html>" elif what_to_return == "json": return { 'some': [{ 'j': 1, 's': [], 'o': "str", 'n': { "дружище": "バディ" } }] } elif what_to_return == "int": return 1342 elif what_to_return == "float": return 3.14159 elif what_to_return == "bad_request_excetion": raise uhttpd.BadRequestException("derp") elif what_to_return == "not_found_excetion": raise uhttpd.NotFoundException("what you were looking for") elif what_to_return == "forbidden_excetion": raise uhttpd.ForbiddenException("tsktsk") else: return {'action': 'get'}
def handle_request(self, http_request): relative_path = uhttpd.get_relative_path(http_request) path_part, query_params = self.extract_query(relative_path) components = path_part.strip('/').split('/') prefix, handler, context = self.find_handler(components) if handler: json_body = None headers = http_request['headers'] if 'body' in http_request and 'content-type' in headers and headers[ 'content-type'] == "application/json": try: json_body = ujson.loads(http_request['body']) except Exception as e: raise uhttpd.BadRequestException( "Failed to load JSON: {}".format(e)) verb = http_request['verb'] api_request = { 'prefix': prefix, 'context': context, 'query_params': query_params, 'body': json_body, 'http': http_request } if verb == 'get': response = handler.get(api_request) elif verb == 'put': response = handler.put(api_request) elif verb == 'post': response = handler.post(api_request) elif verb == 'delete': response = handler.delete(api_request) else: # TODO add support for more verbs! error_message = "Unsupported verb: {}".format(verb) raise uhttpd.BadRequestException(error_message) else: error_message = "No handler found for components {}".format( components) raise uhttpd.NotFoundException(error_message) ## ## prepare response ## if response is not None: response_type = type(response) if response_type is dict or response_type is list: data = ujson.dumps(response).encode('UTF-8') body = lambda stream: stream.awrite(data) content_type = "application/json" content_length = len(data) elif response_type is bytes: data = response body = lambda stream: stream.awrite(data) content_type = "application/binary" content_length = len(data) elif response_type is str: data = response.encode("UTF-8") body = lambda stream: stream.awrite(data) content_type = "text/html; charset=utf-8" content_length = len(data) elif response_type is int or response_type is float: data = str(response) body = lambda stream: stream.awrite(data) content_type = "text/plain" content_length = len(data) else: data = str(response) body = lambda stream: stream.awrite(data) content_type = "text/plain" content_length = len(data) else: body = None content_type = None content_length = None headers = {} if content_length: headers.update({'content-length': content_length}) if content_type: headers.update({'content-type': content_type}) return {'code': 200, 'headers': headers, 'body': body}
def handle_request(self, http_request): relative_path = uhttpd.get_relative_path(http_request) path_part, query_params = self.extract_query(relative_path) components = path_part.strip('/').split('/') prefix, handler, context = self.find_handler(components) if handler: json_body = None headers = http_request['headers'] if 'body' in http_request and 'content-type' in headers and headers[ 'content-type'] == "application/json": try: json_body = ujson.loads(http_request['body']) except Exception as e: raise uhttpd.BadRequestException( "Failed to load JSON: {}".format(e)) verb = http_request['verb'] api_request = { 'prefix': prefix, 'context': context, 'query_params': query_params, 'body': json_body, 'http': http_request } if verb == 'get': response = handler.get(api_request) elif verb == 'put': response = handler.put(api_request) elif verb == 'post': response = handler.post(api_request) elif verb == 'delete': response = handler.delete(api_request) else: # TODO add support for more verbs! error_message = "Unsupported verb: {}".format(verb) raise uhttpd.BadRequestException(error_message) else: error_message = "No handler found for components {}".format( components) raise uhttpd.NotFoundException(error_message) if response is not None: if type(response) is dict: data = ujson.dumps(response).encode('UTF-8') content_type = "application/json" elif type(response) is bytes: data = response content_type = "application/binary" else: raise Exception( "Response from API Handler is neither dict nor bytearray nor None" ) body = lambda stream: stream.awrite(data) else: data = body = None ret = { 'code': 200, 'headers': { 'content-length': len(data) if data else 0 }, 'body': body } if data is not None: ret['headers']['content-type'] = content_type return ret