Exemplo n.º 1
0
        private void GetContextCallback(IAsyncResult asyncResult)
        {
            HttpListenerContext ctx = null;
            try
            {
                try
                {
                    ctx = httpListener.EndGetContext(asyncResult);
                }
                finally
                {
                    BeginGetContext();
                }

                if (maxPacket < ctx.Request.ContentLength64)
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "request-too-large");
                    return;
                }

                var url = ctx.Request.Url;
                log.DebugFormat("{0}: Begin process http request {1}", Name, url);

                if (url.AbsolutePath == bindUri.AbsolutePath)
                {
                    var body = BoshXmppHelper.ReadBodyFromRequest(ctx);
                    if (body == null)
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                        return;
                    }

                    var connection = GetXmppConnection(body.Sid) as BoshXmppConnection;

                    if (!string.IsNullOrEmpty(body.Sid) && connection == null)
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "item-not-found");
                        return;
                    }

                    if (connection == null)
                    {
                        connection = new BoshXmppConnection();
                        AddNewXmppConnection(connection);
                    }

                    connection.ProcessBody(body, ctx);
                }
                else if ((url.AbsolutePath == domainUri.AbsolutePath || url.AbsolutePath == "/crossdomain.xml") && ctx.Request.HttpMethod == "GET")
                {
                    SendPolicy(ctx);
                }
                else
                {
                    IHttpResponder responder;
                    if (apiUri != null && url.AbsolutePath.Length > apiUri.AbsolutePath.Length)
                    {
                        var urlPath = url.AbsolutePath.Substring(apiUri.AbsolutePath.Length).Trim('/');
                        if ((responder = httpResponders.FirstOrDefault(x => x.Path == urlPath)) != null)
                        {
                            //To responder
                            try
                            {
                                var responce = responder.Process(ctx.Request).ToString();
                                BoshXmppHelper.SendAndCloseResponse(ctx, responce);
                            }
                            catch (UnauthorizedAccessException)
                            {
                                ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                                BoshXmppHelper.TerminateBoshSession(ctx, "unathorized");
                            }
                            catch (Exception)
                            {
                                ctx.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                                BoshXmppHelper.TerminateBoshSession(ctx, "server-error");
                            }
                        }
                        else
                        {
                            BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                        }
                    }
                    else
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                    }
                }
            }
            catch (ObjectDisposedException) { }
            catch (Exception e)
            {
                if (ctx != null) BoshXmppHelper.TerminateBoshSession(ctx, "internal-server-error");
                if (Started) log.ErrorFormat("{0}: Error GetContextCallback: {1}", Name, e);
            }
        }
Exemplo n.º 2
0
        private void GetContextCallback(IAsyncResult asyncResult)
        {
            HttpListenerContext ctx = null;

            try
            {
                try
                {
                    ctx = httpListener.EndGetContext(asyncResult);
                }
                finally
                {
                    BeginGetContext();
                }

                if (maxPacket < ctx.Request.ContentLength64)
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "request-too-large");
                    return;
                }

                if (ctx.Request.Url.AbsolutePath == bindUri.AbsolutePath)
                {
                    var body = BoshXmppHelper.ReadBodyFromRequest(ctx);
                    if (body == null)
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                        return;
                    }

                    var connection = GetXmppConnection(body.Sid) as BoshXmppConnection;

                    if (!string.IsNullOrEmpty(body.Sid) && connection == null)
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "item-not-found");
                        return;
                    }

                    if (connection == null)
                    {
                        connection = new BoshXmppConnection(body);
                        log.DebugFormat("Create new Bosh connection Id = {0}", connection.Id);

                        AddNewXmppConnection(connection);
                    }
                    connection.ProcessBody(body, ctx);
                }
                else
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                    log.DebugFormat("{0}: Unknown uri request {1}", Name, ctx.Request.Url);
                }
            }
            catch (Exception e)
            {
                BoshXmppHelper.TerminateBoshSession(ctx, "internal-server-error");
                if (Started && !(e is ObjectDisposedException))
                {
                    log.ErrorFormat("{0}: Error GetContextCallback: {1}", Name, e);
                }
            }
        }
        private void GetContextCallback(IAsyncResult asyncResult)
        {
            HttpListenerContext ctx = null;
            try
            {
                try
                {
                    ctx = httpListener.EndGetContext(asyncResult);
                }
                finally
                {
                    BeginGetContext();
                }

                if (maxPacket < ctx.Request.ContentLength64)
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "request-too-large");
                    return;
                }

                var url = ctx.Request.Url;
                log.DebugFormat("{0}: Begin process http request {1}", Name, url);

                if (url.AbsolutePath == bindUri.AbsolutePath)
                {
                    var body = BoshXmppHelper.ReadBodyFromRequest(ctx);
                    if (body == null)
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                        return;
                    }

                    var connection = GetXmppConnection(body.Sid) as BoshXmppConnection;

                    if (!string.IsNullOrEmpty(body.Sid) && connection == null)
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "item-not-found");
                        return;
                    }

                    if (connection == null)
                    {
                        connection = new BoshXmppConnection();
                        AddNewXmppConnection(connection);
                    }

                    connection.ProcessBody(body, ctx);
                }
                else if ((url.AbsolutePath == domainUri.AbsolutePath || url.AbsolutePath == "/crossdomain.xml") && ctx.Request.HttpMethod == "GET")
                {
                    SendPolicy(ctx);
                }
                else
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                }
            }
            catch (ObjectDisposedException) { }
            catch (Exception e)
            {
                if (ctx != null) BoshXmppHelper.TerminateBoshSession(ctx, "internal-server-error");
                if (Started) log.ErrorFormat("{0}: Error GetContextCallback: {1}", Name, e);
            }
        }
Exemplo n.º 4
0
        private void GetContextCallback(IAsyncResult asyncResult)
        {
            HttpListenerContext ctx = null;

            try
            {
                try
                {
                    ctx = httpListener.EndGetContext(asyncResult);
                }
                finally
                {
                    BeginGetContext();
                }

                if (maxPacket < ctx.Request.ContentLength64)
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "request-too-large");
                    return;
                }

                var url = ctx.Request.Url;
                log.DebugFormat("{0}: Begin process http request {1}", Name, url);

                if (url.AbsolutePath == bindUri.AbsolutePath)
                {
                    var body = BoshXmppHelper.ReadBodyFromRequest(ctx);
                    if (body == null)
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                        return;
                    }

                    var connection = GetXmppConnection(body.Sid) as BoshXmppConnection;

                    if (!string.IsNullOrEmpty(body.Sid) && connection == null)
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "item-not-found");
                        return;
                    }

                    if (connection == null)
                    {
                        connection = new BoshXmppConnection();
                        AddNewXmppConnection(connection);
                    }

                    connection.ProcessBody(body, ctx);
                }
                else if ((url.AbsolutePath == domainUri.AbsolutePath || url.AbsolutePath == "/crossdomain.xml") && ctx.Request.HttpMethod == "GET")
                {
                    SendPolicy(ctx);
                }
                else
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                }
            }
            catch (ObjectDisposedException) { }
            catch (Exception e)
            {
                if (ctx != null)
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "internal-server-error");
                }
                if (Started)
                {
                    log.ErrorFormat("{0}: Error GetContextCallback: {1}", Name, e);
                }
            }
        }
Exemplo n.º 5
0
        private void GetContextCallback(IAsyncResult asyncResult)
        {
            HttpListenerContext ctx = null;

            try
            {
                try
                {
                    ctx = httpListener.EndGetContext(asyncResult);
                }
                finally
                {
                    BeginGetContext();
                }

                if (maxPacket < ctx.Request.ContentLength64)
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "request-too-large");
                    return;
                }

                var url = ctx.Request.Url;
                log.DebugFormat("{0}: Begin process http request {1}", Name, url);

                if (url.AbsolutePath == bindUri.AbsolutePath)
                {
                    var body = BoshXmppHelper.ReadBodyFromRequest(ctx);
                    if (body == null)
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                        return;
                    }

                    var connection = GetXmppConnection(body.Sid) as BoshXmppConnection;

                    if (!string.IsNullOrEmpty(body.Sid) && connection == null)
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "item-not-found");
                        return;
                    }

                    if (connection == null)
                    {
                        connection = new BoshXmppConnection();
                        AddNewXmppConnection(connection);
                    }

                    connection.ProcessBody(body, ctx);
                }
                else if ((url.AbsolutePath == domainUri.AbsolutePath || url.AbsolutePath == "/crossdomain.xml") && ctx.Request.HttpMethod == "GET")
                {
                    SendPolicy(ctx);
                }
                else
                {
                    IHttpResponder responder;
                    if (apiUri != null && url.AbsolutePath.Length > apiUri.AbsolutePath.Length)
                    {
                        var urlPath = url.AbsolutePath.Substring(apiUri.AbsolutePath.Length).Trim('/');
                        if ((responder = httpResponders.FirstOrDefault(x => x.Path == urlPath)) != null)
                        {
                            //To responder
                            try
                            {
                                var responce = responder.Process(ctx.Request).ToString();
                                BoshXmppHelper.SendAndCloseResponse(ctx, responce);
                            }
                            catch (UnauthorizedAccessException)
                            {
                                ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                                BoshXmppHelper.TerminateBoshSession(ctx, "unathorized");
                            }
                            catch (Exception)
                            {
                                ctx.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                                BoshXmppHelper.TerminateBoshSession(ctx, "server-error");
                            }
                        }
                        else
                        {
                            BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                        }
                    }
                    else
                    {
                        BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                    }
                }
            }
            catch (ObjectDisposedException) { }
            catch (Exception e)
            {
                if (ctx != null)
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "internal-server-error");
                }
                if (Started)
                {
                    log.ErrorFormat("{0}: Error GetContextCallback: {1}", Name, e);
                }
            }
        }