[Solved] How to process multi-frame HTTP requests? (W5100 + 2560 + Ethernet lib)
hi!
i writing http server , webpage , 1 of it's features receive put or post request , write body of request file. problem: writes first ethernet frame's content file, ethernetclient library says there no more data (client.available() false).
the server can send multi-frame responses without problems.
i tried pre-written http server library (webduino), works same.
so questions are: causes problem? (my code?/the ethernet library?/the ethernet shield?/something entirely else?)
what can make work? have make workaround? (i avoid that)
hardware: arduino mega 2560 + ethernet shield (with w5100)
libraries: ethernet, sdfat
code of server:
thank reading, appreciated!
g.
i writing http server , webpage , 1 of it's features receive put or post request , write body of request file. problem: writes first ethernet frame's content file, ethernetclient library says there no more data (client.available() false).
the server can send multi-frame responses without problems.
i tried pre-written http server library (webduino), works same.
so questions are: causes problem? (my code?/the ethernet library?/the ethernet shield?/something entirely else?)
what can make work? have make workaround? (i avoid that)
hardware: arduino mega 2560 + ethernet shield (with w5100)
libraries: ethernet, sdfat
code of server:
code: [select]
#include <ethernet.h>
#include <sdfat.h>
const byte macaddress[] = { 0x02, 0x52, 0x3b, 0xe1, 0xca, 0x19 };
const ipaddress ipaddressofserver(192, 168, 1, 5);
const ipaddress networkmask(255, 255, 255, 255);
const ipaddress defaultgateway(192, 168, 1, 1);
const ipaddress dnsserver(8, 8, 8, 8);
ethernetserver server(80);
ethernetclient client;
sdfat sd;
file file; // ok
void processrequest()
{
// example:
// /asd.txt http/1.1
// "get"
char method[5];
uint16_t index = 0;
char temp = client.read();
while (index < 4 && temp != ' ') {
method[index++] = temp;
temp = client.read();
}
method[index] = '\0';
// "/asd.txt"
char url[32];
index = 0;
temp = client.read();
while (index < 31 && temp != ' ') {
url[index++] = temp;
temp = client.read();
}
url[index] = '\0';
// if client wants read file
if (!strcmp(method, "get")) {
serial.println("get!");
if (sd.exists(url)) {
// file requested present
client.println("http/1.1 200 ok");
client.println("content - type: text/plain\n");
// send file client
file = sd.open(url);
while (file.available()) {
client.write(file.read());
}
file.close();
}
else {
// file not present
// send 404 client
client.println("http/1.1 404 not found");
client.println("content - type: text/plain\n");
client.print("<h1>404 :(</h1>");
}
}
// if client wants put (rewrite/create) file
// @ point write of http request file, that's ok now
if (!strcmp(method, "post"))
{
serial.println("post!");
// if file exists, open it
if (sd.exists(url)) {
file = sd.open(url, o_write);
// , overwrite it
while (client.available()) {
file.write(client.read());
}
// truncate it
file.truncate(file.position());
// close it
file.close();
// send success message
client.println("http/1.1 200 ok");
client.println("content - type: text/plain\n");
// , content
client.print("ok");
}
// otherwise send fail message
else {
client.println("http/1.1 404 not found");
client.println("content - type: text/plain\n");
client.print("<h1>404 :(</h1>");
}
}
}
void setup() {
serial.begin(921600);
ethernet.begin(macaddress, ipaddressofserver,
dnsserver, defaultgateway, networkmask);
sd.begin(4);
server.begin();
serial.println("started!");
delay(100); // give stuff time ready
}
void loop() {
client = server.available();
if (client) {
serial.println("incoming request!");
processrequest();
client.stop();
}
delay(100);
}
thank reading, appreciated!
g.
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > [Solved] How to process multi-frame HTTP requests? (W5100 + 2560 + Ethernet lib)
arduino
Comments
Post a Comment