def put(self, env, start_response):
        """Writes a file passed in the request to the archive"""
        archivefile = None
        resp = interface_responses.Responses()
        path_info = env['PATH_INFO']
        mod_time = get_http_modified_time(env)
        stderr.flush()
        archivefile = self._archive.open(path_info, "w")
        try:
            content_length = int(env['CONTENT_LENGTH'])
        except Exception as ex:
            raise ArchiveInterfaceError("Can't get file content length with "\
                "error: " + str(ex))
        while content_length > 0:
            if content_length > BLOCK_SIZE:
                buf = env['wsgi.input'].read(BLOCK_SIZE)
            else:
                buf = env['wsgi.input'].read(content_length)
            archivefile.write(buf)
            content_length -= len(buf)

        self._response = resp.successful_put_response(start_response,
                                                      env['CONTENT_LENGTH'])
        archivefile.close()
        archivefile.set_mod_time(mod_time)
        return self.return_response()
 def stage(self, env, start_response):
     """Stage the file specified in the request to disk"""
     archivefile = None
     path_info = env['PATH_INFO']
     resp = interface_responses.Responses()
     stderr.flush()
     archivefile = self._archive.open(path_info, "r")
     archivefile.stage()
     self._response = resp.file_stage(start_response, path_info)
     archivefile.close()
     return self.return_response()
 def status(self, env, start_response):
     """Gets the status of a file specified in the request"""
     archivefile = None
     path_info = env['PATH_INFO']
     resp = interface_responses.Responses()
     stderr.flush()
     archivefile = self._archive.open(path_info, "r")
     status = archivefile.status()
     self._response = resp.file_status(start_response, status)
     archivefile.close()
     return self.return_response()
 def pacifica_archiveinterface(self, env, start_response):
     """Parses request method type"""
     try:
         if env['REQUEST_METHOD'] == 'GET':
             return self.get(env, start_response)
         elif env['REQUEST_METHOD'] == 'PUT':
             return self.put(env, start_response)
         elif env['REQUEST_METHOD'] == 'HEAD':
             return self.status(env, start_response)
         elif env['REQUEST_METHOD'] == 'POST':
             return self.stage(env, start_response)
         else:
             resp = interface_responses.Responses()
             self._response = resp.unknown_request(start_response,
                                                   env['REQUEST_METHOD'])
             return self.return_response()
     except ArchiveInterfaceError as ex:
         #catching application errors
         #set the error reponse
         resp = interface_responses.Responses()
         self._response = resp.archive_exception(start_response, ex, env['REQUEST_METHOD'])
         return self.return_response()
    def get(self, env, start_response):
        """Gets a file specified in the request and writes back the data"""
        archivefile = None
        path_info = env['PATH_INFO']
        #if asking for / then return a message that the archive is working
        if path_info == '/':
            resp = interface_responses.Responses()
            self._response = resp.archive_working_response(start_response)
            return self.return_response()
        stderr.flush()
        archivefile = self._archive.open(path_info, "r")

        start_response('200 OK', [('Content-Type',
                                   'application/octet-stream')])
        if 'wsgi.file_wrapper' in env:
            return env['wsgi.file_wrapper'](archivefile, BLOCK_SIZE)
        return iter(lambda: archivefile.read(BLOCK_SIZE), '')