Thanks for helping me.
Here is the code that calculates the response and do the parsing in look for the handshake. It is still pretty primitive but I wanted to get it to work first, at least for chrome, and then I would try to cover more cases.
static bool decodeHttpMessage (char * inputMessage, char * outputMessage, uint32_t *outputLength)
{
volatile const char str1[98] = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ";
volatile const char s[3] = "\r\n";
volatile const char str2[5] = "\r\n\r\n";
volatile char *tokens[12];
volatile uint32_t index = 1;
volatile uint32_t i;
volatile char key [24]; //24 bytes
volatile char magic[36] = { '2', '5', '8', 'E', 'A', 'F', 'A', '5', '-', 'E', '9', '1', '4', '-', '4', '7', 'D', 'A', '-', '9', '5', 'C', 'A', '-', 'C', '5', 'A', 'B', '0', 'D', 'C', '8', '5', 'B', '1', '1'};
volatile char sha1Input [60];
volatile char sha1Output [20];
volatile char encodedSha1 [40];
volatile uint32_t encodedLength;
//Split the message into substrings to identify it
tokens[0] = strtok(inputMessage, s);
while( (tokens[index-1] != NULL)&& (index < 12) )
{
tokens[index] = strtok(NULL, s);
index ++;
}
//Get the key and the type of message
if (strncmp(tokens[0], "GET ", 4) == 0)
{
for (index = 1; index < 12; index++)
{
if (strncmp(tokens[index], "Connection: ", 12) == 0)
{
if (strncmp(tokens[index] + 12, "Upgrade", 7) == 0)
break;
else
return false;
}
}
}
else
return false;
//It's a websocket request
for (index = 1; index < 12; index++)
{
if (strncmp(tokens[index], "Sec-WebSocket-Key: ", 19) == 0)
{
//assuming key of fixed length (that's how it is supposed to be)
strncpy(key, tokens[index] + 19, 24);
break;
}
}
//Concatenate Strings
for (index = 0; index < 60; index++)
{
if(index < 24)
sha1Input[index] = key[index];
else
sha1Input[index] = magic[index - 24];
}
//Call to SHA function and encode
mbedtls_sha1( &sha1Input[0], 60, &sha1Output[0]);
//It works
mbedtls_base64_encode( &encodedSha1[0], 40, &encodedLength, &sha1Output[0], 20 );
//It seems to work, it may better to calculate the encodedLength first and then allocate the encodedSha1 buffer.
//Fill Output Buffer
outputLength = encodedLength + 97 + 4;
for (index = 0; index < outputLength ; index++)
{
if(index < 97)
*(outputMessage + index) = str1[index];
else if ((index > 96)&& (index < 97 + encodedLength))
*(outputMessage + index) = encodedSha1[index - 97];
else
*(outputMessage + index) = str2[index - 97 - encodedLength];
}
//Add extra /n/r at the end
return true;
}
I know that for example I am missing the protocols. But as I said, I will cover it later...