Hi!
I am issuing a GET request against a server component running on my Arduino Mega. The request is a simple test request: http://192.168.0.90?q1=v1&q2=v2
When I simply output what is received via
char cha;
while (cha != '-1') {
cha = server.read();
Serial.print(cha);
}
I get what I expect:
GET /?q1=v1 HTTP/1.1
Host: 192.168.0.90
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en,en-us;q=0.9,sl;q=0.8,pt-br;q=0.7,pt;q=0.6,de;q=0.5,ca;q=0.4,he;q=0.3,ar;q=0.2,ja;q=0.1
Accept-Encoding: gzip, deflate
Connection: keep-alive
I am actually only interested in the 1st line.
So, my plan was to:
- Checking whether it is a GET, POST or whatever Request
- Checking which resource was adresses (/ in this case)
- Parsing the query parameters
This is the code:
void loop()
{
char method[6], resource[32], param[64], *ptr, *p1, *p2, *p3;
int len;
int c;
if(server.available())
{
method[0] = 0;
server.read((uint8_t*)method, 4);
debugoutln("Determining type of request...");
if(strncmp_P(method, PSTR("GET "), 4) == 0)
{
debugoutln("GET");
//get addressed resource
resource[0] = 0;
ptr = resource;
do
{
c = server.read();
if(isalnum(c) || (c == '/'))
{
*ptr++ = c;
*ptr = 0;
}
Serial.print(c);
if (c == ' ') {
debugoutln("BLANK");
}
if (c == '?') {
debugoutln("QM ?");
}
if (c == -1) {
debugoutln("Error");
}
}
while((c != ' ') && (c != '?') && (c != -1)); // stop when we have read the addressed reasource
debugoutln("Determining parameters specifiying commands...");
I never receive anything other than a -1 in the do-while loop. So the GET is detected, which means I have read GET (+1 blank).
But then, after having arrived in the do-while loop I immediately get -1 when doing a server.read(). I would have expected to get a /, then a ? (which would let the loop end).
This is the output I see in the serial monitor:
Determining type of request...
GET
-1Error
Determining parameters specifiying commands...
Any idea?
I am usually coding Java and PHP, so it might be a stupid C issue...
Thanks!