thomasgds changed

Dependencies:   SDFileSystem WIZnetInterface mbed-dev

Fork of HTTP_SDCard_File_Server_WIZwiki-W7500P by jiang hao

Committer:
thomasgds
Date:
Tue Oct 25 05:32:17 2016 +0000
Revision:
2:0de89eb662fe
Parent:
1:b1028771b336
thomasgds changed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jh_ndm 0:48575e6a55f9 1 #include "mbed.h"
jh_ndm 0:48575e6a55f9 2 #include "EthernetInterface.h"
jh_ndm 0:48575e6a55f9 3 #include "SDFileSystem.h"
jh_ndm 0:48575e6a55f9 4 #include <stdio.h>
jh_ndm 0:48575e6a55f9 5 #include <string.h>
jh_ndm 0:48575e6a55f9 6
jh_ndm 0:48575e6a55f9 7
jh_ndm 0:48575e6a55f9 8 #define MAC "\x00\x08\xDC\x11\x34\x78"
jh_ndm 0:48575e6a55f9 9 #define IP "192.168.1.210"
jh_ndm 0:48575e6a55f9 10 #define MASK "255.255.255.0"
jh_ndm 0:48575e6a55f9 11 #define GATEWAY "192.168.1.1"
jh_ndm 0:48575e6a55f9 12
jh_ndm 0:48575e6a55f9 13 #define HTTPD_SERVER_PORT 80
jh_ndm 0:48575e6a55f9 14 #define HTTPD_MAX_REQ_LENGTH 1023
jh_ndm 0:48575e6a55f9 15 #define HTTPD_MAX_HDR_LENGTH 255
jh_ndm 0:48575e6a55f9 16 #define HTTPD_MAX_FNAME_LENGTH 127
jh_ndm 0:48575e6a55f9 17 #define HTTPD_MAX_DNAME_LENGTH 127
jh_ndm 0:48575e6a55f9 18
thomasgds 1:b1028771b336 19 //Serial uart(PA_13,PA_14);
thomasgds 1:b1028771b336 20 Serial uart(PC_10,PC_11);
jh_ndm 0:48575e6a55f9 21
jh_ndm 0:48575e6a55f9 22 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
jh_ndm 0:48575e6a55f9 23 //SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
jh_ndm 0:48575e6a55f9 24 //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F
jh_ndm 0:48575e6a55f9 25 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500
jh_ndm 0:48575e6a55f9 26
jh_ndm 0:48575e6a55f9 27 EthernetInterface eth;
jh_ndm 0:48575e6a55f9 28 TCPSocketServer server;
jh_ndm 0:48575e6a55f9 29 TCPSocketConnection client;
jh_ndm 0:48575e6a55f9 30
jh_ndm 0:48575e6a55f9 31 char buffer[HTTPD_MAX_REQ_LENGTH+1];
jh_ndm 0:48575e6a55f9 32 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
jh_ndm 0:48575e6a55f9 33 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
jh_ndm 0:48575e6a55f9 34 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
jh_ndm 0:48575e6a55f9 35 char *uristr;
jh_ndm 0:48575e6a55f9 36 char *eou;
jh_ndm 0:48575e6a55f9 37 char *qrystr;
jh_ndm 0:48575e6a55f9 38
jh_ndm 0:48575e6a55f9 39 FILE *fp;
jh_ndm 0:48575e6a55f9 40 int rdCnt;
jh_ndm 0:48575e6a55f9 41
jh_ndm 0:48575e6a55f9 42 DigitalOut led1(D10); //server listning status
jh_ndm 0:48575e6a55f9 43 DigitalOut led2(D11); //socket connecting status
jh_ndm 0:48575e6a55f9 44
jh_ndm 0:48575e6a55f9 45 Ticker ledTick;
jh_ndm 0:48575e6a55f9 46
jh_ndm 0:48575e6a55f9 47 void ledTickfunc()
jh_ndm 0:48575e6a55f9 48 {
jh_ndm 0:48575e6a55f9 49 led1 = !led1;
jh_ndm 0:48575e6a55f9 50 }
jh_ndm 0:48575e6a55f9 51
jh_ndm 0:48575e6a55f9 52
jh_ndm 0:48575e6a55f9 53
jh_ndm 0:48575e6a55f9 54 void getFile(char* uri)
jh_ndm 0:48575e6a55f9 55 {
jh_ndm 0:48575e6a55f9 56 uart.printf("getFile %s\n", uri);
jh_ndm 0:48575e6a55f9 57 char *lstchr = strrchr(uri, NULL) -1;
jh_ndm 0:48575e6a55f9 58 if ('/' == *lstchr) {
jh_ndm 0:48575e6a55f9 59 uart.printf("Open directory /sd%s\r\n", uri);
jh_ndm 0:48575e6a55f9 60 *lstchr = 0;
jh_ndm 0:48575e6a55f9 61 sprintf(fileName, "/sd%s", uri);
jh_ndm 0:48575e6a55f9 62 DIR *d = opendir(fileName);
jh_ndm 0:48575e6a55f9 63 if (d != NULL) {
jh_ndm 0:48575e6a55f9 64 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
jh_ndm 0:48575e6a55f9 65 client.send(httpHeader,strlen(httpHeader));
jh_ndm 0:48575e6a55f9 66 sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri);
jh_ndm 0:48575e6a55f9 67 client.send(httpHeader,strlen(httpHeader));
jh_ndm 0:48575e6a55f9 68 struct dirent *p;
jh_ndm 0:48575e6a55f9 69 while((p = readdir(d)) != NULL) {
jh_ndm 0:48575e6a55f9 70 sprintf(dirName, "%s/%s", fileName, p->d_name);
jh_ndm 0:48575e6a55f9 71 uart.printf("%s\n", dirName);
jh_ndm 0:48575e6a55f9 72 DIR *subDir = opendir(dirName);
jh_ndm 0:48575e6a55f9 73 if (subDir != NULL) {
jh_ndm 0:48575e6a55f9 74 sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
jh_ndm 0:48575e6a55f9 75 } else {
jh_ndm 0:48575e6a55f9 76 sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name);
jh_ndm 0:48575e6a55f9 77 }
jh_ndm 0:48575e6a55f9 78 client.send(httpHeader,strlen(httpHeader));
jh_ndm 0:48575e6a55f9 79 }
jh_ndm 0:48575e6a55f9 80 }
jh_ndm 0:48575e6a55f9 81 closedir(d);
jh_ndm 0:48575e6a55f9 82 uart.printf("Directory closed\r\n");
jh_ndm 0:48575e6a55f9 83 sprintf(httpHeader,"</ul></body></html>");
jh_ndm 0:48575e6a55f9 84 client.send(httpHeader,strlen(httpHeader));
jh_ndm 0:48575e6a55f9 85 } else {
jh_ndm 0:48575e6a55f9 86 sprintf(fileName, "/sd%s", uri);
jh_ndm 0:48575e6a55f9 87 fp = fopen(fileName, "r");
jh_ndm 0:48575e6a55f9 88 if (fp == NULL) {
jh_ndm 0:48575e6a55f9 89 uart.printf("File not found\r\n");
jh_ndm 0:48575e6a55f9 90 sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
jh_ndm 0:48575e6a55f9 91 client.send(httpHeader,strlen(httpHeader));
jh_ndm 0:48575e6a55f9 92 client.send(uri,strlen(uri));
jh_ndm 0:48575e6a55f9 93 } else {
jh_ndm 0:48575e6a55f9 94 uart.printf("Sending: header");
jh_ndm 0:48575e6a55f9 95 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
jh_ndm 0:48575e6a55f9 96 client.send(httpHeader,strlen(httpHeader));
jh_ndm 0:48575e6a55f9 97 uart.printf(" file");
jh_ndm 0:48575e6a55f9 98 while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
jh_ndm 0:48575e6a55f9 99 client.send(buffer, rdCnt);
jh_ndm 0:48575e6a55f9 100 uart.printf(".");
jh_ndm 0:48575e6a55f9 101 }
jh_ndm 0:48575e6a55f9 102 client.send(buffer, rdCnt);
jh_ndm 0:48575e6a55f9 103 fclose(fp);
jh_ndm 0:48575e6a55f9 104 uart.printf("done\r\n");
jh_ndm 0:48575e6a55f9 105 }
jh_ndm 0:48575e6a55f9 106 }
jh_ndm 0:48575e6a55f9 107 }
jh_ndm 0:48575e6a55f9 108
jh_ndm 0:48575e6a55f9 109 int main (void)
jh_ndm 0:48575e6a55f9 110 {
jh_ndm 0:48575e6a55f9 111 ledTick.attach(&ledTickfunc,0.5);
jh_ndm 0:48575e6a55f9 112 // Serial Interface eth;
jh_ndm 0:48575e6a55f9 113 uart.baud(115200);
jh_ndm 0:48575e6a55f9 114 uart.printf("Initializing\r\n");
jh_ndm 0:48575e6a55f9 115
jh_ndm 0:48575e6a55f9 116 // Check File System
jh_ndm 0:48575e6a55f9 117 uart.printf("Checking File System\r\n");
jh_ndm 0:48575e6a55f9 118 DIR *d = opendir("/sd/");
jh_ndm 0:48575e6a55f9 119 if (d != NULL) {
jh_ndm 0:48575e6a55f9 120 uart.printf("SD Card Present\r\n");
jh_ndm 0:48575e6a55f9 121 } else {
jh_ndm 0:48575e6a55f9 122 uart.printf("SD Card Root Directory Not Found\r\n");
jh_ndm 0:48575e6a55f9 123 }
jh_ndm 0:48575e6a55f9 124
jh_ndm 0:48575e6a55f9 125 // EthernetInterface eth;
jh_ndm 0:48575e6a55f9 126 uart.printf("Initializing Ethernet\r\n");
jh_ndm 0:48575e6a55f9 127 //eth.init(); //Use DHCP
jh_ndm 0:48575e6a55f9 128 //eth.init((uint8_t*)MAC); // Use DHCP for WIZnetInterface
jh_ndm 0:48575e6a55f9 129 eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway
jh_ndm 0:48575e6a55f9 130 uart.printf("Connecting\r\n");
jh_ndm 0:48575e6a55f9 131 eth.connect();
jh_ndm 0:48575e6a55f9 132 uart.printf("IP Address is %s\r\n", eth.getIPAddress());
jh_ndm 0:48575e6a55f9 133
jh_ndm 0:48575e6a55f9 134 // TCPSocketServer server;
jh_ndm 0:48575e6a55f9 135 server.bind(HTTPD_SERVER_PORT);
jh_ndm 0:48575e6a55f9 136 server.listen();
jh_ndm 0:48575e6a55f9 137 uart.printf("Server Listening\r\n");
jh_ndm 0:48575e6a55f9 138
jh_ndm 0:48575e6a55f9 139 while (true) {
jh_ndm 0:48575e6a55f9 140 uart.printf("\nWait for new connection...\r\n");
jh_ndm 0:48575e6a55f9 141 server.accept(client);
jh_ndm 0:48575e6a55f9 142 client.set_blocking(false, 1500); // Timeout after (1.5)s
jh_ndm 0:48575e6a55f9 143
jh_ndm 0:48575e6a55f9 144 uart.printf("Connection from: %s\r\n", client.get_address());
jh_ndm 0:48575e6a55f9 145 while (true) {
jh_ndm 0:48575e6a55f9 146 led2 = true;
jh_ndm 0:48575e6a55f9 147 int n = client.receive(buffer, sizeof(buffer));
jh_ndm 0:48575e6a55f9 148 if (n <= 0) break;
jh_ndm 0:48575e6a55f9 149 uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
jh_ndm 0:48575e6a55f9 150 if (n >= 1024) {
jh_ndm 0:48575e6a55f9 151 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
jh_ndm 0:48575e6a55f9 152 client.send(httpHeader,strlen(httpHeader));
jh_ndm 0:48575e6a55f9 153 client.send(buffer,n);
jh_ndm 0:48575e6a55f9 154 break;
jh_ndm 0:48575e6a55f9 155 } else {
jh_ndm 0:48575e6a55f9 156 buffer[n]=0;
jh_ndm 0:48575e6a55f9 157 }
jh_ndm 0:48575e6a55f9 158 if (!strncmp(buffer, "GET ", 4)) {
jh_ndm 0:48575e6a55f9 159 uristr = buffer + 4;
jh_ndm 0:48575e6a55f9 160 eou = strstr(uristr, " ");
jh_ndm 0:48575e6a55f9 161 if (eou == NULL) {
jh_ndm 0:48575e6a55f9 162 sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
jh_ndm 0:48575e6a55f9 163 client.send(httpHeader,strlen(httpHeader));
jh_ndm 0:48575e6a55f9 164 client.send(buffer,n);
jh_ndm 0:48575e6a55f9 165 } else {
jh_ndm 0:48575e6a55f9 166 *eou = 0;
jh_ndm 0:48575e6a55f9 167 getFile(uristr);
jh_ndm 0:48575e6a55f9 168 }
jh_ndm 0:48575e6a55f9 169 }
jh_ndm 0:48575e6a55f9 170 }
jh_ndm 0:48575e6a55f9 171 led2 = false;
jh_ndm 0:48575e6a55f9 172 client.close();
jh_ndm 0:48575e6a55f9 173 }
jh_ndm 0:48575e6a55f9 174 }