@@ -87,33 +87,43 @@ HTTPClient::~HTTPClient()
87
87
}
88
88
}
89
89
90
+ void HTTPClient::clear ()
91
+ {
92
+ _returnCode = 0 ;
93
+ _size = -1 ;
94
+ _headers = " " ;
95
+ }
96
+
90
97
91
98
bool HTTPClient::begin (String url, String httpsFingerprint)
92
99
{
100
+ _transportTraits.reset (nullptr );
93
101
if (httpsFingerprint.length () == 0 ) {
94
102
return false ;
95
103
}
96
- if (!begin (url)) {
104
+ if (!beginInternal (url, " https " )) {
97
105
return false ;
98
106
}
99
107
_transportTraits = TransportTraitsPtr (new TLSTraits (httpsFingerprint));
100
108
DEBUG_HTTPCLIENT (" [HTTP-Client][begin] httpsFingerprint: %s\n " , httpsFingerprint.c_str ());
101
109
return true ;
102
110
}
103
111
104
- void HTTPClient::clear ()
105
- {
106
- _returnCode = 0 ;
107
- _size = -1 ;
108
- _headers = " " ;
109
- }
110
-
111
-
112
112
/* *
113
113
* parsing the url for all needed parameters
114
114
* @param url String
115
115
*/
116
116
bool 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)
117
127
{
118
128
DEBUG_HTTPCLIENT (" [HTTP-Client][begin] url: %s\n " , url.c_str ());
119
129
bool hasPort = false ;
@@ -148,25 +158,14 @@ bool HTTPClient::begin(String url)
148
158
_host = host.substring (0 , index); // hostname
149
159
host.remove (0 , (index + 1 )); // remove hostname + :
150
160
_port = host.toInt (); // get port
151
- hasPort = true ;
152
161
} else {
153
162
_host = host;
154
163
}
155
164
_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);
167
167
return false ;
168
168
}
169
- _transportTraits = TransportTraitsPtr (new TransportTraits ());
170
169
DEBUG_HTTPCLIENT (" [HTTP-Client][begin] host: %s port: %d url: %s\n " , _host.c_str (), _port, _uri.c_str ());
171
170
return true ;
172
171
}
@@ -785,7 +784,7 @@ bool HTTPClient::connect(void)
785
784
}
786
785
787
786
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 " );
789
788
return false ;
790
789
}
791
790
0 commit comments