W3C home > Mailing lists > Public > www-lib@w3.org > January to March 2000

RE: PostFormAnchorToChunk function??? (Emergency)

From: Desrochers, Gary <Gary.Desrochers@fmr.com>
Date: Mon, 7 Feb 2000 09:44:03 -0500
Message-ID: <6146FA7F7068D21184DD00A0C9B57D4001A210AB@msgmmk530nts.fmr.com>
To: "'Sofiane Cherchalli'" <abou_sofiane@hotmail.com>, www-lib@w3.org
Good morning,

I don't know if these will help but I made these so that I did not need to
use the PostFormAnchorToChunk function in order to send an XML request.

I will, hopefully when I find all of the needed fixes, submit these and any
other fixes (including leak fixes), but you said this was an emergency.

Sorry if these do not help.  I tried.

/* These routines were made because they were not in libwww and they 
   seemed like they should have been.  I could not modify the library and
these
   are not submitted yet.  Here they are. */
/*
 I use these like (from memory sorry if it doesn't compile.:-)):

    HTParentAnchor *src = NULL;
    char *tmpdata = (char *)malloc(datalength+1);
    HTChunk*aChunk = NULL;

    src = HTAppTmpAnchor(NULL);
    strcpy(tmpdata, data);
    HTAnchor_setDocument(src, tmpdata);
    HTAnchor_setLength(src, datalength);
    aChunk = HTPostAnchorToChunk(src, anchor, request);
    if(aChunk == NULL)
    {
       HTAnchor_setDocument(src, NULL);
       free(tmpdata);
       HTAnchor_removeSource(anchor, src);
       return NULL;
    }
    
    /* Call event list loop if not persistent. If persistent then it is even
easier. */
    /* You only need to free and such once. */
    HTAnchor_setDocument(src, NULL);
    free(tmpdata);
    HTAnchor_removeSource(anchor, src);
    return aChunk;
*/

/*
**	Post and then get data back from URL to a mem buffer
**	--------------------------
**	Load a request and store the result in a memory buffer.
**	Returns chunk if OK - else NULL
*/
PUBLIC HTChunk *HTPostAnchorToChunk(HTParentAnchor *source, HTAnchor
*destination,
        HTRequest * request)
{
    HTChunk *aChunk = NULL;

    HTStream *target = HTStreamToChunk(request, &aChunk, NULL);
    HTRequest_setOutputStream(request, target);

    // Request a document referenced by an anchor.
    // Put the information into the anchor.
    if(HTPostAnchor(source, destination, request) == NO)
    {
        HTChunk_delete(aChunk);
        aChunk = NULL;
    }

    return aChunk;
}

/*
 * This was made because HTTmpAnchor leaks an anchor for each request.
 * At the time I could not change the code so I made my own.
 * To use this when you do not want the users to be prompted whether or not
 * they want to overwrite the old file, you need to not call the default
 * HTAlertInit() or use HTAlert_setInteractive(NO).
 * If you don't want the user to be prompted for other stuff you may be
 * doing this already.
 */
PUBLIC HTParentAnchor * HTAppTmpAnchor (HTUserProfile * up)
{
    HTParentAnchor *retPAnchor = NULL;
    char tmpfile[PATH_MAX+1];
    char pidval[64];
    strcpy(tmpfile,"/tmp/postfile.");
    // This is not thread safe.  Maybe add thread id as well?
    sprintf(pidval,"%d", getpid()); 
    strcat(tmpfile,pidval);
    char * tmpurl = HTParse(tmpfile, "file:", PARSE_ALL);
    if (tmpurl)
    {
        HTTRACE(APP_TRACE, "Tmp Anchor.. With location `%s\'\n" _ tmpurl);
        retPAnchor = HTAnchor_parent(HTAnchor_findAddress(tmpurl));
        /* Fall Through */
    }

    HT_FREE(tmpurl);
    return retPAnchor;
}

/*
  Sources are leaked and there is no way to clean them up?
*/
PUBLIC void HTAnchor_removeSource (HTParentAnchor* me, HTAnchor* src)
{
    if(!me || !src)
        return;

    HTList *linkList = me->sources;

    if(linkList)
        HTList_removeObject(linkList, src);

    return;
}

-----Original Message-----
From: Sofiane Cherchalli [mailto:abou_sofiane@hotmail.com]
Sent: Monday, February 07, 2000 8:29 AM
To: www-lib@w3.org
Subject: PostFormAnchorToChunk function??? (Emergency)


Hi, I'm having a big problem using the PostFormAnchorToChunk function. I 
wrote a simple library which is based on libwww library to transmit xml 
message using HTTP post form between a unix (solaris 2.7) client (linked 
with libwww) and Netscape Server 4.0 running on windows NT. I configured 
Netscape server to run perl scripts as cgi shell. I wrote a very simple 
script in perl 'hello.pl' wich prints a "Hello World" message. I tested this

script on a client browser (netscape navigator). It works well.


The binary sample name that I wrote which is based on my library is 
'TestXMLRequest'. The command line is :

TestXMLRequest http://myserver/cgi/cgi-shell/hello.pl "xml message" 80

where 80 is the http port number.

When I run the program TestXMLRequest I get the 'HTTP/1.1 500 Server Error'

The wierd thing is that if I run the program refering to an html file 
instead of my perl file it returns me the content of the html file:

TestXMLRequest http://myserver/docroot/index.html "xml message" 80

It seems also that in both cases the form parameters are not transmited

Please could anyone one help me cuz I'm getting mad, this is an emergency :

Thanks in advance,

Sofiane CHERCHALLI




Here I include the source file of my library and the sample program :

########################## XMLRequest.h #############################

#ifndef __XMLREQUEST_H
#define __XMLREQUEST_H

#include "WWWLib.h"
#include "WWWInit.h"
#include "WWWHTTP.h"

typedef struct {
   char * uri_str;
   int port;
   int timeout;
   int status;
   char * xml_in_str;
   char * xml_out_str;

   HTRequest * request;
   HTAnchor * uri_anchor;
   HTAssocList * formfields;
   HTChunk * chunk;
} XMLRequest;

extern void XMLRequest_init (BOOL trace);
extern void XMLRequest_terminate ();
extern XMLRequest * XMLRequest_new (const char * uri_str, int port, int 
timeout, const char * xml_in_str);
extern void XMLRequest_post (XMLRequest * xmlr);
extern BOOL XMLRequest_delete(XMLRequest * xmlr);

#endif

#####################################################################

######################### XMLRequest.c ##############################

#include "XMLRequest.h"

#define MILLIES    1000


PRIVATE int printer (const char * fmt, va_list pArgs)
{
    return (vfprintf(stdout, fmt, pArgs));
}


PRIVATE int tracer (const char * fmt, va_list pArgs)
{
    return (vfprintf(stderr, fmt, pArgs));
}


PRIVATE int terminate_handler (HTRequest * request, HTResponse * response,
                               void * param, int status)
{
    XMLRequest * xmlr = (XMLRequest *) HTRequest_context(request);
    xmlr->status = status;
    HTPrint("STATUS = %d\n", status);

    /* Get the response content and set xml_out_str to point to it */
    if (status == HT_LOADED && xmlr->chunk && HTChunk_data(xmlr->chunk)) {
        xmlr->xml_out_str = HTChunk_data(xmlr->chunk);

    }

    HTEventList_stopLoop();
    return HT_OK;
}


PUBLIC void XMLRequest_init (BOOL trace)
{
    HTList * converters = HTList_new();
    HTList * transfert_encodings = HTList_new();

    if (!HTLib_isInitialized()) HTLibInit("XMLRequest-Library", "1.0");

    HTEventInit();

    HTAlertInit();

    HTTransport_add("buffered_tcp", HT_TP_SINGLE, HTReader_new, 
HTBufferWriter_new);

    HTProtocol_add("http", "buffered_tcp", HTTP_PORT, YES, HTLoadHTTP, 
NULL);

    HTNet_setMaxSocket(50);

    HTNetInit();
    HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);

    HTProxy_getEnvVar();

    HTConverterInit(converters);
    HTFormat_setConversion(converters);

    HTTransferEncoderInit(transfert_encodings);
    HTFormat_setTransferCoding(transfert_encodings);

    HTMIMEInit();

    HTFileInit();

    HTIconInit(NULL);

    /* Need our own trace and print functions */
    if (trace) {
        HTPrint_setCallback(printer);
        HTTrace_setCallback(tracer);
        HTSetTraceMessageMask("p");
    }
}


PUBLIC void XMLRequest_terminate ()
{
  HTEventTerminate();

  if (HTLib_isInitialized()) {
      HTFormat_deleteAll();

      HTLibTerminate();
  }
}


PUBLIC XMLRequest * XMLRequest_new (const char * uri_str, int port, int 
timeout, const char * xml_in_str)
{
    XMLRequest * xmlr;

    if ((xmlr = (XMLRequest *) HT_CALLOC(1, sizeof(XMLRequest))) == NULL)
        return NULL;

    if (uri_str && *uri_str && xml_in_str && *xml_in_str) {

        xmlr->uri_str = (char *)uri_str;
        xmlr->port = port;
        xmlr->timeout = timeout * MILLIES;
        xmlr->status = HT_ERROR;
        xmlr->xml_in_str = (char *)xml_in_str;

        xmlr->formfields = HTAssocList_new();
        HTAssocList_addObject(xmlr->formfields, "xmlmsg", xmlr->xml_in_str);

        xmlr->uri_anchor = HTAnchor_findAddress(uri_str);
        xmlr->chunk = NULL;

        xmlr->request = HTRequest_new();
        HTRequest_setOutputFormat(xmlr->request, WWW_SOURCE);
        HTRequest_setMaxRetry(5);
        HTRequest_setContext(xmlr->request, xmlr);
    }

    return xmlr;
}


PUBLIC BOOL XMLRequest_delete(XMLRequest * xmlr)
{
    if (xmlr) {
        HTRequest_delete(xmlr->request);

        if (xmlr->uri_anchor) {
            HTAnchor_delete((HTParentAnchor *)xmlr->uri_anchor);
            xmlr->uri_anchor = NULL;
        }

        if (xmlr->formfields) {
            HTAssocList_delete(xmlr->formfields);
            xmlr->formfields = NULL;
        }

        if (xmlr->chunk) {
            HTChunk_delete(xmlr->chunk);
            xmlr->chunk = NULL;
        }

        HT_FREE(xmlr);
        return YES;
    }
    return NO;
}


PUBLIC void XMLRequest_post (XMLRequest * xmlr)
{
    BOOL status = NO;

    HTProtocol_delete("http");
    HTProtocol_add("http", "buffered_tcp", xmlr->port, NO, HTLoadHTTP, 
NULL);

    /* Set a timeout for response */
    HTHost_setEventTimeout(xmlr->timeout);

    /* POST the source to the dest */
    xmlr->chunk = HTPostFormAnchorToChunk(xmlr->formfields, 
xmlr->uri_anchor, xmlr->request);

    /* Go into the event loop... */
    HTEventList_loop(xmlr->request);

    /* Only gets here if event loop fails */
    HTPrint("Timeout");
    xmlr->status = HT_ERROR;
}
####################################################################

############################ TestXMLRequest.c ######################

#include "XMLRequest.h"

int main(int argc, char **argv)
{
XMLRequest * xmlr = NULL;
char * uri_str = argv[1];
char * xml_in_str = argv[2];
int port = atoi(argv[3]);

XMLRequest_init(YES);

xmlr = XMLRequest_new(uri_str, port, 10000, xml_in_str);
XMLRequest_post(xmlr);
XMLRequest_delete(xmlr);
XMLRequest_terminate();
return 0;
}

##################################################################

########################################## Makefile ################

CC = c89
LIBWWW_INCLUDE = `libwww-config --cflags`
LIBWWW_LIBS = `libwww-config --libs`

all : libXMLRequest.so XMLRequest.o TestXMLRequest.o TestXMLRequest

libXMLRequest.so : XMLRequest.o
	c89 -G -h libXMLRequest.so -o libXMLRequest.so XMLRequest.o

# Create links to your local library
# ln -s ./libXMLRequest.so /usr/lib/libXMLRequest.so
# ln -s ./libXMLRequest.so /usr/lib/libXMLRequest.so.1

XMLRequest.o : XMLRequest.h XMLRequest.c
	$(CC) -KPIC -c $(LIBWWW_INCLUDE) XMLRequest.c

TestXMLRequest.o : TestXMLRequest.c
	$(CC) -c $(LIBWWW_INCLUDE) TestXMLRequest.c

TestXMLRequest : TestXMLRequest.o XMLRequest.o
	$(CC) $(LIBWWW_LIBS) -lXMLRequest -o TestXMLRequest TestXMLRequest.o

####################################################################
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com
Received on Monday, 7 February 2000 09:44:56 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 23 April 2007 18:18:35 GMT