HTTP Client library

Dependents:   weather_LCD_display News_LCD_display TwitterExample_1 GeoLocation_LCD_Display ... more

Committer:
donatien
Date:
Thu Apr 19 09:52:54 2012 +0000
Revision:
7:d97a4fc01c86
Parent:
6:3d3824893be1
Documentation++

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 6:3d3824893be1 1 /* HTTPClient.h */
donatien 6:3d3824893be1 2 /*
donatien 6:3d3824893be1 3 Copyright (C) 2012 ARM Limited.
donatien 6:3d3824893be1 4
donatien 6:3d3824893be1 5 Permission is hereby granted, free of charge, to any person obtaining a copy of
donatien 6:3d3824893be1 6 this software and associated documentation files (the "Software"), to deal in
donatien 6:3d3824893be1 7 the Software without restriction, including without limitation the rights to
donatien 6:3d3824893be1 8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
donatien 6:3d3824893be1 9 of the Software, and to permit persons to whom the Software is furnished to do
donatien 6:3d3824893be1 10 so, subject to the following conditions:
donatien 6:3d3824893be1 11
donatien 6:3d3824893be1 12 The above copyright notice and this permission notice shall be included in all
donatien 6:3d3824893be1 13 copies or substantial portions of the Software.
donatien 6:3d3824893be1 14
donatien 6:3d3824893be1 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
donatien 6:3d3824893be1 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
donatien 6:3d3824893be1 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
donatien 6:3d3824893be1 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
donatien 6:3d3824893be1 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 6:3d3824893be1 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
donatien 6:3d3824893be1 21 SOFTWARE.
donatien 6:3d3824893be1 22 */
donatien 6:3d3824893be1 23
donatien 6:3d3824893be1 24 /** \file
donatien 6:3d3824893be1 25 HTTP Client header file
donatien 6:3d3824893be1 26 */
donatien 6:3d3824893be1 27
donatien 6:3d3824893be1 28 #ifndef HTTP_CLIENT_H
donatien 6:3d3824893be1 29 #define HTTP_CLIENT_H
donatien 6:3d3824893be1 30
donatien 6:3d3824893be1 31 #include "api/socket.h"
donatien 6:3d3824893be1 32
donatien 6:3d3824893be1 33 #define HTTP_CLIENT_DEFAULT_TIMEOUT 4000
donatien 6:3d3824893be1 34
donatien 6:3d3824893be1 35 class HTTPData;
donatien 6:3d3824893be1 36
donatien 6:3d3824893be1 37 #include "IHTTPData.h"
donatien 6:3d3824893be1 38 #include "mbed.h"
donatien 6:3d3824893be1 39
donatien 6:3d3824893be1 40 ///HTTP client results
donatien 6:3d3824893be1 41 enum HTTPResult
donatien 6:3d3824893be1 42 {
donatien 6:3d3824893be1 43 HTTP_OK, ///<Success
donatien 6:3d3824893be1 44 HTTP_PROCESSING, ///<Processing
donatien 6:3d3824893be1 45 HTTP_PARSE, ///<url Parse error
donatien 6:3d3824893be1 46 HTTP_DNS, ///<Could not resolve name
donatien 6:3d3824893be1 47 HTTP_PRTCL, ///<Protocol error
donatien 6:3d3824893be1 48 HTTP_NOTFOUND, ///<HTTP 404 Error
donatien 6:3d3824893be1 49 HTTP_REFUSED, ///<HTTP 403 Error
donatien 6:3d3824893be1 50 HTTP_ERROR, ///<HTTP xxx error
donatien 6:3d3824893be1 51 HTTP_TIMEOUT, ///<Connection timeout
donatien 6:3d3824893be1 52 HTTP_CONN ///<Connection error
donatien 6:3d3824893be1 53 };
donatien 6:3d3824893be1 54
donatien 7:d97a4fc01c86 55 /**A simple HTTP Client
donatien 6:3d3824893be1 56 The HTTPClient is composed of:
donatien 6:3d3824893be1 57 - The actual client (HTTPClient)
donatien 6:3d3824893be1 58 - Classes that act as a data repository, each of which deriving from the HTTPData class (HTTPText for short text content, HTTPFile for file I/O, HTTPMap for key/value pairs, and HTTPStream for streaming purposes)
donatien 6:3d3824893be1 59 */
donatien 6:3d3824893be1 60 class HTTPClient
donatien 6:3d3824893be1 61 {
donatien 6:3d3824893be1 62 public:
donatien 7:d97a4fc01c86 63 ///Instantiate the HTTP client
donatien 6:3d3824893be1 64 HTTPClient();
donatien 6:3d3824893be1 65 ~HTTPClient();
donatien 6:3d3824893be1 66
donatien 6:3d3824893be1 67 #if 0 //TODO add header handlers
donatien 6:3d3824893be1 68 /**
donatien 6:3d3824893be1 69 Provides a basic authentification feature (Base64 encoded username and password)
donatien 6:3d3824893be1 70 Pass two NULL pointers to switch back to no authentication
donatien 6:3d3824893be1 71 @param user username to use for authentication, must remain valid durlng the whole HTTP session
donatien 6:3d3824893be1 72 @param user password to use for authentication, must remain valid durlng the whole HTTP session
donatien 6:3d3824893be1 73 */
donatien 6:3d3824893be1 74 void basicAuth(const char* user, const char* password); //Basic Authentification
donatien 6:3d3824893be1 75 #endif
donatien 6:3d3824893be1 76
donatien 6:3d3824893be1 77 //High Level setup functions
donatien 7:d97a4fc01c86 78 /** Execute a GET request on the url
donatien 7:d97a4fc01c86 79 Blocks until completion
donatien 6:3d3824893be1 80 @param url : url on which to execute the request
donatien 6:3d3824893be1 81 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
donatien 6:3d3824893be1 82 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
donatien 7:d97a4fc01c86 83 @return 0 on success, NET error (<0) on failure
donatien 6:3d3824893be1 84 */
donatien 6:3d3824893be1 85 int get(const char* url, IHTTPDataIn* pDataIn, uint32_t timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
donatien 6:3d3824893be1 86
donatien 7:d97a4fc01c86 87 /** Execute a GET request on the url
donatien 7:d97a4fc01c86 88 Blocks until completion
donatien 7:d97a4fc01c86 89 This is a helper to directly get a piece of text from a HTTP result
donatien 6:3d3824893be1 90 @param url : url on which to execute the request
donatien 6:3d3824893be1 91 @param result : pointer to a char array in which the result will be stored
donatien 6:3d3824893be1 92 @param maxResultLen : length of the char array (including space for the NULL-terminating char)
donatien 6:3d3824893be1 93 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
donatien 6:3d3824893be1 94 @return 0 on success, NET error on failure
donatien 6:3d3824893be1 95 */
donatien 6:3d3824893be1 96 int get(const char* url, char* result, size_t maxResultLen, uint32_t timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
donatien 6:3d3824893be1 97
donatien 7:d97a4fc01c86 98 /** Execute a POST request on the url
donatien 7:d97a4fc01c86 99 Blocks until completion
donatien 6:3d3824893be1 100 @param url : url on which to execute the request
donatien 6:3d3824893be1 101 @param dataOut : a IHTTPDataOut instance that contains the data that will be posted
donatien 6:3d3824893be1 102 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
donatien 6:3d3824893be1 103 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
donatien 6:3d3824893be1 104 @return 0 on success, NET error on failure
donatien 6:3d3824893be1 105 */
donatien 6:3d3824893be1 106 int post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, uint32_t timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
donatien 6:3d3824893be1 107
donatien 7:d97a4fc01c86 108 /** Get last request's HTTP response code
donatien 6:3d3824893be1 109 @return The HTTP response code of the last request
donatien 6:3d3824893be1 110 */
donatien 6:3d3824893be1 111 int getHTTPResponseCode();
donatien 6:3d3824893be1 112
donatien 6:3d3824893be1 113 private:
donatien 6:3d3824893be1 114 enum HTTP_METH
donatien 6:3d3824893be1 115 {
donatien 6:3d3824893be1 116 HTTP_GET,
donatien 6:3d3824893be1 117 HTTP_POST,
donatien 6:3d3824893be1 118 HTTP_HEAD
donatien 6:3d3824893be1 119 };
donatien 6:3d3824893be1 120
donatien 6:3d3824893be1 121 int connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, uint32_t timeout); //Execute request
donatien 6:3d3824893be1 122 int recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
donatien 6:3d3824893be1 123 int send(char* buf, size_t len = 0); //0 on success, err code on failure
donatien 6:3d3824893be1 124 int parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL
donatien 6:3d3824893be1 125
donatien 6:3d3824893be1 126 //Parameters
donatien 6:3d3824893be1 127 int m_sock;
donatien 6:3d3824893be1 128 uint32_t m_timeout;
donatien 6:3d3824893be1 129
donatien 6:3d3824893be1 130 const char* m_basicAuthUser;
donatien 6:3d3824893be1 131 const char* m_basicAuthPassword;
donatien 6:3d3824893be1 132 int m_httpResponseCode;
donatien 6:3d3824893be1 133
donatien 6:3d3824893be1 134 struct sockaddr_in m_serverAddr;
donatien 6:3d3824893be1 135
donatien 6:3d3824893be1 136 };
donatien 6:3d3824893be1 137
donatien 6:3d3824893be1 138 //Including data containers here for more convenience
donatien 6:3d3824893be1 139 #include "data/HTTPText.h"
donatien 6:3d3824893be1 140 #include "data/HTTPMap.h"
donatien 6:3d3824893be1 141
donatien 6:3d3824893be1 142 #endif