Skip to content

SDWebserver.ino example produces invalid json and is dog-slow #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sunnyhighway opened this issue Jun 6, 2015 · 1 comment
Closed

Comments

@sunnyhighway
Copy link

printDirectory() in the SDWebserver.ino example produces invalid json.

When a directory contains no files or directories, printDirectory produces only the end of array marker:

]

Better to produce an empty array:

[]

server.sendContent() appears to be dog-slow.
server.send() is faster by at least 1 second.

Proposal:

void printDirectory() {
  if(!server.hasArg("dir")) return returnFail("BAD ARGS");
  String path = server.arg("dir");
  if(path != "/" && !SD.exists((char *)path.c_str())) return returnFail("BAD PATH");
  File dir = SD.open((char *)path.c_str());
  path = String();
  if(!dir.isDirectory()){
    dir.close();
    return returnFail("NOT DIR");
  }
  dir.rewindDirectory();
  WiFiClient client = server.client();

  String output = "[";
  for (int cnt = 0; true; ++cnt) {
    File entry = dir.openNextFile();
    if (!entry)
    break;

    if (cnt > 0) 
      output += ',';

    output += "{\"type\":\"";
    output += (entry.isDirectory()) ? "dir" : "file";
    output += "\",\"name\":\"";
    output += entry.name();
    output += "\"";
    output += "}";
    entry.close();
  }

  output += "]";
  server.send(200, "text/json", output);
  dir.close();
}
@me-no-dev
Copy link
Collaborator

#406

what you suggest will break if folder has many files and content get's large.

@igrr igrr closed this as completed Jul 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants