embedded Web site ,files store on SD card

Dependencies:  

Committer:
yao6116601
Date:
Thu Apr 19 13:36:33 2018 +0000
Revision:
0:9f5b2dd88884
website on SD card

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yao6116601 0:9f5b2dd88884 1
yao6116601 0:9f5b2dd88884 2 #include "mbed.h"
yao6116601 0:9f5b2dd88884 3 #include "EthernetInterface.h"
yao6116601 0:9f5b2dd88884 4 #include "TCPServer.h"
yao6116601 0:9f5b2dd88884 5 #include "TCPSocket.h"
yao6116601 0:9f5b2dd88884 6 #include "SDBlockDevice.h"
yao6116601 0:9f5b2dd88884 7 #include "FATFileSystem.h"
yao6116601 0:9f5b2dd88884 8 #include <stdio.h>
yao6116601 0:9f5b2dd88884 9 #include <string.h>
yao6116601 0:9f5b2dd88884 10 #define HTTPD_SERVER_PORT 80
yao6116601 0:9f5b2dd88884 11 #define HTTPD_MAX_REQ_LENGTH 1024
yao6116601 0:9f5b2dd88884 12 #define HTTPD_MAX_HDR_LENGTH 255
yao6116601 0:9f5b2dd88884 13 #define HTTPD_MAX_FNAME_LENGTH 127
yao6116601 0:9f5b2dd88884 14 #define HTTPD_MAX_DNAME_LENGTH 127
yao6116601 0:9f5b2dd88884 15 static const char* mbedIp = "192.168.11.102"; //IP
yao6116601 0:9f5b2dd88884 16 static const char* mbedMask = "255.255.255.0"; // Mask
yao6116601 0:9f5b2dd88884 17 static const char* mbedGateway = "192.168.11.1"; //Gateway
yao6116601 0:9f5b2dd88884 18 char buffer[HTTPD_MAX_REQ_LENGTH+1];
yao6116601 0:9f5b2dd88884 19 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
yao6116601 0:9f5b2dd88884 20 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
yao6116601 0:9f5b2dd88884 21 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
yao6116601 0:9f5b2dd88884 22 char *uristr;
yao6116601 0:9f5b2dd88884 23 char *eou;
yao6116601 0:9f5b2dd88884 24 char *qrystr;
yao6116601 0:9f5b2dd88884 25 DigitalOut myled(PF_12);
yao6116601 0:9f5b2dd88884 26 TCPServer server;
yao6116601 0:9f5b2dd88884 27 TCPSocket client;
yao6116601 0:9f5b2dd88884 28 SocketAddress client_addr;
yao6116601 0:9f5b2dd88884 29 SDBlockDevice sd(PC_12,PC_11,PC_10,PC_9,40000000,40000000);
yao6116601 0:9f5b2dd88884 30 FATFileSystem fs("sd", &sd);
yao6116601 0:9f5b2dd88884 31 Mutex sd_mutex;
yao6116601 0:9f5b2dd88884 32 void geturistr(char *reqstr,char * uri,int n)
yao6116601 0:9f5b2dd88884 33 { int i;
yao6116601 0:9f5b2dd88884 34 char * p;
yao6116601 0:9f5b2dd88884 35
yao6116601 0:9f5b2dd88884 36 p=uri;
yao6116601 0:9f5b2dd88884 37 i=0;
yao6116601 0:9f5b2dd88884 38 while(i<n)
yao6116601 0:9f5b2dd88884 39 {
yao6116601 0:9f5b2dd88884 40 if (*p=='/') i++;
yao6116601 0:9f5b2dd88884 41 p++;
yao6116601 0:9f5b2dd88884 42
yao6116601 0:9f5b2dd88884 43 }
yao6116601 0:9f5b2dd88884 44 while((*p!='/')&&(*p!=0))
yao6116601 0:9f5b2dd88884 45 *reqstr++=*p++;
yao6116601 0:9f5b2dd88884 46 *reqstr=0x00;
yao6116601 0:9f5b2dd88884 47
yao6116601 0:9f5b2dd88884 48 }
yao6116601 0:9f5b2dd88884 49 int getparamName(char *name,char * params)
yao6116601 0:9f5b2dd88884 50 { int i=0;
yao6116601 0:9f5b2dd88884 51 char *p;
yao6116601 0:9f5b2dd88884 52 char *n;
yao6116601 0:9f5b2dd88884 53 p=params;
yao6116601 0:9f5b2dd88884 54 n=name;
yao6116601 0:9f5b2dd88884 55 while(*p!='=')
yao6116601 0:9f5b2dd88884 56 {
yao6116601 0:9f5b2dd88884 57 *n++=*p++;
yao6116601 0:9f5b2dd88884 58 i++;
yao6116601 0:9f5b2dd88884 59 }
yao6116601 0:9f5b2dd88884 60 *n=0x00;
yao6116601 0:9f5b2dd88884 61 i++;
yao6116601 0:9f5b2dd88884 62 return i;
yao6116601 0:9f5b2dd88884 63 }
yao6116601 0:9f5b2dd88884 64 int getparamValue(char *value,char * param)
yao6116601 0:9f5b2dd88884 65 { int i=0;
yao6116601 0:9f5b2dd88884 66 char *p;
yao6116601 0:9f5b2dd88884 67 char *v;
yao6116601 0:9f5b2dd88884 68 p=param;
yao6116601 0:9f5b2dd88884 69 v=value;
yao6116601 0:9f5b2dd88884 70 while((*p!='&')&&(*p!=0x00))
yao6116601 0:9f5b2dd88884 71 {
yao6116601 0:9f5b2dd88884 72 *v++=*p++;
yao6116601 0:9f5b2dd88884 73 i++;
yao6116601 0:9f5b2dd88884 74 }
yao6116601 0:9f5b2dd88884 75 *v=0x00;
yao6116601 0:9f5b2dd88884 76 return i;
yao6116601 0:9f5b2dd88884 77 }
yao6116601 0:9f5b2dd88884 78 bool response_File(char * filename,const char *type)
yao6116601 0:9f5b2dd88884 79 {int n;
yao6116601 0:9f5b2dd88884 80 FILE *fp = fopen(filename, "r");
yao6116601 0:9f5b2dd88884 81 if (fp==NULL) return false;
yao6116601 0:9f5b2dd88884 82 if (strcmp(type,"text/css")||strcmp(type,"text/javascript"))
yao6116601 0:9f5b2dd88884 83 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: %s\r\nCache-Control: max-age=2592000, public\r\n\r\n",type);
yao6116601 0:9f5b2dd88884 84 else
yao6116601 0:9f5b2dd88884 85 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: %s\r\n",type);
yao6116601 0:9f5b2dd88884 86
yao6116601 0:9f5b2dd88884 87 client.send(httpHeader,strlen(httpHeader));
yao6116601 0:9f5b2dd88884 88 // sd_mutex.lock();
yao6116601 0:9f5b2dd88884 89
yao6116601 0:9f5b2dd88884 90 n=1024;
yao6116601 0:9f5b2dd88884 91 while (n == 1024) {
yao6116601 0:9f5b2dd88884 92 n = fread(buffer, sizeof( char ), 1024, fp);
yao6116601 0:9f5b2dd88884 93 client.send(buffer, n);
yao6116601 0:9f5b2dd88884 94 }
yao6116601 0:9f5b2dd88884 95 fclose(fp);
yao6116601 0:9f5b2dd88884 96 // sd_mutex.unlock();
yao6116601 0:9f5b2dd88884 97 return true;
yao6116601 0:9f5b2dd88884 98 }
yao6116601 0:9f5b2dd88884 99 void getEXT( char *ext,char * fileName)
yao6116601 0:9f5b2dd88884 100 {
yao6116601 0:9f5b2dd88884 101 char *f;
yao6116601 0:9f5b2dd88884 102 char *e;
yao6116601 0:9f5b2dd88884 103 e=ext;
yao6116601 0:9f5b2dd88884 104 f=fileName;
yao6116601 0:9f5b2dd88884 105 while(*f!='.')
yao6116601 0:9f5b2dd88884 106 f++;
yao6116601 0:9f5b2dd88884 107 f++;
yao6116601 0:9f5b2dd88884 108 while(*f!=0x00)
yao6116601 0:9f5b2dd88884 109 *e++=*f++;
yao6116601 0:9f5b2dd88884 110 *e=0x00;
yao6116601 0:9f5b2dd88884 111 }
yao6116601 0:9f5b2dd88884 112
yao6116601 0:9f5b2dd88884 113 int main()
yao6116601 0:9f5b2dd88884 114 { char fileName[32];
yao6116601 0:9f5b2dd88884 115 char reqstr[32];
yao6116601 0:9f5b2dd88884 116 char temp[32];
yao6116601 0:9f5b2dd88884 117 char ext[8];
yao6116601 0:9f5b2dd88884 118 char *params;
yao6116601 0:9f5b2dd88884 119 bool res;
yao6116601 0:9f5b2dd88884 120 printf("Basic HTTP server example\n");
yao6116601 0:9f5b2dd88884 121 EthernetInterface eth;
yao6116601 0:9f5b2dd88884 122 eth.set_network(mbedIp,mbedMask,mbedGateway);
yao6116601 0:9f5b2dd88884 123 eth.connect();
yao6116601 0:9f5b2dd88884 124 printf("The target IP address is '%s'\n", eth.get_ip_address());
yao6116601 0:9f5b2dd88884 125 /* Open the server on ethernet stack */
yao6116601 0:9f5b2dd88884 126 server.open(&eth);
yao6116601 0:9f5b2dd88884 127
yao6116601 0:9f5b2dd88884 128 /* Bind the HTTP port (TCP 80) to the server */
yao6116601 0:9f5b2dd88884 129 server.bind(eth.get_ip_address(), 80);
yao6116601 0:9f5b2dd88884 130
yao6116601 0:9f5b2dd88884 131 /* Can handle 5 simultaneous connections */
yao6116601 0:9f5b2dd88884 132 server.listen(8);
yao6116601 0:9f5b2dd88884 133 myled=1;
yao6116601 0:9f5b2dd88884 134 while (true) {
yao6116601 0:9f5b2dd88884 135 server.accept(&client, &client_addr);
yao6116601 0:9f5b2dd88884 136 // client.set_blocking(true);
yao6116601 0:9f5b2dd88884 137 printf("Connection from: %s\r\n", client_addr.get_ip_address());
yao6116601 0:9f5b2dd88884 138 // while (true) {
yao6116601 0:9f5b2dd88884 139 int n = client.recv(buffer, sizeof(buffer));
yao6116601 0:9f5b2dd88884 140 // if (n <= 0) break;
yao6116601 0:9f5b2dd88884 141 if (strncmp(buffer, "GET ", 4)==0)
yao6116601 0:9f5b2dd88884 142 {
yao6116601 0:9f5b2dd88884 143 uristr = buffer + 4;
yao6116601 0:9f5b2dd88884 144 eou = strstr(uristr, " ");
yao6116601 0:9f5b2dd88884 145 *eou = 0;
yao6116601 0:9f5b2dd88884 146 geturistr(reqstr,uristr,1);
yao6116601 0:9f5b2dd88884 147 printf("request=%s+\n",uristr);
yao6116601 0:9f5b2dd88884 148 if (strcmp(reqstr,"index")==0){ //GET index.html
yao6116601 0:9f5b2dd88884 149 // printf("OK\n");
yao6116601 0:9f5b2dd88884 150 res= response_File("/sd/views/index.html","text/html");
yao6116601 0:9f5b2dd88884 151 } else
yao6116601 0:9f5b2dd88884 152 if (strcmp(reqstr,"images")==0){ //GET images
yao6116601 0:9f5b2dd88884 153 geturistr(reqstr,uristr,2);
yao6116601 0:9f5b2dd88884 154 // printf("image name%s\n",reqstr);
yao6116601 0:9f5b2dd88884 155 strcpy(fileName,"/sd/images/");
yao6116601 0:9f5b2dd88884 156 strcat(fileName,reqstr);
yao6116601 0:9f5b2dd88884 157 // printf("filename=%s\n",fileName);
yao6116601 0:9f5b2dd88884 158 getEXT(ext,fileName);
yao6116601 0:9f5b2dd88884 159 strcpy(temp,"image/");
yao6116601 0:9f5b2dd88884 160 strcat(temp,ext);
yao6116601 0:9f5b2dd88884 161 res= response_File(fileName,temp);
yao6116601 0:9f5b2dd88884 162 }else
yao6116601 0:9f5b2dd88884 163 if (strcmp(reqstr,"css")==0){ //GET css
yao6116601 0:9f5b2dd88884 164 geturistr(reqstr,uristr,2);
yao6116601 0:9f5b2dd88884 165 strcpy(fileName,"/sd/css/");
yao6116601 0:9f5b2dd88884 166 strcat(fileName,reqstr);
yao6116601 0:9f5b2dd88884 167 response_File(fileName,"text/css");
yao6116601 0:9f5b2dd88884 168 } else
yao6116601 0:9f5b2dd88884 169 if (strcmp(reqstr,"js")==0){ //GET js
yao6116601 0:9f5b2dd88884 170 geturistr(reqstr,uristr,2);
yao6116601 0:9f5b2dd88884 171 strcpy(fileName,"/sd/js/");
yao6116601 0:9f5b2dd88884 172 strcat(fileName,reqstr);
yao6116601 0:9f5b2dd88884 173 res= response_File(fileName,"text/javascript");
yao6116601 0:9f5b2dd88884 174 } else
yao6116601 0:9f5b2dd88884 175 // if (strcmp(reqstr,"views")==0){ //GET js
yao6116601 0:9f5b2dd88884 176 {
yao6116601 0:9f5b2dd88884 177 // geturistr(reqstr,uristr,2);
yao6116601 0:9f5b2dd88884 178 strcpy(fileName,"/sd/views/");
yao6116601 0:9f5b2dd88884 179 strcat(fileName,reqstr);
yao6116601 0:9f5b2dd88884 180 // strcat(fileName,".html");
yao6116601 0:9f5b2dd88884 181 printf("fileName=%s\n",fileName);
yao6116601 0:9f5b2dd88884 182 res= response_File(fileName,"text/html");
yao6116601 0:9f5b2dd88884 183
yao6116601 0:9f5b2dd88884 184 }
yao6116601 0:9f5b2dd88884 185 if (!res)
yao6116601 0:9f5b2dd88884 186 {
yao6116601 0:9f5b2dd88884 187 sprintf(httpHeader,"HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n");
yao6116601 0:9f5b2dd88884 188 client.send(httpHeader,strlen(httpHeader));
yao6116601 0:9f5b2dd88884 189 client.send("Page not found!\n",16);
yao6116601 0:9f5b2dd88884 190 }
yao6116601 0:9f5b2dd88884 191 } else
yao6116601 0:9f5b2dd88884 192 if (strncmp(buffer, "PUT ", 4)==0){
yao6116601 0:9f5b2dd88884 193 uristr = buffer + 4;
yao6116601 0:9f5b2dd88884 194 eou = strstr(uristr, " ");
yao6116601 0:9f5b2dd88884 195 *eou = 0;
yao6116601 0:9f5b2dd88884 196 geturistr(reqstr,uristr,1);
yao6116601 0:9f5b2dd88884 197 printf("PUT%s+\n",uristr);
yao6116601 0:9f5b2dd88884 198 if (strcmp(reqstr,"LEDControl")==0){
yao6116601 0:9f5b2dd88884 199 geturistr(reqstr,uristr,2);
yao6116601 0:9f5b2dd88884 200 printf("%s\n",reqstr);
yao6116601 0:9f5b2dd88884 201 params= reqstr ;
yao6116601 0:9f5b2dd88884 202 int n=getparamName(temp, params);
yao6116601 0:9f5b2dd88884 203 printf("name=%s\n",temp);
yao6116601 0:9f5b2dd88884 204 params=params+n;
yao6116601 0:9f5b2dd88884 205 n=getparamValue(temp,params);
yao6116601 0:9f5b2dd88884 206 printf("val=%s\n",temp);
yao6116601 0:9f5b2dd88884 207 if (strcmp(temp,"1")==0)
yao6116601 0:9f5b2dd88884 208 { myled=1;}
yao6116601 0:9f5b2dd88884 209 else {myled=0;}
yao6116601 0:9f5b2dd88884 210 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n");
yao6116601 0:9f5b2dd88884 211 client.send(httpHeader,strlen(httpHeader));
yao6116601 0:9f5b2dd88884 212 client.send("OK\n",3);
yao6116601 0:9f5b2dd88884 213 }
yao6116601 0:9f5b2dd88884 214 }else {
yao6116601 0:9f5b2dd88884 215 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
yao6116601 0:9f5b2dd88884 216 client.send(httpHeader,strlen(httpHeader));
yao6116601 0:9f5b2dd88884 217 client.send("BAD The World!\n",15);
yao6116601 0:9f5b2dd88884 218 }
yao6116601 0:9f5b2dd88884 219 // }
yao6116601 0:9f5b2dd88884 220 client.close();
yao6116601 0:9f5b2dd88884 221 }
yao6116601 0:9f5b2dd88884 222 }