|
| 1 | +/** |
| 2 | + * ESP8266httpClient.cpp |
| 3 | + * |
| 4 | + * Created on: 02.11.2015 |
| 5 | + * |
| 6 | + * Copyright (c) 2015 Markus Sattler. All rights reserved. |
| 7 | + * This file is part of the ESP8266httpClient for Arduino. |
| 8 | + * |
| 9 | + * This library is free software; you can redistribute it and/or |
| 10 | + * modify it under the terms of the GNU Lesser General Public |
| 11 | + * License as published by the Free Software Foundation; either |
| 12 | + * version 2.1 of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This library is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with this library; if not, write to the Free Software |
| 21 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#include <Arduino.h> |
| 26 | +#include <ESP8266WiFi.h> |
| 27 | +#include <WiFiClientSecure.h> |
| 28 | + |
| 29 | +#include "ESP8266httpClient.h" |
| 30 | + |
| 31 | +httpClient::httpClient() { |
| 32 | + _tcp = NULL; |
| 33 | + _tcps = NULL; |
| 34 | +} |
| 35 | + |
| 36 | +httpClient::~httpClient() { |
| 37 | + if(connected()) { |
| 38 | + _tcp->stop(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +void httpClient::begin(const char *host, uint16_t port, const char * url, bool https, const char * httpsFingerprint) { |
| 44 | + _host = host; |
| 45 | + _port = port; |
| 46 | + _url = url; |
| 47 | + _https = https; |
| 48 | + _httpsFingerprint = httpsFingerprint; |
| 49 | +} |
| 50 | + |
| 51 | +void httpClient::begin(String host, uint16_t port, String url, bool https, String httpsFingerprint) { |
| 52 | + begin(host.c_str(), port, url.c_str(), https, httpsFingerprint.c_str()); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * connected |
| 57 | + * @return connected status |
| 58 | + */ |
| 59 | +bool httpClient::connected() { |
| 60 | + if(_tcp) { |
| 61 | + return _tcp->connected(); |
| 62 | + } |
| 63 | + return false; |
| 64 | +} |
| 65 | + |
| 66 | +bool httpClient::GET() { |
| 67 | + |
| 68 | + bool status; |
| 69 | + status = connect(); |
| 70 | + if(status) { |
| 71 | + status = sendHeader("GET"); |
| 72 | + } |
| 73 | + |
| 74 | + return status; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * sends a post request to the server |
| 79 | + * @param payload uint8_t * |
| 80 | + * @param size size_t |
| 81 | + * @return status |
| 82 | + */ |
| 83 | +bool httpClient::POST(uint8_t * payload, size_t size) { |
| 84 | + |
| 85 | + bool status; |
| 86 | + status = connect(); |
| 87 | + if(status) { |
| 88 | + addHeader("Content-Length", String(size)); |
| 89 | + status = sendHeader("POST"); |
| 90 | + } |
| 91 | + |
| 92 | + if(status) { |
| 93 | + status = _tcp->write(&payload[0], size); |
| 94 | + } |
| 95 | + |
| 96 | + return status; |
| 97 | +} |
| 98 | + |
| 99 | +bool httpClient::POST(String payload) { |
| 100 | + return POST((uint8_t *) payload.c_str(), payload.length()); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * returns the stram of the tcp connection |
| 105 | + * @return WiFiClient |
| 106 | + */ |
| 107 | +WiFiClient & httpClient::getStream(void) { |
| 108 | + if(connected()) { |
| 109 | + return *_tcp; |
| 110 | + } |
| 111 | + // todo return error? |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * adds Headder to the request |
| 116 | + * @param name |
| 117 | + * @param value |
| 118 | + * @param first |
| 119 | + */ |
| 120 | +void httpClient::addHeader(const String& name, const String& value, bool first) { |
| 121 | + |
| 122 | + String headerLine = name; |
| 123 | + headerLine += ": "; |
| 124 | + headerLine += value; |
| 125 | + headerLine += "\r\n"; |
| 126 | + |
| 127 | + if(first) { |
| 128 | + _Headers = headerLine + _Headers; |
| 129 | + } else { |
| 130 | + _Headers += headerLine; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * init TCP connection and handle ssl verify if needed |
| 136 | + * @return true if connection is ok |
| 137 | + */ |
| 138 | +bool httpClient::connect(void) { |
| 139 | + |
| 140 | + if(connected()) { |
| 141 | + DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected!\n"); |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | + if(_https) { |
| 146 | + DEBUG_HTTPCLIENT("[HTTP-Client] connect https...\n"); |
| 147 | + _tcps = new WiFiClientSecure(); |
| 148 | + _tcp = _tcps; |
| 149 | + } else { |
| 150 | + DEBUG_HTTPCLIENT("[HTTP-Client] connect...\n"); |
| 151 | + _tcp = new WiFiClient(); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + if(!_tcp->connect(_host.c_str(), _port)) { |
| 156 | + DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u.\n", _host.c_str(), _port); |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u.\n", _host.c_str(), _port); |
| 161 | + |
| 162 | + if(_https) { |
| 163 | + if(_tcps->verify(_httpsFingerprint.c_str(), _host.c_str())) { |
| 164 | + DEBUG_HTTPCLIENT("[HTTP-Client] https certificate matches\n"); |
| 165 | + } else { |
| 166 | + DEBUG_HTTPCLIENT("[HTTP-Client] https certificate doesn't match!\n"); |
| 167 | + _tcp->stop(); |
| 168 | + return false; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // set Timeout for readBytesUntil and readStringUntil |
| 173 | + _tcp->setTimeout(HTTPCLIENT_TCP_TIMEOUT); |
| 174 | + |
| 175 | +#ifdef ESP8266 |
| 176 | + _tcp->setNoDelay(true); |
| 177 | +#endif |
| 178 | + return connected(); |
| 179 | +} |
| 180 | + |
| 181 | +/** |
| 182 | + * sends HTTP request header |
| 183 | + * @param type (GET, POST, ...) |
| 184 | + * @return status |
| 185 | + */ |
| 186 | +bool httpClient::sendHeader(const char * type) { |
| 187 | + String header = String(type) + " " + _url + " HTTP/1.1\r\n" |
| 188 | + "Host: " + _host + "\r\n" |
| 189 | + "User-Agent: ESP8266httpClient\r\n" |
| 190 | + "Connection: close\r\n" + |
| 191 | + _Headers + |
| 192 | + "\r\n"; |
| 193 | + |
| 194 | + return _tcp->write(header.c_str(), header.length()); |
| 195 | +} |
0 commit comments