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:
11:0ee7d100db24
merged

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aktk 0:cc483bea4fe3 1 #include "FileHandler.h"
aktk 0:cc483bea4fe3 2 #ifndef DEBUG
aktk 10:4a48594c2f44 3 //#define DEBUG
aktk 0:cc483bea4fe3 4 #endif
aktk 0:cc483bea4fe3 5 LocalFileSystem local("local");
aktk 0:cc483bea4fe3 6
aktk 0:cc483bea4fe3 7 FileHandler::FileHandler()
aktk 0:cc483bea4fe3 8 {
aktk 0:cc483bea4fe3 9 fullpath = NULL;
aktk 0:cc483bea4fe3 10 filename = NULL;
aktk 0:cc483bea4fe3 11 suffix = NULL;
aktk 0:cc483bea4fe3 12 fp = NULL;
aktk 0:cc483bea4fe3 13 file_size = 0;
aktk 0:cc483bea4fe3 14 }
aktk 0:cc483bea4fe3 15 FileHandler::~FileHandler()
aktk 0:cc483bea4fe3 16 {
aktk 0:cc483bea4fe3 17 if (fullpath != NULL) free(fullpath);
aktk 0:cc483bea4fe3 18 if (fp != NULL) fclose(fp);
aktk 0:cc483bea4fe3 19 }
aktk 0:cc483bea4fe3 20
aktk 0:cc483bea4fe3 21 FILE* FileHandler::open
aktk 0:cc483bea4fe3 22 ( const char* arg_filepath,
aktk 0:cc483bea4fe3 23 const char* arg_mode
aktk 0:cc483bea4fe3 24 )
aktk 0:cc483bea4fe3 25 {
aktk 0:cc483bea4fe3 26 FILE *tmp;
aktk 0:cc483bea4fe3 27
aktk 11:0ee7d100db24 28 //////printf("\r\n"
aktk 11:0ee7d100db24 29 // "fp: %d@FileHandler::open\r\n", fp);
aktk 0:cc483bea4fe3 30 if (fullpath != NULL) free(fullpath);
aktk 0:cc483bea4fe3 31 fullpath = (char*)malloc(sizeof(char) * (strlen("/local/") + strlen(arg_filepath) + strlen("index.htm") + 1));
aktk 11:0ee7d100db24 32 //////printf("fp: %d@FileHandler::open\r\n", fp);
aktk 0:cc483bea4fe3 33
aktk 0:cc483bea4fe3 34 // Path formatting
aktk 0:cc483bea4fe3 35 if (arg_filepath[0] == '/') {
aktk 0:cc483bea4fe3 36 sprintf(fullpath, "/local/%s", arg_filepath + 1);
aktk 0:cc483bea4fe3 37 } else {
aktk 0:cc483bea4fe3 38 sprintf(fullpath, "/local/%s", arg_filepath);
aktk 0:cc483bea4fe3 39 }
aktk 0:cc483bea4fe3 40 // if the argument has no file name but directory, defalt settiing.
aktk 0:cc483bea4fe3 41 if (fullpath[strlen(fullpath) - 1] == '/')
aktk 0:cc483bea4fe3 42 strcat(fullpath, "index.htm");
aktk 0:cc483bea4fe3 43 // store the file name part to a pointer
aktk 0:cc483bea4fe3 44 filename = strrchr(fullpath, '/');
aktk 0:cc483bea4fe3 45 if(filename != NULL) filename++; // remove '/' and just get only the file name.
aktk 0:cc483bea4fe3 46 // store the suffix part to a pointer
aktk 0:cc483bea4fe3 47 suffix = strchr(filename, '.');
aktk 0:cc483bea4fe3 48 if(suffix != NULL) suffix++; // remove '.' and just get only the suffix.
aktk 0:cc483bea4fe3 49 #ifdef DEBUG
aktk 11:0ee7d100db24 50 //////printf("full path: %s\r\nfilename: %s\r\nsuffix: %s\r\n", getFullpath(), getFilename(), getSuffix());
aktk 0:cc483bea4fe3 51 #endif
aktk 0:cc483bea4fe3 52 fp = fopen(fullpath, arg_mode);
aktk 0:cc483bea4fe3 53 #ifdef DEBUG
aktk 11:0ee7d100db24 54 //////////printf("file opened@FileHandler::open\r\n");
aktk 0:cc483bea4fe3 55 #endif
aktk 0:cc483bea4fe3 56 // mesure file size
aktk 0:cc483bea4fe3 57 file_size = 0;
aktk 0:cc483bea4fe3 58 tmp = fp;
aktk 0:cc483bea4fe3 59 if(tmp != NULL ) {
aktk 11:0ee7d100db24 60 ////printf("\r\nfile content\r\n");
aktk 0:cc483bea4fe3 61 int ctmp;
aktk 0:cc483bea4fe3 62 while(1) {
aktk 0:cc483bea4fe3 63 ctmp = fgetc(tmp);
aktk 0:cc483bea4fe3 64 if(ctmp != EOF) {
aktk 11:0ee7d100db24 65 //////printf("%c", ctmp);
aktk 0:cc483bea4fe3 66 file_size++;
aktk 0:cc483bea4fe3 67 } else {
aktk 11:0ee7d100db24 68 //////printf("[EOF]\r\n");
aktk 0:cc483bea4fe3 69 break;
aktk 0:cc483bea4fe3 70 }
aktk 0:cc483bea4fe3 71 }
aktk 11:0ee7d100db24 72 ////printf("file size: %d\r\n", file_size);
aktk 0:cc483bea4fe3 73 if(fseek(tmp, 0L, SEEK_SET) != 0) {
aktk 11:0ee7d100db24 74 //////printf("fseek failed\r\n");
aktk 0:cc483bea4fe3 75 }
aktk 0:cc483bea4fe3 76 } else {
aktk 0:cc483bea4fe3 77 file_size = 0;
aktk 0:cc483bea4fe3 78 }
aktk 0:cc483bea4fe3 79
aktk 0:cc483bea4fe3 80 return fp;
aktk 0:cc483bea4fe3 81 }
aktk 0:cc483bea4fe3 82 int FileHandler::close()
aktk 0:cc483bea4fe3 83 {
aktk 0:cc483bea4fe3 84 int tmp;
aktk 7:184c6f1ace94 85
aktk 7:184c6f1ace94 86 if(fp != NULL) {
aktk 0:cc483bea4fe3 87 tmp = fclose(fp);
aktk 0:cc483bea4fe3 88 fp = NULL;
aktk 0:cc483bea4fe3 89 return tmp;
aktk 7:184c6f1ace94 90 } else {
aktk 0:cc483bea4fe3 91 return 1;
aktk 0:cc483bea4fe3 92 }
aktk 0:cc483bea4fe3 93 }
aktk 0:cc483bea4fe3 94
aktk 0:cc483bea4fe3 95 int FileHandler::getc()
aktk 0:cc483bea4fe3 96 {
aktk 0:cc483bea4fe3 97 int tmp = fgetc(fp);
aktk 11:0ee7d100db24 98 #ifdef DEBUG
aktk 0:cc483bea4fe3 99 if(0x20 < tmp && tmp < 0x7e)
aktk 0:cc483bea4fe3 100 printf("%c", tmp);
aktk 0:cc483bea4fe3 101 else if (tmp == '\r')
aktk 0:cc483bea4fe3 102 printf("\r");
aktk 0:cc483bea4fe3 103 else if (tmp == '\n')
aktk 0:cc483bea4fe3 104 printf("\n");
aktk 0:cc483bea4fe3 105 else
aktk 0:cc483bea4fe3 106 printf("@");
aktk 11:0ee7d100db24 107 #endif
aktk 11:0ee7d100db24 108 return tmp;
aktk 0:cc483bea4fe3 109 }
aktk 0:cc483bea4fe3 110 bool FileHandler::arrival()
aktk 0:cc483bea4fe3 111 {
aktk 0:cc483bea4fe3 112 return (bool)fp;
aktk 0:cc483bea4fe3 113 }
aktk 0:cc483bea4fe3 114 bool FileHandler::atEOF()
aktk 0:cc483bea4fe3 115 {
aktk 0:cc483bea4fe3 116 return (bool)feof(fp);
aktk 0:cc483bea4fe3 117 }
aktk 0:cc483bea4fe3 118 bool FileHandler::hasError()
aktk 0:cc483bea4fe3 119 {
aktk 0:cc483bea4fe3 120 return (bool)ferror(fp);
aktk 0:cc483bea4fe3 121 }
aktk 0:cc483bea4fe3 122 char *FileHandler::getFullpath()
aktk 0:cc483bea4fe3 123 {
aktk 0:cc483bea4fe3 124 return fullpath;
aktk 0:cc483bea4fe3 125 }
aktk 0:cc483bea4fe3 126 char *FileHandler::getFilename()
aktk 0:cc483bea4fe3 127 {
aktk 0:cc483bea4fe3 128 return filename;
aktk 0:cc483bea4fe3 129 }
aktk 0:cc483bea4fe3 130 char *FileHandler::getSuffix()
aktk 0:cc483bea4fe3 131 {
aktk 0:cc483bea4fe3 132 return suffix;
aktk 0:cc483bea4fe3 133 }
aktk 0:cc483bea4fe3 134 int FileHandler::getFileSize()
aktk 0:cc483bea4fe3 135 {
aktk 0:cc483bea4fe3 136 return file_size;
aktk 0:cc483bea4fe3 137 }