# Web client
[Applications]

## Detailed Description

This example shows a HTTP client that is able to download web pages and files from web servers.

It requires a number of callback functions to be implemented by the module that utilizes the code: webclient_datahandler(), webclient_connected(), webclient_timedout(), webclient_aborted(), webclient_closed().

## Files

file | webclient.h
| Header file for the HTTP client.

file | webclient.c
| Implementation of the HTTP client.

## Data Structures

struct | webclient_state

## Defines

#define | WEBCLIENT_CONF_MAX_URLLEN 100
#define | UIP_APPCALL webclient_appcall
#define | WEBCLIENT_TIMEOUT 100
#define | WEBCLIENT_STATE_STATUSLINE 0
#define | WEBCLIENT_STATE_HEADERS 1
#define | WEBCLIENT_STATE_DATA 2
#define | WEBCLIENT_STATE_CLOSE 3
#define | HTTPFLAG_NONE 0
#define | HTTPFLAG_OK 1
#define | HTTPFLAG_MOVED 2
#define | HTTPFLAG_ERROR 3
#define | ISO_nl 0x0a
#define | ISO_cr 0x0d
#define | ISO_space 0x20

## Typedefs

typedef webclient_state | uip_tcp_appstate_t

## Functions

void | webclient_datahandler (char *data, u16_t len)
| Callback function that is called from the webclient code when HTTP data has been received.
void | webclient_connected (void)
| Callback function that is called from the webclient code when the HTTP connection has been connected to the web server.
void | webclient_timedout (void)
| Callback function that is called from the webclient code if the HTTP connection to the web server has timed out.
void | webclient_aborted (void)
| Callback function that is called from the webclient code if the HTTP connection to the web server has been aborted by the web server.
void | webclient_closed (void)
| Callback function that is called from the webclient code when the HTTP connection to the web server has been closed.
void | webclient_init (void)
| Initialize the webclient module.
unsigned char | webclient_get (char *host, u16_t port, char *file)
| Open an HTTP connection to a web server and ask for a file using the GET method.
void | webclient_close (void)
| Close the currently open HTTP connection.
void | webclient_appcall (void)
char * | webclient_mimetype (void)
| Obtain the MIME type of the current HTTP data stream.
char * | webclient_filename (void)
| Obtain the filename of the current HTTP data stream.
char * | webclient_hostname (void)
| Obtain the hostname of the current HTTP data stream.
unsigned short | webclient_port (void)
| Obtain the port number of the current HTTP data stream.

## Function Documentation

void webclient_aborted | ( | void |  | ) |

Callback function that is called from the webclient code if the HTTP connection to the web server has been aborted by the web server.

This function must be implemented by the module that uses the webclient code.

Examples:
  webclient.c, and webclient.h.

Referenced by webclient_appcall().

void webclient_closed | ( | void |  | ) |

Callback function that is called from the webclient code when the HTTP connection to the web server has been closed.

This function must be implemented by the module that uses the webclient code.

Examples:
  webclient.c, and webclient.h.

Referenced by webclient_appcall().

void webclient_connected | ( | void |  | ) |

Callback function that is called from the webclient code when the HTTP connection has been connected to the web server.

This function must be implemented by the module that uses the webclient code.

Examples:
  webclient.c, and webclient.h.

Referenced by webclient_appcall().

void webclient_datahandler | ( | char * | data,
|  | u16_t | len
| ) |

Callback function that is called from the webclient code when HTTP data has been received.

This function must be implemented by the module that uses the webclient code. The function is called from the webclient module when HTTP data has been received. The function is not called when HTTP headers are received, only for the actual data.

Note:
  This function is called many times, repetedly, when data is being received, and not once when all data has been received.

Parameters:

  | data | A pointer to the data that has been received.
  | len | The length of the data that has been received.

Examples:
  webclient.c, and webclient.h.

Referenced by webclient_appcall().

char * webclient_filename | ( | void |  | ) |

Obtain the filename of the current HTTP data stream.

The filename of an HTTP request may be changed by the web server, and may therefore not be the same as when the original GET request was made with webclient_get(). This function is used for obtaining the current filename.

Returns:
  A pointer to the current filename.

Examples:
  webclient.c, and webclient.h.

Definition at line 93 of file webclient.c.

References webclient_state::file.

unsigned char webclient_get | ( | char * | host,
|  | u16_t | port,
|  | char * | file
| ) |

Open an HTTP connection to a web server and ask for a file using the GET method.

This function opens an HTTP connection to the specified web server and requests the specified file using the GET method. When the HTTP connection has been connected, the webclient_connected() callback function is called and when the HTTP data arrives the webclient_datahandler() callback function is called.

The callback function webclient_timedout() is called if the web server could not be contacted, and the webclient_aborted() callback function is called if the HTTP connection is aborted by the web server.

When the HTTP request has been completed and the HTTP connection is closed, the webclient_closed() callback function will be called.

Note:
  If the function is passed a host name, it must already be in the resolver cache in order for the function to connect to the web server. It is therefore up to the calling module to implement the resolver calls and the signal handler used for reporting a resolv query answer.

Parameters:

  | host | A pointer to a string containing either a host name or a numerical IP address in dotted decimal notation (e.g., 192.168.23.1).
  | port | The port number to which to connect, in host byte order.
  | file | A pointer to the name of the file to get.

Return values:

  | 0 | if the host name could not be found in the cache, or if a TCP connection could not be created.
  | 1 | if the connection was initiated.

Examples:
  webclient.c, and webclient.h.

Definition at line 140 of file webclient.c.

References webclient_state::file, webclient_state::host, htons(), NULL, webclient_state::port, resolv_lookup(), and uip_connect().

Referenced by webclient_appcall().

char * webclient_hostname | ( | void |  | ) |

Obtain the hostname of the current HTTP data stream.

The hostname of the web server of an HTTP request may be changed by the web server, and may therefore not be the same as when the original GET request was made with webclient_get(). This function is used for obtaining the current hostname.

Returns:
  A pointer to the current hostname.

Examples:
  webclient.c, and webclient.h.

Definition at line 99 of file webclient.c.

References webclient_state::host.

char * webclient_mimetype | ( | void |  | ) |

Obtain the MIME type of the current HTTP data stream.

Returns:
  A pointer to a string contaning the MIME type. The string may be empty if no MIME type was reported by the web server.

Examples:
  webclient.c, and webclient.h.

Definition at line 87 of file webclient.c.

References webclient_state::mimetype.

unsigned short webclient_port | ( | void |  | ) |

Obtain the port number of the current HTTP data stream.

The port number of an HTTP request may be changed by the web server, and may therefore not be the same as when the original GET request was made with webclient_get(). This function is used for obtaining the current port number.

Returns:
  The port number of the current HTTP data stream, in host byte order.

Examples:
  webclient.c, and webclient.h.

Definition at line 105 of file webclient.c.

References webclient_state::port.

void webclient_timedout | ( | void |  | ) |

Callback function that is called from the webclient code if the HTTP connection to the web server has timed out.

This function must be implemented by the module that uses the webclient code.

Examples:
  webclient.c, and webclient.h.

Referenced by webclient_appcall().