void readResponse (char *buf, int max_len, int content_len) { // Read content. int len = wifly.readBytes(buf, max_len > content_len ? content_len : max_len); buf[len] = '\0'; // insurance // Flush buffer. while (wifly.available() > 0) { wifly.read(); } }
void readResponseHeaders (int *status_code, int *content_len) { boolean line_start = true; char toss[1], buf[64]; int len; *content_len = *status_code = 0; // HTTP/1.1 xxx if (wifly.readBytes(buf, 9) == 0) { return; } if (wifly.readBytes(buf, 3) == 0) { return; } buf[3] = '\0'; *status_code = strtol(buf, NULL, 10); if (wifly.readBytesUntil('\n', buf, sizeof(buf)) == 0) { return; } while (true) { // Read first character of line. if (wifly.readBytes(buf, 1) == 0) { return; } char ch = buf[0]; if (line_start) { // Beginning content. if (ch == '\r') { // read ...\n\r\n if (wifly.readBytes(buf, 1) == 0) { return; } break; } } // Read header name. len = wifly.readBytesUntil(':', &buf[1], sizeof(buf) - 2); if (len == 0) { return; } buf[len + 1] = '\0'; // space if (wifly.readBytes(toss, 1) == 0) { return; } if (strncmp(buf, "Content-Length", len) == 0) { len = wifly.readBytesUntil('\n', buf, sizeof(buf)); if (len == 0) { return; } buf[len] = '\0'; *content_len = atoi(buf); } else { if (wifly.readBytesUntil('\n', buf, sizeof(buf)) == 0) { return; } } } }