Fork HTTPClient and Modfiy code for mbed 6.0
Dependents: mbed-demo-http-get-json
Diff: HTTPClient.cpp
- Revision:
- 18:cf5d7427a9ec
- Parent:
- 17:8299c6440e37
- Child:
- 19:bcbf0af9fac3
--- 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; +}