Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Car_Bon_car_module
Fork of HTTPClient by
Revision 19:bcbf0af9fac3, committed 2014-01-24
- Comitter:
- vwochnik
- Date:
- Fri Jan 24 13:51:36 2014 +0000
- Parent:
- 18:cf5d7427a9ec
- Child:
- 20:4ea5255c1b04
- Commit message:
- Improve auth, add custom headers
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 |
--- 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));
}
--- a/HTTPClient.h Sun Apr 28 10:04:51 2013 +0000 +++ b/HTTPClient.h Fri Jan 24 13:51:36 2014 +0000 @@ -61,7 +61,6 @@ HTTPClient(); ~HTTPClient(); -#if 1 //TODO add header handlers /** Provides a basic authentification feature (Base64 encoded username and password) Pass two NULL pointers to switch back to no authentication @@ -69,7 +68,14 @@ @param user password to use for authentication, must remain valid durlng the whole HTTP session */ void basicAuth(const char* user, const char* password); //Basic Authentification -#endif + + /** + Set custom headers for request. + Pass NULL, 0 to turn off custom headers. + @param headers an array (size multiple of two) key-value pairs, must remain valid during the whole HTTP session + @param pairs number of key-value pairs + */ + void customHeaders(const char** headers, size_t pairs); //High Level setup functions /** Execute a GET request on the URL @@ -150,6 +156,8 @@ const char* m_basicAuthUser; const char* m_basicAuthPassword; + const char** m_customHeaders; + size_t m_nCustomHeaders; int m_httpResponseCode; };
