Fix #1792 by retrying 502, 504 HTTP errors.

Fixes #1792. Includes tests to make sure those failure codes are retried.

@toastdriven, please sanity check.