예제 #1
0
        private void logRequests(HttpRequestData data, int mode = 0)
        {
            string output;

            // Check for a non-success response/errors
            bool isErorr = data.IsError;

            if (mode != 0)
                output = data.StatusCode + " " + data.HttpVerb + " " + data.Url + " " + data.TimeTakenMs;
            else
                output = data.HttpVerb + " " + data.Url;

            LogString(output);
        }
        public void HttpRequestDataEncoding()
        {
            string body =
                @"""{""Artist"":{""Id"":331,""ArtistName"":""Motörhead testé"" }"" ";

            var req = new HttpRequestData()
            {
                TextEncoding = "utf-8",
                RequestContent = body
            };

            Console.WriteLine("Raw: " + body);
            var encoded = req.GetRequestContentAsEncodedString();
            Console.WriteLine("Encoded: " + encoded);

            Assert.IsTrue(encoded.Contains("Motörhead"));
        }
        public override bool OnBeforeRequestSent(HttpRequestData data)
        {
            var header = new HttpRequestHeader
            {
                Name = "x-request-time",
                Value = DateTime.UtcNow.ToString("o")
            };
            data.Headers.Add(header);

            if (!string.IsNullOrEmpty(data.RequestContent))
            {
                // do something here to get your unique value
                var userId = new Random().Next().ToString();

                // then embed it into the content
                data.RequestContent = data.RequestContent.Replace("##UserId##", "USER_" + userId);
            }

            return true;
        }
예제 #4
0
        /// <summary>
        /// Checks an individual site and returns a new HttpRequestData object
        /// </summary>
        /// <param name="reqData"></param>
        /// <returns></returns>
        public HttpRequestData CheckSite(HttpRequestData reqData)
        {
            if (CancelThreads)
                return null;

            // create a new instance
            var result = HttpRequestData.Copy(reqData);

            result.ErrorMessage = "Request is incomplete"; // assume not going to make it

            result.IsWarmupRequest =  StartTime.AddSeconds(Options.WarmupSeconds) > DateTime.UtcNow;

            try
            {
                var client = new HttpClient();
                

                    if (!string.IsNullOrEmpty(Options.ReplaceDomain))
                        result.Url = ReplaceDomain(result.Url);

                    if (!string.IsNullOrEmpty(Options.ReplaceQueryStringValuePairs))
                        result.Url = ReplaceQueryStringValuePairs(result.Url, Options.ReplaceQueryStringValuePairs);

                    client.CreateWebRequestObject(result.Url);
                    var webRequest = client.WebRequest;

                    // TODO: Connection Groups might help with sharing connections more efficiently
                    // Initial tests show no improvements - more research required
                    //webRequest.ConnectionGroupName = "_WebSurge_" + Thread.CurrentContext.ContextID;

                    if (!string.IsNullOrEmpty(Options.Username))
                    {
                        client.Username = Options.Username;
                        webRequest.UnsafeAuthenticatedConnectionSharing = true;
                    }
                    if (!string.IsNullOrEmpty(Options.Password))
                        client.Password = Options.Password;

                    webRequest.Method = reqData.HttpVerb;

                    client.ContentType = reqData.ContentType;
                    client.Timeout = Options.RequestTimeoutMs/1000;                    
                
                    // don't auto-add gzip headers and don't decode by default
                    client.UseGZip = false;

                    if (Options.NoContentDecompression)
                        webRequest.AutomaticDecompression = DecompressionMethods.None;
                    else
                        webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

                    if (!string.IsNullOrEmpty(reqData.RequestContent))
                    {
                        var data = reqData.GetRequestContentBytes();
                        client.AddPostKey(data);
                    }
                    else
                    {
                        webRequest.ContentLength = 0;
                    }

                    foreach (var header in result.Headers)
                    {
                        var lheader = header.Name.ToLower();

                        // Header Overrides that fail if you try to set them
                        // directly in HTTP
                        if (lheader == "cookie" && !string.IsNullOrEmpty(Options.ReplaceCookieValue))
                        {
                            string cookie = Options.ReplaceCookieValue;
                            webRequest.Headers.Add("Cookie", cookie);
                            header.Value = cookie;
                            continue;
                        }
                        if (lheader == "authorization" && !string.IsNullOrEmpty(Options.ReplaceAuthorization))
                        {
                            webRequest.Headers.Add("Authorization", Options.ReplaceAuthorization);
                            header.Value = Options.ReplaceAuthorization;
                            continue;
                        }
                        if (lheader == "user-agent")
                        {
                            client.UserAgent = header.Value;
                            continue;
                        }
                        if (lheader == "accept")
                        {
                            webRequest.Accept = header.Value;
                            continue;
                        }
                        if (lheader == "referer")
                        {
                            webRequest.Referer = header.Value;
                            continue;
                        }
                        if (lheader == "connection")
                        {
                            if (header.Value.ToLower() == "keep-alive")
                                webRequest.KeepAlive = true; // this has no effect
                            else if (header.Value.ToLower() == "close")
                                webRequest.KeepAlive = false;
                            continue;
                        }
                        // set above view property
                        if (lheader == "content-type")
                            continue;
                        // not handled at the moment
                        if (lheader == "proxy-connection")
                            continue; // TODO: Figure out what to do with this one

                        // set explicitly via properties
                        if (lheader == "transfer-encoding")
                        {
                            webRequest.TransferEncoding = header.Value;
                            continue;
                        }
                        if (lheader == "date")
                            continue;
                        if (lheader == "expect")
                        {
                            //webRequest.Expect = header.Value;
                            continue;
                        }
                        if (lheader == "if-modified-since")
                            continue;

                        webRequest.Headers.Add(header.Name, header.Value);
                    }

                    foreach (var plugin in App.Plugins)
                    {
                        try
                        {
                            if (!plugin.OnBeforeRequestSent(result))
                                return result;
                        }
                        catch (Exception ex)
                        {
                            App.Log(plugin.GetType().Name + " failed in OnBeforeRequestSent(): " + ex.Message);
                        }
                    }

                    DateTime dt = DateTime.UtcNow;

                    if (CancelThreads)
                        return null;

                    string httpOutput = client.DownloadString(result.Url);

                    if (CancelThreads)
                        return null;

                    result.TimeTakenMs = (int) DateTime.UtcNow.Subtract(dt).TotalMilliseconds;
                    // result.TimeToFirstByteMs = client.Timings.TimeToFirstByteMs;

                    if (client.Error || client.WebResponse == null)
                    {
                        result.ErrorMessage = client.ErrorMessage;
                        return result;
                    }

                    result.StatusCode = ((int) client.WebResponse.StatusCode).ToString();
                    result.StatusDescription = client.WebResponse.StatusDescription ?? string.Empty;
                    result.TimeToFirstByteMs = client.HttpTimings.TimeToFirstByteMs;

                    result.ResponseLength = (int) client.WebResponse.ContentLength;
                    result.ResponseContent = httpOutput;

                    StringBuilder sb = new StringBuilder();
                    foreach (string key in client.WebResponse.Headers.Keys)
                    {
                        sb.AppendLine(key + ": " + client.WebResponse.Headers[key]);
                    }

                    result.ResponseHeaders = sb.ToString();

                    // update to actual Http headers sent
                    result.Headers.Clear();
                    foreach (string key in webRequest.Headers.Keys)
                    {
                        result.Headers.Add(new HttpRequestHeader() {Name = key, Value = webRequest.Headers[key]});
                    }

                    char statusCode = result.StatusCode[0];
                    if (statusCode == '4' || statusCode == '5')
                    {
                        result.IsError = true;
                        result.ErrorMessage = client.WebResponse.StatusDescription;
                    }
                    else
                    {
                        result.IsError = false;
                        result.ErrorMessage = null;

                        if (Options.MaxResponseSize > 0 && result.ResponseContent.Length > Options.MaxResponseSize)
                            result.ResponseContent = result.ResponseContent.Substring(0, Options.MaxResponseSize);
                    }

                //} // using client
                client.Dispose();

                if (!CancelThreads)
                    OnRequestProcessed(result);

                return result;
                
            }

            // these will occur on shutdown - don't log since they will return
            // unstable results - just ignore
            catch (ThreadAbortException)
            {                
                return null;
            }
            catch (Exception ex)
            {                
                Console.WriteLine("Exception: " + ex.Message);
                result.IsError = true;
                result.ErrorMessage = "CheckSite Error: " + ex.GetBaseException().Message;
                
                if (!CancelThreads)
                    OnRequestProcessed(result);

                return result;
            }
        }
예제 #5
0
        public void OnRequestProcessed(HttpRequestData request)
        {
            foreach (var plugin in App.Plugins)
            {
                try
                {
                    plugin.OnAfterRequestSent(request);
                }
                catch (Exception ex)
                {
                    App.Log(plugin.GetType().Name + " failed in OnBeforeRequestSent(): " + ex.Message);
                }
            }

            if (!Options.NoProgressEvents && RequestProcessed != null)
                RequestProcessed(request);
        }
        /// <summary>
        /// Parses a single HttpRequestData object to HTML
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public string ToHtml(bool asDocument = true)
        {
            //return TemplateRenderer.RenderTemplate("Request.cshtml", this);

            HttpRequestData req = this;

            StringBuilder sb   = new StringBuilder();
            string        html = "";

            if (!string.IsNullOrEmpty(req.ErrorMessage))
            {
                sb.AppendLine("<div class='error-display'>");
                sb.AppendLine(@"<div class='error-header'>Error Message</div>");
                sb.AppendLine(@"<div>" + req.ErrorMessage + "</div>");
                sb.AppendLine("</div>");
            }

            html = @"
<h3>Request Headers</h3>
<pre>
<b><span style='color: darkred;'>{0}</span> <a href='{1}'>{1}</a></b> HTTP/1.1
";
            sb.AppendFormat(html, req.HttpVerb, req.Url);

            if (!string.IsNullOrEmpty(req.ContentType))
            {
                sb.AppendLine("Content-type: " + req.ContentType);
            }

            if (req.Headers != null)
            {
                foreach (var header in req.Headers)
                {
                    sb.AppendLine(header.Name + ": " + header.Value);
                }
            }

            if (!string.IsNullOrEmpty(req.RequestContent))
            {
                sb.AppendLine();
                sb.AppendLine(HtmlUtils.HtmlEncode(req.RequestContent.Trim()));
            }

            sb.AppendLine("</pre>");

            if (req.TimeTakenMs > 0)
            {
                sb.AppendFormat("<div class='timetaken'>{0}ms</div>", req.TimeTakenMs.ToString("n0"));
            }

            if (!string.IsNullOrEmpty(req.StatusCode))
            {
                html = @"<h3>Http Response</h3>
<pre>";

                sb.AppendLine(html);


                string cssClass = req.StatusCode.CompareTo("399") > 0 ? "error-response" : "success-response";


                if (!string.IsNullOrEmpty(req.StatusCode))
                {
                    sb.AppendFormat("<div class='{0}'>HTTP/1.1 {1} {2}</div>", cssClass, req.StatusCode, req.StatusDescription);
                }

                sb.AppendLine(req.ResponseHeaders);

                if (req.ResponseContent != null)
                {
                    sb.Append(HtmlUtils.HtmlEncode(req.ResponseContent.Trim()));
                }
            }

            if (!asDocument)
            {
                return(sb.ToString());
            }

            html = @"<!DOCTYPE HTML>
<html>
<head>
    <link href='css/WebSurge.css' type='text/css' rel='stylesheet' />
</head>
<body>
";
            return(html + sb + "</body>\r\n</html>");
        }
예제 #7
0
        public override void Write(HttpRequestData result)
        {
            // don't log request detail data for non errors over a certain no of requests
            if (!result.IsError && RequestsProcessed > 30000)
            {
                // always clear response
                result.ResponseContent = null;

                // detail data only if we explicitly requested
                if (_stressTester.Options.CaptureMinimalResponseData)
                {
                    result.Headers         = null;
                    result.ResponseHeaders = null;
                    result.FullRequest     = null;
                    result.RequestContent  = null;
                }
            }

            bool writeOut     = false;
            var  savedResults = Results;
            int  savedCount   = 0;

            lock (InsertLock)
            {
                Results.Add(result);
                RequestsProcessed++;
                if (result.IsError)
                {
                    RequestsFailed++;
                }

                if (Results.Count >= MaxCollectionItems)
                {
                    FileCount++;
                    savedCount = FileCount;
                    writeOut   = true;

                    Results = null;
                    Results = new List <HttpRequestData>();
                }
            }

            if (writeOut)
            {
                //Console.WriteLine();
                ThreadPool.QueueUserWorkItem((parm) =>
                {
                    //Console.WriteLine("QUWI Thread: " + Thread.CurrentThread.ManagedThreadId);
                    FileWriteParms parms = (FileWriteParms)parm;

                    var file = Path.Combine(TempFolderName, BaseFilename + parms.FileCount + ".json");
                    JsonSerializationUtils.SerializeToFile(parms.Requests, file, false);

                    //SerializationUtils.SerializeObject(parms.Requests, file, true);

                    //if (ResultsList == null)
                    //    ResultsList = new List<List<HttpRequestData>>();

                    //ResultsList.Add(parms.Requests);
                    ////ResultsList.Add(null);
                    //ResultsList[ResultsList.Count-1] = parms.Requests;

                    //var r = parms.Requests;
                    //Console.WriteLine("Queued Item " + parms.FileCount  + " " + r.Count);

                    //IFormatter formatter = new BinaryFormatter();
                    //Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None);
                    //formatter.Serialize(stream, parms.Requests);
                    //stream.Close();
                }, new FileWriteParms {
                    FileCount = savedCount, Requests = savedResults
                });

                //Console.WriteLine("Queue Use Worker Item time: " + swatch.ElapsedTicks.ToString("N0") + " " + Thread.CurrentThread.ManagedThreadId);
            }
        }
예제 #8
0
        private void CloseSession()
        {
            Requests = new List<HttpRequestData>();
            FileName = null;
            RenderRequests(Requests);
            TabSessions.SelectedTab = tabSession;

            string oldTheme = StressTester.Options.FormattedPreviewTheme;

            // clear out options so we don't 'inherit' weird
            // settings from the last session
            StressTester.Options = new StressTesterConfiguration()
            {
                FormattedPreviewTheme = oldTheme
            };
            OptionsPropertyGrid.SelectedObject = StressTester.Options;

            StressTester.Results = new List<HttpRequestData>();

            // render an empty list
            RenderResultList(StressTester.Results);

            var request = new HttpRequestData();
            LoadRequest(request);

            // open the options tab
            TabsResult.SelectedTab = tabRequest;

            try
            {
                GC.Collect();
            }
            catch
            {
            }
        }
예제 #9
0
        HttpRequestData SaveRequest(HttpRequestData request)
        {
            if (request == null)
                request = new HttpRequestData();

            request.IsActive = chkIsActive.Checked;
            request.Name = txtName.Text;
            request.Url = txtRequestUrl.Text;
            request.HttpVerb = txtHttpMethod.Text;
            request.RequestContent = txtRequestContent.Text;
            request.ParseHttpHeaders(txtRequestHeaders.Text);

            return request;
        }
예제 #10
0
 public bool OnBeforeRequestSent(HttpRequestData data)
 {
     logRequests(data, 0);
     return true;
 }
        /// <summary>
        /// Checks an individual site and returns a new HttpRequestData object
        /// </summary>
        /// <param name="reqData"></param>
        /// <returns></returns>
        public HttpRequestData CheckSite(HttpRequestData reqData)
        {
            if (CancelThreads)
            {
                return(null);
            }

            // create a new instance
            var result = HttpRequestData.Copy(reqData);

            result.ErrorMessage = "Request is incomplete"; // assume not going to make it

            result.IsWarmupRequest = StartTime.AddSeconds(Options.WarmupSeconds) > DateTime.UtcNow;

            try
            {
                var client = new HttpClient();


                if (!string.IsNullOrEmpty(Options.ReplaceDomain))
                {
                    result.Url = ReplaceDomain(result.Url);
                }

                if (!string.IsNullOrEmpty(Options.ReplaceQueryStringValuePairs))
                {
                    result.Url = ReplaceQueryStringValuePairs(result.Url, Options.ReplaceQueryStringValuePairs);
                }

                foreach (var plugin in App.Plugins)
                {
                    try
                    {
                        if (!plugin.OnBeforeRequestSent(result))
                        {
                            return(result);
                        }
                    }
                    catch (Exception ex)
                    {
                        App.Log(plugin.GetType().Name + " failed in OnBeforeRequestSent(): " + ex.Message);
                    }
                }

                client.CreateWebRequestObject(result.Url);
                var webRequest = client.WebRequest;

                // TODO: Connection Groups might help with sharing connections more efficiently
                // Initial tests show no improvements - more research required
                //webRequest.ConnectionGroupName = "_WebSurge_" + Thread.CurrentContext.ContextID;

                if (!string.IsNullOrEmpty(Options.Username))
                {
                    client.Username = Options.Username;
                    webRequest.UnsafeAuthenticatedConnectionSharing = true;
                }
                if (!string.IsNullOrEmpty(Options.Password))
                {
                    client.Password = Options.Password;
                }

                webRequest.Method = reqData.HttpVerb;

                client.ContentType = reqData.ContentType;
                client.Timeout     = Options.RequestTimeoutMs / 1000;

                // don't auto-add gzip headers and don't decode by default
                client.UseGZip = false;

                if (Options.NoContentDecompression)
                {
                    webRequest.AutomaticDecompression = DecompressionMethods.None;
                }
                else
                {
                    webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }

                if (!string.IsNullOrEmpty(reqData.RequestContent))
                {
                    var data = reqData.GetRequestContentBytes();
                    client.AddPostKey(data);
                }
                else
                {
                    webRequest.ContentLength = 0;
                }

                foreach (var header in result.Headers)
                {
                    var lheader = header.Name.ToLower();

                    // Header Overrides that fail if you try to set them
                    // directly in HTTP
                    if (lheader == "cookie" && !string.IsNullOrEmpty(Options.ReplaceCookieValue))
                    {
                        string cookie = Options.ReplaceCookieValue;
                        webRequest.Headers.Add("Cookie", cookie);
                        header.Value = cookie;
                        continue;
                    }
                    if (lheader == "authorization" && !string.IsNullOrEmpty(Options.ReplaceAuthorization))
                    {
                        webRequest.Headers.Add("Authorization", Options.ReplaceAuthorization);
                        header.Value = Options.ReplaceAuthorization;
                        continue;
                    }
                    if (lheader == "user-agent")
                    {
                        client.UserAgent = header.Value;
                        continue;
                    }
                    if (lheader == "accept")
                    {
                        webRequest.Accept = header.Value;
                        continue;
                    }
                    if (lheader == "referer")
                    {
                        webRequest.Referer = header.Value;
                        continue;
                    }
                    if (lheader == "connection")
                    {
                        if (header.Value.ToLower() == "keep-alive")
                        {
                            webRequest.KeepAlive = true; // this has no effect
                        }
                        else if (header.Value.ToLower() == "close")
                        {
                            webRequest.KeepAlive = false;
                        }
                        continue;
                    }
                    // set above view property
                    if (lheader == "content-type")
                    {
                        continue;
                    }
                    // not handled at the moment
                    if (lheader == "proxy-connection")
                    {
                        continue; // TODO: Figure out what to do with this one
                    }
                    // set explicitly via properties
                    if (lheader == "transfer-encoding")
                    {
                        webRequest.TransferEncoding = header.Value;
                        continue;
                    }
                    if (lheader == "date")
                    {
                        continue;
                    }
                    if (lheader == "expect")
                    {
                        //webRequest.Expect = header.Value;
                        continue;
                    }
                    if (lheader == "if-modified-since")
                    {
                        continue;
                    }

                    webRequest.Headers.Add(header.Name, header.Value);
                }

                DateTime dt = DateTime.UtcNow;

                if (CancelThreads)
                {
                    return(null);
                }

                // using West Wind HttpClient
                string httpOutput = client.DownloadString(result.Url);

                if (CancelThreads)
                {
                    return(null);
                }

                result.TimeTakenMs = (int)DateTime.UtcNow.Subtract(dt).TotalMilliseconds;
                // result.TimeToFirstByteMs = client.Timings.TimeToFirstByteMs;

                if (client.Error || client.WebResponse == null)
                {
                    result.ErrorMessage = client.ErrorMessage;
                    return(result);
                }

                result.StatusCode        = ((int)client.WebResponse.StatusCode).ToString();
                result.StatusDescription = client.WebResponse.StatusDescription ?? string.Empty;
                result.TimeToFirstByteMs = client.HttpTimings.TimeToFirstByteMs;

                result.ResponseLength  = (int)client.WebResponse.ContentLength;
                result.ResponseContent = httpOutput;

                StringBuilder sb = new StringBuilder();
                foreach (string key in client.WebResponse.Headers.Keys)
                {
                    sb.AppendLine(key + ": " + client.WebResponse.Headers[key]);
                }

                result.ResponseHeaders = sb.ToString();

                // update to actual Http headers sent
                result.Headers.Clear();
                foreach (string key in webRequest.Headers.Keys)
                {
                    result.Headers.Add(new HttpRequestHeader()
                    {
                        Name = key, Value = webRequest.Headers[key]
                    });
                }

                char statusCode = result.StatusCode[0];
                if (statusCode == '4' || statusCode == '5')
                {
                    result.IsError      = true;
                    result.ErrorMessage = client.WebResponse.StatusDescription;
                }
                else
                {
                    result.IsError      = false;
                    result.ErrorMessage = null;

                    if (Options.MaxResponseSize > 0 && result.ResponseContent.Length > Options.MaxResponseSize)
                    {
                        result.ResponseContent = result.ResponseContent.Substring(0, Options.MaxResponseSize);
                    }
                }

                //} // using client
                client.Dispose();

                if (!CancelThreads)
                {
                    OnRequestProcessed(result);
                }

                return(result);
            }

            // these will occur on shutdown - don't log since they will return
            // unstable results - just ignore
            catch (ThreadAbortException)
            {
                return(null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                result.IsError      = true;
                result.ErrorMessage = "CheckSite Error: " + ex.GetBaseException().Message;

                if (!CancelThreads)
                {
                    OnRequestProcessed(result);
                }

                return(result);
            }
        }
예제 #12
0
 void LoadRequest(HttpRequestData request)
 {
     txtName.Text = request.Name;
     txtHttpMethod.Text = request.HttpVerb;
     txtRequestUrl.Text = request.Url;
     txtRequestUrl.Tag = request;
     chkIsActive.Checked = request.IsActive;
     StringBuilder sb = new StringBuilder();
     foreach (var hd in request.Headers)
     {
         sb.AppendLine(hd.Name + ": " + hd.Value);
     }
     txtRequestHeaders.Text = sb.ToString();
     txtRequestContent.Text = request.RequestContent;
 }
 public void OnAfterRequestSent(HttpRequestData data)
 {
 }
 /// <summary>
 /// Post-request processing of the request that has
 /// been fired agains the server. The passed request data
 /// contains the full completed request data.
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public void OnAfterRequestSent(HttpRequestData data)
 {
 }
 /// <summary>
 /// Pre-request processing of the request that is
 /// fired against the server.
 /// </summary>
 /// <remarks>
 /// These events are fired asynchronously and have to be threadsafe. The data
 /// instance passed is thread-safe, but any other resources you use may not be.
 /// For example, if you write output to a file make sure you put a Sync lock on
 /// the file to avoid simultaneous access by multiple threads.
 /// </remarks>
 /// <param name="data"></param>
 /// <returns>return true to process the request, false to abort processing and skip this request</returns>
 public bool OnBeforeRequestSent(HttpRequestData data)
 {
     return(true);
 }
예제 #16
0
        /// <summary>
        /// Writes the actual result to the storage container
        /// </summary>
        /// <param name="result"></param>
        public virtual void WriteResult(HttpRequestData result)
        {
            // don't log request detail data for non errors over a certain no of requests
            if (!result.IsError && Results.Count > 30000)
            {
                // always clear response
                result.ResponseContent = null;

                // detail data only if we explicitly requested
                if (Options.CaptureMinimalResponseData)
                {
                    result.Headers = null;
                    result.ResponseHeaders = null;
                    result.FullRequest = null;
                    result.RequestContent = null;
                }
            }

            lock (InsertLock)
            {
                Results.Add(result);
                RequestsProcessed++;
                if (result.IsError)
                    RequestsFailed++;
            }
        }
예제 #17
0
        /// <summary>
        /// Parses an individual requests in HTTP header format.
        /// Expects the URL to be part of the first HTTP header line:
        /// 
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public HttpRequestData ParseRequest(string requestData)
        {
            var reqHttp = new HttpRequestData();

            string fullHeader = StringUtils.ExtractString(requestData, "", "\r\n\r\nHTTP",false,true);
            reqHttp.FullRequest = fullHeader;

            string header = StringUtils.ExtractString(fullHeader, "", "\r\n\r\n",false,true);

            var lines = StringUtils.GetLines(header);
            reqHttp.Url = StringUtils.ExtractString(lines[0], " ", " HTTP/");
            reqHttp.HttpVerb = StringUtils.ExtractString(lines[0], "", " ");

            if (reqHttp.HttpVerb == "CONNECT")
            {
                return null;
            }

            if (reqHttp.HttpVerb != "GET")
                reqHttp.RequestContent = StringUtils.ExtractString(fullHeader, "\r\n\r\n", "\r\nHTTP", false, true);

            if (lines.Length > 0)
            {
                lines[0] = string.Empty;
                reqHttp.ParseHttpHeaders(lines);
            }

            reqHttp.Host = reqHttp.Headers
                .Where(hd => hd.Name == "Host")
                .Select(hd => hd.Value)
                .FirstOrDefault();

            return reqHttp;
        }
예제 #18
0
 public void OnAfterRequestSent(HttpRequestData data)
 {
     logRequests(data, 1);
 }
 /// <summary>
 /// Pre-request processing of the request that is
 /// fired against the server.
 /// </summary>
 /// <remarks>
 /// These events are fired asynchronously and have to be threadsafe. The data
 /// instance passed is thread-safe, but any other resources you use may not be.
 /// For example, if you write output to a file make sure you put a Sync lock on
 /// the file to avoid simultaneous access by multiple threads.
 /// </remarks>
 /// <param name="data"></param>
 /// <returns>return true to process the request, false to abort processing and skip this request</returns>
 public virtual bool OnBeforeRequestSent(HttpRequestData data)
 {
     return true;
 }
예제 #20
0
        private void StressTester_RequestProcessed(HttpRequestData req)
        {
            string currentLine = req.StatusCode + " -  " + req.HttpVerb + " " + req.Url + "(" +
                                 req.TimeTakenMs.ToString("n0") + "ms)";

            string textToWrite = null;

            lock (ConsoleLog)
            {
                statusOutput.AppendLine(currentLine);
                if (lastUpdate.AddMilliseconds(statusOutputBufferDelay) < DateTime.UtcNow)
                {
                    lastUpdate = DateTime.UtcNow;
                    textToWrite = statusOutput.ToString();
                    statusOutput.Length = 0;
                }
            }

            if (textToWrite != null)
            {
                try
                {
                    BeginInvoke(new Action<string>(WriteConsoleOutput), textToWrite);
                }
                catch
                {
                }
            }
        }
예제 #21
0
        /// <summary>
        /// Parses an individual requests in HTTP header format.
        /// Expects the URL to be part of the first HTTP header line:
        ///
        /// Parser supports both CRLF and LF only
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public HttpRequestData ParseRequest(string requestData)
        {
            StringBuilder sbHeader = new StringBuilder();
            StringBuilder sbBody   = new StringBuilder();

            var reqLines = StringUtils.GetLines(requestData.TrimStart());

            var reqHttp = new HttpRequestData();

            reqHttp.Url      = StringUtils.ExtractString(reqLines[0], " ", " HTTP/");
            reqHttp.HttpVerb = StringUtils.ExtractString(reqLines[0], "", " ");

            // ignore CONNECT requests
            if (reqHttp.HttpVerb == "CONNECT")
            {
                return(null);
            }

            int  state         = 0; // 0 header, 1 body, 2  Response/done
            bool lastLineEmpty = false;

            for (var index = 0; index < reqLines.Length; index++)
            {
                var line = reqLines[index];
                if (state == 0)
                {
                    if (string.IsNullOrEmpty(line))  // transition to body
                    {
                        state = 1;
                        continue;
                    }
                    if (line.StartsWith("HTTP/"))   // done
                    {
                        break;
                    }

                    sbHeader.AppendLine(line);
                }

                if (state == 1)
                {
                    if (line.StartsWith("HTTP/"))
                    {
                        break;
                    }

                    sbBody.AppendLine(line);
                }
            }

            reqHttp.FullRequest = sbHeader + (sbBody.Length > 0 ? "\r\n" + sbBody.ToString().TrimEnd() :  string.Empty);

            if (reqLines.Length > 0)
            {
                var lines = StringUtils.GetLines(sbHeader.ToString());
                lines[0] = string.Empty; // HTTP header is not a 'real' header
                reqHttp.ParseHttpHeaders(lines);
            }

            if (reqHttp.HttpVerb != "GET" && sbBody.Length > 0)
            {
                reqHttp.RequestContent = sbBody.ToString().TrimEnd();
            }


            reqHttp.Host = reqHttp.Headers
                           .Where(hd => hd.Name == "Host")
                           .Select(hd => hd.Value)
                           .FirstOrDefault();

            return(reqHttp);
        }
예제 #22
0
 public static HttpRequestData Copy(HttpRequestData req)
 {
     var rnew = req.MemberwiseClone() as HttpRequestData;
     rnew.Headers = new List<HttpRequestHeader>(req.Headers);
     rnew.Timestamp = DateTime.UtcNow;
     return rnew;
 }
예제 #23
0
        private void TestSiteUrl(HttpRequestData req)
        {
            Cursor = Cursors.WaitCursor;

            StressTester.CancelThreads = false;

            var action = new Action<HttpRequestData>(rq =>
            {
                ShowStatus("Checking URL: " + rq.Url);

                ActiveRequest = StressTester.CheckSite(rq);
                string html = TemplateRenderer.RenderTemplate("Request.cshtml", ActiveRequest);

                Invoke(new Action<string>(htmlText =>
                {
                    HtmlPreview(html);
                    TabsResult.SelectedTab = tabPreview;
                    ShowStatus("URL check complete.", 1, 5000);

                }),html);

            });
            action.BeginInvoke(req,null,null);

            Cursor = Cursors.Default;
        }