[HttpFoundation] refactored code to avoid code duplication