@@ -482,7 +482,6 @@ Error HTTPClientTCP::poll() {
482482 (rs >= 2 && response_str[rs - 2 ] == ' \n ' && response_str[rs - 1 ] == ' \n ' ) ||
483483 (rs >= 4 && response_str[rs - 4 ] == ' \r ' && response_str[rs - 3 ] == ' \n ' && response_str[rs - 2 ] == ' \r ' && response_str[rs - 1 ] == ' \n ' )) {
484484 // End of response, parse.
485- response_str.push_back (0 );
486485 String response = String::utf8 ((const char *)response_str.ptr (), response_str.size ());
487486 Vector<String> responses = response.split (" \n " );
488487 body_size = -1 ;
@@ -506,13 +505,20 @@ Error HTTPClientTCP::poll() {
506505 if (s.length () == 0 ) {
507506 continue ;
508507 }
509- if (s.begins_with (" content-length:" )) {
510- body_size = s.substr (s.find_char (' :' ) + 1 ).strip_edges ().to_int ();
508+ const char content_length_label[] = " content-length:" ;
509+ const char transfer_encoding_label[] = " transfer-encoding:" ;
510+ if (s.begins_with (content_length_label)) {
511+ const int index = std::size (content_length_label) - 1 ;
512+ Span<char32_t > sp = Span<char32_t >(s.ptr () + index, s.length () - index);
513+ sp = String::strip_edges_span (sp, true , true );
514+ body_size = String::to_int (sp.begin (), sp.size ());
511515 body_left = body_size;
512516
513- } else if (s.begins_with (" transfer-encoding:" )) {
514- String encoding = header.substr (header.find_char (' :' ) + 1 ).strip_edges ();
515- if (encoding == " chunked" ) {
517+ } else if (s.begins_with (transfer_encoding_label)) {
518+ const int index = std::size (transfer_encoding_label) - 1 ;
519+ Span<char32_t > sp = Span<char32_t >(s.ptr () + index, s.length () - index);
520+ sp = String::strip_edges_span (sp, true , true );
521+ if (String (" chunked" ) == sp) {
516522 chunked = true ;
517523 }
518524 } else if (s.begins_with (" connection: close" )) {
0 commit comments