From 0110bb29b4bab1ad58b7fb204634f29ff9151132 Mon Sep 17 00:00:00 2001 From: armink Date: Thu, 14 Feb 2019 15:40:23 +0800 Subject: [PATCH 01/32] =?UTF-8?q?=E3=80=90=E5=AE=8C=E5=96=84=E3=80=91wget?= =?UTF-8?q?=20=E5=91=BD=E4=BB=A4=E6=8F=90=E7=A4=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- src/webclient_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webclient_file.c b/src/webclient_file.c index f06c235..a9629c3 100644 --- a/src/webclient_file.c +++ b/src/webclient_file.c @@ -277,7 +277,7 @@ int wget(int argc, char** argv) { if (argc != 3) { - rt_kprintf("Please using: wget "); + rt_kprintf("Please using: wget \n"); return -1; } From 568810ff9dbec181393504242f72e7fe9f0cea20 Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Mon, 4 Mar 2019 17:18:20 +0800 Subject: [PATCH 02/32] =?UTF-8?q?=E3=80=90=E6=B7=BB=E5=8A=A0=E3=80=91?= =?UTF-8?q?=E5=BC=80=E5=90=AF=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97=EF=BC=8C?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E8=AF=B7=E6=B1=82=E5=92=8C=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E5=A4=B4=E9=83=A8=E4=BF=A1=E6=81=AF=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- samples/webclient_get_sample.c | 2 +- src/webclient.c | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/samples/webclient_get_sample.c b/samples/webclient_get_sample.c index 4c0b9ee..7df41b7 100644 --- a/samples/webclient_get_sample.c +++ b/samples/webclient_get_sample.c @@ -49,7 +49,7 @@ int webclient_get_test(int argc, char **argv) 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"); diff --git a/src/webclient.c b/src/webclient.c index 9d68fa1..5301a95 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) From 20aad8f2507130f08c9310741e3339dce041d363 Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Tue, 5 Mar 2019 00:21:25 +0800 Subject: [PATCH 03/32] =?UTF-8?q?=E3=80=90=E6=B7=BB=E5=8A=A0=E3=80=91?= =?UTF-8?q?=E5=A4=B4=E9=83=A8=E6=95=B0=E6=8D=AE=E6=8B=BC=E6=8E=A5=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E9=80=82=E7=94=A8=E4=BA=8E=20webclient=5Freq?= =?UTF-8?q?uest=20=E6=8E=A5=E5=8F=A3=20=E3=80=90=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E3=80=91webclient=5Frequest=20=E6=8E=A5=E5=8F=A3=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E7=9A=84=20GET/POST=20=E8=AF=B7=E6=B1=82=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- inc/webclient.h | 1 + samples/webclient_get_sample.c | 62 +++++++++++++++++++++- samples/webclient_post_sample.c | 69 ++++++++++++++++++++++++- src/webclient.c | 92 +++++++++++++++++++++++++++++---- 4 files changed, 209 insertions(+), 15 deletions(-) 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 7df41b7..87e4a4a 100644 --- a/samples/webclient_get_sample.c +++ b/samples/webclient_get_sample.c @@ -74,7 +74,7 @@ int webclient_get_test(int argc, char **argv) goto __exit; } - rt_kprintf("webclient GET request response data :\n"); + rt_kprintf("get response data: \n"); content_length = webclient_content_length_get(session); if (content_length < 0) @@ -140,7 +140,65 @@ __exit: return ret; } + +int webclient_get_smpl_test(int argc, char **argv) +{ + char *URI, *request; + int index; + + 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_smpl_test [URI] - webclient simplify GET request test.\n"); + return -1; + } + + if (webclient_request(URI, RT_NULL, RT_NULL, (unsigned char **)&request) < 0) + { + rt_kprintf("webclient send get request failed."); + web_free(URI); + return -1; + } + + rt_kprintf("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); + } + + if (URI) + { + web_free(URI); + } + + 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, web_get [URI] webclient GET request test); +MSH_CMD_EXPORT_ALIAS(webclient_get_smpl_test, web_get_smpl, web_get_smpl [URI] webclient simplify GET request test); #endif /* FINSH_USING_MSH */ diff --git a/samples/webclient_post_sample.c b/samples/webclient_post_sample.c index 538e01e..ef7e34a 100644 --- a/samples/webclient_post_sample.c +++ b/samples/webclient_post_sample.c @@ -81,7 +81,7 @@ int webclient_post_test(int argc, char **argv) goto __exit; } - rt_kprintf("webclient POST request response data :\n"); + rt_kprintf("post response data: \n"); do { bytes_read = webclient_read(session, buffer, POST_RESP_BUFSZ); @@ -117,7 +117,72 @@ __exit: return ret; } +int webclient_post_smpl_test(int argc, char **argv) +{ + char *URI, *request, *header = RT_NULL; + int index; + + 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_smpl_test [URI] - webclient simplify POST request test.\n"); + return -1; + } + + 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) + { + rt_kprintf("webclient send post request failed."); + web_free(header); + web_free(URI); + return -1; + } + + rt_kprintf("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 (URI) + { + web_free(URI); + } + + if (request) + { + web_free(request); + } + 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, web_post [URI] - webclient POST request test.); +MSH_CMD_EXPORT_ALIAS(webclient_post_smpl_test, web_post_smpl, web_post_smpl [URI] - webclient simplify POST request test.); #endif /* FINSH_USING_MSH */ diff --git a/src/webclient.c b/src/webclient.c index 5301a95..4d222d0 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -1448,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; } @@ -1481,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. * @@ -1512,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) { @@ -1521,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) @@ -1539,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) { @@ -1548,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"); } @@ -1571,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) { From 0c484f4a538668029f9a507fe9c902afa102202b Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Tue, 5 Mar 2019 11:33:51 +0800 Subject: [PATCH 04/32] =?UTF-8?q?=E3=80=90=E5=AE=8C=E5=96=84=E3=80=91GET/P?= =?UTF-8?q?OST=20=E4=BE=8B=E7=A8=8B=E7=9B=B8=E5=85=B3=E7=9A=84=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- samples/webclient_get_sample.c | 138 +++++++++++++++--------------- samples/webclient_post_sample.c | 144 ++++++++++++++++---------------- 2 files changed, 144 insertions(+), 138 deletions(-) diff --git a/samples/webclient_get_sample.c b/samples/webclient_get_sample.c index 87e4a4a..17bbfcb 100644 --- a/samples/webclient_get_sample.c +++ b/samples/webclient_get_sample.c @@ -16,39 +16,15 @@ #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_RESP_BUFSZ); if (buffer == RT_NULL) { @@ -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("get response data: \n"); + rt_kprintf("webclient get response data: \n"); content_length = webclient_content_length_get(session); if (content_length < 0) @@ -132,52 +108,23 @@ __exit: web_free(buffer); } - if (URI) - { - web_free(URI); - } - return ret; } - -int webclient_get_smpl_test(int argc, char **argv) +/* send HTTP GET request by simplify request interface, it used to received shorter data */ +static int webclient_get_smpl(const char *uri) { - char *URI, *request; + char *request = RT_NULL; int index; - 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_smpl_test [URI] - webclient simplify GET request test.\n"); - return -1; - } - - if (webclient_request(URI, RT_NULL, RT_NULL, (unsigned char **)&request) < 0) + if (webclient_request(uri, RT_NULL, RT_NULL, (unsigned char **)&request) < 0) { rt_kprintf("webclient send get request failed."); - web_free(URI); - return -1; + return -RT_ERROR; } - rt_kprintf("get response data: \n"); + 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]); @@ -189,9 +136,65 @@ int webclient_get_smpl_test(int argc, char **argv) web_free(request); } - if (URI) + 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 { - web_free(URI); + 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; @@ -199,6 +202,5 @@ int webclient_get_smpl_test(int argc, char **argv) #ifdef FINSH_USING_MSH #include -MSH_CMD_EXPORT_ALIAS(webclient_get_test, web_get, web_get [URI] webclient GET request test); -MSH_CMD_EXPORT_ALIAS(webclient_get_smpl_test, web_get_smpl, web_get_smpl [URI] webclient simplify 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 ef7e34a..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("post response data: \n"); + rt_kprintf("webclient post response data: \n"); do { bytes_read = webclient_read(session, buffer, POST_RESP_BUFSZ); @@ -109,55 +84,27 @@ __exit: web_free(buffer); } - if (URI) - { - web_free(URI); - } - return ret; } -int webclient_post_smpl_test(int argc, char **argv) +/* 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 *URI, *request, *header = RT_NULL; + char *request = RT_NULL, *header = RT_NULL; int index; - 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_smpl_test [URI] - webclient simplify POST request test.\n"); - return -1; - } - 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) + if (webclient_request(uri, (const char *)header, post_data, (unsigned char **)&request) < 0) { rt_kprintf("webclient send post request failed."); web_free(header); - web_free(URI); - return -1; + return -RT_ERROR; } - rt_kprintf("post response data: \n"); + 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]); @@ -169,20 +116,77 @@ int webclient_post_smpl_test(int argc, char **argv) web_free(header); } - if (URI) + if (request) { - web_free(URI); + web_free(request); } - if (request) + return 0; +} + + +int webclient_post_test(int argc, char **argv) +{ + char *uri = RT_NULL; + + if (argc == 1) { - web_free(request); + 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, web_post [URI] - webclient POST request test.); -MSH_CMD_EXPORT_ALIAS(webclient_post_smpl_test, web_post_smpl, web_post_smpl [URI] - webclient simplify POST request test.); +MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post_test, webclient post request test.); #endif /* FINSH_USING_MSH */ From 9d5741daf2280be3965bac50df3f83d44663d5ba Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Tue, 5 Mar 2019 13:40:58 +0800 Subject: [PATCH 05/32] =?UTF-8?q?=E3=80=90=E6=9B=B4=E6=96=B0=E3=80=91UM=20?= =?UTF-8?q?=E5=92=8C=20README=20=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- README.md | 1 + docs/api.md | 17 +++++++++++++++++ docs/samples.md | 27 +++++++++++++++++++-------- docs/user-guide.md | 17 ++++++++++++++--- 4 files changed, 51 insertions(+), 11 deletions(-) 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`。 ### 头部数据长度超出 From 8971d528e23b75b339abf70c3a5be9fc254684a8 Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Tue, 5 Mar 2019 16:43:35 +0800 Subject: [PATCH 06/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91chunk?= =?UTF-8?q?=20=E6=A8=A1=E5=BC=8F=E4=B8=8B=E8=8E=B7=E5=8F=96=20chunk=20size?= =?UTF-8?q?=20=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- README.md | 2 ++ src/webclient.c | 1 + 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index 8e4f1f5..b83d482 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,8 @@ RT-Thread online packages Version (latest) ---> ``` +**Enable debug log output**:开启调试日志显示,可以用于查看请求和响应的头部数据信息; + **Enable webclient GET/POST samples** :添加示例代码; **Select TLS mode** :配置开启 HTTPS 支持,选择支持的模式; diff --git a/src/webclient.c b/src/webclient.c index 4d222d0..55de69a 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -1095,6 +1095,7 @@ static int webclient_next_chunk(struct webclient_session *session) RT_ASSERT(session); + rt_memset(line, 0x00, sizeof(line)); length = webclient_read_line(session, line, sizeof(line)); if (length > 0) { From a028b1322e3c3fe658f39dda79f2fe76c4563224 Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Mon, 11 Mar 2019 11:11:41 +0800 Subject: [PATCH 07/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91chunk?= =?UTF-8?q?=20=E6=A8=A1=E5=BC=8F=E8=8E=B7=E5=8F=96=20chunk=5Fsz=20=3D=200?= =?UTF-8?q?=20=E6=97=B6=EF=BC=8Cwebclient=5Frecv=20=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- src/webclient.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/webclient.c b/src/webclient.c index 55de69a..5497dd1 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -1126,6 +1126,7 @@ static int webclient_next_chunk(struct webclient_session *session) /* end of chunks */ closesocket(session->socket); session->socket = -1; + session->chunk_sz = -1; } return session->chunk_sz; @@ -1150,6 +1151,12 @@ int webclient_read(struct webclient_session *session, unsigned char *buffer, siz RT_ASSERT(session); + /* get next chunk size is zero, client is already closed, return zero */ + if (session->chunk_sz < 0) + { + return 0; + } + if (session->socket < 0) { return -WEBCLIENT_DISCONNECT; From 7eadaac3fc42888eeacb212c30df5d77c4efe8d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=81=E5=85=89?= <1004383796@qq.com> Date: Thu, 28 Mar 2019 09:52:13 +0800 Subject: [PATCH 08/32] =?UTF-8?q?[=E4=BC=98=E5=8C=96]=20=E5=B0=86get/get?= =?UTF-8?q?=5Fposition/post=E6=89=93=E5=8D=B0=E5=93=8D=E5=BA=94=E7=A0=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BALOG=5FD=E6=89=93=E5=8D=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/webclient.c b/src/webclient.c index 5497dd1..8e9fddb 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -889,6 +889,9 @@ int webclient_get(struct webclient_session *session, const char *URI) /* 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"); @@ -919,11 +922,6 @@ int webclient_get(struct webclient_session *session, const char *URI) web_free(new_url); return rc; } - else if (resp_status != 200) - { - LOG_E("get failed, handle response(%d) error!", resp_status); - return resp_status; - } } return resp_status; @@ -968,6 +966,9 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i /* 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"); @@ -998,11 +999,6 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i web_free(new_url); return rc; } - else if (resp_status != 206) - { - LOG_E("get failed, handle response(%d) error!", resp_status); - return resp_status; - } } return resp_status; @@ -1050,11 +1046,7 @@ int webclient_post(struct webclient_session *session, const char *URI, const cha /* resolve response data, get http status code */ resp_status = webclient_handle_response(session); - if (resp_status != 200) - { - LOG_E("post failed, handle response(%d) error.", resp_status); - return resp_status; - } + LOG_D("post handle response(%d).", resp_status); } return resp_status; From 10ece6296d7ea0686aede55ac0a9af57663c6ae8 Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Fri, 10 May 2019 18:10:03 +0800 Subject: [PATCH 09/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- samples/webclient_get_sample.c | 7 ++++++- samples/webclient_post_sample.c | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/samples/webclient_get_sample.c b/samples/webclient_get_sample.c index 17bbfcb..4697e97 100644 --- a/samples/webclient_get_sample.c +++ b/samples/webclient_get_sample.c @@ -197,7 +197,12 @@ int webclient_get_test(int argc, char **argv) return -RT_ERROR; } - return 0; + if (uri) + { + web_free(uri); + } + + return RT_EOK; } #ifdef FINSH_USING_MSH diff --git a/samples/webclient_post_sample.c b/samples/webclient_post_sample.c index caa026c..30b225c 100644 --- a/samples/webclient_post_sample.c +++ b/samples/webclient_post_sample.c @@ -181,8 +181,13 @@ int webclient_post_test(int argc, char **argv) rt_kprintf("web_post_test -s [uri] - webclient simplify post request test.\n"); return -RT_ERROR; } + + if (uri) + { + web_free(uri); + } - return 0; + return RT_EOK; } From 72bb25e2cbe92ddb02974febbbcdaeb210713dd0 Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Wed, 15 May 2019 09:39:08 +0800 Subject: [PATCH 10/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91webcl?= =?UTF-8?q?ient=5Frecv=20=E5=87=BD=E6=95=B0=E4=B8=AD=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E5=87=BA=E9=94=99=E6=97=B6=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=BA=A7=E5=88=AB=E4=B8=BA=20LOG=5FD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- src/webclient.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webclient.c b/src/webclient.c index 8e9fddb..6ca01a8 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -1223,7 +1223,7 @@ int webclient_read(struct webclient_session *session, unsigned char *buffer, siz } #endif - LOG_E("receive data error(%d).", bytes_read); + LOG_D("receive data error(%d).", bytes_read); if (total_read) { From dcd13c5b39d9115fe360c5dd37effff588558686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=81=E5=85=89?= <1004383796@qq.com> Date: Fri, 24 May 2019 14:30:07 +0800 Subject: [PATCH 11/32] =?UTF-8?q?[=E4=BF=AE=E5=A4=8D]=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=BD=93=E4=BD=BF=E7=94=A8=20webclinet=5Fget=5Fpostion=20?= =?UTF-8?q?=E5=8E=BBget=20301/302=20=E9=87=8D=E5=AE=9A=E5=90=91=E7=9A=84?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=EF=BC=8C=E9=87=8D=E5=AE=9A=E5=90=91=E5=90=8E?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E7=9A=84=E4=BB=8E=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E7=BD=AEget=E7=9A=84=E9=97=AE=E9=A2=98.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webclient.c b/src/webclient.c index 8e9fddb..e096790 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -994,7 +994,7 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i return -WEBCLIENT_NOMEM; } - rc = webclient_get(session, new_url); + rc = webclient_get_position(session, new_url, position); web_free(new_url); return rc; From 1d95c336a81710ae3b997519bd39bdd4d0c48dc2 Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Fri, 24 May 2019 20:12:31 +0800 Subject: [PATCH 12/32] =?UTF-8?q?=E3=80=90=E6=9B=B4=E6=96=B0=E3=80=91?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7=20V2.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- inc/webclient.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/webclient.h b/inc/webclient.h index d046a6e..45e70bd 100644 --- a/inc/webclient.h +++ b/inc/webclient.h @@ -47,8 +47,8 @@ extern "C" { #define web_strdup rt_strdup #endif -#define WEBCLIENT_SW_VERSION "2.0.1" -#define WEBCLIENT_SW_VERSION_NUM 0x20001 +#define WEBCLIENT_SW_VERSION "2.1.0" +#define WEBCLIENT_SW_VERSION_NUM 0x20100 #define WEBCLIENT_HEADER_BUFSZ 4096 #define WEBCLIENT_RESPONSE_BUFSZ 4096 From 0b2643919147236c68925c0e7e787de1364ac0ac Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Fri, 24 May 2019 20:15:54 +0800 Subject: [PATCH 13/32] =?UTF-8?q?=E3=80=90=E6=9B=B4=E6=96=B0=E3=80=91?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7=20V2.1.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- inc/webclient.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/webclient.h b/inc/webclient.h index 45e70bd..d48072e 100644 --- a/inc/webclient.h +++ b/inc/webclient.h @@ -47,8 +47,8 @@ extern "C" { #define web_strdup rt_strdup #endif -#define WEBCLIENT_SW_VERSION "2.1.0" -#define WEBCLIENT_SW_VERSION_NUM 0x20100 +#define WEBCLIENT_SW_VERSION "2.1.1" +#define WEBCLIENT_SW_VERSION_NUM 0x20101 #define WEBCLIENT_HEADER_BUFSZ 4096 #define WEBCLIENT_RESPONSE_BUFSZ 4096 From bb37a77c770d8f54449c1845065abdd694adf204 Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Mon, 24 Jun 2019 14:55:58 +0800 Subject: [PATCH 14/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91atoi?= =?UTF-8?q?=20=E5=87=BD=E6=95=B0=E7=BC=96=E8=AF=91=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- src/webclient.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/webclient.c b/src/webclient.c index 021bcc1..e1238bc 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -14,6 +14,9 @@ * 2018-08-07 chenyong modify header processing */ +#include +#include + #include #include From 4afcfe0b42c014c69bc51f070262001d9cfda077 Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Wed, 11 Sep 2019 22:11:18 +0800 Subject: [PATCH 15/32] =?UTF-8?q?=E3=80=90=E6=B7=BB=E5=8A=A0=E3=80=91?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=E5=8A=9F=E8=83=BD=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong <1521761801@qq.com> --- README.md | 3 +++ SConscript | 5 ++++- docs/samples.md | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b83d482..2ad914f 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ RT-Thread online packages [*] WebClient: A HTTP/HTTPS Client for RT-Thread [ ] Enable debug log output [ ] Enable webclient GET/POST samples + [ ] Enable file download feature support Select TLS mode (Not support) ---> (x) Not support ( ) SAL TLS support @@ -70,6 +71,8 @@ RT-Thread online packages **Enable webclient GET/POST samples** :添加示例代码; +**Enable file download feature support** :开启文件下载功能支持(`wget` 命令支持); + **Select TLS mode** :配置开启 HTTPS 支持,选择支持的模式; - **Not support**:不支持 TLS 功能; diff --git a/SConscript b/SConscript index 86086e9..ae8dbd4 100644 --- a/SConscript +++ b/SConscript @@ -3,7 +3,10 @@ from building import * cwd = GetCurrentDir() path = [cwd + '/inc'] -src = Glob('src/*.c') +src = Glob('src/webclient.c') + +if GetDepend(['WEBCLIENT_USING_FILE_DOWMLOAD']): + src += Glob('src/webclient_file.c') if GetDepend(['WEBCLIENT_USING_SAMPLES']): src += Glob('samples/*.c') diff --git a/docs/samples.md b/docs/samples.md index 41eaf3b..5ac0dab 100644 --- a/docs/samples.md +++ b/docs/samples.md @@ -25,6 +25,7 @@ RT-Thread online packages [*] WebClient: A HTTP/HTTPS Client for RT-Thread [ ] Enable debug log output [*] Enable webclient GET/POST samples # 开启 WebClient 测试例程 + [ ] Enable file download feature support Select TLS mode (Not support) ---> Version (latest) ---> # 开启使用最新版本软件包 ``` From dbdc22e3ab052ede574508b0c436b1570ba8f534 Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Sat, 9 Nov 2019 15:42:44 +0800 Subject: [PATCH 16/32] [fix] used uninitialized var Signed-off-by: MurphyZhao --- src/webclient.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webclient.c b/src/webclient.c index e1238bc..a11d5e8 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -1556,7 +1556,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data { struct webclient_session *session; int rc = WEBCLIENT_OK; - int totle_length; + int totle_length = 0; RT_ASSERT(URI); From d66c291fd69a6751af79a6565f0ad9b2f638aeb7 Mon Sep 17 00:00:00 2001 From: chenyong Date: Wed, 13 Nov 2019 00:21:19 +0800 Subject: [PATCH 17/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91webcl?= =?UTF-8?q?ient=5Fpost=5Ffile=20=E5=87=BD=E6=95=B0=20POST=20=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong --- src/webclient.c | 50 ++++++++++++++++++++++---------------------- src/webclient_file.c | 44 ++++++++++++++++++++++++++++++-------- 2 files changed, 60 insertions(+), 34 deletions(-) diff --git a/src/webclient.c b/src/webclient.c index a11d5e8..2588f26 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -65,7 +65,7 @@ static int webclient_recv(struct webclient_session* session, unsigned char *buff { return mbedtls_client_read(session->tls_session, buffer, len); } -#endif +#endif return recv(session->socket, buffer, len, flag); } @@ -86,8 +86,8 @@ static int webclient_read_line(struct webclient_session *session, char *buffer, if (session->is_tls && (rc == MBEDTLS_ERR_SSL_WANT_READ || rc == MBEDTLS_ERR_SSL_WANT_WRITE)) { continue; - } -#endif + } +#endif if (rc <= 0) return rc; @@ -195,7 +195,7 @@ static int webclient_resolve_address(struct webclient_session *session, struct a } host_addr_len = ptr - host_addr; *request = (char *) ptr; - + /* resolve port */ port_ptr = rt_strstr(host_addr, ":"); if (port_ptr && port_ptr < ptr) @@ -241,7 +241,7 @@ static int webclient_resolve_address(struct webclient_session *session, struct a { return -WEBCLIENT_NOMEM; } - + return rc; } #endif @@ -356,7 +356,7 @@ static int webclient_connect(struct webclient_session *session, const char *URI) { LOG_E("connect failed, https client open URI(%s) failed!", URI); return -WEBCLIENT_ERROR; - } + } session->is_tls = RT_TRUE; #else LOG_E("not support https connect, please enable webclient https configure!"); @@ -673,17 +673,17 @@ 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); @@ -694,7 +694,7 @@ static int webclient_send_header(struct webclient_session *session, int method) LOG_RAW("\n"); #endif } - + __exit: return rc; } @@ -746,7 +746,7 @@ int webclient_handle_response(struct webclient_session *session) /* echo response header data */ LOG_D("%s", mime_buffer); - + session->header->length += rc; if (session->header->length >= session->header->size) @@ -843,7 +843,7 @@ struct webclient_session *webclient_session_create(size_t header_sz) } session->header->size = header_sz; - session->header->buffer = (char *) web_malloc(header_sz); + session->header->buffer = (char *) web_calloc(1, header_sz); if (session->header->buffer == RT_NULL) { LOG_E("webclient create failed, no memory for session header buffer!"); @@ -893,7 +893,7 @@ int webclient_get(struct webclient_session *session, const char *URI) /* handle the response header of webclient server */ resp_status = webclient_handle_response(session); - LOG_D("get position handle response(%d).", resp_status); + LOG_D("get position handle response(%d).", resp_status); if (resp_status > 0) { @@ -970,7 +970,7 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i /* handle the response header of webclient server */ resp_status = webclient_handle_response(session); - LOG_D("get position handle response(%d).", resp_status); + LOG_D("get position handle response(%d).", resp_status); if (resp_status > 0) { @@ -1049,7 +1049,7 @@ int webclient_post(struct webclient_session *session, const char *URI, const cha /* resolve response data, get http status code */ resp_status = webclient_handle_response(session); - LOG_D("post handle response(%d).", resp_status); + LOG_D("post handle response(%d).", resp_status); } return resp_status; @@ -1219,13 +1219,13 @@ int webclient_read(struct webclient_session *session, unsigned char *buffer, siz if (bytes_read <= 0) { #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS) - if(session->is_tls && + if(session->is_tls && (bytes_read == MBEDTLS_ERR_SSL_WANT_READ || bytes_read == MBEDTLS_ERR_SSL_WANT_WRITE)) { continue; } - -#endif + +#endif LOG_D("receive data error(%d).", bytes_read); if (total_read) @@ -1298,7 +1298,7 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff if (bytes_write <= 0) { #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS) - if(session->is_tls && + if(session->is_tls && (bytes_write == MBEDTLS_ERR_SSL_WANT_READ || bytes_write == MBEDTLS_ERR_SSL_WANT_WRITE)) { continue; @@ -1346,7 +1346,7 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff int webclient_close(struct webclient_session *session) { RT_ASSERT(session); - + #ifdef WEBCLIENT_USING_MBED_TLS if (session->tls_session) { @@ -1356,7 +1356,7 @@ int webclient_close(struct webclient_session *session) { if (session->socket >= 0) { - closesocket(session->socket); + closesocket(session->socket); session->socket = -1; } } @@ -1485,7 +1485,7 @@ int webclient_response(struct webclient_session *session, unsigned char **respon } /** - * add request(GET/POST) header data. + * add request(GET/POST) header data. * * @param request_header add request buffer address * @param fmt fields format @@ -1583,7 +1583,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data 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; + 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; } @@ -1619,7 +1619,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data 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; + 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; } @@ -1640,7 +1640,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data rc = -WEBCLIENT_ERROR; goto __exit; } - + totle_length = webclient_response(session, response); if (totle_length <= 0) { diff --git a/src/webclient_file.c b/src/webclient_file.c index a9629c3..598e90d 100644 --- a/src/webclient_file.c +++ b/src/webclient_file.c @@ -74,7 +74,7 @@ int webclient_get_file(const char* URI, const char* filename) rc = -WEBCLIENT_NOMEM; goto __exit; } - + if (session->content_length < 0) { while (1) @@ -158,6 +158,7 @@ int webclient_post_file(const char* URI, const char* filename, char *header = RT_NULL, *header_ptr; unsigned char *buffer = RT_NULL, *buffer_ptr; struct webclient_session* session = RT_NULL; + int resp_data_len = 0; fd = open(filename, O_RDONLY, 0); if (fd < 0) @@ -171,7 +172,7 @@ int webclient_post_file(const char* URI, const char* filename, length = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); - buffer = (unsigned char *) web_malloc(WEBCLIENT_RESPONSE_BUFSZ); + buffer = (unsigned char *) web_calloc(1, WEBCLIENT_RESPONSE_BUFSZ); if (buffer == RT_NULL) { LOG_D("post file failed, no memory for response buffer."); @@ -179,7 +180,7 @@ int webclient_post_file(const char* URI, const char* filename, goto __exit; } - header = (char *) web_malloc(WEBCLIENT_HEADER_BUFSZ); + header = (char *) web_calloc(1, WEBCLIENT_HEADER_BUFSZ); if (header == RT_NULL) { LOG_D("post file failed, no memory for header buffer."); @@ -204,7 +205,7 @@ int webclient_post_file(const char* URI, const char* filename, "Content-Type: application/octet-stream\r\n\r\n"); /* calculate content-length */ length += buffer_ptr - buffer; - length += rt_strlen(boundary) + 6; /* add the last boundary */ + length += rt_strlen(boundary) + 8; /* add the last boundary */ /* build header for upload */ header_ptr += rt_snprintf(header_ptr, @@ -221,10 +222,11 @@ int webclient_post_file(const char* URI, const char* filename, goto __exit; } - session->header->buffer = web_strdup(header); + rt_strncpy(session->header->buffer, header, rt_strlen(header)); + session->header->length = rt_strlen(session->header->buffer); rc = webclient_post(session, URI, NULL); - if( rc< 0) + if(rc < 0) { goto __exit; } @@ -246,7 +248,32 @@ int webclient_post_file(const char* URI, const char* filename, /* send last boundary */ rt_snprintf((char*) buffer, WEBCLIENT_RESPONSE_BUFSZ, "\r\n--%s--\r\n", boundary); - webclient_write(session, buffer, rt_strlen(boundary) + 6); + webclient_write(session, buffer, rt_strlen(boundary) + 8); + + extern int webclient_handle_response(struct webclient_session *session); + if( webclient_handle_response(session) != 200) + { + rc = -WEBCLIENT_ERROR; + goto __exit; + } + + resp_data_len = webclient_content_length_get(session); + if (resp_data_len > 0) + { + int bytes_read = 0; + + rt_memset(buffer, 0x00, WEBCLIENT_RESPONSE_BUFSZ); + do + { + bytes_read = webclient_read(session, buffer, + resp_data_len < WEBCLIENT_RESPONSE_BUFSZ ? resp_data_len : WEBCLIENT_RESPONSE_BUFSZ); + if (bytes_read <= 0) + { + break; + } + resp_data_len -= bytes_read; + } while(resp_data_len > 0); + } __exit: if (fd >= 0) @@ -269,10 +296,9 @@ __exit: web_free(header); } - return 0; + return rc; } - int wget(int argc, char** argv) { if (argc != 3) From dd0f9f88b302349f818644cb63ebbda3e793fccb Mon Sep 17 00:00:00 2001 From: chenyong Date: Wed, 13 Nov 2019 10:24:08 +0800 Subject: [PATCH 18/32] =?UTF-8?q?=E3=80=90=E6=9B=B4=E6=96=B0=E3=80=91?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7=20V2.1.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong --- inc/webclient.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/webclient.h b/inc/webclient.h index d48072e..b4ea163 100644 --- a/inc/webclient.h +++ b/inc/webclient.h @@ -47,8 +47,8 @@ extern "C" { #define web_strdup rt_strdup #endif -#define WEBCLIENT_SW_VERSION "2.1.1" -#define WEBCLIENT_SW_VERSION_NUM 0x20101 +#define WEBCLIENT_SW_VERSION "2.1.2" +#define WEBCLIENT_SW_VERSION_NUM 0x20102 #define WEBCLIENT_HEADER_BUFSZ 4096 #define WEBCLIENT_RESPONSE_BUFSZ 4096 From 546f62c5fc88ff2a74cd622c01cc7009a4a2fe30 Mon Sep 17 00:00:00 2001 From: liangyingjian Date: Tue, 19 Nov 2019 19:26:57 +0800 Subject: [PATCH 19/32] =?UTF-8?q?[update]=20=E6=B7=BB=E5=8A=A0=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=AF=86=E5=88=AB=E9=83=A8=E5=88=86=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E8=BF=94=E5=9B=9E=20header=20=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E4=B8=AD=E7=9A=84"Content-Type"=20=E4=B8=BA=E5=B0=8F=E5=86=99?= =?UTF-8?q?=E7=9A=84"content-type"=E7=9A=84=E5=8A=9F=E8=83=BD=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 94 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 29 deletions(-) diff --git a/src/webclient.c b/src/webclient.c index 2588f26..2ff1ff4 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -13,7 +13,6 @@ * 2018-07-26 chenyong modify log information * 2018-08-07 chenyong modify header processing */ - #include #include @@ -46,6 +45,45 @@ extern long int strtol(const char *nptr, char **endptr, int base); +static int _tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + c ^= 0x20; + return c; +} + +static int webclient_strncasecmp(const char *a, const char *b, size_t n) +{ + uint8_t c1, c2; + if (n <= 0) + return 0; + do { + c1 = _tolower(*a++); + c2 = _tolower(*b++); + } while (--n && c1 && c1 == c2); + return c1 - c2; +} + +static const char *webclient_strstri(const char* str, const char* subStr) +{ + int len = strlen(subStr); + + if(len == 0) + { + return RT_NULL; + } + + while(*str) + { + if(webclient_strncasecmp(str, subStr, len) == 0) + { + return str; + } + ++str; + } + return RT_NULL; +} + static int webclient_send(struct webclient_session* session, const unsigned char *buffer, size_t len, int flag) { #ifdef WEBCLIENT_USING_MBED_TLS @@ -65,7 +103,7 @@ static int webclient_recv(struct webclient_session* session, unsigned char *buff { return mbedtls_client_read(session->tls_session, buffer, len); } -#endif +#endif return recv(session->socket, buffer, len, flag); } @@ -86,8 +124,8 @@ static int webclient_read_line(struct webclient_session *session, char *buffer, if (session->is_tls && (rc == MBEDTLS_ERR_SSL_WANT_READ || rc == MBEDTLS_ERR_SSL_WANT_WRITE)) { continue; - } -#endif + } +#endif if (rc <= 0) return rc; @@ -195,7 +233,7 @@ static int webclient_resolve_address(struct webclient_session *session, struct a } host_addr_len = ptr - host_addr; *request = (char *) ptr; - + /* resolve port */ port_ptr = rt_strstr(host_addr, ":"); if (port_ptr && port_ptr < ptr) @@ -241,7 +279,7 @@ static int webclient_resolve_address(struct webclient_session *session, struct a { return -WEBCLIENT_NOMEM; } - + return rc; } #endif @@ -356,7 +394,7 @@ static int webclient_connect(struct webclient_session *session, const char *URI) { LOG_E("connect failed, https client open URI(%s) failed!", URI); return -WEBCLIENT_ERROR; - } + } session->is_tls = RT_TRUE; #else LOG_E("not support https connect, please enable webclient https configure!"); @@ -530,7 +568,7 @@ const char *webclient_header_fields_get(struct webclient_session *session, const resp_buf = session->header->buffer; while (resp_buf_len < session->header->length) { - if (rt_strstr(resp_buf, fields)) + if (webclient_strstri(resp_buf, fields)) { char *mime_ptr = RT_NULL; @@ -556,7 +594,6 @@ const char *webclient_header_fields_get(struct webclient_session *session, const return RT_NULL; } - /** * get http response status code. * @@ -673,17 +710,17 @@ 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); @@ -694,7 +731,7 @@ static int webclient_send_header(struct webclient_session *session, int method) LOG_RAW("\n"); #endif } - + __exit: return rc; } @@ -746,7 +783,7 @@ int webclient_handle_response(struct webclient_session *session) /* echo response header data */ LOG_D("%s", mime_buffer); - + session->header->length += rc; if (session->header->length >= session->header->size) @@ -755,7 +792,6 @@ int webclient_handle_response(struct webclient_session *session) return -WEBCLIENT_NOMEM; } } - /* get HTTP status code */ mime_ptr = web_strdup(session->header->buffer); if (mime_ptr == RT_NULL) @@ -893,7 +929,7 @@ int webclient_get(struct webclient_session *session, const char *URI) /* handle the response header of webclient server */ resp_status = webclient_handle_response(session); - LOG_D("get position handle response(%d).", resp_status); + LOG_D("get position handle response(%d).", resp_status); if (resp_status > 0) { @@ -970,7 +1006,7 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i /* handle the response header of webclient server */ resp_status = webclient_handle_response(session); - LOG_D("get position handle response(%d).", resp_status); + LOG_D("get position handle response(%d).", resp_status); if (resp_status > 0) { @@ -1049,7 +1085,7 @@ int webclient_post(struct webclient_session *session, const char *URI, const cha /* resolve response data, get http status code */ resp_status = webclient_handle_response(session); - LOG_D("post handle response(%d).", resp_status); + LOG_D("post handle response(%d).", resp_status); } return resp_status; @@ -1219,13 +1255,13 @@ int webclient_read(struct webclient_session *session, unsigned char *buffer, siz if (bytes_read <= 0) { #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS) - if(session->is_tls && + if(session->is_tls && (bytes_read == MBEDTLS_ERR_SSL_WANT_READ || bytes_read == MBEDTLS_ERR_SSL_WANT_WRITE)) { continue; } - -#endif + +#endif LOG_D("receive data error(%d).", bytes_read); if (total_read) @@ -1298,7 +1334,7 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff if (bytes_write <= 0) { #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS) - if(session->is_tls && + if(session->is_tls && (bytes_write == MBEDTLS_ERR_SSL_WANT_READ || bytes_write == MBEDTLS_ERR_SSL_WANT_WRITE)) { continue; @@ -1346,7 +1382,7 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff int webclient_close(struct webclient_session *session) { RT_ASSERT(session); - + #ifdef WEBCLIENT_USING_MBED_TLS if (session->tls_session) { @@ -1356,7 +1392,7 @@ int webclient_close(struct webclient_session *session) { if (session->socket >= 0) { - closesocket(session->socket); + closesocket(session->socket); session->socket = -1; } } @@ -1485,7 +1521,7 @@ int webclient_response(struct webclient_session *session, unsigned char **respon } /** - * add request(GET/POST) header data. + * add request(GET/POST) header data. * * @param request_header add request buffer address * @param fmt fields format @@ -1554,7 +1590,7 @@ int webclient_request_header_add(char **request_header, const char *fmt, ...) */ int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **response) { - struct webclient_session *session; + struct webclient_session *session = RT_NULL; int rc = WEBCLIENT_OK; int totle_length = 0; @@ -1583,7 +1619,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data 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; + 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; } @@ -1619,7 +1655,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data 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; + 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; } @@ -1640,7 +1676,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data rc = -WEBCLIENT_ERROR; goto __exit; } - + totle_length = webclient_response(session, response); if (totle_length <= 0) { From 7eb9628024661c8a35a53129251d58c24b6fd154 Mon Sep 17 00:00:00 2001 From: liangyingjian Date: Wed, 20 Nov 2019 09:31:22 +0800 Subject: [PATCH 20/32] =?UTF-8?q?[update]=20=E6=95=B4=E7=90=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=B8=AD=E7=9A=84=E7=A9=BA=E8=A1=8C=E5=92=8C=E7=A9=BA?= =?UTF-8?q?=E6=A0=BC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/webclient.c b/src/webclient.c index 2ff1ff4..1bb7f35 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -13,6 +13,7 @@ * 2018-07-26 chenyong modify log information * 2018-08-07 chenyong modify header processing */ + #include #include @@ -594,6 +595,7 @@ const char *webclient_header_fields_get(struct webclient_session *session, const return RT_NULL; } + /** * get http response status code. * From dcbc7de391edeccc51f8f19a403e980714de0fe5 Mon Sep 17 00:00:00 2001 From: liangyingjian Date: Wed, 20 Nov 2019 10:48:19 +0800 Subject: [PATCH 21/32] =?UTF-8?q?[update]=20=E6=95=B4=E7=90=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E5=8E=BB=E6=8E=89=E4=BD=BF=E7=94=A8vscode=20?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=B7=BB=E5=8A=A0=E7=9A=84=E7=A9=BA=E6=A0=BC?= =?UTF-8?q?=E7=AC=A6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/webclient.c b/src/webclient.c index 1bb7f35..10b70e9 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -104,7 +104,7 @@ static int webclient_recv(struct webclient_session* session, unsigned char *buff { return mbedtls_client_read(session->tls_session, buffer, len); } -#endif +#endif return recv(session->socket, buffer, len, flag); } @@ -125,8 +125,8 @@ static int webclient_read_line(struct webclient_session *session, char *buffer, if (session->is_tls && (rc == MBEDTLS_ERR_SSL_WANT_READ || rc == MBEDTLS_ERR_SSL_WANT_WRITE)) { continue; - } -#endif + } +#endif if (rc <= 0) return rc; @@ -234,7 +234,7 @@ static int webclient_resolve_address(struct webclient_session *session, struct a } host_addr_len = ptr - host_addr; *request = (char *) ptr; - + /* resolve port */ port_ptr = rt_strstr(host_addr, ":"); if (port_ptr && port_ptr < ptr) @@ -280,7 +280,7 @@ static int webclient_resolve_address(struct webclient_session *session, struct a { return -WEBCLIENT_NOMEM; } - + return rc; } #endif @@ -395,7 +395,7 @@ static int webclient_connect(struct webclient_session *session, const char *URI) { LOG_E("connect failed, https client open URI(%s) failed!", URI); return -WEBCLIENT_ERROR; - } + } session->is_tls = RT_TRUE; #else LOG_E("not support https connect, please enable webclient https configure!"); @@ -712,17 +712,17 @@ 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); @@ -733,7 +733,7 @@ static int webclient_send_header(struct webclient_session *session, int method) LOG_RAW("\n"); #endif } - + __exit: return rc; } @@ -785,7 +785,7 @@ int webclient_handle_response(struct webclient_session *session) /* echo response header data */ LOG_D("%s", mime_buffer); - + session->header->length += rc; if (session->header->length >= session->header->size) @@ -794,6 +794,7 @@ int webclient_handle_response(struct webclient_session *session) return -WEBCLIENT_NOMEM; } } + /* get HTTP status code */ mime_ptr = web_strdup(session->header->buffer); if (mime_ptr == RT_NULL) @@ -931,7 +932,7 @@ int webclient_get(struct webclient_session *session, const char *URI) /* handle the response header of webclient server */ resp_status = webclient_handle_response(session); - LOG_D("get position handle response(%d).", resp_status); + LOG_D("get position handle response(%d).", resp_status); if (resp_status > 0) { @@ -1008,7 +1009,7 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i /* handle the response header of webclient server */ resp_status = webclient_handle_response(session); - LOG_D("get position handle response(%d).", resp_status); + LOG_D("get position handle response(%d).", resp_status); if (resp_status > 0) { @@ -1087,7 +1088,7 @@ int webclient_post(struct webclient_session *session, const char *URI, const cha /* resolve response data, get http status code */ resp_status = webclient_handle_response(session); - LOG_D("post handle response(%d).", resp_status); + LOG_D("post handle response(%d).", resp_status); } return resp_status; @@ -1257,13 +1258,13 @@ int webclient_read(struct webclient_session *session, unsigned char *buffer, siz if (bytes_read <= 0) { #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS) - if(session->is_tls && + if(session->is_tls && (bytes_read == MBEDTLS_ERR_SSL_WANT_READ || bytes_read == MBEDTLS_ERR_SSL_WANT_WRITE)) { continue; } - -#endif + +#endif LOG_D("receive data error(%d).", bytes_read); if (total_read) @@ -1336,7 +1337,7 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff if (bytes_write <= 0) { #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS) - if(session->is_tls && + if(session->is_tls && (bytes_write == MBEDTLS_ERR_SSL_WANT_READ || bytes_write == MBEDTLS_ERR_SSL_WANT_WRITE)) { continue; @@ -1384,7 +1385,7 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff int webclient_close(struct webclient_session *session) { RT_ASSERT(session); - + #ifdef WEBCLIENT_USING_MBED_TLS if (session->tls_session) { @@ -1394,7 +1395,7 @@ int webclient_close(struct webclient_session *session) { if (session->socket >= 0) { - closesocket(session->socket); + closesocket(session->socket); session->socket = -1; } } @@ -1523,7 +1524,7 @@ int webclient_response(struct webclient_session *session, unsigned char **respon } /** - * add request(GET/POST) header data. + * add request(GET/POST) header data. * * @param request_header add request buffer address * @param fmt fields format @@ -1621,7 +1622,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data 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; + 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; } @@ -1657,7 +1658,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data 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; + 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; } @@ -1678,7 +1679,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data rc = -WEBCLIENT_ERROR; goto __exit; } - + totle_length = webclient_response(session, response); if (totle_length <= 0) { From 6488dc693fa10496ea4d911640db490ee08a7cb3 Mon Sep 17 00:00:00 2001 From: liangyingjian Date: Thu, 21 Nov 2019 18:05:04 +0800 Subject: [PATCH 22/32] =?UTF-8?q?[update]=E4=BF=AE=E6=94=B9=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E5=AD=97=E7=AC=A6=E8=BD=AC=E6=8D=A2=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E4=B8=BA=20clib=20=E4=B8=AD=E7=9A=84=20tolower()?= =?UTF-8?q?=EF=BC=8C=E5=87=8F=E5=B0=8F=E4=BB=A3=E7=A0=81=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/webclient.c b/src/webclient.c index 10b70e9..d78e9f5 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -46,21 +46,14 @@ extern long int strtol(const char *nptr, char **endptr, int base); -static int _tolower(int c) -{ - if (c >= 'A' && c <= 'Z') - c ^= 0x20; - return c; -} - static int webclient_strncasecmp(const char *a, const char *b, size_t n) { uint8_t c1, c2; if (n <= 0) return 0; do { - c1 = _tolower(*a++); - c2 = _tolower(*b++); + c1 = tolower(*a++); + c2 = tolower(*b++); } while (--n && c1 && c1 == c2); return c1 - c2; } From d247c1fb33cff44ab937a810053f517d770d0cb2 Mon Sep 17 00:00:00 2001 From: aozima Date: Wed, 11 Dec 2019 22:45:01 +0800 Subject: [PATCH 23/32] =?UTF-8?q?=E6=9B=B4=E6=96=B0URL=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=EF=BC=9A=E6=94=AF=E6=8C=81=E8=AE=BF=E9=97=AE=E4=B8=BB=E9=A1=B5?= =?UTF-8?q?=E4=B8=94URL=E6=9C=AB=E5=B0=BE=E4=B8=8D=E6=98=AF/=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 73 +++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/webclient.c b/src/webclient.c index d78e9f5..a4ce1ea 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2019, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -16,8 +16,9 @@ #include #include - #include +#include + #include #include @@ -151,17 +152,23 @@ static int webclient_read_line(struct webclient_session *session, char *buffer, * @return 0 on resolve server address OK, others failed * * URL example: - * http://www.rt-thread.org/ + * http://www.rt-thread.org + * http://www.rt-thread.org:80 + * https://www.rt-thread.org/ * http://192.168.1.1:80/index.htm + * http://[fe80::1] + * http://[fe80::1]/ * http://[fe80::1]/index.html * http://[fe80::1]:80/index.html */ static int webclient_resolve_address(struct webclient_session *session, struct addrinfo **res, - const char *url, char **request) + const char *url, const char **request) { int rc = WEBCLIENT_OK; char *ptr; char port_str[6] = "80"; /* default port of 80(http) */ + const char *port_ptr; + const char *path_ptr; const char *host_addr = 0; int url_len, host_addr_len = 0; @@ -198,46 +205,40 @@ static int webclient_resolve_address(struct webclient_session *session, struct a goto __exit; } host_addr_len = ptr - host_addr; + } - ptr = rt_strstr(host_addr + host_addr_len, "/"); - if (!ptr) - { - rc = -WEBCLIENT_ERROR; - goto __exit; - } - else if (ptr != (host_addr + host_addr_len + 1)) - { - int port_len = ptr - host_addr - host_addr_len - 2; + path_ptr = rt_strstr(host_addr, "/"); + *request = path_ptr ? path_ptr : "/"; - rt_strncpy(port_str, host_addr + host_addr_len + 2, port_len); - port_str[port_len] = '\0'; - } + /* resolve port */ + port_ptr = rt_strstr(host_addr + host_addr_len, ":"); + if (port_ptr && path_ptr && (port_ptr < path_ptr)) + { + int port_len = path_ptr - port_ptr - 1; - *request = (char *) ptr; + rt_strncpy(port_str, port_ptr + 1, port_len); + port_str[port_len] = '\0'; } - else /* ipv4 or domain. */ + + if (port_ptr && (!path_ptr)) { - char *port_ptr; + strcpy(port_str, port_ptr + 1); + } - ptr = rt_strstr(host_addr, "/"); - if (!ptr) + /* ipv4 or domain. */ + if (!host_addr_len) + { + if (port_ptr) { - rc = -WEBCLIENT_ERROR; - goto __exit; + host_addr_len = port_ptr - host_addr; } - host_addr_len = ptr - host_addr; - *request = (char *) ptr; - - /* resolve port */ - port_ptr = rt_strstr(host_addr, ":"); - if (port_ptr && port_ptr < ptr) + else if (path_ptr) { - int port_len = ptr - port_ptr - 1; - - rt_strncpy(port_str, port_ptr + 1, port_len); - port_str[port_len] = '\0'; - - host_addr_len = port_ptr - host_addr; + host_addr_len = path_ptr - host_addr; + } + else + { + host_addr_len = strlen(host_addr); } } @@ -368,7 +369,7 @@ static int webclient_connect(struct webclient_session *session, const char *URI) int socket_handle; struct timeval timeout; struct addrinfo *res = RT_NULL; - char *req_url; + const char *req_url; RT_ASSERT(session); RT_ASSERT(URI); From d50aa9e85291f840cda5c704ad59ce4fd75351a8 Mon Sep 17 00:00:00 2001 From: chenyong Date: Tue, 17 Dec 2019 10:48:16 +0800 Subject: [PATCH 24/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91http?= =?UTF-8?q?=20location=20=E8=B7=B3=E8=BD=AC=E5=8A=9F=E8=83=BD=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E5=AD=98=E5=9C=A8=E7=9A=84=20session=20=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=88=9B=E5=BB=BA=E5=92=8C=E9=87=8A=E6=94=BE=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyong --- src/webclient.c | 65 ++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/src/webclient.c b/src/webclient.c index a4ce1ea..b136ba8 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -374,9 +374,6 @@ static int webclient_connect(struct webclient_session *session, const char *URI) RT_ASSERT(session); RT_ASSERT(URI); - /* initialize the socket of session */ - session->socket = -1; - timeout.tv_sec = WEBCLIENT_DEFAULT_TIMEO; timeout.tv_usec = 0; @@ -864,6 +861,8 @@ struct webclient_session *webclient_session_create(size_t header_sz) return RT_NULL; } + /* initialize the socket of session */ + session->socket = -1; session->content_length = -1; session->header = (struct webclient_header *) web_calloc(1, sizeof(struct webclient_header)); @@ -889,6 +888,8 @@ struct webclient_session *webclient_session_create(size_t header_sz) return session; } +static int webclient_clean(struct webclient_session *session); + /** * send GET request to http server and get response header. * @@ -943,15 +944,11 @@ int webclient_get(struct webclient_session *session, const char *URI) 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; - } + /* clean webclient session */ + webclient_clean(session); + /* clean webclient session header */ + session->header->length = 0; + rt_memset(session->header->buffer, 0, session->header->size); rc = webclient_get(session, new_url); @@ -1020,15 +1017,11 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i 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; - } + /* clean webclient session */ + webclient_clean(session); + /* clean webclient session header */ + session->header->length = 0; + rt_memset(session->header->buffer, 0, session->header->size); rc = webclient_get_position(session, new_url, position); @@ -1369,17 +1362,9 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff return total_write; } -/** - * close a webclient client session. - * - * @param session http client session - * - * @return 0: close success - */ -int webclient_close(struct webclient_session *session) +/* close session socket, free host and request url */ +static int webclient_clean(struct webclient_session *session) { - RT_ASSERT(session); - #ifdef WEBCLIENT_USING_MBED_TLS if (session->tls_session) { @@ -1411,6 +1396,24 @@ int webclient_close(struct webclient_session *session) web_free(session->req_url); } + session->content_length = -1; + + return 0; +} + +/** + * close a webclient client session. + * + * @param session http client session + * + * @return 0: close success + */ +int webclient_close(struct webclient_session *session) +{ + RT_ASSERT(session); + + webclient_clean(session); + if (session->header && session->header->buffer) { web_free(session->header->buffer); From bbf34d35935d666108a8b015351da1b8a8c38359 Mon Sep 17 00:00:00 2001 From: chenyong Date: Tue, 31 Dec 2019 12:03:08 +0800 Subject: [PATCH 25/32] =?UTF-8?q?=E3=80=90=E6=B7=BB=E5=8A=A0=E3=80=91?= =?UTF-8?q?=E6=8E=A5=E6=94=B6=E5=92=8C=E5=8F=91=E9=80=81=E9=9D=9E=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E6=A0=BC=E5=BC=8F=E6=95=B0=E6=8D=AE=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=94=AF=E6=8C=81=EF=BC=8C=E6=9B=B4=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=92=8C=E6=96=87=E6=A1=A3=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api.md | 14 ++++++--- docs/migration-guide.md | 2 +- docs/user-guide.md | 12 ++++--- inc/webclient.h | 12 +++---- samples/webclient_get_sample.c | 17 +++++----- samples/webclient_post_sample.c | 30 +++++++++--------- src/webclient.c | 56 +++++++++++++++++++++++---------- src/webclient_file.c | 2 +- 8 files changed, 89 insertions(+), 56 deletions(-) diff --git a/docs/api.md b/docs/api.md index 5cfcfcc..f722a50 100644 --- a/docs/api.md +++ b/docs/api.md @@ -66,7 +66,7 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i ## 发送 POST 请求 ```c -int webclient_post(struct webclient_session *session, const char *URI, const char *post_data); +int webclient_post(struct webclient_session *session, const char *URI, const void *post_data, size_t data_len); ``` 发送 HTTP POST 请求命令,上传数据到 HTTP 服务器。 @@ -76,6 +76,7 @@ int webclient_post(struct webclient_session *session, const char *URI, const cha |session | 当前连接会话结构体指针 | |URI | 连接的 HTTP 服务器地址 | |post_data | 需要上传的数据地址 | +|data_len | 需要上传数据的长度 | | **返回** | **描述** | |`>0` | HTTP 响应状态码 | |<0 | 发送请求失败 | @@ -83,7 +84,7 @@ int webclient_post(struct webclient_session *session, const char *URI, const cha ## 发送数据 ```c -int webclient_write(struct webclient_session *session, const unsigned char *buffer, size_t size); +int webclient_write(struct webclient_session *session, const void *buffer, size_t size); ``` 发送数据到连接的服务器。 @@ -101,7 +102,7 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff ## 接收数据 ```c -int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t size); +int webclient_read(struct webclient_session *session, void *buffer, size_t size); ``` 从连接的服务器接收数据。 @@ -169,7 +170,7 @@ const char *webclient_header_fields_get(struct webclient_session *session, const ## 接收响应数据到指定地址 ```c -int webclient_response(struct webclient_session *session, unsigned char **response); +int webclient_response(struct webclient_session *session, void **response, size_t *resp_len); ``` 该函数用于发送 GET 或 POST 请求之后, 可以接收响应数据到指定地址。 @@ -178,6 +179,7 @@ int webclient_response(struct webclient_session *session, unsigned char **respon |:------------------|:-----------------------------------| |session | 当前连接会话结构体指针 | |response | 存放接收数据的字符串地址 | +|resp_len | 接收数据的长度的指针 | | **返回** | **描述** | | `>0` | 成功接收数据的长度 | | <=0 | 接收数据失败 | @@ -185,7 +187,7 @@ int webclient_response(struct webclient_session *session, unsigned char **respon ## 发送 GET/POST 请求并接收响应数据 ```c -int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **response); +int webclient_request(const char *URI, const char *header, const void *post_data, size_t data_len, void **response, size_t *resp_len); ``` | 参数 | 描述 | @@ -197,7 +199,9 @@ int webclient_request(const char *URI, const char *header, const char *post_data |post_data | 发送到服务器的数据 | | | = NULL,该发送请求为 GET 请求 | | | != NULL,该发送请求为 POST 请求 | +| data_len | 发送数据的长度 | |response | 存放接收数据的字符串地址 | +|resp_len | 接收数据长度的指针 | | **返回** | **描述** | | `>0` | 成功接收数据的长度 | | <=0 | 接收数据失败 | diff --git a/docs/migration-guide.md b/docs/migration-guide.md index 31bc31d..1c1f7f8 100644 --- a/docs/migration-guide.md +++ b/docs/migration-guide.md @@ -143,7 +143,7 @@ session = webclient_session_create(1024) webclient_header_fields_add(session, "Content-Length: %s", post_data_sz); -webclient_post(session, URI, post_data); +webclient_post(session, URI, post_data, rt_strlen(post_data)); webclient_close(session); ``` diff --git a/docs/user-guide.md b/docs/user-guide.md index d00980c..e8d90f2 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -256,6 +256,7 @@ webclient_close(session); ```c struct webclient_session *session = NULL; +size_t length = 0; char *result; session = webclient_create(1024); @@ -265,7 +266,7 @@ if(webclient_get(session, URI) != 200) LOG_E("error!"); } -webclient_response(session, &result); +webclient_response(session, &result, &length); web_free(result); webclient_close(session); @@ -276,12 +277,13 @@ webclient_close(session); 多用于接收数据长度较小,且头部信息已经拼接给出的 GET 请求。 ```c +size_t length = 0; char *result, *header = RT_NULL; /* 拼接自定义头部数据 */ webclient_request_header_add(&header, "User-Agent: RT-Thread HTTP Agent\r\n"); -webclient_request(URI, header, NULL, &result); +webclient_request(URI, header, NULL, 0, &result, &length); web_free(result); ``` @@ -302,7 +304,7 @@ webclient_header_fields_add(session, "Content-Length: %d\r\n", post_data_sz); webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n"); /* 分段数据上传 webclient_post 第三个传输上传数据为 NULL,改为下面循环上传数据*/ -if( webclient_post(session, URI, NULL) != 200) +if( webclient_post(session, URI, NULL, 0) != 200) { LOG_E("error!"); } @@ -334,7 +336,7 @@ session = webclient_create(1024); 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"); -if(webclient_post(session, URI, post_data) != 200); +if(webclient_post(session, URI, post_data, rt_strlen(post_data)) != 200); { LOG_E("error!"); } @@ -353,7 +355,7 @@ 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, header, post_data, NULL); +webclient_request(URI, header, post_data, rt_strlen(post_data), NULL, NULL); ``` ## 常见问题 diff --git a/inc/webclient.h b/inc/webclient.h index b4ea163..018bd24 100644 --- a/inc/webclient.h +++ b/inc/webclient.h @@ -25,7 +25,7 @@ #ifdef __cplusplus extern "C" { -#endif +#endif #ifndef web_malloc #define web_malloc rt_malloc @@ -110,7 +110,7 @@ int webclient_get(struct webclient_session *session, const char *URI); int webclient_get_position(struct webclient_session *session, const char *URI, int position); /* send HTTP POST request */ -int webclient_post(struct webclient_session *session, const char *URI, const char *post_data); +int webclient_post(struct webclient_session *session, const char *URI, const void *post_data, size_t data_len); /* close and release wenclient session */ int webclient_close(struct webclient_session *session); @@ -118,16 +118,16 @@ int webclient_close(struct webclient_session *session); int webclient_set_timeout(struct webclient_session *session, int millisecond); /* send or receive data from server */ -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_read(struct webclient_session *session, void *buffer, size_t size); +int webclient_write(struct webclient_session *session, const void *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 */ -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_response(struct webclient_session *session, void **response, size_t *resp_len); +int webclient_request(const char *URI, const char *header, const void *post_data, size_t data_len, void **response, size_t *resp_len); 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 4697e97..8185229 100644 --- a/samples/webclient_get_sample.c +++ b/samples/webclient_get_sample.c @@ -58,7 +58,7 @@ static int webclient_get_comm(const char *uri) rt_kprintf("webclient GET request type is chunked.\n"); do { - bytes_read = webclient_read(session, buffer, GET_RESP_BUFSZ); + bytes_read = webclient_read(session, (void *)buffer, GET_RESP_BUFSZ); if (bytes_read <= 0) { break; @@ -78,7 +78,7 @@ static int webclient_get_comm(const char *uri) do { - bytes_read = webclient_read(session, buffer, + bytes_read = webclient_read(session, (void *)buffer, content_length - content_pos > GET_RESP_BUFSZ ? GET_RESP_BUFSZ : content_length - content_pos); if (bytes_read <= 0) @@ -114,10 +114,11 @@ __exit: /* 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; + char *response = RT_NULL; + size_t resp_len = 0; int index; - if (webclient_request(uri, RT_NULL, RT_NULL, (unsigned char **)&request) < 0) + if (webclient_request(uri, RT_NULL, RT_NULL, 0, (void **)&response, &resp_len) < 0) { rt_kprintf("webclient send get request failed."); return -RT_ERROR; @@ -125,15 +126,15 @@ static int webclient_get_smpl(const char *uri) 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++) + for (index = 0; index < rt_strlen(response); index++) { - rt_kprintf("%c", request[index]); + rt_kprintf("%c", response[index]); } rt_kprintf("\n"); - if (request) + if (response) { - web_free(request); + web_free(response); } return 0; diff --git a/samples/webclient_post_sample.c b/samples/webclient_post_sample.c index 30b225c..a5e7711 100644 --- a/samples/webclient_post_sample.c +++ b/samples/webclient_post_sample.c @@ -21,7 +21,7 @@ const char *post_data = "RT-Thread is an open source IoT operating system from China!"; /* 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) +static int webclient_post_comm(const char *uri, const void *post_data, size_t data_len) { struct webclient_session* session = RT_NULL; unsigned char *buffer = RT_NULL; @@ -49,7 +49,7 @@ static int webclient_post_comm(const char *uri, const char *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) + if ((resp_status = webclient_post(session, uri, post_data, data_len)) != 200) { rt_kprintf("webclient POST request failed, response(%d) error.\n", resp_status); ret = -RT_ERROR; @@ -88,15 +88,17 @@ __exit: } /* 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) +static int webclient_post_smpl(const char *uri, const char *post_data, size_t data_len) { - char *request = RT_NULL, *header = RT_NULL; - int index; + char *response = RT_NULL; + char *header = RT_NULL; + size_t resp_len = 0; + int index = 0; 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) + if (webclient_request(uri, header, post_data, data_len, (void **)&response, &resp_len) < 0) { rt_kprintf("webclient send post request failed."); web_free(header); @@ -105,9 +107,9 @@ static int webclient_post_smpl(const char *uri, const char *post_data) 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++) + for (index = 0; index < resp_len; index++) { - rt_kprintf("%c", request[index]); + rt_kprintf("%c", response[index]); } rt_kprintf("\n"); @@ -116,9 +118,9 @@ static int webclient_post_smpl(const char *uri, const char *post_data) web_free(header); } - if (request) + if (response) { - web_free(request); + web_free(response); } return 0; @@ -138,7 +140,7 @@ int webclient_post_test(int argc, char **argv) return -RT_ENOMEM; } - webclient_post_comm(uri, post_data); + webclient_post_comm(uri, (void *)post_data, rt_strlen(post_data)); } else if (argc == 2) { @@ -151,7 +153,7 @@ int webclient_post_test(int argc, char **argv) return -RT_ENOMEM; } - webclient_post_smpl(uri, post_data); + webclient_post_smpl(uri, (void *)post_data, rt_strlen(post_data)); } else { @@ -161,7 +163,7 @@ int webclient_post_test(int argc, char **argv) rt_kprintf("no memory for create post request uri buffer.\n"); return -RT_ENOMEM; } - webclient_post_comm(uri, post_data); + webclient_post_comm(uri, (void *)post_data, rt_strlen(post_data)); } } else if(argc == 3 && rt_strcmp(argv[1], "-s") == 0) @@ -173,7 +175,7 @@ int webclient_post_test(int argc, char **argv) return -RT_ENOMEM; } - webclient_post_smpl(uri, post_data); + webclient_post_smpl(uri, (void *)post_data, rt_strlen(post_data)); } else { diff --git a/src/webclient.c b/src/webclient.c index b136ba8..ada9c35 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -79,7 +79,7 @@ static const char *webclient_strstri(const char* str, const char* subStr) return RT_NULL; } -static int webclient_send(struct webclient_session* session, const unsigned char *buffer, size_t len, int flag) +static int webclient_send(struct webclient_session* session, const void *buffer, size_t len, int flag) { #ifdef WEBCLIENT_USING_MBED_TLS if (session->tls_session) @@ -91,7 +91,7 @@ static int webclient_send(struct webclient_session* session, const unsigned char return send(session->socket, buffer, len, flag); } -static int webclient_recv(struct webclient_session* session, unsigned char *buffer, size_t len, int flag) +static int webclient_recv(struct webclient_session* session, void *buffer, size_t len, int flag) { #ifdef WEBCLIENT_USING_MBED_TLS if (session->tls_session) @@ -1039,15 +1039,16 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i * @param session webclient session * @param URI input server URI address * @param header POST request header, can't be empty - * @param post_data data sent to the server + * @param post_data data send to the server * = NULL: just connect server and send header * != NULL: send header and body data, resolve response data + * @param data_len the length of send data * * @return <0: send POST request failed * =0: send POST header success * >0: response http status code */ -int webclient_post(struct webclient_session *session, const char *URI, const char *post_data) +int webclient_post(struct webclient_session *session, const char *URI, const void *post_data, size_t data_len) { int rc = WEBCLIENT_OK; int resp_status = 0; @@ -1055,6 +1056,12 @@ int webclient_post(struct webclient_session *session, const char *URI, const cha RT_ASSERT(session); RT_ASSERT(URI); + if ((post_data != RT_NULL) && (data_len == 0)) + { + LOG_E("input post data length failed"); + return -WEBCLIENT_ERROR; + } + rc = webclient_connect(session, URI); if (rc != WEBCLIENT_OK) { @@ -1069,9 +1076,9 @@ int webclient_post(struct webclient_session *session, const char *URI, const cha return rc; } - if (post_data) + if (post_data && (data_len > 0)) { - webclient_write(session, (unsigned char *) post_data, rt_strlen(post_data)); + webclient_write(session, post_data, data_len); /* resolve response data, get http status code */ resp_status = webclient_handle_response(session); @@ -1164,7 +1171,7 @@ static int webclient_next_chunk(struct webclient_session *session) * =0: http server disconnect * >0: successfully read data length */ -int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t length) +int webclient_read(struct webclient_session *session, void *buffer, size_t length) { int bytes_read = 0; int total_read = 0; @@ -1241,7 +1248,7 @@ int webclient_read(struct webclient_session *session, unsigned char *buffer, siz left = length; do { - bytes_read = webclient_recv(session, buffer + total_read, left, 0); + bytes_read = webclient_recv(session, (void *)((char *)buffer + total_read), left, 0); if (bytes_read <= 0) { #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS) @@ -1299,7 +1306,7 @@ int webclient_read(struct webclient_session *session, unsigned char *buffer, siz * =0: http server disconnect * >0: successfully write data length */ -int webclient_write(struct webclient_session *session, const unsigned char *buffer, size_t length) +int webclient_write(struct webclient_session *session, const void *buffer, size_t length) { int bytes_write = 0; int total_write = 0; @@ -1320,7 +1327,7 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff /* send all of data on the buffer. */ do { - bytes_write = webclient_send(session, buffer + total_write, left, 0); + bytes_write = webclient_send(session, (void *)((char *)buffer + total_write), left, 0); if (bytes_write <= 0) { #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS) @@ -1438,10 +1445,11 @@ int webclient_close(struct webclient_session *session) * * @param session wenclient session * @param response response buffer address + * @param resp_len response buffer length * * @return response data size */ -int webclient_response(struct webclient_session *session, unsigned char **response) +int webclient_response(struct webclient_session *session, void **response, size_t *resp_len) { unsigned char *buf_ptr; unsigned char *response_buf = 0; @@ -1513,8 +1521,9 @@ int webclient_response(struct webclient_session *session, unsigned char **respon if (response_buf) { - *response = response_buf; + *response = (void *)response_buf; *(response_buf + total_read) = '\0'; + *resp_len = total_read; } return total_read; @@ -1583,12 +1592,14 @@ int webclient_request_header_add(char **request_header, const char *fmt, ...) * @param post_data data sent to the server * = NULL: it is GET request * != NULL: it is POST request + * @param data_len send data length * @param response response buffer address + * @param resp_len response buffer length * * @return <0: request failed * >=0: response buffer size */ -int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **response) +int webclient_request(const char *URI, const char *header, const void *post_data, size_t data_len, void **response, size_t *resp_len) { struct webclient_session *session = RT_NULL; int rc = WEBCLIENT_OK; @@ -1602,6 +1613,19 @@ int webclient_request(const char *URI, const char *header, const char *post_data return -WEBCLIENT_ERROR; } + if ((post_data != RT_NULL) && (data_len == 0)) + { + LOG_E("input post data length failed"); + return -WEBCLIENT_ERROR; + } + + if ((response != RT_NULL && resp_len == RT_NULL) || + (response == RT_NULL && resp_len != RT_NULL)) + { + LOG_E("input response data or length failed"); + return -WEBCLIENT_ERROR; + } + if (post_data == RT_NULL) { /* send get request */ @@ -1631,7 +1655,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data goto __exit; } - totle_length = webclient_response(session, response); + totle_length = webclient_response(session, response, resp_len); if (totle_length <= 0) { rc = -WEBCLIENT_ERROR; @@ -1671,13 +1695,13 @@ int webclient_request(const char *URI, const char *header, const char *post_data webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n"); } - if (webclient_post(session, URI, post_data) != 200) + if (webclient_post(session, URI, post_data, data_len) != 200) { rc = -WEBCLIENT_ERROR; goto __exit; } - totle_length = webclient_response(session, response); + totle_length = webclient_response(session, response, resp_len); if (totle_length <= 0) { rc = -WEBCLIENT_ERROR; diff --git a/src/webclient_file.c b/src/webclient_file.c index 598e90d..f8db2e0 100644 --- a/src/webclient_file.c +++ b/src/webclient_file.c @@ -225,7 +225,7 @@ int webclient_post_file(const char* URI, const char* filename, rt_strncpy(session->header->buffer, header, rt_strlen(header)); session->header->length = rt_strlen(session->header->buffer); - rc = webclient_post(session, URI, NULL); + rc = webclient_post(session, URI, NULL, 0); if(rc < 0) { goto __exit; From c7d11332ae9da255374b57ded7f8330aebf7548d Mon Sep 17 00:00:00 2001 From: chenyong Date: Thu, 2 Jan 2020 17:13:22 +0800 Subject: [PATCH 26/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC=E5=8F=B7=20V2.2.0=20?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E7=89=88=E6=9C=AC=E6=94=B9=E5=8A=A8?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- docs/migration-guide.md | 39 ++++++++++++++++++++++++++++++++++----- inc/webclient.h | 4 ++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2ad914f..f47cf7d 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,8 @@ RT-Thread online packages ## 4、注意事项 - WebClient 软件包连接 HTTPS 服务器时需要开启 WebClient 中对 TLS 功能的支持。 - - WebClient 软件包版本更新(`V1.0.0 -> 当前最新版 V2.0.0`)后软件包中函数接口和使用流程都有所变化,若开发者代码中使用之前接口,可以适配最新版本接口,或者在版本号配置中选择 `V1.0.0` 版本,具体改动方式可参考软件包 [迁移指南](docs/migration-guide.md)。 + - WebClient 软件包版本更新(`V1.0.0 -> V2.0.0`)后软件包中函数接口和使用流程都有所变化,若开发者代码中使用之前接口,可以适配最新版本接口,或者在版本号配置中选择 `V1.0.0` 版本,具体改动方式可参考软件包 [迁移指南](docs/migration-guide.md)。 + - webclient 软件包版本更新(`V2.1.0 -> V2.2.0`),为了适配非字符串数据传输,部分接口定义有所改动,具体修改可参考软件包 [迁移指南](docs/migration-guide.md)。 ## 5、联系方式 & 感谢 diff --git a/docs/migration-guide.md b/docs/migration-guide.md index 1c1f7f8..a3c088f 100644 --- a/docs/migration-guide.md +++ b/docs/migration-guide.md @@ -1,8 +1,10 @@ # 迁移指南 +## 1、V1.0.0 -> V2.0.0 版本改动 + 本节主要介绍 WebClient 软件包版本升级之后(`V1.0.0` -> 最近版本 `V2.0.0`),软件包的改动和适配最新版本的方式。 -## 函数改动 +### 函数改动 1. 添加 `webclient_session_create()` 创建客户端结构体函数 @@ -29,9 +31,9 @@ - 添加获取当前响应状态码函数(`webclient_resp_status_get` ); - 添加获取当前响应 Content-Length 数据函数(`webclient_content_length_get`)。 -## 流程改动 +### 流程改动 -### GET 请求流程改动 +#### GET 请求流程改动 下面以发送自定义头部数据的 GET 请求方式为例,介绍新版本软件包中 GET 流程改动。 @@ -93,7 +95,7 @@ while(1) webclient_close(session); ``` -### POST 请求流程改动 +#### POST 请求流程改动 下面以发送自定义头部数据的 POST 请求方式为例,介绍新版本软件包中的 POST 流程改动。 @@ -148,4 +150,31 @@ webclient_post(session, URI, post_data, rt_strlen(post_data)); webclient_close(session); ``` -上述介绍 WebClient 常用 GET/POST 请求方法改动,更多流程介绍请查看软件包 [使用指南](user-guide.md)。 \ No newline at end of file +上述介绍 WebClient 常用 GET/POST 请求方法改动,更多流程介绍请查看软件包 [使用指南](user-guide.md)。 + +## 2、V2.1.0 -> V2.2.0 版本改动 + +V2.2.0 版本主要改动部分为函数接口参数定义,目的是为了适配非字符串格式数据的读写,其他函数的使用流程未产生明显改动。 + +### 函数改动 + +- `webclient_post` 函数参数改动 + + 该函数入参 `const char *post_data` 修改为 `const void *post_data`,并添加`size_t data_len` 参数,用于确定需要发送的数据长度。 + +- `webclient_read` 函数参数改动 + + 该函数入参 `unsigned char *buffer` 修改为 `void *buffer`,用于适配非字符串数据接收。 + +- `webclient_write` 函数参数改动 + + 该函数入参 `const unsigned char *buffer` 修改为 `const void *buffer`,用于适配非字符串数据发送。 + +- `webclient_response` 函数参数改动 + + 该函数入参 `unsigned char **response` 修改为 `void **response`,并且添加参数 `size_t *resp_len`,用于获取接收数据长度信息,适配非字符串数据接收。 + +- `webclient_request` 函数参数改动 + + 该函数入参 `const char *post_data` 修改为 `const void *post_data`,入参 `unsigned char **response` 修改为 `void **response`,并且添加入参 `size_t data_len` 和 `size_t *resp_len`,用于适配非字符串数据接收和发送。 + diff --git a/inc/webclient.h b/inc/webclient.h index 018bd24..45977a2 100644 --- a/inc/webclient.h +++ b/inc/webclient.h @@ -47,8 +47,8 @@ extern "C" { #define web_strdup rt_strdup #endif -#define WEBCLIENT_SW_VERSION "2.1.2" -#define WEBCLIENT_SW_VERSION_NUM 0x20102 +#define WEBCLIENT_SW_VERSION "2.2.0" +#define WEBCLIENT_SW_VERSION_NUM 0x20200 #define WEBCLIENT_HEADER_BUFSZ 4096 #define WEBCLIENT_RESPONSE_BUFSZ 4096 From 2457ea52e2e53bf259cdc356cf0d7b643153a5e3 Mon Sep 17 00:00:00 2001 From: chasel <377103859@qq.com> Date: Fri, 12 Jun 2020 00:20:16 +0800 Subject: [PATCH 27/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91webcl?= =?UTF-8?q?ient=5Fget=5Fposition=E5=8F=AF=E8=83=BD=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E5=86=85=E5=AD=98=E8=B6=8A=E7=95=8C=20#65?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webclient.c b/src/webclient.c index ada9c35..8be863a 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -685,7 +685,7 @@ static int webclient_send_header(struct webclient_session *session, int method) } /* header data end */ - rt_snprintf(session->header->buffer + session->header->length, session->header->size, "\r\n"); + rt_snprintf(session->header->buffer + session->header->length, session->header->size - session->header->length, "\r\n"); session->header->length += 2; /* check header size */ From b4b5a4023b5f297f01c5c6ae99de103db801181f Mon Sep 17 00:00:00 2001 From: liuxianliang Date: Fri, 19 Jun 2020 09:36:33 +0800 Subject: [PATCH 28/32] [fix] the bug of redeclare when "timeval" is defined by lwip oneself. Signed-off-by: liuxianliang --- src/webclient.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/webclient.c b/src/webclient.c index ada9c35..8733f1b 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -19,10 +19,12 @@ #include #include -#include - #include +#if defined(RT_USING_LIBC) || defined(RT_USING_MINILIBC) || defined(RT_LIBC_USING_TIME) +#include +#endif + /* support both enable and disable "SAL_USING_POSIX" */ #if defined(RT_USING_SAL) #include @@ -1619,7 +1621,7 @@ int webclient_request(const char *URI, const char *header, const void *post_data return -WEBCLIENT_ERROR; } - if ((response != RT_NULL && resp_len == RT_NULL) || + if ((response != RT_NULL && resp_len == RT_NULL) || (response == RT_NULL && resp_len != RT_NULL)) { LOG_E("input response data or length failed"); From 2392fe485be40f3654858fdec6ab205639274d60 Mon Sep 17 00:00:00 2001 From: "Haley.lin" <1209363203@qq.com> Date: Fri, 21 Aug 2020 16:03:27 +0800 Subject: [PATCH 29/32] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dwebclient=20GET=20reque?= =?UTF-8?q?st=20failed=E6=83=85=E5=86=B5=E4=B8=8B=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=8F=8C=E9=87=8D=E9=87=8A=E6=94=BE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/webclient.c b/src/webclient.c index b047c72..3c82773 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -1398,11 +1398,13 @@ static int webclient_clean(struct webclient_session *session) if (session->host) { web_free(session->host); + session->host = RT_NULL; } if (session->req_url) { web_free(session->req_url); + session->req_url = RT_NULL; } session->content_length = -1; From d159d6991fb6c241aeb945330e74808e67ab713c Mon Sep 17 00:00:00 2001 From: liuduanfei <939763442@qq.com> Date: Mon, 19 Oct 2020 13:41:40 +0800 Subject: [PATCH 30/32] Update user-guide.md --- docs/user-guide.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/user-guide.md b/docs/user-guide.md index e8d90f2..f08de01 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -138,7 +138,7 @@ if ((resp_status = webclient_get(session, URI)) != 200) (4) **接收响应的数据** -发送 GET/POST 请求之后,可以使用 `webclient_read` 函数接收响应的实际数据。因为响应的实际数据可能比较长,所以往往我们需要循环接收响应数据,指导数据接收完毕。 +发送 GET/POST 请求之后,可以使用 `webclient_read` 函数接收响应的实际数据。因为响应的实际数据可能比较长,所以往往我们需要循环接收响应数据,直到数据接收完毕。 如下所示为循环接收并打印响应数据方式: @@ -378,4 +378,4 @@ webclient_request(URI, header, post_data, rt_strlen(post_data), NULL, NULL); - 原因:添加的头部数据长度超过了最大支持的头部数据长度。 -- 解决方法:在创建客户端会话结构体的时候,增大传入的最大支持的头部数据长度。 \ No newline at end of file +- 解决方法:在创建客户端会话结构体的时候,增大传入的最大支持的头部数据长度。 From b80db949946c8acb22abbed04e7d0914c0701350 Mon Sep 17 00:00:00 2001 From: luanxueguang <1600577905@qq.com> Date: Thu, 24 Dec 2020 11:19:25 +0800 Subject: [PATCH 31/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91?= =?UTF-8?q?=E5=88=A4=E6=96=ADfields=E5=9C=A8=E8=A1=8C=E9=A6=96=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webclient.c b/src/webclient.c index b047c72..3636898 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -562,7 +562,7 @@ const char *webclient_header_fields_get(struct webclient_session *session, const resp_buf = session->header->buffer; while (resp_buf_len < session->header->length) { - if (webclient_strstri(resp_buf, fields)) + if (webclient_strstri(resp_buf, fields) == resp_buf) { char *mime_ptr = RT_NULL; From bd79a67be738eac1aa999c23757a175e57dcbd27 Mon Sep 17 00:00:00 2001 From: luanxueguang <1600577905@qq.com> Date: Thu, 24 Dec 2020 11:25:04 +0800 Subject: [PATCH 32/32] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8A=E6=94=BE=E6=8C=87=E9=92=88=E5=90=8E?= =?UTF-8?q?=E6=9C=AA=E7=BD=AE=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webclient.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/webclient.c b/src/webclient.c index 3636898..f52eede 100644 --- a/src/webclient.c +++ b/src/webclient.c @@ -1398,11 +1398,13 @@ static int webclient_clean(struct webclient_session *session) if (session->host) { web_free(session->host); + session->host = RT_NULL; } if (session->req_url) { web_free(session->req_url); + session->req_url = RT_NULL; } session->content_length = -1;