Skip to content

Commit 6eabea3

Browse files
committed
URLStreamESP32.h arduino_esp_crt_bundle_attach not in 5.4
1 parent 6aa056e commit 6eabea3

File tree

1 file changed

+155
-156
lines changed

1 file changed

+155
-156
lines changed

src/AudioTools/CoreAudio/AudioHttp/URLStreamESP32.h

Lines changed: 155 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -217,190 +217,189 @@ class URLStreamESP32 : public AbstractURLStream {
217217
http_config.cert_pem = (const char*)pem_cert;
218218
http_config.cert_len = pem_cert_len;
219219
} else {
220-
#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 3, 7) && defined(ARDUINO)
220+
#if defined(ARDUINO) && ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 3, 7) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 0)
221221
http_config.crt_bundle_attach = arduino_esp_crt_bundle_attach;
222222
#else
223223
http_config.crt_bundle_attach = esp_crt_bundle_attach;
224-
#endif }
225-
226-
switch (action) {
227-
case GET:
228-
http_config.method = HTTP_METHOD_GET;
229-
break;
230-
case POST:
231-
http_config.method = HTTP_METHOD_POST;
232-
break;
233-
case PUT:
234-
http_config.method = HTTP_METHOD_PUT;
235-
break;
236-
case DELETE:
237-
http_config.method = HTTP_METHOD_DELETE;
238-
break;
239-
default:
240-
LOGE("Unsupported action: %d", action);
241-
break;
242-
}
224+
#endif
225+
}
243226

244-
// Init only the first time
245-
if (client_handle == nullptr) {
246-
client_handle = esp_http_client_init(&http_config);
247-
}
227+
switch (action) {
228+
case GET:
229+
http_config.method = HTTP_METHOD_GET;
230+
break;
231+
case POST:
232+
http_config.method = HTTP_METHOD_POST;
233+
break;
234+
case PUT:
235+
http_config.method = HTTP_METHOD_PUT;
236+
break;
237+
case DELETE:
238+
http_config.method = HTTP_METHOD_DELETE;
239+
break;
240+
default:
241+
LOGE("Unsupported action: %d", action);
242+
break;
243+
}
248244

249-
// process header parameters
250-
if (!StrView(acceptMime).isEmpty()) addRequestHeader(ACCEPT, acceptMime);
251-
if (!StrView(reqMime).isEmpty()) addRequestHeader(CONTENT_TYPE, reqMime);
252-
List<HttpHeaderLine*>& lines = request.header().getHeaderLines();
253-
for (auto it = lines.begin(); it != lines.end(); ++it) {
254-
if ((*it)->active) {
255-
esp_http_client_set_header(client_handle, (*it)->key.c_str(),
256-
(*it)->value.c_str());
257-
}
258-
}
245+
// Init only the first time
246+
if (client_handle == nullptr) {
247+
client_handle = esp_http_client_init(&http_config);
248+
}
259249

260-
// Open http
261-
if (esp_http_client_open(client_handle, 0) != ESP_OK) {
262-
LOGE("esp_http_client_open");
263-
return false;
250+
// process header parameters
251+
if (!StrView(acceptMime).isEmpty()) addRequestHeader(ACCEPT, acceptMime);
252+
if (!StrView(reqMime).isEmpty()) addRequestHeader(CONTENT_TYPE, reqMime);
253+
List<HttpHeaderLine*>& lines = request.header().getHeaderLines();
254+
for (auto it = lines.begin(); it != lines.end(); ++it) {
255+
if ((*it)->active) {
256+
esp_http_client_set_header(client_handle, (*it)->key.c_str(),
257+
(*it)->value.c_str());
264258
}
259+
}
265260

266-
// Determine the result
267-
int content_length = esp_http_client_fetch_headers(client_handle);
268-
int status_code = esp_http_client_get_status_code(client_handle);
269-
LOGI("status_code: %d / content_length: %d", status_code, content_length);
261+
// Open http
262+
if (esp_http_client_open(client_handle, 0) != ESP_OK) {
263+
LOGE("esp_http_client_open");
264+
return false;
265+
}
270266

271-
// Process post/put data
272-
StrView data(reqData);
273-
if (!data.isEmpty()) {
274-
write((const uint8_t*)reqData, data.length());
275-
}
267+
// Determine the result
268+
int content_length = esp_http_client_fetch_headers(client_handle);
269+
int status_code = esp_http_client_get_status_code(client_handle);
270+
LOGI("status_code: %d / content_length: %d", status_code, content_length);
276271

277-
return status_code == 200;
278-
}
279-
// ends the request
280-
virtual void end() override {
281-
esp_http_client_close(client_handle);
282-
esp_http_client_cleanup(client_handle);
272+
// Process post/put data
273+
StrView data(reqData);
274+
if (!data.isEmpty()) {
275+
write((const uint8_t*)reqData, data.length());
283276
}
284277

285-
/// Writes are not supported
286-
int availableForWrite() override { return 1024; }
278+
return status_code == 200;
279+
}
280+
// ends the request
281+
virtual void end() override {
282+
esp_http_client_close(client_handle);
283+
esp_http_client_cleanup(client_handle);
284+
}
287285

288-
/// Sets the ssid that will be used for logging in (when calling begin)
289-
virtual void setSSID(const char* ssid) { this->ssid = ssid; }
286+
/// Writes are not supported
287+
int availableForWrite() override { return 1024; }
290288

291-
/// Sets the password that will be used for logging in (when calling begin)
292-
virtual void setPassword(const char* password) {
293-
this->password = password;
294-
}
289+
/// Sets the ssid that will be used for logging in (when calling begin)
290+
virtual void setSSID(const char* ssid) { this->ssid = ssid; }
295291

296-
/// Sets the power save mode (default false)!
297-
virtual void setPowerSave(bool ps) {
298-
IDF_WIFI.setPowerSave(ps ? WIFI_PS_MAX_MODEM : WIFI_PS_NONE);
299-
}
292+
/// Sets the password that will be used for logging in (when calling begin)
293+
virtual void setPassword(const char* password) { this->password = password; }
300294

301-
size_t write(const uint8_t* data, size_t len) override {
302-
TRACED();
303-
return esp_http_client_write(client_handle, (const char*)data, len);
304-
}
295+
/// Sets the power save mode (default false)!
296+
virtual void setPowerSave(bool ps) {
297+
IDF_WIFI.setPowerSave(ps ? WIFI_PS_MAX_MODEM : WIFI_PS_NONE);
298+
}
305299

306-
size_t readBytes(uint8_t* data, size_t len) override {
307-
TRACED();
308-
return esp_http_client_read(client_handle, (char*)data, len);
309-
}
300+
size_t write(const uint8_t* data, size_t len) override {
301+
TRACED();
302+
return esp_http_client_write(client_handle, (const char*)data, len);
303+
}
310304

311-
/// Adds/Updates a request header
312-
void addRequestHeader(const char* key, const char* value) override {
313-
TRACED();
314-
request.addRequestHeader(key, value);
315-
}
316-
/// Provides a header entry
317-
const char* getReplyHeader(const char* key) override {
318-
return request.getReplyHeader(key);
319-
}
305+
size_t readBytes(uint8_t* data, size_t len) override {
306+
TRACED();
307+
return esp_http_client_read(client_handle, (char*)data, len);
308+
}
320309

321-
/// Define the Root PEM Certificate for SSL: Method compatible with Arduino
322-
/// WiFiClientSecure API
323-
void setCACert(const char* cert) override {
324-
int len = strlen(cert);
325-
setCACert((const uint8_t*)cert, len + 1);
326-
}
310+
/// Adds/Updates a request header
311+
void addRequestHeader(const char* key, const char* value) override {
312+
TRACED();
313+
request.addRequestHeader(key, value);
314+
}
315+
/// Provides a header entry
316+
const char* getReplyHeader(const char* key) override {
317+
return request.getReplyHeader(key);
318+
}
327319

328-
/// Defines the read buffer size
329-
void setReadBufferSize(int size) { buffer_size = size; }
330-
331-
/// Used for request and reply header parameters
332-
HttpRequest& httpRequest() override { return request; }
333-
334-
/// Does nothing
335-
void setClient(Client & client) override {}
336-
337-
protected:
338-
int id = 0;
339-
HttpRequest request;
340-
esp_http_client_handle_t client_handle = nullptr;
341-
bool is_power_save = false;
342-
const char* ssid = nullptr;
343-
const char* password = nullptr;
344-
int buffer_size = DEFAULT_BUFFER_SIZE;
345-
const uint8_t* pem_cert = nullptr;
346-
int pem_cert_len = 0;
347-
348-
/// Define the Root PEM Certificate for SSL: the last byte must be null, the
349-
/// len is including the ending null
350-
void setCACert(const uint8_t* cert, int len) {
351-
pem_cert_len = len;
352-
pem_cert = cert;
353-
// certificate must end with traling null
354-
assert(cert[len - 1] == 0);
355-
}
320+
/// Define the Root PEM Certificate for SSL: Method compatible with Arduino
321+
/// WiFiClientSecure API
322+
void setCACert(const char* cert) override {
323+
int len = strlen(cert);
324+
setCACert((const uint8_t*)cert, len + 1);
325+
}
356326

357-
static esp_err_t http_event_handler(esp_http_client_event_t * evt) {
358-
switch (evt->event_id) {
359-
case HTTP_EVENT_ERROR:
360-
LOGI("HTTP_EVENT_ERROR");
361-
break;
362-
case HTTP_EVENT_ON_CONNECTED:
363-
LOGD("HTTP_EVENT_ON_CONNECTED");
364-
break;
365-
case HTTP_EVENT_HEADER_SENT:
366-
LOGD("HTTP_EVENT_HEADER_SENT");
367-
break;
368-
case HTTP_EVENT_ON_HEADER:
369-
LOGI("HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key,
370-
evt->header_value);
371-
// store reply headers
372-
actualURLStreamESP32->request.reply().put(evt->header_key,
373-
evt->header_value);
374-
break;
375-
case HTTP_EVENT_ON_DATA:
376-
LOGD("HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
377-
break;
378-
case HTTP_EVENT_ON_FINISH:
379-
LOGI("HTTP_EVENT_ON_FINISH");
380-
break;
381-
case HTTP_EVENT_DISCONNECTED:
382-
LOGI("HTTP_EVENT_DISCONNECTED");
383-
break;
327+
/// Defines the read buffer size
328+
void setReadBufferSize(int size) { buffer_size = size; }
329+
330+
/// Used for request and reply header parameters
331+
HttpRequest& httpRequest() override { return request; }
332+
333+
/// Does nothing
334+
void setClient(Client& client) override {}
335+
336+
protected:
337+
int id = 0;
338+
HttpRequest request;
339+
esp_http_client_handle_t client_handle = nullptr;
340+
bool is_power_save = false;
341+
const char* ssid = nullptr;
342+
const char* password = nullptr;
343+
int buffer_size = DEFAULT_BUFFER_SIZE;
344+
const uint8_t* pem_cert = nullptr;
345+
int pem_cert_len = 0;
346+
347+
/// Define the Root PEM Certificate for SSL: the last byte must be null, the
348+
/// len is including the ending null
349+
void setCACert(const uint8_t* cert, int len) {
350+
pem_cert_len = len;
351+
pem_cert = cert;
352+
// certificate must end with traling null
353+
assert(cert[len - 1] == 0);
354+
}
355+
356+
static esp_err_t http_event_handler(esp_http_client_event_t* evt) {
357+
switch (evt->event_id) {
358+
case HTTP_EVENT_ERROR:
359+
LOGI("HTTP_EVENT_ERROR");
360+
break;
361+
case HTTP_EVENT_ON_CONNECTED:
362+
LOGD("HTTP_EVENT_ON_CONNECTED");
363+
break;
364+
case HTTP_EVENT_HEADER_SENT:
365+
LOGD("HTTP_EVENT_HEADER_SENT");
366+
break;
367+
case HTTP_EVENT_ON_HEADER:
368+
LOGI("HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key,
369+
evt->header_value);
370+
// store reply headers
371+
actualURLStreamESP32->request.reply().put(evt->header_key,
372+
evt->header_value);
373+
break;
374+
case HTTP_EVENT_ON_DATA:
375+
LOGD("HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
376+
break;
377+
case HTTP_EVENT_ON_FINISH:
378+
LOGI("HTTP_EVENT_ON_FINISH");
379+
break;
380+
case HTTP_EVENT_DISCONNECTED:
381+
LOGI("HTTP_EVENT_DISCONNECTED");
382+
break;
384383
#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 3, 7)
385-
case HTTP_EVENT_REDIRECT:
386-
LOGI("HTTP_EVENT_REDIRECT");
387-
break;
384+
case HTTP_EVENT_REDIRECT:
385+
LOGI("HTTP_EVENT_REDIRECT");
386+
break;
388387
#endif
389-
}
390-
return ESP_OK;
391388
}
392-
};
389+
return ESP_OK;
390+
}
391+
};
393392

394-
/// ICYStream
395-
using ICYStreamESP32 = ICYStreamT<URLStreamESP32>;
396-
using URLStreamBufferedESP32 = URLStreamBufferedT<URLStreamESP32>;
397-
using ICYStreamBufferedESP32 = URLStreamBufferedT<ICYStreamESP32>;
393+
/// ICYStream
394+
using ICYStreamESP32 = ICYStreamT<URLStreamESP32>;
395+
using URLStreamBufferedESP32 = URLStreamBufferedT<URLStreamESP32>;
396+
using ICYStreamBufferedESP32 = URLStreamBufferedT<ICYStreamESP32>;
398397

399398
/// Support URLStream w/o Arduino
400399
#if !defined(ARDUINO)
401-
using URLStream = URLStreamESP32;
402-
using URLStreamBuffered = URLStreamBufferedESP32;
403-
using ICYStreamBuffered = ICYStreamBufferedESP32;
400+
using URLStream = URLStreamESP32;
401+
using URLStreamBuffered = URLStreamBufferedESP32;
402+
using ICYStreamBuffered = ICYStreamBufferedESP32;
404403
#endif
405404

406405
} // namespace audio_tools

0 commit comments

Comments
 (0)