Simple server for AGH accademic purpose

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of HTTP_SD_Server_K64F by FRDM-K64F Code Share

Revision:
8:a27748c1fc33
Parent:
7:04744a9ce2b9
Child:
9:aebb88e6e653
diff -r 04744a9ce2b9 -r a27748c1fc33 main.cpp
--- a/main.cpp	Sun Apr 12 03:04:28 2015 +0000
+++ b/main.cpp	Thu Nov 17 16:35:33 2016 +0000
@@ -1,20 +1,16 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
-#include "SDFileSystem.h"
+
 #include <stdio.h>
 #include <string.h>
 
 #define HTTPD_SERVER_PORT   80
 #define HTTPD_MAX_REQ_LENGTH   1023
 #define HTTPD_MAX_HDR_LENGTH   255
-#define HTTPD_MAX_FNAME_LENGTH   127
-#define HTTPD_MAX_DNAME_LENGTH   127
+
 
 Serial uart(USBTX, USBRX);
 
-//SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
-//SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
-SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F
 
 EthernetInterface eth;
 TCPSocketServer server;
@@ -22,67 +18,37 @@
 
 char buffer[HTTPD_MAX_REQ_LENGTH+1];
 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
-char fileName[HTTPD_MAX_FNAME_LENGTH+1];
-char dirName[HTTPD_MAX_DNAME_LENGTH+1];
+
 char *uristr;
 char *eou;
 char *qrystr;
 
-FILE *fp;
+
 int rdCnt;
 
-void get_file(char* uri)
+char *first = "/first";
+char *sec = "/second";
+void show_page(char* uri)
 {
-    uart.printf("get_file %s\n", uri);
-    char *lstchr = strrchr(uri, NULL) -1;
-    if ('/' == *lstchr) {
-        uart.printf("Open directory /sd%s\n", uri);
-        *lstchr = 0;
-        sprintf(fileName, "/sd%s", uri);
-        DIR *d = opendir(fileName);
-        if (d != NULL) {
-            sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
-            client.send(httpHeader,strlen(httpHeader));
-            sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri);
-            client.send(httpHeader,strlen(httpHeader));
-            struct dirent *p;
-            while((p = readdir(d)) != NULL) {
-                sprintf(dirName, "%s/%s", fileName, p->d_name);
-                uart.printf("%s\n", dirName);
-                DIR *subDir = opendir(dirName);
-                if (subDir != NULL) {
-                    sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
-                } else {
-                    sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name);
-                }
-                client.send(httpHeader,strlen(httpHeader));
-            }
-        }
-        closedir(d);
-        uart.printf("Directory closed\n");
-        sprintf(httpHeader,"</ul></body></html>");
+    uart.printf("Trying to open requested uri\n");
+    char *lstchr_ptr = strrchr(uri, NULL) -1; //function try to find char which is non ascii (recently set to 0 so no ascii)
+    if(!memcmp(uri, first, 6)){
+        sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
+        client.send(httpHeader,strlen(httpHeader));
+        sprintf(httpHeader,"<html><head><title>First_one</title></head><body><h1>First page has been opened</h1></body></html>");
         client.send(httpHeader,strlen(httpHeader));
-    } else {
-        sprintf(fileName, "/sd%s", uri);
-        fp = fopen(fileName, "r");
-        if (fp == NULL) {
-            uart.printf("File not found\n");
-            sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
-            client.send(httpHeader,strlen(httpHeader));
-            client.send(uri,strlen(uri));
-        } else {
-            uart.printf("Sending: header");
-            sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
-            client.send(httpHeader,strlen(httpHeader));
-            uart.printf(" file");
-            while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
-                client.send(buffer, rdCnt);
-                uart.printf(".");
-            }
-            client.send(buffer, rdCnt);
-            fclose(fp);
-            uart.printf("done\n");
-        }
+    }
+    else if(!memcmp(uri, sec, 6)){
+        sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
+        client.send(httpHeader,strlen(httpHeader));
+        sprintf(httpHeader,"<html><head><title>Seond_one</title></head><body><h1>Second page has been opened</h1></body></html>");
+        client.send(httpHeader,strlen(httpHeader));
+    }
+    else{
+        sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
+        client.send(httpHeader,strlen(httpHeader));
+        sprintf(httpHeader,"<html><head><title>ERROR</title></head><body><h1>THERE IS NO SUCH PAGE</h1></body></html>");
+        client.send(httpHeader,strlen(httpHeader));
     }
 }
 
@@ -92,33 +58,28 @@
     uart.baud(115200);
     uart.printf("Initializing\n");
 
-//    Check File System
-    uart.printf("Checking File System\n");
-    DIR *d = opendir("/sd/");
-    if (d != NULL) {
-        uart.printf("SD Card Present\n");
-    } else {
-        uart.printf("SD Card Root Directory Not Found\n");
+//    EthernetInterface eth;
+    uart.printf("Initializing Ethernet ...\n");
+    if(!eth.init("192.168.5.10", "255.255.255.0", "192.168.5.50")){//Init interface with static IP
+        uart.printf("Ethernet interface configured properly\n");
     }
-
-//    EthernetInterface eth;
-    uart.printf("Initializing Ethernet\n");
-    eth.init(); //Use DHCP
-    uart.printf("Connecting\n");
+    uart.printf("Setting interface UP ...\n");
     eth.connect();
-    uart.printf("IP Address is %s\n", eth.getIPAddress());
+    uart.printf("IP: %s\n", eth.getIPAddress());
+    uart.printf("IP: %s\n", eth.getNetworkMask());
 
 //    TCPSocketServer server;
+    uart.printf("Starting TCP server at port: %d", HTTPD_SERVER_PORT);
     server.bind(HTTPD_SERVER_PORT);
     server.listen();
-    uart.printf("Server Listening\n");
+    uart.printf("Server starts listening at port: %d\n", HTTPD_SERVER_PORT);
 
     while (true) {
-        uart.printf("\nWait for new connection...\r\n");
+        uart.printf("\nWaiting for new connection...\r\n");
         server.accept(client);
         client.set_blocking(false, 1500); // Timeout after (1.5)s
 
-        uart.printf("Connection from: %s\r\n", client.get_address());
+        uart.printf("Client with IP %s has connected with server.\r\n", client.get_address());
         while (true) {
             int n = client.receive(buffer, sizeof(buffer));
             if (n <= 0) break;
@@ -140,7 +101,7 @@
                     client.send(buffer,n);
                 } else {
                     *eou = 0;
-                    get_file(uristr);
+                    show_page(uristr);
                 }
             }
         }