![](/media/cache/group/Logo-slogen.png.50x50_q85.png)
HTTP File Server using SD Card for Arch Max
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed
Fork of HTTP_SDcard_file_server by
main.cpp@2:27a5e2b422ee, 2015-11-19 (annotated)
- Committer:
- yihui
- Date:
- Thu Nov 19 06:04:52 2015 +0000
- Revision:
- 2:27a5e2b422ee
- Parent:
- 1:8150297e8849
Use another SDFileSystem - https://developer.mbed.org/users/neilt6/code/SDFileSystem/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
azsymaivan | 0:fbb29d9ea96b | 1 | #include "mbed.h" |
azsymaivan | 0:fbb29d9ea96b | 2 | #include "EthernetInterface.h" |
azsymaivan | 0:fbb29d9ea96b | 3 | #include "SDFileSystem.h" |
azsymaivan | 0:fbb29d9ea96b | 4 | |
azsymaivan | 0:fbb29d9ea96b | 5 | #define EnDebugMSG false //true-> print debug message to PC USB terminal, false->not print |
azsymaivan | 0:fbb29d9ea96b | 6 | #include "filelib.h" |
azsymaivan | 0:fbb29d9ea96b | 7 | |
azsymaivan | 0:fbb29d9ea96b | 8 | #define IP "192.168.100.100" |
azsymaivan | 0:fbb29d9ea96b | 9 | #define MASK "255.255.255.0" |
azsymaivan | 0:fbb29d9ea96b | 10 | #define GATEWAY "192.168.100.1" |
azsymaivan | 0:fbb29d9ea96b | 11 | #define PORT 80 |
azsymaivan | 0:fbb29d9ea96b | 12 | |
azsymaivan | 0:fbb29d9ea96b | 13 | Serial pc (USBTX,USBRX); // tx, rx |
yihui | 1:8150297e8849 | 14 | SDFileSystem sd(SD_MOSI, SD_MISO, SD_SCK, SD_CS, "wfs"); // the pinout on the mbed |
azsymaivan | 0:fbb29d9ea96b | 15 | |
azsymaivan | 0:fbb29d9ea96b | 16 | char sMethod[7]; |
azsymaivan | 0:fbb29d9ea96b | 17 | char sURL[250]; |
azsymaivan | 0:fbb29d9ea96b | 18 | char sProtocol[8]; |
azsymaivan | 0:fbb29d9ea96b | 19 | |
azsymaivan | 0:fbb29d9ea96b | 20 | EthernetInterface eth; |
azsymaivan | 0:fbb29d9ea96b | 21 | |
azsymaivan | 0:fbb29d9ea96b | 22 | TCPSocketServer svr; |
azsymaivan | 0:fbb29d9ea96b | 23 | bool serverIsListened = false; |
azsymaivan | 0:fbb29d9ea96b | 24 | |
azsymaivan | 0:fbb29d9ea96b | 25 | TCPSocketConnection client; |
azsymaivan | 0:fbb29d9ea96b | 26 | bool clientIsConnected = false; |
azsymaivan | 0:fbb29d9ea96b | 27 | |
azsymaivan | 0:fbb29d9ea96b | 28 | char sentBuffer[1608] = {}; // 2*536=1072, 3*536=1608, 4*536=2144 !1500 |
azsymaivan | 0:fbb29d9ea96b | 29 | char line_response[256]= {0}; |
azsymaivan | 0:fbb29d9ea96b | 30 | char file_path[256] = {0}; |
azsymaivan | 0:fbb29d9ea96b | 31 | |
azsymaivan | 0:fbb29d9ea96b | 32 | DigitalOut led1(LED1); //server listning status |
azsymaivan | 0:fbb29d9ea96b | 33 | DigitalOut led2(LED2); //socket connecting status |
azsymaivan | 0:fbb29d9ea96b | 34 | |
azsymaivan | 0:fbb29d9ea96b | 35 | Ticker ledTick; |
azsymaivan | 0:fbb29d9ea96b | 36 | |
azsymaivan | 0:fbb29d9ea96b | 37 | void ledTickfunc() |
azsymaivan | 0:fbb29d9ea96b | 38 | { |
azsymaivan | 0:fbb29d9ea96b | 39 | if(serverIsListened) { |
azsymaivan | 0:fbb29d9ea96b | 40 | led1 = !led1; |
azsymaivan | 0:fbb29d9ea96b | 41 | } else { |
azsymaivan | 0:fbb29d9ea96b | 42 | led1 = false; |
azsymaivan | 0:fbb29d9ea96b | 43 | } |
azsymaivan | 0:fbb29d9ea96b | 44 | } |
azsymaivan | 0:fbb29d9ea96b | 45 | |
azsymaivan | 0:fbb29d9ea96b | 46 | void send_HTTP_header(char* protocol, int code, char* title, char* mime_type, long long lengthBody) |
azsymaivan | 0:fbb29d9ea96b | 47 | { |
azsymaivan | 0:fbb29d9ea96b | 48 | snprintf(line_response, sizeof(line_response),"%s %d %s\r\n", protocol, code, title ); |
azsymaivan | 0:fbb29d9ea96b | 49 | snprintf(sentBuffer, sizeof(sentBuffer),"%s",line_response); |
azsymaivan | 0:fbb29d9ea96b | 50 | |
azsymaivan | 0:fbb29d9ea96b | 51 | if ( mime_type != NULL ) { |
azsymaivan | 0:fbb29d9ea96b | 52 | snprintf(line_response, sizeof(line_response), "Content-Type: %s\r\n", mime_type ); |
azsymaivan | 0:fbb29d9ea96b | 53 | snprintf(sentBuffer, sizeof(sentBuffer), "%s%s",sentBuffer,line_response); //append to sentBuffer |
azsymaivan | 0:fbb29d9ea96b | 54 | } |
azsymaivan | 0:fbb29d9ea96b | 55 | if ( lengthBody >= 0 ) { |
azsymaivan | 0:fbb29d9ea96b | 56 | snprintf(line_response, sizeof(line_response), "Content-Length: %lld\r\n", lengthBody ); |
azsymaivan | 0:fbb29d9ea96b | 57 | snprintf(sentBuffer, sizeof(sentBuffer), "%s%s",sentBuffer,line_response); //append to sentBuffer |
azsymaivan | 0:fbb29d9ea96b | 58 | } |
azsymaivan | 0:fbb29d9ea96b | 59 | snprintf(line_response, sizeof(line_response), "Connection: close\r\n" ); |
azsymaivan | 0:fbb29d9ea96b | 60 | snprintf(sentBuffer, sizeof(sentBuffer),"%s%s\r\n",sentBuffer,line_response); //append to sentBuffer |
azsymaivan | 0:fbb29d9ea96b | 61 | |
azsymaivan | 0:fbb29d9ea96b | 62 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 63 | printf("\n-->sent Header--\n"); |
azsymaivan | 0:fbb29d9ea96b | 64 | |
azsymaivan | 0:fbb29d9ea96b | 65 | client.send_all(sentBuffer,strlen(sentBuffer)); |
azsymaivan | 0:fbb29d9ea96b | 66 | if (EnDebugMSG) { |
azsymaivan | 0:fbb29d9ea96b | 67 | printf(sentBuffer); |
azsymaivan | 0:fbb29d9ea96b | 68 | printf("\n--end Header-- bytes:%d",strlen(sentBuffer)); |
azsymaivan | 0:fbb29d9ea96b | 69 | } |
azsymaivan | 0:fbb29d9ea96b | 70 | Thread::wait(200); //200ms important for browser! |
azsymaivan | 0:fbb29d9ea96b | 71 | } |
azsymaivan | 0:fbb29d9ea96b | 72 | |
azsymaivan | 0:fbb29d9ea96b | 73 | void send_HTML_line(char* line, unsigned int length_line) |
azsymaivan | 0:fbb29d9ea96b | 74 | { |
azsymaivan | 0:fbb29d9ea96b | 75 | client.send_all(line,length_line); |
azsymaivan | 0:fbb29d9ea96b | 76 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 77 | printf("\n-->send HTML line:\n%s ...Ok!",line); |
azsymaivan | 0:fbb29d9ea96b | 78 | Thread::wait(10); |
azsymaivan | 0:fbb29d9ea96b | 79 | } |
azsymaivan | 0:fbb29d9ea96b | 80 | |
azsymaivan | 0:fbb29d9ea96b | 81 | void send_HTML_error( int status_code, char* title, char* body_text) |
azsymaivan | 0:fbb29d9ea96b | 82 | { |
azsymaivan | 0:fbb29d9ea96b | 83 | send_HTTP_header("HTTP/1.1", status_code, title, "text/html", -1); |
azsymaivan | 0:fbb29d9ea96b | 84 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 85 | printf("\n-->send_error...\n"); |
azsymaivan | 0:fbb29d9ea96b | 86 | sentBuffer[0]=NULL; //clear buffer |
azsymaivan | 0:fbb29d9ea96b | 87 | sprintf(line_response, "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<title>%d %s</title>\r\n</head>\r\n", status_code, title); |
azsymaivan | 0:fbb29d9ea96b | 88 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
azsymaivan | 0:fbb29d9ea96b | 89 | sprintf(line_response, "<body><center><h2><center>%d %s</center></h2>\r\n",status_code, title ); |
azsymaivan | 0:fbb29d9ea96b | 90 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
azsymaivan | 0:fbb29d9ea96b | 91 | sprintf(line_response, "%s\r\n", body_text ); |
azsymaivan | 0:fbb29d9ea96b | 92 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
azsymaivan | 0:fbb29d9ea96b | 93 | sprintf(line_response, "<p>mbed HTTP File Server</p>\r\n</center></body></html>\r\n"); |
azsymaivan | 0:fbb29d9ea96b | 94 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
azsymaivan | 0:fbb29d9ea96b | 95 | send_HTML_line(sentBuffer, strlen(sentBuffer)); |
azsymaivan | 0:fbb29d9ea96b | 96 | } |
azsymaivan | 0:fbb29d9ea96b | 97 | |
azsymaivan | 0:fbb29d9ea96b | 98 | int send_file(char *path_file) |
azsymaivan | 0:fbb29d9ea96b | 99 | { |
azsymaivan | 0:fbb29d9ea96b | 100 | char *mime_type = {0}; |
azsymaivan | 0:fbb29d9ea96b | 101 | unsigned int bytes_for_send=0; |
azsymaivan | 0:fbb29d9ea96b | 102 | long long filesize, all_send_bytes = 0; |
azsymaivan | 0:fbb29d9ea96b | 103 | |
azsymaivan | 0:fbb29d9ea96b | 104 | mime_type = get_mime_type( path_file ); |
azsymaivan | 0:fbb29d9ea96b | 105 | snprintf(file_path, sizeof(file_path),"/wfs/%s",path_file); |
azsymaivan | 0:fbb29d9ea96b | 106 | if (EnDebugMSG) { |
azsymaivan | 0:fbb29d9ea96b | 107 | printf("\n-->from send_file:%s",file_path); |
azsymaivan | 0:fbb29d9ea96b | 108 | printf("\n-->from send_file mime type:%s",mime_type); |
azsymaivan | 0:fbb29d9ea96b | 109 | } |
azsymaivan | 0:fbb29d9ea96b | 110 | |
azsymaivan | 0:fbb29d9ea96b | 111 | if (Mystat(path_file, &myStatBuf)) { //fault with file |
azsymaivan | 0:fbb29d9ea96b | 112 | send_HTML_error( 403, "Forbidden", "403 - File access forbidden."); |
azsymaivan | 0:fbb29d9ea96b | 113 | return 403; |
azsymaivan | 0:fbb29d9ea96b | 114 | } |
azsymaivan | 0:fbb29d9ea96b | 115 | FILE* fp = NULL; |
azsymaivan | 0:fbb29d9ea96b | 116 | fp = fopen(file_path,"r"); |
azsymaivan | 0:fbb29d9ea96b | 117 | if (fp==NULL ) { |
azsymaivan | 0:fbb29d9ea96b | 118 | send_HTML_error( 403, "Forbidden", "403 - File access forbidden."); |
azsymaivan | 0:fbb29d9ea96b | 119 | return 403; |
azsymaivan | 0:fbb29d9ea96b | 120 | } |
azsymaivan | 0:fbb29d9ea96b | 121 | |
azsymaivan | 0:fbb29d9ea96b | 122 | filesize = myStatBuf.st_size; |
azsymaivan | 0:fbb29d9ea96b | 123 | send_HTTP_header("HTTP/1.1", 200, "Ok", mime_type, myStatBuf.st_size); |
azsymaivan | 0:fbb29d9ea96b | 124 | //binary send |
azsymaivan | 0:fbb29d9ea96b | 125 | all_send_bytes=0; |
azsymaivan | 0:fbb29d9ea96b | 126 | while(filesize) { //check for EOF !feof(fp) |
azsymaivan | 0:fbb29d9ea96b | 127 | bytes_for_send = filesize; |
azsymaivan | 0:fbb29d9ea96b | 128 | if (bytes_for_send > sizeof(sentBuffer)) { |
azsymaivan | 0:fbb29d9ea96b | 129 | bytes_for_send = sizeof(sentBuffer); |
azsymaivan | 0:fbb29d9ea96b | 130 | } |
azsymaivan | 0:fbb29d9ea96b | 131 | fread (sentBuffer,1,bytes_for_send,fp); |
azsymaivan | 0:fbb29d9ea96b | 132 | filesize -= bytes_for_send; |
azsymaivan | 0:fbb29d9ea96b | 133 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 134 | printf("\n---bytes_for_send...%d",bytes_for_send); |
azsymaivan | 0:fbb29d9ea96b | 135 | client.send_all(sentBuffer,bytes_for_send); |
azsymaivan | 0:fbb29d9ea96b | 136 | //Thread::wait(10); |
azsymaivan | 0:fbb29d9ea96b | 137 | all_send_bytes += bytes_for_send; |
azsymaivan | 0:fbb29d9ea96b | 138 | } |
azsymaivan | 0:fbb29d9ea96b | 139 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 140 | printf("\n---buffer fill end - all ...%lld", all_send_bytes); |
azsymaivan | 0:fbb29d9ea96b | 141 | //binary send |
azsymaivan | 0:fbb29d9ea96b | 142 | |
azsymaivan | 0:fbb29d9ea96b | 143 | sprintf(line_response, "\r\n"); |
azsymaivan | 0:fbb29d9ea96b | 144 | client.send_all(line_response,strlen(line_response)); |
azsymaivan | 0:fbb29d9ea96b | 145 | if ( fp != NULL ) |
azsymaivan | 0:fbb29d9ea96b | 146 | fclose(fp); |
azsymaivan | 0:fbb29d9ea96b | 147 | //Thread::wait(10); |
azsymaivan | 0:fbb29d9ea96b | 148 | return 0; |
azsymaivan | 0:fbb29d9ea96b | 149 | } |
azsymaivan | 0:fbb29d9ea96b | 150 | |
azsymaivan | 0:fbb29d9ea96b | 151 | int send_directory(char *path) |
azsymaivan | 0:fbb29d9ea96b | 152 | { |
azsymaivan | 0:fbb29d9ea96b | 153 | char process_name[64]= {0}; |
azsymaivan | 0:fbb29d9ea96b | 154 | |
azsymaivan | 0:fbb29d9ea96b | 155 | char posOfLastSlash; |
azsymaivan | 0:fbb29d9ea96b | 156 | char *pLS; |
azsymaivan | 0:fbb29d9ea96b | 157 | |
azsymaivan | 0:fbb29d9ea96b | 158 | struct dirent *p; |
azsymaivan | 0:fbb29d9ea96b | 159 | struct sMystat sb; |
azsymaivan | 0:fbb29d9ea96b | 160 | struct tm *timeinfo; |
azsymaivan | 0:fbb29d9ea96b | 161 | char timeBuf[40]; |
azsymaivan | 0:fbb29d9ea96b | 162 | |
azsymaivan | 0:fbb29d9ea96b | 163 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 164 | printf("\n-->from send_directory:%s",path); |
azsymaivan | 0:fbb29d9ea96b | 165 | snprintf(file_path,sizeof(file_path),"/wfs%s",path); |
azsymaivan | 0:fbb29d9ea96b | 166 | DIR *d = opendir(file_path); |
azsymaivan | 0:fbb29d9ea96b | 167 | if (EnDebugMSG && d!=NULL) |
azsymaivan | 0:fbb29d9ea96b | 168 | printf("\n-->from send_directory:%s ...open OK",file_path); |
azsymaivan | 0:fbb29d9ea96b | 169 | if (d==NULL) { //error open dir |
azsymaivan | 0:fbb29d9ea96b | 170 | send_HTML_error( 403, "Forbidden", "403 - Directory access forbidden."); |
azsymaivan | 0:fbb29d9ea96b | 171 | return -1; |
azsymaivan | 0:fbb29d9ea96b | 172 | } |
azsymaivan | 0:fbb29d9ea96b | 173 | send_HTTP_header("HTTP/1.1", 200, "Ok",NULL, -1); |
azsymaivan | 0:fbb29d9ea96b | 174 | sentBuffer[0]=NULL; |
azsymaivan | 0:fbb29d9ea96b | 175 | sprintf(line_response,"<!DOCTYPE html>\r\n<html>\n<head><title>Index of %s</title>\n",path); |
azsymaivan | 0:fbb29d9ea96b | 176 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
yihui | 1:8150297e8849 | 177 | |
azsymaivan | 0:fbb29d9ea96b | 178 | sprintf(line_response,"<meta content=\"text/html; charset=iso-8859-1\" http-equiv=\"Content-Type\"></head>\n"); |
azsymaivan | 0:fbb29d9ea96b | 179 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
yihui | 1:8150297e8849 | 180 | |
yihui | 1:8150297e8849 | 181 | |
azsymaivan | 0:fbb29d9ea96b | 182 | sprintf(line_response,"<body><center>\n<h3>Index of %s</h3>\n", path); |
azsymaivan | 0:fbb29d9ea96b | 183 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
azsymaivan | 0:fbb29d9ea96b | 184 | send_HTML_line(sentBuffer, strlen(sentBuffer)); |
azsymaivan | 0:fbb29d9ea96b | 185 | //begin table |
azsymaivan | 0:fbb29d9ea96b | 186 | sentBuffer[0]=NULL; //clear buffer |
azsymaivan | 0:fbb29d9ea96b | 187 | sprintf(line_response,"<table border=\"0\">\n"); |
azsymaivan | 0:fbb29d9ea96b | 188 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
azsymaivan | 0:fbb29d9ea96b | 189 | sprintf(line_response,"<tr><th align=\"left\" width=\"200\">Name</th><th align=\"right\" width=\"100\">Size(bytes)</th><th align=\"right\" width=\"200\">Date/Time</th></tr>\n"); |
azsymaivan | 0:fbb29d9ea96b | 190 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
azsymaivan | 0:fbb29d9ea96b | 191 | //begin table |
azsymaivan | 0:fbb29d9ea96b | 192 | |
azsymaivan | 0:fbb29d9ea96b | 193 | pLS=strrchr(path,'/'); |
azsymaivan | 0:fbb29d9ea96b | 194 | posOfLastSlash=pLS-path+1; |
azsymaivan | 0:fbb29d9ea96b | 195 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 196 | printf("\r\n>>posOfLastSlash=%d",posOfLastSlash); |
azsymaivan | 0:fbb29d9ea96b | 197 | snprintf(process_name,posOfLastSlash+1,"%s",path); |
azsymaivan | 0:fbb29d9ea96b | 198 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 199 | printf("\r\n>>process_name=%s",process_name); |
azsymaivan | 0:fbb29d9ea96b | 200 | sprintf(line_response,"<tr><td align=\"left\"><a href=\"%s\">../</a></td></tr>\n",process_name); |
azsymaivan | 0:fbb29d9ea96b | 201 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
azsymaivan | 0:fbb29d9ea96b | 202 | while((p = readdir(d)) != NULL) { |
azsymaivan | 0:fbb29d9ea96b | 203 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 204 | printf("\n :%s",p->d_name); |
azsymaivan | 0:fbb29d9ea96b | 205 | sprintf(file_path,"%s/%s",path,p->d_name); |
azsymaivan | 0:fbb29d9ea96b | 206 | Mystat( file_path, &sb ); |
azsymaivan | 0:fbb29d9ea96b | 207 | if (get_dirInfo(file_path)==0 ) { //this is directory path |
azsymaivan | 0:fbb29d9ea96b | 208 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 209 | printf("\nDIR"); |
azsymaivan | 0:fbb29d9ea96b | 210 | sprintf(line_response, "<tr><td align=\"left\"><a href=\"%s\">%s</a><br></td></tr>\n",file_path,p->d_name); |
azsymaivan | 0:fbb29d9ea96b | 211 | if (strlen(line_response)>(sizeof(sentBuffer)-strlen(sentBuffer))) { //buffer must be sent |
azsymaivan | 0:fbb29d9ea96b | 212 | send_HTML_line(sentBuffer, strlen(sentBuffer)); |
azsymaivan | 0:fbb29d9ea96b | 213 | sentBuffer[0]=NULL; //clear buffer |
azsymaivan | 0:fbb29d9ea96b | 214 | } |
azsymaivan | 0:fbb29d9ea96b | 215 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
azsymaivan | 0:fbb29d9ea96b | 216 | |
azsymaivan | 0:fbb29d9ea96b | 217 | } else { //this is file |
azsymaivan | 0:fbb29d9ea96b | 218 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 219 | printf("\nFILE"); |
azsymaivan | 0:fbb29d9ea96b | 220 | timeinfo = localtime (&sb.st_mtime); |
azsymaivan | 0:fbb29d9ea96b | 221 | //strftime(timeBuf,40, "%I:%M:%S %p (%Y/%m/%d)\r\n", localtime(&sb.st_mtime)); |
azsymaivan | 0:fbb29d9ea96b | 222 | strftime(timeBuf, 40, "%c", timeinfo); |
azsymaivan | 0:fbb29d9ea96b | 223 | sprintf(line_response, "<tr><td align=\"left\"><a href=\"%s\">%s</a></td><td align=\"right\">%lld</td><td align=\"right\">%s</td></tr>\n", file_path, p->d_name,(long long) sb.st_size,timeBuf); // asctime(timeinfo) ); |
azsymaivan | 0:fbb29d9ea96b | 224 | |
azsymaivan | 0:fbb29d9ea96b | 225 | if (strlen(line_response)>(sizeof(sentBuffer)-strlen(sentBuffer))) { //buffer must be sent |
azsymaivan | 0:fbb29d9ea96b | 226 | send_HTML_line(sentBuffer, strlen(sentBuffer)); |
azsymaivan | 0:fbb29d9ea96b | 227 | sentBuffer[0]=NULL; //clear buffer |
azsymaivan | 0:fbb29d9ea96b | 228 | } |
azsymaivan | 0:fbb29d9ea96b | 229 | snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer |
azsymaivan | 0:fbb29d9ea96b | 230 | } |
azsymaivan | 0:fbb29d9ea96b | 231 | } |
azsymaivan | 0:fbb29d9ea96b | 232 | send_HTML_line(sentBuffer, strlen(sentBuffer)); |
azsymaivan | 0:fbb29d9ea96b | 233 | closedir(d); |
azsymaivan | 0:fbb29d9ea96b | 234 | |
azsymaivan | 0:fbb29d9ea96b | 235 | sprintf(line_response, "</table>\n<br><h4>mbed HTTP File Server</h4>\n</center></body></html>\n"); |
azsymaivan | 0:fbb29d9ea96b | 236 | send_HTML_line(line_response, strlen(line_response)); |
azsymaivan | 0:fbb29d9ea96b | 237 | |
azsymaivan | 0:fbb29d9ea96b | 238 | return 0; |
azsymaivan | 0:fbb29d9ea96b | 239 | } |
azsymaivan | 0:fbb29d9ea96b | 240 | |
azsymaivan | 0:fbb29d9ea96b | 241 | void parseHTTPRequest(char* buffer) |
azsymaivan | 0:fbb29d9ea96b | 242 | { |
azsymaivan | 0:fbb29d9ea96b | 243 | char spacePos; |
azsymaivan | 0:fbb29d9ea96b | 244 | char *tmpBuffer; |
azsymaivan | 0:fbb29d9ea96b | 245 | spacePos = strcspn(buffer, " ") + 1; //position of first space character |
azsymaivan | 0:fbb29d9ea96b | 246 | snprintf(sMethod, spacePos,"%s", buffer); |
azsymaivan | 0:fbb29d9ea96b | 247 | |
azsymaivan | 0:fbb29d9ea96b | 248 | //get Protocol |
azsymaivan | 0:fbb29d9ea96b | 249 | tmpBuffer=&(buffer[spacePos]); //move pointer to buffer (delete Method) |
azsymaivan | 0:fbb29d9ea96b | 250 | spacePos = strcspn(tmpBuffer, "\r\n") + 1; |
azsymaivan | 0:fbb29d9ea96b | 251 | tmpBuffer[spacePos]='\0'; //set end of string ...cut |
azsymaivan | 0:fbb29d9ea96b | 252 | sprintf(sProtocol, "%s", strrchr(tmpBuffer,' ')); //get string after last (space ) |
azsymaivan | 0:fbb29d9ea96b | 253 | printf("\r\nsProtocol:%s", tmpBuffer); |
azsymaivan | 0:fbb29d9ea96b | 254 | buffer = &(sProtocol[1]); //cut first character (space) |
azsymaivan | 0:fbb29d9ea96b | 255 | sprintf(sProtocol, "%s", buffer); |
azsymaivan | 0:fbb29d9ea96b | 256 | |
azsymaivan | 0:fbb29d9ea96b | 257 | //get URL |
azsymaivan | 0:fbb29d9ea96b | 258 | snprintf(sURL,strlen(tmpBuffer)-strlen(sProtocol),"%s\r\n", tmpBuffer); //URL is between Method and Protocol |
azsymaivan | 0:fbb29d9ea96b | 259 | |
azsymaivan | 0:fbb29d9ea96b | 260 | printf("\nParse Method:%s",sMethod); |
azsymaivan | 0:fbb29d9ea96b | 261 | printf("\nParse URL:%s",sURL); |
azsymaivan | 0:fbb29d9ea96b | 262 | printf("\nParse PROTOCOL:%s",sProtocol); |
azsymaivan | 0:fbb29d9ea96b | 263 | printf("\n\r\n"); |
azsymaivan | 0:fbb29d9ea96b | 264 | } |
azsymaivan | 0:fbb29d9ea96b | 265 | |
azsymaivan | 0:fbb29d9ea96b | 266 | int processHTTP(char* sMethod, char* sURL, char* sProtocol) |
azsymaivan | 0:fbb29d9ea96b | 267 | { |
azsymaivan | 0:fbb29d9ea96b | 268 | int gdi, gfi; //status of get_dir_info(xxx), and get_file_info(xxx) |
azsymaivan | 0:fbb29d9ea96b | 269 | |
azsymaivan | 0:fbb29d9ea96b | 270 | if (strcmp(sMethod,"GET")!=0) { |
azsymaivan | 0:fbb29d9ea96b | 271 | send_HTML_error( 501, "501 Not Implemented", "501 - The server either does not recognize the request method"); |
azsymaivan | 0:fbb29d9ea96b | 272 | return 501; |
azsymaivan | 0:fbb29d9ea96b | 273 | } |
azsymaivan | 0:fbb29d9ea96b | 274 | if (sURL[0]!= '/') { |
azsymaivan | 0:fbb29d9ea96b | 275 | send_HTML_error( 400, "Bad Request", "400 - The request cannot be fulfilled due to bad syntax."); |
azsymaivan | 0:fbb29d9ea96b | 276 | return 400; |
azsymaivan | 0:fbb29d9ea96b | 277 | } |
azsymaivan | 0:fbb29d9ea96b | 278 | if (sURL[strlen(sURL)-1]=='/') { |
azsymaivan | 0:fbb29d9ea96b | 279 | sURL[strlen(sURL)-1]=sURL[strlen(sURL)]; //delete last symbol |
azsymaivan | 0:fbb29d9ea96b | 280 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 281 | printf("\n delete last:%s",sURL); |
azsymaivan | 0:fbb29d9ea96b | 282 | } |
azsymaivan | 0:fbb29d9ea96b | 283 | gdi= get_dirInfo(sURL); |
azsymaivan | 0:fbb29d9ea96b | 284 | gfi= get_fileInfo(sURL); |
azsymaivan | 0:fbb29d9ea96b | 285 | if (gfi!=0) { //!=0 file not found |
azsymaivan | 0:fbb29d9ea96b | 286 | if (gdi==0) { //0-ok this is directory |
azsymaivan | 0:fbb29d9ea96b | 287 | return send_directory(sURL); |
azsymaivan | 0:fbb29d9ea96b | 288 | } |
azsymaivan | 0:fbb29d9ea96b | 289 | if (EnDebugMSG) |
azsymaivan | 0:fbb29d9ea96b | 290 | printf("\n404-br File not found or...(Fresult is:%d)",gfi); |
azsymaivan | 0:fbb29d9ea96b | 291 | send_HTML_error( 404, "Not Found","404 - The requested resource could not be found."); |
azsymaivan | 0:fbb29d9ea96b | 292 | return 404; |
azsymaivan | 0:fbb29d9ea96b | 293 | } else { //==0 found |
azsymaivan | 0:fbb29d9ea96b | 294 | if (gdi==0) //0-ok this is directory |
azsymaivan | 0:fbb29d9ea96b | 295 | return send_directory(sURL); |
azsymaivan | 0:fbb29d9ea96b | 296 | else |
azsymaivan | 0:fbb29d9ea96b | 297 | return send_file(sURL); |
azsymaivan | 0:fbb29d9ea96b | 298 | } |
azsymaivan | 0:fbb29d9ea96b | 299 | } |
azsymaivan | 0:fbb29d9ea96b | 300 | |
azsymaivan | 0:fbb29d9ea96b | 301 | int main() |
azsymaivan | 0:fbb29d9ea96b | 302 | { |
yihui | 1:8150297e8849 | 303 | printf("\r\n---- HTTP Server ----\r\n"); |
azsymaivan | 0:fbb29d9ea96b | 304 | |
azsymaivan | 0:fbb29d9ea96b | 305 | ledTick.attach(&ledTickfunc,0.5); |
azsymaivan | 0:fbb29d9ea96b | 306 | //ledTick.detach(); |
azsymaivan | 0:fbb29d9ea96b | 307 | //setup ethernet interface |
yihui | 1:8150297e8849 | 308 | eth.init(); //Use DHCP |
yihui | 1:8150297e8849 | 309 | // eth.init(IP,MASK,GATEWAY); //IP,mask,Gateway |
azsymaivan | 0:fbb29d9ea96b | 310 | eth.connect(); |
azsymaivan | 0:fbb29d9ea96b | 311 | printf("IP Address is %s\n\r", eth.getIPAddress()); |
azsymaivan | 0:fbb29d9ea96b | 312 | |
azsymaivan | 0:fbb29d9ea96b | 313 | //setup tcp socket |
azsymaivan | 0:fbb29d9ea96b | 314 | if(svr.bind(PORT)< 0) { |
azsymaivan | 0:fbb29d9ea96b | 315 | printf("tcp server bind failed.\n\r"); |
azsymaivan | 0:fbb29d9ea96b | 316 | return -1; |
azsymaivan | 0:fbb29d9ea96b | 317 | } else { |
azsymaivan | 0:fbb29d9ea96b | 318 | printf("tcp server bind successed.\n\r"); |
azsymaivan | 0:fbb29d9ea96b | 319 | serverIsListened = true; |
azsymaivan | 0:fbb29d9ea96b | 320 | } |
azsymaivan | 0:fbb29d9ea96b | 321 | |
azsymaivan | 0:fbb29d9ea96b | 322 | if(svr.listen(1) < 0) { |
azsymaivan | 0:fbb29d9ea96b | 323 | printf("tcp server listen failed.\n\r"); |
azsymaivan | 0:fbb29d9ea96b | 324 | return -1; |
azsymaivan | 0:fbb29d9ea96b | 325 | } else { |
azsymaivan | 0:fbb29d9ea96b | 326 | printf("tcp server is listening...\n\r"); |
azsymaivan | 0:fbb29d9ea96b | 327 | } |
azsymaivan | 0:fbb29d9ea96b | 328 | |
azsymaivan | 0:fbb29d9ea96b | 329 | //listening for http GET request |
azsymaivan | 0:fbb29d9ea96b | 330 | while (serverIsListened) { |
azsymaivan | 0:fbb29d9ea96b | 331 | //blocking mode(never timeout) |
azsymaivan | 0:fbb29d9ea96b | 332 | if(svr.accept(client)<0) { |
azsymaivan | 0:fbb29d9ea96b | 333 | printf("failed to accept connection.\n\r"); |
azsymaivan | 0:fbb29d9ea96b | 334 | } else { |
azsymaivan | 0:fbb29d9ea96b | 335 | //client.set_blocking(false,5000); //5000=5sec |
azsymaivan | 0:fbb29d9ea96b | 336 | printf("connection success!\n\rIP: %s\n\r",client.get_address()); |
azsymaivan | 0:fbb29d9ea96b | 337 | clientIsConnected = true; |
azsymaivan | 0:fbb29d9ea96b | 338 | led2 = true; |
azsymaivan | 0:fbb29d9ea96b | 339 | while(clientIsConnected) { |
azsymaivan | 0:fbb29d9ea96b | 340 | char buffer[1024] = {}; |
azsymaivan | 0:fbb29d9ea96b | 341 | switch(client.receive(buffer, 1023)) { |
azsymaivan | 0:fbb29d9ea96b | 342 | case 0: |
azsymaivan | 0:fbb29d9ea96b | 343 | printf("recieved buffer is empty.\n\r"); |
azsymaivan | 0:fbb29d9ea96b | 344 | clientIsConnected = false; |
azsymaivan | 0:fbb29d9ea96b | 345 | break; |
azsymaivan | 0:fbb29d9ea96b | 346 | case -1: |
azsymaivan | 0:fbb29d9ea96b | 347 | printf("failed to read data from client.\n\r"); |
azsymaivan | 0:fbb29d9ea96b | 348 | clientIsConnected = false; |
azsymaivan | 0:fbb29d9ea96b | 349 | break; |
azsymaivan | 0:fbb29d9ea96b | 350 | default: |
azsymaivan | 0:fbb29d9ea96b | 351 | printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); |
azsymaivan | 0:fbb29d9ea96b | 352 | parseHTTPRequest(buffer); |
azsymaivan | 0:fbb29d9ea96b | 353 | //if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) { |
azsymaivan | 0:fbb29d9ea96b | 354 | if (strcmp(sMethod, "GET" ) == 0 ) { |
azsymaivan | 0:fbb29d9ea96b | 355 | printf("GET request incomming.\n\r"); |
azsymaivan | 0:fbb29d9ea96b | 356 | processHTTP(sMethod, sURL, sProtocol); |
azsymaivan | 0:fbb29d9ea96b | 357 | clientIsConnected = false; |
azsymaivan | 0:fbb29d9ea96b | 358 | }//if get |
azsymaivan | 0:fbb29d9ea96b | 359 | break; |
azsymaivan | 0:fbb29d9ea96b | 360 | } //switch |
azsymaivan | 0:fbb29d9ea96b | 361 | //ledTick.attach(&ledTickfunc,0.5); |
azsymaivan | 0:fbb29d9ea96b | 362 | }//while |
azsymaivan | 0:fbb29d9ea96b | 363 | printf("close connection.\n\rHTTP server is listening...\n\r\n"); |
azsymaivan | 0:fbb29d9ea96b | 364 | client.close(); |
azsymaivan | 0:fbb29d9ea96b | 365 | Thread::wait(50); |
azsymaivan | 0:fbb29d9ea96b | 366 | led2 = false; |
azsymaivan | 0:fbb29d9ea96b | 367 | } |
azsymaivan | 0:fbb29d9ea96b | 368 | } |
azsymaivan | 0:fbb29d9ea96b | 369 | |
azsymaivan | 0:fbb29d9ea96b | 370 | } |