protected void UpdateStatus(HttpRequestStatus status) { this.status = status; if (handler != null) { handler(this, status); } }
void OnRequestStatusChange(AbstractHttpRequest request, HttpRequestStatus status) { switch (status) { case HttpRequestStatus.Downloading: Invoke(new SetTextHandler(SetText), string.Format("Downloading... ({0} bytes)", request.Response.Length)); break; case HttpRequestStatus.Error: MessageBox.Show(request.Error, null, MessageBoxButtons.OK, MessageBoxIcon.Error); Invoke(new CloseHandler(Close)); break; case HttpRequestStatus.Done: Invoke(new ShowResultsHandler(ShowResults)); break; } }
/// <summary> /// Gets the request status. /// </summary> /// <param name="settings">The settings.</param> /// <returns>A <see cref="HttpRequestStatus"/>.</returns> protected HttpRequestStatus GetRequestStatus(HttpRequestSettings settings) { HttpRequestStatus ret = new HttpRequestStatus { Settings = settings, Success = false }; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(settings.Uri); HttpWebResponse response = null; if (settings.HasHeaders) { foreach (HttpRequestHeader header in settings.Headers) { request.Headers.Set(header.Name, header.Value); } } if (settings.HasTimeout || settings.HasOverrideTimeout) { request.Timeout = (settings.OverrideTimeout ?? settings.Timeout).Millis; } if (settings.HasReadWriteTimeout) { request.ReadWriteTimeout = settings.ReadWriteTimeout.Millis; } request.Method = settings.Method; if (settings.HasCredentials) { request.Credentials = settings.Credentials; } else { request.UseDefaultCredentials = settings.UseDefaultCredentials; } ret.RequestTime = DateTime.Now; long start = Stopwatch.GetTimestamp(); Timeout timeout = new Timeout(request.Timeout); timeout.Normalize(); Timeout readWriteTimeout = new Timeout(request.ReadWriteTimeout); readWriteTimeout.Normalize(); Log.Debug("Requesting uri (timeout: {0}, read/write-timeout: {1})...", timeout.ToString(), readWriteTimeout.ToString()); try { // TODO this could be better, support chunked transfer etc? if (settings.HasBody || settings.HasSendFile) { using (Stream requestStream = request.GetRequestStream()) { if (settings.HasBody) { using (TextWriter writer = new StreamWriter(requestStream, new UTF8Encoding(false, true))) { writer.Write(settings.Body); } } else { using (FileStream inputFile = File.OpenRead(settings.SendFile)) { int read; byte[] buff = new byte[16384]; do { read = inputFile.Read(buff, 0, buff.Length); requestStream.Write(buff, 0, read); }while (read > 0); } } requestStream.Close(); } } response = (HttpWebResponse)request.GetResponse(); } catch (WebException wEx) { ret.Exception = wEx; if (wEx.Status == WebExceptionStatus.Timeout) { ret.TimedOut = true; } if (wEx.Response is HttpWebResponse) { response = (HttpWebResponse)wEx.Response; } } if (response != null) { ret.StatusCode = response.StatusCode; ret.StatusDescription = response.StatusDescription; ret.ContentEncoding = response.ContentEncoding; ret.CharacterSet = response.CharacterSet; ret.ContentType = response.ContentType; ret.Headers = response.Headers; if (this.IncludeContent) { try { using (Stream responseStream = response.GetResponseStream()) { try { ret.ResponseEncoding = Encoding.GetEncoding(response.CharacterSet); } catch (ArgumentException) { ret.ResponseEncoding = null; } if (ret.ResponseEncoding != null) { using (TextReader reader = new StreamReader(responseStream, ret.ResponseEncoding)) { ret.Content = reader.ReadToEnd(); } } else { // BASE64 encode a blob using (MemoryStream memBuffer = new MemoryStream()) { int read; byte[] buff = new byte[512]; do { read = responseStream.Read(buff, 0, buff.Length); memBuffer.Write(buff, 0, read); }while (read > 0); if (memBuffer.Length > 0) { ret.ContentIsBase64Encoded = true; ret.Content = Convert.ToBase64String( memBuffer.ToArray(), Base64FormattingOptions.InsertLineBreaks); } } } } } catch (Exception ex) { // keep the first exception if (ret.Exception == null) { ret.Exception = ex; } ret.Content = null; } } ret.Success = true; } ret.Duration = TimeSpan.FromSeconds((double)(Stopwatch.GetTimestamp() - start) / Stopwatch.Frequency); return(ret); }
/// <summary> /// Gets the request status. /// </summary> /// <param name="settings">The settings.</param> /// <returns>A <see cref="HttpRequestStatus"/>.</returns> protected HttpRequestStatus GetRequestStatus(HttpRequestSettings settings) { HttpRequestStatus ret = new HttpRequestStatus { Settings = settings, Success = false }; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(settings.Uri); HttpWebResponse response = null; if (settings.HasHeaders) { foreach (HttpRequestHeader header in settings.Headers) { request.Headers.Set(header.Name, header.Value); } } if (settings.HasTimeout || settings.HasOverrideTimeout) { request.Timeout = (settings.OverrideTimeout ?? settings.Timeout).Millis; } if (settings.HasReadWriteTimeout) { request.ReadWriteTimeout = settings.ReadWriteTimeout.Millis; } request.Method = settings.Method; if (settings.HasCredentials) { request.Credentials = settings.Credentials; } else { request.UseDefaultCredentials = settings.UseDefaultCredentials; } ret.RequestTime = DateTime.Now; long start = Stopwatch.GetTimestamp(); Timeout timeout = new Timeout(request.Timeout); timeout.Normalize(); Timeout readWriteTimeout = new Timeout(request.ReadWriteTimeout); readWriteTimeout.Normalize(); Log.Debug("Requesting uri (timeout: {0}, read/write-timeout: {1})...", timeout.ToString(), readWriteTimeout.ToString()); try { // TODO this could be better, support chunked transfer etc? if (settings.HasBody || settings.HasSendFile) { using (Stream requestStream = request.GetRequestStream()) { if (settings.HasBody) { using (TextWriter writer = new StreamWriter(requestStream, new UTF8Encoding(false, true))) { writer.Write(settings.Body); } } else { using (FileStream inputFile = File.OpenRead(settings.SendFile)) { int read; byte[] buff = new byte[16384]; do { read = inputFile.Read(buff, 0, buff.Length); requestStream.Write(buff, 0, read); } while (read > 0); } } requestStream.Close(); } } response = (HttpWebResponse)request.GetResponse(); } catch (WebException wEx) { ret.Exception = wEx; if (wEx.Status == WebExceptionStatus.Timeout) { ret.TimedOut = true; } if (wEx.Response is HttpWebResponse) { response = (HttpWebResponse)wEx.Response; } } if (response != null) { ret.StatusCode = response.StatusCode; ret.StatusDescription = response.StatusDescription; ret.ContentEncoding = response.ContentEncoding; ret.CharacterSet = response.CharacterSet; ret.ContentType = response.ContentType; ret.Headers = response.Headers; if (this.IncludeContent) { try { using (Stream responseStream = response.GetResponseStream()) { try { ret.ResponseEncoding = Encoding.GetEncoding(response.CharacterSet); } catch (ArgumentException) { ret.ResponseEncoding = null; } if (ret.ResponseEncoding != null) { using (TextReader reader = new StreamReader(responseStream, ret.ResponseEncoding)) { ret.Content = reader.ReadToEnd(); } } else { // BASE64 encode a blob using (MemoryStream memBuffer = new MemoryStream()) { int read; byte[] buff = new byte[512]; do { read = responseStream.Read(buff, 0, buff.Length); memBuffer.Write(buff, 0, read); } while (read > 0); if (memBuffer.Length > 0) { ret.ContentIsBase64Encoded = true; ret.Content = Convert.ToBase64String( memBuffer.ToArray(), Base64FormattingOptions.InsertLineBreaks); } } } } } catch (Exception ex) { // keep the first exception if (ret.Exception == null) { ret.Exception = ex; } ret.Content = null; } } ret.Success = true; } ret.Duration = TimeSpan.FromSeconds((double)(Stopwatch.GetTimestamp() - start) / Stopwatch.Frequency); return ret; }
/// <summary> /// Execute the actual task functionality. /// </summary> /// <param name="result">The current build result.</param> /// <returns> /// True if the task was successful, false otherwise. /// </returns> protected override bool Execute(IIntegrationResult result) { Log.Debug("HttpStatusTask is executing"); bool taskTimedOut = false; string msg = string.Format(System.Globalization.CultureInfo.CurrentCulture, "Checking http status of URI: '{0}'", this.RequestSettings.Uri.ToString()); if (!String.IsNullOrEmpty(Description)) { msg = Description + " (" + msg + ")"; } Log.Debug(msg); result.BuildProgressInformation.SignalStartRunTask(msg); XmlTaskResult taskResult = new XmlTaskResult(); List <HttpRequestStatus> resp = new List <HttpRequestStatus>(); long startTimeStamp = -1; bool keepTrying; int attempt = 0; if (this.HasTimeout) { startTimeStamp = Stopwatch.GetTimestamp(); } do { try { if (this.HasTimeout) { long taskDurationTicks = Stopwatch.GetTimestamp() - startTimeStamp; long taskDurationMilliSecs = (long)Math.Round(((double)taskDurationTicks / Stopwatch.Frequency) * 1000.0); long msecsLeft = this.Timeout.Millis - taskDurationMilliSecs; if (msecsLeft < 0) { taskTimedOut = true; break; } if (msecsLeft < this.RequestSettings.Timeout.Millis) { this.RequestSettings.Timeout = new Timeout((int)msecsLeft, TimeUnits.MILLIS); } Log.Debug("Task timeout in T-{0:N1} seconds", msecsLeft / 1000.0); } HttpRequestStatus status = this.GetRequestStatus(this.RequestSettings); if (status.Success) { Log.Debug("Checking returned status code ({0}) against success status codes: {1}", ((int)status.StatusCode).ToString(CultureInfo.CurrentCulture), this.SuccessStatusCodes); for (int i = 0; i < this.successStatusCodes.Length; i++) { if ((int)status.StatusCode == this.successStatusCodes[i]) { Log.Debug("Success, exiting..."); taskResult.Success = true; break; } } } resp.Add(status); } catch (Exception ex) { throw new BuilderException(this, string.Format(System.Globalization.CultureInfo.CurrentCulture, "An exception occured in HttpStatusTask while checking status for: '{0}'", this.RequestSettings.Uri), ex); } // sleeping keepTrying = !taskResult.Success && (this.Retries == -1 || (this.Retries - attempt++) > 0); if (keepTrying) { var delay = this.RetryDelay ?? new Timeout(5000); if (delay.Millis > 0) { delay.Normalize(); Log.Debug("Retrying in {0}...", delay.ToString()); System.Threading.Thread.Sleep(delay.Millis); } else { Log.Debug("Retrying..."); } } }while (keepTrying); if (taskTimedOut) { throw new BuilderException(this, string.Format(System.Globalization.CultureInfo.CurrentCulture, "HttpStatusTask timed out while checking status for: '{0}'", this.RequestSettings.Uri)); } Log.Debug("Writing output..."); XmlWriter writer = taskResult.GetWriter(); writer.WriteStartElement("httpStatus"); writer.WriteAttributeString("uri", this.RequestSettings.Uri.ToString()); writer.WriteAttributeString("success", XmlConvert.ToString(taskResult.Success)); if (!String.IsNullOrEmpty(this.Description)) { writer.WriteElementString("description", this.Description); } foreach (HttpRequestStatus status in resp) { status.WriteTo(writer, this.IncludeContent); } writer.WriteEndElement(); writer.Close(); result.AddTaskResult(taskResult); Log.Debug("HttpStatusTask finished, return value: {0}", taskResult.Success.ToString(CultureInfo.CurrentCulture)); return(taskResult.Success); }