Hello everyone,
I am trying to make a webserver with a authorization login. I start with the webserver example to understand exactly what I'm doing.
I sent the "HTTP/1.0 401 Authorization Required" Header so the browser ask me to insert a login. But I can't handle with the Header the browser sent back.
So far I have a string with the http headers (String HTTP_req, in my code) and the length of string (HTTP_reqLenght, in my code).
GET / HTTP/1.1
Host: 192.168.1.210
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Authorization: Basic YWRtaW46YWRtaW4=
I am trying to use substring to compare the login but I can't got anything:
if (HTTP_req.substring(HTTP_reqLenght-16) == "YWRtaW46YWRtaW4=")
{
Serial.println("Got an OK from the server");
}
I try substring because the Authorization Header appears always at the end of all headers I think.
I try to understand this library and applied to this webserver example but it is to confuse :S
Can anyone give me a hint to do that?
The complete code:
#include <SPI.h>
#include <Ethernet.h>
#include "WebServer.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, 1, 210);
String HTTP_req; // stores the HTTP request
int HTTP_reqLenght=0;
boolean authorization=0;
// 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(115200);
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 loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
String pass=""+c;
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
HTTP_req += c; // save the HTTP request 1 char at a time
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
HTTP_reqLenght=HTTP_req.length();
Serial.print("\nHTTP REQUEST STRING\n");
Serial.println(HTTP_req);
if (HTTP_req.substring(HTTP_reqLenght-16) == "YWRtaW46YWRtaW4=")
{
Serial.println("Got an OK from the server");
}
if(authorization){
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
}else{
Unauthorized(client);
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
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 disconnected");
HTTP_req="";
}
}
void Unauthorized(EthernetClient client)
{
client.println("HTTP/1.0 401 Authorization Required");
client.println("Content-Type: text/html");
client.println("WWW-Authenticate: Basic realm=\"Private Area\"");
}