/*
 * $Log: proxy,v $
 * Revision 1.10  1998/07/01 21:10:58  joseph
 * Added status window
 * Fixed persistent connections
 * Fixed magtags to only count processed url references
 * Fixed proxy_forunqualified, again.
 * Fixed leading/trailing spaces in urls...
 *
 * Revision 1.9  1998/06/16 18:29:38  joseph
 * Added recalculation of fetch portion for base href & redirections
 * Added processing of base href in fetch and rewrite
 * Moved file_truncate from rewrite to file
 * Logging of urls with unknown schemes
 * Errorlog file kept open across whole fetch
 * Log routines may handle effects of *close better.
 * Invalid chars in urls and hostnames replaced with ~'s on filesystem
 * Rewrite recovers from zero length log file
 * Removed permfail, softfail states as not used
 * Removed some unused bits from url structures
 * Changed proxy code to handle dotless hostnames okay.
 * Version to 0.06a.
 *
 * Revision 1.8  1998/06/14 21:24:37  joseph
 * Moved relative url resolver from rewrite / http -> misc and rewrote
 * Fixed rewrite leaving []'s all over log file.
 * Fixed misc_urltofilename barfing on url's like http://wiggle
 * Lessened processor time when fetching pages with _lots_ of links in them.
 * Make FetchError open/close at start/end of fetch like FetchLog
 * Fixed handling of ftp urls, with & without proxy set.
 * Fixed bugs in code to set fetchportion
 * URLs now moved to fetch list when fetched (whether sucessful or not)
 * Log file now says '[http]' or '[ftp]' for real link, as appropriate.
 * Recalculates fetch portion for redirected urls
 * Fixed handling of proxy for ftp (wasn't always using proxy)
 * url.c now uses 'times 2' allocator / deallocator.
 * url_handled corrected for ftp (returns true only if proxy set)
 * closes log file when fetch aborted.
 *
 * Revision 1.7  1998/06/08 16:40:27  joseph
 * Improved debug routines, removed/inserted some debugging
 *
 * Revision 1.6  1998/06/07 19:41:32  joseph
 * Fixed up proxy code so it actually works
 * Improved error handling
 * If a fetch is repeatedly aborted by the server during a download, we
 * now keep the resulting file and mark it as incomplete in the logfile.
 *
 * Revision 1.5  1998/05/28 19:41:11  joseph
 * Changed all printf()'s to debug_printf(())'s
 *
 * Revision 1.4  1998/04/05 11:26:18  jogu
 * Changed to use values read from configuration file
 *
 * Revision 1.3  1998/03/21 20:32:16  jogu
 * Changed proxy to wwwoffle
 *
 * Revision 1.2  1997/12/29 18:40:48  jogu
 * Fixed header file inclusion for NFS compile
 * Fixed <a name="xxx" href="tttt"> not getting picked up
 *   Added my_strcasestr, a case insensitive version of strstr
 *
 * Revision 1.1.1.1  1997/12/29 14:37:55  jogu
 * WebGet Initial CMS Ver
 *
 */


#include <stdio.h>
#include <string.h>

#include "config.h"
#include "my_string.h"
#include "debug.h"

#include "proxy.h"

/* Values read from the config file */
#define proxy_name config_proxyname
#define proxy_notfor config_proxynotfor
#define proxy_forunqualified config_proxyforunqualified

static int proxy_noproxy( char *host )
{
  if ( !strchr(host,'.') && proxy_forunqualified ) return 1; /* don't proxy - must be local*/

  if ( *proxy_notfor )
  {
    /* there are hosts we don't use a proxy for -> check against our hostname */
    int len = strlen(host);
    char *end, *ptr = proxy_notfor;

    do
    {
      end = strchr( ptr,',' );
      if ( !end ) end = ptr + strlen(ptr);
      /* end -> ',' or '\0' */

      if ( len >= (end-ptr) )
      {
        int noproxylen = end - ptr;
        /* could match - host length >= no proxy entry length */
        /* calculate position of start of last x chars of host, where x=len of this noproxy match */
        char *cmp = host + len - noproxylen;
        debug_printf(("comparing %s to %s\n",cmp,ptr));

        if ( my_strncasecmp( cmp, ptr, end-ptr ) == 0 )
          return 1; /* no proxy */
      }
      ptr=end+1;
    }
    while (*end); /* loop until we hit the null */
  }
  return 0;
}

/* returns true or false. */
int proxy_usefor( char *url, char *host )
{
  int ret;
  /* all addresses not containing .'s are local */

//  debug_printf(( "Picking proxy for %s, %s. noproxy = '%s'",url,host,proxy_notfor ));

  if ( my_strncasecmp( url, "http:", 5 ) == 0 )
    ret = proxy_noproxy(host) ? 0/*no proxy*/ : 1/*http proxy*/;
  else if ( my_strncasecmp( url, "ftp:", 4 ) == 0 )
    ret = 2; /* ftp proxy */
  else
    ret = 3; /* unknown proxy */

  /* okay, we want a proxy, decide if we want http or ftp one */

  if ( ret == 0 || *proxy_name[ret-1] )
    return ret; /* there is a proxy for this url type, so return it's index */

  return 0; /* no proxy set */
}

/*
static int main(void)
{
  char host[100];
  gets(host);
  debug_printf(("%s\n", proxy_usefor(host) ? "proxy" : "don't proxy"));
  return 1;
}
*/
