Problem now is the Content-length... How do I calculate it ?
Do you absolutely need it? If you do, instead of building up the entire content first, I suggest two passes.
First pass outputs to a dummy class which simply calculates the length, second pass actually outputs. Something like this:
// outputting class that just counts output
class DummyOutput : public Print
{
private:
unsigned long length_;
public:
void begin ()
{
length_ = 0;
}
virtual size_t write (byte c)
{
length_++;
return 1;
}
unsigned long getLength () const { return length_; }
}; // end of DummyOutput
DummyOutput countOutput; // instance of DummyOutput
void setup ()
{
Serial.begin (115200);
Serial.println ();
Serial.println ("Starting");
Print * outputter; // where to write to
for (int pass = 1; pass <= 2; pass++)
{
randomSeed (1);
switch (pass)
{
case 1: outputter = &countOutput;
countOutput.begin ();
break;
case 2: outputter = &Serial;
break;
} // end of switch
// generate some output
for (int i = 0; i < 25; i++)
{
outputter->print (random (5000));
outputter->print ('\n');
}
switch (pass)
{
case 1: Serial.print (F("Content-length: "));
Serial.println (countOutput.getLength ());
break;
case 2: break;
} // end of switch
} // end of for loop
} // end of setup
void loop ()
{
} // end of loop
Output:
Starting
Content-length: 121
1807
249
73
3658
3930
1272
2544
878
2923
2709
4440
3165
4492
3042
2987
2503
2327
1729
3840
2612
4303
3169
2709
2157
4560
By generating the same output in two passes you can first just calculate the length (without outputting) and the second time you can output that length first, and then output the actual data.