Exemplo n.º 1
0
        private static void onRead(IAsyncResult asyncResult)
        {
            //Discarded unreachable code: IL_00cb
            HttpConnection httpConnection = (HttpConnection)asyncResult.AsyncState;

            if (httpConnection._socket == null)
            {
                return;
            }
            lock (httpConnection._sync)
            {
                if (httpConnection._socket == null)
                {
                    return;
                }
                int num = -1;
                try
                {
                    httpConnection._timer.Change(-1, -1);
                    num = httpConnection._stream.EndRead(asyncResult);
                    httpConnection._requestBuffer.Write(httpConnection._buffer, 0, num);
                    if (httpConnection._requestBuffer.Length > 32768)
                    {
                        httpConnection.SendError("Bad request", 400);
                        httpConnection.Close(force: true);
                        return;
                    }
                }
                catch
                {
                    if (httpConnection._requestBuffer != null && httpConnection._requestBuffer.Length > 0)
                    {
                        httpConnection.SendError();
                    }
                    httpConnection.close();
                    return;
                }
                if (num <= 0)
                {
                    httpConnection.close();
                }
                else if (httpConnection.processInput(httpConnection._requestBuffer.GetBuffer()))
                {
                    if (!httpConnection._context.HasError)
                    {
                        httpConnection._context.Request.FinishInitialization();
                    }
                    if (httpConnection._context.HasError)
                    {
                        httpConnection.SendError();
                        httpConnection.Close(force: true);
                        return;
                    }
                    if (!httpConnection._listener.BindContext(httpConnection._context))
                    {
                        httpConnection.SendError("Invalid host", 400);
                        httpConnection.Close(force: true);
                        return;
                    }
                    HttpListener listener = httpConnection._context.Listener;
                    if (httpConnection._lastListener != listener)
                    {
                        httpConnection.removeConnection();
                        listener.AddConnection(httpConnection);
                        httpConnection._lastListener = listener;
                    }
                    httpConnection._contextWasBound = true;
                    listener.RegisterContext(httpConnection._context);
                }
                else
                {
                    httpConnection._stream.BeginRead(httpConnection._buffer, 0, 8192, onRead, httpConnection);
                }
            }
        }
Exemplo n.º 2
0
        private static void onRead(IAsyncResult asyncResult)
        {
            WebSocketSharp.Net.HttpListener httpListener;
            HttpConnection asyncState = (HttpConnection)asyncResult.AsyncState;

            if (asyncState._socket != null)
            {
                object obj = asyncState._sync;
                Monitor.Enter(obj);
                try
                {
                    if (asyncState._socket != null)
                    {
                        int num    = -1;
                        int length = 0;
                        try
                        {
                            int num1 = asyncState._reuses;
                            if (!asyncState._timeoutCanceled[num1])
                            {
                                asyncState._timer.Change(-1, -1);
                                asyncState._timeoutCanceled[num1] = true;
                            }
                            num = asyncState._stream.EndRead(asyncResult);
                            asyncState._requestBuffer.Write(asyncState._buffer, 0, num);
                            length = (int)asyncState._requestBuffer.Length;
                        }
                        catch (Exception exception1)
                        {
                            Exception exception = exception1;
                            if ((asyncState._requestBuffer == null ? true : asyncState._requestBuffer.Length <= (long)0))
                            {
                                asyncState.close();
                                return;
                            }
                            else
                            {
                                asyncState.SendError(exception.Message, 400);
                                return;
                            }
                        }
                        if (num <= 0)
                        {
                            asyncState.close();
                        }
                        else if (!asyncState.processInput(asyncState._requestBuffer.GetBuffer(), length))
                        {
                            asyncState._stream.BeginRead(asyncState._buffer, 0, 8192, new AsyncCallback(HttpConnection.onRead), asyncState);
                        }
                        else
                        {
                            if (!asyncState._context.HasError)
                            {
                                asyncState._context.Request.FinishInitialization();
                            }
                            if (asyncState._context.HasError)
                            {
                                asyncState.SendError();
                            }
                            else if (asyncState._listener.TrySearchHttpListener(asyncState._context.Request.Url, out httpListener))
                            {
                                if (asyncState._lastListener != httpListener)
                                {
                                    asyncState.removeConnection();
                                    if (httpListener.AddConnection(asyncState))
                                    {
                                        asyncState._lastListener = httpListener;
                                    }
                                    else
                                    {
                                        asyncState.close();
                                        return;
                                    }
                                }
                                asyncState._context.Listener = httpListener;
                                if (asyncState._context.Authenticate())
                                {
                                    if (asyncState._context.Register())
                                    {
                                        asyncState._contextRegistered = true;
                                    }
                                }
                            }
                            else
                            {
                                asyncState.SendError(null, 404);
                            }
                        }
                    }
                }
                finally
                {
                    Monitor.Exit(obj);
                }
            }
        }