34
34
#include < SPI.h>
35
35
#include < SD.h>
36
36
37
- #define WWW_BUF_SIZE 1460
38
37
#define DBG_OUTPUT_PORT Serial
39
38
40
39
const char * ssid = " **********" ;
@@ -47,28 +46,17 @@ ESP8266WebServer server(80);
47
46
static bool hasSD = false ;
48
47
File uploadFile;
49
48
50
- void returnOK (){
51
- WiFiClient client = server.client ();
52
- String message = " HTTP/1.1 200 OK\r\n " ;
53
- message += " Content-Type: text/plain\r\n " ;
54
- message += " Connection: close\r\n " ;
55
- message += " Access-Control-Allow-Origin: *\r\n " ;
56
- message += " \r\n " ;
57
- client.print (message);
58
- client.stop ();
49
+
50
+ void returnOK () {
51
+ server.sendHeader (" Connection" , " close" );
52
+ server.sendHeader (" Access-Control-Allow-Origin" , " *" );
53
+ server.send (200 , " text/plain" , " " );
59
54
}
60
55
61
- void returnFail (String msg){
62
- WiFiClient client = server.client ();
63
- String message = " HTTP/1.1 500 Fail\r\n " ;
64
- message += " Content-Type: text/plain\r\n " ;
65
- message += " Connection: close\r\n " ;
66
- message += " Access-Control-Allow-Origin: *\r\n " ;
67
- message += " \r\n " ;
68
- message += msg;
69
- message += " \r\n " ;
70
- client.print (message);
71
- client.stop ();
56
+ void returnFail (String msg) {
57
+ server.sendHeader (" Connection" , " close" );
58
+ server.sendHeader (" Access-Control-Allow-Origin" , " *" );
59
+ server.send (500 , " text/plain" , msg + " \r\n " );
72
60
}
73
61
74
62
bool loadFromSdCard (String path){
@@ -93,59 +81,40 @@ bool loadFromSdCard(String path){
93
81
dataType = " text/html" ;
94
82
dataFile = SD.open (path.c_str ());
95
83
}
84
+
85
+ if (!dataFile)
86
+ return false ;
96
87
97
88
if (server.hasArg (" download" )) dataType = " application/octet-stream" ;
98
89
99
- if (dataFile) {
100
- WiFiClient client = server.client ();
101
- String head = " HTTP/1.1 200 OK\r\n Content-Type: " ;
102
- head += dataType;
103
- head += " \r\n Content-Length: " ;
104
- head += dataFile.size ();
105
- head += " \r\n Connection: close" ;
106
- head += " \r\n Access-Control-Allow-Origin: *" ;
107
- head += " \r\n\r\n " ;
108
- client.print (head);
109
- dataType = String ();
110
- path = String ();
111
-
112
- uint8_t obuf[WWW_BUF_SIZE];
113
-
114
- while (dataFile.available () > WWW_BUF_SIZE){
115
- dataFile.read (obuf, WWW_BUF_SIZE);
116
- if (client.write (obuf, WWW_BUF_SIZE) != WWW_BUF_SIZE){
117
- DBG_OUTPUT_PORT.println (" Sent less data than expected!" );
118
- dataFile.close ();
119
- return true ;
120
- }
121
- }
122
- uint16_t leftLen = dataFile.available ();
123
- dataFile.read (obuf, leftLen);
124
- if (client.write (obuf, leftLen) != leftLen){
125
- DBG_OUTPUT_PORT.println (" Sent less data than expected!" );
126
- dataFile.close ();
127
- return true ;
128
- }
129
- dataFile.close ();
130
- client.stop ();
131
- return true ;
90
+ server.sendHeader (" Content-Length" , String (dataFile.size ()));
91
+ server.sendHeader (" Connection" , " close" );
92
+ server.sendHeader (" Access-Control-Allow-Origin" , " *" );
93
+ server.send (200 , dataType.c_str (), " " );
94
+
95
+ WiFiClient client = server.client ();
96
+ size_t totalSize = dataFile.size ();
97
+ if (client.write (dataFile, PAYLOAD_UNIT_SIZE) != totalSize) {
98
+ DBG_OUTPUT_PORT.println (" Sent less data than expected!" );
132
99
}
133
- return false ;
100
+
101
+ dataFile.close ();
102
+ return true ;
134
103
}
135
104
136
105
void handleFileUpload (){
137
106
if (server.uri () != " /edit" ) return ;
138
- HTTPUpload upload = server.upload ();
107
+ HTTPUpload& upload = server.upload ();
139
108
if (upload.status == UPLOAD_FILE_START){
140
109
if (SD.exists ((char *)upload.filename .c_str ())) SD.remove ((char *)upload.filename .c_str ());
141
110
uploadFile = SD.open (upload.filename .c_str (), FILE_WRITE);
142
111
DBG_OUTPUT_PORT.print (" Upload: START, filename: " ); DBG_OUTPUT_PORT.println (upload.filename );
143
112
} else if (upload.status == UPLOAD_FILE_WRITE){
144
- if (uploadFile) uploadFile.write (upload.buf , upload.buflen );
145
- DBG_OUTPUT_PORT.print (" Upload: WRITE, Bytes: " ); DBG_OUTPUT_PORT.println (upload.buflen );
113
+ if (uploadFile) uploadFile.write (upload.buf , upload.currentSize );
114
+ DBG_OUTPUT_PORT.print (" Upload: WRITE, Bytes: " ); DBG_OUTPUT_PORT.println (upload.currentSize );
146
115
} else if (upload.status == UPLOAD_FILE_END){
147
116
if (uploadFile) uploadFile.close ();
148
- DBG_OUTPUT_PORT.print (" Upload: END, Size: " ); DBG_OUTPUT_PORT.println (upload.size );
117
+ DBG_OUTPUT_PORT.print (" Upload: END, Size: " ); DBG_OUTPUT_PORT.println (upload.totalSize );
149
118
}
150
119
}
151
120
@@ -156,41 +125,45 @@ void deleteRecursive(String path){
156
125
SD.remove ((char *)path.c_str ());
157
126
return ;
158
127
}
128
+
159
129
file.rewindDirectory ();
160
- File entry;
161
- String entryPath;
162
130
while (true ) {
163
- entry = file.openNextFile ();
131
+ File entry = file.openNextFile ();
164
132
if (!entry) break ;
165
- entryPath = path + " /" +entry.name ();
133
+ String entryPath = path + " /" +entry.name ();
166
134
if (entry.isDirectory ()){
167
135
entry.close ();
168
136
deleteRecursive (entryPath);
169
137
} else {
170
138
entry.close ();
171
139
SD.remove ((char *)entryPath.c_str ());
172
140
}
173
- entryPath = String ();
174
141
yield ();
175
142
}
143
+
176
144
SD.rmdir ((char *)path.c_str ());
177
- path = String ();
178
145
file.close ();
179
146
}
180
147
181
148
void handleDelete (){
182
149
if (server.args () == 0 ) return returnFail (" BAD ARGS" );
183
150
String path = server.arg (0 );
184
- if (path == " /" || !SD.exists ((char *)path.c_str ())) return returnFail (" BAD PATH" );
151
+ if (path == " /" || !SD.exists ((char *)path.c_str ())) {
152
+ returnFail (" BAD PATH" );
153
+ return ;
154
+ }
185
155
deleteRecursive (path);
186
156
returnOK ();
187
- path = String ();
188
157
}
189
158
190
159
void handleCreate (){
191
160
if (server.args () == 0 ) return returnFail (" BAD ARGS" );
192
161
String path = server.arg (0 );
193
- if (path == " /" || SD.exists ((char *)path.c_str ())) return returnFail (" BAD PATH" );
162
+ if (path == " /" || SD.exists ((char *)path.c_str ())) {
163
+ returnFail (" BAD PATH" );
164
+ return ;
165
+ }
166
+
194
167
if (path.indexOf (' .' ) > 0 ){
195
168
File file = SD.open ((char *)path.c_str (), FILE_WRITE);
196
169
if (file){
@@ -201,7 +174,6 @@ void handleCreate(){
201
174
SD.mkdir ((char *)path.c_str ());
202
175
}
203
176
returnOK ();
204
- path = String ();
205
177
}
206
178
207
179
void printDirectory () {
@@ -216,31 +188,31 @@ void printDirectory() {
216
188
}
217
189
dir.rewindDirectory ();
218
190
219
- File entry ;
191
+ server. send ( 200 , " text/json " , " " ) ;
220
192
WiFiClient client = server.client ();
221
- client.print (" HTTP/1.1 200 OK\r\n Content-Type: text/json\r\n\r\n " );
222
- String output = " [" ;
223
- while (true ) {
224
- entry = dir.openNextFile ();
225
- if (!entry) break ;
226
- if (output != " [" ) output += ' ,' ;
227
- output += " {\" type\" :\" " ;
228
- output += (entry.isDirectory ())?" dir" :" file" ;
229
- output += " \" ,\" name\" :\" " ;
230
- output += entry.name ();
231
- output += " \" " ;
232
- output += " }" ;
233
- entry.close ();
234
- if (output.length () > 1460 ){
235
- client.write (output.substring (0 , 1460 ).c_str (), 1460 );
236
- output = output.substring (1460 );
237
- }
193
+
194
+ for (int cnt = 0 ; true ; ++cnt) {
195
+ File entry = dir.openNextFile ();
196
+ if (!entry)
197
+ break ;
198
+
199
+ String output;
200
+ if (cnt == 0 )
201
+ output = ' [' ;
202
+ else
203
+ output = ' ,' ;
204
+
205
+ output += " {\" type\" :\" " ;
206
+ output += (entry.isDirectory ()) ? " dir" : " file" ;
207
+ output += " \" ,\" name\" :\" " ;
208
+ output += entry.name ();
209
+ output += " \" " ;
210
+ output += " }" ;
211
+ server.sendContent (output);
212
+ entry.close ();
238
213
}
214
+ server.sendContent (" ]" );
239
215
dir.close ();
240
- output += " ]" ;
241
- client.write (output.c_str (), output.length ());
242
- client.stop ();
243
- output = String ();
244
216
}
245
217
246
218
void handleNotFound (){
@@ -280,14 +252,14 @@ void setup(void){
280
252
}
281
253
DBG_OUTPUT_PORT.print (" Connected! IP address: " );
282
254
DBG_OUTPUT_PORT.println (WiFi.localIP ());
283
- /*
255
+
284
256
if (mdns.begin (hostname, WiFi.localIP ())) {
285
257
DBG_OUTPUT_PORT.println (" MDNS responder started" );
286
258
DBG_OUTPUT_PORT.print (" You can now connect to http://" );
287
259
DBG_OUTPUT_PORT.print (hostname);
288
260
DBG_OUTPUT_PORT.println (" .local" );
289
261
}
290
- */
262
+
291
263
292
264
server.on (" /list" , HTTP_GET, printDirectory);
293
265
server.on (" /edit" , HTTP_DELETE, handleDelete);
@@ -304,7 +276,7 @@ void setup(void){
304
276
hasSD = true ;
305
277
}
306
278
}
307
-
279
+
308
280
void loop (void ){
309
281
server.handleClient ();
310
282
}
0 commit comments