{"action":"created","comment":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/51250620","id":51250620,"diff_hunk":"@@ -0,0 +1,126 @@\n+/*\n+ * Licensed to Elasticsearch under one or more contributor\n+ * license agreements. See the NOTICE file distributed with\n+ * this work for additional information regarding copyright\n+ * ownership. Elasticsearch licenses this file to you under\n+ * the Apache License, Version 2.0 (the \"License\"); you may\n+ * not use this file except in compliance with the License.\n+ * 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,\n+ * software distributed under the License is distributed on an\n+ * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+ * KIND, either express or implied.  See the License for the\n+ * specific language governing permissions and limitations\n+ * under the License.\n+ */\n+\n+package org.elasticsearch.http.netty;\n+\n+import org.jboss.netty.handler.codec.http.DefaultHttpResponse;\n+import org.jboss.netty.handler.codec.http.HttpMethod;\n+import org.jboss.netty.handler.codec.http.HttpRequest;\n+import org.jboss.netty.handler.codec.http.HttpResponse;\n+import org.jboss.netty.handler.codec.http.HttpResponseStatus;\n+import org.jboss.netty.handler.codec.http.HttpVersion;\n+\n+import java.util.Optional;\n+\n+import static org.elasticsearch.common.Strings.enLowerCase;\n+import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;\n+import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.CLOSE;\n+import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.KEEP_ALIVE;\n+import static org.jboss.netty.handler.codec.http.HttpMethod.CONNECT;\n+import static org.jboss.netty.handler.codec.http.HttpMethod.DELETE;\n+import static org.jboss.netty.handler.codec.http.HttpMethod.GET;\n+import static org.jboss.netty.handler.codec.http.HttpMethod.HEAD;\n+import static org.jboss.netty.handler.codec.http.HttpMethod.OPTIONS;\n+import static org.jboss.netty.handler.codec.http.HttpMethod.PATCH;\n+import static org.jboss.netty.handler.codec.http.HttpMethod.POST;\n+import static org.jboss.netty.handler.codec.http.HttpMethod.PUT;\n+import static org.jboss.netty.handler.codec.http.HttpMethod.TRACE;\n+\n+/**\n+ * Some utils for working with Netty's Http functionality.\n+ */\n+public class HttpUtils {\n+\n+    /**\n+     * Determine if the request protocol version is HTTP 1.0\n+     *\n+     * @param request the {@link HttpRequest}\n+     * @return {@code true} if the protocol version is HTTP 1.0, otherwise {@code false}\n+     */\n+    public static boolean isHttp10(HttpRequest request) {\n+        return request.getProtocolVersion().equals(HttpVersion.HTTP_1_0);\n+    }\n+\n+    /**\n+     * Determine if the request connection should be closed on completion.\n+     *\n+     * @param request the {@link HttpRequest}\n+     * @return {@code true} if the connection should be closed, otherwise {@code false}\n+     */\n+    public static boolean isCloseConnection(HttpRequest request) {\n+        final boolean http10 = isHttp10(request);\n+        return CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION)) ||\n+            (http10 && !KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION)));\n+    }\n+\n+    /**\n+     * Create a new {@link HttpResponse} to transmit the response for the given request.\n+     *\n+     * @param request the {@link HttpRequest} to create a response for\n+     * @return the {@link HttpResponse} object\n+     */\n+    public static HttpResponse newResponse(HttpRequest request) {\n+        final boolean http10 = isHttp10(request);\n+        final boolean close = isCloseConnection(request);\n+        // Build the response object.\n+        HttpResponseStatus status = HttpResponseStatus.OK; // default to initialize\n+        org.jboss.netty.handler.codec.http.HttpResponse resp;\n+        if (http10) {\n+            resp = new DefaultHttpResponse(HttpVersion.HTTP_1_0, status);\n+            if (!close) {\n+                resp.headers().add(CONNECTION, \"Keep-Alive\");\n+            }\n+        } else {\n+            resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);\n+        }\n+        return resp;\n+    }\n+\n+    /**\n+     * Convert a string representation of an HTTP method to its {@link HttpMethod} equivalent.\n+     *\n+     * @param method the method name to convert\n+     * @return an {@link Optional} that contains the HttpMethod, or empty if no match was found.\n+     */\n+    public static Optional<HttpMethod> toHttpMethod(final String method) {","path":"core/src/main/java/org/elasticsearch/http/netty/HttpUtils.java","position":101,"original_position":101,"commit_id":"3a725b07b622a8bac84739f6971e1fb28cca5df7","original_commit_id":"3a725b07b622a8bac84739f6971e1fb28cca5df7","user":{"login":"spinscale","id":667544,"avatar_url":"https://avatars.githubusercontent.com/u/667544?v=3","gravatar_id":"","url":"https://api.github.com/users/spinscale","html_url":"https://github.com/spinscale","followers_url":"https://api.github.com/users/spinscale/followers","following_url":"https://api.github.com/users/spinscale/following{/other_user}","gists_url":"https://api.github.com/users/spinscale/gists{/gist_id}","starred_url":"https://api.github.com/users/spinscale/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spinscale/subscriptions","organizations_url":"https://api.github.com/users/spinscale/orgs","repos_url":"https://api.github.com/users/spinscale/repos","events_url":"https://api.github.com/users/spinscale/events{/privacy}","received_events_url":"https://api.github.com/users/spinscale/received_events","type":"User","site_admin":false},"body":"Cant we just use `HttpMethod.valueOf()` instead of this? And thus also get rid of the `enLowerCase()` method above?","created_at":"2016-01-29T11:21:20Z","updated_at":"2016-01-29T11:21:20Z","html_url":"https://github.com/elastic/elasticsearch/pull/16092#discussion_r51250620","pull_request_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/16092","_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/51250620"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/16092#discussion_r51250620"},"pull_request":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/16092"}}},"pull_request":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/16092","id":56477369,"html_url":"https://github.com/elastic/elasticsearch/pull/16092","diff_url":"https://github.com/elastic/elasticsearch/pull/16092.diff","patch_url":"https://github.com/elastic/elasticsearch/pull/16092.patch","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/16092","number":16092,"state":"open","locked":false,"title":"More robust handling of CORS HTTP Access Control","user":{"login":"abeyad","id":1631297,"avatar_url":"https://avatars.githubusercontent.com/u/1631297?v=3","gravatar_id":"","url":"https://api.github.com/users/abeyad","html_url":"https://github.com/abeyad","followers_url":"https://api.github.com/users/abeyad/followers","following_url":"https://api.github.com/users/abeyad/following{/other_user}","gists_url":"https://api.github.com/users/abeyad/gists{/gist_id}","starred_url":"https://api.github.com/users/abeyad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abeyad/subscriptions","organizations_url":"https://api.github.com/users/abeyad/orgs","repos_url":"https://api.github.com/users/abeyad/repos","events_url":"https://api.github.com/users/abeyad/events{/privacy}","received_events_url":"https://api.github.com/users/abeyad/received_events","type":"User","site_admin":false},"body":"Uses a refactored version of Netty's CORS implementation to provide more\r\nrobust cross-origin resource request functionality.  The CORS specific\r\nElasticsearch parameters remain the same, just the underlying\r\nimplementation has changed.\r\n\r\nIt has also been refactored in a way that allows dropping in Netty's\r\nCORS handler as a replacement once Elasticsearch is upgraded to Netty 4.","created_at":"2016-01-19T15:02:54Z","updated_at":"2016-01-29T11:21:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"87d8a5fc986630e5d5bdb547f3a47c2186a17d91","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/16092/commits","review_comments_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/16092/comments","review_comment_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/16092/comments","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/3a725b07b622a8bac84739f6971e1fb28cca5df7","head":{"label":"abeyad:cors-handling","ref":"cors-handling","sha":"3a725b07b622a8bac84739f6971e1fb28cca5df7","user":{"login":"abeyad","id":1631297,"avatar_url":"https://avatars.githubusercontent.com/u/1631297?v=3","gravatar_id":"","url":"https://api.github.com/users/abeyad","html_url":"https://github.com/abeyad","followers_url":"https://api.github.com/users/abeyad/followers","following_url":"https://api.github.com/users/abeyad/following{/other_user}","gists_url":"https://api.github.com/users/abeyad/gists{/gist_id}","starred_url":"https://api.github.com/users/abeyad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abeyad/subscriptions","organizations_url":"https://api.github.com/users/abeyad/orgs","repos_url":"https://api.github.com/users/abeyad/repos","events_url":"https://api.github.com/users/abeyad/events{/privacy}","received_events_url":"https://api.github.com/users/abeyad/received_events","type":"User","site_admin":false},"repo":{"id":48216890,"name":"elasticsearch","full_name":"abeyad/elasticsearch","owner":{"login":"abeyad","id":1631297,"avatar_url":"https://avatars.githubusercontent.com/u/1631297?v=3","gravatar_id":"","url":"https://api.github.com/users/abeyad","html_url":"https://github.com/abeyad","followers_url":"https://api.github.com/users/abeyad/followers","following_url":"https://api.github.com/users/abeyad/following{/other_user}","gists_url":"https://api.github.com/users/abeyad/gists{/gist_id}","starred_url":"https://api.github.com/users/abeyad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abeyad/subscriptions","organizations_url":"https://api.github.com/users/abeyad/orgs","repos_url":"https://api.github.com/users/abeyad/repos","events_url":"https://api.github.com/users/abeyad/events{/privacy}","received_events_url":"https://api.github.com/users/abeyad/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/abeyad/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":true,"url":"https://api.github.com/repos/abeyad/elasticsearch","forks_url":"https://api.github.com/repos/abeyad/elasticsearch/forks","keys_url":"https://api.github.com/repos/abeyad/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/abeyad/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/abeyad/elasticsearch/teams","hooks_url":"https://api.github.com/repos/abeyad/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/abeyad/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/abeyad/elasticsearch/events","assignees_url":"https://api.github.com/repos/abeyad/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/abeyad/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/abeyad/elasticsearch/tags","blobs_url":"https://api.github.com/repos/abeyad/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/abeyad/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/abeyad/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/abeyad/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/abeyad/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/abeyad/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/abeyad/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/abeyad/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/abeyad/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/abeyad/elasticsearch/subscription","commits_url":"https://api.github.com/repos/abeyad/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/abeyad/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/abeyad/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/abeyad/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/abeyad/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/abeyad/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/abeyad/elasticsearch/merges","archive_url":"https://api.github.com/repos/abeyad/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/abeyad/elasticsearch/downloads","issues_url":"https://api.github.com/repos/abeyad/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/abeyad/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/abeyad/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/abeyad/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/abeyad/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/abeyad/elasticsearch/releases{/id}","deployments_url":"https://api.github.com/repos/abeyad/elasticsearch/deployments","created_at":"2015-12-18T05:46:26Z","updated_at":"2015-12-18T05:46:48Z","pushed_at":"2016-01-28T04:50:28Z","git_url":"git://github.com/abeyad/elasticsearch.git","ssh_url":"git@github.com:abeyad/elasticsearch.git","clone_url":"https://github.com/abeyad/elasticsearch.git","svn_url":"https://github.com/abeyad/elasticsearch","homepage":"https://www.elastic.co/products/elasticsearch","size":189413,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"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":"elastic:master","ref":"master","sha":"df24019261b99fd32906add48e9a3826ecc5998c","user":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"repo":{"id":507775,"name":"elasticsearch","full_name":"elastic/elasticsearch","owner":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elastic/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":false,"url":"https://api.github.com/repos/elastic/elasticsearch","forks_url":"https://api.github.com/repos/elastic/elasticsearch/forks","keys_url":"https://api.github.com/repos/elastic/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elastic/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elastic/elasticsearch/teams","hooks_url":"https://api.github.com/repos/elastic/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/elastic/elasticsearch/events","assignees_url":"https://api.github.com/repos/elastic/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/elastic/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/elastic/elasticsearch/tags","blobs_url":"https://api.github.com/repos/elastic/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elastic/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elastic/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/elastic/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/elastic/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/elastic/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/elastic/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/elastic/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/elastic/elasticsearch/subscription","commits_url":"https://api.github.com/repos/elastic/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/elastic/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/elastic/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/elastic/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/elastic/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elastic/elasticsearch/merges","archive_url":"https://api.github.com/repos/elastic/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elastic/elasticsearch/downloads","issues_url":"https://api.github.com/repos/elastic/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/elastic/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/elastic/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/elastic/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elastic/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/elastic/elasticsearch/releases{/id}","deployments_url":"https://api.github.com/repos/elastic/elasticsearch/deployments","created_at":"2010-02-08T13:20:56Z","updated_at":"2016-01-29T11:20:23Z","pushed_at":"2016-01-29T11:21:04Z","git_url":"git://github.com/elastic/elasticsearch.git","ssh_url":"git@github.com:elastic/elasticsearch.git","clone_url":"https://github.com/elastic/elasticsearch.git","svn_url":"https://github.com/elastic/elasticsearch","homepage":"https://www.elastic.co/products/elasticsearch","size":283219,"stargazers_count":14821,"watchers_count":14821,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4892,"mirror_url":null,"open_issues_count":1144,"forks":4892,"open_issues":1144,"watchers":14821,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/16092"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/16092"},"issue":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/16092"},"comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/16092/comments"},"review_comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/16092/comments"},"review_comment":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/16092/commits"},"statuses":{"href":"https://api.github.com/repos/elastic/elasticsearch/statuses/3a725b07b622a8bac84739f6971e1fb28cca5df7"}}}}