protected override void ServiceClient(TcpClient client)
        {
            var stream = client.GetStream();

            try
            {
                byte[] buffer = new byte[8000];
                StringBuilder data = new StringBuilder();
                bool httpRequestComplete = false;
                while (client.Connected)
                {
                    if (stream.DataAvailable && !httpRequestComplete)
                    {
                        int bytes_read = stream.Read(buffer, 0, buffer.Length);
                        data.Append(ASCIIEncoding.ASCII.GetString(buffer, 0, bytes_read));

                        if (data.ToString().Contains("\r\n\r\n"))
                        {
                            //Logger.ReportVerbose("Request complete");
                            //Logger.ReportVerbose("Data were: " + data.ToString());
                            httpRequestComplete = true;
                        }
                    }
                    else
                    {
                        Thread.Sleep(1);
                    }

                    if (httpRequestComplete)
                    {
                        var headers = new HttpHeaders(data.ToString());
                        string requestedPath = headers.Path;

                        try
                        {
                            ServeImage(stream, requestedPath);
                        }
                        catch (Exception ee)
                        {
                            Logger.ReportException("Failed to proxy image : " + requestedPath, ee);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.ReportException("Failed to serve image  ", e);
            }
            finally
            {

                try
                {
                    //Logger.ReportVerbose("Image proxy closing connections.");
                    stream.Close();
                }
                catch
                {
                    // well we tried
                }
            }
        }
Пример #2
0
        protected override void ServiceClient(TcpClient client)
        {
            var stream = client.GetStream();

            try
            {
                byte[]        buffer = new byte[8000];
                StringBuilder data   = new StringBuilder();
                bool          httpRequestComplete = false;
                while (client.Connected)
                {
                    if (stream.DataAvailable && !httpRequestComplete)
                    {
                        int bytes_read = stream.Read(buffer, 0, buffer.Length);
                        data.Append(ASCIIEncoding.ASCII.GetString(buffer, 0, bytes_read));

                        if (data.ToString().Contains("\r\n\r\n"))
                        {
                            //Logger.ReportVerbose("Request complete");
                            //Logger.ReportVerbose("Data were: " + data.ToString());
                            httpRequestComplete = true;
                        }
                    }
                    else
                    {
                        Thread.Sleep(1);
                    }

                    if (httpRequestComplete)
                    {
                        var    headers       = new HttpHeaders(data.ToString());
                        string requestedPath = headers.Path;

                        try
                        {
                            ServeImage(stream, requestedPath);
                        }
                        catch (Exception ee)
                        {
                            Logger.ReportException("Failed to proxy image : " + requestedPath, ee);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.ReportException("Failed to serve image  ", e);
            }
            finally
            {
                try
                {
                    //Logger.ReportVerbose("Image proxy closing connections.");
                    stream.Close();
                }
                catch
                {
                    // well we tried
                }
            }
        }