Merge pull request #21 from chenyong111/master

【添加】webclient GET/POST 示例代码。
master
朱天龙 (Armink) 7 years ago committed by GitHub
commit 64891bc5c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,9 +1,13 @@
from building import * from building import *
cwd = GetCurrentDir() cwd = GetCurrentDir()
src = Glob('*.c') path = [cwd + '/inc']
CPPPATH = [cwd]
group = DefineGroup('WebClient', src, depend = ['PKG_USING_WEBCLIENT'], CPPPATH = CPPPATH) src = Glob('src/*.c')
if GetDepend(['WEBCLIENT_USING_SAMPLES']):
src += Glob('samples/*.c')
group = DefineGroup('WebClient', src, depend = ['PKG_USING_WEBCLIENT'], CPPPATH = path)
Return('group') Return('group')

@ -24,6 +24,7 @@
* 2017-12-23 aozima update gethostbyname to getaddrinfo. * 2017-12-23 aozima update gethostbyname to getaddrinfo.
* 2018-01-04 aozima add ipv6 address support. * 2018-01-04 aozima add ipv6 address support.
* 2018-07-26 chenyong modify log information * 2018-07-26 chenyong modify log information
* 2018-08-07 chenyong modify header processing
*/ */
#ifndef __WEBCLIENT_H__ #ifndef __WEBCLIENT_H__
@ -96,59 +97,43 @@ enum WEBCLIENT_METHOD
WEBCLIENT_POST, WEBCLIENT_POST,
}; };
struct webclient_header
{
char *buffer;
size_t length; /* content header buffer size */
size_t size; /* maximum support header size */
};
struct webclient_session struct webclient_session
{ {
/* the session socket */ struct webclient_header *header; /* webclient response header information */
int socket; int socket;
/* the response code of HTTP request */ int resp_status;
int response;
char *host; /* server host */
char *req_url; /* HTTP request address*/
/* transfer encoding */
char *transfer_encoding;
int chunk_sz; int chunk_sz;
int chunk_offset; int chunk_offset;
/* content_type of HTTP response */
char *content_type;
/* content_length of HTTP response */
int content_length; int content_length;
size_t content_remainder; /* remainder of content length */
/* last modified timestamp of resource */
char *last_modified;
/* location */
char *location;
/* server host */
char *host;
/* HTTP request */
char *request;
/* position of reading */
unsigned int position;
/* remainder of content reading */
size_t content_length_remainder;
int header_sz;
int resp_sz;
#ifdef WEBCLIENT_USING_TLS #ifdef WEBCLIENT_USING_TLS
/* mbedtls connect session */ MbedTLSSession *tls_session; /* mbedtls connect session */
MbedTLSSession *tls_session;
#endif #endif
}; };
/* create webclient session and set header response size */ /* create webclient session and set header response size */
struct webclient_session *webclient_session_create(size_t header_sz, size_t resp_sz); struct webclient_session *webclient_session_create(size_t header_sz);
/* send HTTP GET request */ /* send HTTP GET request */
int webclient_get(struct webclient_session *session, const char *URI, const char *header); int webclient_get(struct webclient_session *session, const char *URI);
int webclient_get_position(struct webclient_session *session, const char *URI, int position); int webclient_get_position(struct webclient_session *session, const char *URI, int position);
/* send HTTP POST request */ /* send HTTP POST request */
int webclient_post(struct webclient_session *session, const char *URI, int webclient_post(struct webclient_session *session, const char *URI, const char *post_data);
const char *header, const char *post_data);
/* close and release wenclient session */ /* close and release wenclient session */
int webclient_close(struct webclient_session *session); int webclient_close(struct webclient_session *session);
@ -159,9 +144,14 @@ int webclient_set_timeout(struct webclient_session *session, int millisecond);
int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t size); int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t size);
int webclient_write(struct webclient_session *session, const unsigned char *buffer, size_t size); int webclient_write(struct webclient_session *session, const unsigned char *buffer, size_t size);
/* webclient GET/POST header buffer operate by the header fields */
int webclient_header_fields_add(struct webclient_session *session, const char *fmt, ...);
const char *webclient_header_fields_get(struct webclient_session *session, const char *fields);
/* send HTTP POST/GET request, and get response data */ /* send HTTP POST/GET request, and get response data */
int webclient_response(struct webclient_session *session, void **response); int webclient_response(struct webclient_session *session, unsigned char **response);
int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **result); int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **response);
int webclient_resp_status_get(struct webclient_session *session);
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
/* file related operations */ /* file related operations */

@ -0,0 +1,161 @@
/*
* File : webclient_get_sample.c
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-08-03 chenyong the first version
*/
#include <rtthread.h>
#include <webclient.h>
#define GET_HEADER_BUFSZ 1024
#define GET_RESP_BUFSZ 1024
#define GET_LOCAL_URI "http://www.rt-thread.com/service/rt-thread.txt"
int webclient_get_test(int argc, char **argv)
{
struct webclient_session* session = RT_NULL;
unsigned char *buffer = RT_NULL;
char *URI = RT_NULL;
int index, ret = 0;
int bytes_read, resp_status;
int content_length = -1;
if (argc == 1)
{
URI = web_strdup(GET_LOCAL_URI);
if(URI == RT_NULL)
{
rt_kprintf("no memory for create URI buffer.\n");
return -1;
}
}
else if (argc == 2)
{
URI = web_strdup(argv[1]);
if(URI == RT_NULL)
{
rt_kprintf("no memory for create URI buffer.\n");
return -1;
}
}
else
{
rt_kprintf("webclient_get_test [URI] - webclient GET request test.\n");
return -1;
}
buffer = (unsigned char *) web_malloc(GET_HEADER_BUFSZ);
if (buffer == RT_NULL)
{
rt_kprintf("no memory for receive buffer.\n");
ret = -RT_ENOMEM;
goto __exit;
}
/* create webclient session and set header response size */
session = webclient_session_create(GET_HEADER_BUFSZ);
if (session == RT_NULL)
{
ret = -RT_ENOMEM;
goto __exit;
}
/* send GET request by default header */
if ((resp_status = webclient_get(session, URI)) != 200)
{
rt_kprintf("webclient GET request failed, response(%d) error.\n", resp_status);
ret = -RT_ERROR;
goto __exit;
}
rt_kprintf("webclient GET request response data :");
if(webclient_header_fields_get(session, "Content-Length"))
{
content_length = atoi(webclient_header_fields_get(session, "Content-Length"));
}
if (content_length < 0)
{
rt_kprintf("The webclient GET request type is chunked.\n");
do
{
bytes_read = webclient_read(session, buffer, GET_RESP_BUFSZ);
if (bytes_read <= 0)
{
break;
}
for (index = 0; index < bytes_read; index++)
{
rt_kprintf("%c", buffer[index]);
}
} while (1);
rt_kprintf("\n");
}
else
{
int content_pos = 0;
do
{
bytes_read = webclient_read(session, buffer, GET_RESP_BUFSZ);
if (bytes_read <= 0)
{
break;
}
for (index = 0; index < bytes_read; index++)
{
rt_kprintf("%c", buffer[index]);
}
content_pos += bytes_read;
} while (content_pos < content_length);
rt_kprintf("\n");
}
__exit:
if (session)
{
webclient_close(session);
}
if (buffer)
{
web_free(buffer);
}
if (URI)
{
web_free(URI);
}
return ret;
}
#ifdef FINSH_USING_MSH
#include <finsh.h>
MSH_CMD_EXPORT_ALIAS(webclient_get_test, web_get_test, web_get_test [URI] webclient GET request test);
#endif /* FINSH_USING_MSH */

@ -0,0 +1,136 @@
/*
* File : webclient_post_sample.c
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-08-03 chenyong the first version
*/
#include <string.h>
#include <rtthread.h>
#include <webclient.h>
#define POST_RESP_BUFSZ 1024
#define POST_HEADER_BUFSZ 1024
#define POST_LOCAL_URI "http://www.rt-thread.com/service/echo"
const char *post_data = "RT-Thread is an open source IoT operating system from China!";
int webclient_post_test(int argc, char **argv)
{
struct webclient_session* session = RT_NULL;
unsigned char *buffer = RT_NULL;
char *URI = RT_NULL;
int index, ret = 0;
int bytes_read, resp_status;
if (argc == 1)
{
URI = web_strdup(POST_LOCAL_URI);
if(URI == RT_NULL)
{
rt_kprintf("no memory for create URI buffer.\n");
return -1;
}
}
else if (argc == 2)
{
URI = web_strdup(argv[1]);
if(URI == RT_NULL)
{
rt_kprintf("no memory for create URI buffer.\n");
return -1;
}
}
else
{
rt_kprintf("webclient_post_test [URI] - webclient POST request test.\n");
return -1;
}
buffer = (unsigned char *) web_malloc(POST_RESP_BUFSZ);
if (buffer == RT_NULL)
{
rt_kprintf("no memory for receive response buffer.\n");
ret = -RT_ENOMEM;
goto __exit;
}
/* create webclient session and set header response size */
session = webclient_session_create(POST_HEADER_BUFSZ);
if (session == RT_NULL)
{
ret = -RT_ENOMEM;
goto __exit;
}
/* build header for upload */
webclient_header_fields_add(session, "Content-Length: %d\r\n", strlen(post_data));
webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n");
/* send POST request by default header */
if ((resp_status = webclient_post(session, URI, post_data)) != 200)
{
rt_kprintf("webclient POST request failed, response(%d) error.\n", resp_status);
ret = -RT_ERROR;
goto __exit;
}
LOG_I("webclient POST request response data :");
do
{
bytes_read = webclient_read(session, buffer, POST_RESP_BUFSZ);
if (bytes_read <= 0)
{
break;
}
for (index = 0; index < bytes_read; index++)
{
rt_kprintf("%c", buffer[index]);
}
} while (1);
rt_kprintf("\n");
__exit:
if (session)
{
webclient_close(session);
}
if (buffer)
{
web_free(buffer);
}
if (URI)
{
web_free(URI);
}
return ret;
}
#ifdef FINSH_USING_MSH
#include <finsh.h>
MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post_test, webclient_post_test [URI] - webclient POST request test.);
#endif /* FINSH_USING_MSH */

@ -24,12 +24,13 @@
* 2017-12-23 aozima update gethostbyname to getaddrinfo. * 2017-12-23 aozima update gethostbyname to getaddrinfo.
* 2018-01-04 aozima add ipv6 address support. * 2018-01-04 aozima add ipv6 address support.
* 2018-07-26 chenyong modify log information * 2018-07-26 chenyong modify log information
* 2018-08-07 chenyong modify header processing
*/ */
#include <string.h> #include <string.h>
#include <sys/time.h> #include <sys/time.h>
#include "webclient.h" #include <webclient.h>
#if defined(RT_USING_DFS_NET) || defined(SAL_USING_POSIX) #if defined(RT_USING_DFS_NET) || defined(SAL_USING_POSIX)
#include <netdb.h> #include <netdb.h>
@ -68,45 +69,10 @@ static int webclient_recv(struct webclient_session* session, unsigned char *buff
return recv(session->socket, buffer, len, flag); return recv(session->socket, buffer, len, flag);
} }
static char *webclient_header_skip_prefix(char *line, const char *prefix)
{
char *ptr;
size_t len;
RT_ASSERT(line);
RT_ASSERT(prefix);
len = strlen(prefix);
if (strncmp(line, prefix, len))
return RT_NULL;
ptr = line + len;
/* skip whitespace */
while (*ptr && (*ptr == ' ' || *ptr == '\t'))
ptr += 1;
/* remove '\r\n' */
line = ptr;
ptr = strstr(line, "\r\n");
if (ptr != RT_NULL)
{
*ptr = '\0';
}
return line;
}
/*
* When a request has been sent, we can expect mime headers to be
* before the data. We need to read exactly to the end of the headers
* and no more data. This readline reads a single char at a time.
*/
static int webclient_read_line(struct webclient_session *session, char *buffer, int size) static int webclient_read_line(struct webclient_session *session, char *buffer, int size)
{ {
char *ptr = buffer;
int rc, count = 0; int rc, count = 0;
char ch = 0, last_ch = 0;
RT_ASSERT(session); RT_ASSERT(session);
RT_ASSERT(buffer); RT_ASSERT(buffer);
@ -114,7 +80,7 @@ static int webclient_read_line(struct webclient_session *session, char *buffer,
/* Keep reading until we fill the buffer. */ /* Keep reading until we fill the buffer. */
while (count < size) while (count < size)
{ {
rc = webclient_recv(session, (unsigned char *)ptr, 1, 0); rc = webclient_recv(session, (unsigned char *) &ch, 1, 0);
#ifdef WEBCLIENT_USING_TLS #ifdef WEBCLIENT_USING_TLS
if(session->tls_session && rc == MBEDTLS_ERR_SSL_WANT_READ) if(session->tls_session && rc == MBEDTLS_ERR_SSL_WANT_READ)
continue; continue;
@ -122,22 +88,19 @@ static int webclient_read_line(struct webclient_session *session, char *buffer,
if (rc <= 0) if (rc <= 0)
return rc; return rc;
if (*ptr == '\n') if (ch == '\n' && last_ch == '\r')
{
ptr++;
count++;
break; break;
}
/* increment after check for cr. Don't want to count the cr. */ buffer[count++] = ch;
count++;
ptr++;
}
/* add terminate string */ last_ch = ch;
*ptr = '\0'; }
LOG_D("read line: %s", buffer); if (count > size)
{
LOG_E("read line failed. The line data length is out of buffer size(%d)!", count);
return -WEBCLIENT_ERROR;
}
return count; return count;
} }
@ -344,7 +307,7 @@ static int webclient_open_tls(struct webclient_session *session, const char *URI
return -WEBCLIENT_NOMEM; return -WEBCLIENT_NOMEM;
} }
session->tls_session->buffer_len = session->resp_sz; session->tls_session->buffer_len = WEBCLIENT_RESPONSE_BUFSZ;
session->tls_session->buffer = web_malloc(session->tls_session->buffer_len); session->tls_session->buffer = web_malloc(session->tls_session->buffer_len);
if(session->tls_session->buffer == RT_NULL) if(session->tls_session->buffer == RT_NULL)
{ {
@ -377,7 +340,7 @@ static int webclient_connect(struct webclient_session *session, const char *URI)
int socket_handle; int socket_handle;
struct timeval timeout; struct timeval timeout;
struct addrinfo *res = RT_NULL; struct addrinfo *res = RT_NULL;
char *request; char *req_url;
RT_ASSERT(session); RT_ASSERT(session);
RT_ASSERT(URI); RT_ASSERT(URI);
@ -404,10 +367,10 @@ static int webclient_connect(struct webclient_session *session, const char *URI)
} }
/* Check valid IP address and URL */ /* Check valid IP address and URL */
rc = webclient_resolve_address(session, &res, URI, &request); rc = webclient_resolve_address(session, &res, URI, &req_url);
if (rc != WEBCLIENT_OK) if (rc != WEBCLIENT_OK)
{ {
LOG_E("connect failed, resolve address error."); LOG_E("connect failed, resolve address error(%d).", rc);
goto __exit; goto __exit;
} }
@ -418,9 +381,9 @@ static int webclient_connect(struct webclient_session *session, const char *URI)
} }
/* copy host address */ /* copy host address */
if (*request) if (*req_url)
{ {
session->request = web_strdup(request); session->req_url = web_strdup(req_url);
} }
#ifdef WEBCLIENT_USING_TLS #ifdef WEBCLIENT_USING_TLS
@ -491,118 +454,189 @@ __exit:
return rc; return rc;
} }
static int webclient_send_header(struct webclient_session *session, int method, /**
const char *header, size_t header_sz) * add fields data to request header data.
*
* @param session webclient session
* @param fmt fields format
*
* @return >0: data length of successfully added
* <0: not enough header buffer size
*/
int webclient_header_fields_add(struct webclient_session *session, const char *fmt, ...)
{ {
int rc = WEBCLIENT_OK; rt_int32_t length;
unsigned char *header_buffer = RT_NULL, *header_ptr; va_list args;
RT_ASSERT(session); RT_ASSERT(session);
RT_ASSERT(session->header->buffer);
if (header == RT_NULL) va_start(args, fmt);
length = rt_vsnprintf(session->header->buffer + session->header->length, session->header->size, fmt, args);
if (length < 0)
{ {
header_buffer = web_malloc(session->header_sz); LOG_E("add fields header data failed, return length(%d) error.", length);
if (header_buffer == RT_NULL) return -WEBCLIENT_ERROR;
}
va_end(args);
session->header->length += length;
/* check header size */
if (session->header->length >= session->header->size)
{ {
LOG_E("send header failed, no memory for header buffer!"); LOG_E("not enough header buffer size(%d)!", session->header->size);
rc = -WEBCLIENT_NOMEM; return -WEBCLIENT_ERROR;
goto __exit; }
return length;
}
/**
* get fields information from request/response header data.
*
* @param session webclient session
* @param fields fields keyword
*
* @return = NULL: get fields data failed
* != NULL: success get fields data
*/
const char *webclient_header_fields_get(struct webclient_session *session, const char *fields)
{
char *resp_buf = RT_NULL;
size_t resp_buf_len = 0;
RT_ASSERT(session);
RT_ASSERT(session->header->buffer);
resp_buf = session->header->buffer;
while (resp_buf_len < session->header->length)
{
if (strstr(resp_buf, fields))
{
char *mime_ptr = RT_NULL;
/* jump space */
mime_ptr = strstr(resp_buf, ":");
if (mime_ptr != NULL)
{
mime_ptr += 1;
while (*mime_ptr && (*mime_ptr == ' ' || *mime_ptr == '\t'))
mime_ptr++;
return mime_ptr;
}
}
if (*resp_buf == '\0')
break;
resp_buf += strlen(resp_buf) + 1;
resp_buf_len += strlen(resp_buf) + 1;
}
return RT_NULL;
}
/**
* get http response status code.
*
* @param session webclient session
*
* @return response status code
*/
int webclient_resp_status_get(struct webclient_session *session)
{
RT_ASSERT(session);
return session->resp_status;
} }
header_ptr = header_buffer; static int webclient_send_header(struct webclient_session *session, int method)
header_ptr += rt_snprintf((char *) header_ptr, {
session->header_sz - (header_ptr - header_buffer), int rc = WEBCLIENT_OK;
"GET %s HTTP/1.1\r\n", char *header = RT_NULL;
session->request ? session->request : "/");
header_ptr += rt_snprintf((char *) header_ptr, RT_ASSERT(session);
session->header_sz - (header_ptr - header_buffer),
"Host: %s\r\n", session->host); header = session->header->buffer;
header_ptr += rt_snprintf((char *) header_ptr,
session->header_sz - (header_ptr - header_buffer), if (session->header->length == 0)
"User-Agent: RT-Thread HTTP Agent\r\n\r\n"); {
/* use default header data */
webclient_header_fields_add(session, "GET %s HTTP/1.1\r\n", session->req_url);
webclient_header_fields_add(session, "Host: %s\r\n", session->host);
webclient_header_fields_add(session, "User-Agent: RT-Thread HTTP Agent\r\n\r\n");
webclient_write(session, header_buffer, header_ptr - header_buffer); webclient_write(session, (unsigned char *) session->header->buffer, session->header->length);
} }
else else
{ {
if (method != WEBCLIENT_USER_METHOD) if (method != WEBCLIENT_USER_METHOD)
{ {
header_buffer = web_malloc(session->header_sz); /* check and add fields header data */
if (memcmp(header, "HTTP/1.", strlen("HTTP/1.")))
{
char *header_buffer = RT_NULL;
int length = 0;
header_buffer = web_strdup(session->header->buffer);
if (header_buffer == RT_NULL) if (header_buffer == RT_NULL)
{ {
LOG_E("send header failed, no memory for header buffer!"); LOG_E("no memory for header buffer!");
rc = -WEBCLIENT_NOMEM; rc = -WEBCLIENT_NOMEM;
goto __exit; goto __exit;
} }
header_ptr = header_buffer; /* splice http request header data */
if (strstr(header, "HTTP/1.") == RT_NULL)
{
if (method == WEBCLIENT_GET) if (method == WEBCLIENT_GET)
header_ptr += rt_snprintf((char *) header_ptr, length = rt_snprintf(session->header->buffer, session->header->size, "GET %s HTTP/1.1\r\n%s",
session->header_sz session->req_url ? session->req_url : "/", header_buffer);
- (header_ptr - header_buffer),
"GET %s HTTP/1.1\r\n",
session->request ? session->request : "/");
else if (method == WEBCLIENT_POST) else if (method == WEBCLIENT_POST)
header_ptr += rt_snprintf((char *) header_ptr, length = rt_snprintf(session->header->buffer, session->header->size, "POST %s HTTP/1.1\r\n%s",
session->header_sz session->req_url ? session->req_url : "/", header_buffer);
- (header_ptr - header_buffer), session->header->length = length;
"POST %s HTTP/1.1\r\n",
session->request ? session->request : "/"); web_free(header_buffer);
} }
if (strstr(header, "Host:") == RT_NULL) if (memcmp(header, "Host:", strlen("Host:")))
{ {
header_ptr += rt_snprintf((char *) header_ptr, webclient_header_fields_add(session, "Host: %s\r\n", session->host);
session->header_sz - (header_ptr - header_buffer),
"Host: %s\r\n", session->host);
} }
if (strstr(header, "User-Agent:") == RT_NULL) if (memcmp(header, "User-Agent:", strlen("User-Agent:")))
{ {
header_ptr += rt_snprintf((char *) header_ptr, webclient_header_fields_add(session, "User-Agent: RT-Thread HTTP Agent\r\n");
session->header_sz - (header_ptr - header_buffer),
"User-Agent: RT-Thread HTTP Agent\r\n");
} }
if (strstr(header, "Accept: ") == RT_NULL) if (memcmp(header, "Accept:", strlen("Accept:")))
{ {
header_ptr += rt_snprintf((char *) header_ptr, webclient_header_fields_add(session, "Accept: */*\r\n");
session->header_sz - (header_ptr - header_buffer),
"Accept: */*\r\n");
} }
if ((session->header_sz - (header_ptr - header_buffer)) /* header data end */
< (int) header_sz + 3) rt_snprintf(session->header->buffer + session->header->length, session->header->size, "\r\n");
session->header->length += 2;
/* check header size */
if (session->header->length > session->header->size)
{ {
LOG_E("send header failed, not enough header buffer size(%d)!", session->header_sz); LOG_E("send header failed, not enough header buffer size(%d)!", session->header->size);
rc = -WEBCLIENT_NOBUFFER; rc = -WEBCLIENT_NOBUFFER;
goto __exit; goto __exit;
} }
/* append user's header */ webclient_write(session, (unsigned char *) session->header->buffer, session->header->length);
memcpy(header_ptr, header, header_sz);
header_ptr += header_sz;
header_ptr += rt_snprintf((char *) header_ptr,
session->header_sz - (header_ptr - header_buffer),
"\r\n");
webclient_write(session, header_buffer, header_ptr - header_buffer);
} }
else else
{ {
webclient_write(session, (unsigned char *) header, header_sz); webclient_write(session, (unsigned char *) session->header->buffer, session->header->length);
} }
} }
__exit: __exit:
if(header_buffer)
{
web_free(header_buffer);
}
return rc; return rc;
} }
@ -617,127 +651,82 @@ __exit:
int webclient_handle_response(struct webclient_session *session) int webclient_handle_response(struct webclient_session *session)
{ {
int rc = WEBCLIENT_OK; int rc = WEBCLIENT_OK;
char *mime_buffer, *mime_ptr; char *mime_buffer = RT_NULL;
char *mime_ptr = RT_NULL;
const char *transfer_encoding;
int i;
RT_ASSERT(session); RT_ASSERT(session);
/* set content length of session */ /* clean header buffer and size */
session->content_length = -1; memset(session->header->buffer, 0x00, session->header->size);
session->header->length = 0;
mime_buffer = (char *) web_malloc(session->resp_sz + 1);
if (!mime_buffer)
{
LOG_E("handle response failed, no memory for mime buffer!");
return -WEBCLIENT_NOMEM;
}
/* We now need to read the header information */ /* We now need to read the header information */
while (1) while (1)
{ {
int i; mime_buffer = session->header->buffer + session->header->length;
/* read a line from the header information. */ /* read a line from the header information. */
rc = webclient_read_line(session, mime_buffer, session->resp_sz); rc = webclient_read_line(session, mime_buffer, session->header->size - session->header->length);
if (rc < 0) if (rc < 0)
break; break;
/* set terminal charater */
mime_buffer[rc] = '\0';
/* End of headers is a blank line. exit. */ /* End of headers is a blank line. exit. */
if (rc == 0) if (rc == 0)
break; break;
if ((rc == 2) && (mime_buffer[0] == '\r')) if ((rc == 1) && (mime_buffer[0] == '\r'))
{
mime_buffer[0] = '\0';
break; break;
}
mime_ptr = webclient_header_skip_prefix(mime_buffer, "HTTP/1."); /* set terminal charater */
if (mime_ptr != RT_NULL) mime_buffer[rc - 1] = '\0';
{
mime_ptr += 1;
while (*mime_ptr && (*mime_ptr == ' ' || *mime_ptr == '\t'))
mime_ptr++;
/* Terminate string after status code */
for (i = 0; ((mime_ptr[i] != ' ') && (mime_ptr[i] != '\t')); i++);
mime_ptr[i] = '\0';
session->response = (int) strtol(mime_ptr, RT_NULL, 10); session->header->length += rc;
} }
mime_ptr = webclient_header_skip_prefix(mime_buffer, "Last-Modified:"); /* get HTTP status code */
mime_ptr = strstr(session->header->buffer, "HTTP/1.");
if (mime_ptr != RT_NULL) if (mime_ptr != RT_NULL)
{ {
session->last_modified = web_strdup(mime_ptr); mime_ptr += strlen("HTTP/1.x");
}
mime_ptr = webclient_header_skip_prefix(mime_buffer, "Transfer-Encoding: "); while (*mime_ptr && (*mime_ptr == ' ' || *mime_ptr == '\t'))
if (mime_ptr != RT_NULL) mime_ptr++;
{
session->transfer_encoding = web_strdup(mime_ptr);
}
mime_ptr = webclient_header_skip_prefix(mime_buffer, "Content-Type:"); /* Terminate string after status code */
if (mime_ptr != RT_NULL) for (i = 0; ((mime_ptr[i] != ' ') && (mime_ptr[i] != '\t')); i++);
{ mime_ptr[i] = '\0';
session->content_type = web_strdup(mime_ptr);
}
mime_ptr = webclient_header_skip_prefix(mime_buffer, "Content-Length:"); session->resp_status = (int) strtol(mime_ptr, RT_NULL, 10);
if (mime_ptr != RT_NULL)
{
session->content_length = (int) strtol(mime_ptr, RT_NULL, 10);
} }
mime_ptr = webclient_header_skip_prefix(mime_buffer, "Location: "); /* get content length */
if (mime_ptr != RT_NULL) if (webclient_header_fields_get(session, "Content-Length") != RT_NULL)
{ {
session->location = web_strdup(mime_ptr); session->content_length = atoi(webclient_header_fields_get(session, "Content-Length"));
} }
session->content_remainder = session->content_length ? (size_t) session->content_length : 0xFFFFFFFF;
mime_ptr = webclient_header_skip_prefix(mime_buffer, "Content-Range:"); transfer_encoding = webclient_header_fields_get(session, "Transfer-Encoding");
if (mime_ptr != RT_NULL) if (transfer_encoding && strcmp(transfer_encoding, "chunked") == 0)
{ {
char *ptr = RT_NULL; char line[16];
int totle_length;
mime_ptr = webclient_header_skip_prefix(mime_ptr, "bytes");
while (*mime_ptr == ' ')
mime_ptr++;
session->position = atoi(mime_ptr);
ptr = strstr(mime_ptr, "/");
if (ptr)
{
ptr ++;
/* The total length of the get data */
totle_length = atoi(ptr);
//TODO: process total length
}
}
}
session->content_length_remainder =
(session->content_length) ? (size_t) session->content_length : 0xFFFFFFFF;
if (session->transfer_encoding && strcmp(session->transfer_encoding, "chunked") == 0)
{
/* chunk mode, we should get the first chunk size */ /* chunk mode, we should get the first chunk size */
webclient_read_line(session, mime_buffer, session->resp_sz); webclient_read_line(session, line, session->header->size);
session->chunk_sz = strtol(mime_buffer, RT_NULL, 16); session->chunk_sz = strtol(line, RT_NULL, 16);
session->chunk_offset = 0; session->chunk_offset = 0;
} }
/* release buffer */
if(mime_buffer)
{
web_free(mime_buffer);
}
if (rc < 0) if (rc < 0)
{ {
return rc; return rc;
} }
return session->response; return session->resp_status;
} }
/** /**
@ -748,7 +737,7 @@ int webclient_handle_response(struct webclient_session *session)
* *
* @return webclient session structure * @return webclient session structure
*/ */
struct webclient_session *webclient_session_create(size_t header_sz, size_t resp_sz) struct webclient_session *webclient_session_create(size_t header_sz)
{ {
struct webclient_session *session; struct webclient_session *session;
@ -756,12 +745,31 @@ struct webclient_session *webclient_session_create(size_t header_sz, size_t resp
session = (struct webclient_session *) web_calloc(1, sizeof(struct webclient_session)); session = (struct webclient_session *) web_calloc(1, sizeof(struct webclient_session));
if (session == RT_NULL) if (session == RT_NULL)
{ {
LOG_E("webclient create failed, no memory for session!"); LOG_E("webclient create failed, no memory for webclient session!");
return RT_NULL; return RT_NULL;
} }
session->header_sz = header_sz; session->content_length = -1;
session->resp_sz = resp_sz;
session->header = (struct webclient_header *) web_calloc(1, sizeof(struct webclient_header));
if (session->header == RT_NULL)
{
LOG_E("webclient create failed, no memory for session header!");
web_free(session);
session = RT_NULL;
return RT_NULL;
}
session->header->size = header_sz;
session->header->buffer = (char *) web_malloc(header_sz);
if (session->header->buffer == RT_NULL)
{
LOG_E("webclient create failed, no memory for session header buffer!");
web_free(session->header);
web_free(session);
session = RT_NULL;
return RT_NULL;
}
return session; return session;
} }
@ -778,9 +786,10 @@ struct webclient_session *webclient_session_create(size_t header_sz, size_t resp
* @return <0: send GET request failed * @return <0: send GET request failed
* >0: response http status code * >0: response http status code
*/ */
int webclient_get(struct webclient_session *session, const char *URI, const char *header) int webclient_get(struct webclient_session *session, const char *URI)
{ {
int rc = WEBCLIENT_OK; int rc = WEBCLIENT_OK;
int resp_status = 0;
RT_ASSERT(session); RT_ASSERT(session);
RT_ASSERT(URI); RT_ASSERT(URI);
@ -792,7 +801,7 @@ int webclient_get(struct webclient_session *session, const char *URI, const char
goto __exit; goto __exit;
} }
rc = webclient_send_header(session, WEBCLIENT_GET, header, strlen(header)); rc = webclient_send_header(session, WEBCLIENT_GET);
if (rc != WEBCLIENT_OK) if (rc != WEBCLIENT_OK)
{ {
/* send header to webclient server failed. */ /* send header to webclient server failed. */
@ -800,40 +809,38 @@ int webclient_get(struct webclient_session *session, const char *URI, const char
} }
/* handle the response header of webclient server */ /* handle the response header of webclient server */
rc = webclient_handle_response(session); resp_status = webclient_handle_response(session);
if (rc > 0) if (resp_status > 0)
{ {
const char *location = webclient_header_fields_get(session, "Location");
/* relocation */ /* relocation */
if ((session->response == 302 || session->response == 301) && session->location) if ((resp_status == 302 || resp_status == 301) && location)
{
char *location = web_strdup(session->location);
if (location)
{ {
webclient_close(session); webclient_close(session);
rc = webclient_get(session, location, header); resp_status = webclient_get(session, location);
if (rc != WEBCLIENT_OK) if (resp_status < 0)
{ {
rc = resp_status;
goto __exit; goto __exit;
} }
web_free(location); return resp_status;
return session->response;
}
} }
else if (session->response != 200) else if (resp_status != 200)
{ {
LOG_E("get failed, handle response(%d) error!", session->response); LOG_E("get failed, handle response(%d) error!", resp_status);
goto __exit; goto __exit;
} }
} }
__exit: __exit:
if (rc < 0) if (resp_status)
{ {
return rc; return resp_status;
} }
return session->response; return rc;
} }
/** /**
@ -848,8 +855,8 @@ __exit:
*/ */
int webclient_get_position(struct webclient_session *session, const char *URI, int position) int webclient_get_position(struct webclient_session *session, const char *URI, int position)
{ {
char *range_header = RT_NULL;
int rc = WEBCLIENT_OK; int rc = WEBCLIENT_OK;
int resp_status = 0;
RT_ASSERT(session); RT_ASSERT(session);
RT_ASSERT(URI); RT_ASSERT(URI);
@ -860,66 +867,54 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i
goto __exit; goto __exit;
} }
range_header = (char *) web_malloc(session->header_sz); /* splice header*/
if (range_header == RT_NULL) if (webclient_header_fields_add(session, "Range: bytes=%d-\r\n", position) <= 0)
{ {
LOG_E("open position failed, no memory for range header!"); rc = WEBCLIENT_ERROR;
rc = -WEBCLIENT_NOMEM;
goto __exit; goto __exit;
} }
/* splice header*/ rc = webclient_send_header(session, WEBCLIENT_GET);
rt_snprintf(range_header, session->header_sz - 1,
"Range: bytes=%d-\r\n", position);
rc = webclient_send_header(session, WEBCLIENT_GET, range_header, strlen(range_header));
if (rc != WEBCLIENT_OK) if (rc != WEBCLIENT_OK)
{ {
goto __exit; goto __exit;
} }
/* handle the response header of webclient server */ /* handle the response header of webclient server */
rc = webclient_handle_response(session); resp_status = webclient_handle_response(session);
if (rc > 0) if (rc > 0)
{ {
const char *location = webclient_header_fields_get(session, "Location");
/* relocation */ /* relocation */
if ((session->response == 302 || session->response == 301) && session->location) if ((resp_status == 302 || resp_status == 301) && location)
{
char *location = web_strdup(session->location);
if (location)
{ {
webclient_close(session); webclient_close(session);
rc = webclient_get_position(session, location, position); resp_status = webclient_get_position(session, location, position);
if (rc != WEBCLIENT_OK) if (resp_status < 0)
{ {
rc = resp_status;
goto __exit; goto __exit;
} }
web_free(location); return resp_status;
return session->response;
}
} }
else if (session->response != 206) else if (resp_status != 206)
{ {
LOG_E("get failed, handle response(%d) error!", session->response); LOG_E("get failed, handle response(%d) error!", resp_status);
goto __exit; goto __exit;
} }
} }
__exit: __exit:
if (range_header) if (resp_status)
{ {
web_free(range_header); return resp_status;
} }
if (rc < 0)
{
return rc; return rc;
} }
return session->response;
}
/** /**
* send POST request to server and get response header data. * send POST request to server and get response header data.
* *
@ -934,10 +929,10 @@ __exit:
* =0: send POST header success * =0: send POST header success
* >0: response http status code * >0: response http status code
*/ */
int webclient_post(struct webclient_session *session, const char *URI, int webclient_post(struct webclient_session *session, const char *URI, const char *post_data)
const char *header, const char *post_data)
{ {
int rc = WEBCLIENT_OK; int rc = WEBCLIENT_OK;
int resp_status = 0;
RT_ASSERT(session); RT_ASSERT(session);
RT_ASSERT(URI); RT_ASSERT(URI);
@ -949,7 +944,7 @@ int webclient_post(struct webclient_session *session, const char *URI,
goto __exit; goto __exit;
} }
rc = webclient_send_header(session, WEBCLIENT_POST, header, strlen(header)); rc = webclient_send_header(session, WEBCLIENT_POST);
if (rc != WEBCLIENT_OK) if (rc != WEBCLIENT_OK)
{ {
/* send header to webclient server failed. */ /* send header to webclient server failed. */
@ -961,28 +956,21 @@ int webclient_post(struct webclient_session *session, const char *URI,
webclient_write(session, (unsigned char *) post_data, strlen(post_data)); webclient_write(session, (unsigned char *) post_data, strlen(post_data));
/* resolve response data, get http status code */ /* resolve response data, get http status code */
rc = webclient_handle_response(session); resp_status = webclient_handle_response(session);
if (rc > 0) if (resp_status != 200)
{
if (session->response != 200)
{ {
LOG_E("post failed, handle response(%d) error.", session->response); LOG_E("post failed, handle response(%d) error.", resp_status);
goto __exit; goto __exit;
} }
} }
}
else
{
return rc;
}
__exit: __exit:
if (rc < 0) if (resp_status)
{ {
return rc; return resp_status;
} }
return session->response; return rc;
} }
@ -1021,9 +1009,9 @@ static int webclient_next_chunk(struct webclient_session *session)
RT_ASSERT(session); RT_ASSERT(session);
length = webclient_read_line(session, line, sizeof(line)); length = webclient_read_line(session, line, sizeof(line));
if (length) if (length > 0)
{ {
if (strcmp(line, "\r\n") == 0) if (strcmp(line, "\r") == 0)
{ {
length = webclient_read_line(session, line, sizeof(line)); length = webclient_read_line(session, line, sizeof(line));
if (length <= 0) if (length <= 0)
@ -1119,9 +1107,9 @@ int webclient_read(struct webclient_session *session, unsigned char *buffer, siz
if (session->content_length > 0) if (session->content_length > 0)
{ {
if (length > session->content_length_remainder) if (length > session->content_remainder)
{ {
length = session->content_length_remainder; length = session->content_remainder;
} }
if (length == 0) if (length == 0)
@ -1174,7 +1162,7 @@ int webclient_read(struct webclient_session *session, unsigned char *buffer, siz
if (session->content_length > 0) if (session->content_length > 0)
{ {
session->content_length_remainder -= total_read; session->content_remainder -= total_read;
} }
return total_read; return total_read;
@ -1283,23 +1271,21 @@ int webclient_close(struct webclient_session *session)
} }
#endif #endif
if(session->transfer_encoding)
web_free(session->transfer_encoding);
if(session->content_type)
web_free(session->content_type);
if(session->last_modified)
web_free(session->last_modified);
if (session->host) if (session->host)
web_free(session->host); web_free(session->host);
if(session->request) if (session->req_url)
web_free(session->request); web_free(session->req_url);
if (session->header && session->header->buffer)
{
web_free(session->header->buffer);
}
if(session->location) if (session->header)
web_free(session->location); {
web_free(session->header);
}
if (session) if (session)
{ {
@ -1318,7 +1304,7 @@ int webclient_close(struct webclient_session *session)
* *
* @return response data size * @return response data size
*/ */
int webclient_response(struct webclient_session *session, void **response) int webclient_response(struct webclient_session *session, unsigned char **response)
{ {
unsigned char *buf_ptr; unsigned char *buf_ptr;
unsigned char *response_buf = 0; unsigned char *response_buf = 0;
@ -1340,7 +1326,7 @@ int webclient_response(struct webclient_session *session, void **response)
{ {
unsigned char *new_resp = RT_NULL; unsigned char *new_resp = RT_NULL;
result_sz = total_read + session->resp_sz; result_sz = total_read + WEBCLIENT_RESPONSE_BUFSZ;
new_resp = web_realloc(response_buf, result_sz + 1); new_resp = web_realloc(response_buf, result_sz + 1);
if (new_resp == RT_NULL) if (new_resp == RT_NULL)
{ {
@ -1434,20 +1420,25 @@ int webclient_request(const char *URI, const char *header, const char *post_data
if (post_data == RT_NULL) if (post_data == RT_NULL)
{ {
session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ, WEBCLIENT_RESPONSE_BUFSZ); session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
if (session == RT_NULL) if (session == RT_NULL)
{ {
rc = -WEBCLIENT_NOMEM; rc = -WEBCLIENT_NOMEM;
goto __exit; goto __exit;
} }
if(webclient_get(session, URI, header) != 200) if (header != RT_NULL)
{
strncpy(session->header->buffer, header, strlen(header));
}
if (webclient_get(session, URI) != 200)
{ {
rc = -WEBCLIENT_ERROR; rc = -WEBCLIENT_ERROR;
goto __exit; goto __exit;
} }
totle_length = webclient_response(session, (void **)response); totle_length = webclient_response(session, response);
if (totle_length <= 0) if (totle_length <= 0)
{ {
rc = -WEBCLIENT_ERROR; rc = -WEBCLIENT_ERROR;
@ -1456,20 +1447,30 @@ int webclient_request(const char *URI, const char *header, const char *post_data
} }
else else
{ {
session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ, WEBCLIENT_RESPONSE_BUFSZ); session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
if (session == RT_NULL) if (session == RT_NULL)
{ {
rc = -WEBCLIENT_NOMEM; rc = -WEBCLIENT_NOMEM;
goto __exit; goto __exit;
} }
if(webclient_post(session, URI, header, post_data) != 200) if (header != RT_NULL)
{
strncpy(session->header->buffer, header, strlen(header));
}
if (webclient_post(session, URI, post_data) != 200)
{ {
rc = -WEBCLIENT_ERROR; rc = -WEBCLIENT_ERROR;
goto __exit; goto __exit;
} }
} }
if (header != RT_NULL)
{
strncpy(session->header->buffer, header, strlen(header));
}
__exit: __exit:
if (session) if (session)
{ {
@ -1483,5 +1484,4 @@ __exit:
} }
return totle_length; return totle_length;
} }

@ -25,7 +25,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <rtthread.h> #include <rtthread.h>
#include "webclient.h" #include <webclient.h>
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
#include <dfs_posix.h> #include <dfs_posix.h>
@ -46,17 +46,18 @@ int webclient_get_file(const char* URI, const char* filename)
size_t length, total_length = 0; size_t length, total_length = 0;
unsigned char *ptr = RT_NULL; unsigned char *ptr = RT_NULL;
struct webclient_session* session = RT_NULL; struct webclient_session* session = RT_NULL;
int resp_status = 0, content_length = -1;
session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ, WEBCLIENT_RESPONSE_BUFSZ); session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
if(session == RT_NULL) if(session == RT_NULL)
{ {
rc = -WEBCLIENT_NOMEM; rc = -WEBCLIENT_NOMEM;
goto __exit; goto __exit;
} }
if (webclient_get(session, URI, RT_NULL) != 200) if ((resp_status = webclient_get(session, URI)) != 200)
{ {
LOG_E("get file failed, wrong response: %d.", session->response); LOG_E("get file failed, wrong response: %d.", resp_status);
rc = -WEBCLIENT_ERROR; rc = -WEBCLIENT_ERROR;
goto __exit; goto __exit;
} }
@ -77,7 +78,7 @@ int webclient_get_file(const char* URI, const char* filename)
goto __exit; goto __exit;
} }
if (session->content_length == 0) if (session->content_length < 0)
{ {
while (1) while (1)
{ {
@ -86,7 +87,7 @@ int webclient_get_file(const char* URI, const char* filename)
{ {
write(fd, ptr, length); write(fd, ptr, length);
total_length += length; total_length += length;
rt_kprintf(">"); LOG_RAW(">");
} }
else else
{ {
@ -96,7 +97,7 @@ int webclient_get_file(const char* URI, const char* filename)
} }
else else
{ {
for (offset = 0; offset < session->content_length;) for (offset = 0; offset < (size_t) session->content_length;)
{ {
length = webclient_read(session, ptr, length = webclient_read(session, ptr,
session->content_length - offset > WEBCLIENT_RESPONSE_BUFSZ ? session->content_length - offset > WEBCLIENT_RESPONSE_BUFSZ ?
@ -106,7 +107,7 @@ int webclient_get_file(const char* URI, const char* filename)
{ {
write(fd, ptr, length); write(fd, ptr, length);
total_length += length; total_length += length;
rt_kprintf(">"); LOG_RAW(">");
} }
else else
{ {
@ -216,14 +217,16 @@ int webclient_post_file(const char* URI, const char* filename,
WEBCLIENT_HEADER_BUFSZ - (header_ptr - header), WEBCLIENT_HEADER_BUFSZ - (header_ptr - header),
"Content-Type: multipart/form-data; boundary=%s\r\n", boundary); "Content-Type: multipart/form-data; boundary=%s\r\n", boundary);
session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ, WEBCLIENT_RESPONSE_BUFSZ); session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
if(session == RT_NULL) if(session == RT_NULL)
{ {
rc = -WEBCLIENT_NOMEM; rc = -WEBCLIENT_NOMEM;
goto __exit; goto __exit;
} }
rc = webclient_post(session, URI, header, NULL); session->header->buffer = web_strdup(header);
rc = webclient_post(session, URI, NULL);
if( rc< 0) if( rc< 0)
{ {
goto __exit; goto __exit;
Loading…
Cancel
Save