Example #1
0
        /// <summary>
        /// Handle client request.
        /// </summary>
        private void HandleRequest()
        {
            ApplicationSettings             applicationSettings = (ApplicationSettings)this.Application["applicationSettings"];
            Dictionary <string, AppDirInfo> lastReportNumbers   = (Dictionary <string, AppDirInfo>) this.Application["lastReportNumbers"];

            if (this.Request.Form["protocolSignature"] != RequestHandler.protocolSignature)
            {
                throw new ApplicationException("Unsupported protocol version");
            }
            MessageType messageType = (MessageType)byte.Parse(this.Request.Form["messageType"]);

            if (messageType != MessageType.CompundMessage)
            {
                throw new Exception("Unsupported message type");
            }
            CompoundMessageFlags messageFlags = (CompoundMessageFlags)uint.Parse(this.Request.Form["messageFlags"]);

            if (messageFlags != CompoundMessageFlags.None)
            {
                throw new Exception("Unsupported message flags");
            }
            string appName    = this.Request.Form["appName"];
            string appVersion = this.Request.Form["appVersion"];

            if (appName == string.Empty)
            {
                appName = "(UNTITLED)";
            }
            string   appTitle = appVersion == string.Empty ? appName : appName + ' ' + appVersion;
            AppEntry appEntry = new AppEntry(appName, appVersion);

            if (!this.FindAppEntry(applicationSettings, appEntry))
            {
                throw new Exception("Report excluded by filter");
            }
            string extension = this.Request.Form["reportFileExtension"];

            if (!this.FindReportFileExtension(applicationSettings, extension))
            {
                throw new Exception("Invalid report file extension");
            }
            HttpPostedFile reportDataFile = this.Request.Files["reportData"];

            if (reportDataFile == null)
            {
                throw new Exception("Invalid report data");
            }
            int reportSize = reportDataFile.ContentLength;

            if ((applicationSettings.MaxReportSize >= 0 && reportSize > applicationSettings.MaxReportSize) || reportSize <= 0)
            {
                throw new Exception("Report exceeds size limit");
            }
            string email = this.Request.Form["notificationEMail"];
            string dirName = GetAppDirName(appTitle);
            int    maxReportNumber = 0, numReports = 0;

            lock (lastReportNumbers)
            {
                AppDirInfo appDirInfo;
                if (lastReportNumbers.TryGetValue(dirName, out appDirInfo))
                {
                    maxReportNumber = appDirInfo.MaxReportNumber;
                    numReports      = appDirInfo.NumReports;
                }
                if (applicationSettings.ReportsLimit >= 0 && numReports > applicationSettings.ReportsLimit)
                {
                    throw new ApplicationException("Number of reports exceeds the limit");
                }
                if (appDirInfo == null)
                {
                    appDirInfo = new AppDirInfo();
                    lastReportNumbers[dirName] = appDirInfo;
                }
                appDirInfo.NumReports      = ++numReports;
                appDirInfo.MaxReportNumber = ++maxReportNumber;
            }
            string reportDir = Path.Combine(applicationSettings.ReportPath, dirName);

            Directory.CreateDirectory(reportDir);
            string     fileName     = Global.GetReportName(applicationSettings, maxReportNumber, extension);
            string     filePath     = Path.Combine(reportDir, fileName);
            Stream     inputStream  = null;
            FileStream outputStream = null;

            try
            {
                inputStream  = reportDataFile.InputStream;
                outputStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
                byte[] reportData = new byte[1024];
                while (reportSize > 0)
                {
                    int chunkSize = Math.Min(reportSize, reportData.Length);
                    inputStream.Read(reportData, 0, chunkSize);
                    outputStream.Write(reportData, 0, chunkSize);
                    reportSize -= chunkSize;
                }
                this.SendEMail(applicationSettings, email, appTitle);
            }
            finally
            {
                if (inputStream != null)
                {
                    inputStream.Close();
                }
                if (outputStream != null)
                {
                    outputStream.Close();
                }
            }
        }
Example #2
0
        /// <summary>
        /// Handles client request and stores bug report in repository.
        /// </summary>
        /// <param name="state">Socket state object.</param>
        private void HandleRequestCallback(object state)
        {
            SocketState socketState = (SocketState)state;
            bool        shutdown    = false;

            this.BeginCallback();
            try
            {
                BinaryReader binReader    = null;
                FileStream   outputStream = null;
                try
                {
                    // Extract data from the stream.
                    FileStream tempStream = socketState.TempStream;
                    tempStream.Seek(0, SeekOrigin.Begin);
                    long dataSize = tempStream.Length;
                    // Message size (DWORD) + protocol signature (DWORD) = 8 bytes.
                    if (dataSize < 8)
                    {
                        throw new ApplicationException("Invalid message size");
                    }
                    binReader = new BinaryReader(tempStream, Encoding.UTF8);
                    if (BugTrapService.protocolSignature != binReader.ReadUInt32())
                    {
                        throw new ApplicationException("Unsupported protocol version");
                    }
                    if (dataSize != binReader.ReadInt32())
                    {
                        throw new ApplicationException("Invalid message size");
                    }
                    MessageType messageType = (MessageType)binReader.ReadByte();
                    if (messageType != MessageType.CompundMessage)
                    {
                        throw new ApplicationException("Unsupported message type");
                    }
                    CompoundMessageFlags messageFlags = (CompoundMessageFlags)binReader.ReadUInt32();
                    if (messageFlags != CompoundMessageFlags.None)
                    {
                        throw new ApplicationException("Unsupported message flags");
                    }
                    string appName    = binReader.ReadString();
                    string appVersion = binReader.ReadString();
                    string extension  = binReader.ReadString();
                    string email      = binReader.ReadString();
                    if (appName == string.Empty)
                    {
                        appName = "(UNTITLED)";
                    }
                    string appTitle   = appVersion == string.Empty ? appName : appName + ' ' + appVersion;
                    string dirName    = GetAppDirName(appTitle);
                    int    reportSize = (int)(tempStream.Length - tempStream.Position);
                    if ((this.applicationSettings.MaxReportSize >= 0 && reportSize > this.applicationSettings.MaxReportSize) || reportSize <= 0)
                    {
                        throw new ApplicationException("Report exceeds size limit");
                    }
                    AppEntry appEntry = new AppEntry(appName, appVersion);
                    if (!this.FindAppEntry(appEntry))
                    {
                        throw new ApplicationException("Report excluded by filter");
                    }
                    if (!this.FindReportFileExtension(extension))
                    {
                        throw new ApplicationException("Invalid report file extension");
                    }
                    int maxReportNumber = 0, numReports = 0;
                    lock (this.lastReportNumbers)
                    {
                        AppDirInfo appDirInfo;
                        if (this.lastReportNumbers.TryGetValue(dirName, out appDirInfo))
                        {
                            maxReportNumber = appDirInfo.MaxReportNumber;
                            numReports      = appDirInfo.NumReports;
                        }
                        if (this.applicationSettings.ReportsLimit >= 0 && numReports > this.applicationSettings.ReportsLimit)
                        {
                            throw new ApplicationException("Number of reports exceeds the limit");
                        }
                        if (appDirInfo == null)
                        {
                            appDirInfo = new AppDirInfo();
                            this.lastReportNumbers[dirName] = appDirInfo;
                        }
                        appDirInfo.NumReports      = ++numReports;
                        appDirInfo.MaxReportNumber = ++maxReportNumber;
                    }
                    string reportDir = Path.Combine(this.applicationSettings.ReportPath, dirName);
                    Directory.CreateDirectory(reportDir);
                    string fileName = reportNamePattern + maxReportNumber.ToString() + '.' + extension;
                    outputStream = new FileStream(Path.Combine(reportDir, fileName), FileMode.Create, FileAccess.ReadWrite, FileShare.None);
                    byte[] reportData = new byte[1024];
                    while (reportSize > 0)
                    {
                        int chunkSize = Math.Min(reportSize, reportData.Length);
                        binReader.Read(reportData, 0, chunkSize);
                        outputStream.Write(reportData, 0, chunkSize);
                        reportSize -= chunkSize;
                    }
                    this.SendEMail(email, appTitle);
                    // Done!
                    shutdown = true;
                }
                finally
                {
                    if (binReader != null)
                    {
                        binReader.Close();
                    }
                    if (outputStream != null)
                    {
                        outputStream.Close();
                    }
                }
            }
            catch (Exception error)
            {
                this.ReportNestedError(error);
            }
            finally
            {
                this.EndCallback(socketState, shutdown);
            }
        }