I am writing a class file to create a Http Digest authentication response string. It is doing something weird when I print it out to the serial port, and I don't understand the reason for it.
HttpAuth.h
#ifndef HttpAuth_h
#define HttpAuth_h
// the #include statment and code go here...
#include "Arduino.h"
class HttpAuth
{
public:
HttpAuth();
static void DigestGenerateResponse(char * username, char * password, char * realm,
char * method, char * uri, char * nounce, char * qop,
char * nounceCount, char * clientNounce);
};
#endif
HttpAuth.cpp
#include "HttpAuth.h"
void HttpAuth::DigestGenerateResponse(char * username, char * password, char * realm,
char * method, char * uri, char * nounce, char * qop,
char * nounceCount, char * clientNounce)
{
char * aOneString;
char * aTwoString;
// Build the a1String
aOneString = (char*) malloc((sizeof(username)+1));
strcpy(aOneString, username);
strcat(aOneString, ":");
strcat(aOneString, realm);
strcat(aOneString, ":");
strcat(aOneString, password);
// Build the a2String
aTwoString = (char*) malloc((sizeof(method)+1));
strcpy(aTwoString, method);
strcat(aTwoString, ":");
strcat(aTwoString, uri);
delay(1000);
Serial.println(aOneString);
delay(1000);
Serial.println(aTwoString);
}
If I look at the serial monitor I get the result of.
Start
the
GET:/test/testing
when I am really expecting
Start
thenetimp:Bedroom:g@rd3nh0s3
GET:/test/testing
If I remove the code for aTwoString then I get the right return for aOneString so it seems to me like malloc of my 2nd string is interfiering with the 1st string. I don't Understand why it would do that. I wonder if it has to do with me passing in pointers, my C/C++ is not very strong. Can someone please point me in the right direction?
HTTPAuthTest.ino.
#include "HttpAuth.h"
void setup()
{
Serial.begin(9600);
delay(1000);
Serial.println("start");
HttpAuth::DigestGenerateResponse((char*)"thenetimp", (char*)"g@rd3nh0s3", (char*)"Bedroom",
(char*)"GET", (char*)"/test/testing", (char*)"12345678910", (char*)"auth", (char*)"", (char*)"");
}
void loop()
{
}
void print_testing(char * stringy)
{
char * testing2;
testing2 = (char*) malloc(11);
strcpy(testing2, stringy);
Serial.println(testing2);
}