Hi,
I recently got an Ethernet module and wanted to make a basic web server. After browsing a couple of the Ethernet and SD examples I got it working with GET requests. However, since adding support for multiple files (like getting the index page, then the css page etc...) I've come across (what I think is) a pointer problem.
I must admit I am not too clued up on pointers, so any help would be appreciated. I did try researching my problem, but as I'm not entirely sure what the problem is, I didn't get very far.
This is the problematic function:
char *filepath;
char *filename;
char *extension;
...
void handleGet(char *filepath) {
if(!strcmp(filepath, "/")) {
filepath = "index.htm";
}
Serial.println(filepath);
if(SD.exists(filepath)) {
contentFile = SD.open(filepath);
filename = strtok(filepath, ".");
extension = strtok(NULL, ".");
if(!strcmp(extension, "css")) {
type = "text/css";
} else if(!strcmp(extension, "htm")) {
type = "text/html";
} else if(!strcmp(extension, "txt")) {
type = "text/plain";
}
if(contentFile) {
client.println("HTTP/1.1 200 OK");
client.print("Content-Type: ");
client.println(type);
client.println("Connection: close");
client.println();
while(contentFile.available()) {
client.print((char)contentFile.read());
}
} else {
handle404();
}
contentFile.close();
} else {
handle404();
}
}
The problem comes when loading the index.htm file for the second time.
Initially it works, the index.htm file and the style.css file are served and everything works fine.
However, when I go to refresh the page, the 'filepath' variable is set to "index" instead of "index.htm" (this is done on the second line). I thought this was probably a problem with reusing a pointer, but I'm not sure.
If any further information is needed I am more than willing to provide.
Any ideas what the problem might be?
Thanks.