diff --git a/README.md b/README.md index 0545eb3..8e4f1f5 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ WebClient 软件包遵循 Apache-2.0 许可,详见 LICENSE 文件。 RT-Thread online packages IoT - internet of things ---> [*] WebClient: A HTTP/HTTPS Client for RT-Thread + [ ] Enable debug log output [ ] Enable webclient GET/POST samples Select TLS mode (Not support) ---> (x) Not support diff --git a/docs/api.md b/docs/api.md index b2cb5a8..5cfcfcc 100644 --- a/docs/api.md +++ b/docs/api.md @@ -202,6 +202,23 @@ int webclient_request(const char *URI, const char *header, const char *post_data | `>0` | 成功接收数据的长度 | | <=0 | 接收数据失败 | +## 拼接请求头部数据 + +```c +int webclient_request_header_add(char **request_header, const char *fmt, ...); +``` + +该函数适用于 webclient_request 函数发送请求之前,头部数据的拼接和添加。 + +| 参数 | 描述 | +|:------------------|:-----------------------------------| +|request_header | 请求头部数据缓冲区地址 | +|fmt | 添加字段数据的表达式 | +|... | 添加的字段数据,为可变参数 | +| **返回** | **描述** | +| `>0` | 成功添加的字段数据的长度 | +| <=0 | 头部数据添加失败或内存不足 | + ## 获取 HTTP 响应状态码 diff --git a/docs/samples.md b/docs/samples.md index 0fe508c..41eaf3b 100644 --- a/docs/samples.md +++ b/docs/samples.md @@ -23,8 +23,9 @@ WebClient 软件包提供两个 HTTP Client 示例程序, 分别用于演示软 RT-Thread online packages IoT - internet of things ---> [*] WebClient: A HTTP/HTTPS Client for RT-Thread - [ ] Enable support tls protocol + [ ] Enable debug log output [*] Enable webclient GET/POST samples # 开启 WebClient 测试例程 + Select TLS mode (Not support) ---> Version (latest) ---> # 开启使用最新版本软件包 ``` @@ -50,17 +51,22 @@ GET 请求示例流程: GET 请求示例使用方式有如下两种: -- 在 MSH 中使用命令 `web_get_test` 执行 GET 请求示例程序,可以获取并打印显示默认网址下载的文件信息,如下图 LOG 显示: +- 在 MSH 中使用命令 `web_get_test` 执行 GET 请求示例程序,可以获取并打印显示默认网址下载的文件信息;在 MSH 中使用命令 `web_get_test -s` 执行 POST 请求示例程序,使用简化接口(webclient_request 接口)发送 GET请求,适用于简短数据的收发。如下图 LOG 显示: ```c -msh />web_get_test -webclient GET request response data : +msh />web_get_test +webclient get response data: +RT-Thread is an open source IoT operating system from China, which has strong scalability: from a tiny kernel running on a tiny core, for example ARM Cortex-M0, or Cortex-M3/4/7, to a rich feature system running on MIPS32, ARM Cortex-A8, ARM Cortex-A9 DualCore etc. + +msh />web_get_test -s +webclient send get request by simplify request interface. +webclient get response data: RT-Thread is an open source IoT operating system from China, which has strong scalability: from a tiny kernel running on a tiny core, for example ARM Cortex-M0, or Cortex-M3/4/7, to a rich feature system running on MIPS32, ARM Cortex-A8, ARM Cortex-A9 DualCore etc. msh /> ``` -- 在 MSH 中使用命令 `web_get_test [URI]` 格式命令执行 GET 请求示例程序,其中 URI 为用户自定义的支持 GET 请求的地址。 +- 在 MSH 中使用命令 `web_get_test [URI]` 或 `web_get_test -s [URI]` 格式命令执行 GET 请求示例程序,其中 URI 为用户自定义的支持 GET 请求的地址。 ### POST 请求示例 @@ -75,13 +81,18 @@ POST 请求示例流程如下: POST 请求示例使用方式有如下两种: -- 在 MSH 中使用命令 `web_post_test` 执行 POST 请求示例程序,可以获取并打印显示响应数据(默认 POST 请求的地址是类似于回显的地址,会返回上传的数据),如下图 LOG 显示: +- 在 MSH 中使用命令 `web_post_test` 执行 POST 请求示例程序,可以获取并打印显示响应数据(默认 POST 请求的地址是类似于回显的地址,会返回上传的数据);在 MSH 中使用命令 `web_post_test -s` 执行 POST 请求示例程序,使用简化接口(webclient_request 接口)发送 POST 请求,适用于简短数据的收发。如下图 LOG 显示: ```c msh />web_post_test -webclient POST request response data : +webclient post response data : RT-Thread is an open source IoT operating system from China! msh /> +msh />web_post_test -s +webclient send post request by simplify request interface. +webclient post response data: +RT-Thread is an open source IoT operating system from China! +msh /> ``` -- 在 MSH 中使用命令 `web_post_test [URI]` 格式命令执行 POST 请求示例程序,其中 URI 为用户自定义的支持 POST 请求的地址。 \ No newline at end of file +- 在 MSH 中使用命令 `web_post_test [URI]` 或者 `web_post_test -s [URI]` 格式命令执行 POST 请求示例程序,其中 URI 为用户自定义的支持 POST 请求的地址。 \ No newline at end of file diff --git a/docs/user-guide.md b/docs/user-guide.md index 02c10c1..d00980c 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -14,6 +14,7 @@ RT-Thread online packages IoT - internet of things ---> [*] WebClient: A HTTP/HTTPS Client for RT-Thread + [ ] Enable debug log output [ ] Enable webclient GET/POST samples Select TLS mode (Not support) ---> (x) Not support @@ -22,6 +23,8 @@ RT-Thread online packages Version (latest) ---> ``` +**Enable debug log output**:开启调试日志显示,可以用于查看请求和响应的头部数据信息; + **Enable webclient GET/POST samples** :添加示例代码; **Select TLS mode** :配置开启 HTTPS 支持,选择支持的模式; @@ -273,7 +276,10 @@ webclient_close(session); 多用于接收数据长度较小,且头部信息已经拼接给出的 GET 请求。 ```c -char *result; +char *result, *header = RT_NULL; + +/* 拼接自定义头部数据 */ +webclient_request_header_add(&header, "User-Agent: RT-Thread HTTP Agent\r\n"); webclient_request(URI, header, NULL, &result); @@ -341,8 +347,13 @@ webclient_close(session); ```c char *post_data = "abcdefg"; +char *header = RT_NULL; + +/* 拼接自定义头部数据 */ +webclient_request_header_add(&header, "Content-Length: %d\r\n", strlen(post_data)); +webclient_request_header_add(&header, "Content-Type: application/octet-stream\r\n"); -webclient_request(URI, NULL, post_data, NULL); +webclient_request(URI, header, post_data, NULL); ``` ## 常见问题 @@ -355,7 +366,7 @@ webclient_request(URI, NULL, post_data, NULL); - 原因:使用 HTTPS 地址但是没有开启 HTTPS 支持。 -- 解决方法:在 WebClient 软件包 menuconfig 配置选项中开启 `Enable support tls protocol` 选项支持。 +- 解决方法:在 WebClient 软件包 menuconfig 配置选项中 选择 `Select TLS mode` 选项为 `MbedTLS support` 或者 `SAL TLS support`。 ### 头部数据长度超出 diff --git a/inc/webclient.h b/inc/webclient.h index e08b739..d046a6e 100644 --- a/inc/webclient.h +++ b/inc/webclient.h @@ -128,6 +128,7 @@ const char *webclient_header_fields_get(struct webclient_session *session, const /* send HTTP POST/GET request, and get response data */ 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 **response); +int webclient_request_header_add(char **request_header, const char *fmt, ...); int webclient_resp_status_get(struct webclient_session *session); int webclient_content_length_get(struct webclient_session *session); diff --git a/samples/webclient_get_sample.c b/samples/webclient_get_sample.c index 4c0b9ee..17bbfcb 100644 --- a/samples/webclient_get_sample.c +++ b/samples/webclient_get_sample.c @@ -16,40 +16,16 @@ #define GET_LOCAL_URI "http://www.rt-thread.com/service/rt-thread.txt" -int webclient_get_test(int argc, char **argv) +/* send HTTP GET request by common request interface, it used to receive longer data */ +static int webclient_get_comm(const char *uri) { 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); + buffer = (unsigned char *) web_malloc(GET_RESP_BUFSZ); if (buffer == RT_NULL) { rt_kprintf("no memory for receive buffer.\n"); @@ -67,14 +43,14 @@ int webclient_get_test(int argc, char **argv) } /* send GET request by default header */ - if ((resp_status = webclient_get(session, URI)) != 200) + 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 :\n"); + rt_kprintf("webclient get response data: \n"); content_length = webclient_content_length_get(session); if (content_length < 0) @@ -132,15 +108,99 @@ __exit: web_free(buffer); } - if (URI) + return ret; +} + +/* send HTTP GET request by simplify request interface, it used to received shorter data */ +static int webclient_get_smpl(const char *uri) +{ + char *request = RT_NULL; + int index; + + if (webclient_request(uri, RT_NULL, RT_NULL, (unsigned char **)&request) < 0) { - web_free(URI); + rt_kprintf("webclient send get request failed."); + return -RT_ERROR; } - return ret; + rt_kprintf("webclient send get request by simplify request interface.\n"); + rt_kprintf("webclient get response data: \n"); + for (index = 0; index < rt_strlen(request); index++) + { + rt_kprintf("%c", request[index]); + } + rt_kprintf("\n"); + + if (request) + { + web_free(request); + } + + return 0; +} + + +int webclient_get_test(int argc, char **argv) +{ + char *uri = RT_NULL; + + if (argc == 1) + { + uri = web_strdup(GET_LOCAL_URI); + if(uri == RT_NULL) + { + rt_kprintf("no memory for create get request uri buffer.\n"); + return -RT_ENOMEM; + } + + webclient_get_comm(uri); + } + else if (argc == 2) + { + if (rt_strcmp(argv[1], "-s") == 0) + { + uri = web_strdup(GET_LOCAL_URI); + if(uri == RT_NULL) + { + rt_kprintf("no memory for create get request uri buffer.\n"); + return -RT_ENOMEM; + } + + webclient_get_smpl(uri); + } + else + { + uri = web_strdup(argv[1]); + if(uri == RT_NULL) + { + rt_kprintf("no memory for create get request uri buffer.\n"); + return -RT_ENOMEM; + } + webclient_get_comm(uri); + } + } + else if(argc == 3 && rt_strcmp(argv[1], "-s") == 0) + { + uri = web_strdup(argv[2]); + if(uri == RT_NULL) + { + rt_kprintf("no memory for create get request uri buffer.\n"); + return -RT_ENOMEM; + } + + webclient_get_smpl(uri); + } + else + { + rt_kprintf("web_get_test [URI] - webclient GET request test.\n"); + rt_kprintf("web_get_test -s [URI] - webclient simplify GET request test.\n"); + return -RT_ERROR; + } + + return 0; } #ifdef FINSH_USING_MSH #include -MSH_CMD_EXPORT_ALIAS(webclient_get_test, web_get_test, web_get_test [URI] webclient GET request test); +MSH_CMD_EXPORT_ALIAS(webclient_get_test, web_get_test, webclient get request test); #endif /* FINSH_USING_MSH */ diff --git a/samples/webclient_post_sample.c b/samples/webclient_post_sample.c index 538e01e..caa026c 100644 --- a/samples/webclient_post_sample.c +++ b/samples/webclient_post_sample.c @@ -20,45 +20,20 @@ const char *post_data = "RT-Thread is an open source IoT operating system from China!"; -int webclient_post_test(int argc, char **argv) +/* send HTTP POST request by common request interface, it used to receive longer data */ +static int webclient_post_comm(const char *uri, const char *post_data) { 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 */ @@ -74,14 +49,14 @@ int webclient_post_test(int argc, char **argv) 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) + 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; } - rt_kprintf("webclient POST request response data :\n"); + rt_kprintf("webclient post response data: \n"); do { bytes_read = webclient_read(session, buffer, POST_RESP_BUFSZ); @@ -109,15 +84,109 @@ __exit: web_free(buffer); } - if (URI) + return ret; +} + +/* send HTTP POST request by simplify request interface, it used to received shorter data */ +static int webclient_post_smpl(const char *uri, const char *post_data) +{ + char *request = RT_NULL, *header = RT_NULL; + int index; + + webclient_request_header_add(&header, "Content-Length: %d\r\n", strlen(post_data)); + webclient_request_header_add(&header, "Content-Type: application/octet-stream\r\n"); + + if (webclient_request(uri, (const char *)header, post_data, (unsigned char **)&request) < 0) { - web_free(URI); + rt_kprintf("webclient send post request failed."); + web_free(header); + return -RT_ERROR; } - return ret; + rt_kprintf("webclient send post request by simplify request interface.\n"); + rt_kprintf("webclient post response data: \n"); + for (index = 0; index < rt_strlen(request); index++) + { + rt_kprintf("%c", request[index]); + } + rt_kprintf("\n"); + + if (header) + { + web_free(header); + } + + if (request) + { + web_free(request); + } + + return 0; } + +int webclient_post_test(int argc, char **argv) +{ + char *uri = RT_NULL; + + if (argc == 1) + { + uri = web_strdup(POST_LOCAL_URI); + if(uri == RT_NULL) + { + rt_kprintf("no memory for create post request uri buffer.\n"); + return -RT_ENOMEM; + } + + webclient_post_comm(uri, post_data); + } + else if (argc == 2) + { + if (rt_strcmp(argv[1], "-s") == 0) + { + uri = web_strdup(POST_LOCAL_URI); + if(uri == RT_NULL) + { + rt_kprintf("no memory for create post request uri buffer.\n"); + return -RT_ENOMEM; + } + + webclient_post_smpl(uri, post_data); + } + else + { + uri = web_strdup(argv[1]); + if(uri == RT_NULL) + { + rt_kprintf("no memory for create post request uri buffer.\n"); + return -RT_ENOMEM; + } + webclient_post_comm(uri, post_data); + } + } + else if(argc == 3 && rt_strcmp(argv[1], "-s") == 0) + { + uri = web_strdup(argv[2]); + if(uri == RT_NULL) + { + rt_kprintf("no memory for create post request uri buffer.\n"); + return -RT_ENOMEM; + } + + webclient_post_smpl(uri, post_data); + } + else + { + rt_kprintf("web_post_test [uri] - webclient post request test.\n"); + rt_kprintf("web_post_test -s [uri] - webclient simplify post request test.\n"); + return -RT_ERROR; + } + + return 0; +} + + #ifdef FINSH_USING_MSH #include -MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post_test, webclient_post_test [URI] - webclient POST request test.); +MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post_test, webclient post request test.); #endif /* FINSH_USING_MSH */ diff --git a/src/webclient.c b/src/webclient.c index 9d68fa1..4d222d0 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -670,7 +670,28 @@ static int webclient_send_header(struct webclient_session *session, int method) webclient_write(session, (unsigned char *) session->header->buffer, session->header->length); } } - + + /* get and echo request header data */ + { + char *header_str, *header_ptr; + int header_line_len; + LOG_D("request header:"); + + for(header_str = session->header->buffer; (header_ptr = rt_strstr(header_str, "\r\n")) != RT_NULL; ) + { + header_line_len = header_ptr - header_str; + + if (header_line_len > 0) + { + LOG_D("%.*s", header_line_len, header_str); + } + header_str = header_ptr + rt_strlen("\r\n"); + } +#ifdef WEBCLIENT_DEBUG + LOG_RAW("\n"); +#endif + } + __exit: return rc; } @@ -697,6 +718,7 @@ int webclient_handle_response(struct webclient_session *session) rt_memset(session->header->buffer, 0x00, session->header->size); session->header->length = 0; + LOG_D("response header:"); /* We now need to read the header information */ while (1) { @@ -719,6 +741,9 @@ int webclient_handle_response(struct webclient_session *session) /* set terminal charater */ mime_buffer[rc - 1] = '\0'; + /* echo response header data */ + LOG_D("%s", mime_buffer); + session->header->length += rc; if (session->header->length >= session->header->size) @@ -1423,8 +1448,8 @@ int webclient_response(struct webclient_session *session, unsigned char **respon int result_sz; result_sz = session->content_length; - response_buf = web_malloc(result_sz + 1); - if (!response_buf) + response_buf = web_calloc(1, result_sz + 1); + if (response_buf == RT_NULL) { return -WEBCLIENT_NOMEM; } @@ -1456,6 +1481,59 @@ int webclient_response(struct webclient_session *session, unsigned char **respon return total_read; } +/** + * add request(GET/POST) header data. + * + * @param request_header add request buffer address + * @param fmt fields format + * + * @return <=0: add header failed + * >0: add header data size + */ + +int webclient_request_header_add(char **request_header, const char *fmt, ...) +{ + rt_int32_t length, header_length; + char *header; + va_list args; + + RT_ASSERT(request_header); + + if (*request_header == RT_NULL) + { + header = rt_calloc(1, WEBCLIENT_HEADER_BUFSZ); + if (header == RT_NULL) + { + LOG_E("No memory for webclient request header add."); + return RT_NULL; + } + *request_header = header; + } + else + { + header = *request_header; + } + + va_start(args, fmt); + header_length = rt_strlen(header); + length = rt_vsnprintf(header + header_length, WEBCLIENT_HEADER_BUFSZ - header_length, fmt, args); + if (length < 0) + { + LOG_E("add request header data failed, return length(%d) error.", length); + return -WEBCLIENT_ERROR; + } + va_end(args); + + /* check header size */ + if (rt_strlen(header) >= WEBCLIENT_HEADER_BUFSZ) + { + LOG_E("not enough request header data size(%d)!", WEBCLIENT_HEADER_BUFSZ); + return -WEBCLIENT_ERROR; + } + + return length; +} + /** * send request(GET/POST) to server and get response data. * @@ -1487,6 +1565,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data if (post_data == RT_NULL) { + /* send get request */ session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ); if (session == RT_NULL) { @@ -1496,7 +1575,15 @@ int webclient_request(const char *URI, const char *header, const char *post_data if (header != RT_NULL) { - rt_strncpy(session->header->buffer, header, rt_strlen(header)); + char *header_str, *header_ptr; + int header_line_length; + + for(header_str = (char *)header; (header_ptr = rt_strstr(header_str, "\r\n")) != RT_NULL; ) + { + header_line_length = header_ptr + rt_strlen("\r\n") - header_str; + webclient_header_fields_add(session, "%.*s", header_line_length, header_str); + header_str += header_line_length; + } } if (webclient_get(session, URI) != 200) @@ -1514,6 +1601,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data } else { + /* send post request */ session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ); if (session == RT_NULL) { @@ -1523,12 +1611,24 @@ int webclient_request(const char *URI, const char *header, const char *post_data if (header != RT_NULL) { - rt_strncpy(session->header->buffer, header, rt_strlen(header)); + char *header_str, *header_ptr; + int header_line_length; + + for(header_str = (char *)header; (header_ptr = rt_strstr(header_str, "\r\n")) != RT_NULL; ) + { + header_line_length = header_ptr + rt_strlen("\r\n") - header_str; + webclient_header_fields_add(session, "%.*s", header_line_length, header_str); + header_str += header_line_length; + } } - else + + if (rt_strstr(session->header->buffer, "Content-Length") == RT_NULL) { - /* build header for upload */ webclient_header_fields_add(session, "Content-Length: %d\r\n", rt_strlen(post_data)); + } + + if (rt_strstr(session->header->buffer, "Content-Type") == RT_NULL) + { webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n"); } @@ -1546,11 +1646,6 @@ int webclient_request(const char *URI, const char *header, const char *post_data } } - if (header != RT_NULL) - { - rt_strncpy(session->header->buffer, header, rt_strlen(header)); - } - __exit: if (session) {