# Libwww Ready to go Samples

Initialization | GET | HEAD | PUT | POST | TRACE | OPTIONS | HTML Parsing | XML/RDF Parsing | Serving Requests | Eventloop | Big sample apps

This is the list of small examples using the very basic features of the Library. If you have not already compiled the library then please read the Installation guide before you continue. If you want more details of how to use the Library then please read the User's Guide.

In the example we assume that the compiler knows where to look for libwww include files and the binary library itself. If you are using the GNU autoconf configure script then the examples are built as part of the overall package. On other platforms, you must provide the make file on your own.

## Initialization Samples

Start and Stop the Library
  This is a dummy program that just initializes as terminates the Library
Default Initialization of libwww
  This example shows how to set up all the default handlers, parsers, converters, etc. using the WWWInit interface.
Specialized Initialization of libwww
  This example show how to register a protocol module and a stream converter
Timezone sampler
  Figures out what timezone you are in. This is important for libwww to know as the persistent cache and a lot of other things depends on it.

## Client Requests using libwww

These sample programs are all using libwww as a client side library which is what comes most natural to libwww.

### Samples using GET Requests

Load a document and headers into memory
  A simple example of how you can create a memory buffer containing the contents of the URL and all the protocol headers.
Load a document without headers into memory
  This program shows how to load a URL into memory without getting the header information.
Load a document and count the number of bytes in the body
  This is an example of how you can add streams and then get the desired result. The example shows how to add a content length counter stream and get the result back.
Load a document and dump to local File
  We here show how to fetch a document using HTTP and to save it to local file. You must specify a local file name on the command line. There are two versions of this program: The preemptive version with no event loop and the non-preemptive version for integration with Windows' event and other event loops.
Load the same document `n` times using blocking sockets
  You can use this (but be careful) to beat up a server with the same document as fast as possible.
Load the same document `n` times using non-blocking sockets
  Be careful! Also remember that only `GET` and `HEAD` requests can be pipelined in HTTP - not `POST`, for example.
Range Requests
  Range requests can be used to ask for a part or for several parts of a document from an HTTP server without asking for the whole thing. The output is sent directly to stdout. Run the program without parameters for instructions.
Simple cookie usage example
  Not that I like cookies - but this is a simple sample app showing how to use cookies with the HTTP Cookie module.

### Samples using HEAD Requests

Get the header fields of a response
  Prints out and potentially filters the response header fields you are interested
Issue a HEAD request
  This small example sets up a request and performs a HEAD method on the resource provided on the command line
Issue a HEAD request and check for redirections
  A small program that sends a request out and checks whether is was redirected. If so then print out the redirected address

### Samples using PUT Requests

Simple PUT program
  Lets you save a document to an HTTP server. Note that both input and output is a Web address (URI) so you can save a document from anywhere to anywhere.
Save data from memory to a remote server
  This sample shows you how to use temporary anchors and how to save data from memory using PUT

### Samples using POST Requests

Post an HTML Form
  Use this example to POST HTML form data to a server
Post data from memory to a remote server
  This sample shows you how to use temporary anchors and how to POST data from memory

### Samples using TRACE Requests

Send a TRACE request to a server
  Allows to include the max number of hops (proxies) the request must pass

### Samples using OPTIONS Requests

Send an OPTIONS request to a server - this is yet an exercise for the user!
  It should allow support for sending `OPTIONS` on a * URI (although I really don't like that!)

## HTML Parsing

Three small sample apps that show how to use the new HText interface and the libwww HTML parser.

showlinks.c
  Parses a document and writes out all the links that it found to stdout
showtags.c
  Parses a document and writes out all the HTML tags found to stdout
showtext.c
  Parses a document and writes out all the "plain" text - that is what's between the HTML markup
tiny.c
  Is a very rudimentary HTTP/HTML browser with no layout engine and no fuzz. It's main feature is that it is small (<200K when stripped on linux).

## XML/RDF Parsing

Here are some sample apps that show how to use libwww together with James Clark's expat XML parser and John Punin's integration of the SiRPAC RDF parser in libwww. See the XML module for more information

showxml.c
  A simple program that shows all XML start and end tags and nothing else
stri.c
  This sample app uses the RDF parser to generate a set of triples and send them to stdout.
ptri.c
  A sample application using the RDF parser more directly as we control where the triples are sent ourselves

## Server Requests using libwww

Although libwww is primarily for clients, it is in fact symmetric in that it can handle both client requests and server requests. The way this is handled is that each protocol is registered with both a client handler and a server handler - depending on which type of request you use, one of them is called. Note that in order to be able to serve any document, there actually have to be a server handler. Currently, libwww only comes with a raw socket loader which isn't much of a server.

Listen on a socket
  This sample program opens a raw socket and listens on that port. Anything arriving is forwarded asis to stdout

## Using the Eventloop

Libwww is designed using a "coke machine model", in other words using asynchronous requests for most of its operations. For `GET` and `HEAD` requests you can also use synchronous requests, but they are not as flexible and requires more care.

Simple eventloop
  Keeps loading the same file whenever a user enters something. Stop the application by hitting "`q`"

## Bigger Sample Applications

You can also have a look at the more complete sample applications which are capable of doing more interesting things. If you have any example code that you would like to share with us then please send us a mail so that we can add it to this page.