Exemplo n.º 1
0
    def __init__(self, envrionment, *args, **kwargs):
        werkzeug_request = werkzeug.wrappers.BaseRequest(envrionment)
        header_map = {}
        for (key, value) in werkzeug_request.headers:
            header_map[key.upper().replace('_', '-')] = value

        # script_root should be what ever path was consumed by the script handler configuration
        # ie: the  "/api" of: WSGIScriptAlias /api <path to wsgi script>
        uri = werkzeug_request.script_root + werkzeug_request.path

        super().__init__(verb=werkzeug_request.method.upper(),
                         uri=uri,
                         header_map=header_map,
                         cookie_map=werkzeug_request.cookies,
                         *args,
                         **kwargs)

        content_type = self.header_map.get('CONTENT-TYPE', None)
        if content_type is not None:  # if it is none, there isn't (or shoudn't) be anthing to bring in anyway
            if content_type.startswith('application/json'):
                self.fromJSON(
                    str(werkzeug_request.stream.read(164160), 'utf-8')
                )  # hopfully the request isn't larger than 160k, if so, we may need to rethink things

            elif content_type.startswith('text/plain'):
                self.fromText(
                    str(werkzeug_request.stream.read(164160), 'utf-8'))

            elif content_type.startswith('application/xml'):
                self.fromXML(str(werkzeug_request.stream.read(164160),
                                 'utf-8'))

            elif content_type.startswith('application/octet-stream'):
                self.stream = werkzeug_request.stream
                if 'CONTENT-DISPOSITION' in header_map:  # cheet a little, Content-Disposition isn't pure CInP, but this is a bolt on file uploader
                    self.header_map['CONTENT-DISPOSITION'] = header_map[
                        'CONTENT-DISPOSITION']
                pass  # do nothing, down stream is going to have to read from the stream

            elif content_type.startswith('application/x-www-form-urlencoded'):
                self.fromURLEncodedForm(
                    str(werkzeug_request.stream.read(164160), 'utf-8')
                )  # TODO: re-evulate to see if all these need to be converted to string first, newer versions of python might handle them right

            else:
                raise InvalidRequest(
                    message='Unknown Content-Type "{0}"'.format(content_type))

        if 'X-FORWARDED-FOR' in header_map:  # hm... should we really be doing this here?
            self.remote_addr = header_map['X-FORWARDED-FOR']
        else:
            self.remote_addr = werkzeug_request.remote_addr

        self.is_secure = werkzeug_request.is_secure

        werkzeug_request.close()
Exemplo n.º 2
0
    def login(username, password):
        user = auth.authenticate(username=username, password=password)
        if user is None:
            raise InvalidRequest('Invalid Login')

        request = Request(session=session_engine.SessionStore(None), user=user)

        auth.login(request, user)
        request.session.save()

        return request.session.session_key
Exemplo n.º 3
0
    def delete(self, model, object_id):
        try:
            target_object = model._django_model.objects.get(pk=object_id)
        except ObjectDoesNotExist:
            return False

        try:
            target_object.delete()
        except ProtectedError:
            raise InvalidRequest('Not Deletable')

        return True
Exemplo n.º 4
0
def upload_handler(request):  # TODO: also support multi-part
    if request.verb == 'OPTIONS':
        header_map = {}
        header_map['Allow'] = 'OPTIONS, POST'
        header_map['Cache-Control'] = 'max-age=0'
        header_map['Access-Control-Allow-Methods'] = header_map['Allow']
        header_map[
            'Access-Control-Allow-Headers'] = 'Accept, Content-Type, Content-Disposition'

        return Response(200, data=None, header_map=header_map)

    if request.verb != 'POST':
        return Response(400,
                        data='Invalid Verb (HTTP Method)',
                        content_type='text')

    if request.header_map.get('CONTENT-TYPE',
                              None) != 'application/octet-stream':
        return Response(400, data='Invalid Content-Type', content_type='text')

    content_disposition = request.header_map.get('CONTENT-DISPOSITION', None)
    if content_disposition is not None:
        match = INLINE_CONTENT_DISPOSITION.match(content_disposition)
        if not match:
            return InvalidRequest(
                message='Invalid Content-Disposition').asResponse()
        filename = match.groups(1)[0]
    else:
        filename = None

    file_writer, refname = _localFileWriter(filename)

    buff = request.read(CHUNK_SIZE)
    while buff:
        file_writer.write(buff)
        buff = request.read(CHUNK_SIZE)

    file_writer.close()

    return Response(202, data={'uri': 'djfh://{0}'.format(refname)})