def raiseUnavailable(error): raise web.HTTPError('503 Service Unavailable: %s' % error)
def error(self, reason): d = json.dumps({"status": "error", "reason": reason}) return web.HTTPError("400 Bad Request", {"Content-Type": "application/json"}, d)
def GET(self): web.header('Content-Type', 'text/plain') raise web.HTTPError('200 OK', {}, 'OK')
def GET(self, name, arg2=None): if arg2: # Plugin script command. scriptDir = os.path.join(PLUGINDIR, name, 'scripts') command = arg2 else: scriptDir = SCRIPTDIR command = name allArgs = web.input() if '/' in command or '.' in command: raise myWebError("401 Cannot use / or . in command") if allArgs.has_key('args'): args = shlex.split(allArgs.args) else: args = [] # Setup global, per-plugin and per-user data for plugin scripts, if available env = copy.deepcopy(os.environ) initDatabaseAccess() try: # Tell plugin about our url, if we know it myUrl = DATABASE_ACCESS.get_key('services/igor/url', 'application/x-python-object', 'content') env['IGORSERVER_URL'] = myUrl except web.HTTPError: pass try: pluginData = DATABASE_ACCESS.get_key( 'plugindata/%s' % (name), 'application/x-python-object', 'content') except web.HTTPError: web.ctx.status = "200 OK" # Clear error, otherwise it is forwarded from this request pluginData = {} # Put all other arguments into the environment with an "igor_" prefix for k, v in allArgs.items(): if k == 'args': continue if not v: v = '' env['igor_' + k] = v # If there's a user argument see if we need to add per-user data if allArgs.has_key('user'): user = allArgs['user'] try: userData = DATABASE_ACCESS.get_key( 'identities/%s/plugindata/%s' % (user, name), 'application/x-python-object', 'content') except web.HTTPError: web.ctx.status = "200 OK" # Clear error, otherwise it is forwarded from this request userData = {} if userData: pluginData.update(userData) # Pass plugin data in environment, as JSON if pluginData: env['igor_pluginData'] = json.dumps(pluginData) if type(pluginData) == type({}): for k, v in pluginData.items(): env['igor_' + k] = str(v) # Check whether we need to use an interpreter on the command command = os.path.join(scriptDir, command) if os.path.exists(command): interpreter = None elif os.path.exists(command + '.py'): command = command + '.py' interpreter = "python" elif os.name == 'posix' and os.path.exists(command + '.sh'): command = command + '.sh' interpreter = 'sh' else: raise myWebError("401 command not found: %s" % command) if interpreter: args = [interpreter, command] + args else: # Could add windows and .bat here too, if needed args = [command] + args # Call the command and get the output try: rv = subprocess.check_output(args, stderr=subprocess.STDOUT, env=env) except subprocess.CalledProcessError, arg: msg = "502 Command %s exited with status code=%d" % ( command, arg.returncode) output = msg + '\n\n' + arg.output # Convenience for internal logging: if there is 1 line of output only we append it to the error message. argOutputLines = arg.output.split('\n') if len(argOutputLines) == 2 and argOutputLines[1] == '': msg += ': ' + argOutputLines[0] output = '' raise web.HTTPError(msg, {"Content-type": "text/plain"}, output)
def error(self, error_code, error='Invalid item', **kwargs): content = {'success': False, 'error_code': error_code, 'error': error} content.update(kwargs) raise web.HTTPError('400 Bad Request', data=json.dumps(content))
def lan(name=None, service='services/%s', ip=None, port=80, timeout=5): if not name: raise myWebError("401 Required argument name missing") if not ip: ip = name alive = True try: s = socket.create_connection((ip, int(port)), timeout) except socket.error: alive = False if service: if '%' in service: service = service % name if not DATABASE_ACCESS: msg = "502 plugin did not have DATABASE_ACCESS set" raise web.HTTPError(msg, {"Content-type": "text/plain"}, msg + '\n\n') try: oldValue = DATABASE_ACCESS.get_key(service + '/alive', 'text/plain', None) except web.HTTPError: web.ctx.status = "200 OK" oldValue = 'rabarber' xpAlive = 'true' if alive else '' if oldValue != xpAlive: try: rv = DATABASE_ACCESS.put_key(service + '/alive', 'text/plain', None, xpAlive, 'text/plain', replace=True) except web.HTTPError: raise myWebError("501 Failed to store into %s" % (service + '/alive')) if alive: # If the service is alive we delete any error message and we also reset the "ignore errors" indicator try: DATABASE_ACCESS.delete_key(service + '/errorMessage') except web.HTTPError: web.ctx.status = "200 OK" try: DATABASE_ACCESS.delete_key(service + '/ignoreErrorUntil') except web.HTTPError: web.ctx.status = "200 OK" else: # If the service is not alive we set an error message DATABASE_ACCESS.put_key(service + '/errorMessage', 'text/plain', None, "%s is not available" % name, 'text/plain', replace=True) if xpAlive == 'true': try: rv = DATABASE_ACCESS.put_key(service + '/lastActivity', 'text/plain', None, str(int(time.time())), 'text/plain', replace=True) except web.HTTPError: raise myWebError("501 Failed to store into %s" % (service + '/alive')) return repr(alive)
def GET(self, id): data = render.bookreader(id) raise web.HTTPError("200 OK", {}, data)
def data(self, earliesttime=None, latesttime=None, lastget=None, raise_exception=True): """ Return data for an individual station after earliesttime & before latesttime. """ ## TODO: Better latesttime & earliesttime checking. Limits? log.debug("starting station request...") if raise_exception: web.modified(self.lastupdate('obs')) sql = [] sql.append("""select vc.mes_datetime as data_time, s.sensor_id as sensor_id, v.value_num/s.scaling as data_value, s.symbol as data_symbol, s.data_number as data_number, st.vmdb_id as vmdb_id, x.xmltagname as xmltagname, x.xsitype as xsitype, di.datex_id as datex_id, st.station_name as station_name from icecast.value v, icecast.value_control vc, icecast.sensor s, icecast.application_symbol app_s, icecast.station st, exportws.xmltags x, exportws.sensorindex di where v.vc_id = vc.vc_id and v.value_status>= 0 and v.vc_id in""") variables = { 'stationid': self.row['station_id'], } if earliesttime or latesttime: sql.append("""( select vc_id from icecast.value_control where station_id = $stationid""") if earliesttime: log.debug("extracting from %s" % earliesttime) sql.append("and mes_datetime >= $earliesttime") variables['earliesttime'] = earliesttime if latesttime: log.debug("extracting since %s" % latesttime) sql.append("and mes_datetime <= $latesttime") variables['latesttime'] = latesttime sql.append(")") elif lastget: log.debug("extracting data since %s" % lastget) # We can't use > over >= in the sql as entry_datetime is stored # to sub-second accuracy in postgres. (mes_datetime isn't!!) # We'll just add a second to lastget instead. sql.append("""( select vc_id from icecast.value_control where station_id = $stationid and entry_datetime >= $nextget )""") variables['nextget'] = lastget + timedelta(seconds=1) else: sql.append("""( select max(vc_id) from icecast.value_control where station_id = $stationid )""") sql.append("""and v.sensor_id = s.sensor_id and s.symbol = app_s.symbol and s.symbol = x.data_symbol and s.symbol = di.data_symbol and s.data_number = di.data_number and app_s.application_key = 'ExportObservationXML' and vc.station_id = st.station_id order by vc.mes_datetime; """) result = db.query(' '.join(sql), vars=variables) if not result and raise_exception: # FIXME: Should this be here? web.header('Content-Type', 'text/html; charset=utf-8') raise web.HTTPError("404 not found", {}, "No data available.") return Data(result, { self.row['vmdb_id']: self, })
def authz(username, role=None, region=None, station=None): """ Authorize username against region as well as optional role & station. Returns Station or Region object to be queried for data. """ if station: try: station = int(station) except TypeError: log.info("station=%s message='invalid station'" % (station)) raise web.HTTPError("404 not found", {}, "'%s' is not a valid station." \ % station) if check_admin(username): if station: log.info("user=%s admin=True authz=True finalauthz=True role=%s station=%s" \ % (username, role, station)) return Station(vmdb_id=station) elif region: log.info("user=%s admin=True authz=True finalauthz=True role=%s region=%s" \ % (username, role, region)) return Region(vmdb_id=region) else: log.info("user=%s admin=True authz=True finalauthz=True role=%s message='Admin is being a fool'" \ % (username, role)) raise web.HTTPError("404 not found", {}, "Fool of a took!") possible_regions = [] if region: log.debug("possible_regions=%s" % region) possible_regions.append(region) else: regions = list_users_roles(username) for row in regions: if row.region not in possible_regions: possible_regions.append(row.region) log.debug("possible_regions=%s" % possible_regions) for regiontotest in possible_regions: testregion = Region(vmdb_id=regiontotest) if check_region_role( username, testregion.row['vmdb_id'], role, ): if station: log.debug("region=%s testregion_stations=%s" % (regiontotest, testregion.stations().keys())) if station in testregion.stations(): log.info("username=%s authz=True finalauthz=True role=%s region=%s station=%s" \ % (username, role, regiontotest, str(station))) return Station(vmdb_id=station) else: log.debug("username=%s authz=False finalauthz=False role=%s region=%s station=%s" \ % (username, role, regiontotest, str(station))) else: log.info("username=%s authz=True finalauthz=True role=%s region=%s" \ % (username, role, regiontotest)) return testregion else: log.debug("username=%s authz=False finalauthz=False role=%s region=%s" \ % (username, role, regiontotest)) else: # no regions in the list, we've failed to authorize. log.info("username=%s authz=False finalauthz=True region=%s station=%s" \ % (username, str(region), str(station))) web.header('Content-Type', 'text/html; charset=utf-8') raise web.HTTPError("401 unauthorized", {}, "You are not authorized.")
def GET(self): params = web.input(sensor_id=None, after=None, before=None, max_size='4096', rich='false', max_time=None) if params.sensor_id is None: raise web.HTTPError('400 Bad Request: sensor id required') if params.after is None or '' == params.after: raise web.HTTPError('400 Bad Request: need start time') start_time = int(params.after) max_size = int(params.max_size) max_time = 60 * 60 * 4 if params.max_time is not None and '' != params.max_time: max_time = int(params.max_time) end_time = None if params.before is not None and '' != params.before: end_time = int(params.before) rich = True if params.rich == 'true' else False if 0 != start_time: effective_end_time = int(time.time()) if end_time is not None: effective_end_time = end_time if max_time < (effective_end_time - start_time): raise web.HTTPError( '400 Bad Request: maximum time lapse: %d - %d > %d' % (effective_end_time, start_time, max_time)) if 0 == start_time: start_time = int(time.time()) - 5 req = { 'id': params.sensor_id, 'is_include_content': True, 'after': start_time } if not rich: req['max_size'] = max_size if end_time is not None: req['before'] = end_time info = model.request('get_timeline', req) if not info.isSuccess: return render.error(str(info)) if 0 == int(params.after): info.data['new_start'] = start_time if rich: originalEvents = info.data.get('events', []) info.data['events'] = [] for event in originalEvents: richEvent = None if hasattr(eventRender, event[1]): try: richEvent = str( getattr(eventRender, event[1])(sanitizeJson(event[3]))) except: richEvent = None if richEvent is None: richEvent = str( eventRender.default( sanitizeJson(event[3], summarized=1024))) info.data['events'].append( (event[0], event[1], event[2], richEvent)) return info.data
def GET(self, request): web.header('Content-Type', 'text/plain') content = 'CirruxCache (%s) / http://code.google.com/p/cirruxcache/\n' % __version__ if request: raise web.HTTPError(status='404 Not Found', data=content) return content
def auth_failed(self, reason): d = json.dumps({ "status" : "error", "reason" : reason}) return web.HTTPError("401 Authorization Required", {"WWW-Authenticate": 'Basic realm="http://openlibrary.org"', "Content-type": "application/json"}, d)
def PUT(self, deviceId): """ Create, or update notification settings for device. :: PUT /_notifications/{deviceId}/settings { "email_addr": "*****@*****.**", "windowsize": 3600, "sensitivity": 0.99999 } :param email_addr: Target email address associated with device :type email_addr: string :param windowsize: Notification window in seconds during which no other notifications for a given instance should be sent to a given device :type windowsize: int :param sensitivity: Anomaly score threshold that should trigger a notification :type sensitivity: float """ data = web.data() if data: data = utils.jsonDecode(data) if isinstance(data, basestring) else data try: with web.ctx.connFactory() as conn: settingsRow = repository.getDeviceNotificationSettings( conn, deviceId) settingsDict = dict([(col.name, settingsRow[col.name]) for col in schema.notification_settings.c ]) except ObjectNotFoundError: settingsDict = None if settingsDict: # Update existing changes = dict() if "windowsize" in data: changes["windowsize"] = data["windowsize"] if "sensitivity" in data: changes["sensitivity"] = data["sensitivity"] if "email_addr" in data: changes["email_addr"] = data["email_addr"] if changes: log.info( "Notification settings updated for email=%s, " "deviceid=%s, %r", anonymizeEmail(settingsDict["email_addr"]), deviceId, changes.keys()) with web.ctx.connFactory() as conn: repository.updateDeviceNotificationSettings( conn, deviceId, changes) self.addStandardHeaders() for (header, value) in web.ctx.headers: if header == "Content-Type": web.ctx.headers.remove((header, value)) raise web.HTTPError(status="204 No Content") else: # Create new settings if "windowsize" in data: windowsize = data["windowsize"] else: windowsize = 60 * 60 # TODO: Configurable default if "sensitivity" in data: sensitivity = data["sensitivity"] else: sensitivity = 0.99999 # TODO: Configurable default if "email_addr" in data: email_addr = data["email_addr"] else: email_addr = None with web.ctx.connFactory() as conn: repository.addDeviceNotificationSettings( conn, deviceId, windowsize, sensitivity, email_addr) log.info("Notification settings created for deviceid=%s", deviceId) self.addStandardHeaders() raise web.created("") else: # Metric data is missing log.error( "Data is missing in request, raising BadRequest exception") raise web.badrequest("Metric data is missing")
def POST(self): web.header('Content-Type', 'application/json') if not can_write(): raise web.HTTPError('403 Forbidden') i = web.input() require_marc = not (i.get('require_marc') == 'false') bulk_marc = i.get('bulk_marc') == 'true' if 'identifier' not in i: return self.error('bad-input', 'identifier not provided') identifier = i.identifier # First check whether this is a non-book, bulk-marc item if bulk_marc: # Get binary MARC by identifier = ocaid/filename:offset:length re_bulk_identifier = re.compile(r"([^/]*)/([^:]*):(\d*):(\d*)") try: ocaid, filename, offset, length = re_bulk_identifier.match(identifier).groups() data, next_offset, next_length = get_from_archive_bulk(identifier) next_data = {'next_record_offset': next_offset, 'next_record_length': next_length} rec = MarcBinary(data) edition = read_edition(rec) except MarcException as e: details = "%s: %s" % (identifier, str(e)) logger.error("failed to read from bulk MARC record %s", details) return self.error('invalid-marc-record', details, **next_data) actual_length = int(rec.leader()[:MARC_LENGTH_POS]) edition['source_records'] = 'marc:%s/%s:%s:%d' % (ocaid, filename, offset, actual_length) local_id = i.get('local_id') if local_id: local_id_type = web.ctx.site.get('/local_ids/' + local_id) prefix = local_id_type.urn_prefix id_field, id_subfield = local_id_type.id_location.split('$') def get_subfield(field, id_subfield): if isinstance(field, str): return field subfields = field[1].get_subfield_values(id_subfield) return subfields[0] if subfields else None _ids = [get_subfield(f, id_subfield) for f in rec.read_fields([id_field]) if f and get_subfield(f, id_subfield)] edition['local_id'] = ['urn:%s:%s' % (prefix, _id) for _id in _ids] # Don't add the book if the MARC record is a non-book item try: raise_non_book_marc(rec, **next_data) except BookImportError as e: return self.error(e.error_code, e.error, **e.kwargs) result = add_book.load(edition) # Add next_data to the response as location of next record: result.update(next_data) return json.dumps(result) try: return self.ia_import(identifier, require_marc=require_marc) except BookImportError as e: return self.error(e.error_code, e.error, **e.kwargs)
rawWebInput = web.input() if isinstance(rawWebInput, dict): webInput = DustWeb.simplifyWebInputFormat(rawWebInput) elif isinstance(rawWebInput, list): webInput = [] for e in rawWebInput: webInput.append(DustWeb.simplifyWebInputFormat(e)) else: webInput = rawWebInput if self._isDataUrl(): try: return self.postData(webInput, subResource, username) except UnauthorizedException: raise web.HTTPError('401 unauthorized', {}, 'not autorized') except web.seeother: raise except web.HTTPError: raise except ValueError as err: log.error( 'error processing user request.\nerror:{0}\ntraceback:{1}'. format( err, traceback.format_exc(), )) raise web.HTTPError('400 Bad Request', {}, str(err)) except Exception as err: # log log.error(
def GET(self, *k): try: web.header('Access-Control-Allow-Origin', '*') s = settings() s.ReadSettings() if len(k) == 0: web.HTTPError('400 Bad Request') return '400 Bad Request' args = k[0].split('/') if s.pixivRSSSecrets and not verifySign(s.pixivRSSSecrets, args): web.HTTPError('401 Unauthorized') return '401 Unauthorized' m = {} for i in args: d = unquote_plus(i).split('=', 1) if len(d) == 1: continue m[d[0]] = d[1] id = m.get('id', '') typ = m.get('t', '') if typ == '': m.get('type', '') if typ == '': typ = 'url' if id == '': web.HTTPError('400 Bad Request') return '400 Bad Request: id is needed.' db = PixivDb() p = PixivAPI(s, db) lang = m.get('lang', None) lld = {} if lang is not None: lld['lang'] = lang ld = f"/illust/{id}?{urlencode(lld)}" d = db.get_cache(ld, s.pixivSinglePageCacheTime) if d is None: d = p.getIllustDetails(id, lang) lt = db.save_cache(ld, d) else: lt = d[1] d = d[0] sendCacheInfo(60 * s.pixivSinglePageCacheTime, lt) if typ == 'json': web.header("Content-Type", "application/json; charset=utf-8") return dumps(d, ensure_ascii=False, separators=jsonsep) page = m.get('p', '') if page == '': page = m.get('page', '1') page = min(max(int(page), 1), d['page_count']) add_fn = m.get('f', '') if add_fn == '': add_fn = m.get('add_fn', '') add_fn = parseBool(add_fn, True) if typ == 'url': if d['page_count'] == 1: url = getUrl(d, m.get('size', 'original')) else: url = getUrl(d['meta_pages'][page - 1], m.get('size', 'original')) # noqa: E501 if url is None: web.HTTPError('404 Not Found') return '404 Failed to extract url.' web.found(genUrl(url, s.RSSProxySerects, add_fn)) return True except Exception: web.HTTPError('500 Internal Server Error') web.header("Content-Type", "text/plain; charset=UTF-8") try: s = settings() s.ReadSettings() if s.debug: return format_exc() except: pass return ''
def GET(self, subResource=''): ''' print 'GET {0} {1} (subR={2})'.format(web.ctx.session.username, web.ctx.path, subResource) ''' subResource = WebPage.WebPage.urlStringTolist(subResource) username = str(web.ctx.session.username) if not self._isDataUrl(): try: return self.getPage(subResource, username) except UnauthorizedException: raise web.HTTPError('401 unauthorized', {}, 'not autorized') except web.seeother: raise except web.HTTPError: raise except Exception as err: # log log.error( 'error processing user request.\nerror:{0}\ntraceback:{1}'. format( err, traceback.format_exc(), )) # return HTTP error raise web.HTTPError( '500 internal error', {}, traceback.format_exc(), ) else: try: dataToAnswer = self.getData(subResource, username) if isinstance(dataToAnswer, (dict, list)): web.header('Content-Type', 'text/json') return json.dumps(dataToAnswer) elif isinstance(dataToAnswer, tuple) and len(dataToAnswer) == 2: data = dataToAnswer[0] handlingInstruction = dataToAnswer[1] if handlingInstruction == self.ALREADY_JSONIFIED: return data else: raise SystemError( 'unexpected handlingInstruction={0}'.format( handlingInstruction)) else: raise SystemError("Unexpected dataToAnswer={0} {1}".format( dataToAnswer, type(dataToAnswer))) except UnauthorizedException: raise web.HTTPError('401 unauthorized', {}, 'not autorized') except web.HTTPError: raise except Exception as err: # log log.error( 'error processing user request.\nerror:{0}\ntraceback:{1}'. format( err, traceback.format_exc(), )) # return HTTP error raise web.HTTPError( '500 internal error', {}, traceback.format_exc(), )
def GET(self): try: web.header("Content-Type", "application/json; charset=utf-8") s = settings() s.ReadSettings() bagel = web.input().get("bagel") bagel = False if bagel is None else True nod = web.input().get("noda") nod = False if nod is None else True ol = 0 if not nod: r = get(s.clearUrlOrigin) r = r.json() ol = len(r['providers']) else: r = {"providers": {}} li = s.clearUrlCustomList h = web.input().get("hash") h = False if h is None else True if li is None: t = dumps(r, separators=jsonsep) return sha256(t) if h else t ind: str = web.input().get("t") ind = ind.split(',') if ind is not None and ind != '' else None vt = [] for i in li: if ind is not None and i not in ind: continue elif ind is not None: vt.append(i) with open(li[i], 'r', encoding='utf8') as f: j = loadJson(f) if 'providers' in j: for m in j['providers']: ke = m te = ke z = 0 re = search(r'^(.+)_(\d+)$', m) if re is not None: re = re.groups() te = re[0] while ke in r['providers']: z += 1 ke = f"{te}_{z}" r['providers'][ke] = j['providers'][m] t = dumps(r, separators=jsonsep) if h: return sha256(t) elif bagel: lt = str(len(r["providers"].keys())) adm = [] if int(lt) == ol: adm.append('no any custom rules') if int(lt) != ol and ind is not None: adm.append('types: ' + '; '.join(vt)) if not nod: adm.append(f'include {ol} offical rules') at = ', '.join(adm) at = '' if at == '' else f" ({at})" rt = f'custom rules{at}' from drawBagel import drawBagel svg = drawBagel(lt, rt, leftColor='#555', rightColor='#007ec6', spacing=3) web.header('Content-Type', 'image/svg+xml') web.header('Cache-Control', 'public, max-age=300') return svg else: return t except: web.HTTPError('500 Internal Server Error') try: s = settings() s.ReadSettings() if s.debug: return format_exc() except: pass return ''
def POST(self): user = web.input('user')['user'] desc = web.input(desc=None)['desc'] num = int(web.input('num')['num']) machinetype = dict(machinetype=(web.input( machinetype='plana')['machinetype'])) if num < 1: raise web.BadRequest() tries = 0 check_existing = True while True: try: # transaction will be rolled back if an exception is raised with DB.transaction(): if desc is not None and check_existing: # if a description is provided, treat it as a # key for locking in case the same run locked # machines in the db successfully before, but # the web server reported failure to it # because the request took too long. Only try # this once per request. check_existing = False results = list( DB.select( 'machine', machinetype, desc, user, what='name, sshpubkey', where= 'locked = true AND up = true AND type = $machinetype AND description = $desc AND locked_by = $user', limit=num)) if len(results) == num: name_keys = {} for row in results: name_keys[row.name] = row.sshpubkey print 'reusing machines', name_keys.keys() break results = list( DB.select( 'machine', machinetype, what='name, sshpubkey, type', where= 'locked = false AND up = true AND type = $machinetype', limit=num)) if len(results) < num: raise web.HTTPError(status='503 Service Unavailable') name_keys = {} for row in results: if row.type == 'vps': curkey = row.sshpubkey else: curkey, getstatus = get_sshkey(row.name) if getstatus != 0: curkey = row.sshpubkey if row.sshpubkey != curkey: newkey = curkey update_sshkey(row.name, curkey, row.type) else: newkey = row.sshpubkey name_keys[row.name] = newkey where_cond = web.db.sqlors('name = ', name_keys.keys()) \ + ' AND locked = false AND up = true' num_locked = DB.update( 'machine', where=where_cond, locked=True, locked_by=user, description=desc, locked_since=web.db.SQLLiteral('NOW()')) assert num_locked == num, 'Failed to lock machines' except Exception: log.exception("Saw exception") tries += 1 if tries < 10: continue raise else: break print user, 'locked', name_keys.keys(), 'desc', desc web.header('Content-type', 'text/json') return json.dumps(name_keys)
def GET(self): web.HTTPError('301', {'Location': 'static/'})
def do_get(self): try: if os.path.isdir(self.filename): #If necessary, redirect to add trailing slash if not self.filename.endswith('/'): self.response.headers.set('Location', self.request.resource + '/') return 307, '' #Check for index file index = self.filename + 'index.html' if os.path.exists(index) and os.path.isfile(index): indexfile = open(index, 'rb') self.response.headers.set('Content-Type', 'text/html') self.response.headers.set('Content-Length', str(os.path.getsize(index))) return 200, indexfile elif self.dir_index: #If no index and directory indexing enabled, send a generated one return 200, self.index() else: raise web.HTTPError(403) else: file = open(self.filename, 'rb') #Get file size from metadata size = os.path.getsize(self.filename) length = size #HTTP status that changes if partial data is sent status = 200 #Handle range header and modify file pointer and content length as necessary range_header = self.request.headers.get('Range') if range_header: range_match = re.match('bytes=(\d+)-(\d+)?', range_header) if range_match: groups = range_match.groups() #Get lower and upper bounds lower = int(groups[0]) if groups[1]: upper = int(groups[1]) else: upper = size - 1 #Sanity checks if upper < size and upper >= lower: file.seek(lower) self.response.headers.set('Content-Range', 'bytes ' + str(lower) + '-' + str(upper) + '/' + str(size)) length = upper - lower + 1 status = 206 self.response.headers.set('Content-Length', str(length)) #Tell client we allow selecting ranges of bytes self.response.headers.set('Accept-Ranges', 'bytes') #Guess MIME by extension mime = mimetypes.guess_type(self.filename)[0] if mime: self.response.headers.set('Content-Type', mime) return status, file except FileNotFoundError: raise web.HTTPError(404) except NotADirectoryError: raise web.HTTPError(404) except IOError: raise web.HTTPError(403)
def _impl(request): request += web.ctx.query web.header('Location', this.origin + request) status = '%s %s' % (this.code, http.httpResponses[this.code]) raise web.HTTPError(status=status)
def myWebError(msg): return web.HTTPError(msg, {"Content-type": "text/plain"}, msg + '\n\n')
def compare(file, signature): if HMAC(key, file) == signature: return 'Everything looks good to me!!' else: return web.HTTPError('500')
def POST(self): web.header('Content-Type', 'application/json') if not can_write(): raise web.HTTPError('403 Forbidden') i = web.input() require_marc = not (i.get('require_marc') == 'false') bulk_marc = i.get('bulk_marc') == 'true' if 'identifier' not in i: return self.error('bad-input', 'identifier not provided') identifier = i.identifier # First check whether this is a non-book, bulk-marc item if bulk_marc: # Get binary MARC by identifier = ocaid/filename:offset:length re_bulk_identifier = re.compile("([^/]*)/([^:]*):(\d*):(\d*)") try: ocaid, filename, offset, length = re_bulk_identifier.match( identifier).groups() data, next_offset, next_length = get_from_archive_bulk( identifier) next_data = { 'next_record_offset': next_offset, 'next_record_length': next_length } rec = MarcBinary(data) edition = read_edition(rec) except MarcException as e: details = "%s: %s" % (identifier, str(e)) logger.error("failed to read from bulk MARC record %s", details) return self.error('invalid-marc-record', details, **next_data) actual_length = int(rec.leader()[:MARC_LENGTH_POS]) edition['source_records'] = 'marc:%s/%s:%s:%d' % ( ocaid, filename, offset, actual_length) local_id = i.get('local_id') if local_id: local_id_type = web.ctx.site.get('/local_ids/' + local_id) prefix = local_id_type.urn_prefix id_field, id_subfield = local_id_type.id_location.split('$') def get_subfield(field, id_subfield): if isinstance(field, str): return field subfields = field[1].get_subfield_values(id_subfield) return subfields[0] if subfields else None _ids = [ get_subfield(f, id_subfield) for f in rec.read_fields([id_field]) if f and get_subfield(f, id_subfield) ] edition['local_id'] = [ 'urn:%s:%s' % (prefix, _id) for _id in _ids ] # Don't add the book if the MARC record is a non-book item self.reject_non_book_marc(rec, **next_data) result = add_book.load(edition) # Add next_data to the response as location of next record: result.update(next_data) return json.dumps(result) # Case 1 - Is this a valid Archive.org item? metadata = ia.get_metadata(identifier) if not metadata: return self.error('invalid-ia-identifier', '%s not found' % identifier) # Case 2 - Does the item have an openlibrary field specified? # The scan operators search OL before loading the book and add the # OL key if a match is found. We can trust them and attach the item # to that edition. if metadata.get('mediatype') == 'texts' and metadata.get( 'openlibrary'): edition_data = self.get_ia_record(metadata) edition_data['openlibrary'] = metadata['openlibrary'] edition_data = self.populate_edition_data(edition_data, identifier) return self.load_book(edition_data) # Case 3 - Can the item be loaded into Open Library? status = ia.get_item_status(identifier, metadata) if status != 'ok': return self.error(status, 'Prohibited Item %s' % identifier) # Case 4 - Does this item have a marc record? marc_record = get_marc_record_from_ia(identifier) if marc_record: self.reject_non_book_marc(marc_record) try: edition_data = read_edition(marc_record) except MarcException as e: logger.error('failed to read from MARC record %s: %s', identifier, str(e)) return self.error('invalid-marc-record') elif require_marc: return self.error('no-marc-record') else: try: edition_data = self.get_ia_record(metadata) except KeyError: return self.error("invalid-ia-metadata") # Add IA specific fields: ocaid, source_records, and cover edition_data = self.populate_edition_data(edition_data, identifier) return self.load_book(edition_data)
def GET(self): try: s = settings() s.ReadSettings() if s.cfwProfileSecrets and not verifySign(s.cfwProfileSecrets): web.HTTPError('401 Unauthorized') return '' origin = web.input().get("o") if origin is None or origin == '': origin = web.input().get("origin") if origin is None or origin == '': web.HTTPError('400 Bad Request') return '' headers = {"User-Agent": "ClashforWindows/0.13.8"} if 'HTTP_USER_AGENT' in web.ctx.env: ua: str = web.ctx.env['HTTP_USER_AGENT'] if ua.lower().startswith("clash"): headers.update({'User-Agent': ua}) r = get(origin, headers=headers) if r.status_code >= 400: web.HTTPError('400 Bad Request') return f'status = {r.status_code}\n{r.text}' ori = readFile(r.text) if isinstance(ori, str): web.HTTPError('200 Not Supported Type') return ori pro = web.input().get("p") if pro is None or pro == '': pro = web.input().get("profile") if pro is None or pro == '': return r.text prod = readFile(pro, True) d = {} default_proxy = web.input().get("dp") if default_proxy is None or default_proxy == '': default_proxy = web.input().get("default_proxy") if default_proxy is not None and default_proxy != '': d['default_proxy'] = default_proxy remove_rule_providers = web.input().get("rrp") if remove_rule_providers is None: remove_rule_providers = web.input().get( "remove_rule_providers") # noqa: E501 if remove_rule_providers is not None: d['remove_rule_providers'] = True force_enable_udp = web.input().get("feu") if force_enable_udp is None: force_enable_udp = web.input().get("force_enable_udp") if force_enable_udp is not None: d['force_enable_udp'] = True direct_proxy = web.input().get("direct") if direct_proxy is None or direct_proxy == '': direct_proxy = web.input().get("direct_proxy") if direct_proxy is not None and direct_proxy != '': d['direct_proxy'] = direct_proxy cfws = CfwFileSettings(**d) if cfws.remove_rule_providers: removeRuleProviders(ori, headers) removeRuleProviders(prod, headers) addProfileToTarget(prod, ori, cfws) t = dump(ori, Dumper=CSafeDumper, allow_unicode=True) web.header('Content-Type', 'text/yaml; charset=utf-8') return t except: web.HTTPError('500 Internal Server Error') try: s = settings() s.ReadSettings() if s.debug: return format_exc() except: pass return ''
def forbidden(self): headers = {"Content-Type": self.get_content_type()} data = {"message": "Permission denied."} return web.HTTPError("403 Forbidden", data=self.dumps(data), headers=headers)
def require(self, data, key, message): value = data.get(key, None) if value is None: raise web.HTTPError("400 Bad request", {"content-type": "text/plain"}, message)
def GET(self): params = web.input() username = params.username password = params.password domain = params.ddns if domain is None: raise web.HTTPError("422 Unprocessable Entity", {'content-type': 'text/html'}, "Error: ddns not in right form") user = validate_user(username, password) if user is None: if user is None: raise web.HTTPError("401 Unauthorized", {'content-type': 'text/html'}, "Error: login") userid = user.ID results = db.query( 'SELECT * FROM DDNS WHERE domain=$domain AND user_id=$user_id', vars={ 'domain': domain, 'user_id': userid }) try: for row in results: lastip = row['last_ipaddr'] if lastip: is_ipv4 = web.net.validipaddr(lastip) is_ipv6 = web.net.validip6addr(lastip) if is_ipv4: result = run_ipt_cmd(lastip, 'D') web.debug('iptables_update: %s' % [result]) if is_ipv6: result = run_ipt6_cmd(lastip, 'D') web.debug('ip6tables_update: %s' % [result]) except Exception as e: web.debug(traceback.print_exc()) raise web.HTTPError("400 Bad Request", {'content-type': 'text/html'}, traceback.print_exc()) results = db.query( 'SELECT * FROM DDNS WHERE domain=$domain AND user_id=$user_id', vars={ 'domain': domain, 'user_id': userid }) domains = [dom['domain'] for dom in results] if domain not in domains: return 'Error: DDNS not found.' try: db_result = db.delete('DDNS', where="user_id=%s AND domain='%s'" % (userid, domain)) web.debug('db_update: %s' % [db_result]) return 'OK' except Exception as e: web.debug(traceback.print_exc()) raise web.HTTPError("400 Bad Request", {'content-type': 'text/html'}, traceback.print_exc())
def raiseBadRequest(error): raise web.HTTPError('400 Bad Request: %s' % error)