📄 definitions.cs
字号:
/*
Copyright 2006 Christian Gross http://www.devspace.com
From the book Ajax Patterns and Best Practices
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Web;
using Devspace.Commons.Tracer;
using System.Threading;
using Devspace.Ajax.Permutations;
using System.Text.RegularExpressions;
using System.Collections;
namespace Devspace.Ajax.PersistentCommunications {
#region HttpHandlerBase
public abstract class HttpHandlerBase : IHttpHandler {
protected virtual bool DoPermutations {
get {
return false;
}
}
protected virtual bool IsMimeSupported( string mimeType) {
return false;
}
protected string _mimeType;
public virtual void ProcessRequest(HttpContext context) {
try {
HttpRequest req = context.Request;
HttpResponse resp = context.Response;
if( this.DoPermutations) {
string[] elements = (string[])req.AcceptTypes.Clone();
Array.Sort(elements, new CompareMimeTypes());
Regex semiColon = new Regex(";");
resp.StatusCode = 0;
foreach (string type in elements) {
String[] buffers = semiColon.Split(type);
if( IsMimeSupported( buffers[ 0])) {
_mimeType = buffers[ 0];
if (req.HttpMethod == "GET") {
DoGet(req, resp);
}
else if (req.HttpMethod == "POST") {
DoPost(req, resp);
}
if( resp.StatusCode != 0) {
break;
}
}
}
}
else {
if (req.HttpMethod == "GET") {
DoGet(req, resp);
}
else if (req.HttpMethod == "POST") {
DoPost(req, resp);
}
}
}
catch ( Exception e) {
GenerateOutput.Write("HttpHandlerBase.ProcessRequest", e.Message);
GenerateOutput.Write("HttpHandlerBase.DoPost", e.StackTrace);
throw e;
}
}
public bool IsReusable {
get {
return false;
}
}
protected internal abstract void DoGet(HttpRequest req, HttpResponse resp);
protected internal abstract void DoPost(HttpRequest req, HttpResponse resp);
protected internal string GetVariable(HttpRequest req, string identifier) {
string retval = req.QueryString.Get(identifier);
if (retval == null || retval.Length == 0) {
retval = req.Form.Get(identifier);
}
return retval;
}
}
#endregion
public interface IUserIdentification {
string Identifier {
get;
}
bool IsIdentified {
get;
}
}
public interface IUserIdentificationResolver {
IUserIdentification IdentifyUser(HttpContext context);
}
internal class UserResolverImplementation : IUserIdentificationResolver {
protected internal string constCookieIdentifier = "user-identifier";
protected internal string GetVariable(HttpRequest req, string identifier) {
string retval = req.QueryString.Get(identifier);
if (retval == null || retval.Length == 0) {
retval = req.Form.Get(identifier);
}
return retval;
}
private class UserImplementation : IUserIdentification {
bool _isIdentified;
string _identifier;
public UserImplementation(string identifier, bool isIdentified) {
_identifier = identifier;
_isIdentified = isIdentified;
}
public String Identifier {
get {
return _identifier;
}
}
public bool IsIdentified {
get {
return _isIdentified;
}
}
}
public IUserIdentification IdentifyUser(HttpContext context) {
HttpCookie cookie = context.Request.Cookies.Get(constCookieIdentifier);
if (cookie == null) {
GenerateOutput.Write("UserResolverImplementation.IdentifyUser", "User could not be identified trying parameters");
string useridentifier = GetVariable(context.Request, constCookieIdentifier);
if (useridentifier != null && useridentifier.Length > 0) {
GenerateOutput.Write("UserResolverImplementation.IdentifyUser", "Writing Cookie");
cookie = new HttpCookie(constCookieIdentifier, useridentifier);
context.Response.AppendCookie(cookie);
return new UserImplementation(useridentifier, true);
}
else {
GenerateOutput.Write("UserResolverImplementation.IdentifyUser", "User could not be identified");
return new UserImplementation("", false);
}
}
else {
GenerateOutput.Write("UserResolverImplementation.IdentifyUser", "User identified using cookie");
return new UserImplementation(cookie.Value, true);
}
}
}
public abstract class AuthenticatedHttpHandlerBase : HttpHandlerBase {
protected internal IUserIdentificationResolver _resolver;
protected internal IUserIdentification _identifiedUser;
public AuthenticatedHttpHandlerBase() {
_identifiedUser = null;
_resolver = null;
}
public AuthenticatedHttpHandlerBase(IUserIdentificationResolver resolver) {
_identifiedUser = null;
AssignUserResolver(resolver);
}
public void AssignUserResolver(IUserIdentificationResolver resolver) {
_resolver = resolver;
}
protected internal void IdentifyUser(HttpContext context) {
if (_identifiedUser == null) {
GenerateOutput.Write("AuthenticatedHttpHandlerBase.IdentifyUser", "Identifying the user");
if (_resolver == null) {
throw new Exception("Resolver is null");
}
_identifiedUser = _resolver.IdentifyUser(context);
}
else {
GenerateOutput.Write("AuthenticatedHttpHandlerBase.IdentifyUser", "User has been identified");
}
}
protected internal virtual bool CanContinueUnidentified {
get {
return true;
}
}
public override void ProcessRequest(HttpContext context) {
IdentifyUser(context);
if (!_identifiedUser.IsIdentified) {
if (!CanContinueUnidentified) {
GenerateOutput.Write("AuthenticatedHttpHandlerBase.ProcessRequest", "User is not identified");
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.Output.WriteLine("User has not been identified, please identify yourself");
return;
}
}
GenerateOutput.Write("AuthenticatedHttpHandlerBase.ProcessRequest", "User is identified");
base.ProcessRequest(context);
}
}
public abstract class RedirectionHttpHandlerBase : AuthenticatedHttpHandlerBase {
protected internal abstract string BaseURL {
get;
}
protected internal abstract string GetURLEnding();
public virtual int RedirectionStatusCode {
get {
return 201;
}
}
public override void ProcessRequest(HttpContext context) {
GenerateOutput.Write("RedirectionHttpHandlerBase.ProcessRequest", "Processing redirection");
IdentifyUser(context);
if (!_identifiedUser.IsIdentified) {
base.ProcessRequest(context);
return;
}
HttpResponse resp = context.Response;
HttpRequest req = context.Request;
string url = req.Path;
if (url.CompareTo(this.BaseURL) == 0) {
string newURL = this.BaseURL + "/" + GetURLEnding();
GenerateOutput.Write("RedirectionHttpHandlerBase.ProcessRequest", "Doing a redirection (" + newURL + ")");
resp.AddHeader("Location", newURL);
resp.StatusCode = this.RedirectionStatusCode;
resp.StatusDescription = "Redirecting to a user URL";
return;
}
else {
GenerateOutput.Write("RedirectionHttpHandlerBase.ProcessRequest", "Request is redirected");
base.ProcessRequest(context);
}
}
}
public class VersionTracker {
private int _versionNumber;
private int _defaultTimeout;
public VersionTracker(int defaultTimeout) {
_defaultTimeout = defaultTimeout;
_versionNumber = 0;
}
public bool GetNew(long version) {
bool retval = false;
Monitor.Enter(this);
if (_versionNumber > version) {
retval = true;
}
else {
Monitor.Wait(this, _defaultTimeout);
if (_versionNumber > version) {
retval = true;
}
}
Monitor.Exit(this);
return retval;
}
public void Increment() {
Monitor.Enter(this);
_versionNumber ++;
Monitor.Pulse(this);
Monitor.Exit(this);
}
public int VersionNumber {
get {
int retval;
Monitor.Enter(this);
retval = _versionNumber;
Monitor.Exit(this);
return retval;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -