[TASK] Support ports in getBaseUrl function and refactor duplicate code

Resolves: #52573
Change-Id: Ie6f3484f99ce2f79d77eafdc835dadc1b80517df
Reviewed-on: https://review.typo3.org/24447
Reviewed-by: Alex Kellner
Tested-by: Alex Kellner