HTTP SD Card File Server for the FRDM-K64F board

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed

Fork of HTTP_SD_Server_K64F 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 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F
00018 
00019 EthernetInterface eth;
00020 TCPSocketServer server;
00021 TCPSocketConnection client;
00022 
00023 char buffer[HTTPD_MAX_REQ_LENGTH+1];
00024 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
00025 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
00026 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
00027 char *uristr;
00028 char *eou;
00029 char *qrystr;
00030 
00031 FILE *fp;
00032 int rdCnt;
00033 
00034 void get_file(char* uri)
00035 {
00036     uart.printf("get_file %s\n", uri);
00037     char *lstchr = strrchr(uri, NULL) -1;
00038     if ('/' == *lstchr) {
00039         uart.printf("Open directory /sd%s\n", uri);
00040         *lstchr = 0;
00041         sprintf(fileName, "/sd%s", uri);
00042         DIR *d = opendir(fileName);
00043         if (d != NULL) {
00044             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00045             client.send(httpHeader,strlen(httpHeader));
00046             sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri);
00047             client.send(httpHeader,strlen(httpHeader));
00048             struct dirent *p;
00049             while((p = readdir(d)) != NULL) {
00050                 sprintf(dirName, "%s/%s", fileName, p->d_name);
00051                 uart.printf("%s\n", dirName);
00052                 DIR *subDir = opendir(dirName);
00053                 if (subDir != NULL) {
00054                     sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
00055                 } else {
00056                     sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name);
00057                 }
00058                 client.send(httpHeader,strlen(httpHeader));
00059             }
00060         }
00061         closedir(d);
00062         uart.printf("Directory closed\n");
00063         sprintf(httpHeader,"</ul></body></html>");
00064         client.send(httpHeader,strlen(httpHeader));
00065     } else {
00066         sprintf(fileName, "/sd%s", uri);
00067         fp = fopen(fileName, "r");
00068         if (fp == NULL) {
00069             uart.printf("File not found\n");
00070             sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00071             client.send(httpHeader,strlen(httpHeader));
00072             client.send(uri,strlen(uri));
00073         } else {
00074             uart.printf("Sending: header");
00075             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00076             client.send(httpHeader,strlen(httpHeader));
00077             uart.printf(" file");
00078             while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
00079                 client.send(buffer, rdCnt);
00080                 uart.printf(".");
00081             }
00082             client.send(buffer, rdCnt);
00083             fclose(fp);
00084             uart.printf("done\n");
00085         }
00086     }
00087 }
00088 
00089 int main (void)
00090 {
00091 //    Serial Interface eth;
00092     uart.baud(115200);
00093     uart.printf("Initializing\n");
00094 
00095 //    Check File System
00096     uart.printf("Checking File System\n");
00097     DIR *d = opendir("/sd/");
00098     if (d != NULL) {
00099         uart.printf("SD Card Present\n");
00100     } else {
00101         uart.printf("SD Card Root Directory Not Found\n");
00102     }
00103 
00104 //    EthernetInterface eth;
00105     uart.printf("Initializing Ethernet\n");
00106     eth.init(); //Use DHCP
00107     uart.printf("Connecting\n");
00108     eth.connect();
00109     uart.printf("IP Address is %s\n", eth.getIPAddress());
00110 
00111 //    TCPSocketServer server;
00112     server.bind(HTTPD_SERVER_PORT);
00113     server.listen();
00114     uart.printf("Server Listening\n");
00115 
00116     while (true) {
00117         uart.printf("\nWait for new connection...\r\n");
00118         server.accept(client);
00119         client.set_blocking(false, 1500); // Timeout after (1.5)s
00120 
00121         uart.printf("Connection from: %s\r\n", client.get_address());
00122         while (true) {
00123             int n = client.receive(buffer, sizeof(buffer));
00124             if (n <= 0) break;
00125             uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
00126             if (n >= 1024) {
00127                 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00128                 client.send(httpHeader,strlen(httpHeader));
00129                 client.send(buffer,n);
00130                 break;
00131             } else {
00132                 buffer[n]=0;
00133             }
00134             if (!strncmp(buffer, "GET ", 4)) {
00135                 uristr = buffer + 4;
00136                 eou = strstr(uristr, " ");
00137                 if (eou == NULL) {
00138                     sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00139                     client.send(httpHeader,strlen(httpHeader));
00140                     client.send(buffer,n);
00141                 } else {
00142                     *eou = 0;
00143                     get_file(uristr);
00144                 }
00145             }
00146         }
00147 
00148         client.close();
00149     }
00150 }