/*		Handle a Retrieve request from a WWW client	HTRetrieve.c
**		===========================================
**
** Authors
**	CTB	Carl Barker, Brunell
**	DMX	Daniel Martin
**	TBL	Tim Berners-Lee, CERN, Geneva	timbl@info.cern.ch
**
** History:
**	29 Apr 91 (TBL)	Split from daemon.c
**	5 Sept 91 (DMX)	Added path simplification to prevent '..'ing to an 
**			uncorrect directory.
**			Added '\r' as space for telneting to socket.
**	10 Sep 91 (TBL)	Reject request and log if fails authorisation
**	26 Jan 92 (TBL) Added some of CTB's code for directory read.
**	 23 Ap 93 (TBL) keyword untangling passed to lower level
*/

/* (c) CERN WorldWideWeb project 1990,91. See Copyright.html for details */

#define USE_PLAINTEXT	/* Makes retrieval of postscript easier for now */
			/* but not good sgml */

#define BUFFER_SIZE 4096	/* Arbitrary size for efficiency */
#define INFINITY 512		/* file name length @@ FIXME */

#include "HTUtils.h"
#include "HTFormat.h"
#include "tcp.h"

#ifdef RULES			/* Use rules? */
#include "HTRules.h"
#endif
#include "HTParse.h"

#include "HTFile.h"
#include "HTDaemon.h"		/* calls back to HTTP daemon */
#include "HTMLGen.h"		/* For HTML generator */
#include "HTWriter.h"		/* For making streams to net and disk */

#include "HTAccess.h"

#include "HTOracle.h"
#include <string.h>
#include <strings.h>
 


extern int WWW_TraceFlag;	/* Control diagnostic output */
extern FILE * logfile;		/* Log file output */
/* extern char HTClientHost[16]; */	/* Client name to be output */
extern int HTWriteASCII PARAMS((int soc, char * s));	/* In HTDaemon.c */

/* PUBLIC FILE * logfile = 0;	*/ /* Log file if any */
extern char *HTClientHost;	/* Peer internet address */





/*	Override HTML presentation method
**	---------------------------------
**
**	The "presentation" of HTML in the case of a server is
**	the generation of HTML markup.   The presence of this
**	routine prevents any of the client-oriented presentation code
**	from being picked up from the library libwww.
*/
PUBLIC HTStructured* HTML_new ARGS3(
	HTParentAnchor *, 	anchor,
	HTFormat,		format_out,
	HTStream*,		stream)
{
    HTStream * markup = HTStreamStack(
    	WWW_HTML, format_out, stream, anchor);
    if (!markup) return NULL;
    
    return HTMLGenerator(markup);
}


/*	Dummy things in hypertext object @@@@ */

PUBLIC void HText_select() {}
PUBLIC void HText_selectAnchor() {}
PUBLIC void * HTMainAnchor = NULL;

extern int HTLoadOracle();
PUBLIC HTProtocol HTORACLE = { "oracle", HTLoadOracle, NULL};

/*	Retrieve a document
**	-------------------
*/
#ifdef __STDC__
int HTRetrieve(const char * arg,  int soc)
#else
int HTRetrieve(arg, soc)
    char *arg;
    int soc;
#endif
{
  char * arg2 = 0;	/* Simplified argument */
  char * keywords=strchr(arg, '?');
    
#ifdef NOSEARCH
  if (keywords) {
    *keywords++ = 0;		/* Chop keywords off */
    if (!*keywords) keywords = NULL;
    else {
      char *p;
      for (p=keywords; *p; p++) if (*p == REPL_BLANC) *p = ' ';
      /* Plusses to spaces */
      HTUnEscape(keywords);
    }
  }
  
  if (keywords) {
    if (TRACE) printf("HTHandle: can't perform search %s %s\n",
		arg,keywords);
    return HTLoadError(HTASCIIWriter(soc), 403,
		       "Sorry, this server does not perform searches.");
    /* It ought to, using an executable script */
  }
#endif

  StrAllocCopy(arg2, arg);
  HTSimplify(arg2);	/* Remove ".." etc  (DMX) */

  if(keywords){
    if(strstr(arg2,"oracle/")) {
      HTStream * client = HTASCIIWriter(soc);
      HTParentAnchor *me;
      
      me = HTAnchor_parent(HTAnchor_findAddress(arg2));
      me->protocol = &HTORACLE;
      HTLoadOracle(arg2,me,WWW_PRESENT,client);
      return HT_LOADED;
    }
  }

/*		Load the document into the client
*/      

  {
    HTStream * client = HTASCIIWriter(soc);

    HTLoadToStream(arg2, NO, client);
    free(arg2);
    return HT_LOADED;
  }

} /* Retrieve */