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:
0:cc483bea4fe3
merged

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aktk 0:cc483bea4fe3 1 /*FileHandler.h*/
aktk 0:cc483bea4fe3 2 #ifndef FILE_HANDLER_H
aktk 0:cc483bea4fe3 3 #define FILE_HANDLER_H
aktk 0:cc483bea4fe3 4 #include "mbed.h"
aktk 0:cc483bea4fe3 5 #include "string.h"
aktk 0:cc483bea4fe3 6 #include <stdlib.h>
aktk 0:cc483bea4fe3 7
aktk 0:cc483bea4fe3 8 using namespace std;
aktk 0:cc483bea4fe3 9 class FileHandler
aktk 0:cc483bea4fe3 10 {
aktk 0:cc483bea4fe3 11 public:
aktk 0:cc483bea4fe3 12 FileHandler();
aktk 0:cc483bea4fe3 13 ~FileHandler();
aktk 0:cc483bea4fe3 14 FILE* open(const char*, const char*);
aktk 0:cc483bea4fe3 15 int close();
aktk 0:cc483bea4fe3 16 virtual int getc();
aktk 0:cc483bea4fe3 17 bool arrival();
aktk 0:cc483bea4fe3 18 bool atEOF();
aktk 0:cc483bea4fe3 19 bool hasError();
aktk 0:cc483bea4fe3 20 char *getFullpath();
aktk 0:cc483bea4fe3 21 char *getFilename();
aktk 0:cc483bea4fe3 22 char *getSuffix();
aktk 0:cc483bea4fe3 23 int getFileSize();
aktk 0:cc483bea4fe3 24 private:
aktk 0:cc483bea4fe3 25 FILE *fp;
aktk 0:cc483bea4fe3 26 char *fullpath;
aktk 0:cc483bea4fe3 27 char *filename;
aktk 0:cc483bea4fe3 28 char *suffix;
aktk 0:cc483bea4fe3 29 char content_buffer[1024];
aktk 0:cc483bea4fe3 30 int file_size;
aktk 0:cc483bea4fe3 31 };
aktk 0:cc483bea4fe3 32 #endif