Simple server for AGH accademic purpose

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of HTTP_SD_Server_K64F by FRDM-K64F Code Share

Committer:
patlas
Date:
Thu Nov 17 16:35:33 2016 +0000
Revision:
8:a27748c1fc33
Parent:
7:04744a9ce2b9
Child:
9:aebb88e6e653
Simple HTTP server for accademic purpose - SiSK AGH

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsteiert 0:28bc7ce77e20 1 #include "mbed.h"
gsteiert 0:28bc7ce77e20 2 #include "EthernetInterface.h"
patlas 8:a27748c1fc33 3
gsteiert 0:28bc7ce77e20 4 #include <stdio.h>
gsteiert 0:28bc7ce77e20 5 #include <string.h>
gsteiert 0:28bc7ce77e20 6
gsteiert 0:28bc7ce77e20 7 #define HTTPD_SERVER_PORT 80
gsteiert 0:28bc7ce77e20 8 #define HTTPD_MAX_REQ_LENGTH 1023
gsteiert 0:28bc7ce77e20 9 #define HTTPD_MAX_HDR_LENGTH 255
patlas 8:a27748c1fc33 10
gsteiert 3:4f71a37a1ed2 11
gsteiert 3:4f71a37a1ed2 12 Serial uart(USBTX, USBRX);
gsteiert 0:28bc7ce77e20 13
gsteiert 3:4f71a37a1ed2 14
gsteiert 0:28bc7ce77e20 15 EthernetInterface eth;
gsteiert 0:28bc7ce77e20 16 TCPSocketServer server;
gsteiert 0:28bc7ce77e20 17 TCPSocketConnection client;
gsteiert 0:28bc7ce77e20 18
gsteiert 0:28bc7ce77e20 19 char buffer[HTTPD_MAX_REQ_LENGTH+1];
gsteiert 0:28bc7ce77e20 20 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
patlas 8:a27748c1fc33 21
gsteiert 0:28bc7ce77e20 22 char *uristr;
gsteiert 0:28bc7ce77e20 23 char *eou;
gsteiert 0:28bc7ce77e20 24 char *qrystr;
gsteiert 0:28bc7ce77e20 25
patlas 8:a27748c1fc33 26
gsteiert 0:28bc7ce77e20 27 int rdCnt;
gsteiert 0:28bc7ce77e20 28
patlas 8:a27748c1fc33 29 char *first = "/first";
patlas 8:a27748c1fc33 30 char *sec = "/second";
patlas 8:a27748c1fc33 31 void show_page(char* uri)
gsteiert 0:28bc7ce77e20 32 {
patlas 8:a27748c1fc33 33 uart.printf("Trying to open requested uri\n");
patlas 8:a27748c1fc33 34 char *lstchr_ptr = strrchr(uri, NULL) -1; //function try to find char which is non ascii (recently set to 0 so no ascii)
patlas 8:a27748c1fc33 35 if(!memcmp(uri, first, 6)){
patlas 8:a27748c1fc33 36 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
patlas 8:a27748c1fc33 37 client.send(httpHeader,strlen(httpHeader));
patlas 8:a27748c1fc33 38 sprintf(httpHeader,"<html><head><title>First_one</title></head><body><h1>First page has been opened</h1></body></html>");
gsteiert 0:28bc7ce77e20 39 client.send(httpHeader,strlen(httpHeader));
patlas 8:a27748c1fc33 40 }
patlas 8:a27748c1fc33 41 else if(!memcmp(uri, sec, 6)){
patlas 8:a27748c1fc33 42 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
patlas 8:a27748c1fc33 43 client.send(httpHeader,strlen(httpHeader));
patlas 8:a27748c1fc33 44 sprintf(httpHeader,"<html><head><title>Seond_one</title></head><body><h1>Second page has been opened</h1></body></html>");
patlas 8:a27748c1fc33 45 client.send(httpHeader,strlen(httpHeader));
patlas 8:a27748c1fc33 46 }
patlas 8:a27748c1fc33 47 else{
patlas 8:a27748c1fc33 48 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
patlas 8:a27748c1fc33 49 client.send(httpHeader,strlen(httpHeader));
patlas 8:a27748c1fc33 50 sprintf(httpHeader,"<html><head><title>ERROR</title></head><body><h1>THERE IS NO SUCH PAGE</h1></body></html>");
patlas 8:a27748c1fc33 51 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 52 }
gsteiert 0:28bc7ce77e20 53 }
gsteiert 0:28bc7ce77e20 54
gsteiert 0:28bc7ce77e20 55 int main (void)
gsteiert 0:28bc7ce77e20 56 {
gsteiert 3:4f71a37a1ed2 57 // Serial Interface eth;
gsteiert 3:4f71a37a1ed2 58 uart.baud(115200);
gsteiert 5:27b09a0f3fb6 59 uart.printf("Initializing\n");
gsteiert 3:4f71a37a1ed2 60
patlas 8:a27748c1fc33 61 // EthernetInterface eth;
patlas 8:a27748c1fc33 62 uart.printf("Initializing Ethernet ...\n");
patlas 8:a27748c1fc33 63 if(!eth.init("192.168.5.10", "255.255.255.0", "192.168.5.50")){//Init interface with static IP
patlas 8:a27748c1fc33 64 uart.printf("Ethernet interface configured properly\n");
gsteiert 3:4f71a37a1ed2 65 }
patlas 8:a27748c1fc33 66 uart.printf("Setting interface UP ...\n");
gsteiert 0:28bc7ce77e20 67 eth.connect();
patlas 8:a27748c1fc33 68 uart.printf("IP: %s\n", eth.getIPAddress());
patlas 8:a27748c1fc33 69 uart.printf("IP: %s\n", eth.getNetworkMask());
gsteiert 0:28bc7ce77e20 70
gsteiert 0:28bc7ce77e20 71 // TCPSocketServer server;
patlas 8:a27748c1fc33 72 uart.printf("Starting TCP server at port: %d", HTTPD_SERVER_PORT);
gsteiert 0:28bc7ce77e20 73 server.bind(HTTPD_SERVER_PORT);
gsteiert 0:28bc7ce77e20 74 server.listen();
patlas 8:a27748c1fc33 75 uart.printf("Server starts listening at port: %d\n", HTTPD_SERVER_PORT);
gsteiert 0:28bc7ce77e20 76
gsteiert 0:28bc7ce77e20 77 while (true) {
patlas 8:a27748c1fc33 78 uart.printf("\nWaiting for new connection...\r\n");
gsteiert 0:28bc7ce77e20 79 server.accept(client);
gsteiert 0:28bc7ce77e20 80 client.set_blocking(false, 1500); // Timeout after (1.5)s
gsteiert 0:28bc7ce77e20 81
patlas 8:a27748c1fc33 82 uart.printf("Client with IP %s has connected with server.\r\n", client.get_address());
gsteiert 0:28bc7ce77e20 83 while (true) {
gsteiert 0:28bc7ce77e20 84 int n = client.receive(buffer, sizeof(buffer));
gsteiert 0:28bc7ce77e20 85 if (n <= 0) break;
gsteiert 3:4f71a37a1ed2 86 uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
gsteiert 0:28bc7ce77e20 87 if (n >= 1024) {
gsteiert 0:28bc7ce77e20 88 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
gsteiert 0:28bc7ce77e20 89 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 90 client.send(buffer,n);
gsteiert 0:28bc7ce77e20 91 break;
gsteiert 0:28bc7ce77e20 92 } else {
gsteiert 0:28bc7ce77e20 93 buffer[n]=0;
gsteiert 0:28bc7ce77e20 94 }
gsteiert 0:28bc7ce77e20 95 if (!strncmp(buffer, "GET ", 4)) {
gsteiert 0:28bc7ce77e20 96 uristr = buffer + 4;
gsteiert 0:28bc7ce77e20 97 eou = strstr(uristr, " ");
gsteiert 0:28bc7ce77e20 98 if (eou == NULL) {
gsteiert 0:28bc7ce77e20 99 sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
gsteiert 0:28bc7ce77e20 100 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 101 client.send(buffer,n);
gsteiert 0:28bc7ce77e20 102 } else {
gsteiert 0:28bc7ce77e20 103 *eou = 0;
patlas 8:a27748c1fc33 104 show_page(uristr);
gsteiert 0:28bc7ce77e20 105 }
gsteiert 0:28bc7ce77e20 106 }
gsteiert 0:28bc7ce77e20 107 }
gsteiert 0:28bc7ce77e20 108
gsteiert 0:28bc7ce77e20 109 client.close();
gsteiert 0:28bc7ce77e20 110 }
gsteiert 0:28bc7ce77e20 111 }