findRealIp :: RequestHeaders -> HeaderName -> (IP.IP -> Bool) -> Maybe IP.IP
findRealIp reqHeaders header isTrusted =
    case (nonTrusted, ips) of
      ([], xs) -> listToMaybe xs
      (xs, _)  -> listToMaybe $ reverse xs
  where
    -- account for repeated headers
    headerVals = [ v | (k, v) <- reqHeaders, k == header ]
    ips = mapMaybe (readMaybe . B8.unpack) $ concatMap (B8.split ',') headerVals
    nonTrusted = filter (not . isTrusted) ips
findRealIp :: RequestHeaders -> HeaderName -> (IP.IP -> Bool) -> Maybe IP.IP
findRealIp reqHeaders header isTrusted =
    case (nonTrusted, ips) of
      ([], xs) -> listToMaybe xs
      (xs, _)  -> listToMaybe $ reverse xs
  where
    -- account for repeated headers
    headerVals = [ v | (k, v) <- reqHeaders, k == header ]
    ips = mapMaybe (readMaybe . B8.unpack) $ concatMap (B8.split ',') headerVals
    nonTrusted = filter (not . isTrusted) ips
findRealIp reqHeaders header isTrusted =
    case (nonTrusted, ips) of
      ([], xs) -> listToMaybe xs
      (xs, _)  -> listToMaybe $ reverse xs
  where
    -- account for repeated headers
    headerVals = [ v | (k, v) <- reqHeaders, k == header ]
    ips = mapMaybe (readMaybe . B8.unpack) $ concatMap (B8.split ',') headerVals
    nonTrusted = filter (not . isTrusted) ips