HTTP Client data container for form(multipart/form-data)

Dependencies:   mbed EthernetInterface HTTPClient mbed-rtos

Revision:
1:77c616a1ab54
Parent:
0:fcd577a3925b
--- a/HTTPPoster.cpp	Thu May 31 10:32:39 2012 +0000
+++ b/HTTPPoster.cpp	Tue Aug 28 14:39:29 2012 +0000
@@ -1,49 +1,42 @@
+/* HTTPPoster.cpp */
 
-/*
-Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
- 
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
- 
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
- 
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-*/
+//Debug is disabled by default
+#if 1
+//Enable debug
+#include <cstdio>
+#define DBG(x, ...) std::printf("[HTTPPoster : DBG]"x"\r\n", ##__VA_ARGS__); 
+//#define DBG(x, ...) 
+#define WARN(x, ...) std::printf("[HTTPPoster : WARN]"x"\r\n", ##__VA_ARGS__); 
+#define ERR(x, ...) std::printf("[HTTPPoster : ERR]"x"\r\n", ##__VA_ARGS__); 
+
+#else
+//Disable debug
+#define DBG(x, ...) 
+#define WARN(x, ...)
+#define ERR(x, ...) 
+
+#endif
 
 #include "HTTPPoster.h"
-//#define __DEBUG
-//#include "mydbg.h"
-#include "dbg/dbg.h"
+
+#define OK 0
 
 ///HTTP Client data container for form(multipart/form-data)
 HTTPPoster::HTTPPoster()
 {
-    DBG("\n");
     m_cur = 0;
     m_seq = 0;
     m_send_len = 0;
     m_fp = NULL;
     m_post_data.clear();
     m_buf.clear();
-    m_boundary = "----mbedHTTPClientPoster";
+    m_boundary = "----HTTPPoster123";
     m_ContentType = "multipart/form-data; boundary="; 
     m_ContentType += m_boundary;
 }
 
 HTTPPoster::~HTTPPoster()
 {
-    DBG("m_post_data.size()=%d\n", m_post_data.size());
     if (m_fp) {
         fclose(m_fp);
         m_fp = NULL;
@@ -52,7 +45,6 @@
 
 bool HTTPPoster::addFile(const char* name, const char* path)
 {
-    DBG("%p name=%s path=%s\n", this, name, path);
     struct stpost data;
 
     m_fp = fopen(path, "rb");
@@ -78,7 +70,6 @@
 
 bool HTTPPoster::add(const char* name, const char* value)
 {
-    DBG("%p name=%s value=%s\n", this, name, value);
     struct stpost data;
 
     string head = "Content-Disposition: form-data; name=\"";
@@ -93,9 +84,8 @@
     return true;
 }
 
-int HTTPPoster::read(char* buf, int len)
+/*virtual*/ int HTTPPoster::read(char* buf, size_t len, size_t* pReadLen)
 {
-    //DBG("buf=%p len=%d\n", buf, len);
     int c;
     switch(m_seq) {
         case 0:
@@ -157,18 +147,28 @@
     memcpy(buf, m_buf.data(), len2);
     m_buf.erase(0, len2);
     m_send_len += len2;
-    DBG("m_len=%d m_send_len=%d len=%d len2=%d\n", m_len, m_send_len, len, len2);
-    //DBG_HEX((uint8_t*)buf, len2);
-    return len2;
+    DBG("m_len=%d m_send_len=%d len=%d len2=%d", m_len, m_send_len, len, len2);
+    *pReadLen = len2;
+    return OK;
 }
 
-string HTTPPoster::getDataType() //Internet media type for Content-Type header
+/*virtual*/ int HTTPPoster::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header
 {
-    DBG("%s\n", m_ContentType.c_str());
-    return m_ContentType;
+    if (m_ContentType.length() >= maxTypeLen) {
+        WARN("maxTypeLen=%d", maxTypeLen);
+        WARN("m_ContentType.length()=%d", m_ContentType.length());
+        return !OK;
+    }
+    strcpy(type, m_ContentType.c_str());
+    return OK;
 }
 
-int HTTPPoster::getDataLen() //For Content-Length header
+/*virtual*/ bool HTTPPoster::getIsChunked() //For Transfer-Encoding header
+{
+    return false; ////Data is computed one key/value pair at a time
+}
+
+/*virtual*/ size_t HTTPPoster::getDataLen() //For Content-Length header
 {
     m_len = 0;
     for(int i = 0; i < m_post_data.size(); i++) {
@@ -177,7 +177,6 @@
         m_len += 2;                               // CRLF  
         m_len += m_post_data[i].length;           // value / file body
         m_len += 2;                               // CRLF
-        DBG("%d m_len=%d\n", i, m_len);
     }
     m_len += 2 + strlen(m_boundary) + 2 + 2;   // "--" boundary "--" CRLF
     return m_len;