HOme Sheriff And Lamp

Dependencies:   CameraC328 HCSR04 SDFileSystem WIZnetInterface mbed

Fork of HoSAL by pi bae

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rev_httpFile.cpp Source File

rev_httpFile.cpp

00001 /*
00002 # coded by revival / uasonice (at) gmail.com
00003 # DATE: 2015/08/10 / Mon Aug 10 00:01:54 KST 2015
00004 #
00005 # DESCRIPTION:
00006 #       http file server
00007 */
00008 
00009 #include "mbed.h"
00010 #include "EthernetInterface.h"
00011 #include "SDFileSystem.h"
00012 #include <stdio.h>
00013 #include <string.h>
00014 
00015 #define DEBUG_TYPE 1
00016 #define P_ uart.printf
00017 #include "rev_config.h"
00018 
00019 #include "rev_httpFile.h"
00020 
00021 #if defined(USE_HTTP_FILE_SERVER)
00022 //Serial uart(USBTX, USBRX);
00023 
00024 //TCPSocketServer server;
00025 //TCPSocketConnection client;
00026 
00027 char buffer[HTTPD_MAX_REQ_LENGTH];
00028 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
00029 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
00030 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
00031 char *uristr;
00032 char *eou;
00033 
00034 FILE *fp;
00035 int rdCnt;
00036 
00037 //DigitalOut led1(LED1); //server listning status
00038 //DigitalOut led2(LED2); //socket connecting status
00039 
00040 
00041 void get_file(char* uri)
00042 {
00043     DM_FLN("get_file %s", uri);
00044     char *lstchr = strrchr(uri, NULL) -1;
00045     if ('/' == *lstchr) {
00046         DM_FLN("Open directory /sd%s", uri);
00047         *lstchr = 0;
00048         sprintf(fileName, "/sd%s", uri);
00049         DIR *d = opendir(fileName);
00050         if (d != NULL) {
00051             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00052             client.send(httpHeader,strlen(httpHeader));
00053             sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri);
00054             client.send(httpHeader,strlen(httpHeader));
00055             struct dirent *p;
00056             while((p = readdir(d)) != NULL) {
00057                 sprintf(dirName, "%s/%s", fileName, p->d_name);
00058                 DM_FLN("%s", dirName);
00059                 DIR *subDir = opendir(dirName);
00060                 if (subDir != NULL) {
00061                     sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
00062                 } else {
00063                     sprintf(httpHeader,"<li><a href=\"./image.html?%s\">%s</a></li>", p->d_name, p->d_name);
00064                 }
00065                 client.send(httpHeader,strlen(httpHeader));
00066             }
00067         }
00068         closedir(d);
00069         DM_FLN("Directory closed");
00070         sprintf(httpHeader,"</ul></body></html>");
00071         client.send(httpHeader,strlen(httpHeader));
00072     } else {
00073         char *strFind;
00074         sprintf(fileName, "/sd%s", uri);
00075         if((strFind = strstr(fileName, "?")) != NULL) *strFind = NULL; // remove parameter
00076         fp = fopen(fileName, "r");
00077         if (fp == NULL) {
00078             DM_FLN("File not found");
00079             sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00080             client.send(httpHeader,strlen(httpHeader));
00081             client.send(uri,strlen(uri));
00082         } else {
00083             DM_FLN("Sending: header");
00084             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00085             client.send(httpHeader,strlen(httpHeader));
00086             DM_FLN(" file");
00087             while ((rdCnt = fread(buffer, sizeof( char ), HTTPD_MAX_REQ_LENGTH, fp)) == HTTPD_MAX_REQ_LENGTH) {
00088                 client.send(buffer, rdCnt);
00089                 DM_(".");
00090             }
00091             client.send(buffer, rdCnt);
00092             fclose(fp);
00093             DM_FLN("done");
00094         }
00095     }
00096 }
00097 
00098 void fileServer(/*void const *data*/)
00099 {
00100     int ret;
00101 
00102     //DM_FLN("Wait for new connection...");
00103     ret = server.accept(client);
00104     if(ret < 0) return;
00105     client.set_blocking(false, 1500); // Timeout after 1500ms
00106 
00107     DM_FLN("Connection from: %s", client.get_address());
00108     while (true) {
00109         led2 = true;
00110         int n = client.receive(buffer, sizeof(buffer));
00111         if (n <= 0) break;
00112         DM_FLN("Recieved Data: %d\r\n\r\n%.*s",n,n,buffer);
00113         if (n >= 1024) {
00114             sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00115             client.send(httpHeader,strlen(httpHeader));
00116             client.send(buffer,n);
00117             break;
00118         } else {
00119             buffer[n]=0;
00120         }
00121         if (!strncmp(buffer, "GET ", 4)) {
00122             uristr = buffer + 4;
00123             eou = strstr(uristr, " ");
00124             if (eou == NULL) {
00125                 sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00126                 client.send(httpHeader,strlen(httpHeader));
00127                 client.send(buffer,n);
00128             } else {
00129                 *eou = 0;
00130                 get_file(uristr);
00131             }
00132         }
00133     }
00134     led2 = false;
00135     client.close();
00136     return;
00137 }
00138 #endif // defined(USE_HTTP_FILE_SERVER)
00139 
00140 
00141