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
using System;
using System.Threading;
using System.Collections;
using System.Security.Principal;
using System.IO;
using System.Web;

namespace N2.Web
{
/// <summary>
/// A thread local context. It's used to store the nhibernate session
/// instance in situations where we don't have a request available such
/// as code executed by the scheduler.
/// </summary>
public class ThreadContext : IWebContext, IDisposable
{
private static string baseDirectory;

[ThreadStatic] ContentItem currentPage;
[ThreadStatic] PathData currentPath;
[ThreadStatic] static IDictionary items;
[ThreadStatic] Url localUrl = new Url("/");
[ThreadStatic] Url url = new Url("http://localhost");

static ThreadContext()
{
baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
int binIndex = baseDirectory.IndexOf("\\bin\\");
if (binIndex >= 0)
baseDirectory = baseDirectory.Substring(0, binIndex);
else if (baseDirectory.EndsWith("\\bin"))
baseDirectory = baseDirectory.Substring(0, baseDirectory.Length - 4);
}


public virtual IDictionary RequestItems
{
get { return items ?? (items = new Hashtable()); }
}

public virtual IPrincipal User
{
get { return Thread.CurrentPrincipal; }
}

public virtual bool IsWeb
{
get { return false; }
}

public ContentItem CurrentPage
{
get { return currentPage; }
set { currentPage = value; }
}

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

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

public virtual void Close()
{
string[] keys = new string[RequestItems.Keys.Count];
RequestItems.Keys.CopyTo(keys, 0);

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

/// <summary>The requested url, e.g. http://n2cms.com/path/to/a/page.aspx?some=query.</summary>
public virtual Url Url
{
get { return url; }
set { url = value; }
}

public virtual string MapPath(string path)
{
path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');
return Path.Combine(baseDirectory, path);
}

public virtual IHttpHandler Handler
{
get { throw new NotSupportedException("In thread context. No handler when not running in http web context."); }
}

public virtual HttpRequest Request
{
get { throw new NotSupportedException("In thread context. No handler when not running in http web context."); }
}

public virtual HttpResponse Response
{
get { throw new NotSupportedException("In thread context. No handler when not running in http web context."); }
}

public virtual string PhysicalPath
{
get { return MapPath(Url.Path); }
}

public virtual void RewritePath(string path)
{
throw new NotSupportedException("RewritePath not supported in thread context. No handler when not running in http web context.");
}

public virtual void RewritePath(string path, string query)
{
throw new NotSupportedException("RewritePath not supported in thread context. No handler when not running in http web context.");
}

public virtual string ToAbsolute(string virtualPath)
{
return virtualPath.TrimStart('~');
}

public virtual string ToAppRelative(string virtualPath)
{
return "~" + ToAbsolute(virtualPath);
}

[Obsolete]
public void TransferRequest(string path)
{
throw new NotSupportedException("TransferRequest is not supported in thread context. No handler when not running in http web context.");
}

/// <summary>Doen't do anything.</summary>
public void ClearError()
{
}

#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
r1385 by wenqiang.w on Jun 18, 2010   Diff
Fixed the new implementation of
Url.ServerUrl causes error in
ThreadContext.
All revisions of this file

File info

Size: 4717 bytes, 163 lines
Powered by Google Project Hosting