MCPcopy Create free account
hub / github.com/Heltec-Aaron-Lee/WiFi_Kit_series / writeToStream

Method writeToStream

libraries/HTTPClient/src/HTTPClient.cpp:864–942  ·  view source on GitHub ↗

* write all message body / payload to Stream * @param stream Stream * * @return bytes written ( negative values are error codes ) */

Source from the content-addressed store, hash-verified

862 * @return bytes written ( negative values are error codes )
863 */
864int HTTPClient::writeToStream(Stream *stream) {
865
866 if (!stream) {
867 return returnError(HTTPC_ERROR_NO_STREAM);
868 }
869
870 if (!connected()) {
871 return returnError(HTTPC_ERROR_NOT_CONNECTED);
872 }
873
874 // get length of document (is -1 when Server sends no Content-Length header)
875 int len = _size;
876 int ret = 0;
877
878 if (_transferEncoding == HTTPC_TE_IDENTITY) {
879 ret = writeToStreamDataBlock(stream, len);
880
881 // have we an error?
882 if (ret < 0) {
883 return returnError(ret);
884 }
885 } else if (_transferEncoding == HTTPC_TE_CHUNKED) {
886 int size = 0;
887 while (1) {
888 if (!connected()) {
889 return returnError(HTTPC_ERROR_CONNECTION_LOST);
890 }
891 String chunkHeader = _client->readStringUntil('\n');
892
893 if (chunkHeader.length() <= 0) {
894 return returnError(HTTPC_ERROR_READ_TIMEOUT);
895 }
896
897 chunkHeader.trim(); // remove \r
898
899 // read size of chunk
900 len = (uint32_t)strtol((const char *)chunkHeader.c_str(), NULL, 16);
901 size += len;
902 log_v(" read chunk len: %d", len);
903
904 // data left?
905 if (len > 0) {
906 int r = writeToStreamDataBlock(stream, len);
907 if (r < 0) {
908 // error in writeToStreamDataBlock
909 return returnError(r);
910 }
911 ret += r;
912 } else {
913
914 // if no length Header use global chunk size
915 if (_size <= 0) {
916 _size = size;
917 }
918
919 // check if we have write all data out
920 if (ret != _size) {
921 return returnError(HTTPC_ERROR_STREAM_WRITE);

Callers

nothing calls this directly

Calls 6

delayFunction · 0.85
c_strMethod · 0.80
readStringUntilMethod · 0.45
lengthMethod · 0.45
trimMethod · 0.45
readBytesMethod · 0.45

Tested by

no test coverage detected