int HTTPClient::clientWrite(char byte, FILE* stream) { if (stream == NULL) { return EOF; } http_stream_udata* udata = (http_stream_udata*) fdev_get_udata(stream); HTTPClient* client = udata->client; if (client->connected() == 0) { closeStream(stream); return EOF; } if (udata->encode == 0) { client->write(byte); if (client->debugCommunication) { Serial.print(byte); } } else { if (URI_ALLOWED(byte) || ((URI_RESERVED(byte) && (udata->encode & URI_ENCODE_RESERVED) == 0))) { client->write(byte); if (client->debugCommunication) { Serial.print(byte); } } else { char encoded[4] = { 0, 0, 0 }; sprintf(encoded, "%%%2x", byte); // Write only the first three bytes, not the trailing null for (char i = 0; i < 3; i++) { client->write(encoded[i]); if (client->debugCommunication) { Serial.print(encoded[i]); } } } } return 0; }
int HTTPClient::clientRead(FILE* stream) { if (stream == NULL) { return EOF; } http_stream_udata* udata = (http_stream_udata*) fdev_get_udata(stream); HTTPClient* client = udata->client; if (!client->connected()) { return EOF; } //block until we got a byte while (client->available() == 0) { if (client->connected() == 0) { return EOF; } }; int result = client->read(); if (result == EOF) { return EOF; } if (client->debugCommunication) { Serial.print((byte) result); } //as long as we do not read encoded or it is no % everything is ok if (udata->encode == 0 || result != '%') { return result; } else { //block until we got the needed bytes while (client->available() >= 2) { if (client->connected() == 0) { return EOF; } }; char return_value = 0; for (char i = 0; i < 2; i++) { result = client->read(); if (result == EOF) { return EOF; } else if (result >= 'A' && result <= 'Z') { return_value += (1 - i) * 16 * (result - 'A'); } else if (result >= 'a' && result <= 'z') { return_value += (1 - i) * 16 * (result - 'a'); } else if (result >= '0' && result <= '9') { return_value += (1 - i) * 16 * (result - '0'); } } return return_value; } }
int uploadFile(const char* _filename, const char* _fileurl) { // upload new file to fs by downloading from a remote server, rather than reflash the entire spiffs int ret = false; HTTPClient http; //const char* fileUrl = "http://mypi3/iot/index.html"; //const char* fileName = "/test2.html"; if (hasSerial) Serial.printf("url %s\n", _fileurl); if (hasSerial) Serial.printf("file %s\n", _filename); http.begin(_fileurl); // init http client // start connection and send HTTP header int httpCode = http.GET(); // httpCode will be negative on error if(httpCode > 0) { // HTTP header has been send and Server response header has been handled // file found at server if(httpCode == HTTP_CODE_OK) { if (hasSerial) Serial.printf("HTTP client http status %d\n", httpCode); // get lenght of document (is -1 when Server sends no Content-Length header) int len = http.getSize(); int paysize = len; if (hasSerial) Serial.printf("HTTP content size %d bytes\n", paysize); // create buffer for read uint8_t buff[128] = { 0 }; // get tcp stream WiFiClient * stream = http.getStreamPtr(); // create or recreate file on spiffs File configFile = SPIFFS.open(_filename, "w"); if (!configFile) { if (hasSerial) Serial.printf("Failed to open %s for write.\n",_filename); return ret; } if (hasSerial) Serial.println("File open, write start."); // read all data from server while(http.connected() && (len > 0 || len == -1)) { // get available data size size_t size = stream->available(); if (size) { // read up to 128 byte int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); // write it to Serial configFile.write(buff, c); if(len > 0) { len -= c; } } } // EoF or http connection closed configFile.close(); http.end(); if (hasSerial) Serial.println("File closed, write complete."); return paysize; } else { if (hasSerial) Serial.printf("HTTP client http error %d\n", httpCode); return httpCode; } } else { if (hasSerial) Serial.printf("HTTP client http error %d\n", httpCode); return httpCode; } return 0; }