Fork HTTPClient and Modfiy code for mbed 6.0
Dependents: mbed-demo-http-get-json
Diff: HTTPClient.cpp
- Revision:
- 19:bcbf0af9fac3
- Parent:
- 18:cf5d7427a9ec
- Child:
- 20:4ea5255c1b04
--- a/HTTPClient.cpp Sun Apr 28 10:04:51 2013 +0000 +++ b/HTTPClient.cpp Fri Jan 24 13:51:36 2014 +0000 @@ -57,15 +57,18 @@ } -#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 + +void HTTPClient::customHeaders(const char **headers, size_t pairs) +{ + m_customHeaders = headers; + m_nCustomHeaders = pairs; +} + HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking { @@ -162,12 +165,7 @@ 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":""; - 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 - } + snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request ret = send(buf); if(ret) { @@ -175,8 +173,33 @@ ERR("Could not write request"); return HTTP_CONN; } + + // send authorization + if ((!m_basicAuthUser) && (!m_basicAuthPass)) { + strcpy(buf, "Authorization: "); + createauth(m_basicAuthUser, m_basicAuthPass, buf+strlen(buf), sizeof(buf)-strlen(buf)); + strcat(buf, "\r\n"); + + ret = send(buf); + if(ret) + { + m_sock.close(); + ERR("Could not write request"); + return HTTP_CONN; + } + } //Send all headers + for (size_t nh = 0; nh < m_nCustomHeaders; ++nh) { + snprintf(buf, sizeof(buf), "%s: %s\r\n", m_customHeaders[nh], m_customHeaders[nh+1]); + ret = send(buf); + if(ret) + { + m_sock.close(); + ERR("Could not write request"); + return HTTP_CONN; + } + } //Send default headers DBG("Sending headers"); @@ -658,7 +681,6 @@ 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)); }