예제 #1
0
def login():
    add_json_header(response)

    username = request.params.get('username')
    password = request.params.get('password')

    user = API.check_auth(username, password,
                          request.environ.get('REMOTE_ADDR', None))

    if not user:
        return json_response(False)

    s = set_session(request, user)

    # get the session id by dirty way, documentations seems wrong
    try:
        sid = s._headers['cookie_out'].split("=")[1].split(";")[0]
    # reuse old session id
    except Exception:
        sid = request.get_header(session.options['key'])

    result = BaseEncoder().default(user)
    result['session'] = sid

    # Return full user information if needed
    if request.params.get('user'):
        return dumps(result)

    return json_response(sid)
예제 #2
0
def json_response(obj):
    accept = 'gzip' in request.headers.get('Accept-Encoding', '')
    result = dumps(obj)
    # do not compress small string
    if not accept or len(result) <= 500:
        return result
    response.headers['Vary'] = 'Accept-Encoding'
    response.headers['Content-Encoding'] = 'gzip'
    zbuf = io.StringIO()
    try:
        with closing(gzip.GzipFile(mode='wb', compresslevel=6, fileobj=zbuf)) as zfile:
            zfile.write(result)
    except NameError:
        pass
    return zbuf.getvalue()
예제 #3
0
def error(code, msg):
    return HTTPError(code, dumps(msg), **dict(response.headers))