Refactoring extension code to preserve URL and principal

Fixes #427 for real

Also, properly resolves SWF url when BASE is used on the page