summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Web.WebPages/WebPageExecutingBase.cs
blob: 64652078086fa204ca6045c09b1a44181252ce59 (plain)
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
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Web.WebPages.Instrumentation;
using System.Web.WebPages.Resources;

/*
WebPage class hierarchy

WebPageExecutingBase                        The base class for all Plan9 files (_pagestart, _appstart, and regular pages)
    ApplicationStartPage                    Used for _appstart.cshtml
    WebPageRenderingBase
        StartPage                           Used for _pagestart.cshtml
        WebPageBase
            WebPage                         Plan9Pages
            ViewWebPage?                    MVC Views
HelperPage                                  Base class for Web Pages in App_Code.
*/

namespace System.Web.WebPages
{
    // The base class for all CSHTML files (_pagestart, _appstart, and regular pages)
    public abstract class WebPageExecutingBase
    {
        private IVirtualPathFactory _virtualPathFactory;
        private DynamicHttpApplicationState _dynamicAppState;
        private InstrumentationService _instrumentationService = null;

        internal InstrumentationService InstrumentationService
        {
            get
            {
                if (_instrumentationService == null)
                {
                    _instrumentationService = new InstrumentationService();
                }
                return _instrumentationService;
            }
            set { _instrumentationService = value; }
        }

        public virtual HttpApplicationStateBase AppState
        {
            get
            {
                if (Context != null)
                {
                    return Context.Application;
                }
                return null;
            }
        }

        public virtual dynamic App
        {
            get
            {
                if (_dynamicAppState == null && AppState != null)
                {
                    _dynamicAppState = new DynamicHttpApplicationState(AppState);
                }
                return _dynamicAppState;
            }
        }

        public virtual HttpContextBase Context { get; set; }

        public virtual string VirtualPath { get; set; }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public virtual IVirtualPathFactory VirtualPathFactory
        {
            get { return _virtualPathFactory ?? VirtualPathFactoryManager.Instance; }
            set { _virtualPathFactory = value; }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public abstract void Execute();

        public virtual string Href(string path, params object[] pathParts)
        {
            return UrlUtil.Url(VirtualPath, path, pathParts);
        }

        protected internal void BeginContext(int startPosition, int length, bool isLiteral)
        {
            BeginContext(GetOutputWriter(), VirtualPath, startPosition, length, isLiteral);
        }

        protected internal void BeginContext(string virtualPath, int startPosition, int length, bool isLiteral)
        {
            BeginContext(GetOutputWriter(), virtualPath, startPosition, length, isLiteral);
        }

        protected internal void BeginContext(TextWriter writer, int startPosition, int length, bool isLiteral)
        {
            BeginContext(writer, VirtualPath, startPosition, length, isLiteral);
        }

        protected internal void BeginContext(TextWriter writer, string virtualPath, int startPosition, int length, bool isLiteral)
        {
            // Double check that the instrumentation service is active because WriteAttribute always calls this
            if (InstrumentationService.IsAvailable)
            {
                InstrumentationService.BeginContext(Context,
                                                    virtualPath,
                                                    writer,
                                                    startPosition,
                                                    length,
                                                    isLiteral);
            }
        }

        protected internal void EndContext(int startPosition, int length, bool isLiteral)
        {
            EndContext(GetOutputWriter(), VirtualPath, startPosition, length, isLiteral);
        }

        protected internal void EndContext(string virtualPath, int startPosition, int length, bool isLiteral)
        {
            EndContext(GetOutputWriter(), virtualPath, startPosition, length, isLiteral);
        }

        protected internal void EndContext(TextWriter writer, int startPosition, int length, bool isLiteral)
        {
            EndContext(writer, VirtualPath, startPosition, length, isLiteral);
        }

        protected internal void EndContext(TextWriter writer, string virtualPath, int startPosition, int length, bool isLiteral)
        {
            // Double check that the instrumentation service is active because WriteAttribute always calls this
            if (InstrumentationService.IsAvailable)
            {
                InstrumentationService.EndContext(Context,
                                                  virtualPath,
                                                  writer,
                                                  startPosition,
                                                  length,
                                                  isLiteral);
            }
        }

        internal virtual string GetDirectory(string virtualPath)
        {
            return VirtualPathUtility.GetDirectory(virtualPath);
        }

        /// <summary>
        /// Normalizes path relative to the current virtual path and throws if a file does not exist at the location.
        /// </summary>
        internal string NormalizeLayoutPagePath(string layoutPagePath)
        {
            var virtualPath = NormalizePath(layoutPagePath);
            // Look for it as specified, either absolute, relative or same folder
            if (VirtualPathFactory.Exists(virtualPath))
            {
                return virtualPath;
            }
            throw new HttpException(String.Format(CultureInfo.CurrentCulture, WebPageResources.WebPage_LayoutPageNotFound, layoutPagePath, virtualPath));
        }

        public virtual string NormalizePath(string path)
        {
            // If it's relative, resolve it
            return VirtualPathUtility.Combine(VirtualPath, path);
        }

        public abstract void Write(HelperResult result);

        public abstract void Write(object value);

        public abstract void WriteLiteral(object value);

        public virtual void WriteAttribute(string name, PositionTagged<string> prefix, PositionTagged<string> suffix, params AttributeValue[] values)
        {
            WriteAttributeTo(GetOutputWriter(), name, prefix, suffix, values);
        }

        public virtual void WriteAttributeTo(TextWriter writer, string name, PositionTagged<string> prefix, PositionTagged<string> suffix, params AttributeValue[] values)
        {
            WriteAttributeTo(VirtualPath, writer, name, prefix, suffix, values);
        }

        protected internal virtual void WriteAttributeTo(string pageVirtualPath, TextWriter writer, string name, PositionTagged<string> prefix, PositionTagged<string> suffix, params AttributeValue[] values)
        {
            bool first = true;
            bool wroteSomething = false;
            if (values.Length == 0)
            {
                // Explicitly empty attribute, so write the prefix and suffix
                WritePositionTaggedLiteral(writer, pageVirtualPath, prefix);
                WritePositionTaggedLiteral(writer, pageVirtualPath, suffix);
            }
            else
            {
                foreach (AttributeValue attrVal in values)
                {
                    PositionTagged<object> val = attrVal.Value;
                    bool? boolVal = null;
                    if (val.Value is bool)
                    {
                        boolVal = (bool)val.Value;
                    }

                    if (val.Value != null && (boolVal == null || boolVal.Value))
                    {
                        string valStr = val.Value as string;
                        if (valStr == null)
                        {
                            valStr = val.Value.ToString();
                        }
                        if (boolVal != null)
                        {
                            Debug.Assert(boolVal.Value);
                            valStr = name;
                        }

                        if (first)
                        {
                            WritePositionTaggedLiteral(writer, pageVirtualPath, prefix);
                            first = false;
                        }
                        else
                        {
                            WritePositionTaggedLiteral(writer, pageVirtualPath, attrVal.Prefix);
                        }
                        BeginContext(writer, pageVirtualPath, attrVal.Value.Position, valStr.Length, isLiteral: attrVal.Literal);
                        if (attrVal.Literal)
                        {
                            WriteLiteralTo(writer, valStr);
                        }
                        else
                        {
                            WriteTo(writer, valStr); // Write value
                        }
                        EndContext(writer, pageVirtualPath, attrVal.Value.Position, valStr.Length, isLiteral: attrVal.Literal);
                        wroteSomething = true;
                    }
                }
                if (wroteSomething)
                {
                    WritePositionTaggedLiteral(writer, pageVirtualPath, suffix);
                }
            }
        }

        private void WritePositionTaggedLiteral(TextWriter writer, string pageVirtualPath, string value, int position)
        {
            BeginContext(writer, pageVirtualPath, position, value.Length, isLiteral: true);
            WriteLiteralTo(writer, value);
            EndContext(writer, pageVirtualPath, position, value.Length, isLiteral: true);
        }

        private void WritePositionTaggedLiteral(TextWriter writer, string pageVirtualPath, PositionTagged<string> value)
        {
            WritePositionTaggedLiteral(writer, pageVirtualPath, value.Value, value.Position);
        }

        // This method is called by generated code and needs to stay in sync with the parser
        public static void WriteTo(TextWriter writer, HelperResult content)
        {
            if (content != null)
            {
                content.WriteTo(writer);
            }
        }

        // This method is called by generated code and needs to stay in sync with the parser
        public static void WriteTo(TextWriter writer, object content)
        {
            writer.Write(HttpUtility.HtmlEncode(content));
        }

        // This method is called by generated code and needs to stay in sync with the parser
        public static void WriteLiteralTo(TextWriter writer, object content)
        {
            writer.Write(content);
        }

        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "A method is more appropriate in this case since a property likely already exists to hold this value")]
        protected internal virtual TextWriter GetOutputWriter()
        {
            return TextWriter.Null;
        }
    }
}