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:
Tue Feb 16 10:59:31 2016 +0000
Revision:
0:cc483bea4fe3
Child:
1:3a1fe94c6e42
add comment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aktk 0:cc483bea4fe3 1 /* ResponseMessenger.h */
aktk 0:cc483bea4fe3 2 #ifndef RESPONSE_MESSENGER_H
aktk 0:cc483bea4fe3 3 #define RESPONSE_MESSENGER_H
aktk 0:cc483bea4fe3 4 #include "mbed.h"
aktk 0:cc483bea4fe3 5 #include "string.h"
aktk 0:cc483bea4fe3 6 #include "EthernetInterface.h"
aktk 0:cc483bea4fe3 7 #include "FileHandler.h"
aktk 0:cc483bea4fe3 8 #include <stdlib.h>
aktk 0:cc483bea4fe3 9 using namespace std;
aktk 0:cc483bea4fe3 10
aktk 0:cc483bea4fe3 11 class ResponseMessenger
aktk 0:cc483bea4fe3 12 {
aktk 0:cc483bea4fe3 13 enum {
aktk 0:cc483bea4fe3 14 REASON_PHRASE_SIZE = 32,
aktk 0:cc483bea4fe3 15 HEADER_FIELDS_SIZE = 2048
aktk 0:cc483bea4fe3 16 };
aktk 0:cc483bea4fe3 17 public:
aktk 0:cc483bea4fe3 18 ResponseMessenger();
aktk 0:cc483bea4fe3 19 ~ResponseMessenger();
aktk 0:cc483bea4fe3 20 int resetHeader();
aktk 0:cc483bea4fe3 21 int setStatusLine(int,const char*);
aktk 0:cc483bea4fe3 22 int setHeaderField(const char*, const char*);
aktk 0:cc483bea4fe3 23 int setHeaderField(const char*, int);
aktk 0:cc483bea4fe3 24 int getStatusCode();
aktk 0:cc483bea4fe3 25 /**
aktk 0:cc483bea4fe3 26 * Function to send response messages.
aktk 0:cc483bea4fe3 27 * just header only
aktk 0:cc483bea4fe3 28 * @return char
aktk 0:cc483bea4fe3 29 * @retval error code
aktk 0:cc483bea4fe3 30 */
aktk 0:cc483bea4fe3 31 char sendHTTPResponse(TCPSocketConnection&);
aktk 0:cc483bea4fe3 32 /**
aktk 0:cc483bea4fe3 33 * Function to send response messages.
aktk 0:cc483bea4fe3 34 * @return char
aktk 0:cc483bea4fe3 35 * @retval error code
aktk 0:cc483bea4fe3 36 */
aktk 0:cc483bea4fe3 37 char sendHTTPResponse(TCPSocketConnection&, FileHandler&);
aktk 0:cc483bea4fe3 38 private:
aktk 0:cc483bea4fe3 39 // Status-Line
aktk 0:cc483bea4fe3 40 static const char http_ver[9];
aktk 0:cc483bea4fe3 41 int status_code;
aktk 0:cc483bea4fe3 42 char reason_phrase[REASON_PHRASE_SIZE];
aktk 0:cc483bea4fe3 43 // Header Field buffer
aktk 0:cc483bea4fe3 44 // - General Header
aktk 0:cc483bea4fe3 45 // - Response Header
aktk 0:cc483bea4fe3 46 // - Entity Header
aktk 0:cc483bea4fe3 47 char header_field_buffer[HEADER_FIELDS_SIZE];
aktk 0:cc483bea4fe3 48 };
aktk 0:cc483bea4fe3 49
aktk 0:cc483bea4fe3 50 #endif