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>
Revision:
9:84aca9965f9f
Parent:
0:cc483bea4fe3
Child:
12:cbf97b865d76
--- a/HTTP_SERVER.h	Mon Nov 28 08:43:26 2016 +0000
+++ b/HTTP_SERVER.h	Tue Dec 06 07:29:01 2016 +0000
@@ -12,23 +12,25 @@
 enum PortNum {
     TCP_PORT = 80
 };
-
+/** HttpServer class
+ *
+ * This is the class to make a mbed a simple HTTP Server.
+ */
 class HttpServer
 {
 public:
     HttpServer();
     ~HttpServer();
-    /**
-     *  HTTP SERVER Initialization.
-     *  This is called in the Constructor.
-     *  You don't have to use but can call this if you have some reasons to init the server.
+    /** HTTP SERVER Initialization.
+     *
+     *  This function should be called first of all.
      *  @return result of init() as boolean.
      *  @retval TRUE SACCESS
      *  @retval FALSE
      */
     bool init();
-    /**
-     *  Run the surver service while listening flag is true.
+    /** Run the surver service while listening flag is true.
+     *
      *  @return state ending.
      *  @retval TRUE at error end.
      *  @retval FALSE at normal end.