You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by iilyak <gi...@git.apache.org> on 2016/03/10 21:02:54 UTC
[GitHub] couchdb-couch pull request: 2966 before response hook
GitHub user iilyak opened a pull request:
https://github.com/apache/couchdb-couch/pull/150
2966 before response hook
Implement `before_response` and `before_serve_file` EPI hooks.
These hooks are useful in following cases:
- to inject vendor specific headers
- to inject vendor keys into json objects returned to client
- to override return code
This is a replacement PR for #133
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/cloudant/couchdb-couch 2966-before_response_hook
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/couchdb-couch/pull/150.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #150
----
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] couchdb-couch pull request: 2966 before response hook
Posted by kxepal <gi...@git.apache.org>.
Github user kxepal commented on a diff in the pull request:
https://github.com/apache/couchdb-couch/pull/150#discussion_r55953630
--- Diff: src/couch_httpd.erl ---
@@ -1120,6 +1110,37 @@ validate_bind_address(Address) ->
_ -> throw({error, invalid_bind_address})
end.
+add_headers(Req, Headers0) ->
+ Headers = basic_headers(Req, Headers0),
+ http_1_0_keep_alive(Req, Headers).
+
+basic_headers(Req, Headers0) ->
+ Headers = basic_headers_no_cors(Req, Headers0),
+ chttpd_cors:headers(Req, Headers).
+
+basic_headers_no_cors(Req, Headers) ->
+ Headers
+ ++ server_header()
+ ++ couch_httpd_auth:cookie_auth_header(Req, Headers).
+
+handle_response(Req0, Code0, Headers0, Args0, Type) ->
+ {ok, {Req1, Code1, Headers1, Args1}} = before_response(Req0, Code0, Headers0, Args0),
+ couch_stats:increment_counter([couchdb, httpd_status_codes, Code1]),
+ log_request(Req0, Code1),
+ respond_(Req1, Code1, Headers1, Args1, Type).
+
+before_response(Req0, Code0, Headers0, {json, JsonObj}) ->
+ {ok, {Req1, Code1, Headers1, Body1}} =
+ chttpd_plugin:before_response(Req0, Code0, Headers0, JsonObj),
+ Body2 = [start_jsonp(), ?JSON_ENCODE(Body1), end_jsonp(), $\n],
+ {ok, {Req1, Code1, Headers1, Body2}};
+before_response(Req0, Code0, Headers0, Args0) ->
+ chttpd_plugin:before_response(Req0, Code0, Headers0, Args0).
+
+respond_(#httpd{mochi_req = MochiReq}, Code, Headers, _Args, start_response) ->
--- End diff --
Does it worth to isolate MochiReq here and now while we still have a lot of places where we use it directly? I just quite don't like such kind of generic functions and it seems that this doesn't gives any positive gain now, but one more function in call chain (and traceback).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] couchdb-couch pull request: 2966 before response hook
Posted by eiri <gi...@git.apache.org>.
Github user eiri commented on a diff in the pull request:
https://github.com/apache/couchdb-couch/pull/150#discussion_r55997264
--- Diff: src/couch_httpd.erl ---
@@ -476,14 +476,18 @@ accepted_encodings(#httpd{mochi_req=MochiReq}) ->
serve_file(Req, RelativePath, DocumentRoot) ->
serve_file(Req, RelativePath, DocumentRoot, []).
-serve_file(#httpd{mochi_req=MochiReq}=Req, RelativePath, DocumentRoot,
- ExtraHeaders) ->
- log_request(Req, 200),
- ResponseHeaders = server_header()
- ++ couch_httpd_auth:cookie_auth_header(Req, [])
- ++ ExtraHeaders,
- ResponseHeaders1 = chttpd_cors:headers(Req, ResponseHeaders),
- {ok, MochiReq:serve_file(RelativePath, DocumentRoot, ResponseHeaders1)}.
+serve_file(Req0, RelativePath0, DocumentRoot0, ExtraHeaders) ->
--- End diff --
Why is every line in this function double-spaced?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] couchdb-couch pull request: 2966 before response hook
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/couchdb-couch/pull/150
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] couchdb-couch pull request: 2966 before response hook
Posted by kxepal <gi...@git.apache.org>.
Github user kxepal commented on the pull request:
https://github.com/apache/couchdb-couch/pull/150#issuecomment-196123231
+1, but I think we can live without `respond_`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] couchdb-couch pull request: 2966 before response hook
Posted by kxepal <gi...@git.apache.org>.
Github user kxepal commented on a diff in the pull request:
https://github.com/apache/couchdb-couch/pull/150#discussion_r55953822
--- Diff: src/couch_httpd.erl ---
@@ -1120,6 +1110,37 @@ validate_bind_address(Address) ->
_ -> throw({error, invalid_bind_address})
end.
+add_headers(Req, Headers0) ->
+ Headers = basic_headers(Req, Headers0),
+ http_1_0_keep_alive(Req, Headers).
+
+basic_headers(Req, Headers0) ->
+ Headers = basic_headers_no_cors(Req, Headers0),
+ chttpd_cors:headers(Req, Headers).
+
+basic_headers_no_cors(Req, Headers) ->
+ Headers
+ ++ server_header()
+ ++ couch_httpd_auth:cookie_auth_header(Req, Headers).
+
+handle_response(Req0, Code0, Headers0, Args0, Type) ->
+ {ok, {Req1, Code1, Headers1, Args1}} = before_response(Req0, Code0, Headers0, Args0),
+ couch_stats:increment_counter([couchdb, httpd_status_codes, Code1]),
+ log_request(Req0, Code1),
+ respond_(Req1, Code1, Headers1, Args1, Type).
+
+before_response(Req0, Code0, Headers0, {json, JsonObj}) ->
+ {ok, {Req1, Code1, Headers1, Body1}} =
+ chttpd_plugin:before_response(Req0, Code0, Headers0, JsonObj),
+ Body2 = [start_jsonp(), ?JSON_ENCODE(Body1), end_jsonp(), $\n],
+ {ok, {Req1, Code1, Headers1, Body2}};
+before_response(Req0, Code0, Headers0, Args0) ->
+ chttpd_plugin:before_response(Req0, Code0, Headers0, Args0).
+
+respond_(#httpd{mochi_req = MochiReq}, Code, Headers, _Args, start_response) ->
--- End diff --
Ok, after thinking about the ways how to make the same changes without it, I ended with callbacks. Now sure which one is better here. So ok.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] couchdb-couch pull request: 2966 before response hook
Posted by iilyak <gi...@git.apache.org>.
Github user iilyak commented on a diff in the pull request:
https://github.com/apache/couchdb-couch/pull/150#discussion_r56006096
--- Diff: src/couch_httpd.erl ---
@@ -476,14 +476,18 @@ accepted_encodings(#httpd{mochi_req=MochiReq}) ->
serve_file(Req, RelativePath, DocumentRoot) ->
serve_file(Req, RelativePath, DocumentRoot, []).
-serve_file(#httpd{mochi_req=MochiReq}=Req, RelativePath, DocumentRoot,
- ExtraHeaders) ->
- log_request(Req, 200),
- ResponseHeaders = server_header()
- ++ couch_httpd_auth:cookie_auth_header(Req, [])
- ++ ExtraHeaders,
- ResponseHeaders1 = chttpd_cors:headers(Req, ResponseHeaders),
- {ok, MochiReq:serve_file(RelativePath, DocumentRoot, ResponseHeaders1)}.
+serve_file(Req0, RelativePath0, DocumentRoot0, ExtraHeaders) ->
--- End diff --
@eiri: fixed
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] couchdb-couch pull request: 2966 before response hook
Posted by eiri <gi...@git.apache.org>.
Github user eiri commented on the pull request:
https://github.com/apache/couchdb-couch/pull/150#issuecomment-196463655
+1
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] couchdb-couch pull request: 2966 before response hook
Posted by jaydoane <gi...@git.apache.org>.
Github user jaydoane commented on the pull request:
https://github.com/apache/couchdb-couch/pull/150#issuecomment-196397434
+1
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---