{"action":"created","comment":{"url":"https://api.github.com/repos/netty/netty/pulls/comments/104997832","pull_request_review_id":25857238,"id":104997832,"diff_hunk":"@@ -0,0 +1,116 @@\n+/*\n+ * Copyright 2017 The Netty Project\n+ *\n+ * The Netty Project licenses this file to you under the Apache License,\n+ * version 2.0 (the \"License\"); you may not use this file except in compliance\n+ * with the License. You may obtain a copy of the License at:\n+ *\n+ *   http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+ * License for the specific language governing permissions and limitations\n+ * under the License.\n+ */\n+package io.netty.handler.ssl;\n+\n+import io.netty.buffer.ByteBuf;\n+import io.netty.channel.ChannelHandler;\n+import io.netty.channel.ChannelHandlerContext;\n+import io.netty.handler.codec.ByteToMessageDecoder;\n+import io.netty.util.ReferenceCountUtil;\n+\n+import javax.net.ssl.SSLContext;\n+import java.util.List;\n+\n+/**\n+ * {@link OptionalSslHandler} is a utility decoder to support both SSL and non-SSL handlers\n+ * based on the initial message.\n+ */\n+public class OptionalSslHandler extends ByteToMessageDecoder {\n+\n+    private final SslContext sslContext;\n+\n+    public OptionalSslHandler(SslContext sslContext) {\n+        this.sslContext = sslContext;\n+    }\n+\n+    @Override\n+    protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) throws Exception {\n+        if (in.readableBytes() < SslUtils.SSL_RECORD_HEADER_LENGTH) {\n+            return;\n+        }\n+        if (SslHandler.isEncrypted(in)) {\n+            handleSsl(context);\n+        } else {\n+            handleNonSsl(context);\n+        }\n+    }\n+\n+    private void handleSsl(ChannelHandlerContext context) {\n+        SslHandler sslHandler = null;\n+        try {\n+            sslHandler = newSslHandler(context, sslContext);\n+            context.pipeline().replace(this, newSslHandlerName(), sslHandler);\n+            sslHandler = null;\n+        } finally {\n+            // Since the SslHandler was not inserted into the pipeline the ownership of the SSLEngine was not\n+            // transferred to the SslHandler.\n+            if (sslHandler != null) {\n+                ReferenceCountUtil.safeRelease(sslHandler.engine());\n+            }\n+        }\n+    }\n+\n+    private void handleNonSsl(ChannelHandlerContext context) {\n+        ChannelHandler handler = newNonSslHandler(context);\n+        if (handler != null) {\n+            context.pipeline().replace(this, newNonSslHandlerName(), handler);\n+        } else {\n+            context.pipeline().remove(this);\n+        }\n+    }\n+\n+    /**\n+     * Override to specify the SSL handler name.\n+     * @return the name of the SSL handler.\n+     */\n+    protected String newSslHandlerName() {\n+        return null;\n+    }\n+\n+    /**\n+     * Override to configure the SslHandler eg. enable HttpsEndpointIdentificationAlgorithm.\n+     * @param context the {@link ChannelHandlerContext} to use.\n+     * @param sslContext the {@link SSLContext} to use.\n+     * @return the {@link SslHandler} to be added to the pipeline if the traffic is SSL.\n+     */\n+    protected SslHandler newSslHandler(ChannelHandlerContext context, SslContext sslContext) {\n+        return sslContext.newHandler(context.alloc());\n+    }\n+\n+    /**\n+     * Override to specify the non-SSL handler name.","path":"handler/src/main/java/io/netty/handler/ssl/OptionalSslHandler.java","position":94,"original_position":94,"commit_id":"04a34835c23fdca37b90a3f9bb67d9908a631245","original_commit_id":"04a34835c23fdca37b90a3f9bb67d9908a631245","user":{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars0.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false},"body":"@johnou add docs that its allowed to return {@code null}","created_at":"2017-03-08T19:13:32Z","updated_at":"2017-03-08T19:15:18Z","html_url":"https://github.com/netty/netty/pull/6510#discussion_r104997832","pull_request_url":"https://api.github.com/repos/netty/netty/pulls/6510","_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/comments/104997832"},"html":{"href":"https://github.com/netty/netty/pull/6510#discussion_r104997832"},"pull_request":{"href":"https://api.github.com/repos/netty/netty/pulls/6510"}}},"pull_request":{"url":"https://api.github.com/repos/netty/netty/pulls/6510","id":109586811,"html_url":"https://github.com/netty/netty/pull/6510","diff_url":"https://github.com/netty/netty/pull/6510.diff","patch_url":"https://github.com/netty/netty/pull/6510.patch","issue_url":"https://api.github.com/repos/netty/netty/issues/6510","number":6510,"state":"open","locked":false,"title":"Support for handling SSL and non-SSL in pipeline","user":{"login":"johnou","id":323497,"avatar_url":"https://avatars1.githubusercontent.com/u/323497?v=3","gravatar_id":"","url":"https://api.github.com/users/johnou","html_url":"https://github.com/johnou","followers_url":"https://api.github.com/users/johnou/followers","following_url":"https://api.github.com/users/johnou/following{/other_user}","gists_url":"https://api.github.com/users/johnou/gists{/gist_id}","starred_url":"https://api.github.com/users/johnou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnou/subscriptions","organizations_url":"https://api.github.com/users/johnou/orgs","repos_url":"https://api.github.com/users/johnou/repos","events_url":"https://api.github.com/users/johnou/events{/privacy}","received_events_url":"https://api.github.com/users/johnou/received_events","type":"User","site_admin":false},"body":"Motivation:\r\n\r\nSome pipelines require support for both SSL and non-SSL messaging.\r\n\r\nModifications:\r\n\r\nAdd utility decoder to support both SSL and non-SSL handlers based on the initial message.\r\n\r\nResult:\r\n\r\nLess boilerplate code to write for developers.\r\n\r\nFixes #6428.","created_at":"2017-03-07T22:31:50Z","updated_at":"2017-03-08T19:15:18Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ce6922976e4dcf8292ac33095562635ea21e4856","assignee":{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars0.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false},"assignees":[{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars0.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/netty/netty/pulls/6510/commits","review_comments_url":"https://api.github.com/repos/netty/netty/pulls/6510/comments","review_comment_url":"https://api.github.com/repos/netty/netty/pulls/comments{/number}","comments_url":"https://api.github.com/repos/netty/netty/issues/6510/comments","statuses_url":"https://api.github.com/repos/netty/netty/statuses/04a34835c23fdca37b90a3f9bb67d9908a631245","head":{"label":"johnou:optionalssl_handler","ref":"optionalssl_handler","sha":"04a34835c23fdca37b90a3f9bb67d9908a631245","user":{"login":"johnou","id":323497,"avatar_url":"https://avatars1.githubusercontent.com/u/323497?v=3","gravatar_id":"","url":"https://api.github.com/users/johnou","html_url":"https://github.com/johnou","followers_url":"https://api.github.com/users/johnou/followers","following_url":"https://api.github.com/users/johnou/following{/other_user}","gists_url":"https://api.github.com/users/johnou/gists{/gist_id}","starred_url":"https://api.github.com/users/johnou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnou/subscriptions","organizations_url":"https://api.github.com/users/johnou/orgs","repos_url":"https://api.github.com/users/johnou/repos","events_url":"https://api.github.com/users/johnou/events{/privacy}","received_events_url":"https://api.github.com/users/johnou/received_events","type":"User","site_admin":false},"repo":{"id":36216665,"name":"netty","full_name":"johnou/netty","owner":{"login":"johnou","id":323497,"avatar_url":"https://avatars1.githubusercontent.com/u/323497?v=3","gravatar_id":"","url":"https://api.github.com/users/johnou","html_url":"https://github.com/johnou","followers_url":"https://api.github.com/users/johnou/followers","following_url":"https://api.github.com/users/johnou/following{/other_user}","gists_url":"https://api.github.com/users/johnou/gists{/gist_id}","starred_url":"https://api.github.com/users/johnou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnou/subscriptions","organizations_url":"https://api.github.com/users/johnou/orgs","repos_url":"https://api.github.com/users/johnou/repos","events_url":"https://api.github.com/users/johnou/events{/privacy}","received_events_url":"https://api.github.com/users/johnou/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/johnou/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":true,"url":"https://api.github.com/repos/johnou/netty","forks_url":"https://api.github.com/repos/johnou/netty/forks","keys_url":"https://api.github.com/repos/johnou/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/johnou/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/johnou/netty/teams","hooks_url":"https://api.github.com/repos/johnou/netty/hooks","issue_events_url":"https://api.github.com/repos/johnou/netty/issues/events{/number}","events_url":"https://api.github.com/repos/johnou/netty/events","assignees_url":"https://api.github.com/repos/johnou/netty/assignees{/user}","branches_url":"https://api.github.com/repos/johnou/netty/branches{/branch}","tags_url":"https://api.github.com/repos/johnou/netty/tags","blobs_url":"https://api.github.com/repos/johnou/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/johnou/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/johnou/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/johnou/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/johnou/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/johnou/netty/languages","stargazers_url":"https://api.github.com/repos/johnou/netty/stargazers","contributors_url":"https://api.github.com/repos/johnou/netty/contributors","subscribers_url":"https://api.github.com/repos/johnou/netty/subscribers","subscription_url":"https://api.github.com/repos/johnou/netty/subscription","commits_url":"https://api.github.com/repos/johnou/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/johnou/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/johnou/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/johnou/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/johnou/netty/contents/{+path}","compare_url":"https://api.github.com/repos/johnou/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/johnou/netty/merges","archive_url":"https://api.github.com/repos/johnou/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/johnou/netty/downloads","issues_url":"https://api.github.com/repos/johnou/netty/issues{/number}","pulls_url":"https://api.github.com/repos/johnou/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/johnou/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/johnou/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/johnou/netty/labels{/name}","releases_url":"https://api.github.com/repos/johnou/netty/releases{/id}","deployments_url":"https://api.github.com/repos/johnou/netty/deployments","created_at":"2015-05-25T07:26:31Z","updated_at":"2017-02-19T10:17:55Z","pushed_at":"2017-03-08T18:17:08Z","git_url":"git://github.com/johnou/netty.git","ssh_url":"git@github.com:johnou/netty.git","clone_url":"https://github.com/johnou/netty.git","svn_url":"https://github.com/johnou/netty","homepage":"http://netty.io/","size":81908,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"netty:4.0","ref":"4.0","sha":"d787ad1e1f5ca67b41048c4eb5e249347fae0438","user":{"login":"netty","id":473791,"avatar_url":"https://avatars2.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"repo":{"id":1064563,"name":"netty","full_name":"netty/netty","owner":{"login":"netty","id":473791,"avatar_url":"https://avatars2.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/netty/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":false,"url":"https://api.github.com/repos/netty/netty","forks_url":"https://api.github.com/repos/netty/netty/forks","keys_url":"https://api.github.com/repos/netty/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/netty/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/netty/netty/teams","hooks_url":"https://api.github.com/repos/netty/netty/hooks","issue_events_url":"https://api.github.com/repos/netty/netty/issues/events{/number}","events_url":"https://api.github.com/repos/netty/netty/events","assignees_url":"https://api.github.com/repos/netty/netty/assignees{/user}","branches_url":"https://api.github.com/repos/netty/netty/branches{/branch}","tags_url":"https://api.github.com/repos/netty/netty/tags","blobs_url":"https://api.github.com/repos/netty/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/netty/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/netty/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/netty/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/netty/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/netty/netty/languages","stargazers_url":"https://api.github.com/repos/netty/netty/stargazers","contributors_url":"https://api.github.com/repos/netty/netty/contributors","subscribers_url":"https://api.github.com/repos/netty/netty/subscribers","subscription_url":"https://api.github.com/repos/netty/netty/subscription","commits_url":"https://api.github.com/repos/netty/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/netty/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/netty/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/netty/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/netty/netty/contents/{+path}","compare_url":"https://api.github.com/repos/netty/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/netty/netty/merges","archive_url":"https://api.github.com/repos/netty/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/netty/netty/downloads","issues_url":"https://api.github.com/repos/netty/netty/issues{/number}","pulls_url":"https://api.github.com/repos/netty/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/netty/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/netty/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/netty/netty/labels{/name}","releases_url":"https://api.github.com/repos/netty/netty/releases{/id}","deployments_url":"https://api.github.com/repos/netty/netty/deployments","created_at":"2010-11-09T09:22:21Z","updated_at":"2017-03-08T16:14:23Z","pushed_at":"2017-03-08T19:11:43Z","git_url":"git://github.com/netty/netty.git","ssh_url":"git@github.com:netty/netty.git","clone_url":"https://github.com/netty/netty.git","svn_url":"https://github.com/netty/netty","homepage":"http://netty.io/","size":46069,"stargazers_count":9303,"watchers_count":9303,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4572,"mirror_url":null,"open_issues_count":354,"forks":4572,"open_issues":354,"watchers":9303,"default_branch":"4.1"}},"_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/6510"},"html":{"href":"https://github.com/netty/netty/pull/6510"},"issue":{"href":"https://api.github.com/repos/netty/netty/issues/6510"},"comments":{"href":"https://api.github.com/repos/netty/netty/issues/6510/comments"},"review_comments":{"href":"https://api.github.com/repos/netty/netty/pulls/6510/comments"},"review_comment":{"href":"https://api.github.com/repos/netty/netty/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/netty/netty/pulls/6510/commits"},"statuses":{"href":"https://api.github.com/repos/netty/netty/statuses/04a34835c23fdca37b90a3f9bb67d9908a631245"}}}}
{"actor":{"display_login":"normanmaurer"}}