Ejemplo n.º 1
0
        protected override void ChannelRead0(IChannelHandlerContext context, IFullHttpRequest request)
        {
            if (HttpUtil.Is100ContinueExpected(request))
            {
                context.WriteAsync(new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.Continue, Unpooled.Empty));
            }
            var keepAlive = HttpUtil.IsKeepAlive(request);

            var content = context.Allocator.Buffer();

            content.WriteBytes(RESPONSE_BYTES.Duplicate());
            ByteBufferUtil.WriteAscii(content, " - via " + request.ProtocolVersion + " (" + this.establishApproach + ")");

            IFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK, content);

            response.Headers.Set(HttpHeaderNames.ContentType, "text/plain; charset=UTF-8");
            response.Headers.SetInt(HttpHeaderNames.ContentLength, response.Content.ReadableBytes);

            if (keepAlive)
            {
                if (request.ProtocolVersion.Equals(HttpVersion.Http10))
                {
                    response.Headers.Set(HttpHeaderNames.Connection, HttpHeaderValues.KeepAlive);
                }
                context.WriteAsync(response);
            }
            else
            {
                // Tell the client we're going to close the connection.
                response.Headers.Set(HttpHeaderNames.Connection, HttpHeaderValues.Close);
                context.WriteAsync(response).CloseOnComplete(context.Channel);
            }
        }
Ejemplo n.º 2
0
 /**
  * If receive a frame with end-of-stream set, send a pre-canned response.
  */
 private static void OnHeadersRead(IChannelHandlerContext context, IHttp2HeadersFrame headers)
 {
     if (headers.IsEndStream)
     {
         var content = context.Allocator.Buffer();
         content.WriteBytes(Server.HelloWorldHttp1Handler.RESPONSE_BYTES.Duplicate());
         ByteBufferUtil.WriteAscii(content, " - via HTTP/2");
         SendResponse(context, content);
     }
 }
Ejemplo n.º 3
0
 public void OnHeadersRead(IChannelHandlerContext ctx, int streamId, IHttp2Headers headers, int padding, bool endOfStream)
 {
     if (endOfStream)
     {
         var content = ctx.Allocator.Buffer();
         content.WriteBytes(HelloWorldHttp1Handler.RESPONSE_BYTES.Duplicate());
         ByteBufferUtil.WriteAscii(content, " - via HTTP/2");
         this.SendResponse(ctx, streamId, content);
     }
 }
Ejemplo n.º 4
0
        private void WriteHeadersFrame(IChannelHandlerContext ctx, IHttp2HeadersFrame headersFrame, IPromise promise)
        {
            if (Http2CodecUtil.IsStreamIdValid(headersFrame.Stream.Id))
            {
                _ = Encoder.WriteHeadersAsync(ctx, headersFrame.Stream.Id, headersFrame.Headers, headersFrame.Padding,
                                              headersFrame.IsEndStream, promise);
            }
            else
            {
                var stream     = (DefaultHttp2FrameStream)headersFrame.Stream;
                var connection = Connection;
                var streamId   = connection.Local.IncrementAndGetNextStreamId;
                if (streamId < 0)
                {
                    promise.SetException(new Http2NoMoreStreamIdsException());

                    // Simulate a GOAWAY being received due to stream exhaustion on this connection. We use the maximum
                    // valid stream ID for the current peer.
                    OnHttp2Frame(ctx, new DefaultHttp2GoAwayFrame(connection.IsServer ? int.MaxValue :
                                                                  int.MaxValue - 1, Http2Error.NoError,
                                                                  ByteBufferUtil.WriteAscii(ctx.Allocator, "Stream IDs exhausted on local stream creation")));
                    return;
                }
                stream.Id = streamId;

                // Use a Map to store all pending streams as we may have multiple. This is needed as if we would store the
                // stream in a field directly we may override the stored field before onStreamAdded(...) was called
                // and so not correctly set the property for the buffered stream.
                //
                // See https://github.com/netty/netty/issues/8692
                var result = _frameStreamToInitializeMap.TryAdd(streamId, stream);

                // We should not re-use ids.
                Debug.Assert(result);

                _ = Encoder.WriteHeadersAsync(ctx, streamId, headersFrame.Headers, headersFrame.Padding,
                                              headersFrame.IsEndStream, promise);
                if (!promise.IsCompleted)
                {
                    _ = Interlocked.Increment(ref v_numBufferedStreams);
                    // Clean up the stream being initialized if writing the headers fails and also
                    // decrement the number of buffered streams.
                    _ = promise.Task.ContinueWith(ResetNufferedStreamsAction, (this, streamId), TaskContinuationOptions.ExecuteSynchronously);
                }
                else
                {
                    HandleHeaderFuture(promise.Task, streamId);
                }
            }
        }
Ejemplo n.º 5
0
        public void WriteUsAsciiString()
        {
            AsciiString usAscii = new AsciiString("NettyRocks");
            var         buf     = Unpooled.Buffer(16);

            buf.WriteBytes(Encoding.ASCII.GetBytes(usAscii.ToString()));
            var buf2 = Unpooled.Buffer(16);

            ByteBufferUtil.WriteAscii(buf2, usAscii);

            Assert.Equal(buf, buf2);

            buf.Release();
            buf2.Release();
        }
Ejemplo n.º 6
0
        public void WriteUsAsciiWrapped()
        {
            string usAscii = "NettyRocks";
            var    buf     = Unpooled.UnreleasableBuffer(Unpooled.Buffer(16));

            AssertWrapped(buf);
            buf.WriteBytes(Encoding.ASCII.GetBytes(usAscii));
            var buf2 = Unpooled.UnreleasableBuffer(Unpooled.Buffer(16));

            AssertWrapped(buf2);
            ByteBufferUtil.WriteAscii(buf2, usAscii);

            Assert.Equal(buf, buf2);

            buf.Unwrap().Release();
            buf2.Unwrap().Release();
        }
Ejemplo n.º 7
0
        public void WriteUsAsciiCompositeWrapped()
        {
            string usAscii = "NettyRocks";
            var    buf     = Unpooled.Buffer(16);

            buf.WriteBytes(Encoding.ASCII.GetBytes(usAscii));
            var buf2 = new WrappedCompositeByteBuffer(Unpooled.CompositeBuffer().AddComponent(
                                                          Unpooled.Buffer(8)).AddComponent(Unpooled.Buffer(24)));

            // write some byte so we start AddComponent with an offset.
            buf2.WriteByte(1);
            ByteBufferUtil.WriteAscii(buf2, usAscii);

            // Skip the previously written byte.
            Assert.Equal(buf, buf2.SkipBytes(1));

            buf.Release();
            buf2.Release();
        }