Refactor/protocol detection

Use \Request instead of $_SERVER. Working on #2334