Custom urlencoding implementation to match Prosody implementation, and error handling fix