friendev EtherDune TCP/IP library
/home/jander/temp/etherdune/HTTPClient.h
Go to the documentation of this file.
1 // EtherDune HTTP Client class
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Provides an easy way to query a web server
4 //
5 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
6 //
7 // This source is subject to the GPLv2 license.
8 // Please see the License.txt file for more information.
9 // All other rights reserved.
10 //
11 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
12 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 // PARTICULAR PURPOSE.
15 
32 
33 #include <ACross.h>
34 #include "HTTPConstants.h"
35 #include "TCPSocket.h"
36 #include <FlowScanner.h>
37 
38 static const char statusCodePatternString[] PROGMEM = "HTTP/%*1d.%*1d %d%*99[^\r\n]\r\n";
39 static const char bodyBeginPatternString[] PROGMEM = "\r\n\r\n";
40 
41 class HTTPClient : public TCPSocket
42 {
43 
44 private:
45 
46  FlowPattern statusCodePattern;
47  FlowPattern bodyBeginPattern;
48  String host;
49  String res;
50  uint16_t DNSid;
51 
52  void onConnect();
53  void onReceive(uint16_t len, const byte* data);
54  void onClose();
55  void onDNSResolve(uint8_t status, uint16_t identification, const IPAddress& ip);
56 
57 protected:
58 
59  FlowScanner scanner;
60 
62 public:
63 
64  uint16_t statusCode;
65 
66  HTTPClient();
67  ~HTTPClient();
68 
69  void request(const String& hostName, const String& resource, uint16_t port=80);
70 
71  virtual void onResponseReceived();
72  virtual void onHeaderReceived(uint16_t len, const byte* data);
73  virtual void onBodyReceived(uint16_t len, const byte* data);
74  virtual void onBodyBegin();
75  virtual void onResponseEnd();
76 
77 };
78 
void request(const String &hostName, const String &resource, uint16_t port=80)
Starts a new HTTP request.
Definition: HTTPClient.cpp:68
virtual void onResponseReceived()
Called immediately after the first line is received and there is a status code, e.g.
Definition: HTTPClient.cpp:28
virtual void onBodyBegin()
Called when all HTTP headers have been received and the body of the response is about to arrive...
Definition: HTTPClient.cpp:50
static const char bodyBeginPatternString[]
Definition: HTTPClient.h:39
represents an IP address in memory
Definition: inet.h:181
FlowScanner scanner
Internal FlowScanner instance used to detect header/response/status code, etc.
Definition: HTTPClient.h:59
Implements the TCP protocol.
Definition: TCPSocket.h:45
Provides an easy way to query a web server.
Definition: HTTPClient.h:41
uint16_t statusCode
Contains the HTTP status code of the response.
Definition: HTTPClient.h:64
virtual void onBodyReceived(uint16_t len, const byte *data)
Called once for each fragment of the body that is received.
Definition: HTTPClient.cpp:44
virtual void onHeaderReceived(uint16_t len, const byte *data)
Called once for each fragment of the header portion of the response
Definition: HTTPClient.cpp:38
virtual void onResponseEnd()
Called after all the body has been received
Definition: HTTPClient.cpp:32
static const char statusCodePatternString[]
Definition: HTTPClient.h:38