W3C home > Mailing lists > Public > www-lib@w3.org > July to September 2002

Re: htrequest - output

From: Richard Atterer <libwww@list02.atterer.net>
Date: Fri, 9 Aug 2002 12:26:59 +0200
To: www-lib@w3.org
Cc: Ralf <Ralf@ueberdruck.dyndns.org>
Message-ID: <20020809102659.GA2145@nenya.lan>

Hi Ralf,

On Fri, Aug 09, 2002 at 08:46:52AM +0200, Ralf wrote:
> i try to read a http stream but i want to handle the output on this
> stream by a function .. i dont want to write to stdout or a file ..
> 
> so, i read the docs.. but found nothing helpful :(

Read them again, and then again, then curse, fetch the libwww source
code, wade through it, then read the docs once more - and slowly
things will get a bit clearer... ;-)

The following may not be 100% correct, but will hopefully get you
going in the right direction. You first need to define your own
HTStream class (see /Library/src/HTStream.html and
/Library/User/Architecture/Streams.html):

  static const HTStreamClass downloadWriter = {
    "myDownloadWriter", flush, free, abort, putChar, putString, write
  };
  struct My_HTStream { const HTStreamClass *isa; }
  static const struct My_HTStream writer = { &downloadWriter };

flush, free etc. are function pointers, you must implement these
functions.

If your program runs >1 concurrent downloads, you may actually want
writer to point to a malloc()d object and store other
download-dependent information after the "isa" field. See e.g. how
HTFSave.c in the libwww source does this.

Next, prepare the request:

  request = HTRequest_new();
  HTRequest_setOutputFormat(request, WWW_SOURCE); // Raw data, no headers...
  HTRequest_setOutputStream(request, (HTStream*)&writer); // send to writer
  HTRequest_setAnchor(request, HTAnchor_findAddress("http://foo.com/");
  ...

BTW, if you want to store a pointer to extra information inside the
request, use HTRequest_setContext(). (It took /me/ a long time to find
that one!)

For the remaining general setup calls to libwww, see the LoadToFile.c
example. Generally, it is a good idea to set up a tracer with
HTTrace_setCallback() to see what's going on. Maybe you'll also want
to use HTSetTraceMessageMask() to get more extensive debugging info.

Once you've got basic HTTP downloads working, you may find that HTTP
redirects don't work; if that is the case, use this:

  HTList* converters = HTList_new();
  HTConverterInit(converters); // Register the default set of converters
  HTFormat_setConversion(converters); // Global converters for all requests

To get notified of errors, redirects etc, register an after filter
with HTNet_addAfter().

In general, libwww has a very steep learning curve. If you don't need
its advanced features (e.g. many concurrent downloads, HTTP pipelining
support, easy integration in GTK+ apps), libcurl may actually be
better for you.

Cheers,

  Richard

-- 
  __   _
  |_) /|  Richard Atterer     |  CS student at the Technische  |  GnuPG key:
  | \/¯|  http://atterer.net  |  Universität München, Germany  |  0x888354F7
  ¯ '` ¯
Received on Friday, 9 August 2002 07:30:05 GMT

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