本帖最后由 wenbodong 于 2019-9-30 14:58 编辑
webclient软件包版本: v2.1.0
get函数如下。
int webclient_get(struct webclient_session *session, const char *URI)
{
int rc = WEBCLIENT_OK;
int resp_status = 0;
RT_ASSERT(session);
RT_ASSERT(URI);
rc = webclient_connect(session, URI);
if (rc != WEBCLIENT_OK)
{
/* connect to webclient server failed. */
return rc;
}
rc = webclient_send_header(session, WEBCLIENT_GET);
if (rc != WEBCLIENT_OK)
{
/* send header to webclient server failed. */
return rc;
}
/* handle the response header of webclient server */
resp_status = webclient_handle_response(session);
LOG_D("get position handle response(%d).", resp_status);
if (resp_status > 0)
{
const char *location = webclient_header_fields_get(session, "Location");
/* relocation */
if ((resp_status == 302 || resp_status == 301) && location)
{
char *new_url;
new_url = web_strdup(location);
if (new_url == RT_NULL)
{
return -WEBCLIENT_NOMEM;
}
/* close old client session */
webclient_close(session);
/* create new client session by location url */
session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
if (session == RT_NULL)
{
return -WEBCLIENT_NOMEM;
}
rc = webclient_get(session, new_url);
web_free(new_url);
return rc;
}
}
return resp_status;
}
在接收到服务器的重定向指示后会重新创建session。
if ((resp_status == 302 || resp_status == 301) && location)
{
char *new_url;
new_url = web_strdup(location);
if (new_url == RT_NULL)
{
return -WEBCLIENT_NOMEM;
}
/* close old client session */
webclient_close(session);
/* create new client session by location url */
session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
但是这是在函数内部重建创建啊,外层函数的session还是原来的(而且还被关闭了。。。)。后续使用其他函数时,如webclient_read,感觉会出问题啊。
一时没想到怎么测试重定向,只是看到这个实现觉得有内存问题。如果开发者在测试时没遇到问题,也可能是个巧合。即:为新session分配的内存的首地址与原session一致(因为是先释放原session后紧接着创建新session)。
查看更多