diff options
Diffstat (limited to 'pyload')
| -rw-r--r-- | pyload/Core.py | 4 | ||||
| -rw-r--r-- | pyload/remote/json_converter.py | 12 | ||||
| -rw-r--r-- | pyload/web/api_app.py | 19 | 
3 files changed, 23 insertions, 12 deletions
| diff --git a/pyload/Core.py b/pyload/Core.py index 15b036c7a..2d591a2bb 100644 --- a/pyload/Core.py +++ b/pyload/Core.py @@ -261,7 +261,7 @@ class Core(object):      def cleanTree(self): -        for path, dirs, files in walk(self.path("")): +        for path, dirs, files in walk(self.path("pyload")):              for f in files:                  if not f.endswith(".pyo") and not f.endswith(".pyc"):                      continue @@ -664,4 +664,4 @@ def main():  if __name__ == "__main__": -    print "This file can not be started directly."
\ No newline at end of file +    print "This file can not be started directly." diff --git a/pyload/remote/json_converter.py b/pyload/remote/json_converter.py index df397e8ba..a7a0645ce 100644 --- a/pyload/remote/json_converter.py +++ b/pyload/remote/json_converter.py @@ -6,7 +6,6 @@ try:  except ImportError:      import json -  import apitypes  from apitypes import BaseObject  from apitypes import ExceptionObject @@ -16,10 +15,9 @@ separators = (',', ':')  # json encoder that accepts api objects  class BaseEncoder(json.JSONEncoder): -      def default(self, o):          if isinstance(o, BaseObject) or isinstance(o, ExceptionObject): -            ret = {"@class" : o.__class__.__name__} +            ret = {"@class": o.__class__.__name__}              for att in o.__slots__:                  ret[att] = getattr(o, att)              return ret @@ -28,26 +26,28 @@ class BaseEncoder(json.JSONEncoder):  # more compact representation, only clients with information of the classes can handle it  class BaseEncoderCompact(json.JSONEncoder): -      def default(self, o):          if isinstance(o, BaseObject) or isinstance(o, ExceptionObject): -            ret = {"@compact" : [o.__class__.__name__]} +            ret = {"@compact": [o.__class__.__name__]}              ret["@compact"].extend(getattr(o, attr) for attr in o.__slots__)              return ret          return json.JSONEncoder.default(self, o) +  def convert_obj(dct):      if '@class' in dct:          cls = getattr(apitypes, dct['@class'])          del dct['@class'] -        return cls(**dct) +        # convert keywords to str, <=2.6 does not accept unicode +        return cls(**dict((str(x) if type(x) == unicode else x, y) for x, y in dct.iteritems()))      elif '@compact' in dct:          cls = getattr(apitypes, dct['@compact'][0])          return cls(*dct['@compact'][1:])      return dct +  def dumps(*args, **kwargs):      if 'compact' in kwargs and kwargs['compact']:          kwargs['cls'] = BaseEncoderCompact diff --git a/pyload/web/api_app.py b/pyload/web/api_app.py index 66cdd58fd..af5c3074c 100644 --- a/pyload/web/api_app.py +++ b/pyload/web/api_app.py @@ -20,6 +20,10 @@ def add_header(r):      r.headers.append("Access-Control-Allow-Origin", request.get_header('Origin', '*'))      r.headers.append("Access-Control-Allow-Credentials", "true") +# returns http error +def error(code, msg): +    return HTTPError(code, dumps(msg), **dict(response.headers)) +  # accepting positional arguments, as well as kwargs via post and get  # only forbidden path symbol are "?", which is used to separate GET data and #  @route("/api/<func><args:re:[^#?]*>") @@ -67,8 +71,15 @@ def call_api(func, args=""):      # convert arguments from json to obj separately      for x, y in request.params.iteritems(): -        if not x or not y or x == "session": continue -        kwargs[x] = loads(unquote(y)) +        try: +            if not x or not y or x == "session": continue +            kwargs[x] = loads(unquote(y)) +        except Exception, e: +            # Unsupported input +            msg = "Invalid Input %s, %s : %s" % (x, y, e.message) +            print_exc() +            print msg +            return error(415, msg)      try:          result = getattr(api, func)(*args, **kwargs) @@ -77,10 +88,10 @@ def call_api(func, args=""):          return dumps(result)      except ExceptionObject, e: -        return HTTPError(400, dumps(e), **response.headers) +        return error(400, e.message)      except Exception, e:          print_exc() -        return HTTPError(500, dumps({"error": e.message, "traceback": format_exc()}), **response.headers) +        return error(500, {"error": e.message, "traceback": format_exc()})  @route("/api/login") | 
