├── .gitignore ├── LICENSE ├── README.md ├── README_ZH.md ├── SConscript ├── docs ├── README.md ├── api.md ├── figures │ ├── principle.jpg │ ├── request.jpg │ ├── response.jpg │ └── webclient_cfg.jpg ├── introduction.md ├── migration-guide.md ├── principle.md ├── samples.md ├── user-guide.md └── version.md ├── inc └── webclient.h ├── samples ├── webclient_get_sample.c ├── webclient_post_sample.c └── webclient_shard_download_sample.c └── src ├── webclient.c └── webclient_file.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WebClient 2 | 3 | [Chinese](README_ZH.md) | English 4 | 5 | ## 1. Introduction 6 | 7 | The WebClient software package is independently developed by RT-Thread and is based on the implementation of the HTTP protocol client. It provides the basic functions of communication between the device and the HTTP Server. 8 | 9 | The features of the WebClient software package are as follows: 10 | 11 | - Support IPV4/IPV6 address; 12 | - Support GET/POST request method; 13 | - Support file upload and download function; 14 | - Support HTTPS encrypted transmission; 15 | - Complete header data addition and processing methods. 16 | 17 | For more software package introduction, please refer to [Detailed introduction](docs/introduction.md). 18 | 19 | ### 1.1 Directory structure 20 | 21 | The directory structure of the WebClient software package is as follows: 22 | 23 | ``` 24 | webclient 25 | ├───docs 26 | │ └───figures // Documents use pictures 27 | │ │ api.md // API instructions 28 | │ │ introduction.md // Introduction document 29 | │ │ principle.md // Implementation principle 30 | │ │ README.md // Document structure description 31 | │ │ samples.md // package sample 32 | │ │ user-guide.md // Instructions 33 | │ └───version.md // version 34 | ├───inc // header file 35 | ├───src // source file 36 | ├───samples // sample code 37 | | | webclient_get_sample // GET request sample code 38 | │ └───webclient_post_sample // POST request sample code 39 | │ LICENSE // package license 40 | │ README.md // Software package instructions 41 | └───SConscript // RT-Thread default build script 42 | ``` 43 | 44 | ### 1.2 License 45 | 46 | The WebClient software package complies with the Apache-2.0 license, see the LICENSE file for details. 47 | 48 | ### 1.3 Dependency 49 | 50 | - RT_Thread 3.0+ 51 | 52 | - [mbedtls package](https://github.com/RT-Thread-packages/mbedtls) (if HTTPS support is enabled) 53 | 54 | ## 2. Get the software package 55 | 56 | To use the WebClient software package, you need to select it in the RT-Thread package management. The specific path is as follows: 57 | 58 | ``` 59 | RT-Thread online packages 60 | IoT-internet of things ---> 61 | [*] WebClient: A HTTP/HTTPS Client for RT-Thread 62 | [ ] Enable debug log output 63 | [ ] Enable webclient GET/POST/SHARD samples 64 | [ ] Enable file download feature support 65 | Select TLS mode (Not support) ---> 66 | (x) Not support 67 | () SAL TLS support 68 | () MbedTLS support 69 | Version (latest) ---> 70 | ``` 71 | 72 | **Enable webclient GET/POST samples**: add sample code; 73 | **Select TLS mode**: Configure to enable HTTPS support and select the supported mode; 74 | - **Not support**: Does not support TLS function; 75 | - **SAL TLS support**: Configure the TLS function support in the SAL component, and abstract the TLS operation in the SAL component. Users also need to **manually configure the type of TLS software package used** (currently only supports the MbedTLS package); 76 | - **MbedTLS support**: configure MbedTLS function support; 77 | **Version**: Configure the software package version. 78 | 79 | After the configuration is complete, let the RT-Thread package manager automatically update, or use the pkgs --update command to update the package to the BSP. 80 | 81 | ## 3. Use WebClient software package 82 | - For detailed description of the software package, please refer to [Package Introduction](docs/introduction.md) 83 | - For detailed sample introduction, please refer to [Sample Document](docs/samples.md) 84 | - How to use from scratch, please refer to [User Guide](docs/user-guide.md) 85 | - For complete API documentation, please refer to [API Manual](docs/api.md) 86 | - The working principle of the software package, please refer to [Working Principle](docs/principle.md) 87 | 88 | - More **Detailed introduction documents** are located in the [`/docs`](/docs) folder, **Please check before using the package for development**. 89 | 90 | ## 4. Matters needing attention 91 | 92 | 93 | - When the WebClient software package connects to the HTTPS server, you need to enable the TLS function support in WebClient. 94 | - After the WebClient software package version update (`V1.0.0 -> the current latest version V2.0.0`), the function interface and usage process in the software package have changed. If the previous interface is used in the developer code, the latest version interface can be adapted , Or select the `V1.0.0` version in the version number configuration, the specific modification method can refer to the software package [migration guide](docs/migration-guide.md). 95 | 96 | ## 5. Contact & Thanks 97 | 98 | - Maintenance: RT-Thread development team 99 | - Homepage: https://github.com/RT-Thread-packages/webclient -------------------------------------------------------------------------------- /README_ZH.md: -------------------------------------------------------------------------------- 1 | # WebClient 2 | 3 | 中文页 | [英文页](README.md) 4 | 5 | ## 1、介绍 6 | 7 | WebClient 软件包是 RT-Thread 自主研发的,基于 HTTP 协议的客户端的实现,它提供设备与 HTTP Server 的通讯的基本功能。 8 | 9 | WebClient 软件包功能特点如下: 10 | 11 | - 支持 IPV4/IPV6 地址; 12 | - 支持 GET/POST 请求方法; 13 | - 支持文件的上传和下载功能; 14 | - 支持 HTTPS 加密传输; 15 | - 完善的头部数据添加和处理方式。 16 | 17 | 更多软件包介绍请查看 [详细介绍](docs/introduction.md)。 18 | 19 | ### 1.1 目录结构 20 | 21 | WebClient 软件包目录结构如下所示: 22 | 23 | ``` 24 | webclient 25 | ├───docs 26 | │ └───figures // 文档使用图片 27 | │ │ api.md // API 使用说明 28 | │ │ introduction.md // 介绍文档 29 | │ │ principle.md // 实现原理 30 | │ │ README.md // 文档结构说明 31 | │ │ samples.md // 软件包示例 32 | │ │ user-guide.md // 使用说明 33 | │ └───version.md // 版本 34 | ├───inc // 头文件 35 | ├───src // 源文件 36 | ├───samples // 示例代码 37 | | | webclient_get_sample // GET 请求示例代码 38 | │ └───webclient_post_sample // POST 请求示例代码 39 | │ LICENSE // 软件包许可证 40 | │ README.md // 软件包使用说明 41 | └───SConscript // RT-Thread 默认的构建脚本 42 | ``` 43 | 44 | ### 1.2 许可证 45 | 46 | WebClient 软件包遵循 Apache-2.0 许可,详见 LICENSE 文件。 47 | 48 | ### 1.3 依赖 49 | 50 | - RT_Thread 3.0+ 51 | 52 | - [mbedtls 软件包](https://github.com/RT-Thread-packages/mbedtls)(如果开启 HTTPS 支持) 53 | 54 | ## 2、获取软件包 55 | 56 | 使用 WebClient 软件包需要在 RT-Thread 的包管理中选中它,具体路径如下: 57 | 58 | ``` 59 | RT-Thread online packages 60 | IoT - internet of things ---> 61 | [*] WebClient: A HTTP/HTTPS Client for RT-Thread 62 | [ ] Enable webclient GET/POST/SHARD samples 63 | Select TLS mode (Not support) ---> 64 | (x) Not support 65 | ( ) SAL TLS support 66 | ( ) MbedTLS support 67 | Version (latest) ---> 68 | ``` 69 | 70 | **Enable webclient GET/POST samples** :添加示例代码; 71 | 72 | **Select TLS mode** :配置开启 HTTPS 支持,选择支持的模式; 73 | 74 | - **Not support**:不支持 TLS 功能; 75 | - **SAL TLS support**:配置 SAL 组件中 TLS 功能支持,SAL 组件中抽象 TLS 操作,用户还需要**手动配置开启使用的 TLS 软件包类型**(目前只支持 MbedTLS 软件包); 76 | - **MbedTLS support**:配置 MbedTLS 功能支持; 77 | 78 | **Version** :配置软件包版本。 79 | 80 | 配置完成后让 RT-Thread 的包管理器自动更新,或者使用 pkgs --update 命令更新包到 BSP 中。 81 | 82 | ## 3、使用 WebClient 软件包 83 | - 软件包详细介绍,请参考 [软件包介绍](docs/introduction.md) 84 | 85 | - 详细的示例介绍,请参考 [示例文档](docs/samples.md) 86 | 87 | - 如何从零开始使用,请参考 [用户指南](docs/user-guide.md) 88 | 89 | - 完整的 API 文档,请参考 [API 手册](docs/api.md) 90 | 91 | - 软件包工作原理,请参考 [工作原理](docs/principle.md) 92 | 93 | - 更多**详细介绍文档**位于 [`/docs`](/docs) 文件夹下,**使用软件包进行开发前请务必查看**。 94 | 95 | ## 4、注意事项 96 | 97 | - WebClient 软件包连接 HTTPS 服务器时需要开启 WebClient 中对 TLS 功能的支持。 98 | - WebClient 软件包版本更新(`V1.0.0 -> 当前最新版 V2.0.0`)后软件包中函数接口和使用流程都有所变化,若开发者代码中使用之前接口,可以适配最新版本接口,或者在版本号配置中选择 `V1.0.0` 版本,具体改动方式可参考软件包 [迁移指南](docs/migration-guide.md)。 99 | 100 | 101 | ## 5、联系方式 & 感谢 102 | 103 | - 维护:RT-Thread 开发团队 104 | - 主页:https://github.com/RT-Thread-packages/webclient -------------------------------------------------------------------------------- /SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | 3 | cwd = GetCurrentDir() 4 | path = [cwd + '/inc'] 5 | 6 | src = Glob('src/webclient.c') 7 | 8 | if GetDepend(['WEBCLIENT_USING_FILE_DOWMLOAD']): 9 | src += Glob('src/webclient_file.c') 10 | 11 | if GetDepend(['WEBCLIENT_USING_SAMPLES']): 12 | src += Glob('samples/*.c') 13 | 14 | group = DefineGroup('WebClient', src, depend = ['PKG_USING_WEBCLIENT'], CPPPATH = path) 15 | 16 | Return('group') 17 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # 文档 2 | 3 | ## 软件包地址 4 | 5 | - https://github.com/RT-Thread-packages/webclient 6 | 7 | ## 文档列表 8 | 9 | |文件名 |描述| 10 | |:----- |:----| 11 | |[version.md](version.md) |版本信息| 12 | |[introduction.md](introduction.md) |详细介绍| 13 | |[principle.md](principle.md) |工作原理| 14 | |[user-guide.md](user-guide.md) |使用指南| 15 | |[api.md](api.md) |API 说明| 16 | |[samples.md](samples.md) |示例说明| 17 | |[migration-guide.md](migration-guide.md) |迁移指南| 18 | 19 | -------------------------------------------------------------------------------- /docs/api.md: -------------------------------------------------------------------------------- 1 | # API 说明 2 | 3 | ## 创建会话 4 | 5 | ```c 6 | struct webclient_session *webclient_session_create(size_t header_sz); 7 | ``` 8 | 9 | 创建客户端会话结构体。 10 | 11 | | 参数 | 描述 | 12 | |:------------------|:-----------------------------------| 13 | |header_sz | 最大支持的头部长度 | 14 | | **返回** | **描述** | 15 | |!= NULL | webclient 会话结构体指针 | 16 | |= NULL | 创建失败 | 17 | 18 | ## 关闭会话连接 19 | 20 | ```c 21 | int webclient_close(struct webclient_session *session); 22 | ``` 23 | 24 | 关闭传入的客户端会话连接,并释放内存。 25 | 26 | | 参数 | 描述 | 27 | |:------------------|:-----------------------------------| 28 | |session | 当前连接会话结构体指针 | 29 | | **返回** | **描述** | 30 | |=0 | 成功 | 31 | 32 | 33 | ## 发送 GET 请求 34 | 35 | ```c 36 | int webclient_get(struct webclient_session *session, const char *URI); 37 | ``` 38 | 39 | 发送 HTTP GET 请求命令。 40 | 41 | | 参数 | 描述 | 42 | |:------------------|:-----------------------------------| 43 | |session | 当前连接会话结构体指针 | 44 | |URI | 连接的 HTTP 服务器地址 | 45 | | **返回** | **描述** | 46 | |`>0` | HTTP 响应状态码 | 47 | |<0 | 发送请求失败 | 48 | 49 | ## 获取指定数据大小的 HEAD / GET 请求 50 | 51 | ```c 52 | int webclient_shard_position_function(struct webclient_session *session, const char *URI, int size); 53 | ``` 54 | 55 | 发送带有 Range 头信息的 HTTP GET/HEAD 请求命令,多用于断点续传 / 分片下载功能。 56 | 57 | | 参数 | 描述 | 58 | |:------------------|:-----------------------------------| 59 | |session | 当前连接会话结构体指针 | 60 | |URI | 连接的 HTTP 服务器地址 | 61 | |size | 设定的接收空间 | 62 | | **返回** | **描述** | 63 | |`>0` | HTTP 响应状态码 | 64 | |<0 | 发送请求失败 | 65 | 66 | ## 发送 POST 请求 67 | 68 | ```c 69 | int webclient_post(struct webclient_session *session, const char *URI, const void *post_data, size_t data_len); 70 | ``` 71 | 72 | 发送 HTTP POST 请求命令,上传数据到 HTTP 服务器。 73 | 74 | | 参数 | 描述 | 75 | |:------------------|:-----------------------------------| 76 | |session | 当前连接会话结构体指针 | 77 | |URI | 连接的 HTTP 服务器地址 | 78 | |post_data | 需要上传的数据地址 | 79 | |data_len | 需要上传数据的长度 | 80 | | **返回** | **描述** | 81 | |`>0` | HTTP 响应状态码 | 82 | |<0 | 发送请求失败 | 83 | 84 | ## 发送数据 85 | 86 | ```c 87 | int webclient_write(struct webclient_session *session, const void *buffer, size_t size); 88 | ``` 89 | 90 | 发送数据到连接的服务器。 91 | 92 | | 参数 | 描述 | 93 | |:------------------|:-----------------------------------| 94 | |session | 当前连接会话结构体指针 | 95 | |buffer | 发送数据的地址 | 96 | |size | 发送数据的长度 | 97 | | **返回** | **描述** | 98 | |`>0` | 成功发送数据的长度 | 99 | |=0 | 连接关闭 | 100 | |<0 | 发送数据失败 | 101 | 102 | ## 接收数据 103 | 104 | ```c 105 | int webclient_read(struct webclient_session *session, void *buffer, size_t size); 106 | ``` 107 | 108 | 从连接的服务器接收数据。 109 | 110 | | 参数 | 描述 | 111 | |:------------------|:-----------------------------------| 112 | |session | 当前连接会话结构体指针 | 113 | |buffer | 接收数据的存放地址 | 114 | |size | 最大接收数据的长度 | 115 | | **返回** | **描述** | 116 | |`>0` | 成功接收数据的长度 | 117 | |=0 | 连接关闭 | 118 | |<0 | 接收数据失败 | 119 | 120 | 121 | ## 设置接收和发送数据超时时间 122 | 123 | ```c 124 | int webclient_set_timeout(struct webclient_session *session, int millisecond); 125 | ``` 126 | 127 | 设置连接的接收和发送数据超时时间。 128 | 129 | | 参数 | 描述 | 130 | |:------------------|:-----------------------------------| 131 | |session | 当前连接会话结构体指针 | 132 | |millisecond | 设置的超时时间,单位毫秒 | 133 | | **返回** | **描述** | 134 | |=0 | 设置超时成功 | 135 | 136 | ## 在请求头中添加字段数据 137 | 138 | ```c 139 | int webclient_header_fields_add(struct webclient_session *session, const char *fmt, ...); 140 | ``` 141 | 142 | 该函数用于创建会话之后和发送 GET 或 POST 请求之前,用于添加请求头字段数据。 143 | 144 | | 参数 | 描述 | 145 | |:------------------|:-----------------------------------| 146 | |session | 当前连接会话结构体指针 | 147 | |fmt | 添加字段数据的表达式 | 148 | |... | 添加的字段数据,为可变参数 | 149 | | **返回** | **描述** | 150 | |`>0` | 成功添加的字段数据的长度 | 151 | | <=0 | 添加失败或者头部数据长度超出 | 152 | 153 | ## 通过字段名获取字段值数据 154 | 155 | ```c 156 | const char *webclient_header_fields_get(struct webclient_session *session, const char *fields); 157 | ``` 158 | 159 | 该函数用于发送 GET 或 POST 请求之后,可以通过传入的字段名称获取对应的字段数据。 160 | 161 | | 参数 | 描述 | 162 | |:------------------|:-----------------------------------| 163 | |session | 当前连接会话结构体指针 | 164 | |fields | HTTP 字段名称 | 165 | | **返回** | **描述** | 166 | |= NULL | 获取数据失败 | 167 | |!= NULL | 成功获取的字段数据 | 168 | 169 | 170 | ## 接收响应数据到指定地址 171 | 172 | ```c 173 | int webclient_response(struct webclient_session *session, void **response, size_t *resp_len); 174 | ``` 175 | 176 | 该函数用于发送 GET 或 POST 请求之后, 可以接收响应数据到指定地址。 177 | 178 | | 参数 | 描述 | 179 | |:------------------|:-----------------------------------| 180 | |session | 当前连接会话结构体指针 | 181 | |response | 存放接收数据的字符串地址 | 182 | |resp_len | 接收数据的长度的指针 | 183 | | **返回** | **描述** | 184 | | `>0` | 成功接收数据的长度 | 185 | | <=0 | 接收数据失败 | 186 | 187 | ## 发送 GET/POST 请求并接收响应数据 188 | 189 | ```c 190 | int webclient_request(const char *URI, const char *header, const void *post_data, size_t data_len, void **response, size_t *resp_len); 191 | ``` 192 | 193 | | 参数 | 描述 | 194 | |:------------------|:-----------------------------------| 195 | |URI | 连接的 HTTP 服务器地址 | 196 | |header | 需要发送的头部数据 | 197 | | | = NULL,发送默认头数据信息,可用于发送 GET/POST请求 | 198 | | | != NULL,发送指定头数据信息,可用于发送 GET/POST请求 | 199 | |post_data | 发送到服务器的数据 | 200 | | | = NULL,该发送请求为 GET 请求 | 201 | | | != NULL,该发送请求为 POST 请求 | 202 | | data_len | 发送数据的长度 | 203 | |response | 存放接收数据的字符串地址 | 204 | |resp_len | 接收数据长度的指针 | 205 | | **返回** | **描述** | 206 | | `>0` | 成功接收数据的长度 | 207 | | <=0 | 接收数据失败 | 208 | 209 | ## 拼接请求头部数据 210 | 211 | ```c 212 | int webclient_request_header_add(char **request_header, const char *fmt, ...); 213 | ``` 214 | 215 | 该函数适用于 webclient_request 函数发送请求之前,头部数据的拼接和添加。 216 | 217 | | 参数 | 描述 | 218 | |:------------------|:-----------------------------------| 219 | |request_header | 请求头部数据缓冲区地址 | 220 | |fmt | 添加字段数据的表达式 | 221 | |... | 添加的字段数据,为可变参数 | 222 | | **返回** | **描述** | 223 | | `>0` | 成功添加的字段数据的长度 | 224 | | <=0 | 头部数据添加失败或内存不足 | 225 | 226 | 227 | ## 获取 HTTP 响应状态码 228 | 229 | ```c 230 | int webclient_resp_status_get(struct webclient_session *session); 231 | ``` 232 | 233 | 该函数用于发送 GET 或 POST 请求之后,用于获取返回的响应状态码。 234 | 235 | | 参数 | 描述 | 236 | |:------------------|:-----------------------------------| 237 | |session | 当前连接会话结构体指针 | 238 | | **返回** | **描述** | 239 | | `>0` | HTTP 响应状态码 | 240 | 241 | ## 获取 Content-Length 字段数据 242 | 243 | ```c 244 | int webclient_content_length_get(struct webclient_session *session); 245 | ``` 246 | 247 | 该函数用于发送 GET 或 POST 请求之后,用于获取返回的 Content-Length 字段数据。 248 | 249 | | 参数 | 描述 | 250 | |:------------------|:-----------------------------------| 251 | |session | 当前连接会话结构体指针 | 252 | | **返回** | **描述** | 253 | | `>0` | Content-Length 字段数据 | 254 | | <0 | 获取失败 | 255 | 256 | ## 下载文件到本地 257 | 258 | ```c 259 | int webclient_get_file(const char *URI, const char *filename); 260 | ``` 261 | 262 | 从 HTTP 服务器下载文件并存放到本地。 263 | 264 | | 参数 | 描述 | 265 | |:------------------|:-----------------------------------| 266 | |URI | 连接的 HTTP 服务器地址 | 267 | |filename | 存放文件位置、名称 | 268 | | **返回** | **描述** | 269 | |=0 | 下载文件成功 | 270 | |<0 | 下载文件失败 | 271 | 272 | ## 上传文件到服务器 273 | 274 | ```c 275 | int webclient_post_file(const char *URI, const char *filename, const char *form_data); 276 | ``` 277 | 278 | 从 HTTP 服务器下载文件并存放到本地。 279 | 280 | | 参数 | 描述 | 281 | |:------------------|:-----------------------------------| 282 | |URI | 连接的 HTTP 服务器地址 | 283 | |filename | 需要上传的文件位置、名称 | 284 | |form_data | 附加选项 | 285 | | **返回** | **描述** | 286 | |=0 | 上传文件成功 | 287 | |<0 | 上传文件失败 | -------------------------------------------------------------------------------- /docs/figures/principle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/webclient/9b85d41dcaa473cb31baac19b0954469f5dcc0f5/docs/figures/principle.jpg -------------------------------------------------------------------------------- /docs/figures/request.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/webclient/9b85d41dcaa473cb31baac19b0954469f5dcc0f5/docs/figures/request.jpg -------------------------------------------------------------------------------- /docs/figures/response.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/webclient/9b85d41dcaa473cb31baac19b0954469f5dcc0f5/docs/figures/response.jpg -------------------------------------------------------------------------------- /docs/figures/webclient_cfg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/webclient/9b85d41dcaa473cb31baac19b0954469f5dcc0f5/docs/figures/webclient_cfg.jpg -------------------------------------------------------------------------------- /docs/introduction.md: -------------------------------------------------------------------------------- 1 | # 软件包介绍 2 | 3 | WebClient 软件包是 RT-Thread 自主研发的,基于 HTTP 协议的客户端的实现,它提供设备与 HTTP Server 的通讯的基本功能。 4 | 5 | ## 软件包目录结构 6 | 7 | WebClient 软件包目录结构如下所示: 8 | 9 | ```shell 10 | webclient 11 | +---docs 12 | │ +---figures // 文档使用图片 13 | │ │ api.md // API 使用说明 14 | │ │ introduction.md // 介绍文档 15 | │ │ principle.md // 实现原理 16 | │ │ README.md // 文档结构说明 17 | │ │ samples.md // 软件包示例 18 | │ │ user-guide.md // 使用说明 19 | │ +---version.md // 版本 20 | +---inc // 头文件 21 | +---src // 源文件 22 | +---samples // 示例代码 23 | │ │ webclient_get_sample // GET 请求示例代码 24 | │ +---webclient_post_sample // POST 请求示例代码 25 | │ LICENSE // 软件包许可证 26 | │ README.md // 软件包使用说明 27 | +---SConscript // RT-Thread 默认的构建脚本 28 | ``` 29 | 30 | ## 软件包功能特点 31 | 32 | WebClient 软件包功能特点: 33 | 34 | - 支持 IPV4/IPV6 地址 35 | 36 | WebClient 软件包会自动根据传入的 URI 地址的格式判断是 IPV4 地址或 IPV6 地址,并且从其中解析出连接服务器需要的信息,提高代码兼容性。 37 | 38 | - 支持 GET/POST 请求方法 39 | 40 | HTTP 有多种请求方法(GET、POST、PUT、DELETE等),目前 WebClient 软件包支持 GET 和 POST 请求方法,这也是嵌入式设备最常用到的两个命令类型,满足设备开发需求。 41 | 42 | - 支持文件的上传和下载功能 43 | 44 | WebClient 软件包提供文件上传和下载的接口函数,方便用户直接通过 GET/POST 请求方法上传本地文件到服务器或者下载服务器文件到本地,文件操作需要文件系统支持,使用前需开启并完成文件系统的移植。 45 | 46 | - 支持 HTTPS 加密传输 47 | 48 | HTTPS 协议(HyperText Transfer Protocol over Secure Socket Layer)和 HTTP 协议一样是基于 TCP 实现的,实际上是在原有的 HTTP 数据外部添加一层 TLS 加密的封装,从而达到加密传输数据的目的。HTTPS 协议地址区别于 HTTP 地址,是以 `https` 开头的。WebClient 软件包中的 TLS 加密方式依赖 [mbedtls 软件包](https://github.com/RT-Thread-packages/mbedtls) 实现。 49 | 50 | - 完善的头部数据添加和处理方式 51 | 52 | HTTP 头部信息用于确定当前请求或响应的数据和状态信息,在发送 GET/POST 请求时头部的拼接成为用户操作的一大难题,正常的做法是手动逐行输入或使用字符串拼接方式,WebClient 软件包中提供简单的添加发送请求头部信息的方式,方便用户使用。对于请求返回的头部信息,往往用户需要获取头部字段数据,WebClient 软件包同样提供了 `通过字段名获取字段数据的方式`,方便获取需要的数据。 53 | 54 | ## HTTP 协议介绍 55 | 56 | ### HTTP 协议简述 57 | 58 | HTTP (Hypertext Transfer Protocol)协议, 即超文本传输协议,是互联网上应用最为广泛的一种网络协议,由于其简捷、快速的方式,适用于分布式和合作式超媒体信息系统。HTTP 协议是基于 TCP/IP 协议的网络应用层协议。默认端口为 80 端口。协议最新版本是 HTTP 2.0,目前是用最广泛的是 HTTP 1.1。 59 | 60 | HTTP 协议是一种请求/响应式的协议。一个客户端与服务器建立连接之后,发送一个请求给服务器。服务器接收到请求之后,通过接收到的信息判断响应方式,并且给予客户端相应的响应,完成整个 HTTP 数据交互流程。 61 | 62 | 浏览器网页是 HTTP 的主要应用方式,但这不代表 HTTP 只能应用于网页,实际上只要通信的双方遵循 HTTP 协议数据传输合适就可以进行数据交互,比如嵌入式领域设备通过 HTTP 协议与服务器连接。 63 | 64 | ### HTTP 协议特点 65 | 66 | - 无状态协议 67 | 68 | HTTP 协议是`无状态协议`。无状态是指协议对于事件的处理没有记忆能力。这意味着如果后续处理需要前面的信息,则必须重传,这可能导致每次连接传送的数据量增大。这样的好处在于,在服务器不需要先前信息时它的应答就较快。 69 | 70 | - 灵活的数据传输 71 | 72 | HTTP 允许传输任意类型的数据对象,传输的数据类型由 Content-Type 加以标记区分。 73 | 74 | - 简单快速 75 | 76 | 客户端向服务器发送请求时,只需要传送请求方式和路径。由于HTTP协议简单,使得HTTP服务器的程序规模小,因而通信速度很快。 77 | 78 | - 支持 B/S 和 C/S 模式 79 | 80 | C/S 结构,即 Client/Server (客户机/服务器)结构。B/S 结构,即 Browser/Server (浏览器/服务器)结构。 81 | 82 | ### HTTP 协议请求信息 Request 83 | 84 | 客户端发送一个HTTP请求到服务器的请求消息包括: **请求行**、**请求头部**、**空行**和**请求数据**四个部分组成。 85 | 86 | - 请求行:用于说明请求类型,用来说明请求类型,要访问的资源以及所使用的HTTP版本; 87 | 88 | - 请求头部:紧接着请求行(即第一行)之后的部分,用来说明服务器要使用的附加信息; 89 | 90 | - 空行:请求头部后面的空行是必须的,区分头部和请求数据; 91 | 92 | - 请求数据:请求数据也叫主体,可以添加任意的其他数据。 93 | 94 | 如下图是一个 POST 请求的信息: 95 | 96 | ![HTTP 协议请求信息](figures/request.jpg) 97 | 98 | ### HTTP 协议响应信息 Response 99 | 100 | 一般情况下,服务器接收并处理客户端发过来的请求后会返回一个HTTP的响应消息。HTTP响应也由四个部分组成,分别是:**状态行**、**消息报头**、**空行**和**响应数据**。 101 | 102 | - 状态行:由 HTTP 协议号(HTTP 1.1),响应状态码(200),状态信息(OK)三部分组成; 103 | 104 | - 消息报头:用来说明客户端要使用的一些附加信息(日期,数据长度等); 105 | 106 | - 空行:消息报头后面的空行是必须的,用于区分消息报头和响应数据; 107 | 108 | - 响应数据:服务器返回给客户端的文本信息。 109 | 110 | 如下图是一个 POST 请求的响应的信息: 111 | 112 | ![HTTP 协议响应信息](figures/response.jpg) 113 | 114 | ### HTTP 协议状态码 115 | 116 | HTTP 协议中通过返回的状态码信息,判断当前响应状态,WebClient 软件包中也有对状态的获取和判断方式,这里主要介绍常见的状态码的意义。 117 | 118 | 状态代码有三位数字组成,第一个数字定义了响应的类别,共分五种类别: 119 | 120 | - 1xx:指示信息--表示请求已接收,继续处理 121 | 122 | - 2xx:成功--表示请求已被成功接收、理解、接受 123 | 124 | - 3xx:重定向--要完成请求必须进行更进一步的操作 125 | 126 | - 4xx:客户端错误--请求有语法错误或请求无法实现 127 | 128 | - 5xx:服务器端错误--服务器未能实现合法的请求 129 | 130 | 常见的状态码有以下几种: 131 | 132 | ```c 133 | 200 OK //客户端请求成功 134 | 206 Partial Content //服务器已经成功处理了部分 GET 请求 135 | 400 Bad Request //客户端请求有语法错误,不能被服务器所理解 136 | 403 Forbidden //服务器收到请求,但是拒绝提供服务 137 | 404 Not Found //请求资源不存在,eg:输入了错误的URL 138 | 500 Internal Server Error //服务器发生不可预期的错误 139 | 503 Server Unavailable //服务器当前不能处理客户端的请求 140 | ``` -------------------------------------------------------------------------------- /docs/migration-guide.md: -------------------------------------------------------------------------------- 1 | # 迁移指南 2 | 3 | ## 1、V1.0.0 -> V2.0.0 版本改动 4 | 5 | 本节主要介绍 WebClient 软件包版本升级之后(`V1.0.0` -> 最近版本 `V2.0.0`),软件包的改动和适配最新版本的方式。 6 | 7 | ### 函数改动 8 | 9 | 1. 添加 `webclient_session_create()` 创建客户端结构体函数 10 | 11 | V1.0.0 版本客户端会话结构体不需要手动创建,V2.0.0 版本改为手动创建,用户可自定义传入的头文件长度,减少资源占用。 12 | 13 | 2. `webclient_open` -> `wenclient_get` 14 | 15 | V1.0.0 发送 GET 命令的函数 `webclient_open` 改为 `wenclient_get`,两个函数区别在于 `webclient_open` 函数只能发送默认头部信息的 GET 请求, `wenclient_get` 函数既可以发送默认也可以发送自定义头部数据的 GET 请求。 16 | 17 | 3. `webclient_open_position` -> `wenclient_get_position` 18 | 19 | 同上述 `webclient_open` 函数改动类似。 20 | 21 | 4. 添加 `webclient_post` 发送 POST 请求函数 22 | 23 | V1.0.0 版本若要发送 POST 请求需要用户完成创建会话、连接、发送头部数据等整个流程,V2.0.0 版本封装了 `webclient_post` 函数完成整个 POST 请求发送流程,具体使用方式可参考 [API 手册](api.md) 和 [用户指南](user-guide.md)。 24 | 25 | 5. 添加头部信息处理方式 26 | 27 | V1.0.0 版本中没有对头部数据处理的方式,用户只能通过字符串拼接的方式来添加头部数据。V2.0.0 版本就这一问题提出相应改进方式,增加了在请求头中添加字段数据的函数(`webclient_header_fields_add`),以及通过字段名获取字段数据的函数(`webclient_header_fields_get`),方便用户添加请求头数据和获取响应头数据中信息。 28 | 29 | 6. 其他功能函数添加 30 | 31 | - 添加获取当前响应状态码函数(`webclient_resp_status_get` ); 32 | - 添加获取当前响应 Content-Length 数据函数(`webclient_content_length_get`)。 33 | 34 | ### 流程改动 35 | 36 | #### GET 请求流程改动 37 | 38 | 下面以发送自定义头部数据的 GET 请求方式为例,介绍新版本软件包中 GET 流程改动。 39 | 40 | V1.0.0 版本 GET 请求流如下: 41 | 42 | - 拼接头部数据(字符串拼接方式) 43 | - 创建客户端会话结构体 44 | - 客户端与服务器建立连接 45 | - 发送 GET 请求头部数据 46 | - 接收并解析服务器下发头部数据 47 | - 接收服务器发送主体数据 48 | - 关闭连接 49 | 50 | ```c 51 | /* 字符串处理方式拼接头部数据 */ 52 | const char header = "xxx"; 53 | struct webclient_session *session = RT_NULL; 54 | 55 | session = web_malloc(sizeof(struct webclient_session)) 56 | 57 | webclient_connect(session, URI); 58 | 59 | webclient_send_header(session, WEBCLIENT_GET, header, strlen(header)); 60 | 61 | webclient_handle_response(session); 62 | 63 | while(1) 64 | { 65 | webclient_read(session, post_data ,strlen(post_data)); 66 | ... 67 | } 68 | 69 | webclient_close(session); 70 | ``` 71 | 72 | V2.0.0 版本 GET 请求流如下: 73 | 74 | - 创建会话结构体 75 | - 拼接头部数据(函数拼接方式) 76 | - 发送 GET 请求头部并接收响应 77 | - 接收服务器下发数据 78 | - 关闭连接 79 | 80 | ```c 81 | struct webclient_session *session = RT_NULL; 82 | 83 | session = webclient_session_create(1024) 84 | 85 | webclient_header_fields_add(session, "HOST: %s", URI); 86 | 87 | webclient_get(session, URI); 88 | 89 | while(1) 90 | { 91 | webclient_read(session, buffer ,buf_sz); 92 | ... 93 | } 94 | 95 | webclient_close(session); 96 | ``` 97 | 98 | #### POST 请求流程改动 99 | 100 | 下面以发送自定义头部数据的 POST 请求方式为例,介绍新版本软件包中的 POST 流程改动。 101 | 102 | V1.0.0 版本 POST 请求流如下: 103 | 104 | - 拼接头部数据(字符串拼接方式) 105 | - 创建客户端会话结构体 106 | - 客户端与服务器建立连接 107 | - 发送 POST 请求头部数据 108 | - 接收并解析服务器下发头部数据 109 | - 上传数据到服务器 110 | - 关闭连接 111 | 112 | ```c 113 | /* 字符串处理方式拼接头部数据 */ 114 | const char header = "xxx"; 115 | struct webclient_session *session = RT_NULL; 116 | 117 | session = web_malloc(sizeof(struct webclient_session)) 118 | 119 | webclient_connect(session, URI); 120 | 121 | webclient_send_header(session, WEBCLIENT_POST, header, strlen(header)); 122 | 123 | webclient_handle_response(session); 124 | 125 | while(1) 126 | { 127 | webclient_write(session, post_data ,strlen(post_data)); 128 | ... 129 | } 130 | 131 | webclient_close(session); 132 | ``` 133 | 134 | V2.0.0 版本 POST 请求流如下: 135 | 136 | - 创建会话结构体 137 | - 拼接头部数据(函数拼接方式) 138 | - 发送头部数据和主体数据到服务器,并接收服务器响应 139 | - 关闭连接 140 | 141 | ```c 142 | struct webclient_session *session = RT_NULL; 143 | 144 | session = webclient_session_create(1024) 145 | 146 | webclient_header_fields_add(session, "Content-Length: %s", post_data_sz); 147 | 148 | webclient_post(session, URI, post_data, strlen(post_data)); 149 | 150 | webclient_close(session); 151 | ``` 152 | 153 | 上述介绍 WebClient 常用 GET/POST 请求方法改动,更多流程介绍请查看软件包 [使用指南](user-guide.md)。 154 | 155 | ## 2、V2.1.0 -> V2.2.0 版本改动 156 | 157 | V2.2.0 版本主要改动部分为函数接口参数定义,目的是为了适配非字符串格式数据的读写,其他函数的使用流程未产生明显改动。 158 | 159 | ### 函数改动 160 | 161 | - `webclient_post` 函数参数改动 162 | 163 | 该函数入参 `const char *post_data` 修改为 `const void *post_data`,并添加`size_t data_len` 参数,用于确定需要发送的数据长度。 164 | 165 | - `webclient_read` 函数参数改动 166 | 167 | 该函数入参 `unsigned char *buffer` 修改为 `void *buffer`,用于适配非字符串数据接收。 168 | 169 | - `webclient_write` 函数参数改动 170 | 171 | 该函数入参 `const unsigned char *buffer` 修改为 `const void *buffer`,用于适配非字符串数据发送。 172 | 173 | - `webclient_response` 函数参数改动 174 | 175 | 该函数入参 `unsigned char **response` 修改为 `void **response`,并且添加参数 `size_t *resp_len`,用于获取接收数据长度信息,适配非字符串数据接收。 176 | 177 | - `webclient_request` 函数参数改动 178 | 179 | 该函数入参 `const char *post_data` 修改为 `const void *post_data`,入参 `unsigned char **response` 修改为 `void **response`,并且添加入参 `size_t data_len` 和 `size_t *resp_len`,用于适配非字符串数据接收和发送。 180 | 181 | -------------------------------------------------------------------------------- /docs/principle.md: -------------------------------------------------------------------------------- 1 | # 工作原理 2 | 3 | WebClient 软件包主要用于在嵌入式设备上实现 HTTP 协议,软件包的主要工作原理基于 HTTP 协议实现,如下图所示: 4 | 5 | ![WebClient 软件包工作原理](figures/principle.jpg) 6 | 7 | HTTP 协议定义了客户端如何从服务器请求数据,以及服务器如何把数据传送给客户端的方式。HTTP 协议采用了`请求/响应模型`。 客户端向服务器发送一个请求报文,请求报文包含请求的方法、URL、协议版本、请求头部和请求数据。服务器以一个状态行作为响应,响应的内容包括协议的版本、成功或者错误代码、服务器信息、响应头部和响应数据。 8 | 9 | 在 HTTP 协议的实际使用过程中,一般遵循以下流程: 10 | 11 | 1. 客户端连接到服务器 12 | 13 | 通常是通过 TCP 三次握手建立 TCP 连接,默认 HTTP 端口号为 80。 14 | 15 | 2. 客户端发送 HTTP 请求(GET/POST) 16 | 17 | 通过TCP套接字,客户端向Web服务器发送一个文本的请求报文,一个请求报文由请求行、请求头部、空行和请求数据四部分组成 18 | 19 | 3. 服务器接受请求并返回 HTTP 响应 20 | 21 | 服务器解析请求,定位请求资源。服务器将需要发送的资源写到 TCP 套接字,由客户端读取。一个响应由状态行、响应头部、空行和响应数据四部分组成。 22 | 23 | 4. 客户端和服务器断开连接 24 | 25 | 若客户端和服务器之间连接模式为普通模式,则服务器主动关闭 TCP 连接,客户端被动关闭连接,释放 TCP 连接。若连接模式为 keepalive 模式,则该连接保持一段时间,在该时间内可以继续接收数据。 26 | 27 | 5. 客户端解析响应的数据内容 28 | 29 | 客户端获取数据后应该先解析响应状态码,判断请求是否成功,然后逐行解析响应报头,获取响应数据信息,最后读取响应数据,完成整个 HTTP 数据收发流程。 30 | 31 | -------------------------------------------------------------------------------- /docs/samples.md: -------------------------------------------------------------------------------- 1 | # 示例程序 2 | 3 | WebClient 软件包提供三个 HTTP Client 示例程序, 分别用于演示软件包支持的 GET 和 POST 功能,完成数据的上传与下载;以及一个完整的分片下载的功能。 4 | 5 | **示例文件** 6 | 7 | | 示例程序路径 | 说明 | 8 | | ---- | ---- | 9 | | samples/webclient_get_sample.c | GET 请求测试例程 | 10 | | samples/webclient_post_sample.c | POST 请求测试例程 | 11 | | samples/webclient_shard_download_sample.c | 分片下载测试例程 | 12 | 13 | ## 准备工作 14 | 15 | ### 获取软件包 16 | 17 | - menuconfig 配置获取软件包和示例代码 18 | 19 | 打开 RT-Thread 提供的 ENV 工具,使用 **menuconfig** 配置软件包。 20 | 21 | 启用 WebClient 软件包,并配置使能测试例程(Enable webclient GET/POST/SHARD samples),如下所示: 22 | 23 | ```shell 24 | RT-Thread online packages 25 | IoT - internet of things ---> 26 | [*] WebClient: A HTTP/HTTPS Client for RT-Thread 27 | [ ] Enable debug log output 28 | [*] Enable webclient GET/POST/SHARD samples # 开启 WebClient 测试例程 29 | [ ] Enable file download feature support 30 | Select TLS mode (Not support) ---> 31 | Version (latest) ---> # 开启使用最新版本软件包 32 | ``` 33 | 34 | - 使用 `pkgs --update` 命令下载软件包 35 | - 编译下载 36 | 37 | 38 | ## 启动例程 39 | 40 | 本例程使用的测试网站是 RT-Thread 系统的官方网站。GET 请求示例可以从网站中获取并打印显示文件内容;POST 请求示例可以上传数据到测试网站,测试网站会响应相同的数据。 41 | 42 | > HTTP 收发数据包括头部数据和正文数据两部分,以下称头部数据为 `header 数据`,正文数据为 `body 数据`。 43 | 44 | ### GET 请求示例 45 | 46 | GET 请求示例流程: 47 | 48 | - 创建 client 会话结构体 49 | - client 发送 GET 请求 header 数据(使用默认header 数据) 50 | - server 响应 header 数据和 body 数据 51 | - 打印 server 响应 body 数据 52 | - GET 请求测试完成/失败 53 | 54 | GET 请求示例使用方式有如下两种: 55 | 56 | - 在 MSH 中使用命令 `web_get_test` 执行 GET 请求示例程序,可以获取并打印显示默认网址下载的文件信息;在 MSH 中使用命令 `web_get_test -s` 执行 POST 请求示例程序,使用简化接口(webclient_request 接口)发送 GET请求,适用于简短数据的收发。如下图 LOG 显示: 57 | 58 | ```c 59 | msh />web_get_test 60 | webclient get response data: 61 | 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. 62 | 63 | msh />web_get_test -s 64 | webclient send get request by simplify request interface. 65 | webclient get response data: 66 | 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. 67 | 68 | msh /> 69 | ``` 70 | 71 | - 在 MSH 中使用命令 `web_get_test [URI]` 或 `web_get_test -s [URI]` 格式命令执行 GET 请求示例程序,其中 URI 为用户自定义的支持 GET 请求的地址。 72 | 73 | ### POST 请求示例 74 | 75 | POST 请求示例流程如下: 76 | 77 | - 创建 client 会话结构体 78 | - 拼接 POST 请求需要的 header 数据 79 | - client 发送拼接的 header 数据和 body 数据 80 | - server 响应 header 数据和 body 数据 81 | - 打印 server 响应 body 数据 82 | - POST 请求测试完成/失败 83 | 84 | POST 请求示例使用方式有如下两种: 85 | 86 | - 在 MSH 中使用命令 `web_post_test` 执行 POST 请求示例程序,可以获取并打印显示响应数据(默认 POST 请求的地址是类似于回显的地址,会返回上传的数据);在 MSH 中使用命令 `web_post_test -s` 执行 POST 请求示例程序,使用简化接口(webclient_request 接口)发送 POST 请求,适用于简短数据的收发。如下图 LOG 显示: 87 | 88 | ```c 89 | msh />web_post_test 90 | webclient post response data : 91 | RT-Thread is an open source IoT operating system from China! 92 | msh /> 93 | msh />web_post_test -s 94 | webclient send post request by simplify request interface. 95 | webclient post response data: 96 | RT-Thread is an open source IoT operating system from China! 97 | msh /> 98 | ``` 99 | 100 | - 在 MSH 中使用命令 `web_post_test [URI]` 或者 `web_post_test -s [URI]` 格式命令执行 POST 请求示例程序,其中 URI 为用户自定义的支持 POST 请求的地址。 101 | 102 | ### 分片下载示例 103 | 104 | 分片下载示例流程: 105 | 106 | - 创建 client 会话结构体 107 | - client 发送 HEAD 请求 header 数据 108 | - server 响应 header 数据 109 | - client 发送 GET 请求 header 数据,包含 Range 字段 110 | - server 响应 header 数据和指定长度的 body 数据 111 | - 循环发送请求直到接收完成所有数据 112 | - 分片下载测试完成/失败 113 | 114 | GET 请求示例使用方式有如下两种: 115 | 116 | - 在 MSH 中使用命令 `web_get_test -l [size]` 执行分片下载示例程序;指定允许接收的最大 body 数据长度,可以获取并打印显示默认网址下载的文件信息; 117 | ```c 118 | msh >web_shard_test -l 115 119 | Receive, len[0115]: 120 | 0000 - 0059: RT-Thread is an open source IoT operating system from China, 121 | 0060 - 0114: which has strong scalability: from a tiny kernel runni 122 | Total: [0115]Bytes 123 | 124 | Receive, len[0115]: 125 | 0000 - 0059: ng on a tiny core, for example ARM Cortex-M0, or Cortex-M3/4 126 | 0060 - 0114: /7, to a rich feature system running on MIPS32, ARM Cor 127 | Total: [0115]Bytes 128 | 129 | Receive, len[0037]: 130 | 0000 - 0036: tex-A8, ARM Cortex-A9 DualCore etc. 131 | 132 | Total: [0037]Bytes 133 | msh /> 134 | ``` 135 | 分多次下载得到的数据,与通用的 GET 请求获取的数据完全一致,分片下载功能正常。 136 | 137 | - 在 MSH 中使用命令 `web_get_test -u [URI]` 格式命令执行 GET 请求示例程序,其中 URI 为用户自定义的支持 GET 请求的地址。 -------------------------------------------------------------------------------- /docs/user-guide.md: -------------------------------------------------------------------------------- 1 | # 使用指南 2 | 3 | 本节主要介绍 WebClient 软包的基本使用流程, 并针对使用过程中经常涉及到的结构体和重要 API 进行简要说明。 4 | 5 | ## 准备工作 6 | 7 | 首先需要下载 WebClient 软件包,并将软件包加入到项目中。在 BSP 目录下使用 menuconfig 命令打开 env 配置界面,在 `RT-Thread online packages → IoT - internet of things` 中选择 WebClient 软件包,操作界面如下图所示: 8 | 9 | ![WebClient 软件包配置](figures/webclient_cfg.jpg) 10 | 11 | 详细配置介绍如下所示: 12 | 13 | ```shell 14 | RT-Thread online packages 15 | IoT - internet of things ---> 16 | [*] WebClient: A HTTP/HTTPS Client for RT-Thread 17 | [ ] Enable debug log output 18 | [ ] Enable webclient GET/POST samples 19 | Select TLS mode (Not support) ---> 20 | (x) Not support 21 | ( ) SAL TLS support 22 | ( ) MbedTLS support 23 | Version (latest) ---> 24 | ``` 25 | 26 | **Enable debug log output**:开启调试日志显示,可以用于查看请求和响应的头部数据信息; 27 | 28 | **Enable webclient GET/POST samples** :添加示例代码; 29 | 30 | **Select TLS mode** :配置开启 HTTPS 支持,选择支持的模式; 31 | 32 | - **Not support**:不支持 TLS 功能; 33 | - **SAL TLS support**:配置 SAL 组件中 TLS 功能支持,SAL 组件中抽象 TLS 操作,用户还需要**手动配置开启使用的 TLS 软件包类型**(目前只支持 MbedTLS 软件包); 34 | - **MbedTLS support**:配置 MbedTLS 功能支持; 35 | 36 | **Version** :配置软件包版本号。 37 | 38 | 选择合适的配置项后,使用 `pkgs --update` 命令下载软件包并更新用户配置。 39 | 40 | 41 | ## 使用流程 42 | 43 | 使用 WebClient 软件包发送 GET/POST 请求一般需要完成如下基本流程: 44 | 45 | (1) **创建客户端会话结构体** 46 | 47 | ```c 48 | struct webclient_header 49 | { 50 | char *buffer; //添加或者获取的头部数据 51 | size_t length; //存放当前头部数据长度 52 | 53 | size_t size; //存放最大支持的头部数据长度 54 | }; 55 | 56 | struct webclient_session 57 | { 58 | struct webclient_header *header; //保存头部信息结构体 59 | int socket; //当前连接套接字 60 | int resp_status; //响应状态码 61 | 62 | char *host; //连接服务器地址 63 | char *req_url; //连接的请求地址 64 | 65 | int chunk_sz; //chunk 模式下一块数据大小 66 | int chunk_offset; //chunk 模式剩余数据大小 67 | 68 | int content_length; //当前接收数据长度(非 chunk 模式) 69 | size_t content_remainder; //当前剩余接收数据长度 70 | 71 | rt_bool_t is_tls; //当前连接是否是 HTTPS 连接 72 | #ifdef WEBCLIENT_USING_MBED_TLS 73 | MbedTLSSession *tls_session; // HTTPS 协议相关会话结构体 74 | #endif 75 | }; 76 | ``` 77 | 78 | `webclient_session` 结构体用于存放当前建立的 HTTP 连接的部分信息,可用与 HTTP 数据交互整个流程。建立 HTTP 连接前需要创建并初始化该结构体,创建的方式示例如下: 79 | 80 | ```c 81 | struct webclient_session *session = RT_NULL; 82 | 83 | /* create webclient session and set header response size */ 84 | session = webclient_session_create(1024); 85 | if (session == RT_NULL) 86 | { 87 | ret = -RT_ENOMEM; 88 | goto __exit; 89 | } 90 | ``` 91 | 92 | (2) **拼接头部数据** 93 | 94 | WebClient 软件包提供两种请求头部发送方式: 95 | 96 | - 默认头部数据 97 | 98 | 如果要使用默认的头部信息,则不需要拼接任何头部数据,可直接调用 GET 发送命令。默认头部数据一般只用于 GET 请求。 99 | 100 | - 自定义头部数据 101 | 102 | 自定义头部数据使用 `webclient_header_fields_add` 函数添加头部信息,添加的头部信息位于客户端会话结构体中,在发送 GET/POST 请求时发送。 103 | 104 | 添加示例代码如下: 105 | 106 | ```c 107 | /* 拼接头部信息 */ 108 | webclient_header_fields_add(session, "Content-Length: %d\r\n", strlen(post_data)); 109 | 110 | webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n"); 111 | ``` 112 | 113 | (3) **发送 GET/POST 请求** 114 | 115 | 头部信息添加完成之后,就可以调用 `webclient_get` 函数或者 `webclient_post` 函数发送 GET/POST 请求命令了,函数中主要操作如下: 116 | 117 | - 通过传入的 URI 获取信息,建立 TCP 连接; 118 | 119 | - 发送默认或者拼接的头部信息; 120 | 121 | - 接收并解析响应数据的头部信息; 122 | 123 | - 返回错误或者响应状态码。 124 | 125 | 发送 GET 请求示例代码如下: 126 | 127 | ```c 128 | int resp_status = 0; 129 | 130 | /* send GET request by default header */ 131 | if ((resp_status = webclient_get(session, URI)) != 200) 132 | { 133 | LOG_E("webclient GET request failed, response(%d) error.", resp_status); 134 | ret = -RT_ERROR; 135 | goto __exit; 136 | } 137 | ``` 138 | 139 | (4) **接收响应的数据** 140 | 141 | 发送 GET/POST 请求之后,可以使用 `webclient_read` 函数接收响应的实际数据。因为响应的实际数据可能比较长,所以往往我们需要循环接收响应数据,直到数据接收完毕。 142 | 143 | 如下所示为循环接收并打印响应数据方式: 144 | 145 | ```c 146 | int content_pos = 0; 147 | /* 获取接收的响应数据长度 */ 148 | int content_length = webclient_content_length_get(session); 149 | 150 | /* 循环接收响应数据直到数据接收完毕 */ 151 | do 152 | { 153 | bytes_read = webclient_read(session, buffer, 1024); 154 | if (bytes_read <= 0) 155 | { 156 | break; 157 | } 158 | 159 | /* 打印响应数据 */ 160 | for (index = 0; index < bytes_read; index++) 161 | { 162 | rt_kprintf("%c", buffer[index]); 163 | } 164 | 165 | content_pos += bytes_read; 166 | } while (content_pos < content_length); 167 | ``` 168 | 169 | (5) **关闭并释放客户端会话结构体** 170 | 171 | 请求发送并接收完成之后,需要使用 `webclient_close` 函数关闭并释放客户端会话结构体,完成整个 HTTP 数据交互流程。 172 | 173 | 使用方式如下: 174 | 175 | ```c 176 | if (session) 177 | { 178 | webclient_close(session); 179 | } 180 | ``` 181 | 182 | ## 使用方式 183 | 184 | WenClient 软件包对于 GET/POST 请求,分别提供了几种不同的使用方式,用于不同的情况。 185 | 186 | ### GET 请求方式 187 | 188 | - 使用默认头部发送 GET 请求 189 | 190 | ```c 191 | struct webclient_session *session = NULL; 192 | 193 | session = webclient_create(1024); 194 | 195 | if(webclient_get(session, URI) != 200) 196 | { 197 | LOG_E("error!"); 198 | } 199 | 200 | while(1) 201 | { 202 | webclient_read(session, buffer, bfsz); 203 | ... 204 | } 205 | 206 | webclient_close(session); 207 | ``` 208 | 209 | - 使用自定义头部发送 GET 请求 210 | 211 | ```c 212 | struct webclient_session *session = NULL; 213 | 214 | session = webclient_create(1024); 215 | 216 | webclient_header_fields_add(session, "User-Agent: RT-Thread HTTP Agent\r\n"); 217 | 218 | if(webclient_get(session, URI) != 200) 219 | { 220 | LOG_E("error!"); 221 | } 222 | 223 | while(1) 224 | { 225 | webclient_read(session, buffer, bfsz); 226 | ... 227 | } 228 | 229 | webclient_close(session); 230 | ``` 231 | 232 | - 发送获取部分数据的 GET 请求(多用于断点续传/分片下载) 233 | 234 | ```c 235 | struct webclient_session *session = NULL; 236 | 237 | session = webclient_create(1024); 238 | 239 | webclient_connect(session, URI); 240 | webclient_header_fields_add(session, "Range: bytes=%d-%d\r\n", 0, 99); 241 | webclient_send_header(session, WEBCLIENT_GET); 242 | 243 | while(1) 244 | { 245 | webclient_read(session, buffer, bfsz); 246 | ... 247 | } 248 | 249 | webclient_close(session); 250 | ``` 251 | 252 | - 使用 `webclient_response` 接收 GET 数据 253 | 254 | 多用于接收数据长度较小的 GET 请求。 255 | 256 | ```c 257 | struct webclient_session *session = NULL; 258 | size_t length = 0; 259 | char *result; 260 | 261 | session = webclient_create(1024); 262 | 263 | if(webclient_get(session, URI) != 200) 264 | { 265 | LOG_E("error!"); 266 | } 267 | 268 | webclient_response(session, &result, &length); 269 | 270 | web_free(result); 271 | webclient_close(session); 272 | ``` 273 | 274 | - 使用 `webclient_request` 函数发送并接收 GET 请求 275 | 276 | 多用于接收数据长度较小,且头部信息已经拼接给出的 GET 请求。 277 | 278 | ```c 279 | size_t length = 0; 280 | char *result, *header = RT_NULL; 281 | 282 | /* 拼接自定义头部数据 */ 283 | webclient_request_header_add(&header, "User-Agent: RT-Thread HTTP Agent\r\n"); 284 | 285 | webclient_request(URI, header, NULL, 0, &result, &length); 286 | 287 | web_free(result); 288 | ``` 289 | 290 | ### POST 请求方式 291 | 292 | - 分段数据 POST 请求 293 | 294 | 多用于上传数据量较大的 POST 请求,如:上传文件到服务器。 295 | 296 | ```c 297 | struct webclient_session *session = NULL; 298 | 299 | session = webclient_create(1024); 300 | 301 | /* 拼接必要的头部信息 */ 302 | webclient_header_fields_add(session, "Content-Length: %d\r\n", post_data_sz); 303 | webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n"); 304 | 305 | /* 分段数据上传 webclient_post 第三个传输上传数据为 NULL,改为下面循环上传数据*/ 306 | if( webclient_post(session, URI, NULL, 0) != 200) 307 | { 308 | LOG_E("error!"); 309 | } 310 | 311 | while(1) 312 | { 313 | webclient_write(session, post_data, 1024); 314 | ... 315 | } 316 | 317 | if( webclient_handle_response(session) != 200) 318 | { 319 | LOG_E("error!"); 320 | } 321 | 322 | webclient_close(session); 323 | ``` 324 | 325 | - 整段数据 POST 请求 326 | 327 | 多用于上传数据量较小的 POST 请求。 328 | 329 | ```c 330 | char *post_data = "abcdefg"; 331 | 332 | session = webclient_create(1024); 333 | 334 | /* 拼接必要的头部信息 */ 335 | webclient_header_fields_add(session, "Content-Length: %d\r\n", strlen(post_data)); 336 | webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n"); 337 | 338 | if(webclient_post(session, URI, post_data, strlen(post_data)) != 200); 339 | { 340 | LOG_E("error!"); 341 | } 342 | webclient_close(session); 343 | ``` 344 | 345 | - 使用 `webclient_request` 函数发送 POST 请求 346 | 347 | 多用于上传文件较小且头头部信息已经拼接给出的 POST 请求。 348 | 349 | ```c 350 | char *post_data = "abcdefg"; 351 | char *header = RT_NULL; 352 | 353 | /* 拼接自定义头部数据 */ 354 | webclient_request_header_add(&header, "Content-Length: %d\r\n", strlen(post_data)); 355 | webclient_request_header_add(&header, "Content-Type: application/octet-stream\r\n"); 356 | 357 | webclient_request(URI, header, post_data, strlen(post_data), NULL, NULL); 358 | ``` 359 | 360 | ## 常见问题 361 | 362 | ### HTTPS 地址不支持 363 | 364 | ```c 365 | [E/WEB]not support https connect, please enable webclient https configure! 366 | ``` 367 | 368 | - 原因:使用 HTTPS 地址但是没有开启 HTTPS 支持。 369 | 370 | - 解决方法:在 WebClient 软件包 menuconfig 配置选项中 选择 `Select TLS mode` 选项为 `MbedTLS support` 或者 `SAL TLS support`。 371 | 372 | ### 头部数据长度超出 373 | 374 | ```c 375 | [E/WEB]not enough header buffer size(xxx)! 376 | ``` 377 | 378 | - 原因:添加的头部数据长度超过了最大支持的头部数据长度。 379 | 380 | - 解决方法:在创建客户端会话结构体的时候,增大传入的最大支持的头部数据长度。 381 | -------------------------------------------------------------------------------- /docs/version.md: -------------------------------------------------------------------------------- 1 | # 版本和修订 2 | 3 | | Date | Version | Author | Note | 4 | | -------- | :-----: | :---- | :---- | 5 | | 2013-05-05 | v1.0.0 | bernard | 初始版本 | 6 | | 2018-08-06 | v2.0.0 | chenyong | 版本更新 | -------------------------------------------------------------------------------- /inc/webclient.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2013-05-05 Bernard the first version 9 | * 2013-06-10 Bernard fix the slow speed issue when download file. 10 | * 2015-11-14 aozima add content_length_remainder. 11 | * 2017-12-23 aozima update gethostbyname to getaddrinfo. 12 | * 2018-01-04 aozima add ipv6 address support. 13 | * 2018-07-26 chenyong modify log information 14 | * 2018-08-07 chenyong modify header processing 15 | */ 16 | 17 | #ifndef __WEBCLIENT_H__ 18 | #define __WEBCLIENT_H__ 19 | 20 | #include 21 | #include 22 | 23 | #if defined(WEBCLIENT_USING_MBED_TLS) || defined(WEBCLIENT_USING_SAL_TLS) 24 | #include 25 | #endif 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #ifndef web_malloc 32 | #define web_malloc rt_malloc 33 | #endif 34 | 35 | #ifndef web_calloc 36 | #define web_calloc rt_calloc 37 | #endif 38 | 39 | #ifndef web_realloc 40 | #define web_realloc rt_realloc 41 | #endif 42 | 43 | #ifndef web_free 44 | #define web_free rt_free 45 | #endif 46 | 47 | /** 48 | * The gcc libc api is not threadsafe, 49 | * especially the float type operation. 50 | * So, use rt_xxx whose RT-Thread threadsafe api to instead of strandard libc api. 51 | */ 52 | #ifndef web_memset 53 | #define web_memset rt_memset 54 | #endif 55 | 56 | #ifndef web_memcpy 57 | #define web_memcpy rt_memcpy 58 | #endif 59 | 60 | #ifndef web_memcmp 61 | #define web_memcmp rt_memcmp 62 | #endif 63 | 64 | #ifndef web_snprintf 65 | #define web_snprintf rt_snprintf 66 | #endif 67 | 68 | #ifndef web_vsnprintf 69 | #define web_vsnprintf rt_vsnprintf 70 | #endif 71 | 72 | #ifndef web_strdup 73 | #define web_strdup rt_strdup 74 | #endif 75 | 76 | #define WEBCLIENT_SW_VERSION "2.3.0" 77 | #define WEBCLIENT_SW_VERSION_NUM 0x20300 78 | 79 | #define WEBCLIENT_HEADER_BUFSZ 4096 80 | #define WEBCLIENT_RESPONSE_BUFSZ 4096 81 | 82 | enum WEBCLIENT_STATUS 83 | { 84 | WEBCLIENT_OK, 85 | WEBCLIENT_ERROR, 86 | WEBCLIENT_TIMEOUT, 87 | WEBCLIENT_NOMEM, 88 | WEBCLIENT_NOSOCKET, 89 | WEBCLIENT_NOBUFFER, 90 | WEBCLIENT_CONNECT_FAILED, 91 | WEBCLIENT_DISCONNECT, 92 | WEBCLIENT_FILE_ERROR, 93 | }; 94 | 95 | enum WEBCLIENT_METHOD 96 | { 97 | WEBCLIENT_USER_METHOD, 98 | WEBCLIENT_GET, 99 | WEBCLIENT_POST, 100 | WEBCLIENT_HEAD 101 | }; 102 | 103 | struct webclient_header 104 | { 105 | char *buffer; 106 | size_t length; /* content header buffer size */ 107 | 108 | size_t size; /* maximum support header size */ 109 | }; 110 | 111 | struct webclient_session 112 | { 113 | struct webclient_header *header; /* webclient response header information */ 114 | int socket; 115 | int resp_status; 116 | 117 | char *host; /* server host */ 118 | char *req_url; /* HTTP request address*/ 119 | 120 | int chunk_sz; 121 | int chunk_offset; 122 | 123 | int content_length; 124 | size_t content_remainder; /* remainder of content length */ 125 | int (*handle_function)(char *buffer, int size); /* handle function */ 126 | 127 | rt_bool_t is_tls; /* HTTPS connect */ 128 | #ifdef WEBCLIENT_USING_MBED_TLS 129 | MbedTLSSession *tls_session; /* mbedtls connect session */ 130 | #endif 131 | }; 132 | 133 | /* create webclient session and set header response size */ 134 | struct webclient_session *webclient_session_create(size_t header_sz); 135 | 136 | /* send HTTP GET request */ 137 | int webclient_get(struct webclient_session *session, const char *URI); 138 | 139 | /* send HTTP HEAD request */ 140 | int webclient_shard_head_function(struct webclient_session *session, const char *URI, int *length); 141 | 142 | /* send HTTP Range parameter, shard download */ 143 | int webclient_shard_position_function(struct webclient_session *session, const char *URI, int start, int length, int mem_size); 144 | int *webclient_register_shard_position_function(struct webclient_session *session, int (*handle_function)(char *buffer, int size)); 145 | 146 | /* send HTTP POST request */ 147 | int webclient_post(struct webclient_session *session, const char *URI, const void *post_data, size_t data_len); 148 | 149 | /* close and release wenclient session */ 150 | int webclient_close(struct webclient_session *session); 151 | 152 | int webclient_set_timeout(struct webclient_session *session, int millisecond); 153 | 154 | /* send or receive data from server */ 155 | int webclient_read(struct webclient_session *session, void *buffer, size_t size); 156 | int webclient_write(struct webclient_session *session, const void *buffer, size_t size); 157 | 158 | /* webclient GET/POST header buffer operate by the header fields */ 159 | int webclient_header_fields_add(struct webclient_session *session, const char *fmt, ...); 160 | const char *webclient_header_fields_get(struct webclient_session *session, const char *fields); 161 | 162 | /* send HTTP POST/GET request, and get response data */ 163 | int webclient_response(struct webclient_session *session, void **response, size_t *resp_len); 164 | int webclient_request(const char *URI, const char *header, const void *post_data, size_t data_len, void **response, size_t *resp_len); 165 | int webclient_request_header_add(char **request_header, const char *fmt, ...); 166 | int webclient_resp_status_get(struct webclient_session *session); 167 | int webclient_content_length_get(struct webclient_session *session); 168 | 169 | #ifdef RT_USING_DFS 170 | /* file related operations */ 171 | int webclient_get_file(const char *URI, const char *filename); 172 | int webclient_post_file(const char *URI, const char *filename, const char *form_data); 173 | #endif 174 | 175 | #ifdef __cplusplus 176 | } 177 | #endif 178 | 179 | #endif 180 | -------------------------------------------------------------------------------- /samples/webclient_get_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-03 chenyong the first version 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | #define GET_HEADER_BUFSZ 1024 15 | #define GET_RESP_BUFSZ 1024 16 | 17 | #define GET_LOCAL_URI "http://www.rt-thread.com/service/rt-thread.txt" 18 | 19 | /* send HTTP GET request by common request interface, it used to receive longer data */ 20 | static int webclient_get_comm(const char *uri) 21 | { 22 | struct webclient_session* session = RT_NULL; 23 | unsigned char *buffer = RT_NULL; 24 | int index, ret = 0; 25 | int bytes_read, resp_status; 26 | int content_length = -1; 27 | 28 | buffer = (unsigned char *) web_malloc(GET_RESP_BUFSZ); 29 | if (buffer == RT_NULL) 30 | { 31 | rt_kprintf("no memory for receive buffer.\n"); 32 | ret = -RT_ENOMEM; 33 | goto __exit; 34 | 35 | } 36 | 37 | /* create webclient session and set header response size */ 38 | session = webclient_session_create(GET_HEADER_BUFSZ); 39 | if (session == RT_NULL) 40 | { 41 | ret = -RT_ENOMEM; 42 | goto __exit; 43 | } 44 | 45 | /* send GET request by default header */ 46 | if ((resp_status = webclient_get(session, uri)) != 200) 47 | { 48 | rt_kprintf("webclient GET request failed, response(%d) error.\n", resp_status); 49 | ret = -RT_ERROR; 50 | goto __exit; 51 | } 52 | 53 | rt_kprintf("webclient get response data: \n"); 54 | 55 | content_length = webclient_content_length_get(session); 56 | if (content_length < 0) 57 | { 58 | rt_kprintf("webclient GET request type is chunked.\n"); 59 | do 60 | { 61 | bytes_read = webclient_read(session, (void *)buffer, GET_RESP_BUFSZ); 62 | if (bytes_read <= 0) 63 | { 64 | break; 65 | } 66 | 67 | for (index = 0; index < bytes_read; index++) 68 | { 69 | rt_kprintf("%c", buffer[index]); 70 | } 71 | } while (1); 72 | 73 | rt_kprintf("\n"); 74 | } 75 | else 76 | { 77 | int content_pos = 0; 78 | 79 | do 80 | { 81 | bytes_read = webclient_read(session, (void *)buffer, 82 | content_length - content_pos > GET_RESP_BUFSZ ? 83 | GET_RESP_BUFSZ : content_length - content_pos); 84 | if (bytes_read <= 0) 85 | { 86 | break; 87 | } 88 | 89 | for (index = 0; index < bytes_read; index++) 90 | { 91 | rt_kprintf("%c", buffer[index]); 92 | } 93 | 94 | content_pos += bytes_read; 95 | } while (content_pos < content_length); 96 | 97 | rt_kprintf("\n"); 98 | } 99 | 100 | __exit: 101 | if (session) 102 | { 103 | webclient_close(session); 104 | session = RT_NULL; 105 | } 106 | 107 | if (buffer) 108 | { 109 | web_free(buffer); 110 | } 111 | 112 | return ret; 113 | } 114 | 115 | /* send HTTP GET request by simplify request interface, it used to received shorter data */ 116 | static int webclient_get_smpl(const char *uri) 117 | { 118 | char *response = RT_NULL; 119 | size_t resp_len = 0; 120 | int index; 121 | 122 | if (webclient_request(uri, RT_NULL, RT_NULL, 0, (void **)&response, &resp_len) < 0) 123 | { 124 | rt_kprintf("webclient send get request failed."); 125 | return -RT_ERROR; 126 | } 127 | 128 | rt_kprintf("webclient send get request by simplify request interface.\n"); 129 | rt_kprintf("webclient get response data: \n"); 130 | for (index = 0; index < strlen(response); index++) 131 | { 132 | rt_kprintf("%c", response[index]); 133 | } 134 | rt_kprintf("\n"); 135 | 136 | if (response) 137 | { 138 | web_free(response); 139 | } 140 | 141 | return 0; 142 | } 143 | 144 | 145 | int webclient_get_test(int argc, char **argv) 146 | { 147 | char *uri = RT_NULL; 148 | 149 | if (argc == 1) 150 | { 151 | uri = web_strdup(GET_LOCAL_URI); 152 | if(uri == RT_NULL) 153 | { 154 | rt_kprintf("no memory for create get request uri buffer.\n"); 155 | return -RT_ENOMEM; 156 | } 157 | 158 | webclient_get_comm(uri); 159 | } 160 | else if (argc == 2) 161 | { 162 | if (strcmp(argv[1], "-s") == 0) 163 | { 164 | uri = web_strdup(GET_LOCAL_URI); 165 | if(uri == RT_NULL) 166 | { 167 | rt_kprintf("no memory for create get request uri buffer.\n"); 168 | return -RT_ENOMEM; 169 | } 170 | 171 | webclient_get_smpl(uri); 172 | } 173 | else 174 | { 175 | uri = web_strdup(argv[1]); 176 | if(uri == RT_NULL) 177 | { 178 | rt_kprintf("no memory for create get request uri buffer.\n"); 179 | return -RT_ENOMEM; 180 | } 181 | webclient_get_comm(uri); 182 | } 183 | } 184 | else if(argc == 3 && strcmp(argv[1], "-s") == 0) 185 | { 186 | uri = web_strdup(argv[2]); 187 | if(uri == RT_NULL) 188 | { 189 | rt_kprintf("no memory for create get request uri buffer.\n"); 190 | return -RT_ENOMEM; 191 | } 192 | 193 | webclient_get_smpl(uri); 194 | } 195 | else 196 | { 197 | rt_kprintf("web_get_test [URI] - webclient GET request test.\n"); 198 | rt_kprintf("web_get_test -s [URI] - webclient simplify GET request test.\n"); 199 | return -RT_ERROR; 200 | } 201 | 202 | if (uri) 203 | { 204 | web_free(uri); 205 | } 206 | 207 | return RT_EOK; 208 | } 209 | 210 | #ifdef FINSH_USING_MSH 211 | #include 212 | MSH_CMD_EXPORT_ALIAS(webclient_get_test, web_get_test, webclient get request test); 213 | #endif /* FINSH_USING_MSH */ 214 | -------------------------------------------------------------------------------- /samples/webclient_post_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-03 chenyong the first version 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | #define POST_RESP_BUFSZ 1024 17 | #define POST_HEADER_BUFSZ 1024 18 | 19 | #define POST_LOCAL_URI "http://www.rt-thread.com/service/echo" 20 | 21 | const char *post_data = "RT-Thread is an open source IoT operating system from China!"; 22 | 23 | /* send HTTP POST request by common request interface, it used to receive longer data */ 24 | static int webclient_post_comm(const char *uri, const void *post_data, size_t data_len) 25 | { 26 | struct webclient_session* session = RT_NULL; 27 | unsigned char *buffer = RT_NULL; 28 | int index, ret = 0; 29 | int bytes_read, resp_status; 30 | 31 | buffer = (unsigned char *) web_malloc(POST_RESP_BUFSZ); 32 | if (buffer == RT_NULL) 33 | { 34 | rt_kprintf("no memory for receive response buffer.\n"); 35 | ret = -RT_ENOMEM; 36 | goto __exit; 37 | } 38 | 39 | /* create webclient session and set header response size */ 40 | session = webclient_session_create(POST_HEADER_BUFSZ); 41 | if (session == RT_NULL) 42 | { 43 | ret = -RT_ENOMEM; 44 | goto __exit; 45 | } 46 | 47 | /* build header for upload */ 48 | webclient_header_fields_add(session, "Content-Length: %d\r\n", strlen(post_data)); 49 | webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n"); 50 | 51 | /* send POST request by default header */ 52 | if ((resp_status = webclient_post(session, uri, post_data, data_len)) != 200) 53 | { 54 | rt_kprintf("webclient POST request failed, response(%d) error.\n", resp_status); 55 | ret = -RT_ERROR; 56 | goto __exit; 57 | } 58 | 59 | rt_kprintf("webclient post response data: \n"); 60 | do 61 | { 62 | bytes_read = webclient_read(session, buffer, POST_RESP_BUFSZ); 63 | if (bytes_read <= 0) 64 | { 65 | break; 66 | } 67 | 68 | for (index = 0; index < bytes_read; index++) 69 | { 70 | rt_kprintf("%c", buffer[index]); 71 | } 72 | } while (1); 73 | 74 | rt_kprintf("\n"); 75 | 76 | __exit: 77 | if (session) 78 | { 79 | webclient_close(session); 80 | session = RT_NULL; 81 | } 82 | 83 | if (buffer) 84 | { 85 | web_free(buffer); 86 | } 87 | 88 | return ret; 89 | } 90 | 91 | /* send HTTP POST request by simplify request interface, it used to received shorter data */ 92 | static int webclient_post_smpl(const char *uri, const char *post_data, size_t data_len) 93 | { 94 | char *response = RT_NULL; 95 | char *header = RT_NULL; 96 | size_t resp_len = 0; 97 | int index = 0; 98 | 99 | webclient_request_header_add(&header, "Content-Length: %d\r\n", strlen(post_data)); 100 | webclient_request_header_add(&header, "Content-Type: application/octet-stream\r\n"); 101 | 102 | if (webclient_request(uri, header, post_data, data_len, (void **)&response, &resp_len) < 0) 103 | { 104 | rt_kprintf("webclient send post request failed."); 105 | web_free(header); 106 | return -RT_ERROR; 107 | } 108 | 109 | rt_kprintf("webclient send post request by simplify request interface.\n"); 110 | rt_kprintf("webclient post response data: \n"); 111 | for (index = 0; index < resp_len; index++) 112 | { 113 | rt_kprintf("%c", response[index]); 114 | } 115 | rt_kprintf("\n"); 116 | 117 | if (header) 118 | { 119 | web_free(header); 120 | } 121 | 122 | if (response) 123 | { 124 | web_free(response); 125 | } 126 | 127 | return 0; 128 | } 129 | 130 | 131 | int webclient_post_test(int argc, char **argv) 132 | { 133 | char *uri = RT_NULL; 134 | 135 | if (argc == 1) 136 | { 137 | uri = web_strdup(POST_LOCAL_URI); 138 | if(uri == RT_NULL) 139 | { 140 | rt_kprintf("no memory for create post request uri buffer.\n"); 141 | return -RT_ENOMEM; 142 | } 143 | 144 | webclient_post_comm(uri, (void *)post_data, strlen(post_data)); 145 | } 146 | else if (argc == 2) 147 | { 148 | if (strcmp(argv[1], "-s") == 0) 149 | { 150 | uri = web_strdup(POST_LOCAL_URI); 151 | if(uri == RT_NULL) 152 | { 153 | rt_kprintf("no memory for create post request uri buffer.\n"); 154 | return -RT_ENOMEM; 155 | } 156 | 157 | webclient_post_smpl(uri, (void *)post_data, strlen(post_data)); 158 | } 159 | else 160 | { 161 | uri = web_strdup(argv[1]); 162 | if(uri == RT_NULL) 163 | { 164 | rt_kprintf("no memory for create post request uri buffer.\n"); 165 | return -RT_ENOMEM; 166 | } 167 | webclient_post_comm(uri, (void *)post_data, strlen(post_data)); 168 | } 169 | } 170 | else if(argc == 3 && strcmp(argv[1], "-s") == 0) 171 | { 172 | uri = web_strdup(argv[2]); 173 | if(uri == RT_NULL) 174 | { 175 | rt_kprintf("no memory for create post request uri buffer.\n"); 176 | return -RT_ENOMEM; 177 | } 178 | 179 | webclient_post_smpl(uri, (void *)post_data, strlen(post_data)); 180 | } 181 | else 182 | { 183 | rt_kprintf("web_post_test [uri] - webclient post request test.\n"); 184 | rt_kprintf("web_post_test -s [uri] - webclient simplify post request test.\n"); 185 | return -RT_ERROR; 186 | } 187 | 188 | if (uri) 189 | { 190 | web_free(uri); 191 | } 192 | 193 | return RT_EOK; 194 | } 195 | 196 | 197 | #ifdef FINSH_USING_MSH 198 | #include 199 | MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post_test, webclient post request test.); 200 | #endif /* FINSH_USING_MSH */ 201 | -------------------------------------------------------------------------------- /samples/webclient_shard_download_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2021-06-03 xiangxistu the first version 9 | */ 10 | 11 | #include 12 | #include 13 | #include "stdlib.h" 14 | 15 | #define GET_LOCAL_URI "http://www.rt-thread.com/service/rt-thread.txt" 16 | #define CHARACTER_LENGTH 60 17 | 18 | /* handle function, you can store data and so on */ 19 | static int shard_download_handle(char *buffer, int length) 20 | { 21 | int outindex, inindex = 0; 22 | int boundary; 23 | 24 | /* print the receive data */ 25 | for (outindex = 0; outindex < length; outindex = outindex + inindex) 26 | { 27 | char print_buffer[CHARACTER_LENGTH + 1] = {0}; 28 | char *point = RT_NULL; 29 | point = print_buffer; 30 | 31 | if(length - outindex > CHARACTER_LENGTH) 32 | { 33 | boundary = CHARACTER_LENGTH; 34 | } 35 | else 36 | { 37 | boundary = length - outindex; 38 | } 39 | 40 | for (inindex = 0; inindex < boundary; inindex++) 41 | { 42 | *point++ = buffer[outindex + inindex]; 43 | } 44 | *point = 0; 45 | rt_kprintf("%04d - %04d: %s\n", outindex, outindex + boundary - 1, print_buffer); 46 | } 47 | 48 | /* release this buffer if we have handled data */ 49 | web_free(buffer); 50 | 51 | return RT_EOK; 52 | } 53 | 54 | 55 | int webclient_shard_download_test(int argc, char **argv) 56 | { 57 | struct webclient_session* session = RT_NULL; 58 | rt_err_t result = RT_EOK; 59 | char *uri = RT_NULL; 60 | int length = 0; 61 | int usage_flag = 0; 62 | int size = 200; 63 | 64 | 65 | if (argc == 1) 66 | { 67 | uri = web_strdup(GET_LOCAL_URI); 68 | } 69 | else 70 | { 71 | int index; 72 | for(index = 1; index < argc; index = index + 2) 73 | { 74 | if(strstr(argv[index], "-u")) 75 | { 76 | uri = web_strdup(argv[index + 1]); 77 | } 78 | else if(strstr(argv[index], "-l")) 79 | { 80 | size = atoi(argv[index + 1]); 81 | } 82 | else 83 | { 84 | usage_flag = 1; 85 | break; 86 | } 87 | } 88 | } 89 | 90 | if(usage_flag) 91 | { 92 | rt_kprintf("web_shard_test -u [URI] - webclient HEAD and GET request test.\n"); 93 | rt_kprintf("web_shard_test -l [SIZE] - the length of receive buffer.\n"); 94 | return -RT_ERROR; 95 | } 96 | 97 | if(uri == RT_NULL) 98 | { 99 | uri = web_strdup(GET_LOCAL_URI); 100 | } 101 | 102 | /* sometime, the header bufsz can set more smaller */ 103 | session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ / 4); 104 | if (session == RT_NULL) 105 | { 106 | result = -RT_ENOMEM; 107 | goto __exit; 108 | } 109 | 110 | /* get the real data length */ 111 | webclient_shard_head_function(session, uri, &length); 112 | 113 | /* register the handle function, you can handle data in the function */ 114 | webclient_register_shard_position_function(session, shard_download_handle); 115 | 116 | /* the "memory size" that you can provide in the project and uri */ 117 | result = webclient_shard_position_function(session, uri, 0, length, size); 118 | if(result != WEBCLIENT_OK) 119 | { 120 | rt_kprintf("web shard download, test failed!\n"); 121 | } 122 | 123 | /* clear the handle function */ 124 | webclient_register_shard_position_function(session, RT_NULL); 125 | 126 | __exit: 127 | if (uri) 128 | { 129 | web_free(uri); 130 | } 131 | 132 | if (session) 133 | { 134 | webclient_close(session); 135 | session = RT_NULL; 136 | } 137 | 138 | return result; 139 | } 140 | 141 | #ifdef FINSH_USING_MSH 142 | #include 143 | MSH_CMD_EXPORT_ALIAS(webclient_shard_download_test, web_shard_test, webclient head and get request test); 144 | #endif /* FINSH_USING_MSH */ 145 | -------------------------------------------------------------------------------- /src/webclient.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2013-05-05 Bernard the first version 9 | * 2013-06-10 Bernard fix the slow speed issue when download file. 10 | * 2015-11-14 aozima add content_length_remainder. 11 | * 2017-12-23 aozima update gethostbyname to getaddrinfo. 12 | * 2018-01-04 aozima add ipv6 address support. 13 | * 2018-07-26 chenyong modify log information 14 | * 2018-08-07 chenyong modify header processing 15 | * 2021-06-09 xiangxistu add shard download function 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | /* support both enable and disable "SAL_USING_POSIX" */ 29 | #if defined(RT_USING_SAL) 30 | #include 31 | #include 32 | #else 33 | #include 34 | #include 35 | #endif /* RT_USING_SAL */ 36 | 37 | #define DBG_ENABLE 38 | #define DBG_SECTION_NAME "web" 39 | #ifdef WEBCLIENT_DEBUG 40 | #define DBG_LEVEL DBG_LOG 41 | #else 42 | #define DBG_LEVEL DBG_INFO 43 | #endif /* WEBCLIENT_DEBUG */ 44 | #define DBG_COLOR 45 | #include 46 | 47 | /* default receive or send timeout */ 48 | #define WEBCLIENT_DEFAULT_TIMEO 6 49 | 50 | extern long int strtol(const char *nptr, char **endptr, int base); 51 | 52 | static int webclient_strncasecmp(const char *a, const char *b, size_t n) 53 | { 54 | uint8_t c1, c2; 55 | if (n <= 0) 56 | return 0; 57 | do { 58 | c1 = tolower(*a++); 59 | c2 = tolower(*b++); 60 | } while (--n && c1 && c1 == c2); 61 | return c1 - c2; 62 | } 63 | 64 | static const char *webclient_strstri(const char* str, const char* subStr) 65 | { 66 | int len = strlen(subStr); 67 | 68 | if(len == 0) 69 | { 70 | return RT_NULL; 71 | } 72 | 73 | while(*str) 74 | { 75 | if(webclient_strncasecmp(str, subStr, len) == 0) 76 | { 77 | return str; 78 | } 79 | ++str; 80 | } 81 | return RT_NULL; 82 | } 83 | 84 | static int webclient_send(struct webclient_session* session, const void *buffer, size_t len, int flag) 85 | { 86 | #ifdef WEBCLIENT_USING_MBED_TLS 87 | if (session->tls_session) 88 | { 89 | return mbedtls_client_write(session->tls_session, buffer, len); 90 | } 91 | #endif 92 | 93 | return send(session->socket, buffer, len, flag); 94 | } 95 | 96 | static int webclient_recv(struct webclient_session* session, void *buffer, size_t len, int flag) 97 | { 98 | #ifdef WEBCLIENT_USING_MBED_TLS 99 | if (session->tls_session) 100 | { 101 | return mbedtls_client_read(session->tls_session, buffer, len); 102 | } 103 | #endif 104 | 105 | return recv(session->socket, buffer, len, flag); 106 | } 107 | 108 | static int webclient_read_line(struct webclient_session *session, char *buffer, int size) 109 | { 110 | int rc, count = 0; 111 | char ch = 0, last_ch = 0; 112 | 113 | RT_ASSERT(session); 114 | RT_ASSERT(buffer); 115 | 116 | /* Keep reading until we fill the buffer. */ 117 | while (count < size) 118 | { 119 | rc = webclient_recv(session, (unsigned char *) &ch, 1, 0); 120 | #if defined(WEBCLIENT_USING_MBED_TLS) || defined(WEBCLIENT_USING_SAL_TLS) 121 | if (session->is_tls && (rc == MBEDTLS_ERR_SSL_WANT_READ || rc == MBEDTLS_ERR_SSL_WANT_WRITE)) 122 | { 123 | continue; 124 | } 125 | #endif 126 | if (rc <= 0) 127 | return rc; 128 | 129 | if (ch == '\n' && last_ch == '\r') 130 | break; 131 | 132 | buffer[count++] = ch; 133 | 134 | last_ch = ch; 135 | } 136 | 137 | if (count > size) 138 | { 139 | LOG_E("read line failed. The line data length is out of buffer size(%d)!", count); 140 | return -WEBCLIENT_ERROR; 141 | } 142 | 143 | return count; 144 | } 145 | 146 | /** 147 | * resolve server address 148 | * 149 | * @param session http session 150 | * @param res the server address information 151 | * @param url the input server URI address 152 | * @param request the pointer to point the request url, for example, /index.html 153 | * 154 | * @return 0 on resolve server address OK, others failed 155 | * 156 | * URL example: 157 | * http://www.rt-thread.org 158 | * http://www.rt-thread.org:80 159 | * https://www.rt-thread.org/ 160 | * http://192.168.1.1:80/index.htm 161 | * http://[fe80::1] 162 | * http://[fe80::1]/ 163 | * http://[fe80::1]/index.html 164 | * http://[fe80::1]:80/index.html 165 | */ 166 | static int webclient_resolve_address(struct webclient_session *session, struct addrinfo **res, 167 | const char *url, const char **request) 168 | { 169 | int rc = WEBCLIENT_OK; 170 | char *ptr; 171 | char port_str[6] = "80"; /* default port of 80(http) */ 172 | const char *port_ptr; 173 | const char *path_ptr; 174 | 175 | const char *host_addr = 0; 176 | int url_len, host_addr_len = 0; 177 | 178 | RT_ASSERT(res); 179 | RT_ASSERT(request); 180 | 181 | /* make sure *res = NULL before getaddrinfo */ 182 | *res = RT_NULL; 183 | url_len = strlen(url); 184 | 185 | /* strip protocol(http or https) */ 186 | if (strncmp(url, "http://", 7) == 0) 187 | { 188 | host_addr = url + 7; 189 | } 190 | else if (strncmp(url, "https://", 8) == 0) 191 | { 192 | strncpy(port_str, "443", 4); 193 | host_addr = url + 8; 194 | } 195 | else 196 | { 197 | rc = -WEBCLIENT_ERROR; 198 | goto __exit; 199 | } 200 | 201 | /* ipv6 address */ 202 | if (host_addr[0] == '[') 203 | { 204 | host_addr += 1; 205 | ptr = strstr(host_addr, "]"); 206 | if (!ptr) 207 | { 208 | rc = -WEBCLIENT_ERROR; 209 | goto __exit; 210 | } 211 | host_addr_len = ptr - host_addr; 212 | } 213 | 214 | path_ptr = strstr(host_addr, "/"); 215 | *request = path_ptr ? path_ptr : "/"; 216 | 217 | /* resolve port */ 218 | port_ptr = strstr(host_addr + host_addr_len, ":"); 219 | if (port_ptr && (!path_ptr || (port_ptr < path_ptr))) 220 | { 221 | if (!path_ptr) 222 | { 223 | strcpy(port_str, port_ptr + 1); 224 | } 225 | else 226 | { 227 | int port_len = path_ptr - port_ptr - 1; 228 | strncpy(port_str, port_ptr + 1, port_len); 229 | port_str[port_len] = '\0'; 230 | } 231 | } 232 | else 233 | { 234 | port_ptr = RT_NULL; 235 | } 236 | 237 | if (port_ptr && (!path_ptr)) 238 | { 239 | strcpy(port_str, port_ptr + 1); 240 | } 241 | 242 | /* ipv4 or domain. */ 243 | if (!host_addr_len) 244 | { 245 | if (port_ptr) 246 | { 247 | host_addr_len = port_ptr - host_addr; 248 | } 249 | else if (path_ptr) 250 | { 251 | host_addr_len = path_ptr - host_addr; 252 | } 253 | else 254 | { 255 | host_addr_len = strlen(host_addr); 256 | } 257 | } 258 | 259 | if ((host_addr_len < 1) || (host_addr_len > url_len)) 260 | { 261 | rc = -WEBCLIENT_ERROR; 262 | goto __exit; 263 | } 264 | 265 | /* get host address ok. */ 266 | { 267 | char *host_addr_new = web_malloc(host_addr_len + 1); 268 | 269 | if (!host_addr_new) 270 | { 271 | rc = -WEBCLIENT_ERROR; 272 | goto __exit; 273 | } 274 | 275 | rt_memcpy(host_addr_new, host_addr, host_addr_len); 276 | host_addr_new[host_addr_len] = '\0'; 277 | session->host = host_addr_new; 278 | } 279 | 280 | LOG_D("host address: %s , port: %s", session->host, port_str); 281 | 282 | #ifdef WEBCLIENT_USING_MBED_TLS 283 | if (session->tls_session) 284 | { 285 | session->tls_session->port = web_strdup(port_str); 286 | session->tls_session->host = web_strdup(session->host); 287 | if (session->tls_session->port == RT_NULL || session->tls_session->host == RT_NULL) 288 | { 289 | return -WEBCLIENT_NOMEM; 290 | } 291 | 292 | return rc; 293 | } 294 | #endif 295 | 296 | /* resolve the host name. */ 297 | { 298 | struct addrinfo hint; 299 | int ret; 300 | 301 | web_memset(&hint, 0, sizeof(hint)); 302 | ret = getaddrinfo(session->host, port_str, &hint, res); 303 | if (ret != 0) 304 | { 305 | LOG_E("getaddrinfo err: %d '%s'.", ret, session->host); 306 | rc = -WEBCLIENT_ERROR; 307 | goto __exit; 308 | } 309 | } 310 | 311 | __exit: 312 | if (rc != WEBCLIENT_OK) 313 | { 314 | if (session->host) 315 | { 316 | web_free(session->host); 317 | session->host = RT_NULL; 318 | } 319 | 320 | if (*res) 321 | { 322 | freeaddrinfo(*res); 323 | *res = RT_NULL; 324 | } 325 | } 326 | 327 | return rc; 328 | } 329 | 330 | #ifdef WEBCLIENT_USING_MBED_TLS 331 | /** 332 | * create and initialize https session. 333 | * 334 | * @param session webclient session 335 | * @param URI input server URI address 336 | * 337 | * @return <0: create failed, no memory or other errors 338 | * =0: success 339 | */ 340 | static int webclient_open_tls(struct webclient_session *session, const char *URI) 341 | { 342 | int tls_ret = 0; 343 | const char *pers = "webclient"; 344 | 345 | RT_ASSERT(session); 346 | 347 | session->tls_session = (MbedTLSSession *) web_calloc(1, sizeof(MbedTLSSession)); 348 | if (session->tls_session == RT_NULL) 349 | { 350 | return -WEBCLIENT_NOMEM; 351 | } 352 | 353 | session->tls_session->buffer_len = WEBCLIENT_RESPONSE_BUFSZ; 354 | session->tls_session->buffer = web_malloc(session->tls_session->buffer_len); 355 | if(session->tls_session->buffer == RT_NULL) 356 | { 357 | LOG_E("no memory for tls_session buffer!"); 358 | return -WEBCLIENT_ERROR; 359 | } 360 | 361 | if((tls_ret = mbedtls_client_init(session->tls_session, (void *)pers, strlen(pers))) < 0) 362 | { 363 | LOG_E("initialize https client failed return: -0x%x.", -tls_ret); 364 | return -WEBCLIENT_ERROR; 365 | } 366 | 367 | return WEBCLIENT_OK; 368 | } 369 | #endif 370 | 371 | /** 372 | * connect to http server. 373 | * 374 | * @param session webclient session 375 | * @param URI the input server URI address 376 | * 377 | * @return <0: connect failed or other error 378 | * =0: connect success 379 | */ 380 | static int webclient_connect(struct webclient_session *session, const char *URI) 381 | { 382 | int rc = WEBCLIENT_OK; 383 | int socket_handle; 384 | struct timeval timeout; 385 | struct addrinfo *res = RT_NULL; 386 | const char *req_url; 387 | 388 | RT_ASSERT(session); 389 | RT_ASSERT(URI); 390 | 391 | timeout.tv_sec = WEBCLIENT_DEFAULT_TIMEO; 392 | timeout.tv_usec = 0; 393 | 394 | if (strncmp(URI, "https://", 8) == 0) 395 | { 396 | #if defined(WEBCLIENT_USING_SAL_TLS) 397 | session->is_tls = RT_TRUE; 398 | #elif defined(WEBCLIENT_USING_MBED_TLS) 399 | if(webclient_open_tls(session, URI) < 0) 400 | { 401 | LOG_E("connect failed, https client open URI(%s) failed!", URI); 402 | return -WEBCLIENT_ERROR; 403 | } 404 | session->is_tls = RT_TRUE; 405 | #else 406 | LOG_E("not support https connect, please enable webclient https configure!"); 407 | rc = -WEBCLIENT_ERROR; 408 | goto __exit; 409 | #endif 410 | } 411 | 412 | /* Check valid IP address and URL */ 413 | rc = webclient_resolve_address(session, &res, URI, &req_url); 414 | if (rc != WEBCLIENT_OK) 415 | { 416 | LOG_E("connect failed, resolve address error(%d).", rc); 417 | goto __exit; 418 | } 419 | 420 | /* Not use 'getaddrinfo()' for https connection */ 421 | if (session->is_tls == RT_FALSE && res == RT_NULL) 422 | { 423 | rc = -WEBCLIENT_ERROR; 424 | goto __exit; 425 | } 426 | 427 | /* copy host address */ 428 | if (req_url) 429 | { 430 | session->req_url = web_strdup(req_url); 431 | } 432 | else 433 | { 434 | LOG_E("connect failed, resolve request address error."); 435 | rc = -WEBCLIENT_ERROR; 436 | goto __exit; 437 | } 438 | 439 | #ifdef WEBCLIENT_USING_MBED_TLS 440 | if (session->tls_session) 441 | { 442 | int tls_ret = 0; 443 | 444 | if ((tls_ret = mbedtls_client_context(session->tls_session)) < 0) 445 | { 446 | LOG_E("connect failed, https client context return: -0x%x", -tls_ret); 447 | return -WEBCLIENT_ERROR; 448 | } 449 | 450 | if ((tls_ret = mbedtls_client_connect(session->tls_session)) < 0) 451 | { 452 | LOG_E("connect failed, https client connect return: -0x%x", -tls_ret); 453 | return -WEBCLIENT_CONNECT_FAILED; 454 | } 455 | 456 | socket_handle = session->tls_session->server_fd.fd; 457 | 458 | /* set recv timeout option */ 459 | setsockopt(socket_handle, SOL_SOCKET, SO_RCVTIMEO, (void*) &timeout, 460 | sizeof(timeout)); 461 | setsockopt(socket_handle, SOL_SOCKET, SO_SNDTIMEO, (void*) &timeout, 462 | sizeof(timeout)); 463 | 464 | session->socket = socket_handle; 465 | 466 | return WEBCLIENT_OK; 467 | } 468 | #endif 469 | 470 | { 471 | #ifdef WEBCLIENT_USING_SAL_TLS 472 | if (session->is_tls) 473 | { 474 | socket_handle = socket(res->ai_family, SOCK_STREAM, PROTOCOL_TLS); 475 | } 476 | else 477 | { 478 | socket_handle = socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP); 479 | } 480 | #else 481 | socket_handle = socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP); 482 | #endif 483 | 484 | if (socket_handle < 0) 485 | { 486 | LOG_E("connect failed, create socket(%d) error.", socket_handle); 487 | rc = -WEBCLIENT_NOSOCKET; 488 | goto __exit; 489 | } 490 | 491 | /* set receive and send timeout option */ 492 | setsockopt(socket_handle, SOL_SOCKET, SO_RCVTIMEO, (void *) &timeout, 493 | sizeof(timeout)); 494 | setsockopt(socket_handle, SOL_SOCKET, SO_SNDTIMEO, (void *) &timeout, 495 | sizeof(timeout)); 496 | 497 | if (connect(socket_handle, res->ai_addr, res->ai_addrlen) != 0) 498 | { 499 | /* connect failed, close socket */ 500 | LOG_E("connect failed, connect socket(%d) error.", socket_handle); 501 | closesocket(socket_handle); 502 | rc = -WEBCLIENT_CONNECT_FAILED; 503 | goto __exit; 504 | } 505 | 506 | session->socket = socket_handle; 507 | } 508 | 509 | __exit: 510 | if (res) 511 | { 512 | freeaddrinfo(res); 513 | } 514 | 515 | return rc; 516 | } 517 | 518 | /** 519 | * add fields data to request header data. 520 | * 521 | * @param session webclient session 522 | * @param fmt fields format 523 | * 524 | * @return >0: data length of successfully added 525 | * <0: not enough header buffer size 526 | */ 527 | int webclient_header_fields_add(struct webclient_session *session, const char *fmt, ...) 528 | { 529 | rt_int32_t length; 530 | va_list args; 531 | 532 | RT_ASSERT(session); 533 | RT_ASSERT(session->header->buffer); 534 | 535 | va_start(args, fmt); 536 | length = web_vsnprintf(session->header->buffer + session->header->length, 537 | session->header->size - session->header->length, fmt, args); 538 | va_end(args); 539 | if (length < 0) 540 | { 541 | LOG_E("add fields header data failed, return length(%d) error.", length); 542 | return -WEBCLIENT_ERROR; 543 | } 544 | 545 | session->header->length += length; 546 | 547 | /* check header size */ 548 | if (session->header->length >= session->header->size) 549 | { 550 | LOG_E("not enough header buffer size(%d)!", session->header->size); 551 | return -WEBCLIENT_ERROR; 552 | } 553 | 554 | return length; 555 | } 556 | 557 | /** 558 | * get fields information from request/response header data. 559 | * 560 | * @param session webclient session 561 | * @param fields fields keyword 562 | * 563 | * @return = NULL: get fields data failed 564 | * != NULL: success get fields data 565 | */ 566 | const char *webclient_header_fields_get(struct webclient_session *session, const char *fields) 567 | { 568 | char *resp_buf = RT_NULL; 569 | size_t resp_buf_len = 0; 570 | 571 | RT_ASSERT(session); 572 | RT_ASSERT(session->header->buffer); 573 | 574 | resp_buf = session->header->buffer; 575 | while (resp_buf_len < session->header->length) 576 | { 577 | if (webclient_strstri(resp_buf, fields) == resp_buf) 578 | { 579 | char *mime_ptr = RT_NULL; 580 | 581 | /* jump space */ 582 | mime_ptr = strstr(resp_buf, ":"); 583 | if (mime_ptr != NULL) 584 | { 585 | mime_ptr += 1; 586 | 587 | while (*mime_ptr && (*mime_ptr == ' ' || *mime_ptr == '\t')) 588 | mime_ptr++; 589 | 590 | return mime_ptr; 591 | } 592 | } 593 | 594 | if (*resp_buf == '\0') 595 | break; 596 | 597 | resp_buf += strlen(resp_buf) + 1; 598 | resp_buf_len += strlen(resp_buf) + 1; 599 | } 600 | 601 | return RT_NULL; 602 | } 603 | 604 | /** 605 | * get http response status code. 606 | * 607 | * @param session webclient session 608 | * 609 | * @return response status code 610 | */ 611 | int webclient_resp_status_get(struct webclient_session *session) 612 | { 613 | RT_ASSERT(session); 614 | 615 | return session->resp_status; 616 | } 617 | 618 | /** 619 | * get http response data content length. 620 | * 621 | * @param session webclient session 622 | * 623 | * @return response content length 624 | */ 625 | int webclient_content_length_get(struct webclient_session *session) 626 | { 627 | RT_ASSERT(session); 628 | 629 | return session->content_length; 630 | } 631 | 632 | static int webclient_send_header(struct webclient_session *session, int method) 633 | { 634 | int rc = WEBCLIENT_OK; 635 | char *header = RT_NULL; 636 | 637 | RT_ASSERT(session); 638 | 639 | header = session->header->buffer; 640 | 641 | if (session->header->length == 0 && method <= WEBCLIENT_GET) 642 | { 643 | /* use default header data */ 644 | if (webclient_header_fields_add(session, "GET %s HTTP/1.1\r\n", session->req_url) < 0) 645 | return -WEBCLIENT_NOMEM; 646 | if (webclient_header_fields_add(session, "Host: %s\r\n", session->host) < 0) 647 | return -WEBCLIENT_NOMEM; 648 | if (webclient_header_fields_add(session, "User-Agent: RT-Thread HTTP Agent\r\n\r\n") < 0) 649 | return -WEBCLIENT_NOMEM; 650 | 651 | webclient_write(session, (unsigned char *) session->header->buffer, session->header->length); 652 | } 653 | else 654 | { 655 | if (method != WEBCLIENT_USER_METHOD) 656 | { 657 | /* check and add fields header data */ 658 | if (web_memcmp(header, "HTTP/1.", strlen("HTTP/1."))) 659 | { 660 | char *header_buffer = RT_NULL; 661 | int length = 0; 662 | 663 | header_buffer = web_strdup(session->header->buffer); 664 | if (header_buffer == RT_NULL) 665 | { 666 | LOG_E("no memory for header buffer!"); 667 | rc = -WEBCLIENT_NOMEM; 668 | goto __exit; 669 | } 670 | 671 | /* splice http request header data */ 672 | if (method == WEBCLIENT_GET) 673 | length = web_snprintf(session->header->buffer, session->header->size, "GET %s HTTP/1.1\r\n%s", 674 | session->req_url ? session->req_url : "/", header_buffer); 675 | else if (method == WEBCLIENT_POST) 676 | length = web_snprintf(session->header->buffer, session->header->size, "POST %s HTTP/1.1\r\n%s", 677 | session->req_url ? session->req_url : "/", header_buffer); 678 | else if (method == WEBCLIENT_HEAD) 679 | length = web_snprintf(session->header->buffer, session->header->size, "HEAD %s HTTP/1.1\r\n%s", 680 | session->req_url ? session->req_url : "/", header_buffer); 681 | session->header->length = length; 682 | 683 | web_free(header_buffer); 684 | } 685 | 686 | if (strstr(header, "Host:") == RT_NULL) 687 | { 688 | if (webclient_header_fields_add(session, "Host: %s\r\n", session->host) < 0) 689 | return -WEBCLIENT_NOMEM; 690 | } 691 | 692 | if (strstr(header, "User-Agent:") == RT_NULL) 693 | { 694 | if (webclient_header_fields_add(session, "User-Agent: RT-Thread HTTP Agent\r\n") < 0) 695 | return -WEBCLIENT_NOMEM; 696 | } 697 | 698 | if (strstr(header, "Accept:") == RT_NULL) 699 | { 700 | if (webclient_header_fields_add(session, "Accept: */*\r\n") < 0) 701 | return -WEBCLIENT_NOMEM; 702 | } 703 | 704 | /* header data end */ 705 | web_snprintf(session->header->buffer + session->header->length, session->header->size - session->header->length, "\r\n"); 706 | session->header->length += 2; 707 | 708 | /* check header size */ 709 | if (session->header->length > session->header->size) 710 | { 711 | LOG_E("send header failed, not enough header buffer size(%d)!", session->header->size); 712 | rc = -WEBCLIENT_NOBUFFER; 713 | goto __exit; 714 | } 715 | 716 | webclient_write(session, (unsigned char *) session->header->buffer, session->header->length); 717 | } 718 | else 719 | { 720 | webclient_write(session, (unsigned char *) session->header->buffer, session->header->length); 721 | } 722 | } 723 | 724 | /* get and echo request header data */ 725 | { 726 | char *header_str, *header_ptr; 727 | int header_line_len; 728 | LOG_D("request header:"); 729 | 730 | for(header_str = session->header->buffer; (header_ptr = strstr(header_str, "\r\n")) != RT_NULL; ) 731 | { 732 | header_line_len = header_ptr - header_str; 733 | 734 | if (header_line_len > 0) 735 | { 736 | LOG_D("%.*s", header_line_len, header_str); 737 | } 738 | header_str = header_ptr + strlen("\r\n"); 739 | } 740 | #ifdef WEBCLIENT_DEBUG 741 | LOG_RAW("\n"); 742 | #endif 743 | } 744 | 745 | __exit: 746 | return rc; 747 | } 748 | 749 | /** 750 | * resolve server response data. 751 | * 752 | * @param session webclient session 753 | * 754 | * @return <0: resolve response data failed 755 | * =0: success 756 | */ 757 | int webclient_handle_response(struct webclient_session *session) 758 | { 759 | int rc = WEBCLIENT_OK; 760 | char *mime_buffer = RT_NULL; 761 | char *mime_ptr = RT_NULL; 762 | const char *transfer_encoding; 763 | int i; 764 | 765 | RT_ASSERT(session); 766 | 767 | /* clean header buffer and size */ 768 | web_memset(session->header->buffer, 0x00, session->header->size); 769 | session->header->length = 0; 770 | 771 | LOG_D("response header:"); 772 | /* We now need to read the header information */ 773 | while (1) 774 | { 775 | mime_buffer = session->header->buffer + session->header->length; 776 | 777 | /* read a line from the header information. */ 778 | rc = webclient_read_line(session, mime_buffer, session->header->size - session->header->length); 779 | if (rc < 0) 780 | break; 781 | 782 | /* End of headers is a blank line. exit. */ 783 | if (rc == 0) 784 | break; 785 | if ((rc == 1) && (mime_buffer[0] == '\r')) 786 | { 787 | mime_buffer[0] = '\0'; 788 | break; 789 | } 790 | 791 | /* set terminal charater */ 792 | mime_buffer[rc - 1] = '\0'; 793 | 794 | /* echo response header data */ 795 | LOG_D("%s", mime_buffer); 796 | 797 | session->header->length += rc; 798 | 799 | if (session->header->length >= session->header->size) 800 | { 801 | LOG_E("not enough header buffer size(%d)!", session->header->size); 802 | return -WEBCLIENT_NOMEM; 803 | } 804 | } 805 | 806 | /* get HTTP status code */ 807 | mime_ptr = web_strdup(session->header->buffer); 808 | if (mime_ptr == RT_NULL) 809 | { 810 | LOG_E("no memory for get http status code buffer!"); 811 | return -WEBCLIENT_NOMEM; 812 | } 813 | 814 | if (strstr(mime_ptr, "HTTP/1.")) 815 | { 816 | char *ptr = mime_ptr; 817 | 818 | ptr += strlen("HTTP/1.x"); 819 | 820 | while (*ptr && (*ptr == ' ' || *ptr == '\t')) 821 | ptr++; 822 | 823 | /* Terminate string after status code */ 824 | for (i = 0; ((ptr[i] != ' ') && (ptr[i] != '\t')); i++); 825 | ptr[i] = '\0'; 826 | 827 | session->resp_status = (int) strtol(ptr, RT_NULL, 10); 828 | } 829 | 830 | /* get content length */ 831 | if (webclient_header_fields_get(session, "Content-Length") != RT_NULL) 832 | { 833 | session->content_length = atoi(webclient_header_fields_get(session, "Content-Length")); 834 | } 835 | session->content_remainder = session->content_length ? (size_t) session->content_length : 0xFFFFFFFF; 836 | 837 | transfer_encoding = webclient_header_fields_get(session, "Transfer-Encoding"); 838 | if (transfer_encoding && strcmp(transfer_encoding, "chunked") == 0) 839 | { 840 | rt_uint16_t len = session->header->size; 841 | char *line = rt_malloc(len); 842 | /* chunk mode, we should get the first chunk size */ 843 | webclient_read_line(session, line, len); 844 | session->chunk_sz = strtol(line, RT_NULL, 16); 845 | session->chunk_offset = 0; 846 | rt_free(line); 847 | } 848 | 849 | if (mime_ptr) 850 | { 851 | web_free(mime_ptr); 852 | } 853 | 854 | if (rc < 0) 855 | { 856 | return rc; 857 | } 858 | 859 | return session->resp_status; 860 | } 861 | 862 | /** 863 | * create webclient session, set maximum header and response size 864 | * 865 | * @param header_sz maximum send header size 866 | * @param resp_sz maximum response data size 867 | * 868 | * @return webclient session structure 869 | */ 870 | struct webclient_session *webclient_session_create(size_t header_sz) 871 | { 872 | struct webclient_session *session; 873 | 874 | /* create session */ 875 | session = (struct webclient_session *) web_calloc(1, sizeof(struct webclient_session)); 876 | if (session == RT_NULL) 877 | { 878 | LOG_E("webclient create failed, no memory for webclient session!"); 879 | return RT_NULL; 880 | } 881 | 882 | /* initialize the socket of session */ 883 | session->socket = -1; 884 | session->content_length = -1; 885 | 886 | session->header = (struct webclient_header *) web_calloc(1, sizeof(struct webclient_header)); 887 | if (session->header == RT_NULL) 888 | { 889 | LOG_E("webclient create failed, no memory for session header!"); 890 | web_free(session); 891 | session = RT_NULL; 892 | return RT_NULL; 893 | } 894 | 895 | session->header->size = header_sz; 896 | session->header->buffer = (char *) web_calloc(1, header_sz); 897 | if (session->header->buffer == RT_NULL) 898 | { 899 | LOG_E("webclient create failed, no memory for session header buffer!"); 900 | web_free(session->header); 901 | web_free(session); 902 | session = RT_NULL; 903 | return RT_NULL; 904 | } 905 | 906 | return session; 907 | } 908 | 909 | static int webclient_clean(struct webclient_session *session); 910 | 911 | /** 912 | * send GET request to http server and get response header. 913 | * 914 | * @param session webclient session 915 | * @param URI input server URI address 916 | * @param header GET request header 917 | * = NULL: use default header data 918 | * != NULL: use custom header data 919 | * 920 | * @return <0: send GET request failed 921 | * >0: response http status code 922 | */ 923 | int webclient_get(struct webclient_session *session, const char *URI) 924 | { 925 | int rc = WEBCLIENT_OK; 926 | int resp_status = 0; 927 | 928 | RT_ASSERT(session); 929 | RT_ASSERT(URI); 930 | 931 | rc = webclient_connect(session, URI); 932 | if (rc != WEBCLIENT_OK) 933 | { 934 | /* connect to webclient server failed. */ 935 | return rc; 936 | } 937 | 938 | rc = webclient_send_header(session, WEBCLIENT_GET); 939 | if (rc != WEBCLIENT_OK) 940 | { 941 | /* send header to webclient server failed. */ 942 | return rc; 943 | } 944 | 945 | /* handle the response header of webclient server */ 946 | resp_status = webclient_handle_response(session); 947 | 948 | LOG_D("get position handle response(%d).", resp_status); 949 | 950 | if (resp_status > 0) 951 | { 952 | const char *location = webclient_header_fields_get(session, "Location"); 953 | 954 | /* relocation */ 955 | if ((resp_status == 302 || resp_status == 301) && location) 956 | { 957 | char *new_url; 958 | 959 | new_url = web_strdup(location); 960 | if (new_url == RT_NULL) 961 | { 962 | return -WEBCLIENT_NOMEM; 963 | } 964 | 965 | /* clean webclient session */ 966 | webclient_clean(session); 967 | /* clean webclient session header */ 968 | session->header->length = 0; 969 | web_memset(session->header->buffer, 0, session->header->size); 970 | 971 | rc = webclient_get(session, new_url); 972 | 973 | web_free(new_url); 974 | return rc; 975 | } 976 | } 977 | 978 | return resp_status; 979 | } 980 | 981 | /** 982 | * register a handle function for http breakpoint resume and shard download. 983 | * 984 | * @param function 985 | * 986 | * @return the pointer 987 | */ 988 | int *webclient_register_shard_position_function(struct webclient_session *session, int (*handle_function)(char *buffer, int size)) 989 | { 990 | session->handle_function = handle_function; 991 | 992 | return (int *)session->handle_function; 993 | } 994 | 995 | /** 996 | * http breakpoint resume and shard download. 997 | * 998 | * @param session webclient session 999 | * @param URI input server URI address 1000 | * @param length the length of point 1001 | * 1002 | * @return <0: send GET request failed 1003 | * >0: response http status code 1004 | */ 1005 | int webclient_shard_head_function(struct webclient_session *session, const char *URI, int *length) 1006 | { 1007 | RT_ASSERT(session); 1008 | RT_ASSERT(URI); 1009 | 1010 | int rc = WEBCLIENT_OK; 1011 | int resp_status = 0; 1012 | 1013 | if(session->socket == -1) 1014 | { 1015 | rc = webclient_connect(session, URI); 1016 | if (rc != WEBCLIENT_OK) 1017 | { 1018 | return rc; 1019 | } 1020 | } 1021 | 1022 | /* clean header buffer and size */ 1023 | web_memset(session->header->buffer, 0x00, session->header->size); 1024 | session->header->length = 0; 1025 | 1026 | rc = webclient_send_header(session, WEBCLIENT_HEAD); 1027 | if (rc != WEBCLIENT_OK) 1028 | { 1029 | return rc; 1030 | } 1031 | 1032 | /* handle the response header of webclient server by HEAD request */ 1033 | resp_status = webclient_handle_response(session); 1034 | if(resp_status >= 0) 1035 | { 1036 | *length = webclient_content_length_get(session); 1037 | LOG_D("The length[%04d] of real data of URI.", *length); 1038 | } 1039 | 1040 | return rc; 1041 | } 1042 | 1043 | /** 1044 | * http breakpoint resume and shard download. 1045 | * 1046 | * @param session webclient session 1047 | * @param URI input server URI address 1048 | * @param start the position of you want to receive 1049 | * @param length the length of data length from "webclient_shard_head_function" 1050 | * @param mem_size the buffer size that you alloc 1051 | * 1052 | * @return <0: send GET request failed 1053 | * >0: response http status code 1054 | */ 1055 | int webclient_shard_position_function(struct webclient_session *session, const char *URI, int start, int length, int mem_size) 1056 | { 1057 | int rc = WEBCLIENT_OK; 1058 | int result = RT_EOK; 1059 | int resp_status = 0; 1060 | size_t resp_len = 0; 1061 | char *buffer = RT_NULL; 1062 | int start_position, end_position = 0; 1063 | int total_len = 0; 1064 | 1065 | RT_ASSERT(session); 1066 | RT_ASSERT(URI); 1067 | RT_ASSERT(mem_size); 1068 | 1069 | /* set the offset of "Range" and "total_len" */ 1070 | end_position = start; 1071 | total_len = start + length; 1072 | 1073 | for(start_position = end_position; start_position < total_len;) 1074 | { 1075 | if(session->socket == -1) 1076 | { 1077 | rc = webclient_connect(session, URI); 1078 | if (rc != WEBCLIENT_OK) 1079 | { 1080 | break; 1081 | } 1082 | } 1083 | 1084 | end_position = start_position + mem_size - 1; 1085 | if(end_position >= total_len) 1086 | { 1087 | end_position = total_len - 1; 1088 | } 1089 | 1090 | /* clean header buffer and size */; 1091 | session->header->length = 0; 1092 | web_memset(session->header->buffer, 0, session->header->size); 1093 | 1094 | /* splice header and send header */ 1095 | LOG_D("Range: [%04d -> %04d]", start_position, end_position); 1096 | webclient_header_fields_add(session, "Range: bytes=%d-%d\r\n", start_position, end_position); 1097 | rc = webclient_send_header(session, WEBCLIENT_GET); 1098 | if (rc != WEBCLIENT_OK) 1099 | { 1100 | break; 1101 | } 1102 | 1103 | /* handle the response header of webclient server */ 1104 | resp_status = webclient_handle_response(session); 1105 | LOG_D("get position handle response(%d).", resp_status); 1106 | if (resp_status < 0) 1107 | { 1108 | webclient_clean(session); 1109 | continue; 1110 | } 1111 | 1112 | const char *location = webclient_header_fields_get(session, "Location"); 1113 | if ((resp_status == 302 || resp_status == 301) && location) 1114 | { 1115 | char *new_url; 1116 | 1117 | new_url = web_strdup(location); 1118 | if (new_url == RT_NULL) 1119 | { 1120 | rc = -WEBCLIENT_NOMEM; 1121 | break; 1122 | } 1123 | 1124 | /* clean webclient session */ 1125 | webclient_clean(session); 1126 | rc = webclient_shard_position_function(session, new_url, start, length, mem_size); 1127 | 1128 | web_free(new_url); 1129 | break; 1130 | } 1131 | 1132 | if (resp_status != 206) 1133 | { 1134 | continue; 1135 | } 1136 | 1137 | /* receive the incoming data */ 1138 | int data_len = webclient_response(session, (void **)&buffer, &resp_len); 1139 | if(data_len <= 0) 1140 | { 1141 | webclient_clean(session); 1142 | continue; 1143 | } 1144 | 1145 | start_position += data_len; 1146 | result = session->handle_function(buffer, data_len); 1147 | if(result != RT_EOK) 1148 | { 1149 | rc = -WEBCLIENT_ERROR; 1150 | break; 1151 | } 1152 | } 1153 | 1154 | webclient_clean(session); 1155 | return rc; 1156 | } 1157 | 1158 | /** 1159 | * send POST request to server and get response header data. 1160 | * 1161 | * @param session webclient session 1162 | * @param URI input server URI address 1163 | * @param post_data data send to the server 1164 | * = NULL: just connect server and send header 1165 | * != NULL: send header and body data, resolve response data 1166 | * @param data_len the length of send data 1167 | * 1168 | * @return <0: send POST request failed 1169 | * =0: send POST header success 1170 | * >0: response http status code 1171 | */ 1172 | int webclient_post(struct webclient_session *session, const char *URI, const void *post_data, size_t data_len) 1173 | { 1174 | int rc = WEBCLIENT_OK; 1175 | int resp_status = 0; 1176 | 1177 | RT_ASSERT(session); 1178 | RT_ASSERT(URI); 1179 | 1180 | if ((post_data != RT_NULL) && (data_len == 0)) 1181 | { 1182 | LOG_E("input post data length failed"); 1183 | return -WEBCLIENT_ERROR; 1184 | } 1185 | 1186 | rc = webclient_connect(session, URI); 1187 | if (rc != WEBCLIENT_OK) 1188 | { 1189 | /* connect to webclient server failed. */ 1190 | return rc; 1191 | } 1192 | 1193 | rc = webclient_send_header(session, WEBCLIENT_POST); 1194 | if (rc != WEBCLIENT_OK) 1195 | { 1196 | /* send header to webclient server failed. */ 1197 | return rc; 1198 | } 1199 | 1200 | if (post_data && (data_len > 0)) 1201 | { 1202 | webclient_write(session, post_data, data_len); 1203 | 1204 | /* resolve response data, get http status code */ 1205 | resp_status = webclient_handle_response(session); 1206 | LOG_D("post handle response(%d).", resp_status); 1207 | } 1208 | 1209 | return resp_status; 1210 | } 1211 | 1212 | 1213 | /** 1214 | * set receive and send data timeout. 1215 | * 1216 | * @param session http session 1217 | * @param millisecond timeout millisecond 1218 | * 1219 | * @return 0: set timeout success 1220 | */ 1221 | int webclient_set_timeout(struct webclient_session *session, int millisecond) 1222 | { 1223 | struct timeval timeout; 1224 | int second = rt_tick_from_millisecond(millisecond) / 1000; 1225 | 1226 | RT_ASSERT(session); 1227 | 1228 | timeout.tv_sec = second; 1229 | timeout.tv_usec = 0; 1230 | 1231 | /* set recv timeout option */ 1232 | setsockopt(session->socket, SOL_SOCKET, SO_RCVTIMEO, 1233 | (void *) &timeout, sizeof(timeout)); 1234 | setsockopt(session->socket, SOL_SOCKET, SO_SNDTIMEO, 1235 | (void *) &timeout, sizeof(timeout)); 1236 | 1237 | return 0; 1238 | } 1239 | 1240 | static int webclient_next_chunk(struct webclient_session *session) 1241 | { 1242 | char line[64]; 1243 | int length; 1244 | 1245 | RT_ASSERT(session); 1246 | 1247 | web_memset(line, 0x00, sizeof(line)); 1248 | length = webclient_read_line(session, line, sizeof(line)); 1249 | if (length > 0) 1250 | { 1251 | if (strcmp(line, "\r") == 0) 1252 | { 1253 | length = webclient_read_line(session, line, sizeof(line)); 1254 | if (length <= 0) 1255 | { 1256 | closesocket(session->socket); 1257 | session->socket = -1; 1258 | return length; 1259 | } 1260 | } 1261 | } 1262 | else 1263 | { 1264 | closesocket(session->socket); 1265 | session->socket = -1; 1266 | 1267 | return length; 1268 | } 1269 | 1270 | session->chunk_sz = strtol(line, RT_NULL, 16); 1271 | session->chunk_offset = 0; 1272 | 1273 | if (session->chunk_sz == 0) 1274 | { 1275 | /* end of chunks */ 1276 | closesocket(session->socket); 1277 | session->socket = -1; 1278 | session->chunk_sz = -1; 1279 | } 1280 | 1281 | return session->chunk_sz; 1282 | } 1283 | 1284 | /** 1285 | * read data from http server. 1286 | * 1287 | * @param session http session 1288 | * @param buffer read buffer 1289 | * @param length the maximum of read buffer size 1290 | * 1291 | * @return <0: read data error 1292 | * =0: http server disconnect 1293 | * >0: successfully read data length 1294 | */ 1295 | int webclient_read(struct webclient_session *session, void *buffer, size_t length) 1296 | { 1297 | int bytes_read = 0; 1298 | int total_read = 0; 1299 | int left; 1300 | 1301 | RT_ASSERT(session); 1302 | 1303 | /* get next chunk size is zero, client is already closed, return zero */ 1304 | if (session->chunk_sz < 0) 1305 | { 1306 | return 0; 1307 | } 1308 | 1309 | if (session->socket < 0) 1310 | { 1311 | return -WEBCLIENT_DISCONNECT; 1312 | } 1313 | 1314 | if (length == 0) 1315 | { 1316 | return 0; 1317 | } 1318 | 1319 | /* which is transfered as chunk mode */ 1320 | if (session->chunk_sz) 1321 | { 1322 | if ((int) length > (session->chunk_sz - session->chunk_offset)) 1323 | { 1324 | length = session->chunk_sz - session->chunk_offset; 1325 | } 1326 | 1327 | bytes_read = webclient_recv(session, buffer, length, 0); 1328 | if (bytes_read <= 0) 1329 | { 1330 | if (errno == EWOULDBLOCK || errno == EAGAIN) 1331 | { 1332 | /* recv timeout */ 1333 | return -WEBCLIENT_TIMEOUT; 1334 | } 1335 | else 1336 | { 1337 | closesocket(session->socket); 1338 | session->socket = -1; 1339 | return 0; 1340 | } 1341 | } 1342 | 1343 | session->chunk_offset += bytes_read; 1344 | if (session->chunk_offset >= session->chunk_sz) 1345 | { 1346 | webclient_next_chunk(session); 1347 | } 1348 | 1349 | return bytes_read; 1350 | } 1351 | 1352 | if (session->content_length > 0) 1353 | { 1354 | if (length > session->content_remainder) 1355 | { 1356 | length = session->content_remainder; 1357 | } 1358 | 1359 | if (length == 0) 1360 | { 1361 | return 0; 1362 | } 1363 | } 1364 | 1365 | /* 1366 | * Read until: there is an error, we've read "size" bytes or the remote 1367 | * side has closed the connection. 1368 | */ 1369 | left = length; 1370 | do 1371 | { 1372 | bytes_read = webclient_recv(session, (void *)((char *)buffer + total_read), left, 0); 1373 | if (bytes_read <= 0) 1374 | { 1375 | #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS) 1376 | if(session->is_tls && 1377 | (bytes_read == MBEDTLS_ERR_SSL_WANT_READ || bytes_read == MBEDTLS_ERR_SSL_WANT_WRITE)) 1378 | { 1379 | continue; 1380 | } 1381 | 1382 | #endif 1383 | LOG_D("receive data error(%d).", bytes_read); 1384 | 1385 | if (total_read) 1386 | { 1387 | break; 1388 | } 1389 | else 1390 | { 1391 | if (errno == EWOULDBLOCK || errno == EAGAIN) 1392 | { 1393 | /* recv timeout */ 1394 | LOG_E("receive data timeout."); 1395 | return -WEBCLIENT_TIMEOUT; 1396 | } 1397 | else 1398 | { 1399 | closesocket(session->socket); 1400 | session->socket = -1; 1401 | return 0; 1402 | } 1403 | } 1404 | } 1405 | 1406 | left -= bytes_read; 1407 | total_read += bytes_read; 1408 | } 1409 | while (left); 1410 | 1411 | if (session->content_length > 0) 1412 | { 1413 | session->content_remainder -= total_read; 1414 | } 1415 | 1416 | return total_read; 1417 | } 1418 | 1419 | /** 1420 | * write data to http server. 1421 | * 1422 | * @param session http session 1423 | * @param buffer write buffer 1424 | * @param length write buffer size 1425 | * 1426 | * @return <0: write data error 1427 | * =0: http server disconnect 1428 | * >0: successfully write data length 1429 | */ 1430 | int webclient_write(struct webclient_session *session, const void *buffer, size_t length) 1431 | { 1432 | int bytes_write = 0; 1433 | int total_write = 0; 1434 | int left = length; 1435 | 1436 | RT_ASSERT(session); 1437 | 1438 | if (session->socket < 0) 1439 | { 1440 | return -WEBCLIENT_DISCONNECT; 1441 | } 1442 | 1443 | if (length == 0) 1444 | { 1445 | return 0; 1446 | } 1447 | 1448 | /* send all of data on the buffer. */ 1449 | do 1450 | { 1451 | bytes_write = webclient_send(session, (void *)((char *)buffer + total_write), left, 0); 1452 | if (bytes_write <= 0) 1453 | { 1454 | #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS) 1455 | if(session->is_tls && 1456 | (bytes_write == MBEDTLS_ERR_SSL_WANT_READ || bytes_write == MBEDTLS_ERR_SSL_WANT_WRITE)) 1457 | { 1458 | continue; 1459 | } 1460 | #endif 1461 | if (errno == EWOULDBLOCK || errno == EAGAIN) 1462 | { 1463 | /* send timeout */ 1464 | if (total_write) 1465 | { 1466 | return total_write; 1467 | } 1468 | continue; 1469 | /* TODO: whether return the TIMEOUT 1470 | * return -WEBCLIENT_TIMEOUT; */ 1471 | } 1472 | else 1473 | { 1474 | closesocket(session->socket); 1475 | session->socket = -1; 1476 | 1477 | if (total_write == 0) 1478 | { 1479 | return -WEBCLIENT_DISCONNECT; 1480 | } 1481 | break; 1482 | } 1483 | } 1484 | 1485 | left -= bytes_write; 1486 | total_write += bytes_write; 1487 | } 1488 | while (left); 1489 | 1490 | return total_write; 1491 | } 1492 | 1493 | /* close session socket, free host and request url */ 1494 | static int webclient_clean(struct webclient_session *session) 1495 | { 1496 | #ifdef WEBCLIENT_USING_MBED_TLS 1497 | if (session->tls_session) 1498 | { 1499 | mbedtls_client_close(session->tls_session); 1500 | } 1501 | else 1502 | { 1503 | if (session->socket >= 0) 1504 | { 1505 | closesocket(session->socket); 1506 | session->socket = -1; 1507 | } 1508 | } 1509 | #else 1510 | if (session->socket >= 0) 1511 | { 1512 | closesocket(session->socket); 1513 | session->socket = -1; 1514 | } 1515 | #endif 1516 | 1517 | if (session->host) 1518 | { 1519 | web_free(session->host); 1520 | session->host = RT_NULL; 1521 | } 1522 | 1523 | if (session->req_url) 1524 | { 1525 | web_free(session->req_url); 1526 | session->req_url = RT_NULL; 1527 | } 1528 | 1529 | session->content_length = -1; 1530 | 1531 | return 0; 1532 | } 1533 | 1534 | /** 1535 | * close a webclient client session. 1536 | * 1537 | * @param session http client session 1538 | * 1539 | * @return 0: close success 1540 | */ 1541 | int webclient_close(struct webclient_session *session) 1542 | { 1543 | RT_ASSERT(session); 1544 | 1545 | webclient_clean(session); 1546 | 1547 | if (session->header && session->header->buffer) 1548 | { 1549 | web_free(session->header->buffer); 1550 | } 1551 | 1552 | if (session->header) 1553 | { 1554 | web_free(session->header); 1555 | } 1556 | 1557 | if (session) 1558 | { 1559 | web_free(session); 1560 | session = RT_NULL; 1561 | } 1562 | 1563 | return 0; 1564 | } 1565 | 1566 | /** 1567 | * get wenclient request response data. 1568 | * 1569 | * @param session wenclient session 1570 | * @param response response buffer address 1571 | * @param resp_len response buffer length 1572 | * 1573 | * @return response data size 1574 | */ 1575 | int webclient_response(struct webclient_session *session, void **response, size_t *resp_len) 1576 | { 1577 | unsigned char *buf_ptr; 1578 | unsigned char *response_buf = 0; 1579 | int length, total_read = 0; 1580 | 1581 | RT_ASSERT(session); 1582 | RT_ASSERT(response); 1583 | 1584 | /* initialize response */ 1585 | *response = RT_NULL; 1586 | 1587 | /* not content length field kind */ 1588 | if (session->content_length < 0) 1589 | { 1590 | size_t result_sz; 1591 | 1592 | total_read = 0; 1593 | while (1) 1594 | { 1595 | unsigned char *new_resp = RT_NULL; 1596 | 1597 | result_sz = total_read + WEBCLIENT_RESPONSE_BUFSZ; 1598 | new_resp = web_realloc(response_buf, result_sz + 1); 1599 | if (new_resp == RT_NULL) 1600 | { 1601 | LOG_E("no memory for realloc new response buffer!"); 1602 | break; 1603 | } 1604 | 1605 | response_buf = new_resp; 1606 | buf_ptr = (unsigned char *) response_buf + total_read; 1607 | 1608 | /* read result */ 1609 | length = webclient_read(session, buf_ptr, result_sz - total_read); 1610 | if (length <= 0) 1611 | break; 1612 | 1613 | total_read += length; 1614 | } 1615 | } 1616 | else 1617 | { 1618 | int result_sz; 1619 | 1620 | result_sz = session->content_length; 1621 | response_buf = web_calloc(1, result_sz + 1); 1622 | if (response_buf == RT_NULL) 1623 | { 1624 | return -WEBCLIENT_NOMEM; 1625 | } 1626 | 1627 | buf_ptr = (unsigned char *) response_buf; 1628 | for (total_read = 0; total_read < result_sz;) 1629 | { 1630 | length = webclient_read(session, buf_ptr, result_sz - total_read); 1631 | if (length <= 0) 1632 | break; 1633 | 1634 | buf_ptr += length; 1635 | total_read += length; 1636 | } 1637 | } 1638 | 1639 | if ((total_read == 0) && (response_buf != 0)) 1640 | { 1641 | web_free(response_buf); 1642 | response_buf = RT_NULL; 1643 | } 1644 | 1645 | if (response_buf) 1646 | { 1647 | *response = (void *)response_buf; 1648 | *(response_buf + total_read) = '\0'; 1649 | *resp_len = total_read; 1650 | } 1651 | 1652 | return total_read; 1653 | } 1654 | 1655 | /** 1656 | * add request(GET/POST) header data. 1657 | * 1658 | * @param request_header add request buffer address 1659 | * @param fmt fields format 1660 | * 1661 | * @return <=0: add header failed 1662 | * >0: add header data size 1663 | */ 1664 | 1665 | int webclient_request_header_add(char **request_header, const char *fmt, ...) 1666 | { 1667 | rt_int32_t length, header_length; 1668 | char *header; 1669 | va_list args; 1670 | 1671 | RT_ASSERT(request_header); 1672 | 1673 | if (*request_header == RT_NULL) 1674 | { 1675 | header = rt_calloc(1, WEBCLIENT_HEADER_BUFSZ); 1676 | if (header == RT_NULL) 1677 | { 1678 | LOG_E("No memory for webclient request header add."); 1679 | return RT_NULL; 1680 | } 1681 | *request_header = header; 1682 | } 1683 | else 1684 | { 1685 | header = *request_header; 1686 | } 1687 | 1688 | va_start(args, fmt); 1689 | header_length = strlen(header); 1690 | length = web_vsnprintf(header + header_length, WEBCLIENT_HEADER_BUFSZ - header_length, fmt, args); 1691 | if (length < 0) 1692 | { 1693 | LOG_E("add request header data failed, return length(%d) error.", length); 1694 | return -WEBCLIENT_ERROR; 1695 | } 1696 | va_end(args); 1697 | 1698 | /* check header size */ 1699 | if (strlen(header) >= WEBCLIENT_HEADER_BUFSZ) 1700 | { 1701 | LOG_E("not enough request header data size(%d)!", WEBCLIENT_HEADER_BUFSZ); 1702 | return -WEBCLIENT_ERROR; 1703 | } 1704 | 1705 | return length; 1706 | } 1707 | 1708 | /** 1709 | * send request(GET/POST) to server and get response data. 1710 | * 1711 | * @param URI input server address 1712 | * @param header send header data 1713 | * = NULL: use default header data, must be GET request 1714 | * != NULL: user custom header data, GET or POST request 1715 | * @param post_data data sent to the server 1716 | * = NULL: it is GET request 1717 | * != NULL: it is POST request 1718 | * @param data_len send data length 1719 | * @param response response buffer address 1720 | * @param resp_len response buffer length 1721 | * 1722 | * @return <0: request failed 1723 | * >=0: response buffer size 1724 | */ 1725 | int webclient_request(const char *URI, const char *header, const void *post_data, size_t data_len, void **response, size_t *resp_len) 1726 | { 1727 | struct webclient_session *session = RT_NULL; 1728 | int rc = WEBCLIENT_OK; 1729 | int totle_length = 0; 1730 | 1731 | RT_ASSERT(URI); 1732 | 1733 | if (post_data == RT_NULL && response == RT_NULL) 1734 | { 1735 | LOG_E("request get failed, get response data cannot be empty."); 1736 | return -WEBCLIENT_ERROR; 1737 | } 1738 | 1739 | if ((post_data != RT_NULL) && (data_len == 0)) 1740 | { 1741 | LOG_E("input post data length failed"); 1742 | return -WEBCLIENT_ERROR; 1743 | } 1744 | 1745 | if ((response != RT_NULL && resp_len == RT_NULL) || 1746 | (response == RT_NULL && resp_len != RT_NULL)) 1747 | { 1748 | LOG_E("input response data or length failed"); 1749 | return -WEBCLIENT_ERROR; 1750 | } 1751 | 1752 | if (post_data == RT_NULL) 1753 | { 1754 | /* send get request */ 1755 | session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ); 1756 | if (session == RT_NULL) 1757 | { 1758 | rc = -WEBCLIENT_NOMEM; 1759 | goto __exit; 1760 | } 1761 | 1762 | if (header != RT_NULL) 1763 | { 1764 | char *header_str, *header_ptr; 1765 | int header_line_length; 1766 | 1767 | for(header_str = (char *)header; (header_ptr = strstr(header_str, "\r\n")) != RT_NULL; ) 1768 | { 1769 | header_line_length = header_ptr + strlen("\r\n") - header_str; 1770 | webclient_header_fields_add(session, "%.*s", header_line_length, header_str); 1771 | header_str += header_line_length; 1772 | } 1773 | } 1774 | 1775 | if (webclient_get(session, URI) != 200) 1776 | { 1777 | rc = -WEBCLIENT_ERROR; 1778 | goto __exit; 1779 | } 1780 | 1781 | totle_length = webclient_response(session, response, resp_len); 1782 | if (totle_length <= 0) 1783 | { 1784 | rc = -WEBCLIENT_ERROR; 1785 | goto __exit; 1786 | } 1787 | } 1788 | else 1789 | { 1790 | /* send post request */ 1791 | session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ); 1792 | if (session == RT_NULL) 1793 | { 1794 | rc = -WEBCLIENT_NOMEM; 1795 | goto __exit; 1796 | } 1797 | 1798 | if (header != RT_NULL) 1799 | { 1800 | char *header_str, *header_ptr; 1801 | int header_line_length; 1802 | 1803 | for(header_str = (char *)header; (header_ptr = strstr(header_str, "\r\n")) != RT_NULL; ) 1804 | { 1805 | header_line_length = header_ptr + strlen("\r\n") - header_str; 1806 | webclient_header_fields_add(session, "%.*s", header_line_length, header_str); 1807 | header_str += header_line_length; 1808 | } 1809 | } 1810 | 1811 | if (strstr(session->header->buffer, "Content-Length") == RT_NULL) 1812 | { 1813 | webclient_header_fields_add(session, "Content-Length: %d\r\n", strlen(post_data)); 1814 | } 1815 | 1816 | if (strstr(session->header->buffer, "Content-Type") == RT_NULL) 1817 | { 1818 | webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n"); 1819 | } 1820 | 1821 | if (webclient_post(session, URI, post_data, data_len) != 200) 1822 | { 1823 | rc = -WEBCLIENT_ERROR; 1824 | goto __exit; 1825 | } 1826 | 1827 | totle_length = webclient_response(session, response, resp_len); 1828 | if (totle_length <= 0) 1829 | { 1830 | rc = -WEBCLIENT_ERROR; 1831 | goto __exit; 1832 | } 1833 | } 1834 | 1835 | __exit: 1836 | if (session) 1837 | { 1838 | webclient_close(session); 1839 | session = RT_NULL; 1840 | } 1841 | 1842 | if (rc < 0) 1843 | { 1844 | return rc; 1845 | } 1846 | 1847 | return totle_length; 1848 | } 1849 | -------------------------------------------------------------------------------- /src/webclient_file.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2017-07-26 chenyong modify log information 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | #ifdef RT_USING_DFS 20 | #include 21 | #include 22 | 23 | #define DBG_ENABLE 24 | #define DBG_SECTION_NAME "web.file" 25 | #ifdef WEBCLIENT_DEBUG 26 | #define DBG_LEVEL DBG_LOG 27 | #else 28 | #define DBG_LEVEL DBG_INFO 29 | #endif /* WEBCLIENT_DEBUG */ 30 | #define DBG_COLOR 31 | #include 32 | 33 | /** 34 | * send GET request and store response data into the file. 35 | * 36 | * @param URI input server address 37 | * @param filename store response date to filename 38 | * 39 | * @return <0: GET request failed 40 | * =0: success 41 | */ 42 | int webclient_get_file(const char* URI, const char* filename) 43 | { 44 | int fd = -1, rc = WEBCLIENT_OK; 45 | size_t offset; 46 | int length, total_length = 0; 47 | unsigned char *ptr = RT_NULL; 48 | struct webclient_session* session = RT_NULL; 49 | int resp_status = 0; 50 | 51 | session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ); 52 | if(session == RT_NULL) 53 | { 54 | rc = -WEBCLIENT_NOMEM; 55 | goto __exit; 56 | } 57 | 58 | if ((resp_status = webclient_get(session, URI)) != 200) 59 | { 60 | LOG_E("get file failed, wrong response: %d (-0x%X).", resp_status, resp_status); 61 | rc = -WEBCLIENT_ERROR; 62 | goto __exit; 63 | } 64 | 65 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0); 66 | if (fd < 0) 67 | { 68 | LOG_E("get file failed, open file(%s) error.", filename); 69 | rc = -WEBCLIENT_ERROR; 70 | goto __exit; 71 | } 72 | 73 | ptr = (unsigned char *) web_malloc(WEBCLIENT_RESPONSE_BUFSZ); 74 | if (ptr == RT_NULL) 75 | { 76 | LOG_E("get file failed, no memory for response buffer."); 77 | rc = -WEBCLIENT_NOMEM; 78 | goto __exit; 79 | } 80 | 81 | if (session->content_length < 0) 82 | { 83 | while (1) 84 | { 85 | length = webclient_read(session, ptr, WEBCLIENT_RESPONSE_BUFSZ); 86 | if (length > 0) 87 | { 88 | write(fd, ptr, length); 89 | total_length += length; 90 | LOG_RAW(">"); 91 | } 92 | else 93 | { 94 | break; 95 | } 96 | } 97 | } 98 | else 99 | { 100 | for (offset = 0; offset < (size_t) session->content_length;) 101 | { 102 | length = webclient_read(session, ptr, 103 | session->content_length - offset > WEBCLIENT_RESPONSE_BUFSZ ? 104 | WEBCLIENT_RESPONSE_BUFSZ : session->content_length - offset); 105 | 106 | if (length > 0) 107 | { 108 | write(fd, ptr, length); 109 | total_length += length; 110 | LOG_RAW(">"); 111 | } 112 | else 113 | { 114 | break; 115 | } 116 | 117 | offset += length; 118 | } 119 | } 120 | 121 | if (total_length) 122 | { 123 | LOG_D("save %d bytes.", total_length); 124 | } 125 | 126 | __exit: 127 | if (fd >= 0) 128 | { 129 | close(fd); 130 | } 131 | 132 | if (session != RT_NULL) 133 | { 134 | webclient_close(session); 135 | session = RT_NULL; 136 | } 137 | 138 | if (ptr != RT_NULL) 139 | { 140 | web_free(ptr); 141 | } 142 | 143 | return rc; 144 | } 145 | 146 | /** 147 | * post file to http server. 148 | * 149 | * @param URI input server address 150 | * @param filename post data filename 151 | * @param form_data form data 152 | * 153 | * @return <0: POST request failed 154 | * =0: success 155 | */ 156 | int webclient_post_file(const char* URI, const char* filename, 157 | const char* form_data) 158 | { 159 | size_t length; 160 | char boundary[60]; 161 | int fd = -1, rc = WEBCLIENT_OK; 162 | char *header = RT_NULL, *header_ptr; 163 | unsigned char *buffer = RT_NULL, *buffer_ptr; 164 | struct webclient_session* session = RT_NULL; 165 | int resp_data_len = 0; 166 | 167 | fd = open(filename, O_RDONLY, 0); 168 | if (fd < 0) 169 | { 170 | LOG_D("post file failed, open file(%s) error.", filename); 171 | rc = -WEBCLIENT_FILE_ERROR; 172 | goto __exit; 173 | } 174 | 175 | /* get the size of file */ 176 | length = lseek(fd, 0, SEEK_END); 177 | lseek(fd, 0, SEEK_SET); 178 | 179 | buffer = (unsigned char *) web_calloc(1, WEBCLIENT_RESPONSE_BUFSZ); 180 | if (buffer == RT_NULL) 181 | { 182 | LOG_D("post file failed, no memory for response buffer."); 183 | rc = -WEBCLIENT_NOMEM; 184 | goto __exit; 185 | } 186 | 187 | header = (char *) web_calloc(1, WEBCLIENT_HEADER_BUFSZ); 188 | if (header == RT_NULL) 189 | { 190 | LOG_D("post file failed, no memory for header buffer."); 191 | rc = -WEBCLIENT_NOMEM; 192 | goto __exit; 193 | } 194 | header_ptr = header; 195 | 196 | /* build boundary */ 197 | web_snprintf(boundary, sizeof(boundary), "----------------------------%012d", rt_tick_get()); 198 | 199 | /* build encapsulated mime_multipart information*/ 200 | buffer_ptr = buffer; 201 | /* first boundary */ 202 | buffer_ptr += web_snprintf((char*) buffer_ptr, 203 | WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer), "--%s\r\n", boundary); 204 | buffer_ptr += web_snprintf((char*) buffer_ptr, 205 | WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer), 206 | "Content-Disposition: form-data; %s\r\n", form_data); 207 | buffer_ptr += web_snprintf((char*) buffer_ptr, 208 | WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer), 209 | "Content-Type: application/octet-stream\r\n\r\n"); 210 | /* calculate content-length */ 211 | length += buffer_ptr - buffer; 212 | length += strlen(boundary) + 8; /* add the last boundary */ 213 | 214 | /* build header for upload */ 215 | header_ptr += web_snprintf(header_ptr, 216 | WEBCLIENT_HEADER_BUFSZ - (header_ptr - header), 217 | "Content-Length: %d\r\n", length); 218 | header_ptr += web_snprintf(header_ptr, 219 | WEBCLIENT_HEADER_BUFSZ - (header_ptr - header), 220 | "Content-Type: multipart/form-data; boundary=%s\r\n", boundary); 221 | 222 | session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ); 223 | if(session == RT_NULL) 224 | { 225 | rc = -WEBCLIENT_NOMEM; 226 | goto __exit; 227 | } 228 | 229 | strncpy(session->header->buffer, header, strlen(header)); 230 | session->header->length = strlen(session->header->buffer); 231 | 232 | rc = webclient_post(session, URI, NULL, 0); 233 | if(rc < 0) 234 | { 235 | goto __exit; 236 | } 237 | 238 | /* send mime_multipart */ 239 | webclient_write(session, buffer, buffer_ptr - buffer); 240 | 241 | /* send file data */ 242 | while (1) 243 | { 244 | length = read(fd, buffer, WEBCLIENT_RESPONSE_BUFSZ); 245 | if (length <= 0) 246 | { 247 | break; 248 | } 249 | 250 | webclient_write(session, buffer, length); 251 | } 252 | 253 | /* send last boundary */ 254 | web_snprintf((char*) buffer, WEBCLIENT_RESPONSE_BUFSZ, "\r\n--%s--\r\n", boundary); 255 | webclient_write(session, buffer, strlen(boundary) + 8); 256 | 257 | extern int webclient_handle_response(struct webclient_session *session); 258 | if( webclient_handle_response(session) != 200) 259 | { 260 | rc = -WEBCLIENT_ERROR; 261 | goto __exit; 262 | } 263 | 264 | resp_data_len = webclient_content_length_get(session); 265 | if (resp_data_len > 0) 266 | { 267 | int bytes_read = 0; 268 | 269 | web_memset(buffer, 0x00, WEBCLIENT_RESPONSE_BUFSZ); 270 | do 271 | { 272 | bytes_read = webclient_read(session, buffer, 273 | resp_data_len < WEBCLIENT_RESPONSE_BUFSZ ? resp_data_len : WEBCLIENT_RESPONSE_BUFSZ); 274 | if (bytes_read <= 0) 275 | { 276 | break; 277 | } 278 | resp_data_len -= bytes_read; 279 | } while(resp_data_len > 0); 280 | } 281 | 282 | __exit: 283 | if (fd >= 0) 284 | { 285 | close(fd); 286 | } 287 | 288 | if (session != RT_NULL) 289 | { 290 | webclient_close(session); 291 | session = RT_NULL; 292 | } 293 | 294 | if (buffer != RT_NULL) 295 | { 296 | web_free(buffer); 297 | } 298 | 299 | if (header != RT_NULL) 300 | { 301 | web_free(header); 302 | } 303 | 304 | return rc; 305 | } 306 | 307 | int wget(int argc, char** argv) 308 | { 309 | if (argc != 3) 310 | { 311 | rt_kprintf("Please using: wget \n"); 312 | return -1; 313 | } 314 | 315 | webclient_get_file(argv[1], argv[2]); 316 | return 0; 317 | } 318 | 319 | #ifdef FINSH_USING_MSH 320 | #include 321 | MSH_CMD_EXPORT(wget, Get file by URI: wget .); 322 | #endif /* FINSH_USING_MSH */ 323 | 324 | #endif /* RT_USING_DFS */ 325 | --------------------------------------------------------------------------------