Hi everybody.
I want to securise the acces to my server with a login/password
I found on the forum a sketch working fine.
But I wanted to add a button :
client.println("<a href=\"/?TEST\"\">TEST</a>");
and a condition on that button:
if (readString.indexOf("?TEST") > 0) {
Serial.print("It works Fine !!!");// Doesn't works!!!
}
It doesn't works...
I have no problem with that if I don't want securise my server.
I tried to put the condition at differents places in the sketch: same problem
Someone as an idea?
Please, find my sketch below ( Login= Patrick Password= Homidom)
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0, 111);
String readString;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void SendOKpage(EthernetClient &client)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<a href=\"/?TEST\"\">TEST</a>");
client.println("</html>");
if (readString.indexOf("?TEST") > 0) {
Serial.print("It works Fine !!!");// Doesn't works!!!
}
}
void SendAuthentificationpage(EthernetClient &client)
{
client.println("HTTP/1.1 401 Authorization Required");
client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<HTML> <HEAD> <TITLE>Error</TITLE>");
client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}
char linebuf[80];
int charcount=0;
boolean authentificated=false;
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
authentificated=false;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
if (authentificated)
SendOKpage(client);
else
SendAuthentificationpage(client);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"UGF0cmljazpIb21pZG9t")>0)// Patrick Homidom
authentificated=true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}