Ejemplo n.º 1
0
        internal HttpRequestChannel(HttpChannelFactory factory,
                                    EndpointAddress to,
                                    Uri via,
                                    MessageEncoder encoder,
                                    HttpTransportBindingElement bindingElement)
            : base(factory)
        {
            _nonceBack    = "";
            _nonceCounter = 0;

            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            this._factory = factory;
            this._to      = to;
            this._via     = via;
            this._encoder = encoder;

            _listeners = new List <ITrafficListener>();

            foreach (IChannelController controller in bindingElement.Controllers)
            {
                if (controller is ITrafficListener)
                {
                    _listeners.Add((ITrafficListener)controller);
                }

                if (controller is IEndpointController)
                {
                    if (_endpointController != null)
                    {
                        throw new ApplicationException("Only one channel controller of type IAddressProvider can be set");
                    }
                    _endpointController = (IEndpointController)controller;
                }
                if (controller is IExecutionController)
                {
                    if (_executionController != null)
                    {
                        throw new ApplicationException("Only one channel controller of type IExecutionController can be set");
                    }
                    _executionController = (IExecutionController)controller;
                }
                if (controller is ICredentialsProvider)
                {
                    _credentialsProvider = controller as ICredentialsProvider;
                }
                if (controller is IWsaController)
                {
                    _wsaController = controller as IWsaController;
                }
            }
            this._networkStream = new RequestNetworkStream(to);

            this._bufferManager = BufferManager.CreateBufferManager(bindingElement.MaxBufferPoolSize, (int)bindingElement.MaxReceivedMessageSize);
        }
Ejemplo n.º 2
0
        public string SendSoapMessage(string request)
        {
            byte[] bytes = CreateMessageBytes(request);

            _networkStream = new RequestNetworkStream(new EndpointAddress(_address.OriginalString));
            _networkStream.Connect();

            _networkStream.Write(bytes, 0, bytes.Length);

            MemoryStream responseStream = new MemoryStream();
            int          readTimeout    = _timeout;
            HttpPacket   header         = null;

            do
            {
                responseStream = new MemoryStream();
                GetResponse(responseStream, out header, readTimeout);

                if (header.StatusCode == 100)
                {
                    //System.Diagnostics.Debug.WriteLine("100-Continue received");
                }
            } while (header.StatusCode == 100);

            _networkStream.Close();

            bool digestEnabled = false;

            if (_credentialsProvider != null)
            {
                digestEnabled = _credentialsProvider.Security == Security.Digest;
            }
            if (header.StatusCode == 401)
            {
                if (digestEnabled)
                {
                    System.Diagnostics.Debug.WriteLine("HTTP 401 received");
                    _networkStream.Close();

                    _networkStream.EnsureOpen(new EndpointAddress(_address.OriginalString));

                    // Send request once more.
                    _digestAuthChallenge = header;

                    // uses _digestAuthChallenge (and _currentMessageBytes), so behaviour will be different
                    // (_digestAuthChallenge might be not null by the first attempt, but if we get status 401,
                    // it will be updated)
                    byte[] messageBytes = CreateMessageBytes(request);
                    _networkStream.Write(messageBytes, 0, messageBytes.Length);

                    //System.Diagnostics.Debug.WriteLine(string.Format("After ResendMessage: socket: {0}", _networkStream.Connected));

                    do
                    {
                        responseStream = new MemoryStream();
                        GetResponse(responseStream, out header, readTimeout);
                    } while (header.StatusCode == 100);

                    if (header.StatusCode == 401)
                    {
                        _networkStream.Close();
                        throw new AccessDeniedException("Digest authentication FAILED (HTTP status 401 received)");
                    }
                }
                else
                {
                    _networkStream.Close();
                    throw new AccessDeniedException("Access denied (HTTP status 401 received)");
                }
            }

            _networkStream.Close();

            int count = (int)responseStream.Length - header.BodyOffset;

            if (header.ContentLength < count)
            {
                throw new HttpProtocolException(
                          string.Format("An error occurred while receiving packet. Expected length: {0}, received: {1}",
                                        header.ContentLength, count));
            }

            // parse response and notify listeners
            string response = HttpHelper.GetFormattedMessage(responseStream.GetBuffer(), header.BodyOffset);

            responseStream.Close();
            return(response);
        }
Ejemplo n.º 3
0
        public string SendSoapMessage(string request)
        {
            // create request bytes
            byte[] soapBytes = Encoding.UTF8.GetBytes(request);

            // create headers
            byte[] httpHeaders = HttpHelper.CreateHttpHeaders(soapBytes.Length,
                                                              _address.AbsolutePath,
                                                              _address.Host);

            //whole message with headers
            byte[] bytes = new byte[httpHeaders.Length + soapBytes.Length];

            Array.Copy(httpHeaders, bytes, httpHeaders.Length);

            Array.Copy(soapBytes, 0, bytes, httpHeaders.Length, soapBytes.Length);

#if DEBUG
            //string str = Encoding.UTF8.GetString(bytes);
            //System.Diagnostics.Debug.WriteLine(string.Format("SEND: {0}", str));
#endif

            RequestNetworkStream _networkStream = new RequestNetworkStream(new EndpointAddress(_address.OriginalString));
            _networkStream.Connect();

            _networkStream.Write(bytes, 0, bytes.Length);

            MemoryStream responseStream = new MemoryStream();
            byte[]       responseBuffer = new byte[2048];
            int          readTimeout    = _timeout;
            int          bytesCount     = 0;
            bool         bContinue      = false;
            HttpPacket   header         = null;

            do
            {
                DateTime startTime = DateTime.Now;

                IAsyncResult result = _networkStream.BeginRead(responseBuffer, 0, responseBuffer.Length);

                // wait for bytes receied, stop event or timeout
                WaitHandle[] handles;
                if (_executionController != null && _executionController.StopEvent != null)
                {
                    handles = new WaitHandle[] { result.AsyncWaitHandle, _executionController.StopEvent };
                }
                else
                {
                    handles = new WaitHandle[] { result.AsyncWaitHandle };
                }
                int handle = System.Threading.WaitHandle.WaitAny(handles, readTimeout);

                if (handle == WaitHandle.WaitTimeout)
                {
                    _networkStream.Close();
                    throw new IOException("The HTTP request has exceeded the allotted timeout");
                }

                if (handle == 1)
                {
                    System.Diagnostics.Debug.WriteLine("Stop event");
                    _networkStream.Close();
                    _executionController.ReportStop();
                }

                bytesCount = _networkStream.EndRead(result);

                DateTime endTime = DateTime.Now;

                System.Diagnostics.Debug.WriteLine(string.Format("--- Bytes received: {0}", bytesCount));

                responseStream.Write(responseBuffer, 0, bytesCount);

                // timeout for next part of answer
                readTimeout -= (int)((endTime - startTime).TotalMilliseconds);

                // parse response received by this moment.
                try
                {
                    bContinue = HttpHelper.ContinueReading(responseStream, out header);
                }
                catch (Exception exc)
                {
                    _networkStream.Close();
                    // clean resources somehow ?
                    throw new Exception("An error occurred while parsing HTTP packet", exc);
                }

                if (readTimeout < 0)
                {
                    _networkStream.Close();
                    throw new IOException("The HTTP request has exceeded the allotted timeout");
                }
            }while (_networkStream.DataAvailable || bContinue);

            _networkStream.Close();

            int count = (int)responseStream.Length - header.BodyOffset;

            if (header.ContentLength < count)
            {
                throw new HttpProtocolException(
                          string.Format("An error occurred while receiving packet. Expected length: {0}, received: {1}",
                                        header.ContentLength, count));
            }

            // parse response and notify listeners
            string response = HttpHelper.GetFormattedMessage(responseStream.GetBuffer(), header.BodyOffset);

            responseStream.Close();
            return(response);
        }