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>

HTTP_SERVER.h

Committer:
aktk
Date:
2018-03-16
Revision:
13:b6dd6ed0060b
Parent:
12:cbf97b865d76

File content as of revision 13:b6dd6ed0060b:

//HTTP_SERVER.h
#ifndef HTTP_SERVER_H
#define HTTP_SERVER_H
#include "mbed.h"
#include "EthernetInterface.h"
#include "ResponseMessenger.h"
#include "FileHandler.h"
#include "string.h"
#include <stdlib.h>
using namespace std;

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 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.
     *
     *  @return state ending.
     *  @retval TRUE at error end.
     *  @retval FALSE at normal end.
     */
    bool run();

private:
    //  Handlers
    EthernetInterface   eth;    //  Eternet
    TCPSocketServer     tcpsvr; //  TCP server
    TCPSocketConnection tcpcon; //  TCP server connection clerk
    ResponseMessenger   msger;  //  Messenger for a client
    FileHandler         fhandl; //
    //  Param
    bool keep_alive;
    bool listening_flag;
    char req_buf[1024];
};

#endif