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>
Committer:
aktk
Date:
Fri Mar 16 21:55:50 2018 +0000
Revision:
13:b6dd6ed0060b
Parent:
12:cbf97b865d76
merged

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aktk 0:cc483bea4fe3 1 //HTTP_SERVER.h
aktk 0:cc483bea4fe3 2 #ifndef HTTP_SERVER_H
aktk 0:cc483bea4fe3 3 #define HTTP_SERVER_H
aktk 0:cc483bea4fe3 4 #include "mbed.h"
aktk 0:cc483bea4fe3 5 #include "EthernetInterface.h"
aktk 0:cc483bea4fe3 6 #include "ResponseMessenger.h"
aktk 0:cc483bea4fe3 7 #include "FileHandler.h"
aktk 0:cc483bea4fe3 8 #include "string.h"
aktk 0:cc483bea4fe3 9 #include <stdlib.h>
aktk 0:cc483bea4fe3 10 using namespace std;
aktk 0:cc483bea4fe3 11
aktk 0:cc483bea4fe3 12 enum PortNum {
aktk 0:cc483bea4fe3 13 TCP_PORT = 80
aktk 0:cc483bea4fe3 14 };
aktk 9:84aca9965f9f 15 /** HttpServer class
aktk 9:84aca9965f9f 16 *
aktk 9:84aca9965f9f 17 * This is the class to make a mbed a simple HTTP Server.
aktk 9:84aca9965f9f 18 */
aktk 0:cc483bea4fe3 19 class HttpServer
aktk 0:cc483bea4fe3 20 {
aktk 0:cc483bea4fe3 21 public:
aktk 0:cc483bea4fe3 22 HttpServer();
aktk 0:cc483bea4fe3 23 ~HttpServer();
aktk 9:84aca9965f9f 24 /** HTTP SERVER Initialization.
aktk 9:84aca9965f9f 25 *
aktk 9:84aca9965f9f 26 * This function should be called first of all.
aktk 0:cc483bea4fe3 27 * @return result of init() as boolean.
aktk 0:cc483bea4fe3 28 * @retval TRUE SACCESS
aktk 0:cc483bea4fe3 29 * @retval FALSE
aktk 0:cc483bea4fe3 30 */
aktk 0:cc483bea4fe3 31 bool init();
aktk 9:84aca9965f9f 32 /** Run the surver service while listening flag is true.
aktk 9:84aca9965f9f 33 *
aktk 0:cc483bea4fe3 34 * @return state ending.
aktk 0:cc483bea4fe3 35 * @retval TRUE at error end.
aktk 0:cc483bea4fe3 36 * @retval FALSE at normal end.
aktk 0:cc483bea4fe3 37 */
aktk 0:cc483bea4fe3 38 bool run();
aktk 0:cc483bea4fe3 39
aktk 0:cc483bea4fe3 40 private:
aktk 0:cc483bea4fe3 41 // Handlers
aktk 0:cc483bea4fe3 42 EthernetInterface eth; // Eternet
aktk 0:cc483bea4fe3 43 TCPSocketServer tcpsvr; // TCP server
aktk 0:cc483bea4fe3 44 TCPSocketConnection tcpcon; // TCP server connection clerk
aktk 0:cc483bea4fe3 45 ResponseMessenger msger; // Messenger for a client
aktk 0:cc483bea4fe3 46 FileHandler fhandl; //
aktk 0:cc483bea4fe3 47 // Param
aktk 0:cc483bea4fe3 48 bool keep_alive;
aktk 0:cc483bea4fe3 49 bool listening_flag;
aktk 12:cbf97b865d76 50 char req_buf[1024];
aktk 0:cc483bea4fe3 51 };
aktk 0:cc483bea4fe3 52
aktk 0:cc483bea4fe3 53 #endif