use existing <base> tag if any
fix URL reference
ensure stream gets closed even on exception