System.Web.Routing: RequestContext, RouteTable, RouteValueDictionary and VirtualPathD...
[mono.git] / mcs / class / System.Web / System.Web.SessionState_2.0 / SessionIDManager.cs
1 //
2 // System.Web.Compilation.SessionStateItemCollection
3 //
4 // Authors:
5 //   Marek Habersack (grendello@gmail.com)
6 //
7 // (C) 2006 Marek Habersack
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System.Web;
31 using System.Web.Configuration;
32 using System.Web.Util;
33
34 namespace System.Web.SessionState 
35 {
36         public class SessionIDManager : ISessionIDManager
37         {
38                 SessionStateSection config;
39                 
40                 public SessionIDManager ()
41                 {
42                 }
43
44                 public static int SessionIDMaxLength {
45                         get { return 80; }
46                 }
47
48                 // Todo: find use for the context parameter?
49                 public virtual string CreateSessionID (HttpContext context)
50                 {
51                         return SessionId.Create ();
52                 }
53
54                 public virtual string Decode (string id)
55                 {
56                         return HttpUtility.UrlDecode (id);
57                 }
58
59                 public virtual string Encode (string id)
60                 {
61                         return HttpUtility.UrlEncode (id);
62                 }
63                 
64                 public string GetSessionID (HttpContext context)
65                 {
66                         string ret = null;
67
68                         if (SessionStateModule.IsCookieLess (context, config)) {
69                                 string tmp = context.Request.Headers [SessionStateModule.HeaderName];
70                                 if (tmp != null)
71                                         ret = Decode (tmp);
72                         } else {
73                                 HttpCookie cookie = context.Request.Cookies [config.CookieName];
74                                 if (cookie != null)
75                                         ret = Decode (cookie.Value);
76                         }
77                         
78                         if (ret != null && ret.Length > SessionIDMaxLength)
79                                 throw new HttpException ("The length of the session-identifier value retrieved from the HTTP request exceeds the SessionIDMaxLength value.");
80                         if (!Validate (ret))
81                                 throw new HttpException ("Invalid session ID");
82                         
83                         return ret;
84                 }
85
86                 public void Initialize ()
87                 {
88                         config = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
89                 }
90
91                 public bool InitializeRequest (HttpContext context, bool suppressAutoDetectRedirect, out bool supportSessionIDReissue)
92                 {
93                         // TODO: Implement AutoDetect handling
94                         if (config.CookieLess) {
95                                 supportSessionIDReissue = true;
96                                 return false;
97                         } else {
98                                 supportSessionIDReissue = false;
99                                 return false;
100                         }
101                 }
102
103                 public void RemoveSessionID (HttpContext context)
104                 {
105                         context.Response.Cookies.Remove(config.CookieName);
106                 }
107
108                 // TODO: add code to check whether the response has already been sent
109                 public void SaveSessionID (HttpContext context, string id, out bool redirected, out bool cookieAdded)
110                 {
111                         if (!Validate (id))
112                                 throw new HttpException ("Invalid session ID");
113
114                         HttpRequest request = context.Request;
115                         if (!SessionStateModule.IsCookieLess (context, config)) {
116                                 HttpCookie cookie = new HttpCookie (config.CookieName, id);
117                                 cookie.Path = request.ApplicationPath;
118                                 context.Response.AppendCookie (cookie);
119                                 cookieAdded = true;
120                                 redirected = false;
121                         } else {
122                                 request.SetHeader (SessionStateModule.HeaderName, id);
123                                 cookieAdded = false;
124                                 redirected = true;
125                                 UriBuilder newUri = new UriBuilder (request.Url);
126                                 newUri.Path = UrlUtils.InsertSessionId (id, request.FilePath);
127                                 context.Response.Redirect (newUri.Uri.PathAndQuery, false);
128                         }
129                 }
130
131                 public virtual bool Validate (string id)
132                 {
133                         return true;
134                 }
135         }
136 }