/*
 * $Log: fetchstruc,v $
 * Revision 1.10  2001/03/11 17:08:05  joseph
 * Changed to use new dns abstraction code.
 *
 * Revision 1.9  1998/09/02 19:21:24  joseph
 * Replaced internal mimemap stuff with calls to MimeMap module
 *
 * Revision 1.8  1998/08/30 18:31:51  joseph
 * Fetching of inline images may be specified more precisely
 *
 * Revision 1.7  1998/08/30 13:10:00  joseph
 * Fetching ordered is now more defined - pages closer to the root, and
 * actual pages (as opposed to inlines) at the same line level are favoured.
 *
 * Revision 1.6  1998/08/09 15:07:02  joseph
 * Added include/exclude lists for URLs
 *
 * Revision 1.5  1998/06/16 18:29:40  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.4  1998/06/06 21:45:02  joseph
 * Added ability to not fetch frames / inline images in fetch / config
 * wimpc_menuread moved to wimpclib
 * Now fades individual fetches when started, unfades all at end of fetch
 *
 * Revision 1.3  1998/03/25 17:59:30  jogu
 * Added fetchportion, made linkdepth a 16 bit quantity
 *
 * Revision 1.2  1998/03/21 20:28:51  jogu
 * Added linkdepth and date
 *
 * Revision 1.1.1.1  1997/12/29 14:37:55  jogu
 * WebGet Initial CMS Ver
 *
 */

#ifndef STRUCT_H
#define STRUCT_H

#include "defines.h"

#include <stdio.h> /* FILE */
#include <time.h> /* time_t */
#include "dnslib.h" /* dnsquery definition */
#include "url.h" /* URL_LISTS */

typedef enum
{
  RESOLVE,
  CONNECT,
  REQUESTSEND,
  RESPONSE,
  HEADER,
  CHUNK,
  CHUNKFOOT,
  BODY
} httpfetch_s;

typedef enum
{
  UNKNOWN,
  CHUNKED,
  CONTENTLENGTH
} http_encoding;

typedef struct httpfetch_struct
{
  /* these contain transient information */
  unsigned int     idle:1; /* needed so we can keep the connection persistent. */
  unsigned int	   poll:1; /* true if this session needs polling other than on select() - eg. dns, sending request */
  unsigned int	   parse:1; /* true if we should parse the output for further links */
  unsigned int     baseurl:1; /* true if there is a 'baseurl' after the url */
  unsigned int	   retries:4; /* Number of retries left. 0-15 */

  /* these contain information that is preserved across <x> */
  unsigned int	   proxy:3;	 /* proxy to use. 0=unknown, 1=none,  2,3...7 = proxy[0-5] */
/*  unsigned int	   mimetype:8;*/	 /* as in mimetype_str, url. 0 = none/unknown */
  unsigned int     noinlineimages:1; /* fetch inline images? */
  unsigned int     inlineframes:1; /* fetch inline frames? */
  unsigned int     linkdepthtogo:16; /* as in url_str. */
  unsigned int     act_linkdepth:16; /* as in url_str. */
  unsigned int     fetchportion:16; /* how many characters of this url any links found must
                                       match in order to get fetched. 0 matches all url's
                                       (to match no urls, set linkdepth to 0) */
  unsigned int     imagefetchportion:16; /* how many characters of this url any links found must
                                       match in order to get fetched. 0 matches all url's
                                       (to match no urls, set linkdepth to 0) */

  int              filetype;
  int              ruleset;

  time_t           date;
  int              sock;
  dns_t            *dnsquery;
  URL_LISTS	   where; /* which list is our url in, so we can find it again? */
  char		   host[HOST_MAXLEN]; /* the host we are connected or *connecting* to */
  char		   buffstart[SOCKBUFF_MAXLEN]; /* ptr to first in use location in 1k 'buff' for incoming data */
  char		   tagbuff[1024]; /* temp buffer to hold parts of tags we've recv'd */
  int		   bufflen; /* amount of data in incoming buffer */
  int		   length; /* expected length of doc/chunk */
  int		   rx_bytes; /* bytes recv'd in this chunk or body */
  http_encoding	   encoding; /* encoding type being used. */
  httpfetch_s	   state;
  int		   respcode; /* Holds http response code till we need it */
  FILE		   *html; /* output file (socket -> html in x-file) */
} httpfetch_t;
/* url sits here, after session */
/* optionality, <base href="x"> url sits here */

#if 0
typedef struct httpfetch_struct
{
  char		   conn_host[HOST_MAXLEN]; /* where we are connected */
  int		   socket;
  char		   filename[FILENAME_MAXLEN];
  int		   (*state_func)(struct session_struct *, int);
  unsigned int     newdata:1; /* true if unprocessed data in buffer */
  unsigned int     datataken:1;
  unsigned int	   start:1;

  unsigned int     length;
  unsigned int	   rx_bytes;
  enum {UNKNOWN,LEN,CHUNKED} encoding;
  enum {NONE,RESPONSE,HEADER,CHUNK} substate;

  char		   sockbuff[1024];
  char		   *sockptr; /* offset into sockbuff of new data */
  int		   bufflen; /* amount of new data */

  /* 'belong' to load_url */
  int		   url_num;
  char		   url[URL_MAXLEN];
  char		   *path; /* ptr into url */
  char             hostname[HOST_MAXLEN]; /* name of host whose page we are fetching */
  int		   port; /* port we are connected on */
  char		   *proxy; /* null -> fetch direct, host -> fetch using host for proxy */
  char		   request[REQUEST_MAXLEN];
  dnsquery         *dnsquery;

} httpfetch_t;
#endif

#endif
