| 224 | } |
| 225 | |
| 226 | void HttpClient::sendBasicAuth(const char* aUser, const char* aPassword) |
| 227 | { |
| 228 | // Send the initial part of this header line |
| 229 | iClient->print("Authorization: Basic "); |
| 230 | // Now Base64 encode "aUser:aPassword" and send that |
| 231 | // This seems trickier than it should be but it's mostly to avoid either |
| 232 | // (a) some arbitrarily sized buffer which hopes to be big enough, or |
| 233 | // (b) allocating and freeing memory |
| 234 | // ...so we'll loop through 3 bytes at a time, outputting the results as we |
| 235 | // go. |
| 236 | // In Base64, each 3 bytes of unencoded data become 4 bytes of encoded data |
| 237 | unsigned char input[3]; |
| 238 | unsigned char output[5]; // Leave space for a '\0' terminator so we can easily print |
| 239 | int userLen = strlen(aUser); |
| 240 | int passwordLen = strlen(aPassword); |
| 241 | int inputOffset = 0; |
| 242 | for (int i = 0; i < (userLen+1+passwordLen); i++) |
| 243 | { |
| 244 | // Copy the relevant input byte into the input |
| 245 | if (i < userLen) |
| 246 | { |
| 247 | input[inputOffset++] = aUser[i]; |
| 248 | } |
| 249 | else if (i == userLen) |
| 250 | { |
| 251 | input[inputOffset++] = ':'; |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | input[inputOffset++] = aPassword[i-(userLen+1)]; |
| 256 | } |
| 257 | // See if we've got a chunk to encode |
| 258 | if ( (inputOffset == 3) || (i == userLen+passwordLen) ) |
| 259 | { |
| 260 | // We've either got to a 3-byte boundary, or we've reached then end |
| 261 | b64_encode(input, inputOffset, output, 4); |
| 262 | // NUL-terminate the output string |
| 263 | output[4] = '\0'; |
| 264 | // And write it out |
| 265 | iClient->print((char*)output); |
| 266 | // FIXME We might want to fill output with '=' characters if b64_encode doesn't |
| 267 | // FIXME do it for us when we're encoding the final chunk |
| 268 | inputOffset = 0; |
| 269 | } |
| 270 | } |
| 271 | // And end the header we've sent |
| 272 | iClient->println(); |
| 273 | } |
| 274 | |
| 275 | void HttpClient::finishHeaders() |
| 276 | { |
nothing calls this directly
no test coverage detected