Permalink
Browse files

added feature to not pick first sconf when virthosting

git-svn-id: https://erlyaws.svn.sourceforge.net/svnroot/erlyaws/trunk/yaws@918 9fbdc01b-0d2c-0410-bfb7-fb27d70d8b52
  • Loading branch information...
1 parent c51b37f commit 14176e66c176441b7a981ef3d89152e732c1f110 @klacke committed Sep 29, 2005
Showing with 91 additions and 26 deletions.
  1. +16 −11 include/yaws.hrl
  2. +9 −0 man/yaws.conf.5
  3. +6 −1 scripts/yaws.conf.template
  4. +15 −2 src/yaws_config.erl
  5. +45 −12 src/yaws_server.erl
View
@@ -10,13 +10,15 @@
%% flags for gconfs
--define(GC_TTY_TRACE, 1).
--define(GC_DEBUG, 2).
--define(GC_AUTH_LOG, 4).
--define(GC_COPY_ERRLOG, 8).
--define(GC_BACKWARDS_COMPAT_PARSE, 16).
--define(GC_LOG_RESOLVE_HOSTNAME, 32).
--define(GC_FAIL_ON_BIND_ERR, 64).
+-define(GC_TTY_TRACE, 1).
+-define(GC_DEBUG, 2).
+-define(GC_AUTH_LOG, 4).
+-define(GC_COPY_ERRLOG, 8).
+-define(GC_BACKWARDS_COMPAT_PARSE, 16).
+-define(GC_LOG_RESOLVE_HOSTNAME, 32).
+-define(GC_FAIL_ON_BIND_ERR, 64).
+-define(GC_PICK_FIRST_VIRTHOST_ON_NOMATCH, 128).
+
-define(GC_DEF, (?GC_AUTH_LOG bor ?GC_FAIL_ON_BIND_ERR)).
@@ -34,7 +36,8 @@
((GC#gconf.flags band ?GC_LOG_RESOLVE_HOSTNAME) /= 0)).
-define(gc_fail_on_bind_err(GC),
((GC#gconf.flags band ?GC_FAIL_ON_BIND_ERR) /= 0)).
-
+-define(gc_pick_first_virthost_on_nomatch(GC),
+ ((GC#gconf.flags band ?GC_PICK_FIRST_VIRTHOST_ON_NOMATCH) /= 0)).
-define(gc_set_tty_trace(GC, Bool),
GC#gconf{flags = yaws:flag(GC#gconf.flags,?GC_TTY_TRACE, Bool)}).
@@ -48,11 +51,13 @@
GC#gconf{flags = yaws:flag(GC#gconf.flags,
?GC_BACKWARDS_COMPAT_PARSE, Bool)}).
-define(gc_log_set_resolve_hostname(GC, Bool),
- GC#gconf{flags = yaws:flag(GC#gconf.flags, ?GC_LOG_RESOLVE_HOSTNAME, Bool)}).
-
+ GC#gconf{flags = yaws:flag(GC#gconf.flags,
+ ?GC_LOG_RESOLVE_HOSTNAME, Bool)}).
-define(gc_set_fail_on_bind_err(GC, Bool),
GC#gconf{flags = yaws:flag(GC#gconf.flags,?GC_FAIL_ON_BIND_ERR,Bool)}).
-
+-define(gc_set_pick_first_virthost_on_nomatch(GC, Bool),
+ GC#gconf{flags = yaws:flag(GC#gconf.flags,
+ ?GC_PICK_FIRST_VIRTHOST_ON_NOMATCH,Bool)}).
%% global conf
View
@@ -323,6 +323,15 @@ It is possible to have multiple reverse proxies inside the same server.
WARNING, this feature is yet not in production quality.
+
+.TP
+\fBservername = Name\fR
+If we're virthosting everal servers and want to force a server
+to match specific Host: headers we can do this with the "servername"
+directive. This name doesn't necessarily have to be the same as the
+the name inside <server Name> in certain NAT scenarios. Rarely used feature.
+
+
.TP
\fB <ssl> .... </ssl> \fR
This begins and ends an SSL configuration for this server.
@@ -94,8 +94,13 @@ auth_log = true
# id = myname
+# earlier versions of Yaws picked the first virtual host
+# in a list of hosts with the same IP/PORT when the Host:
+# header doesn't match any name on any Host
+# This is often nice in testing environments but not
+# acceptable in real live hosting scenarios
-
+pick_first_virthost_on_nomatch = true
View
@@ -447,9 +447,21 @@ fload(FD, globals, GC, C, Cs, Lno, Chars) ->
["id", '=', String] ->
fload(FD, globals, GC#gconf{id=String},C, Cs, Lno+1, Next);
+ ["pick_first_virthost_on_nomatch", '=', Bool] ->
+ case is_bool(Bool) of
+ {true, Val} ->
+ fload(FD, globals,
+ ?gc_set_pick_first_virthost_on_nomatch(GC,Val),
+ C, Cs, Lno+1, Next);
+ false ->
+ {error, ?F("Expect true|false at line ~w", [Lno])}
+ end;
+
+
['<', "server", Server, '>'] -> %% first server
- fload(FD, server, GC, #sconf{servername = Server},
- Cs, Lno+1, Next);
+ fload(FD, server, GC, #sconf{servername = Server},
+ Cs, Lno+1, Next);
+
[H|_] ->
{error, ?F("Unexpected tokens ~p at line ~w", [H, Lno])}
end;
@@ -562,6 +574,7 @@ fload(FD, server, GC, C, Cs, Lno, Chars) ->
['<', "redirect", '>'] ->
fload(FD, server_redirect, GC, C, Cs, Lno+1, Next, []);
+ %% noop
["default_server_on_this_ip", '=', _Bool] ->
fload(FD, server, GC, C, Cs, Lno+1, Next);
View
@@ -63,7 +63,9 @@ gs_status() ->
P ! {self(), status},
receive {P, Stat} -> Stat end
end, Pids).
-
+getconf() ->
+ gen_server:call(?MODULE,getconf).
+
stats() ->
{S, Time} = status(),
Diff = calendar:time_difference(Time, calendar:local_time()),
@@ -908,24 +910,55 @@ fix_abs_uri(Req, H) ->
_ -> {Req, H}
end.
-pick_sconf(_GS, _H, Group, ssl) ->
- hd(Group);
-pick_sconf(GS, H, Group, nossl) ->
+%% compare servername and ignore any optional
+%% :Port postfix
+comp_sname([$:|_], _) -> true;
+comp_sname(_, [$:|_]) -> true;
+comp_sname([H|T], [H|T1]) -> comp_sname(T,T1);
+comp_sname([],[])-> true;
+comp_sname(_,_)-> false.
+
+
+
+
+pick_sconf(GC, H, [SC|Group], ssl) ->
+ case comp_sname(H#headers.host, SC#sconf.servername) of
+ true ->
+ SC;
+ false when ?gc_pick_first_virthost_on_nomatch(GC) ->
+ SC;
+ false ->
+ yaws_debug:format("Drop req since ~p =/ ~p \n",
+ [H#headers.host, SC#sconf.servername]),
+ exit(normal)
+ end;
+
+
+pick_sconf(GC, H, Group, nossl) ->
case H#headers.host of
- undefined ->
+ undefined when ?gc_pick_first_virthost_on_nomatch(GC) ->
hd(Group);
Host ->
- pick_host(GS, Host, Group, Group)
+ pick_host(GC, Host, Group, Group)
+ end.
+
+
+pick_host(GC, Host, [SC|T], Group) ->
+ case comp_sname(Host, SC#sconf.servername) of
+ true -> SC;
+ false -> pick_host(GC, Host, T, Group)
+ end;
+pick_host(GC, Host, [], Group) ->
+ if ?gc_pick_first_virthost_on_nomatch(GC) ->
+ hd(Group);
+ true ->
+ yaws_debug:format("Drop req since ~p doesn't match any servername \n",
+ [Host]),
+ exit(normal)
end.
-pick_host(_GC, Host, [H|_T], _Group) when H#sconf.servername == Host ->
- H;
-pick_host(GC, Host, [_|T], Group) ->
- pick_host(GC, Host, T, Group);
-pick_host(_GC, _Host, [], Group) ->
- hd(Group).
inet_peername(Sock, SC) ->

0 comments on commit 14176e6

Please sign in to comment.