forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added HTTP Support * Updated documentation and removed duplicate code * Added unit test for NettyHttpChannelInitializer
- Loading branch information
1 parent
210d828
commit 7ec205a
Showing
13 changed files
with
368 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...r-io/netty/src/main/java/org/apache/pulsar/io/netty/http/NettyHttpChannelInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.io.netty.http; | ||
|
||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.handler.codec.http.HttpServerCodec; | ||
import io.netty.handler.ssl.SslContext; | ||
|
||
/** | ||
* Netty Channel Initializer to register HTTP decoder and handler. | ||
*/ | ||
public class NettyHttpChannelInitializer extends ChannelInitializer<SocketChannel> { | ||
|
||
private final SslContext sslCtx; | ||
private ChannelInboundHandlerAdapter handler; | ||
|
||
public NettyHttpChannelInitializer(ChannelInboundHandlerAdapter handler, SslContext sslCtx) { | ||
this.handler = handler; | ||
this.sslCtx = sslCtx; | ||
} | ||
|
||
@Override | ||
protected void initChannel(SocketChannel socketChannel) throws Exception { | ||
if (sslCtx != null) { | ||
socketChannel.pipeline().addLast(sslCtx.newHandler(socketChannel.alloc())); | ||
} | ||
socketChannel.pipeline().addLast(new HttpServerCodec()); | ||
socketChannel.pipeline().addLast(this.handler); | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
pulsar-io/netty/src/main/java/org/apache/pulsar/io/netty/http/NettyHttpServerHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.io.netty.http; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import io.netty.handler.codec.http.DefaultFullHttpResponse; | ||
import io.netty.handler.codec.http.FullHttpResponse; | ||
import io.netty.handler.codec.http.HttpContent; | ||
import io.netty.handler.codec.http.HttpHeaderNames; | ||
import io.netty.handler.codec.http.HttpHeaderValues; | ||
import io.netty.handler.codec.http.HttpObject; | ||
import io.netty.handler.codec.http.HttpRequest; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.netty.handler.codec.http.HttpUtil; | ||
import io.netty.handler.codec.http.HttpVersion; | ||
import io.netty.handler.codec.http.LastHttpContent; | ||
import io.netty.util.CharsetUtil; | ||
|
||
import java.io.Serializable; | ||
import java.util.Optional; | ||
|
||
import lombok.Data; | ||
|
||
import org.apache.pulsar.functions.api.Record; | ||
import org.apache.pulsar.io.netty.NettySource; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Handles a server-side HTTP channel. | ||
*/ | ||
@ChannelHandler.Sharable | ||
public class NettyHttpServerHandler extends SimpleChannelInboundHandler<Object> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(NettyHttpServerHandler.class); | ||
|
||
private NettySource nettySource; | ||
|
||
public NettyHttpServerHandler(NettySource nettySource) { | ||
this.nettySource = nettySource; | ||
} | ||
|
||
private HttpRequest request; | ||
|
||
@Override | ||
public void channelReadComplete(ChannelHandlerContext ctx) { | ||
ctx.flush(); | ||
} | ||
|
||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, Object msg) { | ||
|
||
if (msg instanceof HttpRequest) { | ||
HttpRequest request = this.request = (HttpRequest) msg; | ||
|
||
if (HttpUtil.is100ContinueExpected(request)) { | ||
send100Continue(ctx); | ||
} | ||
} | ||
|
||
if (msg instanceof HttpContent) { | ||
HttpContent httpContent = (HttpContent) msg; | ||
|
||
ByteBuf content = httpContent.content(); | ||
if (content.isReadable()) { | ||
nettySource.consume(new NettyHttpRecord(Optional.ofNullable(""), | ||
content.toString(CharsetUtil.UTF_8).getBytes())); | ||
} | ||
|
||
if (msg instanceof LastHttpContent) { | ||
LastHttpContent trailer = (LastHttpContent) msg; | ||
|
||
if (!writeResponse(trailer, ctx)) { | ||
// If keep-alive is off, close the connection once the content is fully written. | ||
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { | ||
// Decide whether to close the connection or not. | ||
boolean keepAlive = HttpUtil.isKeepAlive(request); | ||
// Build the response object. | ||
FullHttpResponse response = new DefaultFullHttpResponse( | ||
HttpVersion.HTTP_1_1, | ||
currentObj.decoderResult().isSuccess() ? HttpResponseStatus.OK : HttpResponseStatus.BAD_REQUEST, | ||
Unpooled.EMPTY_BUFFER); | ||
|
||
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); | ||
|
||
if (keepAlive) { | ||
// Add 'Content-Length' header only for a keep-alive connection. | ||
response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); | ||
// Add keep alive header as per: | ||
// - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection | ||
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); | ||
} | ||
|
||
// Write the response. | ||
ctx.write(response); | ||
|
||
return keepAlive; | ||
} | ||
|
||
private static void send100Continue(ChannelHandlerContext ctx) { | ||
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE); | ||
ctx.write(response); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
logger.error("Error when processing incoming data", cause); | ||
ctx.close(); | ||
} | ||
|
||
@Data | ||
static private class NettyHttpRecord implements Record<byte[]>, Serializable { | ||
private final Optional<String> key; | ||
private final byte[] value; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
pulsar-io/netty/src/main/java/org/apache/pulsar/io/netty/http/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.io.netty.http; |
19 changes: 19 additions & 0 deletions
19
pulsar-io/netty/src/main/java/org/apache/pulsar/io/netty/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.io.netty; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.