public void Init(HttpApplication context)
 {
     context.BeginRequest += CreateEventHandler(OnBeginRequest);
     context.EndRequest += CreateEventHandler(OnEndRequest);
     context.AuthenticateRequest += CreateEventHandler(OnAuthenticateRequest);
     context.Error += CreateEventHandler(OnError);
     context.MapRequestHandler += CreateEventHandler(OnMapRequest);
     var asynchelper = new EventHandlerTaskAsyncHelper(OnBeginRequestAsync);
     context.AddOnBeginRequestAsync(asynchelper.BeginEventHandler, asynchelper.EndEventHandler);
 }
Exemple #2
0
        public void Init(HttpApplication init)
        {
            var appHandler = AppHandlerSingleton.Instance;

            init.AddOnBeginRequestAsync(
                (sender, args, callback, state) =>
                {
                    var httpContext = ((HttpApplication) sender).Context;
                    return appHandler.BeginProcessRequest(new HttpContextWrapper(httpContext), callback, state);
                },
                appHandler.EndProcessRequest);
        }
Exemple #3
0
 public void Init(HttpApplication context)
 {
     _context = context;
     context.AddOnBeginRequestAsync(BeginBeginRequest, EndBeginRequest, null);
 }
Exemple #4
0
        void IHttpModule.Init(HttpApplication context)
        {
            var applicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            var lines = File.ReadLines(Path.Combine(applicationBase, "config.taco"));

            var builder = new Builder();
            builder.ParseLines(lines);
            var app = builder.ToApp();

            var standardError = Console.OpenStandardError(); //TODO: correct place for default output?

            context.AddOnBeginRequestAsync(
                (sender, e, callback, state) => {
                    var httpApplication = (HttpApplication)sender;
                    var httpContext = httpApplication.Context;
                    var httpRequest = httpContext.Request;
                    var httpResponse = httpContext.Response;
                    var serverVariables = httpRequest.ServerVariables;

                    httpResponse.Buffer = false;

                    var env = serverVariables.AllKeys
                        .ToDictionary(key => key, key => (object)serverVariables[key]);

                    new Environment(env) {
                        Version = new Version(1, 0),
                        UrlScheme = httpRequest.Url.Scheme,
                        Body = new RequestBody(httpRequest.GetBufferlessInputStream()),
                        Errors = standardError,
                        Multithread = true,
                        Multiprocess = false,
                        RunOnce = false,
                        Session = new Session(httpContext.Session),
                        Logger = (eventType, message, exception) => { }, //TODO: any default logger for this host?
                    };

                    var scriptName = httpRequest.ApplicationPath;
                    if (scriptName == "/")
                        scriptName = "";
                    var pathInfo = httpRequest.Url.AbsolutePath;
                    if (pathInfo == "")
                        pathInfo = "/";

                    env["SCRIPT_NAME"] = scriptName;
                    env["PATH_INFO"] = pathInfo.Substring(scriptName.Length);

                    var task = Task.Factory.StartNew(_ => {
                        app.InvokeAsync(env)
                            .Then((status, headers, body) => {
                                httpResponse.StatusCode = status;
                                foreach (var header in Split(headers)) {
                                    httpResponse.AppendHeader(header.Key, header.Value);
                                }
                                var writer = new ResponseBody(
                                    httpResponse.ContentEncoding,
                                    httpResponse.OutputStream.Write,
                                    httpResponse.OutputStream.BeginWrite,
                                    httpResponse.OutputStream.EndWrite);
                                body.ForEach(writer.Write).Then(httpResponse.End);
                            });
                    }, state, TaskCreationOptions.PreferFairness);

                    if (callback != null)
                        task.Finally(() => callback(task));

                    return task;
                },
                ar => ((Task)ar).Wait());
        }
Exemple #5
0
        public void Init(HttpApplication init)
        {
            init.AddOnBeginRequestAsync(
                (sender, args, callback, state) =>
                {
                    var taskCompletionSource = new TaskCompletionSource<Action>(state);
                    if (callback != null)
                        taskCompletionSource.Task.ContinueWith(task => callback(task), TaskContinuationOptions.ExecuteSynchronously);

                    var httpContext = ((HttpApplication) sender).Context;
                    var httpRequest = httpContext.Request;
                    var serverVariables = new ServerVariables(httpRequest.ServerVariables);

                    var appRelCurExeFilPat = httpRequest.AppRelativeCurrentExecutionFilePath.Substring(1);

                    var env = new Dictionary<string, object>();
                    new Environment(env)
                    {
                        Version = "1.0",
                        Method = httpRequest.HttpMethod,
                        UriScheme = httpRequest.Url.Scheme,
                        ServerName = serverVariables.ServerName,
                        ServerPort = serverVariables.ServerPort,
                        BaseUri = "",
                        RequestUri = appRelCurExeFilPat + "?" + serverVariables.QueryString,
                        Headers = httpRequest.Headers.AllKeys.ToDictionary(x => x, x => httpRequest.Headers.Get(x)),
                        Body = (next, error, complete) =>
                        {
                            var stream = httpContext.Request.InputStream;
                            var buffer = new byte[4096];
                            var continuation = new AsyncCallback[1];
                            bool[] stopped = {false};
                            continuation[0] = result =>
                            {
                                if (result != null && result.CompletedSynchronously) return;
                                try
                                {
                                    for (;;)
                                    {
                                        if (result != null)
                                        {
                                            var count = stream.EndRead(result);
                                            if (stopped[0]) return;
                                            if (count <= 0)
                                            {
                                                complete();
                                                return;
                                            }
                                            var data = new ArraySegment<byte>(buffer, 0, count);
                                            if (next(data, () => continuation[0](null))) return;
                                        }

                                        if (stopped[0]) return;
                                        result = stream.BeginRead(buffer, 0, buffer.Length, continuation[0], null);
                                        if (!result.CompletedSynchronously) return;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    error(ex);
                                }
                            };
                            continuation[0](null);
                            return () => { stopped[0] = true; };
                        },
                    };
                    Host.Call(
                        env,
                        taskCompletionSource.SetException,
                        (status, headers, body) =>
                        {
                            try
                            {
                                httpContext.Response.Status = status;
                                foreach (var header in headers.SelectMany(kv => kv.Value.Split("\r\n".ToArray(), StringSplitOptions.RemoveEmptyEntries).Select(v => new {kv.Key, Value = v})))
                                {
                                    httpContext.Response.AddHeader(header.Key, header.Value);
                                }
                                if (body == null)
                                {
                                    taskCompletionSource.SetResult(() => httpContext.Response.End());
                                    return;
                                }
                                var stream = httpContext.Response.OutputStream;
                                body(
                                    (data, continuation) =>
                                    {
                                        try
                                        {
                                            if (continuation == null)
                                            {
                                                stream.Write(data.Array, data.Offset, data.Count);
                                                return false;
                                            }
                                            var sr = stream.BeginWrite(data.Array, data.Offset, data.Count, ar =>
                                            {
                                                if (ar.CompletedSynchronously) return;
                                                try
                                                {
                                                    stream.EndWrite(ar);
                                                }
                                                catch (Exception ex)
                                                {
                                                    taskCompletionSource.SetException(ex);
                                                }
                                                continuation();
                                            }, null);
                                            if (sr.CompletedSynchronously)
                                            {
                                                stream.EndWrite(sr);
                                                return false;
                                            }

                                            return true;
                                        }
                                        catch (Exception ex)
                                        {
                                            taskCompletionSource.SetException(ex);
                                            return false;
                                        }
                                    },
                                    taskCompletionSource.SetException,
                                    () => taskCompletionSource.SetResult(() => httpContext.Response.End()));
                            }
                            catch (Exception ex)
                            {
                                taskCompletionSource.SetException(ex);
                            }
                        });
                    return taskCompletionSource.Task;
                },
                ar => ((Task<Action>) ar).Result());
        }
 public void OnContext( HttpApplication application )
 {
    application.AddOnBeginRequestAsync( OnRequestBegun, OnRequestCompleted, application );
 }
Exemple #7
0
		public void Events_Deny_Unrestricted ()
		{
			HttpApplication app = new HttpApplication ();
			app.Disposed += new EventHandler (Handler);
			app.Error += new EventHandler (Handler);
			app.PreSendRequestContent += new EventHandler (Handler);
			app.PreSendRequestHeaders += new EventHandler (Handler);
			app.AcquireRequestState += new EventHandler (Handler);
			app.AuthenticateRequest += new EventHandler (Handler);
			app.AuthorizeRequest += new EventHandler (Handler);
			app.BeginRequest += new EventHandler (Handler);
			app.EndRequest += new EventHandler (Handler);
			app.PostRequestHandlerExecute += new EventHandler (Handler);
			app.PreRequestHandlerExecute += new EventHandler (Handler);
			app.ReleaseRequestState += new EventHandler (Handler);
			app.ResolveRequestCache += new EventHandler (Handler);
			app.UpdateRequestCache += new EventHandler (Handler);

			app.AddOnAcquireRequestStateAsync (null, null);
			app.AddOnAuthenticateRequestAsync (null, null);
			app.AddOnAuthorizeRequestAsync (null, null);
			app.AddOnBeginRequestAsync (null, null);
			app.AddOnEndRequestAsync (null, null);
			app.AddOnPostRequestHandlerExecuteAsync (null, null);
			app.AddOnPreRequestHandlerExecuteAsync (null, null);
			app.AddOnReleaseRequestStateAsync (null, null);
			app.AddOnResolveRequestCacheAsync (null, null);
			app.AddOnUpdateRequestCacheAsync (null, null);

			app.Disposed -= new EventHandler (Handler);
			app.Error -= new EventHandler (Handler);
			app.PreSendRequestContent -= new EventHandler (Handler);
			app.PreSendRequestHeaders -= new EventHandler (Handler);
			app.AcquireRequestState -= new EventHandler (Handler);
			app.AuthenticateRequest -= new EventHandler (Handler);
			app.AuthorizeRequest -= new EventHandler (Handler);
			app.BeginRequest -= new EventHandler (Handler);
			app.EndRequest -= new EventHandler (Handler);
			app.PostRequestHandlerExecute -= new EventHandler (Handler);
			app.PreRequestHandlerExecute -= new EventHandler (Handler);
			app.ReleaseRequestState -= new EventHandler (Handler);
			app.ResolveRequestCache -= new EventHandler (Handler);
			app.UpdateRequestCache -= new EventHandler (Handler);
#if NET_2_0
			app.PostAuthenticateRequest += new EventHandler (Handler);
			app.PostAuthorizeRequest += new EventHandler (Handler);
			app.PostResolveRequestCache += new EventHandler (Handler);
			app.PostMapRequestHandler += new EventHandler (Handler);
			app.PostAcquireRequestState += new EventHandler (Handler);
			app.PostReleaseRequestState += new EventHandler (Handler);
			app.PostUpdateRequestCache += new EventHandler (Handler);

			app.AddOnPostAuthenticateRequestAsync (null, null);
			app.AddOnPostAuthenticateRequestAsync (null, null, null);
			app.AddOnPostAuthorizeRequestAsync (null, null);
			app.AddOnPostAuthorizeRequestAsync (null, null, null);
			app.AddOnPostResolveRequestCacheAsync (null, null);
			app.AddOnPostResolveRequestCacheAsync (null, null, null);
			app.AddOnPostMapRequestHandlerAsync (null, null);
			app.AddOnPostMapRequestHandlerAsync (null, null, null);
			app.AddOnPostAcquireRequestStateAsync (null, null);
			app.AddOnPostAcquireRequestStateAsync (null, null, null);
			app.AddOnPostReleaseRequestStateAsync (null, null);
			app.AddOnPostReleaseRequestStateAsync (null, null, null);
			app.AddOnPostUpdateRequestCacheAsync (null, null);
			app.AddOnPostUpdateRequestCacheAsync (null, null, null);

			app.AddOnAcquireRequestStateAsync (null, null, null);
			app.AddOnAuthenticateRequestAsync (null, null, null);
			app.AddOnAuthorizeRequestAsync (null, null, null);
			app.AddOnBeginRequestAsync (null, null, null);
			app.AddOnEndRequestAsync (null, null, null);
			app.AddOnPostRequestHandlerExecuteAsync (null, null, null);
			app.AddOnPreRequestHandlerExecuteAsync (null, null, null);
			app.AddOnReleaseRequestStateAsync (null, null, null);
			app.AddOnResolveRequestCacheAsync (null, null, null);
			app.AddOnUpdateRequestCacheAsync (null, null, null);

			app.PostAuthenticateRequest -= new EventHandler (Handler);
			app.PostAuthorizeRequest -= new EventHandler (Handler);
			app.PostResolveRequestCache -= new EventHandler (Handler);
			app.PostMapRequestHandler -= new EventHandler (Handler);
			app.PostAcquireRequestState -= new EventHandler (Handler);
			app.PostReleaseRequestState -= new EventHandler (Handler);
			app.PostUpdateRequestCache -= new EventHandler (Handler);
#endif
		}
Exemple #8
0
 /// <summary>
 /// Initialises the module.
 /// </summary>
 /// <param name="context">Application context.</param>
 public void Init(HttpApplication context)
 {
     context.BeginRequest += new EventHandler(Context_AuthenticateRequest);
     context.AddOnBeginRequestAsync(AsyncBeginRequest,AsyncBeginRequestEnd);
 }