forked from solusipse/ureq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ureq_filesystem.c
107 lines (83 loc) · 2.43 KB
/
ureq_filesystem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
// TODO: move all defines to one file
#define UREQ_FS_START 0x12000
void memcpyAligned(char *dst, char *src, int len) {
// This function comes from dht-esp8266 project
// It was written by Jeroen Domburg <[email protected]>
int x;
int w, b;
for (x=0; x<len; x++) {
b=((int)src&3);
w=*((int *)(src-b));
if (b==0) *dst=(w>>0);
if (b==1) *dst=(w>>8);
if (b==2) *dst=(w>>16);
if (b==3) *dst=(w>>24);
dst++; src++;
}
}
static char *ureq_fs_read(int a, int s, char *buf) {
char *pos = (char*) UREQ_FS_START + 0x40200000;
pos += a;
memset(buf, 0, 1024);
memcpyAligned(buf, pos, s);
return buf;
}
UreqFile ureq_fs_open(char *rf) {
char *pos = (char*) UREQ_FS_START + 0x40200000;
int32_t amount;
char name[16];
int32_t size;
int32_t address;
UreqFile f;
f.size = 0;
f.address = 0;
// Get number of files
os_memcpy(&amount, pos, sizeof(amount));
//os_printf("Number of files: %d\n", amount);
// Move to the filesystem header
pos += sizeof(int32_t);
int i;
for (i = 0; i < amount; i++) {
memset(name, 0, sizeof(name));
os_memcpy(name, pos, sizeof(name));
if (strcmp(name, rf) == 0) {
// Requested file was found
pos += sizeof(char) * 16;
int size, address;
os_memcpy(&size, pos, sizeof(int32_t));
pos += sizeof(int32_t);
os_memcpy(&address, pos, sizeof(int32_t));
f.size = size;
f.address = address;
return f;
} else {
// Move to next file
pos += sizeof(char) * 16; // filename
pos += sizeof(int32_t); // size
pos += sizeof(int32_t); // address
}
}
return f;
}
int ureq_fs_first_run(HttpRequest *r) {
// If there's no function bound to /, then try
// to read index.html
UreqFile f;
if ( strcmp(r->url, "/") == 0)
f = ureq_fs_open("index.html");
else
f = ureq_fs_open(r->url + 1);
if (f.address == 0) {
// File was not found
return ureq_set_404_response(r);
}
r->bigFile = 1;
r->complete = -2;
if (r->response.code == 0)
r->response.code = 200;
r->file = f;
r->response.data = ureq_generate_response_header(r);
r->len = strlen(r->response.data);
return r->complete;
}