Fork with basicauth enabled.

Dependents:   TweetRFID

Fork of HTTPClient by Donatien Garnier

Files at this revision

API Documentation at this revision

Comitter:
donatien
Date:
Thu Aug 30 15:38:57 2012 +0000
Parent:
15:5ad07f90e895
Child:
17:8299c6440e37
Commit message:
IHTTPData (HTTPText, HTTPMap) objects can be re-used multiple times (reading/writing position is reset on each connection); Support for PUT and DELETE methods

Changed in this revision

HTTPClient.cpp Show annotated file Show diff for this revision Revisions of this file
HTTPClient.h Show annotated file Show diff for this revision Revisions of this file
IHTTPData.h Show annotated file Show diff for this revision Revisions of this file
data/HTTPMap.cpp Show annotated file Show diff for this revision Revisions of this file
data/HTTPMap.h Show annotated file Show diff for this revision Revisions of this file
data/HTTPText.cpp Show annotated file Show diff for this revision Revisions of this file
data/HTTPText.h Show annotated file Show diff for this revision Revisions of this file
--- a/HTTPClient.cpp	Wed Aug 29 11:16:48 2012 +0000
+++ b/HTTPClient.cpp	Thu Aug 30 15:38:57 2012 +0000
@@ -18,11 +18,10 @@
  */
 
 //Debug is disabled by default
-#if 1
+#if 0
 //Enable debug
 #include <cstdio>
-//#define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__); 
-#define DBG(x, ...) 
+#define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__); 
 #define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__); 
 #define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__); 
 
@@ -82,6 +81,17 @@
   return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
 }
 
+HTTPResult HTTPClient::put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
+{
+  return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
+}
+
+HTTPResult HTTPClient::del(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
+{
+  return connect(url, HTTP_DELETE, NULL, pDataIn, timeout);
+}
+
+
 int HTTPClient::getHTTPResponseCode()
 {
   return m_httpResponseCode;
@@ -107,6 +117,12 @@
 { 
   m_httpResponseCode = 0; //Invalidate code
   m_timeout = timeout;
+  
+  pDataIn->writeReset();
+  if( pDataOut )
+  {
+    pDataOut->readReset();
+  }
 
   char scheme[8];
   uint16_t port;
@@ -143,7 +159,7 @@
   //Send request
   DBG("Sending request");
   char buf[CHUNK_SIZE];
-  const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":"";
+  const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":"";
   snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request
   ret = send(buf);
   if(ret)
@@ -157,7 +173,7 @@
 
   //Send default headers
   DBG("Sending headers");
-  if( (method == HTTP_POST) && (pDataOut != NULL) )
+  if( pDataOut != NULL )
   {
     if( pDataOut->getIsChunked() )
     {
@@ -186,8 +202,8 @@
 
   size_t trfLen;
   
-  //Send data (if POST)
-  if( (method == HTTP_POST) && (pDataOut != NULL) )
+  //Send data (if available)
+  if( pDataOut != NULL )
   {
     DBG("Sending data");
     while(true)
@@ -258,9 +274,9 @@
     PRTCL_ERR();
   }
 
-  if(m_httpResponseCode != 200)
+  if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) )
   {
-    //Cannot match string, error
+    //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers 
     WARN("Response code %d", m_httpResponseCode);
     PRTCL_ERR();
   }
--- a/HTTPClient.h	Wed Aug 29 11:16:48 2012 +0000
+++ b/HTTPClient.h	Thu Aug 30 15:38:57 2012 +0000
@@ -72,7 +72,7 @@
 #endif
   
   //High Level setup functions
-  /** Execute a GET request on the url
+  /** Execute a GET request on the URL
   Blocks until completion
   @param url : url on which to execute the request
   @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
@@ -81,7 +81,7 @@
   */
   HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
   
-  /** Execute a GET request on the url
+  /** Execute a GET request on the URL
   Blocks until completion
   This is a helper to directly get a piece of text from a HTTP result
   @param url : url on which to execute the request
@@ -92,7 +92,7 @@
   */
   HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
 
-  /** Execute a POST request on the url
+  /** Execute a POST request on the URL
   Blocks until completion
   @param url : url on which to execute the request
   @param dataOut : a IHTTPDataOut instance that contains the data that will be posted
@@ -102,6 +102,25 @@
   */
   HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
   
+  /** Execute a PUT request on the URL
+  Blocks until completion
+  @param url : url on which to execute the request
+  @param dataOut : a IHTTPDataOut instance that contains the data that will be put
+  @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
+  @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
+  @return 0 on success, HTTP error (<0) on failure
+  */
+  HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
+  
+  /** Execute a DELETE request on the URL
+  Blocks until completion
+  @param url : url on which to execute the request
+  @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
+  @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
+  @return 0 on success, HTTP error (<0) on failure
+  */
+  HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
+  
   /** Get last request's HTTP response code
   @return The HTTP response code of the last request
   */
@@ -112,6 +131,8 @@
   {
     HTTP_GET,
     HTTP_POST,
+    HTTP_PUT,
+    HTTP_DELETE,
     HTTP_HEAD
   };
 
--- a/IHTTPData.h	Wed Aug 29 11:16:48 2012 +0000
+++ b/IHTTPData.h	Thu Aug 30 15:38:57 2012 +0000
@@ -29,6 +29,11 @@
 {
 protected:
   friend class HTTPClient;
+  
+  /** Reset stream to its beginning 
+   * Called by the HTTPClient on each new request
+   */
+  virtual void readReset() = 0;
 
   /** Read a piece of data to be transmitted
    * @param buf Pointer to the buffer on which to copy the data
@@ -60,6 +65,11 @@
 protected:
   friend class HTTPClient;
 
+  /** Reset stream to its beginning 
+   * Called by the HTTPClient on each new request
+   */
+  virtual void writeReset() = 0;
+
   /** Write a piece of data transmitted by the server
    * @param buf Pointer to the buffer from which to copy the data
    * @param len Length of the buffer
--- a/data/HTTPMap.cpp	Wed Aug 29 11:16:48 2012 +0000
+++ b/data/HTTPMap.cpp	Thu Aug 30 15:38:57 2012 +0000
@@ -49,6 +49,10 @@
   m_pos = 0;
 }
 
+/*virtual*/ void HTTPMap::readReset()
+{
+  m_pos = 0;
+}
 
 /*virtual*/ int HTTPMap::read(char* buf, size_t len, size_t* pReadLen)
 {
--- a/data/HTTPMap.h	Wed Aug 29 11:16:48 2012 +0000
+++ b/data/HTTPMap.h	Thu Aug 30 15:38:57 2012 +0000
@@ -50,6 +50,8 @@
 
 protected:
   //IHTTPDataIn
+  virtual void readReset();
+
   virtual int read(char* buf, size_t len, size_t* pReadLen);
 
   virtual int getDataType(char* type, size_t maxTypeLen); //Internet media type for Content-Type header
--- a/data/HTTPText.cpp	Wed Aug 29 11:16:48 2012 +0000
+++ b/data/HTTPText.cpp	Thu Aug 30 15:38:57 2012 +0000
@@ -40,6 +40,11 @@
 }
 
 //IHTTPDataIn
+/*virtual*/ void HTTPText::readReset()
+{
+  m_pos = 0;
+}
+
 /*virtual*/ int HTTPText::read(char* buf, size_t len, size_t* pReadLen)
 {
   *pReadLen = MIN(len, m_size - 1 - m_pos);
@@ -66,6 +71,11 @@
 }
 
 //IHTTPDataOut
+/*virtual*/ void HTTPText::writeReset()
+{
+  m_pos = 0;
+}
+
 /*virtual*/ int HTTPText::write(const char* buf, size_t len)
 {
   size_t writeLen = MIN(len, m_size - 1 - m_pos);
--- a/data/HTTPText.h	Wed Aug 29 11:16:48 2012 +0000
+++ b/data/HTTPText.h	Thu Aug 30 15:38:57 2012 +0000
@@ -41,6 +41,8 @@
 
 protected:
   //IHTTPDataIn
+  virtual void readReset();
+  
   virtual int read(char* buf, size_t len, size_t* pReadLen);
 
   virtual int getDataType(char* type, size_t maxTypeLen); //Internet media type for Content-Type header
@@ -50,6 +52,8 @@
   virtual size_t getDataLen(); //For Content-Length header
 
   //IHTTPDataOut
+  virtual void writeReset();
+  
   virtual int write(const char* buf, size_t len);
 
   virtual void setDataType(const char* type); //Internet media type from Content-Type header