My favorites | Sign in
Project Home Downloads Wiki Source
Checkout   Browse   Changes    
 
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections;
using System.Security.Principal;
using System.Diagnostics;
using System.Web.Hosting;

namespace N2.Web
{
/// <summary>
/// A request context class that interacts with HttpContext.Current.
/// </summary>
public class WebRequestContext : IWebContext, IDisposable
{
/// <summary>Provides access to HttpContext.Current.</summary>
protected virtual HttpContext CurrentHttpContext
{
get
{
if (HttpContext.Current == null)
throw new N2Exception("Tried to retrieve HttpContext.Current but it's null. This may happen when working outside a request or when doing stuff after the context has been recycled.");
return HttpContext.Current;
}
}

public bool IsWeb
{
get { return true; }
}

/// <summary>Gets a dictionary of request scoped items.</summary>
public IDictionary RequestItems
{
get { return CurrentHttpContext.Items; }
}

/// <summary>A page instance stored in the request context.</summary>
public ContentItem CurrentPage
{
get { return RequestItems["CurrentPage"] as ContentItem; }
set { RequestItems["CurrentPage"] = value; }
}

/// <summary>The template used to serve this request.</summary>
public PathData CurrentPath
{
get { return RequestItems["CurrentTemplate"] as PathData ?? PathData.Empty; }
set
{
RequestItems["CurrentTemplate"] = value;
if (value != null)
CurrentPage = value.CurrentPage;
else
CurrentPage = null;
}
}

/// <summary>Specifies whether the UrlAuthorizationModule should skip authorization for the current request.</summary>
public bool SkipAuthorization
{
get { return CurrentHttpContext.SkipAuthorization; }
set { CurrentHttpContext.SkipAuthorization = value; }
}

/// <summary>The handler associated with the current request.</summary>
public IHttpHandler Handler
{
get { return CurrentHttpContext.Handler; }
}

/// <summary>The current request object.</summary>
public HttpRequest Request
{
get { return CurrentHttpContext.Request; }
}

/// <summary>The physical path on disk to the requested resource.</summary>
public virtual string PhysicalPath
{
get { return Request.PhysicalPath; }
}

/// <summary>The host part of the requested url, e.g. http://n2cms.com/path/to/a/page.aspx?some=query.</summary>
public Url Url
{
get { return new Url(Request.Url.Scheme, Request.Url.Authority, Request.RawUrl); }
}

/// <summary>The current request object.</summary>
public HttpResponse Response
{
get { return CurrentHttpContext.Response; }
}

/// <summary>Gets the current user in the web execution context.</summary>
public IPrincipal User
{
get { return CurrentHttpContext.User; }
}

/// <summary>Converts a virtual url to an absolute url.</summary>
/// <param name="virtualPath">The virtual url to make absolute.</param>
/// <returns>The absolute url.</returns>
public virtual string ToAbsolute(string virtualPath)
{
return N2.Web.Url.ToAbsolute(virtualPath);
}

/// <summary>Converts an absolute url to an app relative url.</summary>
/// <param name="virtualPath">The absolute url to convert.</param>
/// <returns>An app relative url.</returns>
public virtual string ToAppRelative(string virtualPath)
{
if (virtualPath != null && virtualPath.StartsWith(Url.ApplicationPath, System.StringComparison.InvariantCultureIgnoreCase))
return "~/" + virtualPath.Substring(Url.ApplicationPath.Length);
return virtualPath;
}

/// <summary>Maps a virtual path to a physical disk path.</summary>
/// <param name="path">The path to map. E.g. "~/bin"</param>
/// <returns>The physical path. E.g. "c:\inetpub\wwwroot\bin"</returns>
public string MapPath(string path)
{
return HostingEnvironment.MapPath(path);
}

/// <summary>Assigns a rewrite path.</summary>
/// <param name="path">The path to the template that will handle the request.</param>
public void RewritePath(string path)
{
Debug.WriteLine("Rewriting '" + Url.LocalUrl + "' to '" + path + "'");
CurrentHttpContext.RewritePath(path, false);
}

/// <summary>Assigns a rewrite path.</summary>
/// <param name="path">The path to the template that will handle the request.</param>
/// <param name="queryString">The query string to rewrite to.</param>
public void RewritePath(string path, string queryString)
{
Debug.WriteLine("Rewriting '" + Url.LocalUrl + "' to '" + path + "'");
CurrentHttpContext.RewritePath(path, "", queryString);
}

public void TransferRequest(string path)
{
string url = Url.Parse(path).AppendQuery("postback", Url.LocalUrl);
CurrentHttpContext.Server.TransferRequest(url, true);
}

/// <summary>Calls into HttpContext.ClearError().</summary>
public void ClearError()
{
CurrentHttpContext.ClearError();
}

/// <summary>Disposes request items that needs disposing. This method should be called at the end of each request.</summary>
public virtual void Close()
{
object[] keys = new object[RequestItems.Keys.Count];
RequestItems.Keys.CopyTo(keys, 0);

foreach (object key in keys)
{
IClosable value = RequestItems[key] as IClosable;
if (value != null)
{
value.Dispose();
}
}
}

#region IDisposable Members

void IDisposable.Dispose()
{
Close();
}

#endregion
}
}

Change log

r1503 by cristian.libardo on Nov 15, 2010   Diff
- Removed unecessary IMailSender from
template projects
- Fixed authorization + rewrite issue
(ThreadId=77613) (security model change)
Go to: 
Project members, sign in to write a code review

Older revisions

r1457 by cristian.libardo on Sep 26, 2010   Diff
- Updated MVC content and tweaked
styles
- Fixed redirect to fixclass on MVC
r1422 by cristian.libardo on Sep 7, 2010   Diff
- Applied milicicd's patch #6351:
returnUrl looks ugly
r1252 by cristian.libardo on Mar 14, 2010   Diff
- refactored rewriting mechanism to
simplify asp.net authorization usage
All revisions of this file

File info

Size: 6304 bytes, 183 lines
Powered by Google Project Hosting