@@ -14,11 +14,11 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
14
14
Serial.print (F (" beginClient: Socket is already open" ));
15
15
if (*connectionIsOpen)
16
16
{
17
- Serial.print (F (" and the connection is open!" ));
17
+ Serial.println (F (" and the connection is open!" ));
18
18
}
19
19
else
20
20
{
21
- Serial.print (F (" !" ));
21
+ Serial.println (F (" !" ));
22
22
}
23
23
return (false );
24
24
}
@@ -56,23 +56,28 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
56
56
57
57
// Set up the server request (GET)
58
58
const int SERVER_BUFFER_SIZE = 512 ;
59
- char serverRequest[SERVER_BUFFER_SIZE];
59
+ char *serverRequest = new char [SERVER_BUFFER_SIZE];
60
+ memset (serverRequest, 0 , SERVER_BUFFER_SIZE);
60
61
snprintf (serverRequest,
61
62
SERVER_BUFFER_SIZE,
62
63
" GET /%s HTTP/1.0\r\n User-Agent: NTRIP SparkFun u-blox Client v1.0\r\n " ,
63
64
mountPoint);
64
65
65
66
// Set up the credentials
66
- char credentials[512 ];
67
+ const int CREDENTIALS_BUFFER_SIZE = 512 ;
68
+ char *credentials = new char [CREDENTIALS_BUFFER_SIZE];
69
+ memset (credentials, 0 , CREDENTIALS_BUFFER_SIZE);
67
70
if (strlen (casterUser) == 0 )
68
71
{
69
- strncpy (credentials, " Accept: */*\r\n Connection: close\r\n " , sizeof (credentials) );
72
+ strncpy (credentials, " Accept: */*\r\n Connection: close\r\n " , CREDENTIALS_BUFFER_SIZE );
70
73
}
71
74
else
72
75
{
73
76
// Pass base64 encoded user:pw
74
- char userCredentials[sizeof (casterUser) + sizeof (casterUserPW) + 1 ]; // The ':' takes up a spot
75
- snprintf (userCredentials, sizeof (userCredentials), " %s:%s" , casterUser, casterUserPW);
77
+ int userCredentialsSize = sizeof (casterUser) + sizeof (casterUserPW) + 1 ; // The ':' takes up a spot
78
+ char *userCredentials = new char [userCredentialsSize];
79
+ memset (userCredentials, 0 , userCredentialsSize);
80
+ snprintf (userCredentials, userCredentialsSize, " %s:%s" , casterUser, casterUserPW);
76
81
77
82
Serial.print (F (" beginClient: Sending credentials: " ));
78
83
Serial.println (userCredentials);
@@ -81,20 +86,27 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
81
86
// Encode with ESP32 built-in library
82
87
base64 b;
83
88
String strEncodedCredentials = b.encode (userCredentials);
84
- char encodedCredentials[strEncodedCredentials.length () + 1 ];
85
- strEncodedCredentials.toCharArray (encodedCredentials, sizeof (encodedCredentials)); // Convert String to char array
89
+ int encodedCredentialsSize = strEncodedCredentials.length () + 1 ;
90
+ char *encodedCredentials = new char [encodedCredentialsSize];
91
+ memset (encodedCredentials, 0 , encodedCredentialsSize);
92
+ strEncodedCredentials.toCharArray (encodedCredentials, encodedCredentialsSize); // Convert String to char array
86
93
#elif defined(ARDUINO_ARCH_APOLLO3) || defined(ARDUINO_ARDUINO_NANO33BLE)
87
- char encodedCredentials[sizeof (userCredentials) * 8 ];
94
+ int encodedCredentialsSize = userCredentialsSize * 8 ;
95
+ char *encodedCredentials = new char [encodedCredentialsSize];
96
+ memset (encodedCredentials, 0 , encodedCredentialsSize);
88
97
size_t olen;
89
- mbedtls_base64_encode ((unsigned char *)encodedCredentials, sizeof (userCredentials) * 8 , &olen, (const unsigned char *)userCredentials, strlen (userCredentials));
98
+ mbedtls_base64_encode ((unsigned char *)encodedCredentials, encodedCredentialsSize , &olen, (const unsigned char *)userCredentials, strlen (userCredentials));
90
99
#else
91
100
// Encode with nfriendly library
92
- int encodedLen = base64_enc_len (strlen (userCredentials));
93
- char encodedCredentials[encodedLen]; // Create array large enough to house encoded data
101
+ int encodedLen = base64_enc_len (userCredentialsSize);
102
+ char *encodedCredentials = new char [encodedLen]; // Create array large enough to house encoded data
103
+ memset (encodedCredentials, 0 , encodedLen);
94
104
base64_encode (encodedCredentials, userCredentials, strlen (userCredentials)); // Note: Input array is consumed
95
105
#endif
96
106
97
- snprintf (credentials, sizeof (credentials), " Authorization: Basic %s\r\n " , encodedCredentials);
107
+ snprintf (credentials, CREDENTIALS_BUFFER_SIZE, " Authorization: Basic %s\r\n " , encodedCredentials);
108
+ delete[] userCredentials;
109
+ delete[] encodedCredentials;
98
110
}
99
111
100
112
// Add the encoded credentials to the server request
@@ -104,7 +116,7 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
104
116
Serial.print (F (" beginClient: serverRequest size: " ));
105
117
Serial.print (strlen (serverRequest));
106
118
Serial.print (F (" of " ));
107
- Serial.print (sizeof (serverRequest) );
119
+ Serial.print (SERVER_BUFFER_SIZE );
108
120
Serial.println (F (" bytes available" ));
109
121
110
122
// Send the server request
@@ -123,6 +135,8 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
123
135
{
124
136
Serial.println (F (" beginClient: Caster timed out!" ));
125
137
closeConnection (theSocket, connectionIsOpen);
138
+ delete[] serverRequest;
139
+ delete[] credentials;
126
140
return (false );
127
141
}
128
142
delay (100 );
@@ -134,12 +148,12 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
134
148
135
149
// Check reply
136
150
int connectionResult = 0 ;
137
- char response[512 * 4 ];
151
+ char * response = new char [512 * 4 ];
138
152
memset (response, 0 , 512 * 4 );
139
153
size_t responseSpot = 0 ;
140
154
while ((availableLength > 0 ) && (connectionResult == 0 )) // Read bytes from the caster and store them
141
155
{
142
- if ((responseSpot + availableLength) >= (sizeof (response ) - 1 )) // Exit the loop if we get too much data
156
+ if ((responseSpot + availableLength) >= (( 512 * 4 ) - 1 )) // Exit the loop if we get too much data
143
157
break ;
144
158
145
159
mySARA.socketRead (*theSocket, availableLength, &response[responseSpot]);
@@ -172,6 +186,11 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
172
186
173
187
// Serial.print(F("beginClient: Caster responded with: ")); Serial.println(response); // Uncomment this line to see the full response
174
188
189
+ // Free the memory allocated for serverRequest, credentials and response
190
+ delete[] serverRequest;
191
+ delete[] credentials;
192
+ delete[] response;
193
+
175
194
if (connectionResult != 200 )
176
195
{
177
196
Serial.print (F (" beginClient: Failed to connect to " ));
0 commit comments