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:
0:cc483bea4fe3
Child:
7:184c6f1ace94
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/handlers/Filehandler.cpp	Tue Feb 16 10:59:31 2016 +0000
@@ -0,0 +1,136 @@
+#include "FileHandler.h"
+#ifndef DEBUG
+#define DEBUG
+#endif
+LocalFileSystem local("local");
+
+FileHandler::FileHandler()
+{
+    fullpath = NULL;
+    filename = NULL;
+    suffix = NULL;
+    fp = NULL;
+    file_size = 0;
+}
+FileHandler::~FileHandler()
+{
+    if (fullpath != NULL) free(fullpath);
+    if (fp != NULL) fclose(fp);
+}
+
+FILE* FileHandler::open
+(   const char* arg_filepath,
+    const char* arg_mode
+)
+{
+    FILE *tmp;
+
+    printf("\r\nfp: %d@FileHandler::open\r\n", fp);
+    if (fullpath != NULL) free(fullpath);
+    fullpath = (char*)malloc(sizeof(char) * (strlen("/local/") + strlen(arg_filepath) + strlen("index.htm") + 1));
+    printf("\r\nfp: %d@FileHandler::open\r\n", fp);
+
+    //  Path formatting
+    if (arg_filepath[0] == '/') {
+        sprintf(fullpath, "/local/%s", arg_filepath + 1);
+    } else {
+        sprintf(fullpath, "/local/%s", arg_filepath);
+    }
+    //  if the argument has no file name but directory, defalt settiing.
+    if (fullpath[strlen(fullpath) - 1] == '/')
+        strcat(fullpath, "index.htm");
+    //  store the file name part to a pointer
+    filename = strrchr(fullpath, '/');
+    if(filename != NULL)    filename++; //  remove '/' and just get only the file name.
+    //  store the suffix part to a pointer
+    suffix = strchr(filename, '.');
+    if(suffix   != NULL)    suffix++;   //  remove '.' and just get only the suffix.
+#ifdef DEBUG
+    printf("full path: %s\r\nfilename: %s\r\nsuffix: %s\r\n", getFullpath(), getFilename(), getSuffix());
+#endif
+    fp = fopen(fullpath, arg_mode);
+#ifdef DEBUG
+    printf("file opened@FileHandler::open\r\n");
+#endif
+    //  mesure file size
+    file_size = 0;
+    tmp = fp;
+    if(tmp != NULL ) {
+        printf("\r\nfile content\r\n");
+        int ctmp;
+        while(1) {
+            ctmp = fgetc(tmp);
+            if(ctmp != EOF) {
+                printf("%c", ctmp);
+                file_size++;
+            } else {
+                printf("[EOF]\r\n");
+                break;
+            }
+        }
+        printf("file size: %d\r\n", file_size);
+        if(fseek(tmp, 0L, SEEK_SET) != 0) {
+            printf("fseek failed\r\n");
+        }
+    } else {
+        file_size = 0;
+    }
+
+    return fp;
+}
+int FileHandler::close()
+{
+    int tmp;
+    
+    if(fp != NULL){
+        tmp = fclose(fp);
+        fp = NULL;
+        return tmp;
+    } else{
+        return 1;
+    }
+}
+
+int FileHandler::getc()
+{
+    int tmp = fgetc(fp);
+
+    if(0x20 < tmp && tmp < 0x7e)
+        printf("%c", tmp);
+    else if (tmp == '\r')
+        printf("\r");
+    else if (tmp == '\n')
+        printf("\n");
+    else
+        printf("@");
+
+    return tmp;//fgetc(fp);
+}
+bool FileHandler::arrival()
+{
+    return (bool)fp;
+}
+bool FileHandler::atEOF()
+{
+    return (bool)feof(fp);
+}
+bool FileHandler::hasError()
+{
+    return (bool)ferror(fp);
+}
+char *FileHandler::getFullpath()
+{
+    return fullpath;
+}
+char *FileHandler::getFilename()
+{
+    return filename;
+}
+char *FileHandler::getSuffix()
+{
+    return suffix;
+}
+int FileHandler::getFileSize()
+{
+    return file_size;
+}
\ No newline at end of file