friendev EtherDune TCP/IP library
Public Member Functions | Public Attributes | Protected Attributes | List of all members
HTTPClient Class Reference

Provides an easy way to query a web server. More...

#include <HTTPClient.h>

Inheritance diagram for HTTPClient:
TCPSocket Socket Stateful NetworkService ListItem ENC28J60

Public Member Functions

 HTTPClient ()
 
 ~HTTPClient ()
 
void request (const String &hostName, const String &resource, uint16_t port=80)
 Starts a new HTTP request. More...
 
virtual void onResponseReceived ()
 Called immediately after the first line is received and there is a status code, e.g. More...
 
virtual void onHeaderReceived (uint16_t len, const byte *data)
 Called once for each fragment of the header portion of the response More...
 
virtual void onBodyReceived (uint16_t len, const byte *data)
 Called once for each fragment of the body that is received. More...
 
virtual void onBodyBegin ()
 Called when all HTTP headers have been received and the body of the response is about to arrive. More...
 
virtual void onResponseEnd ()
 Called after all the body has been received More...
 
- Public Member Functions inherited from TCPSocket
 TCPSocket ()
 
void connect ()
 Initiates a TCP connection to remoteIP and remotePort. More...
 
void close ()
 Attempts to gracefully close a connection. More...
 
void terminate ()
 Immediately shuts down the socket and makes it available for a new task. More...
 
void listen ()
 Starts listening on the local port indicated by the localPort property. More...
 
void accept ()
 Accepts a connection request that has been received on the port this instance was listening on More...
 
void accept (TCPSocket &listener)
 Accepts a connection request that was sent to another TCPSocket instance. More...
 
void push ()
 Sets the PSH TCP flag and also sends data in the outgoing buffer immediately. More...
 
virtual void onConnectRequest ()
 Called when a listening socket receives a connection request. More...
 
virtual void onTerminate ()
 Called when the socket is ready to be reused. More...
 
- Public Member Functions inherited from Socket
uint16_t write (uint16_t len, const byte *data)
 In the case of TCP, writes the given data buffer to the socket. More...
 
uint16_t write (const String &s)
 In the case of TCP, writes the given String to the socket, not including the null-terminating character. More...
 
uint16_t write (const __FlashStringHelper *pattern,...)
 Writes out the specified PROGMEM string that may contain the % escape character. More...
 
template<class T >
uint16_t write (const T &message)
 Writes out the binary representation of the parameter to the socket. More...
 
- Public Member Functions inherited from Stateful
uint8_t getState ()
 

Public Attributes

uint16_t statusCode
 Contains the HTTP status code of the response. More...
 
- Public Attributes inherited from Socket
nint16_t remotePort
 remote TCP or UDP port More...
 
nint16_t localPort
 local TCP or UDP port More...
 
IPAddress remoteIP
 remote IP address to connect to (TCP) or send the next packet to (UDP) More...
 

Protected Attributes

FlowScanner scanner
 Internal FlowScanner instance used to detect header/response/status code, etc. More...
 
- Protected Attributes inherited from Socket
SharedBuffer buffer
 output buffer for this socket More...
 
- Protected Attributes inherited from Stateful
uint8_t stateTimer
 state timer, in ticks More...
 
uint8_t state
 state code More...
 

Additional Inherited Members

- Protected Member Functions inherited from Socket
void prepareIPPacket ()
 
- Protected Member Functions inherited from NetworkService
 NetworkService ()
 
 ~NetworkService ()
 
- Protected Member Functions inherited from Stateful
void setState (uint8_t newState, uint8_t timeout)
 Changes to a new state More...
 
- Static Protected Member Functions inherited from Socket
static uint16_t calcPseudoHeaderChecksum (uint8_t protocol, uint16_t length)
 Calculates the TCP or UDP pseudo header checksum. More...
 
static uint16_t calcTCPChecksum (bool options, uint16_t dataLength, uint16_t dataChecksum)
 Calculates the TCP checksum. More...
 
static uint16_t calcUDPChecksum (uint16_t dataLength, uint16_t dataChecksum)
 Calculates the UDP checksum. More...
 
static bool verifyUDPTCPChecksum ()
 Verifies if the UDP or TCP checksum of the current packet is correct. More...
 
- Static Protected Member Functions inherited from NetworkService
static bool sendIPPacket (uint16_t length)
 Puts the current in-memory packet in the network More...
 
static void prepareIPPacket (const IPAddress &remoteIP)
 Sets up common IP header values for all outgoing IP packets and calculates the IP header checksum More...
 
static bool sameLAN (IPAddress &dst)
 Determines whether the given IP is in the same subnet as localIP More...
 
static bool begin (uint8_t cspin)
 Initializes EtherDune and the underlying hardware More...
 
static void loop ()
 Gives processing time to EtherDune so that it can check for incoming packets or send queued packets. More...
 
static DNSClientDNS ()
 Obtains access to the DNS service singleton instance. More...
 
- Static Protected Member Functions inherited from ENC28J60
static void writeByte (byte b)
 
static void writeByte (uint16_t dst, byte b)
 
static void writeBuf (uint16_t dst, uint16_t len, const byte *data)
 
static void writeBuf (uint16_t len, const byte *data)
 
static uint16_t hardwareChecksum (uint16_t src, uint16_t len)
 
static uint16_t hardwareChecksumRxOffset (uint16_t offset, uint16_t len)
 
static void moveMem (uint16_t dest, uint16_t src, uint16_t len)
 
static void readBuf (uint16_t src, uint16_t len, byte *data)
 
static void readBuf (uint16_t len, byte *data)
 
static byte readByte (uint16_t src)
 
static void packetSend (uint16_t len)
 
static void packetSend (uint16_t len, const byte *data)
 
static void loadSample ()
 
static void loadAll ()
 
static void release ()
 
static uint8_t begin (uint8_t cspin)
 
static void loop ()
 
static void enableBroadcast ()
 
static bool isLinkUp ()
 Determines whether the network link is ready More...
 
- Static Protected Attributes inherited from Socket
static uint8_t srcPort_L_count = 0
 self-incrementing counter for local ports. More...
 
- Static Protected Attributes inherited from NetworkService
static EthBuffer packet
 in-memory packet buffer currently being processed. More...
 
static ARPService ARP
 ARP singleton instance. More...
 
static MACAddress localMAC
 Ethernet MAC address. More...
 
static IPAddress localIP = { 0, 0, 0, 0 }
 IP address of this application. More...
 
static IPAddress gatewayIP = { 0, 0, 0, 0 }
 IP address of the gateway in this network. More...
 
static IPAddress netmask = { 0, 0, 0, 0 }
 Subnet mask. More...
 
static IPAddress dnsIP = { 0, 0, 0, 0 }
 IP address of the DNS server to use. More...
 

Detailed Description

Provides an easy way to query a web server.

This class extends TCPSocket by adding methods that are helpful when connecting to a web server.

To use this class, create a derived class and override the appropriate virtual functions to receive notifications about the progress of the request and capture the needed data for your application

The flow of calls works as follows:

  1. call request()
  2. onResponseReceived() - called immediately after the first line is received and there is a status code, e.g. 200 OK
  3. onHeaderReceived() - called once for each fragment of the header portion of the response
  4. onBodyBegin() - called when all HTTP headers have been received and the body of the response is about to arrive
  5. onBodyReceived() - called once for each fragment of the body that is received.
  6. onResponseEnd() - called after all the body has been received

See HTTPClientDemo_REST.ino for a full example on how to use this class.

Definition at line 41 of file HTTPClient.h.

Constructor & Destructor Documentation

HTTPClient::HTTPClient ( )

Definition at line 53 of file HTTPClient.cpp.

HTTPClient::~HTTPClient ( )

Definition at line 159 of file HTTPClient.cpp.

Member Function Documentation

void HTTPClient::onBodyBegin ( )
virtual

Called when all HTTP headers have been received and the body of the response is about to arrive.

This call gives an opportunity to configure the scanner (FlowScanner) or other parser to process the body as it arrives.

Definition at line 50 of file HTTPClient.cpp.

void HTTPClient::onBodyReceived ( uint16_t  len,
const byte *  data 
)
virtual

Called once for each fragment of the body that is received.

Parameters
lenlength of the buffer that contains the body portion received
datapointer to the buffer containing the body data

Definition at line 44 of file HTTPClient.cpp.

void HTTPClient::onHeaderReceived ( uint16_t  len,
const byte *  data 
)
virtual

Called once for each fragment of the header portion of the response

Parameters
lenlength of the data byte array containing the header
datapointer to the buffer containing the header data

Definition at line 38 of file HTTPClient.cpp.

void HTTPClient::onResponseEnd ( )
virtual

Called after all the body has been received

Definition at line 32 of file HTTPClient.cpp.

void HTTPClient::onResponseReceived ( )
virtual

Called immediately after the first line is received and there is a status code, e.g.

200 OK. The HTTP status code is stored in the statusCode member variable.

Definition at line 28 of file HTTPClient.cpp.

void HTTPClient::request ( const String &  hostName,
const String &  resource,
uint16_t  port = 80 
)

Starts a new HTTP request.

Any previously ongoing request is aborted.

Parameters
hostNameName of the host to connect to, e.g. www.friendev.com
resourceResource path, e.g. /index.html
portTCP port, 80 by default

Definition at line 68 of file HTTPClient.cpp.

Member Data Documentation

FlowScanner HTTPClient::scanner
protected

Internal FlowScanner instance used to detect header/response/status code, etc.

Can be reused to scan the body after onBodyBegin() is called by EtherDune

Definition at line 59 of file HTTPClient.h.

uint16_t HTTPClient::statusCode

Contains the HTTP status code of the response.

Valid only after onResponseReceived() is called by EtherDune

Definition at line 64 of file HTTPClient.h.


The documentation for this class was generated from the following files: