Fork with basicauth enabled.

Dependents:   TwitterMbed HTTPClient_SuperTweet HTTPClient_SuperTweet_MPL115A2 cc3000_twitter_demo

Fork of HTTPClient by Stephen Paulger

Files at this revision

API Documentation at this revision

Comitter:
kazushi2008
Date:
Sun Apr 28 10:04:51 2013 +0000
Parent:
17:8299c6440e37
Commit message:
implemented basicauth function

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
diff -r 8299c6440e37 -r cf5d7427a9ec HTTPClient.cpp
--- a/HTTPClient.cpp	Fri Jan 18 17:15:39 2013 +0000
+++ b/HTTPClient.cpp	Sun Apr 28 10:04:51 2013 +0000
@@ -58,10 +58,12 @@
 }
 
 #if 1
+char auth[512];
 void HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification
 {
   m_basicAuthUser = user;
   m_basicAuthPassword = password;
+  createauth(m_basicAuthUser, m_basicAuthPassword, auth, strlen(auth));
 }
 #endif
 
@@ -160,7 +162,12 @@
   DBG("Sending request");
   char buf[CHUNK_SIZE];
   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
+  if((!m_basicAuthUser)&&(!strlen(m_basicAuthUser))){
+    snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request
+  } else {
+    //printf("auth: %s\r\n", auth);
+    snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n%s\r\n", meth, path, host, auth); //Write request with basic auth
+  }
   ret = send(buf);
   if(ret)
   {
@@ -647,3 +654,35 @@
 
   return HTTP_OK;
 }
+
+void HTTPClient::createauth (const char *user, const char *pwd, char *buf, int len) {
+    char tmp[80];
+ 
+    strncpy(buf, "Authorization: Basic ", 21);//len);
+    snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
+    base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf));
+}
+
+// Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+int HTTPClient::base64enc(const char *input, unsigned int length, char *output, int len) {
+  static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+  unsigned int c, c1, c2, c3;
+ 
+  if (len < ((((length-1)/3)+1)<<2)) return -1;
+  for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
+    c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
+    c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
+    c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
+ 
+    c = ((c1 & 0xFC) >> 2);
+    output[j+0] = base64[c];
+    c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
+    output[j+1] = base64[c];
+    c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
+    output[j+2] = (length>i+1)?base64[c]:'=';
+    c = (c3 & 0x3F);
+    output[j+3] = (length>i+2)?base64[c]:'=';
+  }
+  output[(((length-1)/3)+1)<<2] = '\0';
+  return 0;
+}
diff -r 8299c6440e37 -r cf5d7427a9ec HTTPClient.h
--- a/HTTPClient.h	Fri Jan 18 17:15:39 2013 +0000
+++ b/HTTPClient.h	Sun Apr 28 10:04:51 2013 +0000
@@ -140,6 +140,8 @@
   HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
   HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
   HTTPResult parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL
+  void createauth (const char *user, const char *pwd, char *buf, int len);
+  int base64enc(const char *input, unsigned int length, char *output, int len);
 
   //Parameters
   TCPSocketConnection m_sock;