Fork of OAuth4Tw to use mbed's official EthernetInterface and HTTPClient

Fork of OAuth4Tw by Masayoshi Takahashi

Files at this revision

API Documentation at this revision

Comitter:
vpcola
Date:
Thu Apr 30 10:19:31 2015 +0000
Parent:
0:0048b264a3ad
Commit message:
Fork of OAuth4Tw to use normal EthernetInterface and HTTPClient library

Changed in this revision

oauth_data.cpp Show annotated file Show diff for this revision Revisions of this file
oauth_data.h Show annotated file Show diff for this revision Revisions of this file
oauth_http.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 0048b264a3ad -r b3501683b856 oauth_data.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/oauth_data.cpp	Thu Apr 30 10:19:31 2015 +0000
@@ -0,0 +1,106 @@
+#include "oauth_data.h"
+
+#include <cstring>
+
+#define OK 0
+
+using std::memcpy;
+using std::strncpy;
+using std::strlen;
+
+#define MIN(x,y) (((x)<(y))?(x):(y))
+
+OAuthDataOut::OAuthDataOut(const char * contentType, const char* data)
+    : m_str((char *) data),
+    m_contentType(contentType),
+    m_pos(0),
+    m_isChunked(false)
+{
+  m_size = strlen(data) + 1;
+}
+
+void OAuthDataOut::readReset()
+{
+    m_pos = 0;
+}
+
+// HTTPClient reads a piece of data to be transmitted
+// buf - pointer to the buffer on which to copy (destination)
+// the data, len is the length of the buffer.
+// pReadLen is a pointer a variable on which the actual number 
+// of data that was copied
+int OAuthDataOut::read(char* buf, size_t len, size_t* pReadLen)
+{
+    *pReadLen = MIN(len, m_size - 1 - m_pos);
+    memcpy(buf, m_str + m_pos, *pReadLen);
+    m_pos += *pReadLen;
+    return OK;
+
+}
+// Copies the content-type of the data 
+// type (out) gets the content type of the data stored.
+int OAuthDataOut::getDataType(char* type, size_t maxTypeLen)
+{
+   strncpy(type, m_contentType.c_str(), maxTypeLen-1);
+   type[maxTypeLen-1] = 0;
+   return OK;
+}
+
+bool OAuthDataOut::getIsChunked()
+{
+    return m_isChunked;
+}
+
+size_t OAuthDataOut::getDataLen()
+{
+    return m_size - 1;
+}
+
+OAuthDataIn::OAuthDataIn(char* str, size_t size)
+    : m_str(str),
+    m_contentType("text/plain"),
+    m_size(size),
+    m_pos(0),
+    m_isChunked(false)
+{
+
+}
+
+std::string OAuthDataIn::getContentType()
+{
+    return m_contentType;
+}
+
+std::string OAuthDataIn::getData()
+{
+    return std::string((const char *)m_str);
+}
+
+void OAuthDataIn::writeReset()
+{
+  m_pos = 0;
+}
+
+int OAuthDataIn::write(const char* buf, size_t len)
+{
+  size_t writeLen = MIN(len, m_size - 1 - m_pos);
+  memcpy(m_str + m_pos, buf, writeLen);
+  m_pos += writeLen;
+  m_str[m_pos] = '\0';
+  return OK;
+}
+
+void OAuthDataIn::setDataType(const char* type)
+{
+    m_contentType = type;
+}
+
+void OAuthDataIn::setIsChunked(bool chunked)
+{
+    m_isChunked = chunked;
+}
+
+void OAuthDataIn::setDataLen(size_t len)
+{
+    m_size = len;
+}
diff -r 0048b264a3ad -r b3501683b856 oauth_data.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/oauth_data.h	Thu Apr 30 10:19:31 2015 +0000
@@ -0,0 +1,70 @@
+#ifndef __OAUTHDATA_H__
+#define __OAUTHDATA_H__
+
+/**
+ * Author : Cola Vergil
+ * Email  : vpcola@gmail.com
+ * Date : Thu Apr 30 2015
+ **/
+
+#include <IHTTPData.h>
+#include <string>
+
+class OAuthDataOut : public IHTTPDataOut
+{
+    public:
+    /** Create an HTTPText instance for output
+     * @param str String to be transmitted
+     */
+    OAuthDataOut(const char * contentType, const char * data);
+
+
+    protected:
+    //IHTTPDataOut
+    virtual void readReset();
+
+    virtual int read(char* buf, size_t len, size_t* pReadLen);
+
+    virtual int getDataType(char* type, size_t maxTypeLen);
+
+    virtual bool getIsChunked();
+
+    virtual size_t getDataLen();
+
+    private:
+    char * m_str;
+    std::string m_contentType;
+    size_t m_size;
+    size_t m_pos;
+    bool m_isChunked;
+};
+
+class OAuthDataIn : public IHTTPDataIn
+{
+    public:
+
+    OAuthDataIn(char* str, size_t size);
+    std::string getContentType();
+    std::string getData();
+
+    protected:
+    //IHTTPDataIn
+    virtual void writeReset();
+
+    virtual int write(const char* buf, size_t len);
+
+    virtual void setDataType(const char* type); //Internet media type for Content-Type header
+
+    virtual void setIsChunked(bool chunked); //For Transfer-Encoding header
+
+    virtual void setDataLen(size_t len); //For Content-Length header
+
+    private:
+    char * m_str;
+    std::string m_contentType;
+    size_t m_size;
+    size_t m_pos;
+     bool m_isChunked;
+};
+
+#endif
diff -r 0048b264a3ad -r b3501683b856 oauth_http.cpp
--- a/oauth_http.cpp	Mon Dec 12 18:47:10 2011 +0000
+++ b/oauth_http.cpp	Thu Apr 30 10:19:31 2015 +0000
@@ -34,6 +34,10 @@
 #include "oauth.h"
 
 #include <HTTPClient.h>
+#include "oauth_data.h"
+
+#define RESULT_BUFFER_SIZ 1024
+static char result_buffer[RESULT_BUFFER_SIZ];
 
 /* wrapper functions */
 
@@ -52,10 +56,10 @@
 std::string oauth_http_post(const char *u, const char *p)
 {
     HTTPClient http;
-    HTTPText req("application/x-www-form-urlencoded");
-    HTTPText res;
-    req.set(p);
+    OAuthDataOut req("application/x-www-form-urlencoded", p);
+    OAuthDataIn  res(result_buffer, RESULT_BUFFER_SIZ);
+
     http.post(u, req, &res);
-    return res.get();
+    return res.getData();
 }