httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chuck Murcko <ch...@n2k.com>
Subject proxy all-purpose patch
Date Mon, 09 Dec 1996 07:31:56 GMT
I'd like to get a sanity check on the following patch for the HTTP proxy
handler. It's fairly ambitious:

1) fixes possible NULL pointer reference w/NoCache
2) fixes NoCache behavior when using ProxyRemote (ProxyRemote host would
     cache nothing if it was in the local domain, and the local domain
     was in the NoCache list)
3) Adds Host: header when not available
4) Some code cleanup and clarification

Actually, all this was related, and I attempted to avoid duplicating code
wherever possible. I also tried to opt for clarity over efficiency when the
difference in speed was minor.

1) and 2) were reported by Martin Kraemer, with patches. The function,
though not the form, of Martin's stuff is adhered to here.

*** proxy_http.c.orig	Mon Nov 25 10:21:22 1996
--- proxy_http.c	Mon Dec  9 01:58:23 1996
***************
*** 142,149 ****
  	     const char *proxyhost, int proxyport)
  {
      char *p;
!     const char *err, *host;
!     int port, i, sock, len;
      array_header *reqhdrs_arr, *resp_hdrs;
      table_entry *reqhdrs;
      struct sockaddr_in server;
--- 142,149 ----
  	     const char *proxyhost, int proxyport)
  {
      char *p;
!     const char *err, *desthost;
!     int i, sock, len;
      array_header *reqhdrs_arr, *resp_hdrs;
      table_entry *reqhdrs;
      struct sockaddr_in server;
***************
*** 152,157 ****
--- 152,159 ----
      char buffer[HUGE_STRING_LEN], inprotocol[9], outprotocol[9];
      pool *pool=r->pool;
      const long int zero=0L;
+     int destport = 0;
+     char *destportstr = NULL;
  
      void *sconf = r->server->module_config;
      proxy_server_conf *conf =
***************
*** 162,182 ****
      memset(&server, '\0', sizeof(server));
      server.sin_family = AF_INET;
  
!     if (proxyhost != NULL)
!     {
! 	server.sin_port = htons(proxyport);
! 	err = proxy_host2addr(proxyhost, &server.sin_addr);
! 	if (err != NULL) return DECLINED;  /* try another */
! 	host = proxyhost;
!     } else
      {
  	url += 7;  /* skip http:// */
! /* We break the URL into host, port, path-search */
! 	port = DEFAULT_PORT;
  	p = strchr(url, '/');
  	if (p == NULL)
  	{
! 	    host = pstrdup(pool, url);
  	    url = "/";
  	} else
  	{
--- 164,179 ----
      memset(&server, '\0', sizeof(server));
      server.sin_family = AF_INET;
  
! /* We break the URL into host, port, path-search */
! 
!     if ((desthost = table_get(r->headers_in, "Host:")) == NULL)
      {
  	url += 7;  /* skip http:// */
! 	destport = DEFAULT_PORT;
  	p = strchr(url, '/');
  	if (p == NULL)
  	{
! 	    desthost = pstrdup(pool, url);
  	    url = "/";
  	} else
  	{
***************
*** 184,200 ****
  	    memcpy(q, url, p-url);
  	    q[p-url] = '\0';
  	    url = p;
! 	    host = q;
  	}
  
! 	p = strchr(host, ':');
! 	if (p != NULL)
! 	{
! 	    *(p++) = '\0';
! 	    port = atoi(p);
! 	}
! 	server.sin_port = htons(port);
! 	err = proxy_host2addr(host, &server.sin_addr);
  	if (err != NULL) return proxyerror(r, err); /* give up */
      }
  
--- 181,208 ----
  	    memcpy(q, url, p-url);
  	    q[p-url] = '\0';
  	    url = p;
! 	    desthost = q;
  	}
+     }
  
!     p = strchr(desthost, ':');
!     if (p != NULL)
!     {
!         *(p++) = '\0';
!         destport = atoi(p);
!         destportstr = p;
!     }
! 
!     if (proxyhost != NULL)
!     {
! 	url = r->uri;			/* restore original URL */
! 	server.sin_port = htons(proxyport);
! 	err = proxy_host2addr(proxyhost, &server.sin_addr);
! 	if (err != NULL) return DECLINED;  /* try another */
!     } else
!     {
! 	server.sin_port = htons(destport);
! 	err = proxy_host2addr(desthost, &server.sin_addr);
  	if (err != NULL) return proxyerror(r, err); /* give up */
      }
  
***************
*** 213,231 ****
  	else return proxyerror(r, "Could not connect to remote machine");
      }
  
!     clear_connection(r->headers_in);   /* Strip connection-based headers */
  
      f = bcreate(pool, B_RDWR);
      bpushfd(f, sock, sock);
  
      hard_timeout ("proxy send", r);
      bvputs(f, r->method, " ", url, " HTTP/1.0\015\012", NULL);
  
      reqhdrs_arr = table_elts (r->headers_in);
      reqhdrs = (table_entry *)reqhdrs_arr->elts;
      for (i=0; i < reqhdrs_arr->nelts; i++)
      {
! 	if (reqhdrs[i].key == NULL || reqhdrs[i].val == NULL) continue;
  	bvputs(f, reqhdrs[i].key, ": ", reqhdrs[i].val, "\015\012", NULL);
      }
  
--- 221,246 ----
  	else return proxyerror(r, "Could not connect to remote machine");
      }
  
!     clear_connection(r->headers_in);	/* Strip connection-based headers */
  
      f = bcreate(pool, B_RDWR);
      bpushfd(f, sock, sock);
  
      hard_timeout ("proxy send", r);
      bvputs(f, r->method, " ", url, " HTTP/1.0\015\012", NULL);
+     bvputs(f, "Host: ", desthost, NULL);
+     if (destportstr && destport != DEFAULT_PORT)
+ 	bvputs(f, ":", destportstr, "\015\012", NULL);
+     else
+ 	bputs("\015\012", f);
  
      reqhdrs_arr = table_elts (r->headers_in);
      reqhdrs = (table_entry *)reqhdrs_arr->elts;
      for (i=0; i < reqhdrs_arr->nelts; i++)
      {
! 	if (reqhdrs[i].key == NULL || reqhdrs[i].val == NULL
! 	  || !strcmp(reqhdrs[i].key, "Host"))	/* already sent if there */
! 	    continue;
  	bvputs(f, reqhdrs[i].key, ": ", reqhdrs[i].val, "\015\012", NULL);
      }
  
***************
*** 308,315 ****
  /* check if NoCache directive on this host */
      for (i=0; i < conf->nocaches->nelts; i++)
      {
!         if (ent[i].name[0] == '*' || (ent[i].name != NULL &&
!           strstr(host, ent[i].name) != NULL))
  	    nocache = 1; 
      }
  
--- 323,330 ----
  /* check if NoCache directive on this host */
      for (i=0; i < conf->nocaches->nelts; i++)
      {
!         if ((ent[i].name != NULL && strstr(desthost, ent[i].name) != NULL)
! 	  || ent[i].name[0] == '*')
  	    nocache = 1; 
      }
  

chuck
Chuck Murcko	N2K Inc.	Wayne PA	chuck@telebase.com
And now, on a lighter note:
Famous last words:

Mime
View raw message