Exemplo n.º 1
0
        /// <summary>
        /// Writes transport headers
        /// </summary>
        private void WriteHeaders(ITransportHeaders headers)
        {
            if (headers != null)
            {
                // proceed headers
                foreach (DictionaryEntry header in headers)
                {
                    string headerName = (string)header.Key;
                    if (!BinaryWireProtocol.IsLocalHeader(headerName))
                    {
                        // check for well-known header
                        byte headerType = BinaryWireProtocol.GetWellKnownHeaderType(headerName);

                        // write header type
                        _writer.Write(headerType);

                        if (headerType == BinaryWireProtocol.HeaderType.Custom)
                        {
                            // write custom header name
                            _writer.Write(headerName);
                        }

                        // write header value
                        _writer.Write(Convert.ToString(header.Value));
                    }
                }
            }

            // end-of-headers marker
            _writer.Write(BinaryWireProtocol.HeaderType.EndOfHeaders);
        }
Exemplo n.º 2
0
        public void ReceiveResponse(out ITransportHeaders responseHeaders, out Stream responseStream)
        {
            // transport signature
            if (!MatchPreamble())
            {
                BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidMessageFormat);
            }
            // operation opcode
            byte operation = _reader.ReadByte();

            if (operation != BinaryWireProtocol.OperationType.Reply)
            {
                BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidMessageFormat);
            }
            // content length
            int contentLength = _reader.ReadInt32();

            // response headers
            responseHeaders = ReadHeaders();
            // set special headers
            responseHeaders[BinaryWireProtocol.WellKnownHeaders.ConnectionId] = _connectionId;

            // create stream for response reading
            if (contentLength == -1)
            {
                responseStream = new ChunkedReadStream(_bufferedStream);
            }
            else
            {
                responseStream = new FixedReadStream(_bufferedStream, contentLength);
            }
        }
Exemplo n.º 3
0
        public void ReceiveRequest(out ITransportHeaders requestHeaders, out Stream requestStream)
        {
            // transport signature
            if (!MatchPreamble())
            {
                BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidMessageFormat);
            }
            // operation opcode
            byte operation = _reader.ReadByte();

            if (operation != BinaryWireProtocol.OperationType.Request && operation != BinaryWireProtocol.OperationType.OneWayRequest)
            {
                BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidMessageFormat);
            }
            // content length
            int contentLength = _reader.ReadInt32();
            // request uri
            string requestUri = _reader.ReadString();

            if (!CheckRequestUri(requestUri))
            {
                BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidRequestUri);
            }
            // request headers
            requestHeaders = ReadHeaders();
            // set special headers
            requestHeaders[BinaryWireProtocol.WellKnownHeaders.ConnectionId] = _connectionId;
            requestHeaders[BinaryWireProtocol.WellKnownHeaders.RequestUri]   = requestUri;

            // create stream for request reading
            if (contentLength == -1)
            {
                requestStream = new ChunkedReadStream(_bufferedStream);
            }
            else
            {
                requestStream = new FixedReadStream(_bufferedStream, contentLength);
            }

            // set client principal
            RemotingService.ClientPrincipal = _transport.ClientPrincipal;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reads transport headers
        /// </summary>
        private ITransportHeaders ReadHeaders()
        {
            TransportHeaders headers = new TransportHeaders();

            // for error-header procesing
            bool   errorHeader  = false;
            string errorMessage = null;
            byte   statusCode   = BinaryWireProtocol.StatusCode.InternalError;

            // proceed headers
            string headerName   = null;
            bool   endOfHeaders = false;

            while (!endOfHeaders)
            {
                // read header type
                byte headerType = _reader.ReadByte();

                switch (headerType)
                {
                case BinaryWireProtocol.HeaderType.EndOfHeaders:
                    // end-of-headers marker
                    endOfHeaders = true;
                    continue;

                case BinaryWireProtocol.HeaderType.ErrorMessage:
                    // error header
                    errorHeader  = true;
                    errorMessage = _reader.ReadString();
                    continue;

                case BinaryWireProtocol.HeaderType.StatusCode:
                    // status code
                    statusCode = _reader.ReadByte();
                    continue;

                case BinaryWireProtocol.HeaderType.Custom:
                    // custom header
                    headerName = _reader.ReadString();
                    break;

                default:
                    // well-known header
                    headerName = BinaryWireProtocol.GetWellKnownHeader(headerType);
                    break;
                }

                if (headerName == null)
                {
                    BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidMessageFormat);
                }

                // read header value
                headers[headerName] = _reader.ReadString();
            }

            if (errorHeader)
            {
                BinaryWireProtocol.ThrowException(statusCode, "Internal server error: " + errorMessage);
            }

            return(headers);
        }