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:
7:184c6f1ace94
add comment

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