Core Base Classes for the Light Endpoints
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more
HTTPTransport.cpp@96:686ec39400dc, 2014-03-14 (annotated)
- Committer:
- ansond
- Date:
- Fri Mar 14 20:20:32 2014 +0000
- Revision:
- 96:686ec39400dc
- Parent:
- 39:4b9165d242f4
- Child:
- 111:dac439bf902b
updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 30:bf56ef794ba6 | 1 | /* Copyright C2013 Doug Anson, MIT License |
ansond | 30:bf56ef794ba6 | 2 | * |
ansond | 30:bf56ef794ba6 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 30:bf56ef794ba6 | 4 | * and associated documentation files the "Software", to deal in the Software without restriction, |
ansond | 30:bf56ef794ba6 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 30:bf56ef794ba6 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 30:bf56ef794ba6 | 7 | * furnished to do so, subject to the following conditions: |
ansond | 30:bf56ef794ba6 | 8 | * |
ansond | 30:bf56ef794ba6 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 30:bf56ef794ba6 | 10 | * substantial portions of the Software. |
ansond | 30:bf56ef794ba6 | 11 | * |
ansond | 30:bf56ef794ba6 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 30:bf56ef794ba6 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 30:bf56ef794ba6 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 30:bf56ef794ba6 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 30:bf56ef794ba6 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 30:bf56ef794ba6 | 17 | */ |
ansond | 30:bf56ef794ba6 | 18 | |
ansond | 30:bf56ef794ba6 | 19 | #include "HTTPTransport.h" |
ansond | 30:bf56ef794ba6 | 20 | |
ansond | 30:bf56ef794ba6 | 21 | // Endpoint Support |
ansond | 30:bf56ef794ba6 | 22 | #include "MBEDEndpoint.h" |
ansond | 31:7a4bb4742e69 | 23 | |
ansond | 30:bf56ef794ba6 | 24 | // HTTP Client |
ansond | 30:bf56ef794ba6 | 25 | HTTPClient _http; |
ansond | 30:bf56ef794ba6 | 26 | |
ansond | 30:bf56ef794ba6 | 27 | // default constructor |
ansond | 30:bf56ef794ba6 | 28 | HTTPTransport::HTTPTransport(ErrorHandler *error_handler,void *endpoint) : Transport(error_handler,endpoint) { |
ansond | 30:bf56ef794ba6 | 29 | this->m_http = &_http; |
ansond | 30:bf56ef794ba6 | 30 | this->m_connected = true; |
ansond | 30:bf56ef794ba6 | 31 | } |
ansond | 30:bf56ef794ba6 | 32 | |
ansond | 96:686ec39400dc | 33 | // constructor with custom HTTPClient binding |
ansond | 96:686ec39400dc | 34 | HTTPTransport::HTTPTransport(HTTPClient *http,ErrorHandler *error_handler,void *endpoint) : Transport(error_handler,endpoint) { |
ansond | 96:686ec39400dc | 35 | this->m_http = http; |
ansond | 96:686ec39400dc | 36 | this->m_connected = true; |
ansond | 96:686ec39400dc | 37 | } |
ansond | 96:686ec39400dc | 38 | |
ansond | 30:bf56ef794ba6 | 39 | // default destructor |
ansond | 30:bf56ef794ba6 | 40 | HTTPTransport::~HTTPTransport() { |
ansond | 30:bf56ef794ba6 | 41 | } |
ansond | 37:1588ba3af6d1 | 42 | |
ansond | 30:bf56ef794ba6 | 43 | // HTTP Get |
ansond | 30:bf56ef794ba6 | 44 | bool HTTPTransport::httpGet(char *url,char *result,int result_length) { |
ansond | 36:73e343ddca7f | 45 | this->logger()->blinkTransportTxLED(); |
ansond | 30:bf56ef794ba6 | 46 | int status = this->m_http->get(url,result,result_length); |
ansond | 36:73e343ddca7f | 47 | this->logger()->blinkTransportRxLED(); |
ansond | 30:bf56ef794ba6 | 48 | this->logger()->log("httpGet: Status: %d",status); |
ansond | 30:bf56ef794ba6 | 49 | return (status == 0); |
ansond | 30:bf56ef794ba6 | 50 | } |
ansond | 30:bf56ef794ba6 | 51 | |
ansond | 30:bf56ef794ba6 | 52 | // HTTP Put |
ansond | 30:bf56ef794ba6 | 53 | bool HTTPTransport::httpPut(char *url,char *data,int data_length,char *result,int result_length) { |
ansond | 30:bf56ef794ba6 | 54 | HTTPText output(data,data_length); |
ansond | 30:bf56ef794ba6 | 55 | HTTPText input(result,result_length); |
ansond | 36:73e343ddca7f | 56 | this->logger()->blinkTransportTxLED(); |
ansond | 30:bf56ef794ba6 | 57 | int status = this->m_http->put(url,output,&input); |
ansond | 36:73e343ddca7f | 58 | this->logger()->blinkTransportRxLED(); |
ansond | 30:bf56ef794ba6 | 59 | this->logger()->log("httpPut: Status: %d",status); |
ansond | 30:bf56ef794ba6 | 60 | return (status == 0); |
ansond | 30:bf56ef794ba6 | 61 | } |
ansond | 30:bf56ef794ba6 | 62 | |
ansond | 30:bf56ef794ba6 | 63 | // HTTP Post |
ansond | 30:bf56ef794ba6 | 64 | bool HTTPTransport::httpPost(char *url,char *data,int data_length,char *result,int result_length) { |
ansond | 30:bf56ef794ba6 | 65 | HTTPText output(data,data_length); |
ansond | 30:bf56ef794ba6 | 66 | HTTPText input(result,result_length); |
ansond | 36:73e343ddca7f | 67 | this->logger()->blinkTransportTxLED(); |
ansond | 30:bf56ef794ba6 | 68 | int status = this->m_http->post(url,output,&input); |
ansond | 36:73e343ddca7f | 69 | this->logger()->blinkTransportRxLED(); |
ansond | 30:bf56ef794ba6 | 70 | this->logger()->log("httpPost: Status: %d",status); |
ansond | 30:bf56ef794ba6 | 71 | return (status == 0); |
ansond | 30:bf56ef794ba6 | 72 | } |
ansond | 30:bf56ef794ba6 | 73 | |
ansond | 30:bf56ef794ba6 | 74 | // HTTP Delete |
ansond | 30:bf56ef794ba6 | 75 | bool HTTPTransport::httpDelete(char *url,char *data,int data_length) { |
ansond | 30:bf56ef794ba6 | 76 | HTTPText input(data,data_length); |
ansond | 36:73e343ddca7f | 77 | this->logger()->blinkTransportTxLED(); |
ansond | 30:bf56ef794ba6 | 78 | int status = this->m_http->del(url,&input); |
ansond | 36:73e343ddca7f | 79 | this->logger()->blinkTransportRxLED(); |
ansond | 30:bf56ef794ba6 | 80 | this->logger()->log("httpDelete: Status: %d",status); |
ansond | 30:bf56ef794ba6 | 81 | return (status == 0); |
ansond | 30:bf56ef794ba6 | 82 | } |