Example #1
0
        internal virtual bool readHeader(ref HTTPHeader header)
        {
            // Read available data
            int dataLen = 0;
            var stream  = getStream();

            if (stream == null)
            {
                throw new Exception("Could not access network stream");
            }
            byte[] data = new byte[READ_BUFFER_LENGTH];
            try
            {
                dataLen = stream.Read(data, 0, READ_BUFFER_LENGTH);

                if (dataLen == 0)
                {
                    return(false); // If it is disconnect
                }
                if (header == null)
                {
                    header = new HTTPHeader(Encoding.ASCII.GetString(data, 0, dataLen));
                    if (header.m_headerStatus == HTTPHeader.STATUS.UNINITIALIZED)
                    {
                        return(false); //should only happen if the constructor's invocation of Append did not happen as desired
                    }
                }
                else if (header.Append(Encoding.ASCII.GetString(data, 0, dataLen)) == HTTPHeader.STATUS.PARTIAL_HEADER)
                {
                    return(true); //if we successfully append a piece of the header, return true, but DO NOT change states
                }
            }
            catch (SocketException ex)
            {
                XmlRpcUtil.error("XmlRpcServerConnection::readHeader: error while reading header ({0}).", ex.Message);
                return(false);
            }
            catch (Exception ex)
            {
                XmlRpcUtil.error("XmlRpcServerConnection::readHeader: error while reading header ({0}).", ex.Message);
                return(false);
            }

            if (header.m_headerStatus != HTTPHeader.STATUS.COMPLETE_HEADER)
            {
                return(false);
            }

            return(true);
        }
Example #2
0
        internal override bool readHeader(ref HTTPHeader header)
        {
            if (base.readHeader(ref header))
            {
                if (header.m_headerStatus == HTTPHeader.STATUS.COMPLETE_HEADER)
                {
                    _connectionState = ConnectionState.READ_RESPONSE;
                }

                return(true);
            }

            return(false);
        }
        internal override bool readHeader(ref HTTPHeader header)
        {
            if (base.readHeader(ref header))
            {
                if (header.m_headerStatus == HTTPHeader.STATUS.COMPLETE_HEADER)
                {
                    XmlRpcUtil.log(XmlRpcUtil.XMLRPC_LOG_LEVEL.DEBUG, "KeepAlive: {0}", _keepAlive);
                    _connectionState = ServerConnectionState.READ_REQUEST;
                }

                return(true);
            }

            return(false);
        }
        internal override bool readHeader(ref HTTPHeader header)
        {
            if (base.readHeader(ref header))
            {
                if (header.m_headerStatus == HTTPHeader.STATUS.COMPLETE_HEADER)
                {
                    XmlRpcUtil.log(XmlRpcUtil.XMLRPC_LOG_LEVEL.DEBUG, "KeepAlive: {0}", _keepAlive);
                    _connectionState = ServerConnectionState.READ_REQUEST;
                }

                return true;
            }

            return false;
        }
Example #5
0
        private bool writeRequest()
        {
            if (_bytesWritten == 0)
            {
                XmlRpcUtil.log(XmlRpcUtil.XMLRPC_LOG_LEVEL.SPEW, "XmlRpcClient::writeRequest (attempt {0}):\n{1}\n", _sendAttempts + 1, _request);
            }
            // Try to write the request
            try
            {
                if (!socket.Connected)
                {
                    XmlRpcUtil.error("XmlRpcClient::writeRequest not connected");
                }
                MemoryStream memstream = new MemoryStream();
                using (StreamWriter writer = new StreamWriter(memstream))
                {
                    writer.Write(_request);
                    writer.Flush();
                }
                var stream = socket.GetStream();
                try
                {
                    var buffer = memstream.GetBuffer();
                    stream.Write(buffer, 0, buffer.Length);
                    stream.Flush();
                }
                catch (Exception ex)
                {
                    XmlRpcUtil.error(string.Format("Exception while writing request: {0}", ex.Message));
                }
                _bytesWritten = _request.Length;
            }
            catch (IOException ex)
            {
                XmlRpcUtil.error("Error in XmlRpcClient::writeRequest: write error ({0}).", ex.Message);
                return(false);
            }

            XmlRpcUtil.log(XmlRpcUtil.XMLRPC_LOG_LEVEL.INFO, "XmlRpcClient::writeRequest: wrote {0} of {1} bytes.", _bytesWritten, _request.Length);

            // Wait for the result
            if (_bytesWritten == _request.Length)
            {
                _connectionState = ConnectionState.READ_HEADER;
                header           = null;
            }
            return(true);
        }
Example #6
0
        // Prepend http headers
        private string generateHeader(string body)
        {
            string header = "POST " + _uri + " HTTP/1.1\r\nUser-Agent: ";

            header += XmlRpcUtil.XMLRPC_VERSION;
            header += "\r\nHost: ";
            header += _host;

            string buff = String.Format(":{0}\r\n", _port);

            //sprintf(buff,":%d\r\n", _port);


            header += buff;
            header += "Content-Type: text/xml\r\nContent-length: ";
            buff    = String.Format("{0}\r\n\r\n", body.Length);

            return(header + buff);
        }
Example #7
0
        internal virtual bool readHeader(ref HTTPHeader header)
        {
            // Read available data
            int dataLen = 0;
            var stream = getStream();
            if (stream == null)
            {
                throw new Exception("Could not access network stream");
            }
            byte[] data = new byte[READ_BUFFER_LENGTH];
            try
            {
                dataLen = stream.Read(data, 0, READ_BUFFER_LENGTH);

                if (dataLen == 0)
                    return false; // If it is disconnect

                if (header == null)
                {
                    header = new HTTPHeader(Encoding.ASCII.GetString(data, 0, dataLen));
                    if (header.m_headerStatus == HTTPHeader.STATUS.UNINITIALIZED)
                        return false; //should only happen if the constructor's invocation of Append did not happen as desired
                }
                else if (header.Append(Encoding.ASCII.GetString(data, 0, dataLen)) == HTTPHeader.STATUS.PARTIAL_HEADER)
                    return true; //if we successfully append a piece of the header, return true, but DO NOT change states 
            }
            catch (SocketException ex)
            {
                XmlRpcUtil.error("XmlRpcServerConnection::readHeader: error while reading header ({0}).", ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                XmlRpcUtil.error("XmlRpcServerConnection::readHeader: error while reading header ({0}).", ex.Message);
                return false;
            }

            if (header.m_headerStatus != HTTPHeader.STATUS.COMPLETE_HEADER)
                return false;

            return true;
        }
Example #8
0
        private bool writeRequest()
        {
            if (_bytesWritten == 0)
                XmlRpcUtil.log(XmlRpcUtil.XMLRPC_LOG_LEVEL.SPEW, "XmlRpcClient::writeRequest (attempt {0}):\n{1}\n", _sendAttempts + 1, _request);
            // Try to write the request
            try
            {
                if (!socket.Connected)
                    XmlRpcUtil.error("XmlRpcClient::writeRequest not connected");
                MemoryStream memstream = new MemoryStream();
                using (StreamWriter writer = new StreamWriter(memstream))
                {
                    writer.Write(_request);
                    writer.Flush();
                }
                var stream = socket.GetStream();
                try
                {
                    var buffer = memstream.GetBuffer();
                    stream.Write(buffer, 0, buffer.Length);
                    stream.Flush();
                }
                catch (Exception ex)
                {
                    XmlRpcUtil.error(string.Format("Exception while writing request: {0}", ex.Message));
                }
                _bytesWritten = _request.Length;
            }
            catch (IOException ex)
            {
                XmlRpcUtil.error("Error in XmlRpcClient::writeRequest: write error ({0}).", ex.Message);
                return false;
            }

            XmlRpcUtil.log(XmlRpcUtil.XMLRPC_LOG_LEVEL.INFO, "XmlRpcClient::writeRequest: wrote {0} of {1} bytes.", _bytesWritten, _request.Length);

            // Wait for the result
            if (_bytesWritten == _request.Length)
            {
                _connectionState = ConnectionState.READ_HEADER;
                header = null;
            }
            return true;
        }
Example #9
0
        // Prepend http headers
        private string generateHeader(string body)
        {
            string header = "POST " + _uri + " HTTP/1.1\r\nUser-Agent: ";
            header += XmlRpcUtil.XMLRPC_VERSION;
            header += "\r\nHost: ";
            header += _host;

            string buff = String.Format(":{0}\r\n", _port);
            //sprintf(buff,":%d\r\n", _port);


            header += buff;
            header += "Content-Type: text/xml\r\nContent-length: ";
            buff = String.Format("{0}\r\n\r\n", body.Length);

            return header + buff;
        }
Example #10
0
        internal override bool readHeader(ref HTTPHeader header)
        {
            if (base.readHeader(ref header))
            {
                if (header.m_headerStatus == HTTPHeader.STATUS.COMPLETE_HEADER)
                {
                    _connectionState = ConnectionState.READ_RESPONSE;
                }

                return true;
            }

            return false;
        }