Decided I had to fix it myself. Not complete but somewhat working.
/*--------------------------------------------------------------
Program: Webserve from SD w/images
Description: Arduino web server that serves up web pages and images.
Hardware: Arduino Uno and official Arduino Ethernet
shield. Should work with other Arduinos and
compatible Ethernet shields.
2Gb or larger micro SD card formatted FAT16/32
Software: Developed using Arduino 1.6.5 software
Should be compatible with Arduino 1.0 +
Requires index.htm, page2.htm and pic.jpg to be
on the micro SD card in the Ethernet shield
micro SD card socket.
References: - WebServer example by David A. Mellis and
modified by Tom Igoe
- SD card examples by David A. Mellis and
Tom Igoe
- Ethernet library documentation:
http://arduino.cc/en/Reference/Ethernet
- SD Card library documentation:
http://arduino.cc/en/Reference/SD
Date: 7 March 2013
Modified: 17 June 2013
17 Aug 2015 MDM
Author:
Basics: W.A. Smith, http://startingelectronics.org
Expanded function: Mike Morrow http://ILikeTheInternet.com
--------------------------------------------------------------*/
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 120
#define MAX_TO_SAVE REQ_BUF_SZ-1
#define FN_BUF_SZ REQ_BUF_SZ-4
// MAC address from Ethernet shield sticker under board
byte MAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress MyDNS(96, 92, 96, 26); // the DNS server IP
IPAddress MyIP(96, 92, 96, 29); // Assigned IP address
IPAddress Gateway(96, 92, 96, 30); // Router's gateway address
IPAddress Subnet(255, 255, 255, 248); // Subnet mask
EthernetServer server(80); // create a server on port 80
File webFile;
char c;
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
char index_htm[10] = "index.htm";
char Reqd_File[FN_BUF_SZ] = {0};
char Filename_Index;
boolean Filename_Found = false;
boolean currentLineIsBlank;
byte buf[512]; // Place to read input file chunks
void setup()
{
//disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.begin(57600); // for debugging
//initialize SD card
Serial.println(F("Initializing SD card..."));
if (!SD.begin(4)) {
Serial.println(F("ERROR - SD card initialization failed!"));
return; // init failed
}
Serial.println(F("SUCCESS - SD card initialized."));
//check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println(F("ERROR - Can't find index.htm file!"));
return; // can't find index file
}
Serial.println(F("SUCCESS - Found index.htm file."));
//Ethernet.begin(MAC, MyIP, MyDNS, Gateway, Subnet); // Use all parms
Serial.println(F("\nNow trying to connect Ethernet"));
if (Ethernet.begin(MAC) == 0)
Serial.println(F("Failed to configure Ethernet using DHCP."));
else
Serial.println(F("DHCP successful"));
Ethernet.begin(MAC); // initialize Ethernet device with DHCP
server.begin(); // start to listen for clients
delay(100);
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
currentLineIsBlank = true;
Filename_Found = false;
while (client.connected()) {
if (client.available()) { // client data available to read
c = client.read(); // read 1 byte (character) from client
//buffer first part of HTTP request in HTTP_req array (string)
//Because only the buffersize (REQ_BUF_SZ) -1 is kept, there can be no overflow
//HEY MICROSOFT!!! LISTENING?!?!?!?!?!?!?!
//leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (MAX_TO_SAVE)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++; // Bump to next array location.
//print HTTP request character to serial monitor
//Serial.print(c);
//last line of client request is blank and ends with \n
//respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
//open requested web page file
if (StrMatchLeft(HTTP_req, "GET /")) { // || StrMatchLeft(HTTP_req, "GET /index.htm")) {
Serial.println(F("Found GET / command."));
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
memcpy(Reqd_File, &HTTP_req[4], FN_BUF_SZ);
Filename_Index = FindCharAfter(Reqd_File," ",0); // Starting in array location 0, find first
Serial.print(F("Ending space "));
if (Filename_Index == 0) Serial.print(F("not "));
Reqd_File[Filename_Index] = '\0';
Serial.println(Reqd_File);
if (Filename_Index == 1) {
StrClear(Reqd_File,FN_BUF_SZ);
memcpy(Reqd_File,index_htm,sizeof(index_htm));
}
webFile = SD.open(Reqd_File); // open web page file
}
if (webFile) {
Serial.print(F("Reading file: "));
Serial.println(Reqd_File);
while (1) {
int n = webFile.available();
if (n == 0) break;
if (n > 512) n = 512;
webFile.read(buf, n);
client.write(buf, n);
}
webFile.close();
}
// reset buffer index and all buffer elements to 0
req_index = 0;
Filename_Index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
StrClear(Reqd_File, REQ_BUF_SZ);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
//last character on line of received text
//starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') { // a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) str[i] = 0;
}
// Search Haystack for Needle starting in position 1 only. If no match before the length of Needle, return 0.
// If there is a match for the length of Needle, then we declare a match.
char StrMatchLeft(char *Haystack, char *Needle)
{
char HaystackPtr = 0;
char NeedlePtr = 0;
char HaystackLen;
HaystackLen = strlen(Haystack); // length the string being searched
if (strlen(Needle) > HaystackLen) return 0; // is sought longer than searched? That's wrong! Can't be a match.
while (HaystackPtr < HaystackLen) { // search whole length of searched string
if (Haystack[HaystackPtr] == Needle[NeedlePtr]) { // if characters are equal,
NeedlePtr++; // more to next sought byte
if (strlen(Needle) == NeedlePtr) return 1; // if we have matches for the entore length of sought, we are done.
} else {
return 0; // got a partial match but it did not last for the runlength of sought. Return no match.
}
HaystackPtr++;
}
return 0; // Should never get here. Maybe... Right? Either we exited with mismatch or Needle length ran out. Right???
}
char FindCharAfter(char *Haystack, char *Needle, char StartByte)
{
char HaystackPtr = StartByte;
char HaystackLen;
HaystackLen = strlen(Haystack); // length the string being searched
if (HaystackLen < 1) return 0; // Can't be too short
while (HaystackPtr < HaystackLen) {
if (Haystack[HaystackPtr] == Needle[0]) return HaystackPtr;
HaystackPtr++;
}
}