HTTPC_STATUS_T http_get_large(const HTTPC_SESSION_T* session,
const char* url,
HTTP_GET_CALLBACK_T getWriteCallback) {
ASSERT(session != NULL);
ASSERT(session->handle != NULL);
wiced_result_t http_return;
session_info * sinfo = (session_info *)(session->handle);
HTTPC_STATUS_T return_value = HTTPC_ERROR;
if (sinfo->hnd.is_connected == 0) {
http_return = open_session(&sinfo->hnd , url);
}
http_return = http_stream_start_headers(&sinfo->hnd, HTTP_GET, url);
if (http_return == WICED_SUCCESS) {
if (sinfo->username_and_password != NULL) {
http_return = http_send_basic_authorization(&sinfo->hnd,
sinfo->username_and_password);
if (http_return == WICED_SUCCESS) {
if ((sinfo->stg.req_hdr_stg.name != NULL) &&
(sinfo->stg.req_hdr_stg.value != NULL)) {
http_header_t header = {
.name = sinfo->stg.req_hdr_stg.name,
.value = sinfo->stg.req_hdr_stg.value,
};
http_return = http_stream_add_headers(&sinfo->hnd, &header, 1);
if (http_return != WICED_SUCCESS) {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"http_stream_add_headers failed. http_return = %d.\n",
url, http_return);
}
}
} else {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"http_send_basic_authorization failed. http_return = %d.\n",
url, http_return);
}
}
} else {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"http_stream_start_headers failed. http_return = %d.\n",
url, http_return);
}
if (http_return == WICED_SUCCESS) {
http_return = http_stream_end_headers(&sinfo->hnd, WICED_TRUE);
if (http_return == WICED_SUCCESS) {
http_return = http_stream_flush(&sinfo->hnd);
if (http_return == WICED_SUCCESS) {
wiced_packet_t *packet;
http_return = http_stream_receive(&sinfo->hnd, &packet, sinfo->session_timeout *
MS_PER_SECOND);
if (http_return == WICED_SUCCESS) {
http_status_code_t response_code;
http_return = http_process_response(packet, &response_code);
if (http_return == WICED_SUCCESS) {
sinfo->result_code = response_code;
if (response_code >= 200 && response_code < 302) {
if (sinfo->result_code != HTTP_NO_CONTENT) {
if ((getWriteCallback != NULL)) {
do {
unsigned char *buffer;
uint32_t size;
http_return = http_get_body(packet,
&buffer,
&size);
if (http_return == WICED_SUCCESS) {
if (size > 0)
if ((getWriteCallback(buffer,
http_return,
NULL)) == 0) {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"getWriteCallback() returned error.\n");
break;
}
} else {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"http_get_body failed. url = %s, http_return = %d.\n",
url, http_return);
}
} while (http_return > 0);
//FIXME: Add checking of failed callback
if (http_return == WICED_SUCCESS) {
return_value = HTTPC_SUCCESS;
}
} else {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"http_get_large: getWriteCallback() is NULL.\n");
}
}
} else {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"http request failed. url = %s, status_code = %d.\n",
url, sinfo->result_code);
}
} else {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"http_process_response failed. url = %s, http_return = %d.\n",
url, http_return);
}
} else {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"http_stream_receive failed. url = %s, http_return = %d.\n",
url, http_return);
}
} else {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"http_stream_flush failed. url = %s, http_return = %d.\n",
url, http_return);
}
} else {
DEBUG_LOG(LOG_LEVEL_DEBUG, __FUNCTION__,
"http_stream_end_headers failed. http_return = %d.\n",
url, http_return);
}
}
return return_value;
}
SFRUUENfU1RBVFVTX1QgaHR0cF9nZXRfbGFyZ2UoY29uc3QgSFRUUENfU0VTU0lPTl9UKiBzZXNzaW9uLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb25zdCBjaGFyKiB1cmwsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIEhUVFBfR0VUX0NBTExCQUNLX1QgZ2V0V3JpdGVDYWxsYmFjaykgewogICAgQVNTRVJUKHNlc3Npb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gICAgICAgICAgICAgIC5uYW1lID0gc2luZm8tPnN0Zy5yZXFfaGRyX3N0Zy5uYW1lLAogICAgICAgICAgICAgICAgICAgICAgICAudmFsdWUgPSBzaW5mby0+c3RnLnJlcV9oZHJfc3RnLnZhbHVlLAogICAgICAgICAgICAgICAgICAgIH07CgogICAgICAgICAgICAgICAgICAgIGh0dHBfcmV0dXJuID0gaHR0cF9zdHJlYW1fYWRkX2hlYWRlcnMoJnNpbmZvLT5obmQsICZoZWFkZXIsIDEpOwogICAgICAgICAgICAgICAgICAgIGlmIChodHRwX3JldHVybiAhPSBXSUNFRF9TVUNDRVNTKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIERFQlVHX0xPRyhMT0dfTEVWRUxfREVCVUcsIF9fRlVOQ1RJT05fXywKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAiaHR0cF9zdHJlYW1fYWRkX2hlYWRlcnMgZmFpbGVkLiBodHRwX3JldHVybiA9ICVkLlxuIiwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB1cmwsIGh0dHBfcmV0dXJuKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBERUJVR19MT0coTE9HX0xFVkVMX0RFQlVHLCBfX0ZVTkNUSU9OX18sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICJodHRwX3NlbmRfYmFzaWNfYXV0aG9yaXphdGlvbiBmYWlsZWQuIGh0dHBfcmV0dXJuID0gJWQuXG4iLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB1cmwsIGh0dHBfcmV0dXJuKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0gZWxzZSB7CiAgICAgICAgREVCVUdfTE9HKExPR19MRVZFTF9ERUJVRywgX19GVU5DVElPTl9fLAogICAgICAgICAgICAgICAgICAgICAgImh0dHBfc3RyZWFtX3N0YXJ0X2hlYWRlcnMgZmFpbGVkLiBodHRwX3JldHVybiA9ICVkLlxuIiwKICAgICAgICAgICAgICAgICAgICAgIHVybCwgaHR0cF9yZXR1cm4pOwogICAgfQogICAgaWYgKGh0dHBfcmV0dXJuID09IFdJQ0VEX1NVQ0NFU1MpIHsKICAgICAgICBodHRwX3JldHVybiA9IGh0dHBfc3RyZWFtX2VuZF9oZWFkZXJzKCZzaW5mby0+aG5kLCBXSUNFRF9UUlVFKTsKICAgICAgICBpZiAoaHR0cF9yZXR1cm4gPT0gV0lDRURfU1VDQ0VTUykgewogICAgICAgICAgICBodHRwX3JldHVybiA9IGh0dHBfc3RyZWFtX2ZsdXNoKCZzaW5mby0+aG5kKTsKICAgICAgICAgICAgaWYgKGh0dHBfcmV0dXJuID09IFdJQ0VEX1NVQ0NFU1MpIHsKICAgICAgICAgICAgICAgIHdpY2VkX3BhY2tldF90ICpwYWNrZXQ7CiAgICAgICAgICAgICAgICBodHRwX3JldHVybiA9IGh0dHBfc3RyZWFtX3JlY2VpdmUoJnNpbmZvLT5obmQsICZwYWNrZXQsIHNpbmZvLT5zZXNzaW9uX3RpbWVvdXQgKgogICAgICAgICAgICAgICAgICAgIE1TX1BFUl9TRUNPTkQpOwogICAgICAgICAgICAgICAgaWYgKGh0dHBfcmV0dXJuID09IFdJQ0VEX1NVQ0NFU1MpIHsKICAgICAgICAgICAgICAgICAgICBodHRwX3N0YXR1c19jb2RlX3QgcmVzcG9uc2VfY29kZTsKICAgICAgICAgICAgICAgICAgICBodHRwX3JldHVybiA9IGh0dHBfcHJvY2Vzc19yZXNwb25zZShwYWNrZXQsICZyZXNwb25zZV9jb2RlKTsKICAgICAgICAgICAgICAgICAgICBpZiAoaHR0cF9yZXR1cm4gPT0gV0lDRURfU1VDQ0VTUykgewogICAgICAgICAgICAgICAgICAgICAgICBzaW5mby0+cmVzdWx0X2NvZGUgPSByZXNwb25zZV9jb2RlOwogICAgICAgICAgICAgICAgICAgICAgICBpZiAocmVzcG9uc2VfY29kZSA+PSAyMDAgJiYgcmVzcG9uc2VfY29kZSA8IDMwMikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHNpbmZvLT5yZXN1bHRfY29kZSAhPSBIVFRQX05PX0NPTlRFTlQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoKGdldFdyaXRlQ2FsbGJhY2sgIT0gTlVMTCkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgdW5zaWduZWQgY2hhciAqYnVmZmVyOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgdWludDMyX3Qgc2l6ZTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGh0dHBfcmV0dXJuID0gaHR0cF9nZXRfYm9keShwYWNrZXQsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgJmJ1ZmZlciwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAmc2l6ZSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoaHR0cF9yZXR1cm4gPT0gV0lDRURfU1VDQ0VTUykgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzaXplID4gMCkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKChnZXRXcml0ZUNhbGxiYWNrKGJ1ZmZlciwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGh0dHBfcmV0dXJuLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgTlVMTCkpID09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIERFQlVHX0xPRyhMT0dfTEVWRUxfREVCVUcsIF9fRlVOQ1RJT05fXywKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgImdldFdyaXRlQ2FsbGJhY2soKSByZXR1cm5lZCBlcnJvci5cbiIpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgREVCVUdfTE9HKExPR19MRVZFTF9ERUJVRywgX19GVU5DVElPTl9fLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgImh0dHBfZ2V0X2JvZHkgZmFpbGVkLiB1cmwgPSAlcywgaHR0cF9yZXR1cm4gPSAlZC5cbiIsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB1cmwsIGh0dHBfcmV0dXJuKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSB3aGlsZSAoaHR0cF9yZXR1cm4gPiAwKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy9GSVhNRTogQWRkIGNoZWNraW5nIG9mIGZhaWxlZCBjYWxsYmFjawogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoaHR0cF9yZXR1cm4gPT0gV0lDRURfU1VDQ0VTUykgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuX3ZhbHVlID0gSFRUUENfU1VDQ0VTUzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIERFQlVHX0xPRyhMT0dfTEVWRUxfREVCVUcsIF9fRlVOQ1RJT05fXywKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAiaHR0cF9nZXRfbGFyZ2U6IGdldFdyaXRlQ2FsbGJhY2soKSBpcyBOVUxMLlxuIik7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgREVCVUdfTE9HKExPR19MRVZFTF9ERUJVRywgX19GVU5DVElPTl9fLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAiaHR0cCByZXF1ZXN0IGZhaWxlZC4gdXJsID0gJXMsIHN0YXR1c19jb2RlID0gJWQuXG4iLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB1cmwsIHNpbmZvLT5yZXN1bHRfY29kZSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICBERUJVR19MT0coTE9HX0xFVkVMX0RFQlVHLCBfX0ZVTkNUSU9OX18sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgImh0dHBfcHJvY2Vzc19yZXNwb25zZSBmYWlsZWQuIHVybCA9ICVzLCBodHRwX3JldHVybiA9ICVkLlxuIiwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB1cmwsIGh0dHBfcmV0dXJuKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIERFQlVHX0xPRyhMT0dfTEVWRUxfREVCVUcsIF9fRlVOQ1RJT05fXywKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICJodHRwX3N0cmVhbV9yZWNlaXZlIGZhaWxlZC4gdXJsID0gJXMsIGh0dHBfcmV0dXJuID0gJWQuXG4iLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgdXJsLCBodHRwX3JldHVybik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBERUJVR19MT0coTE9HX0xFVkVMX0RFQlVHLCBfX0ZVTkNUSU9OX18sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICJodHRwX3N0cmVhbV9mbHVzaCBmYWlsZWQuIHVybCA9ICVzLCBodHRwX3JldHVybiA9ICVkLlxuIiwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgdXJsLCBodHRwX3JldHVybik7CiAgICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBERUJVR19MT0coTE9HX0xFVkVMX0RFQlVHLCBfX0ZVTkNUSU9OX18sCiAgICAgICAgICAgICAgICAgICAgICAgICAgImh0dHBfc3RyZWFtX2VuZF9oZWFkZXJzIGZhaWxlZC4gaHR0cF9yZXR1cm4gPSAlZC5cbiIsCiAgICAgICAgICAgICAgICAgICAgICAgICAgdXJsLCBodHRwX3JldHVybik7CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiByZXR1cm5fdmFsdWU7Cn0=