Changeset 2699


Ignore:
Timestamp:
5/25/2012 4:14:06 AM (3 years ago)
Author:
lowjoel
Message:

Make the Upload function call QueryServer? as well, to refactorise the uploading code. Furthermore, to remove the need to build the post request ourselves, I've replaced the AddStackTraceToRequest? function with GetStackTraceField? function which will return the list of PostDataField? objects which can be appended to the request.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser/Eraser.BlackBox/BlackBoxReportUploader.cs

    r2698 r2699  
    9898            { 
    9999                //Get the status from the server. 
    100                 XmlDocument result = QueryServer("status", true); 
     100                XmlDocument result = QueryServer("status", null, 
     101                    GetStackTraceField(Report.StackTrace).ToArray()); 
    101102                 
    102103                //Parse the result document 
     
    201202            using (Stream logFile = Report.DebugLog) 
    202203            { 
    203                 //Build the POST request 
    204                 PostDataBuilder builder = new PostDataBuilder(); 
    205                 builder.AddPart(new PostDataField("action", "upload")); 
    206                 builder.AddPart(new PostDataFileField("crashReport", "Report.tar.7z", bzipFile)); 
    207                 AddStackTraceToRequest(Report.StackTrace, builder); 
    208  
    209                 //Upload the POST request 
    210                 WebRequest reportRequest = HttpWebRequest.Create(BlackBoxServer); 
    211                 reportRequest.ContentType = builder.ContentType; 
    212                 reportRequest.Method = "POST"; 
    213                 reportRequest.Timeout = int.MaxValue; 
    214                 using (Stream formStream = builder.Stream) 
    215                 { 
    216                     ProgressManager progress = new ProgressManager(); 
    217                     overallProgress.Steps.Add(new SteppedProgressManagerStep( 
    218                         progress, 0.5f, "Uploading")); 
    219                     reportRequest.ContentLength = formStream.Length; 
    220  
    221                     using (Stream requestStream = reportRequest.GetRequestStream()) 
     204                List<PostDataField> fields = GetStackTraceField(Report.StackTrace); 
     205                fields.Add(new PostDataFileField("crashReport", "Report.tar.7z", bzipFile)); 
     206 
     207                ProgressManager progress = new ProgressManager(); 
     208                overallProgress.Steps.Add(new SteppedProgressManagerStep( 
     209                    progress, 0.5f, "Uploading")); 
     210 
     211                QueryServer("upload", delegate(long uploaded, long total) 
    222212                    { 
    223                         int lastRead = 0; 
    224                         byte[] buffer = new byte[32768]; 
    225                         while ((lastRead = formStream.Read(buffer, 0, buffer.Length)) != 0) 
    226                         { 
    227                             requestStream.Write(buffer, 0, lastRead); 
    228  
    229                             progress.Total = formStream.Length; 
    230                             progress.Completed = formStream.Position; 
    231                             progressChanged(this, new ProgressChangedEventArgs(overallProgress, null)); 
    232                         } 
    233                     } 
    234                 } 
    235  
    236                 try 
    237                 { 
    238                     HttpWebResponse response = reportRequest.GetResponse() as HttpWebResponse; 
    239                     using (Stream responseStream = response.GetResponseStream()) 
    240                     { 
    241                         XmlReader reader = XmlReader.Create(responseStream); 
    242                         reader.ReadToFollowing("crashReport"); 
    243                         string reportStatus = reader.GetAttribute("status"); 
    244                         string reportId = reader.GetAttribute("id"); 
    245                     } 
    246  
    247                     Report.Submitted = true; 
    248                 } 
    249                 catch (WebException e) 
    250                 { 
    251                     if (e.Response == null) 
    252                         throw; 
    253  
    254                     using (Stream responseStream = e.Response.GetResponseStream()) 
    255                     { 
    256                         try 
    257                         { 
    258                             XmlReader reader = XmlReader.Create(responseStream); 
    259                             reader.ReadToFollowing("error"); 
    260                             throw new InvalidDataException(string.Format(CultureInfo.CurrentCulture, 
    261                                 "The server encountered a problem while processing the request: {0}", 
    262                                 reader.ReadString())); 
    263                         } 
    264                         catch (XmlException) 
    265                         { 
    266                         } 
    267                     } 
    268  
    269                     throw new InvalidDataException(((HttpWebResponse)e.Response).StatusDescription); 
    270                 } 
    271             } 
    272         } 
    273  
    274         /// <summary> 
    275         /// Adds the stack trace to the given form request. 
     213                        progress.Total = total; 
     214                        progress.Completed = uploaded; 
     215                        progressChanged(this, new ProgressChangedEventArgs(overallProgress, null)); 
     216                    }, fields.ToArray()); 
     217 
     218 
     219                Report.Submitted = true; 
     220            } 
     221        } 
     222 
     223        /// <summary> 
     224        /// Builds the stackTrace POST data field and retrieves the Post data fields to include 
     225        /// with the request. 
    276226        /// </summary> 
    277227        /// <param name="stackTrace">The stack trace to add.</param> 
    278         /// <param name="builder">The Form request builder to add the stack trace to.</param> 
    279         private static void AddStackTraceToRequest(IList<BlackBoxExceptionEntry> stackTrace, 
    280             PostDataBuilder builder) 
     228        /// <returns>A list of PostDataField objects which can be added to a PostDataBuilder 
     229        /// object to add stack trace information to the request.</returns> 
     230        private static List<PostDataField> GetStackTraceField(IList<BlackBoxExceptionEntry> stackTrace) 
    281231        { 
    282232            int exceptionIndex = 0; 
     233            List<PostDataField> result = new List<PostDataField>(); 
    283234            foreach (BlackBoxExceptionEntry exceptionStack in stackTrace) 
    284235            { 
    285236                foreach (string stackFrame in exceptionStack.StackTrace) 
    286                     builder.AddPart(new PostDataField( 
     237                    result.Add(new PostDataField( 
    287238                        string.Format(CultureInfo.InvariantCulture, "stackTrace[{0}][]", exceptionIndex), stackFrame)); 
    288                 builder.AddPart(new PostDataField(string.Format(CultureInfo.InvariantCulture, 
     239 
     240                result.Add(new PostDataField(string.Format(CultureInfo.InvariantCulture, 
    289241                    "stackTrace[{0}][exception]", exceptionIndex), exceptionStack.ExceptionType)); 
    290242                ++exceptionIndex; 
    291243            } 
     244 
     245            return result; 
    292246        } 
    293247 
     
    296250        /// </summary> 
    297251        /// <param name="action">The action to perform.</param> 
    298         /// <param name="includeStackTrace">Whether to include a stack trace.</param> 
     252        /// <param name="progressChanged">A progress changed event handler receiving 
     253        /// upload progress information.</param> 
     254        /// <param name="fields">The POST fields to upload along with the request.</param> 
    299255        /// <returns>An XmlReader containing the response.</returns> 
    300         private XmlDocument QueryServer(string action, bool includeStackTrace) 
     256        private XmlDocument QueryServer(string action, QueryProgress progressChanged = null, 
     257            params PostDataField[] fields) 
    301258        { 
    302259            PostDataBuilder builder = new PostDataBuilder(); 
    303260            builder.AddPart(new PostDataField("action", action)); 
    304             if (includeStackTrace) 
    305                 AddStackTraceToRequest(Report.StackTrace, builder); 
    306261 
    307262            WebRequest reportRequest = HttpWebRequest.Create(BlackBoxServer); 
    308263            reportRequest.ContentType = builder.ContentType; 
    309264            reportRequest.Method = "POST"; 
     265            reportRequest.Timeout = int.MaxValue; 
    310266            using (Stream formStream = builder.Stream) 
    311267            { 
     
    316272                    byte[] buffer = new byte[32768]; 
    317273                    while ((lastRead = formStream.Read(buffer, 0, buffer.Length)) != 0) 
     274                    { 
    318275                        requestStream.Write(buffer, 0, lastRead); 
     276                        if (progressChanged != null) 
     277                            progressChanged(formStream.Position, formStream.Length); 
     278                    } 
    319279                } 
    320280            } 
     
    356316 
    357317        /// <summary> 
     318        /// Delegate object for upload progress callbacks. 
     319        /// </summary> 
     320        /// <param name="uploaded">The amount of data uploaded.</param> 
     321        /// <param name="total">The amount of data to upload.</param> 
     322        private delegate void QueryProgress(long uploaded, long total); 
     323 
     324        /// <summary> 
    358325        /// The path to where the temporary files are stored before uploading. 
    359326        /// </summary> 
Note: See TracChangeset for help on using the changeset viewer.