Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / MIT / System / Web / Mobile / UrlPath.cs / 1305376 / UrlPath.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
* UrlPath class.
*
* Copyright (c) 1999 Microsoft Corporation
*/
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.Runtime.InteropServices;
using System.Collections;
using System.Diagnostics;
namespace System.Web.Mobile
{
/*
* URL Path library.
*/
[Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
internal static class UrlPath
{
private const char appRelativeCharacter = '~';
internal static bool IsRooted(String basepath)
{
return(basepath == null || basepath.Length == 0 || basepath[0] == '/' || basepath[0] == '\\');
}
internal static bool IsRelativeUrl(string url)
{
// If it has a protocol, it's not relative
if (url.IndexOf(":", StringComparison.Ordinal) != -1)
{
return false;
}
return !IsRooted(url);
}
internal static String GetDirectory(String path)
{
if (path == null || path.Length == 0)
{
throw new ArgumentException(SR.GetString(SR.UrlPath_EmptyPathHasNoDirectory));
}
if (path[0] != '/' && path[0] != appRelativeCharacter)
{
throw new ArgumentException(SR.GetString(SR.UrlPath_PathMustBeRooted));
}
// Make sure there is a filename after the last '/'
Debug.Assert(path[path.Length-1] != '/', "Path should not end with a /");
string dir = path.Substring(0, path.LastIndexOf('/'));
// If it's the root dir, we would end up with "". Return "/" instead
if (dir.Length == 0)
{
return "/";
}
return dir;
}
private static void FailIfPhysicalPath(string path)
{
if (path == null || path.Length < 4)
{
return;
}
if (path[1] == ':' || (path[0] == '\\' && path[1] == '\\'))
{
throw new Exception(SR.GetString(SR.UrlPath_PhysicalPathNotAllowed, path));
}
}
internal static String Combine(String basepath, String relative)
{
String path;
// Make sure the relative path is not a physical path (bug 73641)
FailIfPhysicalPath(relative);
if (IsRooted(relative))
{
path = relative;
if (path == null || path.Length == 0)
{
return String.Empty;
}
}
else
{
// If the relative path starts with "~/" or "~\", treat it as app root
// relative (bug 68628)
if (relative.Length >=3 && relative[0] == appRelativeCharacter && (relative[1] == '/' || relative[1] == '\\'))
{
String appPath = HttpRuntime.AppDomainAppVirtualPath;
if (appPath.Length > 1)
{
path = appPath + "/" + relative.Substring(2);
}
else
{
path = "/" + relative.Substring(2);
}
}
else
{
if (basepath == null || (basepath.Length == 1 && basepath[0] == '/'))
{
basepath = String.Empty;
}
path = basepath + "/" + relative;
}
}
return Reduce(path);
}
internal static String Reduce(String path)
{
// ignore query string
String queryString = null;
if (path != null)
{
int iqs = path.IndexOf('?');
if (iqs >= 0)
{
queryString = path.Substring(iqs);
path = path.Substring(0, iqs);
}
}
int length = path.Length;
int examine;
// Make sure we don't have any back slashes
path = path.Replace('\\', '/');
// quickly rule out situations in which there are no . or ..
for (examine = 0; ; examine++)
{
examine = path.IndexOf('.', examine);
if (examine < 0)
{
return (queryString != null) ? (path + queryString) : path;
}
if ((examine == 0 || path[examine - 1] == '/')
&& (examine + 1 == length || path[examine + 1] == '/' ||
(path[examine + 1] == '.' && (examine + 2 == length || path[examine + 2] == '/'))))
{
break;
}
}
// OK, we found a . or .. so process it:
ArrayList list = new ArrayList();
StringBuilder sb = new StringBuilder();
int start;
examine = 0;
for (;;)
{
start = examine;
examine = path.IndexOf('/', start + 1);
if (examine < 0)
{
examine = length;
}
if (examine - start <= 3 &&
(examine < 1 || path[examine - 1] == '.') &&
(start + 1 >= length || path[start + 1] == '.'))
{
if (examine - start == 3)
{
if (list.Count == 0)
{
throw new Exception(SR.GetString(SR.UrlPath_CannotExitUpTopDirectory));
}
sb.Length = (int)list[list.Count - 1];
list.RemoveRange(list.Count - 1, 1);
}
}
else
{
list.Add(sb.Length);
sb.Append(path, start, examine - start);
}
if (examine == length)
{
break;
}
}
return sb.ToString() + queryString;
}
private const string dummyProtocolAndServer = "http://foo";
// Return the relative vpath path from one rooted vpath to another
internal static string MakeRelative(string from, string to)
{
// If either path is app relative (~/...), make it absolute, since the Uri
// class wouldn't know how to deal with it.
from = MakeVirtualPathAppAbsolute(from);
to = MakeVirtualPathAppAbsolute(to);
// Make sure both virtual paths are rooted
Debug.Assert(IsRooted(from));
Debug.Assert(IsRooted(to));
// Uri's need full url's so, we use a dummy root
Uri fromUri = new Uri(dummyProtocolAndServer + from);
Uri toUri = new Uri(dummyProtocolAndServer + to);
return fromUri.MakeRelative(toUri);
}
// If a virtual path is app relative (i.e. starts with ~/), change it to
// start with the actuall app path.
// E.g. ~/Sub/foo.aspx --> /MyApp/Sub/foo.aspx
internal static string MakeVirtualPathAppAbsolute(string virtualPath) {
// If the path is exactly "~", just return the app root path
if (virtualPath.Length == 1 && virtualPath[0] == appRelativeCharacter)
return HttpRuntime.AppDomainAppVirtualPath;
// If the virtual path starts with "~/" or "~\", replace with the app path
// relative (ASURT 68628)
if (virtualPath.Length >=2 && virtualPath[0] == appRelativeCharacter &&
(virtualPath[1] == '/' || virtualPath[1] == '\\')) {
string appPath = HttpRuntime.AppDomainAppVirtualPath;
if (appPath.Length > 1)
return appPath + "/" + virtualPath.Substring(2);
else
return "/" + virtualPath.Substring(2);
}
// Return it unchanged
return virtualPath;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
* UrlPath class.
*
* Copyright (c) 1999 Microsoft Corporation
*/
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.Runtime.InteropServices;
using System.Collections;
using System.Diagnostics;
namespace System.Web.Mobile
{
/*
* URL Path library.
*/
[Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
internal static class UrlPath
{
private const char appRelativeCharacter = '~';
internal static bool IsRooted(String basepath)
{
return(basepath == null || basepath.Length == 0 || basepath[0] == '/' || basepath[0] == '\\');
}
internal static bool IsRelativeUrl(string url)
{
// If it has a protocol, it's not relative
if (url.IndexOf(":", StringComparison.Ordinal) != -1)
{
return false;
}
return !IsRooted(url);
}
internal static String GetDirectory(String path)
{
if (path == null || path.Length == 0)
{
throw new ArgumentException(SR.GetString(SR.UrlPath_EmptyPathHasNoDirectory));
}
if (path[0] != '/' && path[0] != appRelativeCharacter)
{
throw new ArgumentException(SR.GetString(SR.UrlPath_PathMustBeRooted));
}
// Make sure there is a filename after the last '/'
Debug.Assert(path[path.Length-1] != '/', "Path should not end with a /");
string dir = path.Substring(0, path.LastIndexOf('/'));
// If it's the root dir, we would end up with "". Return "/" instead
if (dir.Length == 0)
{
return "/";
}
return dir;
}
private static void FailIfPhysicalPath(string path)
{
if (path == null || path.Length < 4)
{
return;
}
if (path[1] == ':' || (path[0] == '\\' && path[1] == '\\'))
{
throw new Exception(SR.GetString(SR.UrlPath_PhysicalPathNotAllowed, path));
}
}
internal static String Combine(String basepath, String relative)
{
String path;
// Make sure the relative path is not a physical path (bug 73641)
FailIfPhysicalPath(relative);
if (IsRooted(relative))
{
path = relative;
if (path == null || path.Length == 0)
{
return String.Empty;
}
}
else
{
// If the relative path starts with "~/" or "~\", treat it as app root
// relative (bug 68628)
if (relative.Length >=3 && relative[0] == appRelativeCharacter && (relative[1] == '/' || relative[1] == '\\'))
{
String appPath = HttpRuntime.AppDomainAppVirtualPath;
if (appPath.Length > 1)
{
path = appPath + "/" + relative.Substring(2);
}
else
{
path = "/" + relative.Substring(2);
}
}
else
{
if (basepath == null || (basepath.Length == 1 && basepath[0] == '/'))
{
basepath = String.Empty;
}
path = basepath + "/" + relative;
}
}
return Reduce(path);
}
internal static String Reduce(String path)
{
// ignore query string
String queryString = null;
if (path != null)
{
int iqs = path.IndexOf('?');
if (iqs >= 0)
{
queryString = path.Substring(iqs);
path = path.Substring(0, iqs);
}
}
int length = path.Length;
int examine;
// Make sure we don't have any back slashes
path = path.Replace('\\', '/');
// quickly rule out situations in which there are no . or ..
for (examine = 0; ; examine++)
{
examine = path.IndexOf('.', examine);
if (examine < 0)
{
return (queryString != null) ? (path + queryString) : path;
}
if ((examine == 0 || path[examine - 1] == '/')
&& (examine + 1 == length || path[examine + 1] == '/' ||
(path[examine + 1] == '.' && (examine + 2 == length || path[examine + 2] == '/'))))
{
break;
}
}
// OK, we found a . or .. so process it:
ArrayList list = new ArrayList();
StringBuilder sb = new StringBuilder();
int start;
examine = 0;
for (;;)
{
start = examine;
examine = path.IndexOf('/', start + 1);
if (examine < 0)
{
examine = length;
}
if (examine - start <= 3 &&
(examine < 1 || path[examine - 1] == '.') &&
(start + 1 >= length || path[start + 1] == '.'))
{
if (examine - start == 3)
{
if (list.Count == 0)
{
throw new Exception(SR.GetString(SR.UrlPath_CannotExitUpTopDirectory));
}
sb.Length = (int)list[list.Count - 1];
list.RemoveRange(list.Count - 1, 1);
}
}
else
{
list.Add(sb.Length);
sb.Append(path, start, examine - start);
}
if (examine == length)
{
break;
}
}
return sb.ToString() + queryString;
}
private const string dummyProtocolAndServer = "http://foo";
// Return the relative vpath path from one rooted vpath to another
internal static string MakeRelative(string from, string to)
{
// If either path is app relative (~/...), make it absolute, since the Uri
// class wouldn't know how to deal with it.
from = MakeVirtualPathAppAbsolute(from);
to = MakeVirtualPathAppAbsolute(to);
// Make sure both virtual paths are rooted
Debug.Assert(IsRooted(from));
Debug.Assert(IsRooted(to));
// Uri's need full url's so, we use a dummy root
Uri fromUri = new Uri(dummyProtocolAndServer + from);
Uri toUri = new Uri(dummyProtocolAndServer + to);
return fromUri.MakeRelative(toUri);
}
// If a virtual path is app relative (i.e. starts with ~/), change it to
// start with the actuall app path.
// E.g. ~/Sub/foo.aspx --> /MyApp/Sub/foo.aspx
internal static string MakeVirtualPathAppAbsolute(string virtualPath) {
// If the path is exactly "~", just return the app root path
if (virtualPath.Length == 1 && virtualPath[0] == appRelativeCharacter)
return HttpRuntime.AppDomainAppVirtualPath;
// If the virtual path starts with "~/" or "~\", replace with the app path
// relative (ASURT 68628)
if (virtualPath.Length >=2 && virtualPath[0] == appRelativeCharacter &&
(virtualPath[1] == '/' || virtualPath[1] == '\\')) {
string appPath = HttpRuntime.AppDomainAppVirtualPath;
if (appPath.Length > 1)
return appPath + "/" + virtualPath.Substring(2);
else
return "/" + virtualPath.Substring(2);
}
// Return it unchanged
return virtualPath;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TemplateAction.cs
- AliasGenerator.cs
- ClientOptions.cs
- DatagridviewDisplayedBandsData.cs
- SoapReflector.cs
- InstancePersistenceCommandException.cs
- DbDataReader.cs
- DSGeneratorProblem.cs
- ComContractElementCollection.cs
- InputReportEventArgs.cs
- CallSite.cs
- FixedSOMTableCell.cs
- DrawListViewColumnHeaderEventArgs.cs
- BinaryMethodMessage.cs
- MetricEntry.cs
- EntryIndex.cs
- XmlSchemaNotation.cs
- ResponseStream.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- MenuItemBinding.cs
- IDispatchConstantAttribute.cs
- XmlCharType.cs
- CapacityStreamGeometryContext.cs
- PageBreakRecord.cs
- DesignerActionGlyph.cs
- ListBindingConverter.cs
- ProfileInfo.cs
- ConversionContext.cs
- FastEncoderWindow.cs
- TypeHelpers.cs
- PageCodeDomTreeGenerator.cs
- ProgressBar.cs
- UnknownWrapper.cs
- ZipFileInfo.cs
- AutoGeneratedFieldProperties.cs
- SoapSchemaExporter.cs
- BrowserCapabilitiesCodeGenerator.cs
- MetadataCacheItem.cs
- PackageProperties.cs
- PackageRelationshipCollection.cs
- Parser.cs
- DecryptRequest.cs
- ScriptingJsonSerializationSection.cs
- SqlConnectionPoolGroupProviderInfo.cs
- SafeLibraryHandle.cs
- BaseUriHelper.cs
- DockPanel.cs
- PathFigureCollectionConverter.cs
- Span.cs
- _AutoWebProxyScriptEngine.cs
- InternalConfigHost.cs
- XmlQueryOutput.cs
- InstanceOwner.cs
- ModelUIElement3D.cs
- Button.cs
- LabelEditEvent.cs
- PropertyTabAttribute.cs
- Imaging.cs
- TextContainerHelper.cs
- StateManagedCollection.cs
- _ConnectStream.cs
- TimeSpanValidator.cs
- WebPartsSection.cs
- AssemblyResourceLoader.cs
- CompoundFileReference.cs
- TableStyle.cs
- DataGridViewLinkCell.cs
- TextSimpleMarkerProperties.cs
- Accessible.cs
- ContextMenu.cs
- DummyDataSource.cs
- FragmentNavigationEventArgs.cs
- AppSecurityManager.cs
- MethodToken.cs
- WebPartVerbsEventArgs.cs
- BCLDebug.cs
- GroupItem.cs
- PolicyLevel.cs
- TemplateControlCodeDomTreeGenerator.cs
- VirtualPath.cs
- DynamicPropertyHolder.cs
- DataGridColumnStyleMappingNameEditor.cs
- InstancePersistenceEvent.cs
- ItemContainerGenerator.cs
- SoapWriter.cs
- WorkflowFileItem.cs
- CodeTypeMemberCollection.cs
- PrivateFontCollection.cs
- PageBuildProvider.cs
- Coordinator.cs
- KnownAssembliesSet.cs
- DoubleIndependentAnimationStorage.cs
- DefaultBindingPropertyAttribute.cs
- RegexGroup.cs
- ColumnWidthChangingEvent.cs
- FontStyle.cs
- ElementHostPropertyMap.cs
- WebEvents.cs
- ToolStripItemEventArgs.cs
- TypeSystem.cs