Exemplo n.º 1
0
        /// <summary>
        /// This method invokes a check on a thread from the thread pool.
        /// </summary>
        /// <param name="threadContext">State object specified by the caller to QueueUserWorkItem.</param>
        private void ThreadPoolCallback(Object threadContext)
        {
            try
            {
                // Ensure the thread state is of the correct type
                if (!(threadContext is WatcherCheckState))
                {
                    Trace.TraceError("Error: State passed to Watcher check is not of the proper type.");
                    return;
                }

                WatcherCheckState threadState = (WatcherCheckState)threadContext;

                // Invoke the check
                //Stopwatch sw = new Stopwatch();
                //sw.Start();
                //threadState.parser.Open(threadState.session);
                threadState.check.Check(threadState.session);
                // Must close the parser when done.
                // Stop the stopwatch and print the elapsed time for the check to complete (and the threads to be handled).
                //sw.Stop(); if (sw.ElapsedMilliseconds > 0)
                //{
                //    Debug.Print("[*] Timing:{0}:{1}:{2}", threadState.check.GetShortName(), sw.ElapsedMilliseconds, threadState.session.url);
                //}
            }

            catch (Exception e)
            {
                Trace.TraceWarning("Warning: Watcher check threw an unhandled exception: {0}", e.Message);
                ExceptionLogger.HandleException(e);
            }
        }
Exemplo n.º 2
0
        public void Open(Session session)
        {
            String charset = "utf-8";

            Parser = new HTMLparser();

            try
            {
                if (Utility.IsResponseHtml(session) || Utility.IsResponseXml(session))
                {
                    Parser.Init(session.responseBodyBytes == null ? new byte[] { } : session.responseBodyBytes);
                    Parser.bAutoKeepScripts  = true;
                    Parser.bEnableHeuristics = false;

                    // When bAutoExtractBetweenTagsOnly is false, the parser will see attributes
                    // in the script tags, such as <script src="mydata">.  Otherwise it will not.
                    Parser.bAutoExtractBetweenTagsOnly = true;
                }
            }
            catch (Exception e)
            {
                Trace.TraceWarning("Warning: UtilityHtmlParser threw an unhandled exception: {0}", e.Message);
                ExceptionLogger.HandleException(e);
            }


            // Get the encoding name from the HTML or HTTP
            charset = Utility.GetHtmlCharset(session);

            try
            {
                // TODO: check if the encoding is a known good before continuing!!!
                // See if the charset name we got is a valid system encoding name.
                // GetEncoding should throw an Argument ex if not.
                Encoding e = Encoding.GetEncoding(charset);
                Parser.SetEncoding(charset);
            }
            catch (ArgumentException e)
            {
                // Default to utf-8 if
                Parser.SetEncoding(new UTF8Encoding(false, false));
            }
        }
Exemplo n.º 3
0
        public static NameValueCollection GetRequestParameters(Session session)
        {
            NameValueCollection nvc = null;
            String qs = null;

            // If this is GET request
            if (session.HTTPMethodIs("GET"))
            {
                // ...and has query string
                if (session.PathAndQuery.IndexOf("?") > 0)
                {
                    // Get the query string
                    qs = session.PathAndQuery.Substring(session.PathAndQuery.IndexOf("?") + 1);
                }
            }

            // If is a POST request
            if (session.HTTPMethodIs("POST"))
            {
                // ...and has a content-type
                if (session.oRequest.headers.Exists("content-type"))
                {
                    // ... and is urlencoded form data
                    if (session.oRequest.headers["content-type"] == "application/x-www-form-urlencoded")
                    {
                        // TODO: is a decode needed?
                        //session.utilDecodeRequest();

                        // Get the request body as a string
                        qs = System.Text.Encoding.UTF8.GetString(session.requestBodyBytes);
                    }
                }
            }

            // If we have a query string
            if (qs != null)
            {
                // Parse it...
                try
                {
                    nvc = HttpUtility.ParseQueryString(qs);

                    // Remove any nulls from ill-formed query strings
                    List <string> lst = new List <string>();

                    foreach (String param in nvc.Keys)
                    {
                        if (param == null)
                        {
                            lst.Add(param);
                        }
                    }

                    foreach (String param in lst)
                    {
                        nvc.Remove(param);
                    }
                }

                // TODO: Could we be missing things here?  False negatives?
                catch (ArgumentNullException ane)
                {
                    ExceptionLogger.HandleException(ane);// discard
                }
            }

            return(nvc);
        }