1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
//------------------------------------------------------------------------------
// <copyright file="BatchParser.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI {
using System;
using System.IO;
using System.Web.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Threading;
using System.Globalization;
using System.Web.Hosting;
using System.Web.Caching;
using System.Web.Util;
using System.Web.Compilation;
using HttpException = System.Web.HttpException;
using Debug=System.Web.Util.Debug;
using System.Text.RegularExpressions;
internal abstract class DependencyParser : BaseParser {
private VirtualPath _virtualPath;
private StringSet _virtualPathDependencies;
// Used to detect circular references
private StringSet _circularReferenceChecker = new CaseInsensitiveStringSet();
// The <pages> config section
private PagesSection _pagesConfig;
protected PagesSection PagesConfig {
get { return _pagesConfig; }
}
internal void Init(VirtualPath virtualPath) {
CurrentVirtualPath = virtualPath;
_virtualPath = virtualPath;
_pagesConfig = MTConfigUtil.GetPagesConfig(virtualPath);
}
internal ICollection GetVirtualPathDependencies() {
// Always set the culture to Invariant when parsing (ASURT 99071)
Thread currentThread = Thread.CurrentThread;
CultureInfo prevCulture = currentThread.CurrentCulture;
HttpRuntime.SetCurrentThreadCultureWithAssert(CultureInfo.InvariantCulture);
try {
try {
PrepareParse();
ParseFile();
}
finally {
// Restore the previous culture
HttpRuntime.SetCurrentThreadCultureWithAssert(prevCulture);
}
}
catch { throw; } // Prevent Exception Filter Security Issue (ASURT 122835)
return _virtualPathDependencies;
}
protected void AddDependency(VirtualPath virtualPath) {
virtualPath = ResolveVirtualPath(virtualPath);
Debug.Trace("Template", "Parsed dependency: " + _virtualPath + " depends on " + virtualPath);
if (_virtualPathDependencies == null)
_virtualPathDependencies = new CaseInsensitiveStringSet();
_virtualPathDependencies.Add(virtualPath.VirtualPathString);
}
internal abstract string DefaultDirectiveName { get; }
protected virtual void PrepareParse() {}
private void ParseFile() {
ParseFile(null /*physicalPath*/, _virtualPath);
}
private void ParseFile(string physicalPath, VirtualPath virtualPath) {
// Determine the file used for the circular references checker. Normally,
// we use the virtualPath, but we use the physical path if it specified,
// as is the case for <!-- #include file="foo.inc" -->
string fileToReferenceCheck = physicalPath != null ? physicalPath : virtualPath.VirtualPathString;
// Check for circular references of include files
if (_circularReferenceChecker.Contains(fileToReferenceCheck)) {
throw new HttpException(
SR.GetString(SR.Circular_include));
}
// Add the current file to the circular references checker.
_circularReferenceChecker.Add(fileToReferenceCheck);
try {
// Open a TextReader either from the physical or virtual path
TextReader reader;
if (physicalPath != null) {
using (reader = Util.ReaderFromFile(physicalPath, virtualPath)) {
ParseReader(reader);
}
}
else {
using (Stream stream = virtualPath.OpenFile()) {
reader = Util.ReaderFromStream(stream, virtualPath);
ParseReader(reader);
}
}
}
finally {
// Remove the current file from the circular references checker
_circularReferenceChecker.Remove(fileToReferenceCheck);
}
}
private void ParseReader(TextReader input) {
ParseString(input.ReadToEnd());
}
private void ParseString(string text) {
int textPos = 0;
for (;;) {
Match match;
// 1: scan for text up to the next tag.
if ((match = textRegex.Match(text, textPos)).Success) {
textPos = match.Index + match.Length;
}
// we might be done now
if (textPos == text.Length)
break;
// 2: handle constructs that start with <
// Check to see if it's a directive (i.e. <%@ %> block)
if ((match = directiveRegex.Match(text, textPos)).Success) {
IDictionary directive = CollectionsUtil.CreateCaseInsensitiveSortedList();
string directiveName = ProcessAttributes(match, directive);
ProcessDirective(directiveName, directive);
textPos = match.Index + match.Length;
}
else if ((match = includeRegex.Match(text, textPos)).Success) {
ProcessServerInclude(match);
textPos = match.Index + match.Length;
}
else if ((match = commentRegex.Match(text, textPos)).Success) {
// Just skip it
textPos = match.Index + match.Length;
}
else {
int newPos = text.IndexOf("<%@", textPos, StringComparison.Ordinal);
// 2nd condition is used to catch invalid directives, e.g. <%@ attr="value_without_end_quote >
if (newPos == -1 || newPos == textPos) {
return;
}
textPos = newPos;
}
// we might be done now
if (textPos == text.Length)
return;
}
}
/*
* Process a server side include. e.g. <!-- #include file="foo.inc" -->
*/
private void ProcessServerInclude(Match match) {
string pathType = match.Groups["pathtype"].Value;
string filename = match.Groups["filename"].Value;
if (filename.Length == 0) return;
VirtualPath newVirtualPath;
string newPhysicalPath = null;
if (StringUtil.EqualsIgnoreCase(pathType, "file")) {
if (UrlPath.IsAbsolutePhysicalPath(filename)) {
// If it's an absolute physical path, use it as is
newPhysicalPath = filename;
// Reuse the current virtual path
newVirtualPath = CurrentVirtualPath;
}
else {
// If it's relative, just treat it as virtual
newVirtualPath = ResolveVirtualPath(VirtualPath.Create(filename));
}
}
else if (StringUtil.EqualsIgnoreCase(pathType, "virtual")) {
newVirtualPath = ResolveVirtualPath(VirtualPath.Create(filename));
}
else {
// Unknown #include type: ignore it
return;
}
VirtualPath prevVirtualPath = _virtualPath;
try {
_virtualPath = newVirtualPath;
// Parse the included file recursively
ParseFile(newPhysicalPath, newVirtualPath);
}
finally {
// Restore the paths
_virtualPath = prevVirtualPath;
}
}
/*
* Process a <%@ %> block
*/
internal virtual void ProcessDirective(string directiveName, IDictionary directive) {
// Get all the directives into a bag
// Check for the main directive (e.g. "page" for an aspx)
if (directiveName == null ||
StringUtil.EqualsIgnoreCase(directiveName, DefaultDirectiveName) ) {
ProcessMainDirective(directive);
}
else if (StringUtil.EqualsIgnoreCase(directiveName, "register")) {
VirtualPath src = Util.GetAndRemoveVirtualPathAttribute(directive, "src");
if (src != null) {
AddDependency(src);
}
}
else if (StringUtil.EqualsIgnoreCase(directiveName, "reference")) {
VirtualPath virtualPath = Util.GetAndRemoveVirtualPathAttribute(directive, "virtualpath");
if (virtualPath != null)
AddDependency(virtualPath);
VirtualPath page = Util.GetAndRemoveVirtualPathAttribute(directive, "page");
if (page != null)
AddDependency(page);
VirtualPath control = Util.GetAndRemoveVirtualPathAttribute(directive, "control");
if (control != null)
AddDependency(control);
}
else if (StringUtil.EqualsIgnoreCase(directiveName, "assembly")) {
VirtualPath src = Util.GetAndRemoveVirtualPathAttribute(directive, "src");
if (src != null)
AddDependency(src);
}
}
private void ProcessMainDirective(IDictionary mainDirective) {
// Go through all the attributes on the directive
foreach (DictionaryEntry entry in mainDirective) {
string attribName = ((string)entry.Key).ToLower(CultureInfo.InvariantCulture);
// Parse out the device name, if any
string name;
string deviceName = Util.ParsePropertyDeviceFilter(attribName, out name);
// Process the attribute
ProcessMainDirectiveAttribute(deviceName, name, (string) entry.Value);
}
}
internal virtual void ProcessMainDirectiveAttribute(string deviceName, string name,
string value) {
// A "src" attribute is equivalent to an imported source file
if (name == "src") {
string src = Util.GetNonEmptyAttribute(name, value);
AddDependency(VirtualPath.Create(src));
}
}
/*
* Adds attributes and their values to the attribs
*/
private string ProcessAttributes(Match match, IDictionary attribs) {
string ret = null;
CaptureCollection attrnames = match.Groups["attrname"].Captures;
CaptureCollection attrvalues = match.Groups["attrval"].Captures;
CaptureCollection equalsign = match.Groups["equal"].Captures;
for (int i = 0; i < attrnames.Count; i++) {
string attribName = attrnames[i].ToString();
string attribValue = attrvalues[i].ToString();
bool fHasEqual = (equalsign[i].ToString().Length > 0);
if (attribName != null && !fHasEqual && ret == null) {
ret = attribName;
continue;
}
try {
if (attribs != null)
attribs.Add(attribName, attribValue);
}
catch (ArgumentException) {}
}
return ret;
}
}
internal abstract class TemplateControlDependencyParser : DependencyParser {
internal override void ProcessMainDirectiveAttribute(string deviceName, string name,
string value) {
switch (name) {
case "masterpagefile":
value = value.Trim();
if (value.Length > 0) {
// Add a dependency on the master, whether it has a device filter or not
AddDependency(VirtualPath.Create(value));
}
break;
default:
// We didn't handle the attribute. Try the base class
base.ProcessMainDirectiveAttribute(deviceName, name, value);
break;
}
}
}
internal class PageDependencyParser : TemplateControlDependencyParser {
internal override string DefaultDirectiveName {
get { return PageParser.defaultDirectiveName; }
}
protected override void PrepareParse() {
if (PagesConfig != null) {
if (PagesConfig.MasterPageFileInternal != null && PagesConfig.MasterPageFileInternal.Length != 0)
AddDependency(VirtualPath.Create(PagesConfig.MasterPageFileInternal));
}
}
internal override void ProcessDirective(string directiveName, IDictionary directive) {
base.ProcessDirective(directiveName, directive);
if (StringUtil.EqualsIgnoreCase(directiveName, "previousPageType") ||
StringUtil.EqualsIgnoreCase(directiveName, "masterType")) {
VirtualPath virtualPath = Util.GetAndRemoveVirtualPathAttribute(directive, "virtualPath");
if (virtualPath != null)
AddDependency(virtualPath);
}
}
}
internal class UserControlDependencyParser : TemplateControlDependencyParser {
internal override string DefaultDirectiveName {
get { return UserControlParser.defaultDirectiveName; }
}
}
internal class MasterPageDependencyParser : UserControlDependencyParser {
internal override string DefaultDirectiveName {
get { return MasterPageParser.defaultDirectiveName; }
}
internal override void ProcessDirective(string directiveName, IDictionary directive) {
base.ProcessDirective(directiveName, directive);
if (StringUtil.EqualsIgnoreCase(directiveName, "masterType")) {
VirtualPath virtualPath = Util.GetAndRemoveVirtualPathAttribute(directive, "virtualPath");
if (virtualPath != null)
AddDependency(virtualPath);
}
}
}
}
|