HTTP SD Card File Server

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed

Fork of mURI_HTTPserver by Greg Steiert

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "SDFileSystem.h"
00004 #include <stdio.h>
00005 #include <string.h>
00006 
00007 #define HTTPD_SERVER_PORT   80
00008 #define HTTPD_MAX_REQ_LENGTH   1023
00009 #define HTTPD_MAX_HDR_LENGTH   255
00010 #define HTTPD_MAX_FNAME_LENGTH   127
00011 #define HTTPD_MAX_DNAME_LENGTH   127
00012 
00013 Serial uart(USBTX, USBRX);
00014 
00015 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
00016 SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
00017 
00018 EthernetInterface eth;
00019 TCPSocketServer server;
00020 TCPSocketConnection client;
00021 
00022 char buffer[HTTPD_MAX_REQ_LENGTH+1];
00023 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
00024 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
00025 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
00026 char *uristr;
00027 char *eou;
00028 char *qrystr;
00029 
00030 FILE *fp;
00031 int rdCnt;
00032 
00033 void get_file(char* uri)
00034 {
00035     uart.printf("get_file %s\n", uri);
00036     char *lstchr = strrchr(uri, NULL) -1;
00037     if ('/' == *lstchr) {
00038         uart.printf("Open directory /sd%s\n", uri);
00039         *lstchr = 0;
00040         sprintf(fileName, "/sd%s", uri);
00041         DIR *d = opendir(fileName);
00042         if (d != NULL) {
00043             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00044             client.send(httpHeader,strlen(httpHeader));
00045             sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri);
00046             client.send(httpHeader,strlen(httpHeader));
00047             struct dirent *p;
00048             while((p = readdir(d)) != NULL) {
00049                 sprintf(dirName, "%s/%s", fileName, p->d_name);
00050                 uart.printf("%s\n", dirName);
00051                 DIR *subDir = opendir(dirName);
00052                 if (subDir != NULL) {
00053                     sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
00054                 } else {
00055                     sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name);
00056                 }
00057                 client.send(httpHeader,strlen(httpHeader));
00058             }
00059         }
00060         closedir(d);
00061         uart.printf("Directory closed\n");
00062         sprintf(httpHeader,"</ul></body></html>");
00063         client.send(httpHeader,strlen(httpHeader));
00064     } else {
00065         sprintf(fileName, "/sd%s", uri);
00066         fp = fopen(fileName, "r");
00067         if (fp == NULL) {
00068             sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00069             client.send(httpHeader,strlen(httpHeader));
00070             client.send(uri,strlen(uri));
00071         } else {
00072             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00073             client.send(httpHeader,strlen(httpHeader));
00074             while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
00075                 client.send(buffer, rdCnt);
00076             }
00077             client.send(buffer, rdCnt);
00078             fclose(fp);
00079         }
00080     }
00081 }
00082 
00083 int main (void)
00084 {
00085 //    Serial Interface eth;
00086     uart.baud(115200);
00087     uart.printf("RAPM Serial Started\n");
00088 
00089 //    Check File System
00090     DIR *d = opendir("/sd/");
00091     if (d != NULL) {
00092         uart.printf("SD Card Present\n");
00093     } else {
00094         uart.printf("SD Card Root Directory Not Found\n");
00095     }
00096 
00097 //    EthernetInterface eth;
00098     eth.init(); //Use DHCP
00099     eth.connect();
00100     uart.printf("IP Address is %s\n", eth.getIPAddress());
00101 
00102 //    TCPSocketServer server;
00103     server.bind(HTTPD_SERVER_PORT);
00104     server.listen();
00105     uart.printf("RAPM Server Listening\n");
00106 
00107     while (true) {
00108         uart.printf("\nWait for new connection...\r\n");
00109         server.accept(client);
00110         client.set_blocking(false, 1500); // Timeout after (1.5)s
00111 
00112         uart.printf("Connection from: %s\r\n", client.get_address());
00113         while (true) {
00114             int n = client.receive(buffer, sizeof(buffer));
00115             if (n <= 0) break;
00116             uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
00117             if (n >= 1024) {
00118                 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00119                 client.send(httpHeader,strlen(httpHeader));
00120                 client.send(buffer,n);
00121                 break;
00122             } else {
00123                 buffer[n]=0;
00124             }
00125             if (!strncmp(buffer, "GET ", 4)) {
00126                 uristr = buffer + 4;
00127                 eou = strstr(uristr, " ");
00128                 if (eou == NULL) {
00129                     sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00130                     client.send(httpHeader,strlen(httpHeader));
00131                     client.send(buffer,n);
00132                 } else {
00133                     *eou = 0;
00134                     get_file(uristr);
00135                 }
00136             }
00137         }
00138 
00139         client.close();
00140     }
00141 }