Single instance HTTP Server using WiFly Interface.
Dependents: WiFlyHTTPServerSample MultiThreadingHTTPServer
This is my implementation for a HTTP Server using the WiFly Interface. Please note that this is still under development.
It may still contain several bugs. I have tested it using a 1768 on an application board plus RN-XV board.
Currently there is only a FileSystem implemented. Also it is limited to GET request.
I try to extend it further so it will be more useful.
Btw, it does NOT work with RTOS, which seems not to be the Problem of my library.
Do not Forget to Import the WiFly Interface into your Project when using this library.
Change History:
REV5: - added support for basic RPC GET request functionality.
REV4: - added argument parsing from the request uri. - documentation extended and updated.
Diff: HTTPConnection.cpp
- Revision:
- 2:8653bbcf7e58
- Parent:
- 1:6b7472d5e9ee
- Child:
- 3:d6224049b3bf
diff -r 6b7472d5e9ee -r 8653bbcf7e58 HTTPConnection.cpp --- a/HTTPConnection.cpp Sun May 26 22:49:42 2013 +0000 +++ b/HTTPConnection.cpp Sun May 26 23:22:36 2013 +0000 @@ -8,7 +8,7 @@ using std::string; -#if (0 && !defined(TARGET_LPC11U24)) +#if (1 && !defined(TARGET_LPC11U24)) #define INFO(x, ...) std::printf("[HttpConnection : INFO]"x"\r\n", ##__VA_ARGS__); #define WARN(x, ...) std::printf("[HttpConnection : WARN]"x"\r\n", ##__VA_ARGS__); #define ERR(x, ...) std::printf("[HttpConnection : ERR]"x"\r\n", ##__VA_ARGS__); @@ -39,7 +39,6 @@ int HTTPConnection::poll() { static char buffer[256] = {}; - static char echoHeader[256] = {}; int rcvd= 0; @@ -58,9 +57,8 @@ if (rcvd == -1) { // Invalid content received, so close the connection INFO("Invalid message received, so sending negative response and closing connection !"); - sprintf(echoHeader,"HTTP/1.1 400 NOK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer)); + sprintf(buffer,"HTTP/1.1 400 NOK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",0); m_Tcp.set_blocking(true, 1500); - m_Tcp.send(echoHeader,strlen(echoHeader)); m_Tcp.send(buffer,strlen(buffer)); close(); rcvd = -1; @@ -85,17 +83,6 @@ } } } - if (rcvd == 0) { -// sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer)); -// m_Tcp.set_blocking(true); -// m_Tcp.send_all(echoHeader,strlen(echoHeader)); -// m_Tcp.send_all(buffer,strlen(buffer)); - - /// INSERT PRCESSING OF REQUESST HERE - /// END OF PROCESSING REQUEST - - // Do not close the connection, it may be reused - } INFO("Leaving poll function!"); return rcvd; } @@ -147,7 +134,7 @@ return i; } -int HTTPConnection::parse(const char* buffer) +int HTTPConnection::parse(char* buffer) { if ((buffer == NULL) || (strlen(buffer) < 4)) { ERR("Buffer content is invalid or too short."); @@ -159,14 +146,13 @@ // decompose string into a list of arguments char s = 0; // current starting char - static char buff[255] = {}; - for (int i = 0 ; i < strlen(buffer)+1 ; i++) { + int nLen = strlen(buffer)+1; + for (int i = 0 ; i < nLen ; i++) { if ((buffer[i] == ' ') || (buffer[i] == '\n') || (buffer[i] == 0)) { // new arg found - strncpy(buff, &buffer[s], i-s); - buff[i-s] = 0; - INFO("Found argument \"%s\"", buff); - args.push_back(std::string(buff)); + buffer[i] = 0; + INFO("Found argument \"%s\"", &buffer[s]); + args.push_back(&buffer[s]); s = i+1; } }