Simple server for AGH accademic purpose
Dependencies: EthernetInterface mbed-rtos mbed
Fork of HTTP_SD_Server_K64F by
main.cpp
- Committer:
- patlas
- Date:
- 2016-11-17
- Revision:
- 8:a27748c1fc33
- Parent:
- 7:04744a9ce2b9
- Child:
- 9:aebb88e6e653
File content as of revision 8:a27748c1fc33:
#include "mbed.h" #include "EthernetInterface.h" #include <stdio.h> #include <string.h> #define HTTPD_SERVER_PORT 80 #define HTTPD_MAX_REQ_LENGTH 1023 #define HTTPD_MAX_HDR_LENGTH 255 Serial uart(USBTX, USBRX); EthernetInterface eth; TCPSocketServer server; TCPSocketConnection client; char buffer[HTTPD_MAX_REQ_LENGTH+1]; char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; char *uristr; char *eou; char *qrystr; int rdCnt; char *first = "/first"; char *sec = "/second"; void show_page(char* uri) { 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 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)); } } int main (void) { // Serial Interface eth; uart.baud(115200); uart.printf("Initializing\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"); } uart.printf("Setting interface UP ...\n"); eth.connect(); 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 starts listening at port: %d\n", HTTPD_SERVER_PORT); while (true) { uart.printf("\nWaiting for new connection...\r\n"); server.accept(client); client.set_blocking(false, 1500); // Timeout after (1.5)s 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; uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer); if (n >= 1024) { sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); client.send(buffer,n); break; } else { buffer[n]=0; } if (!strncmp(buffer, "GET ", 4)) { uristr = buffer + 4; eou = strstr(uristr, " "); if (eou == NULL) { sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); client.send(buffer,n); } else { *eou = 0; show_page(uristr); } } } client.close(); } }