private HttpURLConnection sendRequestImpl(Request request)
            throws URISyntaxException, IOException, HmacException {
        HttpURLConnection conn = getConn(request);

        if (conn.getResponseCode() == 404
                && request.getPackageStreamGetter() != null) {
            // The package isn't in the HDCS cache, so upload it
            HttpURLConnection uploadConn = getConn(new UploadPackageRequest(
                    request.getPackageId(),
                    request.getPackageStreamGetter()));

            if (Util.httpOk(uploadConn)) {
                uploadConn.disconnect();
                conn.disconnect();
                // The upload succeeded, so retry the original request.
                conn = getConn(request);
            } else if (uploadConn.getResponseCode() != 409) {
                conn.disconnect();
                conn = uploadConn; // Return the response from the upload.
            } else {
                uploadConn.disconnect();
            }

            // If the upload status is 409, then the package was already in the
            // cache, so we'll return the response from the original request.
        }

        return conn;
    }