forked from maniacbug/NanodeUIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpd_fs.cpp
More file actions
executable file
·122 lines (110 loc) · 2.35 KB
/
httpd_fs.cpp
File metadata and controls
executable file
·122 lines (110 loc) · 2.35 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include <NanodeUIP.h>
#include "uip.h"
#include "httpd_fs.h"
uint16_t strncmp_PP(const char *ch1, const char *ch2, size_t len)
{
char c1=0,c2=0;
while (len--)
{
c1 = pgm_read_byte(ch1++);
c2 = pgm_read_byte(ch2++);
if ( c1 != c2 )
break;
if(!c1 && !c2)
return (0);
if(!c1 || !c2)
break;
}
return (c1 -c2);
}
struct entry
{
char* filename;
char* data;
uint16_t* len;
entry(const void* data)
{
memcpy_P(this,data,sizeof(file_entry_t));
}
bool matches(const char* name) const
{
return ( name && filename && !strncmp_P(name,filename,strlen_P(filename)) );
}
bool matches_P(const char* name) const
{
return ( name && filename && !strncmp_PP(name,filename,strlen_P(filename)) );
}
void result(struct httpd_fs_file *file) const
{
file->data = data;
file->len = pgm_read_word(len);
nanode_log_P(PSTR("http: entry result"));
nanode_log((char *)data);
}
bool isvalid(void) const
{
return filename != 0;
}
};
// Note that 'filename' might not be zero-terminated!
uint16_t httpd_fs_open(const char *filename, struct httpd_fs_file *file)
{
nanode_log_P(PSTR("http: file open"));
nanode_log((char*)filename);
uint16_t result = 0;
const file_entry_t* cur = dir;
bool done = false;
while (!done)
{
entry e(cur);
if (e.matches(filename))
{
e.result(file);
done = 1;
result = 1;
}
if (!e.isvalid())
done = 1;
++cur;
}
return result;
}
void log_Pcr(const char* str)
{
char c = pgm_read_byte(str++);
while (c && c != '\r' && c != '\n')
{
Serial.print(c);
c = pgm_read_byte(str++);
}
Serial.println();
}
uint16_t httpd_fs_open_P(const char *filename, struct httpd_fs_file *file)
{
nanode_log_P(PSTR("http: file open _P"));
log_Pcr(filename);
uint16_t result = 0;
const file_entry_t* cur = dir;
bool done = false;
while (!done)
{
entry e(cur);
if (e.matches_P(filename))
{
e.result(file);
done = 1;
result = 1;
}
if (!e.isvalid())
done = 1;
++cur;
}
return result;
}
// vim:cin:ai:sts=2 sw=2 ft=cpp