httpd-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f...@locus.apache.org
Subject cvs commit: apache-1.3/src/main http_vhost.c
Date Tue, 12 Sep 2000 16:32:38 GMT
fanf        00/09/12 09:32:38

  Modified:    htdocs/manual/mod core.html
               htdocs/manual/vhosts details.html examples.html
                        name-based.html
               src      CHANGES
               src/main http_vhost.c
  Log:
  This commit adds support for purely name-based virtual hosting that
  does not require any IP addresses in httpd.conf and which disregards
  the local IP address of any connections. This will be particularly
  appreciated by people running little servers on machines with
  dynamically configured IP addresses, and by people wishing to use
  identical httpd.conf files on all the machines in a load-balanced
  cluster.
  
  The configuration syntax is:
          NameVirtualHost *
          <VirtualHost *>
                  ServerName my.friend.has.a.silly.vanitydomain.org
                  DocumentRoot /usr/local/apache/docs1
          </VirtualHost>
          <VirtualHost *>
                  ServerName but.easier.to.spell.than.my.vanitydomain.org
                  DocumentRoot /usr/local/apache/docs2
          </VirtualHost>
  
  The implementation is a straightforward extension of what is there
  already although the patch is made somewhat larger by the need to
  avoid duplicate code for normal and wildcarded NameVirtualHosts.
  
  PR: 5595, 4455
  
  Revision  Changes    Path
  1.171     +8 -0      httpd-docs-1.3/htdocs/manual/mod/core.html
  
  Index: core.html
  ===================================================================
  RCS file: /home/cvs/httpd-docs-1.3/htdocs/manual/mod/core.html,v
  retrieving revision 1.170
  retrieving revision 1.171
  diff -u -u -r1.170 -r1.171
  --- core.html	2000/09/12 02:29:12	1.170
  +++ core.html	2000/09/12 16:32:35	1.171
  @@ -2146,6 +2146,14 @@
   
   <BLOCKQUOTE><CODE>NameVirtualHost 111.22.33.44:8080</CODE></BLOCKQUOTE>
   
  +In Apache 1.3.13 and greater you can specify a <CODE>*</CODE> for the
  +<EM>addr</EM>. This creates a wildcard NameVirtualHost which will
  +match connections to any address that isn't configured with a more
  +specific NameVirtualHost directive or
  +<A HREF="#virtualhost">&lt;VirtualHost&gt;</A> section. This is useful
  +if you want only name-based virtual hosts and you don't want to
  +hard-code the server's IP address into the configuration file.<P>
  +
   <STRONG>See also:</STRONG>
   <A HREF="../vhosts/index.html">Apache Virtual Host documentation</A>
   <HR>
  
  
  
  1.11      +9 -1      httpd-docs-1.3/htdocs/manual/vhosts/details.html
  
  Index: details.html
  ===================================================================
  RCS file: /home/cvs/httpd-docs-1.3/htdocs/manual/vhosts/details.html,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -u -r1.10 -r1.11
  --- details.html	1999/02/14 14:26:49	1.10
  +++ details.html	2000/09/12 16:32:35	1.11
  @@ -69,7 +69,8 @@
   
   <P>Unless a <A HREF="../mod/core.html#namevirtualhost">NameVirtualHost</A>
   directive is used for a specific IP address the first vhost with
  -that address is treated as an IP-based vhost.
  +that address is treated as an IP-based vhost. In 1.3.13 and later that
  +includes the IP address <CODE>*</CODE>.
   
   <P>If name-based vhosts should be used a <CODE>NameVirtualHost</CODE>
   directive <EM>must</EM> appear with the IP address set to be used for the
  @@ -202,6 +203,11 @@
   matching <SAMP>_default_</SAMP> vhost the request is served from the
   main_server.
   
  +<P>In Apache 1.3.13 and later, if the IP address is not found in the
  +hash table then the match against the port number may also result in
  +an entry corresponding to a <CODE>NameVirtualHost *</CODE>, which is
  +subsequently handled like other name-based vhosts.
  +
   <P>If the lookup succeeded (a corresponding list for the IP address was
   found) the next step is to decide if we have to deal with an IP-based
   or a name-base vhost.
  @@ -309,6 +315,8 @@
       <CODE>_default_</CODE> vhost which is your standard <CODE>Port</CODE>
       by default. A wildcard port can be specified (<EM>i.e.</EM>,
       <CODE>_default_:*</CODE>) to catch requests to any available port.
  +    In Apache 1.3.13 and later this also applies to
  +    <CODE>NameVirtualHost *</CODE> vhosts.
       <P>
       
   <LI>The main_server is only used to serve a request if the IP address
  
  
  
  1.8       +47 -0     httpd-docs-1.3/htdocs/manual/vhosts/examples.html
  
  Index: examples.html
  ===================================================================
  RCS file: /home/cvs/httpd-docs-1.3/htdocs/manual/vhosts/examples.html,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -u -r1.7 -r1.8
  --- examples.html	2000/09/12 02:29:17	1.7
  +++ examples.html	2000/09/12 16:32:36	1.8
  @@ -30,6 +30,7 @@
   <LI><A HREF="#default">Using <CODE>_default_</CODE> vhosts</A>
   <LI><A HREF="#migrate">Migrating a named-based vhost to an IP-based vhost</A>
   <LI><A HREF="#serverpath">Using the <CODE>ServerPath</CODE> directive</A>
  +<LI><A HREF="#purename">Purely name-based vhosting</A>
   </UL>
   
   <HR>
  @@ -540,6 +541,52 @@
   
   </UL> 
   
  +<H3><A NAME="purename">Purely name-based vhosting</A></H3>
  +
  +<UL>
  +
  +<LI><STRONG>Compatibility:</STRONG>
  +    This feature was added in Apache 1.3.13.
  +
  +<LI><STRONG>Setup:</STRONG>
  +    The server machine has an IP address
  +    which resolves to the name <SAMP>server.domain.tld</SAMP>.
  +    There are two aliases (CNAMEs) <SAMP>www.domain.tld</SAMP> and
  +    <SAMP>www.sub.domain.tld</SAMP> for the address <SAMP>server.domain.tld</SAMP>.
  +    <P>
  +    <STRONG>Server configuration:</STRONG>
  +
  +    <BLOCKQUOTE><PRE>
  +    ...
  +    Port 80
  +    ServerName server.domain.tld
  +
  +    NameVirtualHost *
  +
  +    &lt;VirtualHost *&gt;
  +    DocumentRoot /www/domain
  +    ServerName www.domain.tld
  +    ...
  +    &lt;/VirtualHost&gt;
  +    
  +    &lt;VirtualHost *&gt;
  +    DocumentRoot /www/subdomain
  +    ServerName www.sub.domain.tld
  +    ...
  +    &lt;/VirtualHost&gt; 
  +    </PRE>
  +    There are no unspecified
  +    addresses/ports, therefore the main server serves
  +    no requests. Due to the fact
  +    that <SAMP>www.domain.tld</SAMP> has the highest priority
  +    it can be seen as the <CITE>default</CITE> or
  +    <CITE>primary</CITE> server.
  +    </BLOCKQUOTE>
  +    <P>
  +
  +</UL>
  +
  +<LI><STRONG>Setup:</STRONG>
   <!--#include virtual="footer.html" -->
   </BODY>
   </HTML>
  
  
  
  1.12      +9 -0      httpd-docs-1.3/htdocs/manual/vhosts/name-based.html
  
  Index: name-based.html
  ===================================================================
  RCS file: /home/cvs/httpd-docs-1.3/htdocs/manual/vhosts/name-based.html,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -u -r1.11 -r1.12
  --- name-based.html	2000/09/09 17:59:42	1.11
  +++ name-based.html	2000/09/12 16:32:36	1.12
  @@ -84,6 +84,15 @@
   In other words, you should add a &lt;VirtualHost&gt; section for
   <EM>every</EM> server (hostname) you want to maintain on your server.
   
  +<P>In Apache 1.3.13 and later you can specify the
  +<CODE>NameVirtualHost</CODE> IP address as the wildcard <CODE>*</CODE>
  +which matches any IP address not covered by more specific virtual
  +host directive(s). This is useful for configuring a server whose IP
  +address you do not know in advance, e.g. because it has a dynamically
  +configured IP address or because it is part of a load-balanced cluster
  +in which every machine shares the same configuration file.
  +</P>
  +
   <P>Additionally, many servers may wish to be accessible by more than
   one name. For example, the example server might want to be accessible
   as <CODE>domain.tld</CODE>, or <CODE>www2.domain.tld</CODE>, assuming
  
  
  
  1.1573    +6 -0      apache-1.3/src/CHANGES
  
  Index: CHANGES
  ===================================================================
  RCS file: /home/cvs/apache-1.3/src/CHANGES,v
  retrieving revision 1.1572
  retrieving revision 1.1573
  diff -u -u -r1.1572 -r1.1573
  --- CHANGES	2000/09/12 15:16:18	1.1572
  +++ CHANGES	2000/09/12 16:32:36	1.1573
  @@ -1,5 +1,11 @@
   Changes with Apache 1.3.13
   
  +  *) NameVirtualHost can now take "*" as an argument instead of
  +     an IP address. This allows you to create a purely name-based
  +     virtual hosting server that does not have any IP addresses in
  +     the configuration file and which ignores the local address
  +     of any connections. PR #5595, PR #4455 [Tony Finch]
  +
     *) Fix processing/merging of Remove* MIME directives.
        PR #5597 [Sander van Zoest <sander@covalent.net>]
   
  
  
  
  1.19      +140 -112  apache-1.3/src/main/http_vhost.c
  
  Index: http_vhost.c
  ===================================================================
  RCS file: /home/cvs/apache-1.3/src/main/http_vhost.c,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -u -r1.18 -r1.19
  --- http_vhost.c	2000/01/16 20:59:59	1.18
  +++ http_vhost.c	2000/09/12 16:32:38	1.19
  @@ -409,64 +409,122 @@
       return NULL;
   }
   
  +static void dump_a_vhost(FILE *f, ipaddr_chain *ic)
  +{
  +    name_chain *nc;
  +    int len;
  +    char buf[MAX_STRING_LEN];
  +
  +    if (ic->sar->host_addr.s_addr == DEFAULT_VHOST_ADDR) {
  +	len = ap_snprintf(buf, sizeof(buf), "_default_:%u",
  +		ic->sar->host_port);
  +    }
  +    else if (ic->sar->host_addr.s_addr == INADDR_ANY) {
  +	len = ap_snprintf(buf, sizeof(buf), "*:%u",
  +		ic->sar->host_port);
  +    }
  +    else {
  +	len = ap_snprintf(buf, sizeof(buf), "%pA:%u",
  +		&ic->sar->host_addr, ic->sar->host_port);
  +    }
  +    if (ic->sar->host_port == 0) {
  +	buf[len-1] = '*';
  +    }
  +    if (ic->names == NULL) {
  +	fprintf(f, "%-22s %s (%s:%u)\n", buf, ic->server->server_hostname,
  +		ic->server->defn_name, ic->server->defn_line_number);
  +	return;
  +    }
  +    fprintf(f, "%-22s is a NameVirtualHost\n"
  +	    "%22s default server %s (%s:%u)\n",
  +	    buf, "", ic->server->server_hostname,
  +	    ic->server->defn_name, ic->server->defn_line_number);
  +    for (nc = ic->names; nc; nc = nc->next) {
  +	if (nc->sar->host_port) {
  +	    fprintf(f, "%22s port %u ", "", nc->sar->host_port);
  +	}
  +	else {
  +	    fprintf(f, "%22s port * ", "");
  +	}
  +	fprintf(f, "namevhost %s (%s:%u)\n", nc->server->server_hostname,
  +		nc->server->defn_name, nc->server->defn_line_number);
  +    }
  +}
   
   static void dump_vhost_config(FILE *f)
   {
  -    int i;
       ipaddr_chain *ic;
  -    name_chain *nc;
  -    char buf[MAX_STRING_LEN];
  +    int i;
   
       fprintf(f, "VirtualHost configuration:\n");
       for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
   	for (ic = iphash_table[i]; ic; ic = ic->next) {
  -	    if (ic->sar->host_port == 0) {
  -		ap_snprintf(buf, sizeof(buf), "%pA:*", &ic->sar->host_addr);
  -	    }
  -	    else {
  -		ap_snprintf(buf, sizeof(buf), "%pA:%u", &ic->sar->host_addr,
  -		    ic->sar->host_port);
  -	    }
  -	    if (ic->names == NULL) {
  -		fprintf(f, "%-22s %s (%s:%u)\n", buf,
  -		    ic->server->server_hostname, ic->server->defn_name,
  -		    ic->server->defn_line_number);
  -		continue;
  -	    }
  -	    fprintf(f, "%-22s is a NameVirtualHost\n"
  -	               "%22s default server %s (%s:%u)\n",
  -		       buf, "", ic->server->server_hostname,
  -		       ic->server->defn_name, ic->server->defn_line_number);
  -	    for (nc = ic->names; nc; nc = nc->next) {
  -		if (nc->sar->host_port) {
  -		    fprintf(f, "%22s port %u ", "", nc->sar->host_port);
  -		}
  -		else {
  -		    fprintf(f, "%22s port * ", "");
  -		}
  -		fprintf(f, "namevhost %s (%s:%u)\n",
  -			nc->server->server_hostname,
  -			nc->server->defn_name,
  -			nc->server->defn_line_number);
  -	    }
  +	    dump_a_vhost(f, ic);
   	}
       }
       if (default_list) {
  -	fprintf(f, "_default_ servers:\n");
  +	fprintf(f, "wildcard NameVirtualHosts and _default_ servers:\n");
   	for (ic = default_list; ic; ic = ic->next) {
  -	    if (ic->sar->host_port == 0) {
  -		fprintf(f, "port * ");
  -	    }
  -	    else {
  -		fprintf(f, "port %u ", ic->sar->host_port);
  -	    }
  -	    fprintf(f, "server %s (%s:%u)\n",
  -		ic->server->server_hostname, ic->server->defn_name,
  -		ic->server->defn_line_number);
  +	    dump_a_vhost(f, ic);
   	}
       }
   }
   
  +/*
  + * Helper functions for ap_fini_vhost_config()
  + */
  +static int add_name_vhost_config(pool *p, server_rec *main_s, server_rec *s,
  +				 server_addr_rec *sar, ipaddr_chain *ic)
  +{
  +    /* the first time we encounter a NameVirtualHost address
  +     * ic->server will be NULL, on subsequent encounters
  +     * ic->names will be non-NULL.
  +     */
  +    if (ic->names || ic->server == NULL) {
  +	name_chain *nc = new_name_chain(p, s, sar);
  +	nc->next = ic->names;
  +	ic->names = nc;
  +	ic->server = s;
  +	if (sar->host_port != ic->sar->host_port) {
  +	    /* one of the two is a * port, the other isn't */
  +	    ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, main_s,
  +		    "VirtualHost %s:%u -- mixing * "
  +		    "ports and non-* ports with "
  +		    "a NameVirtualHost address is not supported,"
  +		    " proceeding with undefined results",
  +		    sar->virthost, sar->host_port);
  +	}
  +	return 1;
  +    }
  +    else {
  +	/* IP-based vhosts are handled by the caller */
  +	return 0;
  +    }
  +}
  +
  +static void remove_unused_name_vhosts(server_rec *main_s, ipaddr_chain **pic)
  +{
  +    while (*pic) {
  +	ipaddr_chain *ic = *pic;
  +	
  +	if (ic->server == NULL) {
  +	    ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, main_s,
  +			 "NameVirtualHost %s:%u has no VirtualHosts",
  +			 ic->sar->virthost, ic->sar->host_port);
  +	    *pic = ic->next;
  +	}
  +	else if (ic->names == NULL) {
  +	    /* if server != NULL and names == NULL then we're done
  +	     * looking at NameVirtualHosts
  +	     */
  +	    break;
  +	}
  +	else {
  +	    pic = &ic->next;
  +	}
  +    }
  +}
  +
   /* compile the tables and such we need to do the run-time vhost lookups */
   void ap_fini_vhost_config(pool *p, server_rec *main_s)
   {
  @@ -497,13 +555,22 @@
        */
       for (sar = name_vhost_list; sar; sar = sar->next) {
   	unsigned bucket = hash_inaddr(sar->host_addr.s_addr);
  -	ipaddr_chain *new = new_ipaddr_chain(p, NULL, sar);
  +	ipaddr_chain *ic = new_ipaddr_chain(p, NULL, sar);
   
  -	*iphash_table_tail[bucket] = new;
  -	iphash_table_tail[bucket] = &new->next;
  -
  +	if (sar->host_addr.s_addr != INADDR_ANY) {
  +	    *iphash_table_tail[bucket] = ic;
  +	    iphash_table_tail[bucket] = &ic->next;
  +	}
  +	else {
  +	    /* A wildcard NameVirtualHost goes on the default_list so
  +	     * that it can catch incoming requests on any address.
  +	     */
  +	    ic->next = default_list;
  +	    default_list = ic;
  +	}
   	/* Notice that what we've done is insert an ipaddr_chain with
  -	 * both server and names NULL.  Remember that.
  +	 * both server and names NULL. This fact is used to spot name-
  +	 * based vhosts in add_name_vhost_config().
   	 */
       }
   
  @@ -520,46 +587,31 @@
   
   	    if (sar->host_addr.s_addr == DEFAULT_VHOST_ADDR
   		|| sar->host_addr.s_addr == INADDR_ANY) {
  -		/* add it to default bucket for each appropriate sar
  -		 * since we need to do a port test
  -		 */
  -		ipaddr_chain *other;
  -
  -		other = find_default_server(sar->host_port);
  -		if (other && other->sar->host_port != 0) {
  -		    ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, main_s,
  -			    "_default_ VirtualHost overlap on port %u,"
  +		ic = find_default_server(sar->host_port);
  +		if (!ic || !add_name_vhost_config(p, main_s, s, sar, ic)) {
  +		    if (ic && ic->sar->host_port != 0) {
  +			ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
  +			    main_s, "_default_ VirtualHost overlap on port %u,"
   			    " the first has precedence", sar->host_port);
  +		    }
  +		    ic = new_ipaddr_chain(p, s, sar);
  +		    ic->next = default_list;
  +		    default_list = ic;
   		}
   		has_default_vhost_addr = 1;
  -		ic = new_ipaddr_chain(p, s, sar);
  -		ic->next = default_list;
  -		default_list = ic;
   	    }
   	    else {
   		/* see if it matches something we've already got */
   		ic = find_ipaddr(&sar->host_addr, sar->host_port);
   
  -		/* the first time we encounter a NameVirtualHost address
  -		 * ic->server will be NULL, on subsequent encounters
  -		 * ic->names will be non-NULL.
  -		 */
  -		if (ic && (ic->names || ic->server == NULL)) {
  -		    name_chain *nc = new_name_chain(p, s, sar);
  -		    nc->next = ic->names;
  -		    ic->names = nc;
  -		    ic->server = s;
  -		    if (sar->host_port != ic->sar->host_port) {
  -			/* one of the two is a * port, the other isn't */
  -			ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, main_s,
  -				"VirtualHost %s:%u -- mixing * "
  -				"ports and non-* ports with "
  -				"a NameVirtualHost address is not supported,"
  -				" proceeding with undefined results",
  -				sar->virthost, sar->host_port);
  -		    }
  +		if (!ic) {
  +		    unsigned bucket = hash_inaddr(sar->host_addr.s_addr);
  +
  +		    ic = new_ipaddr_chain(p, s, sar);
  +		    ic->next = *iphash_table_tail[bucket];
  +		    *iphash_table_tail[bucket] = ic;
   		}
  -		else if (ic) {
  +		else if (!add_name_vhost_config(p, main_s, s, sar, ic)) {
   		    ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, main_s,
   			    "VirtualHost %s:%u overlaps with "
   			    "VirtualHost %s:%u, the first has precedence, "
  @@ -569,13 +621,6 @@
   		    ic->sar = sar;
   		    ic->server = s;
   		}
  -		else {
  -		    unsigned bucket = hash_inaddr(sar->host_addr.s_addr);
  -
  -		    ic = new_ipaddr_chain(p, s, sar);
  -		    ic->next = *iphash_table_tail[bucket];
  -		    *iphash_table_tail[bucket] = ic;
  -		}
   	    }
   	}
   
  @@ -621,28 +666,9 @@
        * hosts associated with them.  Lamers.
        */
       for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
  -	ipaddr_chain **pic = &iphash_table[i];
  -
  -	while (*pic) {
  -	    ipaddr_chain *ic = *pic;
  -
  -	    if (ic->server == NULL) {
  -		ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, main_s,
  -			"NameVirtualHost %s:%u has no VirtualHosts",
  -			ic->sar->virthost, ic->sar->host_port);
  -		*pic = ic->next;
  -	    }
  -	    else if (ic->names == NULL) {
  -		/* if server != NULL and names == NULL then we're done
  -		 * looking at NameVirtualHosts
  -		 */
  -		break;
  -	    }
  -	    else {
  -		pic = &ic->next;
  -	    }
  -	}
  +	remove_unused_name_vhosts(main_s, &iphash_table[i]);
       }
  +    remove_unused_name_vhosts(main_s, &default_list);
   
   #ifdef IPHASH_STATISTICS
       dump_iphash_statistics(main_s);
  @@ -933,16 +959,18 @@
   	return;
       }
   
  -    /* There's certainly no name-vhosts with this address, they would have
  -     * been matched above.
  +    /* maybe there's a default server or wildcard name-based vhost
  +     * matching this port
        */
  -    conn->vhost_lookup_data = NULL;
  -
  -    /* maybe there's a default server matching this port */
       trav = find_default_server(port);
       if (trav) {
  +	conn->vhost_lookup_data = trav->names;
   	conn->server = trav->server;
  +	return;
       }
   
  -    /* otherwise we're stuck with just the main server */
  +    /* otherwise we're stuck with just the main server
  +     * and no name-based vhosts
  +     */
  +    conn->vhost_lookup_data = NULL;
   }
  
  
  

Mime
View raw message