Beispiel #1
0
        /// <summary>
        /// Read <c>nb_bytes</c> bytes from the socket,
        /// and send it to the specified packet handler
        /// </summary>
        /// <returns>The number of bytes sent</returns>
        public uint TunnelDataTo(MessagePacketHandler mph, uint nb_bytes)
        {
            uint total_sent = 0;

            while (nb_bytes > 0)
            {
                if (AvailableData == 0)
                {
                    if (ReadRaw() == 0)
                    {
                        throw new IoBroken();
                    }
                }
                uint to_send = AvailableData;
                if (to_send > nb_bytes)
                {
                    UseLeftOverBytes = true;
                    to_send          = nb_bytes;
                }
                mph(Buffer, BufferPosition, to_send);
                total_sent     += to_send;
                nb_bytes       -= to_send;
                AvailableData  -= to_send;
                BufferPosition += to_send;
            }

            return(total_sent);
        }
Beispiel #2
0
        /// <summary>
        ///     Read <c>nb_bytes</c> bytes from the socket,
        ///     and send it to the specified packet handler
        /// </summary>
        /// <returns>The number of bytes sent</returns>
        public uint TunnelDataTo(MessagePacketHandler mph, uint nb_bytes)
        {
            uint total_sent = 0;

            Response = new List <byte[]>();
            while (nb_bytes > 0)
            {
                if (AvailableData == 0)
                {
                    if (ReadRaw() == 0)
                    {
                        throw new IoBroken();
                    }
                }
                uint to_send = AvailableData;
                if (to_send > nb_bytes)
                {
                    UseLeftOverBytes = true;
                    to_send          = nb_bytes;
                }
                mph(SocketBuffer, BufferPosition, to_send);
                var buf = new byte[to_send];
                Array.Copy(SocketBuffer, BufferPosition, buf, 0, to_send);
                Response.Add(buf);
                total_sent     += to_send;
                nb_bytes       -= to_send;
                AvailableData  -= to_send;
                BufferPosition += to_send;
            }

            return(total_sent);
        }
Beispiel #3
0
        /// <summary>
        ///     Transfer data from the socket to the specified packet handler
        ///     until the socket closes
        /// </summary>
        /// <returns>The number of bytes sent</returns>
        public uint TunnelDataTo(MessagePacketHandler mph)
        {
            uint total_sent = 0;

            try {
                if (AvailableData == 0)
                {
                    ReadRaw();
                }
                Response = new List <byte[]>();
                var i = 0;
                while (AvailableData > 0)
                {
                    mph(SocketBuffer, BufferPosition, AvailableData);
                    var buffer = new byte[AvailableData];
                    Array.Copy(SocketBuffer, (int)BufferPosition, buffer, 0, AvailableData);
                    Response.Add(buffer);
                    i++;
                    total_sent += AvailableData;
                    ReadRaw();
                }
            }
            catch (SocketException) {
                /* ignore */
            }

            return(total_sent);
        }
Beispiel #4
0
        /* Helper function */

        private void TunnelChunkedDataTo(SocketState dest, MessagePacketHandler mph)
        {
            // (RFC 2616, sections 3.6.1, 19.4.6)
            while (true)
            {
                string chunk_header = ReadAsciiLine();
                if (chunk_header.Length == 0)
                {
                    throw new HttpProtocolBroken("Expected chunk header missing");
                }
                int    sc        = chunk_header.IndexOfAny(c_ChunkSizeEnd);
                string hexa_size = sc > -1
                                       ? chunk_header.Substring(0, sc)
                                       : chunk_header;
                uint size;
                try {
                    size = Convert.ToUInt32(hexa_size, 16);
                }
                catch {
                    string s = chunk_header.Length > 20
                                   ? (chunk_header.Substring(0, 17) + "...")
                                   : chunk_header;
                    throw new HttpProtocolBroken(
                              "Could not parse chunk size in: " + s);
                }

                if (dest != null)
                {
                    dest.WriteAsciiLine(chunk_header);
                }
                if (size == 0)
                {
                    break;
                }
                TunnelDataTo(mph, size);
                // Read/write one more CRLF
                string new_line = ReadAsciiLine();
                Debug.Assert(new_line.Length == 0);
                if (dest != null)
                {
                    dest.WriteAsciiLine(new_line);
                }
            }
            string line;

            do
            {
                // Tunnel any trailing entity headers
                line = ReadAsciiLine();
                if (dest != null)
                {
                    dest.WriteAsciiLine(line);
                }
            } while (line.Length != 0);
        }
Beispiel #5
0
        /// <summary>
        /// Transfer data from the socket to the specified packet handler
        /// until the socket closes
        /// </summary>
        /// <returns>The number of bytes sent</returns>
        public uint TunnelDataTo(MessagePacketHandler mph)
        {
            uint total_sent = 0;

            try
            {
                if (AvailableData == 0)
                {
                    ReadRaw();
                }
                while (AvailableData > 0)
                {
                    mph(Buffer, BufferPosition, AvailableData);
                    total_sent += AvailableData;
                    ReadRaw();
                }
            }
            catch (SocketException) { /* ignore */ }

            return(total_sent);
        }
Beispiel #6
0
 /// <summary>
 /// Tunnel a HTTP-chunked blob of data to the specified packet handler
 /// </summary>
 /// <remarks>
 /// The tunneling stops when the last chunk, identified by a
 /// size of 0, arrives. The optional trailing entities are also
 /// transmitted (but otherwise ignored).
 /// </remarks>
 public void TunnelChunkedDataTo(MessagePacketHandler mph)
 {
     TunnelChunkedDataTo(null, mph);
 }
Beispiel #7
0
 /// <summary>
 /// Sends a buffer to the specified packet handler
 /// </summary>
 /// <returns>The number of bytes sent</returns>
 public uint TunnelDataTo(MessagePacketHandler mph, byte[] buffer)
 {
     mph(buffer, 0, (uint)buffer.Length);
     return((uint)buffer.Length);
 }
Beispiel #8
0
        /* Helper function */
        void TunnelChunkedDataTo(HttpSocket dest, MessagePacketHandler mph)
        {
            // (RFC 2616, sections 3.6.1, 19.4.6)
            while (true)
            {
                string chunk_header = ReadAsciiLine();
                if (chunk_header.Length == 0)
                    throw new HttpProtocolBroken(
                        "Expected chunk header missing");
                int sc = chunk_header.IndexOfAny(c_ChunkSizeEnd);
                string hexa_size;
                if (sc > -1)
                    // We have chunk extensions: ignore them
                    hexa_size = chunk_header.Substring(0, sc);
                else
                    hexa_size = chunk_header;
                uint size;
                try
                {
                    size = Convert.ToUInt32(hexa_size, 16);
                }
                catch
                {
                    string s = chunk_header.Length > 20
                        ? (chunk_header.Substring(0, 17) + "...")
                        : chunk_header;
                    throw new HttpProtocolBroken(
                        "Could not parse chunk size in: " + s);
                }

                if (dest != null)
                    dest.WriteAsciiLine(chunk_header);
                if (size == 0)
                    break;
                TunnelDataTo(mph, size);
                // Read/write one more CRLF
                string new_line = ReadAsciiLine();
                System.Diagnostics.Debug.Assert(new_line.Length == 0);
                if (dest != null)
                    dest.WriteAsciiLine(new_line);
            }
            string line;
            do
            {
                // Tunnel any trailing entity headers
                line = ReadAsciiLine();
                if (dest != null)
                    dest.WriteAsciiLine(line);
            } while (line.Length != 0);
        }
Beispiel #9
0
 /// <summary>
 /// Sends a buffer to the specified packet handler
 /// </summary>
 /// <returns>The number of bytes sent</returns>
 public uint TunnelDataTo(MessagePacketHandler mph, byte[] buffer)
 {
     mph(buffer, 0, (uint)buffer.Length);
     return (uint)buffer.Length;
 }
Beispiel #10
0
        /// <summary>
        /// Read <c>nb_bytes</c> bytes from the socket,
        /// and send it to the specified packet handler
        /// </summary>
        /// <returns>The number of bytes sent</returns>
        public uint TunnelDataTo(MessagePacketHandler mph, uint nb_bytes)
        {
            uint total_sent = 0;
            while (nb_bytes > 0)
            {
                if (AvailableData == 0)
                    if (ReadRaw() == 0)
                        throw new IoBroken();
                uint to_send = AvailableData;
                if (to_send > nb_bytes)
                {
                    UseLeftOverBytes = true;
                    to_send = nb_bytes;
                }
                mph(Buffer, BufferPosition, to_send);
                total_sent += to_send;
                nb_bytes -= to_send;
                AvailableData -= to_send;
                BufferPosition += to_send;
            }

            return total_sent;
        }
Beispiel #11
0
        /// <summary>
        /// Transfer data from the socket to the specified packet handler
        /// until the socket closes
        /// </summary>
        /// <returns>The number of bytes sent</returns>
        public uint TunnelDataTo(MessagePacketHandler mph)
        {
            uint total_sent = 0;

            try
            {
                if (AvailableData == 0)
                    ReadRaw();
                while (AvailableData > 0)
                {
                    mph(Buffer, BufferPosition, AvailableData);
                    total_sent += AvailableData;
                    ReadRaw();
                }
            }
            catch (SocketException) { /* ignore */ }

            return total_sent;
        }
Beispiel #12
0
 /// <summary>
 /// Tunnel a HTTP-chunked blob of data to the specified packet handler
 /// </summary>
 /// <remarks>
 /// The tunneling stops when the last chunk, identified by a
 /// size of 0, arrives. The optional trailing entities are also
 /// transmitted (but otherwise ignored).
 /// </remarks>
 public void TunnelChunkedDataTo(MessagePacketHandler mph)
 {
     TunnelChunkedDataTo(null, mph);
 }
Beispiel #13
0
        /// <summary>
        ///     Read <c>nb_bytes</c> bytes from the socket,
        ///     and send it to the specified packet handler
        /// </summary>
        /// <returns>The number of bytes sent</returns>
        public uint TunnelDataTo(MessagePacketHandler mph, uint nb_bytes)
        {
            uint total_sent = 0;
            Response = new List<byte[]>();
            while (nb_bytes > 0) {
                if (AvailableData == 0) {
                    if (ReadRaw() == 0) {
                        throw new IoBroken();
                    }
                }
                uint to_send = AvailableData;
                if (to_send > nb_bytes) {
                    UseLeftOverBytes = true;
                    to_send = nb_bytes;
                }
                mph(SocketBuffer, BufferPosition, to_send);
                var buf = new byte[to_send];
                Array.Copy(SocketBuffer, BufferPosition, buf, 0, to_send);
                Response.Add(buf);
                total_sent += to_send;
                nb_bytes -= to_send;
                AvailableData -= to_send;
                BufferPosition += to_send;
            }

            return total_sent;
        }
Beispiel #14
0
        /// <summary>
        ///     Transfer data from the socket to the specified packet handler
        ///     until the socket closes
        /// </summary>
        /// <returns>The number of bytes sent</returns>
        public uint TunnelDataTo(MessagePacketHandler mph)
        {
            uint total_sent = 0;

            try {
                if (AvailableData == 0) {
                    ReadRaw();
                }
                Response = new List<byte[]>();
                var i = 0;
                while (AvailableData > 0) {
                    mph(SocketBuffer, BufferPosition, AvailableData);
                    var buffer = new byte[AvailableData];
                    Array.Copy(SocketBuffer, (int)BufferPosition, buffer, 0, AvailableData);
                    Response.Add(buffer);
                    i++;
                    total_sent += AvailableData;
                    ReadRaw();
                }
            }
            catch (SocketException) {
                /* ignore */
            }

            return total_sent;
        }