Exemplo n.º 1
0
        internal void OnLogAccess(LogAccess data)
        {
            LogAccessEventHandler handler = LogAccess;

            if (handler != null)
            {
                handler(this, new LogAccessEventArgs(data));
            }
        }
Exemplo n.º 2
0
        internal void ProcessRequest()
        {
#if (LOG && !MF && !WindowsCE)
            Console.WriteLine((_client.RemoteEndPoint as IPEndPoint).ToString());
#endif

            using (_client)
            {
                while (true)
                {
                    #region Wait for first byte (used for keep-alive, too)

                    int avail = 0;

                    DateTime maxWait = DateTime.Now.AddMilliseconds(2000);
                    do
                    {
                        try
                        {
                            avail = _client.Available;

                            if (avail == 0)
                            {
                                Thread.Sleep(10);
                            }
                        }
                        catch
                        {
                            break;
                        }
                    }while (avail == 0 && DateTime.Now <= maxWait);

                    #endregion

                    if (avail == 0)
                    {
                        break;
                    }

                    DateTime begin = DateTime.Now;

                    HttpRequest  httpRequest  = new HttpRequest();
                    HttpResponse httpResponse = null;

                    Stream stream;

#if (SSL)
                    if (_server.IsSecure && _server.Certificate != null)
                    {
                        SslStream ssl = null;

                        try
                        {
#if (!MF)
                            ssl = new SslStream(new NetworkStream(_client));
                            ssl.AuthenticateAsServer(_server.Certificate, false, SslProtocols.Default, false);
#else
                            ssl = new SslStream(_client);
                            ssl.AuthenticateAsServer(_server.Certificate, SslVerification.NoVerification, SslProtocols.Default);
#endif
                            stream = ssl;
                        }
                        catch (Exception)
                        {
                            Close();
                            return;
                        }
                    }
                    else
#endif
                    {
                        stream = new NetworkStream(_client);
                    }

                    stream.ReadTimeout  = 200;
                    stream.WriteTimeout = 1000;

                    try
                    {
                        if (!httpRequest.Read(stream, (_client.RemoteEndPoint as IPEndPoint)))
                        {
                            httpResponse = new HttpResponse();
                            httpResponse.RaiseError(HttpStatusCode.ServiceUnavailable);
                            httpResponse.AddHeader("Connection", "close");
                        }
                    }
                    catch (HttpException ex)
                    {
                        httpResponse = new HttpResponse();
                        httpResponse.RaiseError(ex.Message, ex.Code);
                        httpResponse.AddHeader("Connection", "close");
                    }
                    catch (Exception)
                    {
                        httpResponse = new HttpResponse();
                        httpResponse.RaiseError();
                        httpResponse.AddHeader("Connection", "close");
                    }

                    if (httpResponse == null)
                    {
                        httpResponse             = new HttpResponse();
                        httpResponse.HttpVersion = httpRequest.HttpVersion;

                        HttpContext ctx = new HttpContext();
                        ctx.Request  = httpRequest;
                        ctx.Response = httpResponse;

                        try
                        {
                            _handler.ProcessRequest(ctx);
                        }
                        catch (HttpException ex)
                        {
                            httpResponse = new HttpResponse();
                            httpResponse.RaiseError(ex.Message, ex.Code);
                            httpResponse.AddHeader("Connection", "close");
                        }
                        catch (Exception)
                        {
                            httpResponse = new HttpResponse();
                            httpResponse.RaiseError();
                            httpResponse.AddHeader("Connection", "close");
                        }
                    }

                    httpResponse.Write(stream);

                    stream.Flush();

                    LogAccess log = new LogAccess();
                    log.ClientIP      = httpRequest.UserHostAddress;
                    log.BytesReceived = httpRequest.totalBytes;
                    log.BytesSent     = httpResponse.totalBytes;
                    log.Date          = begin;
                    log.Method        = httpRequest.HttpMethod;
                    log.RawUrl        = httpRequest.RawUrl;
                    log.UserAgent     = httpRequest.UserAgent;
                    log.HttpReferer   = httpRequest.Referer;

#if (MF)
                    log.Duration = (DateTime.Now.Ticks - begin.Ticks) / TimeSpan.TicksPerMillisecond;
#else
                    log.Duration = (long)(DateTime.Now - begin).TotalMilliseconds;
#endif

                    _server.OnLogAccess(log);

                    if (httpResponse.Connection == null || httpResponse.Connection != "Keep-Alive")
                    {
                        break;
                    }

                    Thread.Sleep(15);
                }

                Close();
            }
        }
Exemplo n.º 3
0
 public LogAccessEventArgs(LogAccess data)
 {
     Data = data;
 }
Exemplo n.º 4
0
        internal void ProcessRequest()
        {
#if(LOG && !MF && !WindowsCE)
            Console.WriteLine((_client.RemoteEndPoint as IPEndPoint).ToString());
#endif

            using (_client)
            {
                while (true)
                {

                    #region Wait for first byte (used for keep-alive, too)

                    int avail = 0;

                    DateTime maxWait = DateTime.Now.AddMilliseconds(2000);
                    do
                    {
                        try
                        {
                            avail = _client.Available;

                            if (avail == 0)
                                Thread.Sleep(10);
                        }
                        catch
                        {
                            break;
                        }
                    }
                    while (avail == 0 && DateTime.Now <= maxWait);

                    #endregion

                    if (avail == 0)
                        break;

                    DateTime begin = DateTime.Now;

                    HttpRequest httpRequest = new HttpRequest();
                    HttpResponse httpResponse = null;

                    Stream stream;

#if(SSL)
                    if (_server.IsSecure && _server.Certificate != null)
                    {
                        SslStream ssl = null;

                        try
                        {
#if(!MF)
                            ssl = new SslStream(new NetworkStream(_client));
                            ssl.AuthenticateAsServer(_server.Certificate, false, SslProtocols.Default, false);
#else
                            ssl = new SslStream(_client);
                            ssl.AuthenticateAsServer(_server.Certificate, SslVerification.NoVerification, SslProtocols.Default);
#endif
                            stream = ssl;
                        }
                        catch (Exception)
                        {
                            Close();
                            return;
                        }
                    }
                    else
#endif
                    {
                        stream = new NetworkStream(_client);
                    }

                    stream.ReadTimeout = 200;
                    stream.WriteTimeout = 1000;

                    try
                    {
                        if (!httpRequest.Read(stream, (_client.RemoteEndPoint as IPEndPoint)))
                        {
                            httpResponse = new HttpResponse();
                            httpResponse.RaiseError(HttpStatusCode.ServiceUnavailable);
                            httpResponse.AddHeader("Connection", "close");
                        }
                    }
                    catch (HttpException ex)
                    {
                        httpResponse = new HttpResponse();
                        httpResponse.RaiseError(ex.Message, ex.Code);
                        httpResponse.AddHeader("Connection", "close");
                    }
                    catch (Exception)
                    {
                        httpResponse = new HttpResponse();
                        httpResponse.RaiseError();
                        httpResponse.AddHeader("Connection", "close");
                    }

                    if (httpResponse == null)
                    {
                        httpResponse = new HttpResponse();
                        httpResponse.HttpVersion = httpRequest.HttpVersion;

                        HttpContext ctx = new HttpContext();
                        ctx.Request = httpRequest;
                        ctx.Response = httpResponse;

                        try
                        {
                            _handler.ProcessRequest(ctx);
                        }
                        catch (HttpException ex)
                        {
                            httpResponse = new HttpResponse();
                            httpResponse.RaiseError(ex.Message, ex.Code);
                            httpResponse.AddHeader("Connection", "close");
                        }
                        catch (Exception)
                        {
                            httpResponse = new HttpResponse();
                            httpResponse.RaiseError();
                            httpResponse.AddHeader("Connection", "close");
                        }
                    }

                    httpResponse.Write(stream);

                    stream.Flush();

                    LogAccess log = new LogAccess();
                    log.ClientIP = httpRequest.UserHostAddress;
                    log.BytesReceived = httpRequest.totalBytes;
                    log.BytesSent = httpResponse.totalBytes;
                    log.Date = begin;
                    log.Method = httpRequest.HttpMethod;
                    log.RawUrl = httpRequest.RawUrl;
                    log.UserAgent = httpRequest.UserAgent;
                    log.HttpReferer = httpRequest.Referer;

                    log.Duration = (DateTime.Now.Ticks - begin.Ticks) / TimeSpan.TicksPerMillisecond;


                    _server.OnLogAccess(log);

                    if (httpResponse.Connection == null || httpResponse.Connection != "Keep-Alive")
                        break;

                    Thread.Sleep(15);
                }

                Close();
            }
        }
Exemplo n.º 5
0
 public LogAccessEventArgs(LogAccess data)
 {
     Data = data;
 }
Exemplo n.º 6
0
        internal void OnLogAccess(LogAccess data)
        {
            LogAccessEventHandler handler = LogAccess;

            if (handler != null)
                handler(this, new LogAccessEventArgs(data));
        }