Exemplo n.º 1
0
        protected override void Run()
        {
            List <string> result = new List <string>(500);

            try {
                NntpWebRequest request = (NntpWebRequest)WebRequest.Create(IdentityNewsServerManager.BuildNntpRequestUri(serverDef));
                request.Method = "LIST";

                if (!string.IsNullOrEmpty(serverDef.AuthUser))
                {
                    IBanditFeedSource extension = app.BanditFeedSourceExtension;
                    if (extension != null)
                    {
                        request.Credentials = extension.GetFeedCredentials(serverDef);
                    }
                    //string u = null, p = null;
                    //FeedSource.GetNntpServerCredentials(serverDef, ref u, ref p);
                    //request.Credentials = FeedSource.CreateCredentialsFrom(u, p);
                }

                //TODO: implement proxy support in NntpWebRequest
                request.Proxy = app.Proxy;

                request.Timeout = 1000 * 60;                    // default timeout: 1 minute
                if (serverDef.Timeout > 0)
                {
                    request.Timeout = serverDef.Timeout * 1000 * 60;    // sd.Timeout specified in minutes, but we need msecs
                }

                WebResponse response = request.GetResponse();

                foreach (string s in NntpParser.GetNewsgroupList(response.GetResponseStream()))
                {
                    result.Add(s);
                }

                this.Newsgroups = result;
            } catch (System.Threading.ThreadAbortException) {
                // eat up
            } catch (Exception ex) {
                p_operationException = ex;
            } finally {
                WorkDone.Set();
            }
        }        // Run
Exemplo n.º 2
0
        /// <summary>
        /// Used to create an HTTP request.
        /// </summary>
        /// <param name="requestParameter">Could be modified for each subsequent request</param>
        internal WebRequest PrepareRequest(RequestParameter requestParameter)
        {
            if (requestParameter == null)
            {
                throw new ArgumentNullException("requestParameter");
            }

            // here are the exceptions caused:
            WebRequest webRequest = WebRequest.Create(requestParameter.RequestUri);

            HttpWebRequest httpRequest = webRequest as HttpWebRequest;
            FileWebRequest fileRequest = webRequest as FileWebRequest;
            NntpWebRequest nntpRequest = webRequest as NntpWebRequest;

            if (httpRequest != null)
            {
                // set extended HttpWebRequest params
                httpRequest.Timeout                = Convert.ToInt32(requestParameter.Timeout.TotalMilliseconds); // default: two minutes timeout
                httpRequest.UserAgent              = FullUserAgent(requestParameter.UserAgent);
                httpRequest.Proxy                  = requestParameter.Proxy;
                httpRequest.AllowAutoRedirect      = false;
                httpRequest.AutomaticDecompression = DecompressionMethods.GZip |
                                                     DecompressionMethods.Deflate;
                if (requestParameter.Headers != null)
                {
                    httpRequest.Headers.Add(requestParameter.Headers);
                }

                // due to the reported bug 893620 some web server fail with a server error 500
                // if we send DateTime.MinValue as IfModifiedSince. Smoe Unix derivates only know
                // about valid lowest DateTime around 1970. So in the case we use the
                // httpRequest class default setting:
                if (requestParameter.LastModified > MinValue)
                {
                    httpRequest.IfModifiedSince = requestParameter.LastModified;
                }

                /* #if DEBUG
                 *                                      // further to investigate: with this setting we don't leak connections
                 *                                      // (try TCPView from http://www.sysinternals.com)
                 *                                      // read:
                 *                                      // * http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B819450
                 *                                      // * http://cephas.net/blog/2003/10/29/the_intricacies_of_http.html
                 *                                      // * http://weblogs.asp.net/jan/archive/2004/01/28/63771.aspx
                 *
                 *                                      httpRequest.KeepAlive = false;		// to prevent open HTTP connection leak
                 *                                      httpRequest.ProtocolVersion = HttpVersion.Version10;	// to prevent "Underlying connection closed" exception(s)
                 #endif */

                if (httpRequest.Proxy == null)
                {
                    httpRequest.KeepAlive         = false;
                    httpRequest.Proxy             = WebRequest.DefaultWebProxy;
                    httpRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
                }

                if (requestParameter.ETag != null)
                {
                    httpRequest.Headers.Add("If-None-Match", requestParameter.ETag);
                    httpRequest.Headers.Add("A-IM", "feed");
                }

                if (requestParameter.Credentials != null)
                {
                    httpRequest.KeepAlive       = true;                  // required for authentication to succeed
                    httpRequest.ProtocolVersion = HttpVersion.Version11; // switch back
                    httpRequest.Credentials     = requestParameter.Credentials;
                }

                if (requestParameter.ClientCertificate != null)
                {
                    httpRequest.ClientCertificates.Add(requestParameter.ClientCertificate);
                    httpRequest.Timeout *= 2;   // double the timeout (SSL && Client Certs used!)
                }

                if (requestParameter.SetCookies)
                {
                    HttpCookieManager.SetCookies(httpRequest);
                }

                if (requestParameter.Cookies != null)
                {
                    httpRequest.CookieContainer = new CookieContainer();
                    httpRequest.CookieContainer.Add(requestParameter.Cookies);
                }

                //this prevents the feed mixup issue that we've been facing. See
                //http://www.davelemen.com/archives/2006/04/rss_bandit_feeds_mix_up.html
                //for a user complaint about the issue.
                httpRequest.Pipelined = false;
            }
            else if (fileRequest != null)
            {
                fileRequest.Timeout = DefaultTimeout;

                if (requestParameter.Credentials != null)
                {
                    fileRequest.Credentials = requestParameter.Credentials;
                }
            }
            else if (nntpRequest != null)
            {
                // ten minutes timeout. Large timeout is needed if this is first time we are fetching news
                //TODO: move the timeout handling to the requestor
                nntpRequest.Timeout = DefaultTimeout * 5;

                if (requestParameter.Credentials != null)
                {
                    nntpRequest.Credentials = requestParameter.Credentials;
                }

                if (requestParameter.LastModified > MinValue)
                {
                    nntpRequest.IfModifiedSince = requestParameter.LastModified;
                }
            }
            else
            {
                throw new NotImplementedException("Unsupported WebRequest type: " + webRequest.GetType());
            }

            return(webRequest);
        }