Ejemplo n.º 1
0
        public void ComplexEmbeddingTest()
        {
            //Set up tokenizer
            WaebricLexer lexer = new WaebricLexer(new StringReader("\"<a(href=\"http://www.microsoft.com\") \"Microsoft Corp\">\""));

            lexer.LexicalizeStream();

            TokenIterator tokens = lexer.GetTokenIterator();

            //Test token
            Assert.AreEqual(1, tokens.GetSize());
            Assert.AreEqual(TokenType.EMBEDDING, tokens.Peek(1).GetType());

            //Test tokens in embedding
            EmbeddingToken embeddingToken  = (EmbeddingToken)tokens.NextToken();
            TokenIterator  embeddingTokens = embeddingToken.GetTokenIterator();

            Assert.AreEqual(12, embeddingTokens.GetSize());
            Assert.AreEqual("\"", embeddingTokens.Peek(1).GetValue().ToString());
            Assert.AreEqual("", embeddingTokens.Peek(2).GetValue().ToString());
            Assert.AreEqual("<", embeddingTokens.Peek(3).GetValue().ToString());
            Assert.AreEqual("a", embeddingTokens.Peek(4).GetValue().ToString());
            Assert.AreEqual("(", embeddingTokens.Peek(5).GetValue().ToString());
            Assert.AreEqual("href", embeddingTokens.Peek(6).GetValue().ToString());
            Assert.AreEqual("=", embeddingTokens.Peek(7).GetValue().ToString());
            Assert.AreEqual("http://www.microsoft.com", embeddingTokens.Peek(8).GetValue().ToString());
            Assert.AreEqual(")", embeddingTokens.Peek(9).GetValue().ToString());
            Assert.AreEqual("Microsoft Corp", embeddingTokens.Peek(10).GetValue().ToString());
            Assert.AreEqual(">", embeddingTokens.Peek(11).GetValue().ToString());
            Assert.AreEqual("\"", embeddingTokens.Peek(12).GetValue().ToString());
        }
Ejemplo n.º 2
0
        public void EmbeddingTest()
        {
            //Set up tokenizer
            WaebricLexer lexer = new WaebricLexer(new StringReader("\"pre<\"\\\">\">post\""));

            lexer.LexicalizeStream();

            TokenIterator tokens = lexer.GetTokenIterator();

            //Test token
            Assert.AreEqual(1, tokens.GetSize());
            Assert.AreEqual(TokenType.EMBEDDING, tokens.Peek(1).GetType());

            //Get embedding and test inner tokens
            EmbeddingToken parsedToken     = (EmbeddingToken)tokens.NextToken();
            TokenIterator  embeddingTokens = parsedToken.GetTokenIterator();

            Assert.AreEqual(7, embeddingTokens.GetSize());
            Assert.AreEqual("\"", embeddingTokens.Peek(1).GetValue().ToString());
            Assert.AreEqual("pre", embeddingTokens.Peek(2).GetValue().ToString());
            Assert.AreEqual("<", embeddingTokens.Peek(3).GetValue().ToString());
            Assert.AreEqual("\\\">", embeddingTokens.Peek(4).GetValue().ToString());
            Assert.AreEqual(">", embeddingTokens.Peek(5).GetValue().ToString());
            Assert.AreEqual("post", embeddingTokens.Peek(6).GetValue().ToString());
            Assert.AreEqual("\"", embeddingTokens.Peek(7).GetValue().ToString());
        }
Ejemplo n.º 3
0
        public void NextTokenTest()
        {
            List <Token>  tokenStream = null;                           // TODO: Initialize to an appropriate value
            TokenIterator target      = new TokenIterator(tokenStream); // TODO: Initialize to an appropriate value
            Token         expected    = null;                           // TODO: Initialize to an appropriate value
            Token         actual;

            actual = target.NextToken();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
Ejemplo n.º 4
0
        public void HasNextTest()
        {
            //Create list and add dummy tokens to it
            List <Token> tokenStream = new List <Token>();
            Token        token1      = new Token();
            Token        token2      = new Token();

            tokenStream.Add(token1);
            tokenStream.Add(token2);

            //Create iterator with tokenstream and then test some things out
            TokenIterator iterator = new TokenIterator(tokenStream);

            //Check size
            Assert.IsTrue(iterator.GetSize() == tokenStream.Count);

            //Check if tokens are in list
            Assert.IsTrue(iterator.HasNext());

            //Check also if there are 2 items in list
            Assert.IsTrue(iterator.HasNext(2));

            //Get first token and check

            Token CurrentToken = iterator.NextToken();

            Assert.AreEqual(token1, CurrentToken);

            //Get second token and check
            Assert.IsTrue(iterator.HasNext());
            CurrentToken = iterator.NextToken();
            Assert.AreEqual(token2, CurrentToken);

            //No items in list left
            Assert.IsFalse(iterator.HasNext());
        }
Ejemplo n.º 5
0
        public void WaebricSymbolTokenTest()
        {
            //Set up tokenizer
            WaebricLexer lexer = new WaebricLexer(new StringReader("'test"));

            lexer.LexicalizeStream();

            TokenIterator tokens = lexer.GetTokenIterator();

            //Test token
            Assert.AreEqual(1, tokens.GetSize());
            Token token = tokens.NextToken();

            Assert.AreEqual(TokenType.WAEBRICSYMBOL, token.GetType());
            Assert.AreEqual("test", token.GetValue().ToString());
        }
        // 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));
        }