Skip to content

Commit 35045f0

Browse files
committed
Auth works
1 parent 4f0a7ae commit 35045f0

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,37 +95,32 @@ static bool _base64_in_place(char *buf, size_t in_len, size_t out_len) {
9595
if (encoded_len + 1 > out_len) {
9696
return false;
9797
}
98-
ESP_LOGI(TAG, "triples %d", triples);
9998

10099
// First pass, we convert input buffer to numeric base 64 values
101100
char *in = buf + (triples - 1) * 3;
102101
char *out = buf + (triples - 1) * 4;
103102
int r = in_len % 3;
104103
int partial = 0;
105104
if (r != 0) {
106-
out[0] = (in[0] & 0xFC) >> 2;
107-
out[1] = (in[0] & 0x03) << 4;
105+
out[3] = 64;
108106
if (r == 2) {
109-
out[1] |= (in[1] & 0xF0) >> 4;
110107
out[2] = (in[1] & 0x0F) << 2;
108+
out[1] = (in[0] & 0x03) << 4 | (in[1] & 0xF0) >> 4;
111109
} else {
112110
out[2] = 64;
111+
out[1] = (in[0] & 0x03) << 4;
113112
}
114-
out[3] = 64;
113+
out[0] = (in[0] & 0xFC) >> 2;
115114
in -= 3;
116115
out -= 4;
117116
partial = 1;
118-
} else {
119-
ESP_LOGI(TAG, "no partial");
120117
}
121118
buf[encoded_len] = '\0';
122119
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;
127120
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;
129124
in -= 3;
130125
out -= 4;
131126
}
@@ -425,7 +420,10 @@ static void _process_request(socketpool_socket_obj_t *socket, _request *request)
425420
request->offset = 0;
426421
request->state = STATE_HEADER_KEY;
427422
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);
429427
} else if (strcmp(request->header_key, "Host") == 0) {
430428
ESP_LOGW(TAG, "Host header check '%s'", request->header_value);
431429
request->redirect = strcmp(request->header_value, "circuitpython.local") == 0;
@@ -567,13 +565,20 @@ static void _process_request(socketpool_socket_obj_t *socket, _request *request)
567565
while (send_offset < quantity_read) {
568566
int sent = socketpool_socket_send(socket, data_buffer + send_offset, quantity_read - send_offset);
569567
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+
}
572574
}
573575
send_offset += sent;
574576
}
575577
}
576578
ESP_LOGW(TAG, "file return done");
579+
if (total_read < total_length) {
580+
socketpool_socket_close(socket);
581+
}
577582

578583
f_close(&active_file);
579584
}

0 commit comments

Comments
 (0)