3434#include < SPI.h>
3535#include < SD.h>
3636
37- #define WWW_BUF_SIZE 1460
3837#define DBG_OUTPUT_PORT Serial
3938
4039const char * ssid = " **********" ;
@@ -47,28 +46,17 @@ ESP8266WebServer server(80);
4746static bool hasSD = false ;
4847File uploadFile;
4948
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" , " " );
5954}
6055
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 " );
7260}
7361
7462bool loadFromSdCard (String path){
@@ -93,59 +81,40 @@ bool loadFromSdCard(String path){
9381 dataType = " text/html" ;
9482 dataFile = SD .open (path.c_str ());
9583 }
84+
85+ if (!dataFile)
86+ return false ;
9687
9788 if (server.hasArg (" download" )) dataType = " application/octet-stream" ;
9889
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!" );
13299 }
133- return false ;
100+
101+ dataFile.close ();
102+ return true ;
134103}
135104
136105void handleFileUpload (){
137106 if (server.uri () != " /edit" ) return ;
138- HTTPUpload upload = server.upload ();
107+ HTTPUpload& upload = server.upload ();
139108 if (upload.status == UPLOAD_FILE_START ){
140109 if (SD .exists ((char *)upload.filename .c_str ())) SD .remove ((char *)upload.filename .c_str ());
141110 uploadFile = SD .open (upload.filename .c_str (), FILE_WRITE );
142111 DBG_OUTPUT_PORT .print (" Upload: START, filename: " ); DBG_OUTPUT_PORT .println (upload.filename );
143112 } 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 );
146115 } else if (upload.status == UPLOAD_FILE_END ){
147116 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 );
149118 }
150119}
151120
@@ -156,41 +125,45 @@ void deleteRecursive(String path){
156125 SD .remove ((char *)path.c_str ());
157126 return ;
158127 }
128+
159129 file.rewindDirectory ();
160- File entry;
161- String entryPath;
162130 while (true ) {
163- entry = file.openNextFile ();
131+ File entry = file.openNextFile ();
164132 if (!entry) break ;
165- entryPath = path + " /" +entry.name ();
133+ String entryPath = path + " /" +entry.name ();
166134 if (entry.isDirectory ()){
167135 entry.close ();
168136 deleteRecursive (entryPath);
169137 } else {
170138 entry.close ();
171139 SD .remove ((char *)entryPath.c_str ());
172140 }
173- entryPath = String ();
174141 yield ();
175142 }
143+
176144 SD .rmdir ((char *)path.c_str ());
177- path = String ();
178145 file.close ();
179146}
180147
181148void handleDelete (){
182149 if (server.args () == 0 ) return returnFail (" BAD ARGS" );
183150 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+ }
185155 deleteRecursive (path);
186156 returnOK ();
187- path = String ();
188157}
189158
190159void handleCreate (){
191160 if (server.args () == 0 ) return returnFail (" BAD ARGS" );
192161 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+
194167 if (path.indexOf (' .' ) > 0 ){
195168 File file = SD .open ((char *)path.c_str (), FILE_WRITE );
196169 if (file){
@@ -201,7 +174,6 @@ void handleCreate(){
201174 SD .mkdir ((char *)path.c_str ());
202175 }
203176 returnOK ();
204- path = String ();
205177}
206178
207179void printDirectory () {
@@ -216,31 +188,31 @@ void printDirectory() {
216188 }
217189 dir.rewindDirectory ();
218190
219- File entry ;
191+ server. send ( 200 , " text/json " , " " ) ;
220192 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 ();
238213 }
214+ server.sendContent (" ]" );
239215 dir.close ();
240- output += " ]" ;
241- client.write (output.c_str (), output.length ());
242- client.stop ();
243- output = String ();
244216}
245217
246218void handleNotFound (){
@@ -280,14 +252,14 @@ void setup(void){
280252 }
281253 DBG_OUTPUT_PORT .print (" Connected! IP address: " );
282254 DBG_OUTPUT_PORT .println (WiFi.localIP ());
283- /*
255+
284256 if (mdns.begin (hostname, WiFi.localIP ())) {
285257 DBG_OUTPUT_PORT .println (" MDNS responder started" );
286258 DBG_OUTPUT_PORT .print (" You can now connect to http://" );
287259 DBG_OUTPUT_PORT .print (hostname);
288260 DBG_OUTPUT_PORT .println (" .local" );
289261 }
290- */
262+
291263
292264 server.on (" /list" , HTTP_GET , printDirectory);
293265 server.on (" /edit" , HTTP_DELETE , handleDelete);
@@ -304,7 +276,7 @@ void setup(void){
304276 hasSD = true ;
305277 }
306278}
307-
279+
308280void loop (void ){
309281 server.handleClient ();
310282}
0 commit comments