コード例 #1
0
 /// <summary>
 /// Creates a new instance of
 /// <see cref="BasicTokenIterator">BasicTokenIterator</see>
 /// .
 /// </summary>
 /// <param name="headerIterator">the iterator for the headers to tokenize</param>
 public BasicTokenIterator(HeaderIterator headerIterator) : base()
 {
     // the order of the characters here is adjusted to put the
     // most likely candidates at the beginning of the collection
     this.headerIt  = Args.NotNull(headerIterator, "Header iterator");
     this.searchPos = FindNext(-1);
 }
コード例 #2
0
 /// <exception cref="System.IO.IOException"></exception>
 /// <exception cref="Org.Apache.Http.HttpException"></exception>
 public virtual void Write(T message)
 {
     Args.NotNull(message, "HTTP message");
     WriteHeadLine(message);
     for (HeaderIterator it = message.HeaderIterator(); it.HasNext();)
     {
         Header header = it.NextHeader();
         this.sessionBuffer.WriteLine(lineFormatter.FormatHeader(this.lineBuf, header));
     }
     this.lineBuf.Clear();
     this.sessionBuffer.WriteLine(this.lineBuf);
 }
コード例 #3
0
 public virtual Apache.Http.Client.Methods.RequestBuilder RemoveHeaders(string name
                                                                        )
 {
     if (name == null || headergroup == null)
     {
         return(this);
     }
     for (HeaderIterator i = headergroup.Iterator(); i.HasNext();)
     {
         Header header = i.NextHeader();
         if (Sharpen.Runtime.EqualsIgnoreCase(name, header.GetName()))
         {
             i.Remove();
         }
     }
     return(this);
 }
コード例 #4
0
        public virtual ICollection <string> GetAllowedMethods(HttpResponse response)
        {
            Args.NotNull(response, "HTTP response");
            HeaderIterator       it      = response.HeaderIterator("Allow");
            ICollection <string> methods = new HashSet <string>();

            while (it.HasNext())
            {
                Header          header   = it.NextHeader();
                HeaderElement[] elements = header.GetElements();
                foreach (HeaderElement element in elements)
                {
                    methods.AddItem(element.GetName());
                }
            }
            return(methods);
        }
コード例 #5
0
        /// <exception cref="Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void Process(HttpResponse response, HttpContext context)
        {
            Args.NotNull(response, "HTTP request");
            Args.NotNull(context, "HTTP context");
            HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context
                                                                                          ));
            // Obtain actual CookieSpec instance
            CookieSpec cookieSpec = clientContext.GetCookieSpec();

            if (cookieSpec == null)
            {
                this.log.Debug("Cookie spec not specified in HTTP context");
                return;
            }
            // Obtain cookie store
            CookieStore cookieStore = clientContext.GetCookieStore();

            if (cookieStore == null)
            {
                this.log.Debug("Cookie store not specified in HTTP context");
                return;
            }
            // Obtain actual CookieOrigin instance
            CookieOrigin cookieOrigin = clientContext.GetCookieOrigin();

            if (cookieOrigin == null)
            {
                this.log.Debug("Cookie origin not specified in HTTP context");
                return;
            }
            HeaderIterator it = response.HeaderIterator(SM.SetCookie);

            ProcessCookies(it, cookieSpec, cookieOrigin, cookieStore);
            // see if the cookie spec supports cookie versioning.
            if (cookieSpec.GetVersion() > 0)
            {
                // process set-cookie2 headers.
                // Cookie2 will replace equivalent Cookie instances
                it = response.HeaderIterator(SM.SetCookie2);
                ProcessCookies(it, cookieSpec, cookieOrigin, cookieStore);
            }
        }
コード例 #6
0
 private void ProcessCookies(HeaderIterator iterator, CookieSpec cookieSpec, CookieOrigin
                             cookieOrigin, CookieStore cookieStore)
 {
     while (iterator.HasNext())
     {
         Header header = iterator.NextHeader();
         try
         {
             IList <Apache.Http.Cookie.Cookie> cookies = cookieSpec.Parse(header, cookieOrigin);
             foreach (Apache.Http.Cookie.Cookie cookie in cookies)
             {
                 try
                 {
                     cookieSpec.Validate(cookie, cookieOrigin);
                     cookieStore.AddCookie(cookie);
                     if (this.log.IsDebugEnabled())
                     {
                         this.log.Debug("Cookie accepted [" + FormatCooke(cookie) + "]");
                     }
                 }
                 catch (MalformedCookieException ex)
                 {
                     if (this.log.IsWarnEnabled())
                     {
                         this.log.Warn("Cookie rejected [" + FormatCooke(cookie) + "] " + ex.Message);
                     }
                 }
             }
         }
         catch (MalformedCookieException ex)
         {
             if (this.log.IsWarnEnabled())
             {
                 this.log.Warn("Invalid cookie header: \"" + header + "\". " + ex.Message);
             }
         }
     }
 }
コード例 #7
0
        // see interface ConnectionReuseStrategy
        public virtual bool KeepAlive(HttpResponse response, HttpContext context)
        {
            Args.NotNull(response, "HTTP response");
            Args.NotNull(context, "HTTP context");
            // Check for a self-terminating entity. If the end of the entity will
            // be indicated by closing the connection, there is no keep-alive.
            ProtocolVersion ver = response.GetStatusLine().GetProtocolVersion();
            Header          teh = response.GetFirstHeader(HTTP.TransferEncoding);

            if (teh != null)
            {
                if (!Sharpen.Runtime.EqualsIgnoreCase(HTTP.ChunkCoding, teh.GetValue()))
                {
                    return(false);
                }
            }
            else
            {
                if (CanResponseHaveBody(response))
                {
                    Header[] clhs = response.GetHeaders(HTTP.ContentLen);
                    // Do not reuse if not properly content-length delimited
                    if (clhs.Length == 1)
                    {
                        Header clh = clhs[0];
                        try
                        {
                            int contentLen = System.Convert.ToInt32(clh.GetValue());
                            if (contentLen < 0)
                            {
                                return(false);
                            }
                        }
                        catch (FormatException)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            // Check for the "Connection" header. If that is absent, check for
            // the "Proxy-Connection" header. The latter is an unspecified and
            // broken but unfortunately common extension of HTTP.
            HeaderIterator hit = response.HeaderIterator(HTTP.ConnDirective);

            if (!hit.HasNext())
            {
                hit = response.HeaderIterator("Proxy-Connection");
            }
            // Experimental usage of the "Connection" header in HTTP/1.0 is
            // documented in RFC 2068, section 19.7.1. A token "keep-alive" is
            // used to indicate that the connection should be persistent.
            // Note that the final specification of HTTP/1.1 in RFC 2616 does not
            // include this information. Neither is the "Connection" header
            // mentioned in RFC 1945, which informally describes HTTP/1.0.
            //
            // RFC 2616 specifies "close" as the only connection token with a
            // specific meaning: it disables persistent connections.
            //
            // The "Proxy-Connection" header is not formally specified anywhere,
            // but is commonly used to carry one token, "close" or "keep-alive".
            // The "Connection" header, on the other hand, is defined as a
            // sequence of tokens, where each token is a header name, and the
            // token "close" has the above-mentioned additional meaning.
            //
            // To get through this mess, we treat the "Proxy-Connection" header
            // in exactly the same way as the "Connection" header, but only if
            // the latter is missing. We scan the sequence of tokens for both
            // "close" and "keep-alive". As "close" is specified by RFC 2068,
            // it takes precedence and indicates a non-persistent connection.
            // If there is no "close" but a "keep-alive", we take the hint.
            if (hit.HasNext())
            {
                try
                {
                    TokenIterator ti        = CreateTokenIterator(hit);
                    bool          keepalive = false;
                    while (ti.HasNext())
                    {
                        string token = ti.NextToken();
                        if (Sharpen.Runtime.EqualsIgnoreCase(HTTP.ConnClose, token))
                        {
                            return(false);
                        }
                        else
                        {
                            if (Sharpen.Runtime.EqualsIgnoreCase(HTTP.ConnKeepAlive, token))
                            {
                                // continue the loop, there may be a "close" afterwards
                                keepalive = true;
                            }
                        }
                    }
                    if (keepalive)
                    {
                        return(true);
                    }
                }
                catch (ParseException)
                {
                    // neither "close" nor "keep-alive", use default policy
                    // invalid connection header means no persistent connection
                    // we don't have logging in HttpCore, so the exception is lost
                    return(false);
                }
            }
            // default since HTTP/1.1 is persistent, before it was non-persistent
            return(!ver.LessEquals(HttpVersion.Http10));
        }
コード例 #8
0
 /// <summary>Creates a token iterator from a header iterator.</summary>
 /// <remarks>
 /// Creates a token iterator from a header iterator.
 /// This method can be overridden to replace the implementation of
 /// the token iterator.
 /// </remarks>
 /// <param name="hit">the header iterator</param>
 /// <returns>the token iterator</returns>
 protected internal virtual TokenIterator CreateTokenIterator(HeaderIterator hit)
 {
     return(new BasicTokenIterator(hit));
 }
コード例 #9
0
 public BasicHeaderElementIterator(HeaderIterator headerIterator) : this(headerIterator
                                                                         , BasicHeaderValueParser.Instance)
 {
 }
コード例 #10
0
 /// <summary>Creates a new instance of BasicHeaderElementIterator</summary>
 public BasicHeaderElementIterator(HeaderIterator headerIterator, HeaderValueParser
                                   parser)
 {
     this.headerIt = Args.NotNull(headerIterator, "Header iterator");
     this.parser   = Args.NotNull(parser, "Parser");
 }