Exemplo n.º 1
0
 /// <summary>Creates an instance of this class.</summary>
 /// <remarks>Creates an instance of this class.</remarks>
 /// <param name="buffer">the session input buffer.</param>
 /// <param name="lineParser">the line parser.</param>
 /// <param name="requestFactory">
 /// the factory to use to create
 /// <see cref="Org.Apache.Http.IHttpRequest">Org.Apache.Http.IHttpRequest</see>
 /// s.
 /// </param>
 /// <param name="params">HTTP parameters.</param>		[System.ObsoleteAttribute(@"(4.3) useDefaultHttpRequestParser(Org.Apache.Http.IO.SessionInputBuffer, Org.Apache.Http.Message.LineParser, Org.Apache.Http.HttpRequestFactory, Org.Apache.Http.Config.MessageConstraints)")]
 public DefaultHttpRequestParser(SessionInputBuffer buffer, LineParser lineParser,
                                 HttpRequestFactory requestFactory, HttpParams @params) : base(buffer, lineParser
                                                                                               , @params)
 {
     this.requestFactory = Args.NotNull(requestFactory, "Request factory");
     this.lineBuf        = new CharArrayBuffer(128);
 }
Exemplo n.º 2
0
 public DefaultHttpResponseParser(SessionInputBuffer buffer, LineParser parser, HttpResponseFactory
                                  responseFactory, HttpParams @params) : base(buffer, parser, @params)
 {
     Args.NotNull(responseFactory, "Response factory");
     this.responseFactory = responseFactory;
     this.lineBuf         = new CharArrayBuffer(128);
 }
Exemplo n.º 3
0
 /// <summary>Wraps session input stream and reads chunk coded input.</summary>
 /// <remarks>Wraps session input stream and reads chunk coded input.</remarks>
 /// <param name="in">The session input buffer</param>
 public ChunkedInputStream(SessionInputBuffer @in) : base()
 {
     this.@in    = Args.NotNull(@in, "Session input buffer");
     this.pos    = 0;
     this.buffer = new CharArrayBuffer(16);
     this.state  = ChunkLen;
 }
Exemplo n.º 4
0
 /// <summary>Creates new instance of DefaultHttpRequestParser.</summary>
 /// <remarks>Creates new instance of DefaultHttpRequestParser.</remarks>
 /// <param name="buffer">the session input buffer.</param>
 /// <param name="lineParser">
 /// the line parser. If <code>null</code>
 /// <see cref="Org.Apache.Http.Message.BasicLineParser.Instance">Org.Apache.Http.Message.BasicLineParser.Instance
 ///     </see>
 /// will be used.
 /// </param>
 /// <param name="requestFactory">
 /// the response factory. If <code>null</code>
 /// <see cref="Org.Apache.Http.Impl.DefaultHttpRequestFactory.Instance">Org.Apache.Http.Impl.DefaultHttpRequestFactory.Instance
 ///     </see>
 /// will be used.
 /// </param>
 /// <param name="constraints">
 /// the message constraints. If <code>null</code>
 /// <see cref="Org.Apache.Http.Config.MessageConstraints.Default">Org.Apache.Http.Config.MessageConstraints.Default
 ///     </see>
 /// will be used.
 /// </param>
 /// <since>4.3</since>
 public DefaultHttpRequestParser(SessionInputBuffer buffer, LineParser lineParser,
                                 HttpRequestFactory requestFactory, MessageConstraints constraints) : base(buffer
                                                                                                           , lineParser, constraints)
 {
     this.requestFactory = requestFactory != null ? requestFactory : DefaultHttpRequestFactory
                           .Instance;
     this.lineBuf = new CharArrayBuffer(128);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Parses HTTP headers from the data receiver stream according to the generic
        /// format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3.
        /// </summary>
        /// <remarks>
        /// Parses HTTP headers from the data receiver stream according to the generic
        /// format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3.
        /// </remarks>
        /// <param name="inbuffer">Session input buffer</param>
        /// <param name="maxHeaderCount">
        /// maximum number of headers allowed. If the number
        /// of headers received from the data stream exceeds maxCount value, an
        /// IOException will be thrown. Setting this parameter to a negative value
        /// or zero will disable the check.
        /// </param>
        /// <param name="maxLineLen">
        /// maximum number of characters for a header line,
        /// including the continuation lines. Setting this parameter to a negative
        /// value or zero will disable the check.
        /// </param>
        /// <returns>array of HTTP headers</returns>
        /// <param name="parser">
        /// line parser to use. Can be <code>null</code>, in which case
        /// the default implementation of this interface will be used.
        /// </param>
        /// <exception cref="System.IO.IOException">in case of an I/O error</exception>
        /// <exception cref="Org.Apache.Http.HttpException">in case of HTTP protocol violation
        ///     </exception>
        public static Header[] ParseHeaders(SessionInputBuffer inbuffer, int maxHeaderCount
                                            , int maxLineLen, LineParser parser)
        {
            IList <CharArrayBuffer> headerLines = new AList <CharArrayBuffer>();

            return(ParseHeaders(inbuffer, maxHeaderCount, maxLineLen, parser != null ? parser
                                 : BasicLineParser.Instance, headerLines));
        }
Exemplo n.º 6
0
 /// <summary>Creates new instance of AbstractMessageParser.</summary>
 /// <remarks>Creates new instance of AbstractMessageParser.</remarks>
 /// <param name="buffer">the session input buffer.</param>
 /// <param name="lineParser">
 /// the line parser. If <code>null</code>
 /// <see cref="Org.Apache.Http.Message.BasicLineParser.Instance">Org.Apache.Http.Message.BasicLineParser.Instance
 ///     </see>
 /// will be used.
 /// </param>
 /// <param name="constraints">
 /// the message constraints. If <code>null</code>
 /// <see cref="Org.Apache.Http.Config.MessageConstraints.Default">Org.Apache.Http.Config.MessageConstraints.Default
 ///     </see>
 /// will be used.
 /// </param>
 /// <since>4.3</since>
 public AbstractMessageParser(SessionInputBuffer buffer, LineParser lineParser, MessageConstraints
                              constraints) : base()
 {
     this.sessionBuffer      = Args.NotNull(buffer, "Session input buffer");
     this.lineParser         = lineParser != null ? lineParser : BasicLineParser.Instance;
     this.messageConstraints = constraints != null ? constraints : MessageConstraints.
                               Default;
     this.headerLines = new AList <CharArrayBuffer>();
     this.state       = HeadLine;
 }
Exemplo n.º 7
0
 /// <summary>Creates an instance of AbstractMessageParser.</summary>
 /// <remarks>Creates an instance of AbstractMessageParser.</remarks>
 /// <param name="buffer">the session input buffer.</param>
 /// <param name="parser">the line parser.</param>
 /// <param name="params">HTTP parameters.</param>		[System.ObsoleteAttribute(@"(4.3) use AbstractMessageParser{T}.AbstractMessageParser(Org.Apache.Http.IO.SessionInputBuffer, Org.Apache.Http.Message.LineParser, Org.Apache.Http.Config.MessageConstraints)")]
 public AbstractMessageParser(SessionInputBuffer buffer, LineParser parser, HttpParams
                              @params) : base()
 {
     Args.NotNull(buffer, "Session input buffer");
     Args.NotNull(@params, "HTTP parameters");
     this.sessionBuffer      = buffer;
     this.messageConstraints = HttpParamConfig.GetMessageConstraints(@params);
     this.lineParser         = (parser != null) ? parser : BasicLineParser.Instance;
     this.headerLines        = new AList <CharArrayBuffer>();
     this.state = HeadLine;
 }
Exemplo n.º 8
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Org.Apache.Http.HttpException"></exception>
        /// <exception cref="Org.Apache.Http.ParseException"></exception>
        protected internal override IHttpRequest ParseHead(SessionInputBuffer sessionBuffer
                                                           )
        {
            this.lineBuf.Clear();
            int i = sessionBuffer.ReadLine(this.lineBuf);

            if (i == -1)
            {
                throw new ConnectionClosedException("Client closed connection");
            }
            ParserCursor cursor      = new ParserCursor(0, this.lineBuf.Length());
            RequestLine  requestline = this.lineParser.ParseRequestLine(this.lineBuf, cursor);

            return(this.requestFactory.NewHttpRequest(requestline));
        }
Exemplo n.º 9
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Org.Apache.Http.HttpException"></exception>
        /// <exception cref="Org.Apache.Http.ParseException"></exception>
        protected internal override HttpResponse ParseHead(SessionInputBuffer sessionBuffer
                                                           )
        {
            this.lineBuf.Clear();
            int i = sessionBuffer.ReadLine(this.lineBuf);

            if (i == -1)
            {
                throw new NoHttpResponseException("The target server failed to respond");
            }
            //create the status line from the status string
            ParserCursor cursor     = new ParserCursor(0, this.lineBuf.Length());
            StatusLine   statusline = lineParser.ParseStatusLine(this.lineBuf, cursor);

            return(this.responseFactory.NewHttpResponse(statusline, null));
        }
 protected internal virtual InputStream CreateInputStream(long len, SessionInputBuffer
                                                          inbuffer)
 {
     if (len == ContentLengthStrategy.Chunked)
     {
         return(new ChunkedInputStream(inbuffer));
     }
     else
     {
         if (len == ContentLengthStrategy.Identity)
         {
             return(new IdentityInputStream(inbuffer));
         }
         else
         {
             return(new ContentLengthInputStream(inbuffer, len));
         }
     }
 }
Exemplo n.º 11
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Apache.Http.HttpException"></exception>
        protected override HttpResponse ParseHead(SessionInputBuffer sessionBuffer)
        {
            //read out the HTTP status string
            int          count  = 0;
            ParserCursor cursor = null;

            do
            {
                // clear the buffer
                this.lineBuf.Clear();
                int i = sessionBuffer.ReadLine(this.lineBuf);
                if (i == -1 && count == 0)
                {
                    // The server just dropped connection on us
                    throw new NoHttpResponseException("The target server failed to respond");
                }
                cursor = new ParserCursor(0, this.lineBuf.Length());
                if (lineParser.HasProtocolVersion(this.lineBuf, cursor))
                {
                    // Got one
                    break;
                }
                else
                {
                    if (i == -1 || Reject(this.lineBuf, count))
                    {
                        // Giving up
                        throw new ProtocolException("The server failed to respond with a " + "valid HTTP response"
                                                    );
                    }
                }
                if (this.log.IsDebugEnabled())
                {
                    this.log.Debug("Garbage in response: " + this.lineBuf.ToString());
                }
                count++;
            }while (true);
            //create the status line from the status string
            StatusLine statusline = lineParser.ParseStatusLine(this.lineBuf, cursor);

            return(this.responseFactory.NewHttpResponse(statusline, null));
        }
 /// <summary>Wraps session input stream and reads input until the the end of stream.</summary>
 /// <remarks>Wraps session input stream and reads input until the the end of stream.</remarks>
 /// <param name="in">The session input buffer</param>
 public IdentityInputStream(SessionInputBuffer @in) : base()
 {
     this.@in = Args.NotNull(@in, "Session input buffer");
 }
Exemplo n.º 13
0
 public virtual HttpMessageParser <HttpResponse> Create(SessionInputBuffer buffer,
                                                        MessageConstraints constraints)
 {
     return(new DefaultHttpResponseParser(buffer, lineParser, responseFactory, constraints
                                          ));
 }
Exemplo n.º 14
0
        /// <summary>
        /// Parses HTTP headers from the data receiver stream according to the generic
        /// format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3.
        /// </summary>
        /// <remarks>
        /// Parses HTTP headers from the data receiver stream according to the generic
        /// format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3.
        /// </remarks>
        /// <param name="inbuffer">Session input buffer</param>
        /// <param name="maxHeaderCount">
        /// maximum number of headers allowed. If the number
        /// of headers received from the data stream exceeds maxCount value, an
        /// IOException will be thrown. Setting this parameter to a negative value
        /// or zero will disable the check.
        /// </param>
        /// <param name="maxLineLen">
        /// maximum number of characters for a header line,
        /// including the continuation lines. Setting this parameter to a negative
        /// value or zero will disable the check.
        /// </param>
        /// <param name="parser">line parser to use.</param>
        /// <param name="headerLines">
        /// List of header lines. This list will be used to store
        /// intermediate results. This makes it possible to resume parsing of
        /// headers in case of a
        /// <see cref="System.Threading.ThreadInterruptedException">System.Threading.ThreadInterruptedException
        ///     </see>
        /// .
        /// </param>
        /// <returns>array of HTTP headers</returns>
        /// <exception cref="System.IO.IOException">in case of an I/O error</exception>
        /// <exception cref="Org.Apache.Http.HttpException">in case of HTTP protocol violation
        ///     </exception>
        /// <since>4.1</since>
        public static Header[] ParseHeaders(SessionInputBuffer inbuffer, int maxHeaderCount
                                            , int maxLineLen, LineParser parser, IList <CharArrayBuffer> headerLines)
        {
            Args.NotNull(inbuffer, "Session input buffer");
            Args.NotNull(parser, "Line parser");
            Args.NotNull(headerLines, "Header line list");
            CharArrayBuffer current  = null;
            CharArrayBuffer previous = null;

            for (; ;)
            {
                if (current == null)
                {
                    current = new CharArrayBuffer(64);
                }
                else
                {
                    current.Clear();
                }
                int l = inbuffer.ReadLine(current);
                if (l == -1 || current.Length() < 1)
                {
                    break;
                }
                // Parse the header name and value
                // Check for folded headers first
                // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
                // discussion on folded headers
                if ((current.CharAt(0) == ' ' || current.CharAt(0) == '\t') && previous != null)
                {
                    // we have continuation folded header
                    // so append value
                    int i = 0;
                    while (i < current.Length())
                    {
                        char ch = current.CharAt(i);
                        if (ch != ' ' && ch != '\t')
                        {
                            break;
                        }
                        i++;
                    }
                    if (maxLineLen > 0 && previous.Length() + 1 + current.Length() - i > maxLineLen)
                    {
                        throw new MessageConstraintException("Maximum line length limit exceeded");
                    }
                    previous.Append(' ');
                    previous.Append(current, i, current.Length() - i);
                }
                else
                {
                    headerLines.AddItem(current);
                    previous = current;
                    current  = null;
                }
                if (maxHeaderCount > 0 && headerLines.Count >= maxHeaderCount)
                {
                    throw new MessageConstraintException("Maximum header count exceeded");
                }
            }
            Header[] headers = new Header[headerLines.Count];
            for (int i_1 = 0; i_1 < headerLines.Count; i_1++)
            {
                CharArrayBuffer buffer = headerLines[i_1];
                try
                {
                    headers[i_1] = parser.ParseHeader(buffer);
                }
                catch (ParseException ex)
                {
                    throw new ProtocolException(ex.Message);
                }
            }
            return(headers);
        }
 /// <summary>
 /// Wraps a session input buffer and cuts off output after a defined number
 /// of bytes.
 /// </summary>
 /// <remarks>
 /// Wraps a session input buffer and cuts off output after a defined number
 /// of bytes.
 /// </remarks>
 /// <param name="in">The session input buffer</param>
 /// <param name="contentLength">
 /// The maximum number of bytes that can be read from
 /// the stream. Subsequent read operations will return -1.
 /// </param>
 public ContentLengthInputStream(SessionInputBuffer @in, long contentLength) : base
         ()
 {
     this.@in           = Args.NotNull(@in, "Session input buffer");
     this.contentLength = Args.NotNegative(contentLength, "Content length");
 }
Exemplo n.º 16
0
 /// <summary>
 /// Subclasses must override this method to generate an instance of
 /// <see cref="Org.Apache.Http.HttpMessage">Org.Apache.Http.HttpMessage</see>
 /// based on the initial input from the session buffer.
 /// <p>
 /// Usually this method is expected to read just the very first line or
 /// the very first valid from the data stream and based on the input generate
 /// an appropriate instance of
 /// <see cref="Org.Apache.Http.HttpMessage">Org.Apache.Http.HttpMessage</see>
 /// .
 /// </summary>
 /// <param name="sessionBuffer">the session input buffer.</param>
 /// <returns>HTTP message based on the input from the session buffer.</returns>
 /// <exception cref="System.IO.IOException">in case of an I/O error.</exception>
 /// <exception cref="Org.Apache.Http.HttpException">in case of HTTP protocol violation.
 ///     </exception>
 /// <exception cref="Org.Apache.Http.ParseException">in case of a parse error.</exception>
 protected internal abstract T ParseHead(SessionInputBuffer sessionBuffer);
Exemplo n.º 17
0
 /// <since>4.3</since>
 public DefaultHttpRequestParser(SessionInputBuffer buffer, MessageConstraints constraints
                                 ) : this(buffer, null, null, constraints)
 {
 }
Exemplo n.º 18
0
 /// <since>4.3</since>
 public DefaultHttpResponseParser(SessionInputBuffer buffer) : this(buffer, null,
                                                                    null, MessageConstraints.Default)
 {
 }