detectAddress static method

String detectAddress(
  1. Request req
)

Detects the IP address from which the given HTTP request was received.

  • req an HTTP RequestContext request to process. Returns the detected IP address (without a port). If no IP is detected - null will be returned.

Implementation

static String detectAddress(Request req) {
  var ip;

  if (req.headers['x-forwarded-for'] != null) {
    ip = req.headers['x-forwarded-for']?[0];
  }

  // Remove port
  if (ip != null) {
    ip = ip.toString();
    var index = ip.indexOf(':');
    if (index > 0) {
      ip = ip.substring(0, index);
    }
  }

  return ip.toString();
}