Changeset 2699
- Timestamp:
- 5/25/2012 4:14:06 AM (13 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/eraser/Eraser.BlackBox/BlackBoxReportUploader.cs
r2698 r2699 98 98 { 99 99 //Get the status from the server. 100 XmlDocument result = QueryServer("status", true); 100 XmlDocument result = QueryServer("status", null, 101 GetStackTraceField(Report.StackTrace).ToArray()); 101 102 102 103 //Parse the result document … … 201 202 using (Stream logFile = Report.DebugLog) 202 203 { 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) 222 212 { 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. 276 226 /// </summary> 277 227 /// <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) 281 231 { 282 232 int exceptionIndex = 0; 233 List<PostDataField> result = new List<PostDataField>(); 283 234 foreach (BlackBoxExceptionEntry exceptionStack in stackTrace) 284 235 { 285 236 foreach (string stackFrame in exceptionStack.StackTrace) 286 builder.AddPart(new PostDataField(237 result.Add(new PostDataField( 287 238 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, 289 241 "stackTrace[{0}][exception]", exceptionIndex), exceptionStack.ExceptionType)); 290 242 ++exceptionIndex; 291 243 } 244 245 return result; 292 246 } 293 247 … … 296 250 /// </summary> 297 251 /// <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> 299 255 /// <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) 301 258 { 302 259 PostDataBuilder builder = new PostDataBuilder(); 303 260 builder.AddPart(new PostDataField("action", action)); 304 if (includeStackTrace)305 AddStackTraceToRequest(Report.StackTrace, builder);306 261 307 262 WebRequest reportRequest = HttpWebRequest.Create(BlackBoxServer); 308 263 reportRequest.ContentType = builder.ContentType; 309 264 reportRequest.Method = "POST"; 265 reportRequest.Timeout = int.MaxValue; 310 266 using (Stream formStream = builder.Stream) 311 267 { … … 316 272 byte[] buffer = new byte[32768]; 317 273 while ((lastRead = formStream.Read(buffer, 0, buffer.Length)) != 0) 274 { 318 275 requestStream.Write(buffer, 0, lastRead); 276 if (progressChanged != null) 277 progressChanged(formStream.Position, formStream.Length); 278 } 319 279 } 320 280 } … … 356 316 357 317 /// <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> 358 325 /// The path to where the temporary files are stored before uploading. 359 326 /// </summary>
Note: See TracChangeset
for help on using the changeset viewer.
