refactor replace_version stream implementation

this implementation has been used by localfiles and proxy middleware.
Now both middlewares use one implementation, more DRY and such.