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 1/4] =?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 2/4] =?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 3/4] =?UTF-8?q?=E3=80=90=E5=AE=8C=E5=96=84=E3=80=91GET/POS?= =?UTF-8?q?T=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 4/4] =?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`。 ### 头部数据长度超出