Ejemplo n.º 1
0
        private Task ProcessRequestAsync(HttpListenerContext context)
        {
            try
            {
                Debug.WriteLine("Server: Incoming request to {0}.", context.Request.Url);

                PersistentConnection connection;

                string path = ResolvePath(context.Request.Url);

                if (TryGetConnection(path, out connection))
                {
                    // https://developer.mozilla.org/En/HTTP_Access_Control
                    string origin = context.Request.Headers["Origin"];
                    if (!String.IsNullOrEmpty(origin))
                    {
                        context.Response.AddHeader("Access-Control-Allow-Origin", origin);
                        context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
                    }

                    var request     = new HttpListenerRequestWrapper(context);
                    var response    = new HttpListenerResponseWrapper(context.Response, _disconnectHandler.GetDisconnectToken(context));
                    var hostContext = new HostContext(request, response);

#if NET45
                    hostContext.Items[HostConstants.SupportsWebSockets] = Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2;
#endif

                    if (OnProcessRequest != null)
                    {
                        OnProcessRequest(hostContext);
                    }

#if DEBUG
                    hostContext.Items[HostConstants.DebugMode] = true;
#endif
                    hostContext.Items["System.Net.HttpListenerContext"] = context;
                    hostContext.Items[HostConstants.ShutdownToken]      = _shutdownToken.Token;

                    // Initialize the connection
                    connection.Initialize(DependencyResolver, hostContext);

                    return(connection.ProcessRequestAsync(hostContext));
                }

                if (path.Equals("/clientaccesspolicy.xml", StringComparison.InvariantCultureIgnoreCase))
                {
                    return(context.Response.WriteAsync(Resources.ClientAccessPolicyXml));
                }

                return(context.Response.NotFound());
            }
            catch (Exception ex)
            {
                return(TaskAsyncHelper.FromError(ex));
            }
        }
Ejemplo n.º 2
0
        private Task ProcessRequestAsync(HttpListenerContext context)
        {
            try
            {
                Debug.WriteLine("Server: Incoming request to {0}.", context.Request.Url);

                PersistentConnection connection;

                string path = ResolvePath(context.Request.Url);

                if (TryGetConnection(path, out connection))
                {
                    var cts = new CancellationTokenSource();

                    // https://developer.mozilla.org/En/HTTP_Access_Control
                    string origin = context.Request.Headers["Origin"];
                    if (!String.IsNullOrEmpty(origin))
                    {
                        context.Response.AddHeader("Access-Control-Allow-Origin", origin);
                        context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
                    }

                    var request     = new HttpListenerRequestWrapper(context);
                    var response    = new HttpListenerResponseWrapper(context.Response, () => RegisterForDisconnect(context, cts.Cancel), cts.Token);
                    var hostContext = new HostContext(request, response);

#if NET45
                    hostContext.Items[HostConstants.SupportsWebSockets] = Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2;
#endif

                    if (OnProcessRequest != null)
                    {
                        OnProcessRequest(hostContext);
                    }

#if DEBUG
                    hostContext.Items[HostConstants.DebugMode] = true;
#endif
                    hostContext.Items["System.Net.HttpListenerContext"] = context;

                    // Initialize the connection
                    connection.Initialize(DependencyResolver);

                    return(connection.ProcessRequestAsync(hostContext));
                }

                return(context.Response.NotFound());
            }
            catch (Exception ex)
            {
                return(TaskAsyncHelper.FromError(ex));
            }
        }
Ejemplo n.º 3
0
        private Task ProcessRequestAsync(HttpListenerContext context, CancellationToken token)
        {
            try
            {
                Debug.WriteLine("Incoming request to {0}.", context.Request.Url);

                PersistentConnection connection;

                string path = ResolvePath(context.Request.Url);

                if (TryGetConnection(path, out connection))
                {
                    var request = new HttpListenerRequestWrapper(context.Request);
                    var response = new HttpListenerResponseWrapper(context.Response, token);
                    var hostContext = new HostContext(request, response, context.User);

                    if (OnProcessRequest != null)
                    {
                        OnProcessRequest(hostContext);
                    }

            #if DEBUG
                    hostContext.Items[HostConstants.DebugMode] = true;
            #endif
                    hostContext.Items["System.Net.HttpListenerContext"] = context;

                    // Initialize the connection
                    connection.Initialize(DependencyResolver);

                    return connection.ProcessRequestAsync(hostContext);
                }

                return context.Response.NotFound();
            }
            catch (Exception ex)
            {
                return TaskAsyncHelper.FromError(ex);
            }
        }
Ejemplo n.º 4
0
        private Task ProcessRequestAsync(HttpListenerContext context)
        {
            try
            {
                Debug.WriteLine("Server: Incoming request to {0}.", context.Request.Url);

                PersistentConnection connection;

                string path = ResolvePath(context.Request.Url);

                if (TryGetConnection(path, out connection))
                {
                    var cts = new CancellationTokenSource();

                    // https://developer.mozilla.org/En/HTTP_Access_Control
                    string origin = context.Request.Headers["Origin"];
                    if (!String.IsNullOrEmpty(origin))
                    {
                        context.Response.AddHeader("Access-Control-Allow-Origin", origin);
                        context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
                    }

                    var request = new HttpListenerRequestWrapper(context.Request, context.User);
                    var response = new HttpListenerResponseWrapper(context.Response, () => RegisterForDisconnect(context, cts.Cancel), cts.Token);
                    var hostContext = new HostContext(request, response);

                    if (OnProcessRequest != null)
                    {
                        OnProcessRequest(hostContext);
                    }

            #if DEBUG
                    hostContext.Items[HostConstants.DebugMode] = true;
            #endif
                    hostContext.Items["System.Net.HttpListenerContext"] = context;

                    // Initialize the connection
                    connection.Initialize(DependencyResolver);

                    return connection.ProcessRequestAsync(hostContext);
                }

                return context.Response.NotFound();
            }
            catch (Exception ex)
            {
                return TaskAsyncHelper.FromError(ex);
            }
        }
Ejemplo n.º 5
0
        private Task ProcessRequestAsync(HttpListenerContext context)
        {
            try
            {
                Debug.WriteLine("Server: Incoming request to {0}.", context.Request.Url);

                PersistentConnection connection;

                string path = ResolvePath(context.Request.Url);

                if (TryGetConnection(path, out connection))
                {
                    var cts = new CancellationTokenSource();

                    // https://developer.mozilla.org/En/HTTP_Access_Control
                    string origin = context.Request.Headers["Origin"];
                    if (!String.IsNullOrEmpty(origin))
                    {
                        context.Response.AddHeader("Access-Control-Allow-Origin", origin);
                        context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
                    }

                    var request = new HttpListenerRequestWrapper(context);
                    var response = new HttpListenerResponseWrapper(context.Response, () => RegisterForDisconnect(context, cts.Cancel), cts.Token);
                    var hostContext = new HostContext(request, response);

            #if NET45
                    hostContext.Items[HostConstants.SupportsWebSockets] = Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2;
            #endif

                    if (OnProcessRequest != null)
                    {
                        OnProcessRequest(hostContext);
                    }

            #if DEBUG
                    hostContext.Items[HostConstants.DebugMode] = true;
            #endif
                    hostContext.Items["System.Net.HttpListenerContext"] = context;

                    // Initialize the connection
                    connection.Initialize(DependencyResolver);

                    return connection.ProcessRequestAsync(hostContext);
                }

                if (path.Equals("/clientaccesspolicy.xml", StringComparison.InvariantCultureIgnoreCase))
                {
                    return context.Response.WriteAsync(Resources.ClientAccessPolicyXml);
                }

                return context.Response.NotFound();
            }
            catch (Exception ex)
            {
                return TaskAsyncHelper.FromError(ex);
            }
        }
Ejemplo n.º 6
0
        private Task ProcessRequestAsync(HttpListenerContext context)
        {
            try
            {
                Debug.WriteLine("Server: Incoming request to {0}.", context.Request.Url);

                PersistentConnection connection;

                string path = ResolvePath(context.Request.Url);

                foreach (var embeddedFileHandler in _embeddedFileHandlers)
                {
                    var result = embeddedFileHandler.Handle(path, context);
                    if (result != null)
                    {
                        return result;
                    }
                }

                if (_routingHost.TryGetConnection(path, out connection))
                {
                    // https://developer.mozilla.org/En/HTTP_Access_Control
                    string origin = context.Request.Headers["Origin"];
                    if (!String.IsNullOrEmpty(origin))
                    {
                        context.Response.AddHeader("Access-Control-Allow-Origin", origin);
                        context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
                    }

                    var request = new HttpListenerRequestWrapper(context);
                    var response = new HttpListenerResponseWrapper(context.Response, _disconnectHandler.GetDisconnectToken(context));
                    var hostContext = new HostContext(request, response);

            #if NET45
                    hostContext.Items[HostConstants.SupportsWebSockets] = Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2;
            #endif

                    if (OnProcessRequest != null)
                    {
                        OnProcessRequest(hostContext);
                    }

            #if DEBUG
                    hostContext.Items[HostConstants.DebugMode] = true;
            #endif
                    hostContext.Items["System.Net.HttpListenerContext"] = context;

                    // Initialize the connection
                    connection.Initialize(_routingHost.DependencyResolver);

                    return connection.ProcessRequestAsync(hostContext);
                }

                if (path.Equals("/clientaccesspolicy.xml", StringComparison.InvariantCultureIgnoreCase))
                {
                    using (var stream = typeof(WebAppServer).Assembly.GetManifestResourceStream(typeof(WebAppServer), "clientaccesspolicy.xml"))
                    {
                        if (stream == null)
                        {
                            var response = new HttpResponseMessage(HttpStatusCode.NotFound);
                            return context.SendResponseAsync(response);
                        }
                        var bytes = new byte[1024];
                        int byteCount = stream.Read(bytes, 0, bytes.Length);
                        return context.Response.WriteAsync(new ArraySegment<byte>(bytes, 0, byteCount));
                    }
                }

                HttpRequestMessage requestMessage = context.GetHttpRequestMessage();

                return _webApiServer.PublicSendAsync(requestMessage, _disconnectHandler.GetDisconnectToken(context))
                    .Then(response =>
                    {
                        var responseMessage = response ?? new HttpResponseMessage(HttpStatusCode.InternalServerError) { RequestMessage = requestMessage };
                        return context.SendResponseAsync(responseMessage);
                    });
            }
            catch (Exception ex)
            {
                return TaskAsyncHelper.FromError(ex);
            }
        }