Greg Steiert / Mbed 2 deprecated HTTP_SD_Server_ArchPro

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed

Fork of HTTP_SD_Server 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             sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00070             client.send(httpHeader,strlen(httpHeader));
00071             client.send(uri,strlen(uri));
00072         } else {
00073             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00074             client.send(httpHeader,strlen(httpHeader));
00075             while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
00076                 client.send(buffer, rdCnt);
00077             }
00078             client.send(buffer, rdCnt);
00079             fclose(fp);
00080         }
00081     }
00082 }
00083 
00084 int main (void)
00085 {
00086 //    Serial Interface eth;
00087     uart.baud(115200);
00088     uart.printf("Initializing\n");
00089 
00090 //    Check File System
00091     uart.printf("Checking File System\n");
00092     DIR *d = opendir("/sd/");
00093     if (d != NULL) {
00094         uart.printf("SD Card Present\n");
00095     } else {
00096         uart.printf("SD Card Root Directory Not Found\n");
00097     }
00098 
00099 //    EthernetInterface eth;
00100     uart.printf("Initializing Ethernet\n");
00101     eth.init(); //Use DHCP
00102     uart.printf("Connecting\n");
00103     eth.connect();
00104     uart.printf("IP Address is %s\n", eth.getIPAddress());
00105 
00106 //    TCPSocketServer server;
00107     server.bind(HTTPD_SERVER_PORT);
00108     server.listen();
00109     uart.printf("Server Listening\n");
00110 
00111     while (true) {
00112         uart.printf("\nWait for new connection...\r\n");
00113         server.accept(client);
00114         client.set_blocking(false, 1500); // Timeout after (1.5)s
00115 
00116         uart.printf("Connection from: %s\r\n", client.get_address());
00117         while (true) {
00118             int n = client.receive(buffer, sizeof(buffer));
00119             if (n <= 0) break;
00120             uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
00121             if (n >= 1024) {
00122                 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00123                 client.send(httpHeader,strlen(httpHeader));
00124                 client.send(buffer,n);
00125                 break;
00126             } else {
00127                 buffer[n]=0;
00128             }
00129             if (!strncmp(buffer, "GET ", 4)) {
00130                 uristr = buffer + 4;
00131                 eou = strstr(uristr, " ");
00132                 if (eou == NULL) {
00133                     sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00134                     client.send(httpHeader,strlen(httpHeader));
00135                     client.send(buffer,n);
00136                 } else {
00137                     *eou = 0;
00138                     get_file(uristr);
00139                 }
00140             }
00141         }
00142 
00143         client.close();
00144     }
00145 }