@@ -95,37 +95,32 @@ static bool _base64_in_place(char *buf, size_t in_len, size_t out_len) {
95
95
if (encoded_len + 1 > out_len ) {
96
96
return false;
97
97
}
98
- ESP_LOGI (TAG , "triples %d" , triples );
99
98
100
99
// First pass, we convert input buffer to numeric base 64 values
101
100
char * in = buf + (triples - 1 ) * 3 ;
102
101
char * out = buf + (triples - 1 ) * 4 ;
103
102
int r = in_len % 3 ;
104
103
int partial = 0 ;
105
104
if (r != 0 ) {
106
- out [0 ] = (in [0 ] & 0xFC ) >> 2 ;
107
- out [1 ] = (in [0 ] & 0x03 ) << 4 ;
105
+ out [3 ] = 64 ;
108
106
if (r == 2 ) {
109
- out [1 ] |= (in [1 ] & 0xF0 ) >> 4 ;
110
107
out [2 ] = (in [1 ] & 0x0F ) << 2 ;
108
+ out [1 ] = (in [0 ] & 0x03 ) << 4 | (in [1 ] & 0xF0 ) >> 4 ;
111
109
} else {
112
110
out [2 ] = 64 ;
111
+ out [1 ] = (in [0 ] & 0x03 ) << 4 ;
113
112
}
114
- out [3 ] = 64 ;
113
+ out [0 ] = ( in [ 0 ] & 0xFC ) >> 2 ;
115
114
in -= 3 ;
116
115
out -= 4 ;
117
116
partial = 1 ;
118
- } else {
119
- ESP_LOGI (TAG , "no partial" );
120
117
}
121
118
buf [encoded_len ] = '\0' ;
122
119
for (size_t i = 0 ; i < triples - partial ; i ++ ) {
123
- ESP_LOGI (TAG , "in %d %d %d" , in [0 ], in [1 ], in [2 ]);
124
- out [0 ] = (in [0 ] & 0xFC ) >> 2 ;
125
- out [1 ] = (in [0 ] & 0x03 ) << 4 | (in [1 ] & 0xF0 ) >> 4 ;
126
- out [2 ] = (in [1 ] & 0x0F ) << 2 | (in [2 ] & 0xC0 ) >> 6 ;
127
120
out [3 ] = in [2 ] & 0x3F ;
128
- ESP_LOGI (TAG , "out %d %d %d %d" , out [0 ], out [1 ], out [2 ], out [3 ]);
121
+ out [2 ] = (in [1 ] & 0x0F ) << 2 | (in [2 ] & 0xC0 ) >> 6 ;
122
+ out [1 ] = (in [0 ] & 0x03 ) << 4 | (in [1 ] & 0xF0 ) >> 4 ;
123
+ out [0 ] = (in [0 ] & 0xFC ) >> 2 ;
129
124
in -= 3 ;
130
125
out -= 4 ;
131
126
}
@@ -425,7 +420,10 @@ static void _process_request(socketpool_socket_obj_t *socket, _request *request)
425
420
request -> offset = 0 ;
426
421
request -> state = STATE_HEADER_KEY ;
427
422
if (strcmp (request -> header_key , "Authorization" ) == 0 ) {
428
- ESP_LOGW (TAG , "Authorization" );
423
+ const char * prefix = "Basic " ;
424
+ request -> authenticated = memcmp (request -> header_value , prefix , strlen (prefix )) == 0 &&
425
+ strcmp (_api_password , request -> header_value + strlen (prefix )) == 0 ;
426
+ ESP_LOGW (TAG , "Authorization %d" , request -> authenticated );
429
427
} else if (strcmp (request -> header_key , "Host" ) == 0 ) {
430
428
ESP_LOGW (TAG , "Host header check '%s'" , request -> header_value );
431
429
request -> redirect = strcmp (request -> header_value , "circuitpython.local" ) == 0 ;
@@ -567,13 +565,20 @@ static void _process_request(socketpool_socket_obj_t *socket, _request *request)
567
565
while (send_offset < quantity_read ) {
568
566
int sent = socketpool_socket_send (socket , data_buffer + send_offset , quantity_read - send_offset );
569
567
if (sent < 0 ) {
570
- ESP_LOGE (TAG , "file send error %d" , sent );
571
- break ;
568
+ if (sent == - EAGAIN ) {
569
+ sent = 0 ;
570
+ } else {
571
+ ESP_LOGE (TAG , "file send error %d" , sent );
572
+ break ;
573
+ }
572
574
}
573
575
send_offset += sent ;
574
576
}
575
577
}
576
578
ESP_LOGW (TAG , "file return done" );
579
+ if (total_read < total_length ) {
580
+ socketpool_socket_close (socket );
581
+ }
577
582
578
583
f_close (& active_file );
579
584
}
0 commit comments