/// <summary>
        /// Builds the headers frame.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="final">if set to <c>true</c> than this frame is final for the stream.</param>
        /// <returns>Headers frame.</returns>
        public ControlFrame BuildHeadersFrame(Http2Stream stream, ProtocolHeaders headers, bool final)
        {
            ControlFrame frame = BuildControlFrame(FrameType.Headers, stream, headers);

            frame.IsFinal = final;
            return(frame);
        }
        /// <summary>
        /// Builds the SYN_REPLY frame.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="headers">The headers.</param>
        /// <returns>SYN_REPLY frame.</returns>
        public ControlFrame BuildSynReplyFrame(Http2Stream stream, ProtocolHeaders headers)
        {
            var frame = BuildControlFrame(FrameType.SynReply, stream, headers);

            frame.StreamId = stream.StreamId;
            frame.Length   = sizeof(Int32) + sizeof(Int32); // sizeof(StreamId) + sizeof(numberOfEntries)
            return(frame);
        }
        /// <summary>
        /// Builds the SYN_STREAM frame.
        /// </summary>
        /// <param name="streamId">The stream id.</param>
        /// <param name="headers">The headers.</param>
        /// <returns>SYN_STREAM frame.</returns>
        public ControlFrame BuildSynStreamFrame(int streamId, ProtocolHeaders headers)
        {
            ControlFrame frame = BuildControlFrame(FrameType.SynStream, streamId, headers);

            frame.Priority = SYNStreamPriority;

            return(frame);
        }
        /// <summary>
        /// Builds the SYN_STREAM frame.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="final">Indicates that stream is not going to send any more data.</param>
        /// <returns>SYN_STREAM frame.</returns>
        public ControlFrame BuildSynStreamFrame(Http2Stream stream, ProtocolHeaders headers, bool final)
        {
            ControlFrame frame = BuildControlFrame(FrameType.SynStream, stream, headers);

            frame.Priority = SYNStreamPriority;
            frame.IsFinal  = final;

            return(frame);
        }
 internal void OpenServer(ProtocolHeaders headers, bool isFin)
 {
     this.Headers.Merge(headers);
     this.State = Http2StreamState.Opened;
     //if (isFin)
     //{
     //    this.State = Http2StreamState.HalfClosed;
     //}
 }
        /// <summary>
        /// Opens the stream.
        /// </summary>
        /// <param name="headers">The headers.</param>
        /// <param name="isFin">The Final flag.</param>
        internal void OpenClient(ProtocolHeaders headers, bool isFin)
        {
            this.Headers.Merge(headers);
            if (isFin)
            {
                this.State = Http2StreamState.HalfClosed;
            }

            this.protocol.SendSynStream(this, headers, isFin);
        }
        /// <summary>
        /// Builds the control frame.
        /// </summary>
        /// <param name="type">The frame type.</param>
        /// <param name="streamId">The Http2 stream id.</param>
        /// <param name="headers">The headers.</param>
        /// <returns>Returns Control frame object.</returns>
        private static ControlFrame BuildControlFrame(FrameType type, int streamId, ProtocolHeaders headers)
        {
            ControlFrame frame = new ControlFrame(headers);

            frame.StreamId = streamId;
            frame.Type     = type;
            frame.Priority = ControlPriority;

            return(frame);
        }
        /// <summary>
        /// Opens the stream in current session.
        /// </summary>
        /// <param name="id">the stream id.</param>
        /// <param name="headers">The S+M headers.</param>
        /// <param name="isFinal">the final flag.</param>
        /// <returns>The Stream.</returns>
        private Http2Stream OpenStream(int id, ProtocolHeaders headers, bool isFinal)
        {
            if (id <= this.lastSeenStreamId)
            {
                this.End(StatusCode.ProtocolError);
                return null;
            }

            this.lastSeenStreamId = id;

            // don't have to wait for stream opening
            Http2Stream stream = new Http2Stream(id, this);

            this.streams.Add(stream);

            stream.OnClose += this.OnCloseStream;
            if (IsServer)
            {
                stream.OpenServer(headers, isFinal);

                // Settings must be sent for session once for session
                if (stream.StreamId <= 2)
                    this.protocol.SendSettings(stream);

                this.protocol.SendSynReply(stream);
            }
            else
            {
                stream.OpenClient(headers, isFinal);
            }

            if (this.OnStreamOpened != null)
            {
                this.OnStreamOpened(this, new StreamEventArgs(stream));
            }

            return stream;
        }
 /// <summary>
 /// Builds the SYN_REPLY frame.
 /// </summary>
 /// <param name="streamId">The stream id.</param>
 /// <param name="headers">The headers.</param>
 /// <returns>SYN_REPLY frame.</returns>
 public ControlFrame BuildSynReplyFrame(int streamId, ProtocolHeaders headers)
 {
     return(BuildControlFrame(FrameType.SynReply, streamId, headers));
 }
 /// <summary>
 /// Opens the stream in current session.
 /// </summary>
 /// <param name="headers">The S+M headers.</param>
 /// <param name="isFinal">the final flag.</param>
 /// <returns>The Stream.</returns>
 public Http2Stream OpenStream(ProtocolHeaders headers, bool isFinal)
 {
     int newId = this.GenerateNewStreamId();
     return this.OpenStream(newId, headers, isFinal);
 }
 /// <summary>
 /// Sends the headers to the stream.
 /// </summary>
 /// <param name="headers">The headers.</param>
 /// <param name="isFin">if set to <c>true</c> than this stream will be half-closed.</param>
 public void SendHeaders(ProtocolHeaders headers, bool isFin)
 {
     this.Headers.Merge(headers);
     this.protocol.SendHeaders(this, headers, isFin);
 }
 /// <summary>
 /// Sends the syn stream request.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="headers">The headers.</param>
 /// <param name="isFin">FIN flag.</param>
 public void SendSynStream(Http2Stream stream, ProtocolHeaders headers, bool isFin)
 {
     this.SendFrame(this.builder.BuildSynStreamFrame(stream, headers, isFin));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ControlFrame"/> class.
 /// </summary>
 /// <param name="headers">The headers.</param>
 public ControlFrame(ProtocolHeaders headers)
 {
     this.IsControl = true;
     this.Version = Http2Protocol.Version;
     this.Headers = headers ?? new ProtocolHeaders();
 }
        /// <summary>
        /// Builds the SYN_STREAM frame.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="final">Indicates that stream is not going to send any more data.</param>
        /// <returns>SYN_STREAM frame.</returns>
        public ControlFrame BuildSynStreamFrame(Http2Stream stream, ProtocolHeaders headers, bool final)
        {
            ControlFrame frame = BuildControlFrame(FrameType.SynStream, stream, headers);
            frame.Priority = SYNStreamPriority;
            frame.IsFinal = final;

            return frame;
        }
 /// <summary>
 /// Builds the SYN_REPLY frame.
 /// </summary>
 /// <param name="streamId">The stream id.</param>
 /// <param name="headers">The headers.</param>
 /// <returns>SYN_REPLY frame.</returns>
 public ControlFrame BuildSynReplyFrame(int streamId, ProtocolHeaders headers)
 {
     return BuildControlFrame(FrameType.SynReply, streamId, headers);
 }
 /// <summary>
 /// Builds the SYN_REPLY frame.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="headers">The headers.</param>
 /// <returns>SYN_REPLY frame.</returns>
 public ControlFrame BuildSynReplyFrame(Http2Stream stream, ProtocolHeaders headers)
 {
     var frame = BuildControlFrame(FrameType.SynReply, stream, headers);
     frame.StreamId = stream.StreamId;
     frame.Length = sizeof(Int32) + sizeof(Int32); // sizeof(StreamId) + sizeof(numberOfEntries)
     return frame;
 }
 /// <summary>
 /// Builds the headers frame.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="headers">The headers.</param>
 /// <param name="final">if set to <c>true</c> than this frame is final for the stream.</param>
 /// <returns>Headers frame.</returns>
 public ControlFrame BuildHeadersFrame(Http2Stream stream, ProtocolHeaders headers, bool final)
 {
     ControlFrame frame = BuildControlFrame(FrameType.Headers, stream, headers);
     frame.IsFinal = final;
     return frame;
 }
Exemple #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ControlFrame"/> class.
 /// </summary>
 /// <param name="headers">The headers.</param>
 public ControlFrame(ProtocolHeaders headers)
 {
     this.IsControl = true;
     this.Version   = Http2Protocol.Version;
     this.Headers   = headers ?? new ProtocolHeaders();
 }
        /// <summary>
        /// Opens the stream in current session.
        /// </summary>
        /// <param name="id">the stream id.</param>
        /// <param name="headers">The S+M headers.</param>
        /// <param name="isFinal">the final flag.</param>
        /// <returns>The Stream.</returns>
        private Http2Stream OpenStream(int id, ProtocolHeaders headers, bool isFinal)
        {
            if (id <= this.lastSeenStreamId)
            {
                this.End(StatusCode.ProtocolError);
                return null;
            }

            this.lastSeenStreamId = id;

            // don't have to wait for stream opening
            Http2Stream stream = new Http2Stream(id, this);

            this.streams.Add(stream);

            stream.OnClose += this.OnCloseStream;
            stream.Open(headers, isFinal);

            if (this.OnStreamOpened != null)
            {
                this.OnStreamOpened(this, new StreamEventArgs(stream));
            }

            return stream;
        }
        private byte[] SerializeControlFrameHeaders(ProtocolHeaders frameHeaders)
        {
            var headers = new List<byte>(256);
            headers.AddRange(BinaryHelper.Int32ToBytes(frameHeaders.Count));
            foreach (KeyValuePair<string, string> pair in frameHeaders)
            {
                byte[] nameBin = Encoding.UTF8.GetBytes(pair.Key);

                headers.AddRange(BinaryHelper.Int32ToBytes(nameBin.Length));
                headers.AddRange(nameBin);

                byte[] valBin = Encoding.UTF8.GetBytes(pair.Value);
                headers.AddRange(BinaryHelper.Int32ToBytes(valBin.Length));

                headers.AddRange(valBin);
            }

            return headers.ToArray();
        }
        /// <summary>
        /// Builds the SYN_STREAM frame.
        /// </summary>
        /// <param name="streamId">The stream id.</param>
        /// <param name="headers">The headers.</param>
        /// <returns>SYN_STREAM frame.</returns>
        public ControlFrame BuildSynStreamFrame(int streamId, ProtocolHeaders headers)
        {
            ControlFrame frame = BuildControlFrame(FrameType.SynStream, streamId, headers);
            frame.Priority = SYNStreamPriority;

            return frame;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="HeadersEventArgs"/> class.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="headers">The headers.</param>
 public HeadersEventArgs(Http2Stream stream, ProtocolHeaders headers)
     : base(stream)
 {
     this.Headers = headers;
 }
        /// <summary>
        /// Builds the control frame.
        /// </summary>
        /// <param name="type">The frame type.</param>
        /// <param name="streamId">The Http2 stream id.</param>
        /// <param name="headers">The headers.</param>
        /// <returns>Returns Control frame object.</returns>
        private static ControlFrame BuildControlFrame(FrameType type, int streamId, ProtocolHeaders headers)
        {
            ControlFrame frame = new ControlFrame(headers);
            frame.StreamId = streamId;
            frame.Type = type;
            frame.Priority = ControlPriority;

            return frame;
        }
 /// <summary>
 /// Sends the headers to the stream.
 /// </summary>
 /// <param name="headers">The headers.</param>
 /// <param name="isFin">if set to <c>true</c> than this stream will be half-closed.</param>
 public void SendHeaders(ProtocolHeaders headers, bool isFin)
 {
     this.Headers.Merge(headers);
     this.protocol.SendHeaders(this, headers, isFin);
 }
 internal void OpenServer(ProtocolHeaders headers, bool isFin)
 {
     this.Headers.Merge(headers);
     this.State = Http2StreamState.Opened;
     //if (isFin)
     //{
     //    this.State = Http2StreamState.HalfClosed;
     //}
 }
        /// <summary>
        /// Opens the stream in current session.
        /// </summary>
        /// <param name="headers">The S+M headers.</param>
        /// <param name="isFinal">the final flag.</param>
        /// <returns>The Stream.</returns>
        public Http2Stream OpenStream(ProtocolHeaders headers, bool isFinal)
        {
            int newId = this.GenerateNewStreamId();
            if (this.State == ProtocolSessionState.Opened)
                return this.OpenStream(newId, headers, isFinal);

            throw new InvalidOperationException("Session is not opened.");
        }
        public void SendSynReply(Http2Stream stream)
        {
            var headers = new ProtocolHeaders();

            headers[ProtocolHeaders.ContentType] = "text/plain";
            headers[ProtocolHeaders.Status] = "200";
            headers[ProtocolHeaders.Version] = "spdy/3";

            this.SendFrame(this.builder.BuildSynReplyFrame(stream, headers));
        }
        /// <summary>
        /// Opens the stream.
        /// </summary>
        /// <param name="headers">The headers.</param>
        /// <param name="isFin">The Final flag.</param>
        internal void OpenClient(ProtocolHeaders headers, bool isFin)
        {
            this.Headers.Merge(headers);
            if (isFin)
            {
                this.State = Http2StreamState.HalfClosed;
            }

            this.protocol.SendSynStream(this, headers, isFin);
        }
 /// <summary>
 /// Builds the SYN_REPLY frame.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="headers">The headers.</param>
 /// <returns>SYN_REPLY frame.</returns>
 public ControlFrame BuildSynReplyFrame(Http2Stream stream, ProtocolHeaders headers)
 {
     return BuildControlFrame(FrameType.SynReply, stream, headers);
 }
        /// <summary>
        /// File download.
        /// </summary>
        /// <param name="fileName">The file name</param>
        /// <returns>The return code.</returns>
        private static bool DownloadPath(string fileName)
        {
            if (session == null)
            {
                Http2Logger.LogError("Not connected to server. Please try again.");
                return false;
            }
            else
            {
                switch (session.State)
                {
                    case ProtocolSessionState.Created:
                        Http2Logger.LogError("Session was created but not opened yet.");
                        break;

                    case ProtocolSessionState.Opened:
                        ProtocolHeaders headers = new ProtocolHeaders();
                        headers[ProtocolHeaders.Path] = "/" + fileName.TrimStart('/');
                        headers[ProtocolHeaders.Version] = "HTTP/1.1";
                        headers[ProtocolHeaders.Method] = "GET";
                        headers[ProtocolHeaders.Scheme] = session.Uri.Scheme;
                        headers[ProtocolHeaders.Host] = session.Uri.Host + ":" + session.Uri.Port;
                        headers[ProtocolHeaders.ContentType] = ContentTypes.GetTypeFromFileName(fileName);

                        // If true, then stream will be half closed.
                        Http2Stream stream = session.OpenStream(headers, true);
                        stream.OnDataReceived += OnDataReceived;
                        stream.OnRSTReceived += OnRSTReceived;
                        break;
                    case ProtocolSessionState.Closed:
                        Http2Logger.LogError("Session was closed due to error or not opened.");
                        return false;

                    default:
                        Http2Logger.LogError("Unknown ProtocolSessionState " + session.State);
                        break;
                }

                return true;
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="HeadersEventArgs"/> class.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="headers">The headers.</param>
 public HeadersEventArgs(Http2Stream stream, ProtocolHeaders headers)
     : base(stream)
 {
     this.Headers = headers;
 }