You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2010/02/19 20:07:43 UTC

svn commit: r911935 - in /couchdb/trunk: etc/couchdb/local.ini src/couchdb/couch_httpd.erl src/couchdb/couch_httpd_misc_handlers.erl src/couchdb/couch_httpd_rewrite.erl

Author: jan
Date: Fri Feb 19 19:07:43 2010
New Revision: 911935

URL: http://svn.apache.org/viewvc?rev=911935&view=rev
Log:
Add virtual host handling.

; To enable Virtual Hosts in CouchDB, add a vhost = path directive. All requests to
; the Virual Host will be redirected to the path. In the example below all requests
; to http://example.com/ are redirected to /database.
; If you run CouchDB on a specific port, include the port number in the vhost:
; example.com:5984 = /database

[vhosts]
;example.com = /database/

Modified:
    couchdb/trunk/etc/couchdb/local.ini
    couchdb/trunk/src/couchdb/couch_httpd.erl
    couchdb/trunk/src/couchdb/couch_httpd_misc_handlers.erl
    couchdb/trunk/src/couchdb/couch_httpd_rewrite.erl

Modified: couchdb/trunk/etc/couchdb/local.ini
URL: http://svn.apache.org/viewvc/couchdb/trunk/etc/couchdb/local.ini?rev=911935&r1=911934&r2=911935&view=diff
==============================================================================
--- couchdb/trunk/etc/couchdb/local.ini (original)
+++ couchdb/trunk/etc/couchdb/local.ini Fri Feb 19 19:07:43 2010
@@ -16,6 +16,16 @@
 [log]
 ;level = debug
 
+
+; To enable Virtual Hosts in CouchDB, add a vhost = path directive. All requests to
+; the Virual Host will be redirected to the path. In the example below all requests
+; to http://example.com/ are redirected to /database.
+; If you run CouchDB on a specific port, include the port number in the vhost:
+; example.com:5984 = /database
+
+[vhosts]
+;example.com = /database/
+
 [update_notification]
 ;unique notifier name=/full/path/to/exe -with "cmd line arg"
 

Modified: couchdb/trunk/src/couchdb/couch_httpd.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_httpd.erl?rev=911935&r1=911934&r2=911935&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_httpd.erl (original)
+++ couchdb/trunk/src/couchdb/couch_httpd.erl Fri Feb 19 19:07:43 2010
@@ -25,7 +25,7 @@
 -export([start_json_response/2, start_json_response/3, end_json_response/1]).
 -export([send_response/4,send_method_not_allowed/2,send_error/4, send_redirect/2,send_chunked_error/2]).
 -export([send_json/2,send_json/3,send_json/4,last_chunk/1,parse_multipart_request/3]).
--export([accepted_encodings/1]).
+-export([accepted_encodings/1,handle_request_int/5]).
 
 start_link() ->
     % read config and register for configuration changes
@@ -127,9 +127,46 @@
 stop() ->
     mochiweb_http:stop(?MODULE).
 
+%%
+
+% if there's a vhost definition that matches the request, redirect internally
+redirect_to_vhost(MochiReq, DefaultFun,
+    UrlHandlers, DbUrlHandlers, DesignUrlHandlers, VhostTarget) ->
+
+    Path = MochiReq:get(path),
+    Target = VhostTarget ++ Path,
+    ?LOG_DEBUG("Vhost Target: '~p'~n", [Target]),
+    % build a new mochiweb request
+    MochiReq1 = mochiweb_request:new(MochiReq:get(socket),
+                                      MochiReq:get(method),
+                                      Target,
+                                      MochiReq:get(version),
+                                      MochiReq:get(headers)),
+    % cleanup, It force mochiweb to reparse raw uri.
+    MochiReq1:cleanup(),
+
+    handle_request1(MochiReq1, DefaultFun,
+        UrlHandlers, DbUrlHandlers, DesignUrlHandlers).
 
 handle_request(MochiReq, DefaultFun,
-        UrlHandlers, DbUrlHandlers, DesignUrlHandlers) ->
+    UrlHandlers, DbUrlHandlers, DesignUrlHandlers) ->
+
+    % grab Host from Req
+    Vhost = MochiReq:get_header_value("Host"),
+
+    % find Vhost in config
+    case couch_config:get("vhosts", Vhost, false) of
+        false -> % business as usual
+            handle_request_int(MochiReq, DefaultFun,
+                    UrlHandlers, DbUrlHandlers, DesignUrlHandlers);
+        VhostTarget ->
+            redirect_to_vhost(MochiReq, DefaultFun,
+                UrlHandlers, DbUrlHandlers, DesignUrlHandlers, VhostTarget)
+    end.
+
+
+handle_request_int(MochiReq, DefaultFun,
+            UrlHandlers, DbUrlHandlers, DesignUrlHandlers) ->
     Begin = now(),
     AuthenticationSrcs = make_fun_spec_strs(
             couch_config:get("httpd", "authentication_handlers")),

Modified: couchdb/trunk/src/couchdb/couch_httpd_misc_handlers.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_httpd_misc_handlers.erl?rev=911935&r1=911934&r2=911935&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_httpd_misc_handlers.erl (original)
+++ couchdb/trunk/src/couchdb/couch_httpd_misc_handlers.erl Fri Feb 19 19:07:43 2010
@@ -46,6 +46,7 @@
         {"Expires", httpd_util:rfc1123_date(OneYearFromNow)}
     ],
     couch_httpd:serve_file(Req, "favicon.ico", DocumentRoot, CachingHeaders);
+
 handle_favicon_req(Req, _) ->
     send_method_not_allowed(Req, "GET,HEAD").
 

Modified: couchdb/trunk/src/couchdb/couch_httpd_rewrite.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_httpd_rewrite.erl?rev=911935&r1=911934&r2=911935&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_httpd_rewrite.erl (original)
+++ couchdb/trunk/src/couchdb/couch_httpd_rewrite.erl Fri Feb 19 19:07:43 2010
@@ -178,8 +178,7 @@
                 default_fun = DefaultFun,
                 url_handlers = UrlHandlers
             } = Req,
-
-            couch_httpd:handle_request(MochiReq1, DefaultFun,
+            couch_httpd:handle_request_int(MochiReq1, DefaultFun,
                     UrlHandlers, DbUrlHandlers, DesignUrlHandlers)
         end.