Response Headers not there when GET request
Posted: Fri Jan 25, 2019 8:12 am
This is on an ESP32 but I am hoping that as they are much the same libraries I might find some help here.
I am sending a GET and try to read the headers and use http.headers() and get zero as the response.
I can use the http.getSize() and it works and gets the correct size from the "Content-Length" header.
I am trying to get a "Content-MD5" header as I'm downloading a firmware file and doing a check before approving the download.
I am using the WiFiClientSecure to connect to my sever so the download is encrypted.
The url going to a .php file on my server and it then transmits the .bin file with the following headers
Using Google Chrome in the Network tab I am able to see the Response Headers including the "Content-MD5" so it at least works on chrome but the ESP doesn't see any of them.
Thanks in advance
I am sending a GET and try to read the headers and use http.headers() and get zero as the response.
I can use the http.getSize() and it works and gets the correct size from the "Content-Length" header.
I am trying to get a "Content-MD5" header as I'm downloading a firmware file and doing a check before approving the download.
I am using the WiFiClientSecure to connect to my sever so the download is encrypted.
The url going to a .php file on my server and it then transmits the .bin file with the following headers
Code: Select all
header("Content-MD5:" . strtoupper(bin2hex(md5_file($firmware, true))));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($firmware));
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($firmware));
Using Google Chrome in the Network tab I am able to see the Response Headers including the "Content-MD5" so it at least works on chrome but the ESP doesn't see any of them.
Code: Select all
const char * headerKeys[] = {"date", "server"} ;
const size_t numberOfHeaders = 2;
boolean downloadFirmware(String url, String fileName){
boolean serverConnected = true;
WiFiClientSecure client; //create new class for downloading the firmware from webserver
if(serverConnected == true){
HTTPClient httpsDownload; //create the http client
if(httpsDownload.begin(client,url)){ //Start the http client useing the secure client class to the requested url
http.collectHeaders(headerKeys, numberOfHeaders);
int httpCode = httpsDownload.GET(); //start the connection and send the HTTP header
Serial.println(httpsDownload.headers());
if(httpCode >0){
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
if(httpCode == HTTP_CODE_OK){
int len = httpsDownload.getSize(); //get the size of the file to be downloaded from the headers
Serial.print("File size: ");
Serial.println(len);
for(int i = 0; i< http.headers(); i++){
Serial.println(http.header(i));
}
String headerDate = http.header("date");
Serial.println(headerDate);
if(SPIFFS.totalBytes() > len){ //if there is enough space in the spi flash
File file = SPIFFS.open(fileName, FILE_WRITE); //open the file to be written
if(file){ //if the file opens
WiFiClient * stream = &client; // get TCP stream
uint8_t buff[255] = { 0 }; //create buffer to hold downloaded data
MD5_CTX md; //needed for the checksum
MD5 md5; //needed for the checksum
md5.MD5Init(&md); //setup the md5 hash for the checksum
//While there is still data or data to be send then download it
while(httpsDownload.connected() && len > 0 || len == -1){
size_t size = stream->available(); //read the amount of data that is able to be downloaded
if (size) { //if there is some data to download
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); // read up to 1024 bytes
file.write(buff,c); //write the data to the file on the SPI flash
md5.MD5Update(&md, buff, c); //update the MD5 hash for the checksum
//decrement the amount of data downloaded so far from the total size of the file on the server
if (len > 0) { len -= c; }
}
delay(1); //delay for if no data is available so we wait a little
}
file.close(); //close the file so we can use it later
unsigned char check[16]; //Holds the MD5 Checksum
md5.MD5Final(check, &md); //finish the calculation for the cheksum
Serial.print("Checksum: ");
for(int i=0;i<16;i++){
Serial.print(check[i],HEX);
}
Serial.println();
}else{
errorMessage("FS: error opening the file to write firmware");
return false;
}
}else{
errorMessage("FLASH: not enough space on the flash chip");
return false;
}
}
}else{
errorMessage("HTTPS: Error from webserver");
errorMessage(String(httpCode));
return false;
}
}else{
errorMessage("HTTPS: Error connecting to the server for firmware download");
return false;
}
httpsDownload.end(); //close the connection once finished
return true;
}
}
Thanks in advance