The library with which to make your mbed a HTTP Server which just reads HTML files in the mbed and sends it to the clients.
Dependents: httpserversample SIMPLE_WSS
Quote:
This library depends on EthernetInterface class, TCPSocketServer class and TCPSocketConnection class. These classes can be imported at EthernetInterface library. Moreover, the EthernetInterface library depends on the mbed-rtos library, so import it here.
Abstract
This is the library to make a mbed a simple HTTP server. With this library, a mbed can understand only GET requests, and can send clients htm or jpg files as a response.
Handleable Requests:
- GET
Handleable files:
- html
- jpg
Note the length of the Filename
A mbed can handle only 8.3 filename (also called as "short filename" or SFN), such as index.htm (in this case, length of the filename is 5.3).
Sample Code
Running the code below, if your mbed is connected to a network, you can see an IP address of the mbed on your console. (When connecting the mbed to a PC with USB, the baud rate should be 9600)
Hello World
#include "mbed.h" #include "HTTP_SERVER.h" int main() { HttpServer httpsvr; if(httpsvr.init()){ if(httpsvr.run() == 0) printf("end\r\n"); else printf("error end\r\n"); } return 0; }
As an example, make a index.htm like below (NOT .html; the reason is referred above) in the mbed, and access to <the IP address>/index.htm with a web browser on your PC or any terminals on the network the mbed is connected to, then you would be able to see the page.
index.htm in mbed
<!DOCTYPE html> <html> <head> <title>mbed http server demo</title> </head> <body> <h1>Mbed Simple HTTP Server</h1> hello world<br /> <a href="./index.htm" terget="self">Hyper Link Test</a><br /> </body> </html>
Diff: HTTP_SERVER.cpp
- Revision:
- 1:3a1fe94c6e42
- Parent:
- 0:cc483bea4fe3
- Child:
- 2:33714d7c0f45
--- a/HTTP_SERVER.cpp Tue Feb 16 10:59:31 2016 +0000 +++ b/HTTP_SERVER.cpp Sat Nov 26 16:49:04 2016 +0000 @@ -1,5 +1,14 @@ #include "HTTP_SERVER.h" +#include "string" #define DEBUG + +void DEBUG_PRINT_LINE(const char* arg_line) +{ +#ifdef DEBUG + printf("%s",arg_line); +#endif +} + HttpServer::HttpServer() { keep_alive = (false); @@ -16,31 +25,31 @@ // Ethernet Initialization if(eth.init()) { - printf("Error!@EthernetInterface::init()\r\n"); + printf("(HTTP_SERVER) Error!@EthernetInterface::init()\r\n"); return false; } // Ethernet Connecting setup if(eth.connect()) { - printf("Error!@EthernetInterface::connect()\r\n"); + printf("(HTTP_SERVER) Error!@EthernetInterface::connect()\r\n"); return false; } else { - printf("IP Address is %s\r\n", eth.getIPAddress()); + printf("(HTTP_SERVER) IP Address is %s\r\n", eth.getIPAddress()); } // TCP Socket setup // To open Server-side PORT if(tcpsvr.bind(TCP_PORT)< 0) { - printf("Error!@TCPSocketServer::bind()\r\n"); + printf("(HTTP_SERVER) Error!@TCPSocketServer::bind()\r\n"); return false; } else { - printf("TCP Server has bounden!\r\n"); + printf("(HTTP_SERVER) TCP Server has bounden!\r\n"); } // Server start listening Request from a web browser. if(tcpsvr.listen(1) < 0) { - printf("tcp server listen failed.\r\n"); + printf("(HTTP_SERVER) tcp server listen failed.\r\n"); return false; } else { listening_flag = true; - printf("tcp server is listening...\r\n"); + printf("(HTTP_SERVER) tcp server is listening...\r\n"); } return true; @@ -55,48 +64,46 @@ led1 = true; // blocking mode (never timeout) // waiting client connection - printf("\r\nwait connection\r\n"); + printf("(HTTP_SERVER) waiting connection\r\n"); if(tcpsvr.accept(tcpcon) < 0) { - printf("failed to accept connection.\r\n"); + printf("(HTTP_SERVER) failed to accept connection.\r\n"); return -1; } else { - printf("connection success!\r\nIP: %s\r\n",tcpcon.get_address()); + printf("(HTTP_SERVER) connection success!\r\nIP: %s\r\n",tcpcon.get_address()); led2 = true; } - //while(client.is_connected()) { + // When conected while(tcpcon.is_connected()) { -#ifdef DEBUG - printf("being connected\r\n"); -#endif + DEBUG_PRINT_LINE("(HTTP_SERVER) connected\r\n"); + char buffer[1024] = {0}; char* httpmethod = NULL; char* filepath = NULL; char* http_ver = NULL; char* header_field_name = NULL; char* header_field_val = NULL; + // // Request Analysis // -#ifdef DEBUG - printf("debug\r\n"); -#endif + DEBUG_PRINT_LINE("(HTTP_SERVER) DEBUG MODE\r\n"); switch(tcpcon.receive(buffer, 1023)) { case 0: - printf("recieved buffer is empty.\r\n"); + printf("(HTTP_SERVER) recieved buffer is empty.\r\n"); msger.setStatusLine(400, "No Request"); httpmethod = NULL; filepath = NULL; http_ver = NULL; break; case -1: - printf("failed to read data from client.\r\n"); + printf("(HTTP_SERVER) failed to read data from client.\r\n"); msger.setStatusLine(500, "Internal Server Error"); httpmethod = NULL; filepath = NULL; http_ver = NULL; break; default: - printf("Recieved Data: %d\r\n\r\n%.*s\r\n",strlen(buffer),strlen(buffer),buffer); + printf("(HTTP_SERVER) Recieved Data: %d\r\n-->\r\n%.*s[End of Request]\r\n",strlen(buffer),strlen(buffer),buffer); // get HTTP method httpmethod = strtok(buffer," "); // get File path @@ -104,25 +111,23 @@ // get HTTP version http_ver = strtok(NULL, "\r\n"); #ifdef DEBUG - printf("httpmethod: %s\r\n", httpmethod); - printf("file path: %s\r\n", filepath); - printf("http ver : %s\r\n", http_ver); + printf("(HTTP_SERVER) httpmethod: %s\r\n", httpmethod); + printf("(HTTP_SERVER) file path: %s\r\n", filepath); + printf("(HTTP_SERVER) http ver : %s\r\n", http_ver); #endif break; } -#ifdef DEBUG - printf("debug before response\r\n"); -#endif + DEBUG_PRINT_LINE("\r\n" + "(HTTP_SERVER) debug before response\r\n"); + // // Response // if (strcmp(httpmethod,"GET") == 0 ) { - printf("GET request incomming.\r\n"); + printf("(HTTP_SERVER) GET request incomming.\r\n"); // file calibration -#ifdef DEBUG - printf("file opening\r\n"); -#endif + DEBUG_PRINT_LINE("(HTTP_SERVER) file opening\r\n"); fhandl.open(filepath,"rb"); if(fhandl.arrival()) { msger.setStatusLine(200, "OK"); @@ -131,16 +136,14 @@ } else { if(msger.setStatusLine(404, "NOT FOUND"))printf("buffer over flow"); if(msger.setHeaderField("Connection", "close"))printf("buffer over flow"); -#ifdef DEBUG - printf("NOT FOUND\r\n"); -#endif + DEBUG_PRINT_LINE("(HTTP_SERVER) NOT FOUND\r\n"); } - if(! ( strcmp(fhandl.getSuffix(), "htm" ) && - strcmp(fhandl.getSuffix(), "HTM" ) && - strcmp(fhandl.getSuffix(), "html") && - strcmp(fhandl.getSuffix(), "HTML") )) { + if( strcmp(fhandl.getSuffix(), "htm" ) || + strcmp(fhandl.getSuffix(), "HTM" ) || + strcmp(fhandl.getSuffix(), "html") || + strcmp(fhandl.getSuffix(), "HTML")) { if(msger.setHeaderField("Content-Type", "text/html"))printf("buffer over flow"); - } else if (!strcmp(fhandl.getSuffix(), "ico" ) ) { + } else if (strcmp(fhandl.getSuffix(), "ico" ) ) { if(msger.setHeaderField("Content-Type", "image/png"))printf("buffer over flow"); } else { msger.setStatusLine(406, "not acceptable"); @@ -148,7 +151,8 @@ // Connection timeout field if(msger.setHeaderField("Keep-Alive", "timeouit=15"))printf("buffer over flow"); -/* // Apply request header field to response header field + + // Define behaviour of server according to Request Header lines do { //strtok(NULL, "\r\n"); header_field_name = strtok(NULL, ":"); @@ -157,20 +161,17 @@ header_field_val = strtok(NULL, "\r\n"); header_field_val++; #ifdef DEBUG - printf("header_field_name adr: %d %s\r\n", header_field_name - 1, header_field_name); - printf("header_field_val adr: %d %s\r\n", header_field_val - 1, header_field_val); + printf("(HTTP_SERVER) *header_field_name adr: %d %s\r\n", header_field_name - 1, header_field_name); + printf("(HTTP_SERVER) header_field_val adr: %d %s\r\n", header_field_val - 1, header_field_val); #endif + // Apply request header field to response header field if(header_field_name - 1 != NULL) { - //if(strcmp(header_field_name,"Connection") == 0) { - // if(msger.setHeaderField(header_field_name, "close"))printf("buffer over flow"); - //} else { if(msger.setHeaderField(header_field_name, header_field_val))printf("buffer over flow"); - //} } else { break; } } while(1); -*/ + #ifdef DEBUG //printf("status code : %d\r\n", status_code); //printf("content type: %s\r\n", content_type); @@ -189,18 +190,18 @@ #endif #ifdef DEBUG == 0) - printf("file has closed\r\n"); + printf("(HTTP_SERVER) file has closed\r\n"); else if(EOF) - printf("failed to close the file\r\n"); + printf("(HTTP_SERVER) failed to close the file\r\n"); #endif msger.resetHeader(); - printf("echo back done.\r\n"); + printf("(HTTP_SERVER) echo back done.\r\n"); } - printf("Response to Request has done\r\n"); + printf("(HTTP_SERVER) Response to Request has done\r\n"); } - printf("close connection.\r\ntcp server is listening...\r\n"); + printf("(HTTP_SERVER) close connection.\r\ntcp server is listening...\r\n"); tcpcon.close(); - tcpsvr.close(); + //tcpsvr.close(); led2 = false; } led1 = false;