Author: jan
Date: Tue Aug 18 17:09:42 2009
New Revision: 805500
URL: http://svn.apache.org/viewvc?rev=805500&view=rev
Log:
allow configurable X-Forwarded-Host header handling for work behind reverse
proxies, patch by Benoit Chesneau, closes COUCHDB-466
Added:
couchdb/trunk/share/www/script/test/http.js
Modified:
couchdb/trunk/share/Makefile.am
couchdb/trunk/share/www/script/couch_tests.js
couchdb/trunk/src/couchdb/couch_httpd.erl
Modified: couchdb/trunk/share/Makefile.am
URL:
http://svn.apache.org/viewvc/couchdb/trunk/share/Makefile.am?rev=805500&r1=805499&r2=805500&view=diff
==============================================================================
--- couchdb/trunk/share/Makefile.am (original)
+++ couchdb/trunk/share/Makefile.am Tue Aug 18 17:09:42 2009
@@ -117,6 +117,7 @@
www/script/test/etags_head.js \
www/script/test/etags_views.js \
www/script/test/form_submit.js \
+ www/script/test/http.js \
www/script/test/invalid_docids.js \
www/script/test/jsonp.js \
www/script/test/large_docs.js \
Modified: couchdb/trunk/share/www/script/couch_tests.js
URL:
http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/couch_tests.js?rev=805500&r1=805499&r2=805500&view=diff
==============================================================================
--- couchdb/trunk/share/www/script/couch_tests.js [utf-8] (original)
+++ couchdb/trunk/share/www/script/couch_tests.js [utf-8] Tue Aug 18 17:09:42
2009
@@ -81,6 +81,7 @@
loadTest("stats.js");
loadTest("rev_stemming.js");
loadTest("erlang_views.js");
+loadTest("http.js");
function makeDocs(start, end, templateDoc) {
var templateDocSrc = templateDoc ? JSON.stringify(templateDoc) : "{}"
Added: couchdb/trunk/share/www/script/test/http.js
URL:
http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/test/http.js?rev=805500&view=auto
==============================================================================
--- couchdb/trunk/share/www/script/test/http.js (added)
+++ couchdb/trunk/share/www/script/test/http.js Tue Aug 18 17:09:42 2009
@@ -0,0 +1,53 @@
+// Licensed 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.
+
+couchTests.http = function(debug) {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+ db.deleteDb();
+
+ // bug COUCHDB-100: DELETE on non-existent DB returns 500 instead of 404
+ db.deleteDb();
+
+ db.createDb();
+
+ // PUT on existing DB should return 412 instead of 500
+ if (debug) debugger;
+
+ var xhr = CouchDB.request("PUT", "/test_suite_db/test", {body: "{}"});
+
+ TEquals("http://127.0.0.1:5984/test_suite_db/test",
+ xhr.getResponseHeader("Location"),
+ "should include ip address");
+
+ xhr = CouchDB.request("PUT", "/test_suite_db/test2", {
+ body: "{}",
+ headers: {"X-Forwarded-Host": "mysite.com"}
+ });
+
+ TEquals("http://mysite.com/test_suite_db/test2",
+ xhr.getResponseHeader("Location"),
+ "should include X-Forwarded-Host");
+
+ run_on_modified_server([{
+ section:"httpd",
+ key:"x_forwarded_host",
+ value:"X-Host"}],
+ function() {
+ xhr = CouchDB.request("PUT", "/test_suite_db/test3", {
+ body: "{}",
+ headers: {"X-Host": "mysite2.com"}
+ });
+ TEquals("http://mysite2.com/test_suite_db/test3",
+ xhr.getResponseHeader("Location"),
+ "should include X-Host");
+ });
+}
Modified: couchdb/trunk/src/couchdb/couch_httpd.erl
URL:
http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_httpd.erl?rev=805500&r1=805499&r2=805500&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_httpd.erl (original)
+++ couchdb/trunk/src/couchdb/couch_httpd.erl Tue Aug 18 17:09:42 2009
@@ -256,10 +256,16 @@
MochiReq:get(path).
absolute_uri(#httpd{mochi_req=MochiReq}, Path) ->
- Host = case MochiReq:get_header_value("Host") of
+ XHost = couch_config:get("httpd", "x_forwarded_host", "X-Forwarded-Host"),
+ Host = case MochiReq:get_header_value(XHost) of
undefined ->
- {ok, {Address, Port}} = inet:sockname(MochiReq:get(socket)),
- inet_parse:ntoa(Address) ++ ":" ++ integer_to_list(Port);
+ case MochiReq:get_header_value("Host") of
+ undefined ->
+ {ok, {Address, Port}} =
inet:sockname(MochiReq:get(socket)),
+ inet_parse:ntoa(Address) ++ ":" ++ integer_to_list(Port);
+ Value1 ->
+ Value1
+ end;
Value -> Value
end,
"http://" ++ Host ++ Path.