internalPath method

String internalPath(
  1. String externalPath
)

Convert an external path to an internal path.

This is the inverse of Request.rewriteUrl, converting a string like "/foo/bar/baz" into "~/foo/bar/baz" (if the server's base path is "/"). Or converting "/abc/def/foo/bar/baz" to "~/foo/bar/baz" (if the server's base path is "/abc/def").

If the external path contains query parameters, they are removed.

Implementation

String internalPath(String externalPath) {
  if (externalPath.startsWith(_basePath)) {
    // Strip off any query parameters
    final q = externalPath.indexOf('?');
    final noQueryParams =
        (0 < q) ? externalPath.substring(0, q) : externalPath;

    return '~/${noQueryParams.substring(_basePath.length)}';
  } else {
    throw ArgumentError.value(
        externalPath, 'externalPath', 'does not start with "$_basePath"');
  }
}