| 223 | } |
| 224 | |
| 225 | void HttpServiceWorker::handleHttpResponse(HTTPClient& httpClient, IHttpResponseHandler* handler, WorkerResponse& response) |
| 226 | { |
| 227 | int32_t contentLength = httpClient.getSize(); /* Get size of the payload. If no Content-Length header present, size will be -1. */ |
| 228 | uint8_t buffer[1024U]; |
| 229 | WiFiClient& stream = httpClient.getStream(); |
| 230 | uint32_t index = 0U; |
| 231 | |
| 232 | while ((true == httpClient.connected()) && ((0 < contentLength) || (-1 == contentLength))) |
| 233 | { |
| 234 | int32_t toRead = stream.available(); |
| 235 | |
| 236 | if (0 < toRead) |
| 237 | { |
| 238 | char* cBuffer = static_cast<char*>(static_cast<void*>(buffer)); |
| 239 | int32_t read = stream.readBytes(cBuffer, sizeof(buffer)); |
| 240 | |
| 241 | if (0 < read) |
| 242 | { |
| 243 | if (0 < contentLength) |
| 244 | { |
| 245 | if (read > contentLength) |
| 246 | { |
| 247 | contentLength = 0; |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | contentLength -= read; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* If a response handler is provided, call it to process the received payload chunk. */ |
| 256 | if (nullptr != handler) |
| 257 | { |
| 258 | bool isFinal = (0 == contentLength); |
| 259 | |
| 260 | handler->onResponse(index, isFinal, buffer, static_cast<size_t>(read)); |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | /* Append data to the response payload. */ |
| 265 | response.append(buffer, static_cast<size_t>(read)); |
| 266 | } |
| 267 | |
| 268 | ++index; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* Give other tasks a chance to run. */ |
| 273 | delay(1U); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /****************************************************************************** |
| 278 | * External Functions |
nothing calls this directly
no test coverage detected