Refactor middleware factory method to allow creating both stream and http middlewares.