@@ -87,33 +87,43 @@ HTTPClient::~HTTPClient()
8787 }
8888}
8989
90+ void HTTPClient::clear ()
91+ {
92+ _returnCode = 0 ;
93+ _size = -1 ;
94+ _headers = " " ;
95+ }
96+
9097
9198bool HTTPClient::begin (String url, String httpsFingerprint)
9299{
100+ _transportTraits.reset (nullptr );
93101 if (httpsFingerprint.length () == 0 ) {
94102 return false ;
95103 }
96- if (!begin (url)) {
104+ if (!beginInternal (url, " https " )) {
97105 return false ;
98106 }
99107 _transportTraits = TransportTraitsPtr (new TLSTraits (httpsFingerprint));
100108 DEBUG_HTTPCLIENT (" [HTTP-Client][begin] httpsFingerprint: %s\n " , httpsFingerprint.c_str ());
101109 return true ;
102110}
103111
104- void HTTPClient::clear ()
105- {
106- _returnCode = 0 ;
107- _size = -1 ;
108- _headers = " " ;
109- }
110-
111-
112112/* *
113113 * parsing the url for all needed parameters
114114 * @param url String
115115 */
116116bool HTTPClient::begin (String url)
117+ {
118+ _transportTraits.reset (nullptr );
119+ if (!beginInternal (url, " http" )) {
120+ return false ;
121+ }
122+ _transportTraits = TransportTraitsPtr (new TransportTraits ());
123+ return true ;
124+ }
125+
126+ bool HTTPClient::beginInternal (String url, const char * expectedProtocol)
117127{
118128 DEBUG_HTTPCLIENT (" [HTTP-Client][begin] url: %s\n " , url.c_str ());
119129 bool hasPort = false ;
@@ -148,25 +158,14 @@ bool HTTPClient::begin(String url)
148158 _host = host.substring (0 , index); // hostname
149159 host.remove (0 , (index + 1 )); // remove hostname + :
150160 _port = host.toInt (); // get port
151- hasPort = true ;
152161 } else {
153162 _host = host;
154163 }
155164 _uri = url;
156-
157- if (_protocol.equalsIgnoreCase (" http" )) {
158- if (!hasPort) {
159- _port = 80 ;
160- }
161- } else if (_protocol.equalsIgnoreCase (" https" )) {
162- if (!hasPort) {
163- _port = 443 ;
164- }
165- } else {
166- DEBUG_HTTPCLIENT (" [HTTP-Client][begin] protocol: %s unknown?!\n " , _protocol.c_str ());
165+ if (_protocol != expectedProtocol) {
166+ DEBUG_HTTPCLIENT (" [HTTP-Client][begin] unexpected protocol: %s, expected %s\n " , _protocol.c_str (), expectedProtocol);
167167 return false ;
168168 }
169- _transportTraits = TransportTraitsPtr (new TransportTraits ());
170169 DEBUG_HTTPCLIENT (" [HTTP-Client][begin] host: %s port: %d url: %s\n " , _host.c_str (), _port, _uri.c_str ());
171170 return true ;
172171}
@@ -785,7 +784,7 @@ bool HTTPClient::connect(void)
785784 }
786785
787786 if (!_transportTraits) {
788- DEBUG_HTTPCLIENT (" [HTTP-Client] _transportTraits is null ( HTTPClient::begin not called?) \n " );
787+ DEBUG_HTTPCLIENT (" [HTTP-Client] connect: HTTPClient::begin was not called or returned error \n " );
789788 return false ;
790789 }
791790
0 commit comments