This Socket Example Program Implements a TCP Socket, UDP Socket, and HTTPClient socket then uses each of these to perform basic I/O.
Dependencies: JSON M2XStreamClient-JMF WNCInterface mbed-rtos mbed
README
This example program has been updated, it uses the WDCInterface Library (for the Wistron NeWeb Corporation (WNC) M14A2A Series data module) that is used on the AT&T Cellular IoT Starter Kit; sold by Avnet.
NOTE: This example is specific to the AT&T Cellular IoT Starter Kit which uses a FRDM-K64F. The users mbed.org compiler should be configured to use the FRDM-K64F platform.
DESCRIPTION¶
This example program provides examples on using two HTTPClient classes to execute GET/POST/PUT/DELETE commands. The HTTPClient class shows the use of non-encrypted commands while the HTTPSClient class show using the same commands using a secure transport layer (SSL/TLS). In the example program a HELLO.TXT file is retrieved from MBED.ORG then a series of commands are sent to HTTPBIN.ORG. These same commands are also sent to the same websites using port 443/SSL to demonstrate how to utilize a SSL/TLS connection.
The primary components in the example program are: 1. HTTPClient-JMF library - this library implements:
- The HTTPClient classes; one for encrypted (HPPTSClient) and one unencrypted (HTTPClient)
- CA Certificates; the include file contains CA Certificates for the servers which are accessed
- A URIParser; dissects a user provided URI into its component parts
2. WNCInterface - an updated hardware interface that includes the ARMmbed TLS library.
3. The example program utilizes the mbed v5.2 RTOS.
Program Discussion¶
The HTTPClient example issues a series of commands; a GET, POST, PUT, and DELETE. The same commands are used with the HTTPClient and the HTTPSClient with the only difference being that HPPTSClient transactions are verified against the CA Certificate and all transactions are encrypted between the client and server.
The example program uses the RTOS because the ARMmbed TLS library is stack sensitive. This is due to the fact that all big-number arithmetic is performed on the stack. In the latest MBED RTOS, main() is a default task that is created automatically but it is not possible to easily modify the default stack size. Instead, the main task is used to create a test task in which the task stack can be specified. This is typically how a system would be implemented though for this example it is slightly overkill.
A second area to be aware of are buffer sizes. This example is simply that, an example, so buffer sizes are fixed at
compile time; HTTP header processing is minimal; and there are likely other security/implementation limitations. It is
not intended to be a final commercial productjust an example to demonstrate the principles of the WNCIntnerface
routines.
It is possible to enable various debug information. For ARMmbed TLS, you can set DEBUG to a value of 1 to 4 depending on the level of information you want the TLS library to output. Similarly, you can enable debugging in HTTPClient.h and various debug messages will be generated as the HTTPClient routines are executed.
Example Execution Overview¶
When run, the example program output looks like:
title-Sample
Testing HTTPClient and HTTPSClient using the WNCInterface & Socket software WNC Module IS initialized (07). IP Address: 10.58.238.30 >>>>>>>>>>>><<<<<<<<<<<< >>> TEST HTTPClient <<< >>>>>>>>>>>><<<<<<<<<<<< ** Fetch a page... ** Page fetched successfully - read 14 characters <-----> Result: Hello world! <-----> ** Post data... ** Executed POST successfully - read 326 characters <-----> Result: { "args": {}, "data": "", "files": {}, "form": { "Hello": "World", "test": "1234" }, "headers": { "Content-Length": "21", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org" }, "json": null, "origin": "204.16.69.128", "url": "http://httpbin.org/post" } <-----> ** Put data... ** Executed PUT successfully - read 276 characters <-----> Result: { "args": {}, "data": "This is a PUT test!", "files": {}, "form": {}, "headers": { "Content-Length": "19", "Content-Type": "text/plain", "Host": "httpbin.org" }, "json": null, "origin": "204.16.69.124", "url": "http://httpbin.org/put" } <-----> ** Delete data... ** Executed DELETE successfully - read 196 characters <-----> Result: { "args": {}, "data": "", "files": {}, "form": {}, "headers": { "Host": "httpbin.org" }, "json": null, "origin": "204.16.69.128", "url": "http://httpbin.org/delete" } <-----> ** HTTP:stream data 15 times... ** STREAM successfull - returned 1865 characters <-----> Result: {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 0, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 1, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 2, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 3, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 4, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 5, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 6, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 7, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 8, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 9, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 10, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 11, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 12, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 13, "origin": "204.16.69.125"} {"url": "http://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 14, "origin": "204.16.69.125"} <-----> >>>>HTTP:Status... Executed STATUS successfully - read 276 characters <-----> Result: { "args": { "show_env": "1" }, "headers": { "Host": "httpbin.org", "Runscope-Service": "httpbin", "X-Forwarded-For": "204.16.69.130", "X-Real-Ip": "204.16.69.130" }, "origin": "204.16.69.130", "url": "http://httpbin.org/get?show_env=1" } <-----> >>>>>>>>>>>><<<<<<<<<<<< >>> TEST HTTPSClient <<< >>>>>>>>>>>><<<<<<<<<<<< ** HTTPS:Fetch a page... ** Certificate verification passed Page fetched successfully - read 14 characters <-----> Result: Hello world! <-----> >>>>HTTPS:Post data... Certificate verification passed Executed POST successfully - read 327 characters <-----> Result: { "args": {}, "data": "", "files": {}, "form": { "https Testing": "https doing a Post" }, "headers": { "Content-Length": "32", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org" }, "json": null, "origin": "204.16.69.130", "url": "https://httpbin.org/post" } <-----> >>>>HTTPS:Put data... Certificate verification passed Executed PUT successfully - read 284 characters <-----> Result: { "args": {}, "data": "This is an HTTPS PUT test!", "files": {}, "form": {}, "headers": { "Content-Length": "26", "Content-Type": "text/plain", "Host": "httpbin.org" }, "json": null, "origin": "204.16.69.131", "url": "https://httpbin.org/put" } <-----> >>>>HTTPS:stream data... Certificate verification passed STREAM successfull - returned 1880 characters <-----> Result: {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 0, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 1, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 2, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 3, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 4, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 5, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 6, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 7, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 8, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 9, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 10, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 11, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 12, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 13, "origin": "204.16.69.124"} {"url": "https://httpbin.org/stream/15", "headers": {"Host": "httpbin.org"}, "args": {}, "id": 14, "origin": "204.16.69.124"} <-----> >>>>HTTPS:Delete data... Certificate verification passed Executed DELETE successfully - read 197 characters <-----> Result: { "args": {}, "data": "", "files": {}, "form": {}, "headers": { "Host": "httpbin.org" }, "json": null, "origin": "204.16.69.128", "url": "https://httpbin.org/delete" } <-----> >>>>HTTPS:Status... Certificate verification passed Executed STATUS successfully - read 345 characters <-----> Result: { "args": { "show_env": "1" }, "headers": { "Host": "httpbin.org", "Runscope-Service": "httpbin", "X-Forwarded-For": "204.16.69.127", "X-Forwarded-Protocol": "https", "X-Forwarded-Ssl": "on", "X-Real-Ip": "204.16.69.127" }, "origin": "204.16.69.127", "url": "https://httpbin.org/get?show_env=1" } <-----> - - - - - - - ALL DONE - - - - - - -
License
This library is released under the Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License and may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.