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