#include #include #include #include #include #include #include #include #include #define SUCCESS 0 #define ERROR 1 #define END_LINE 0x0 #define SERVER_PORT 1500 #define MAX_MSG 100 int exiting=0; static char* ok_response = "HTTP/1.0 200 OK\n" "Content-type: text/html\n" "\n"; static char* bad_request_response = "HTTP/1.0 400 Bad Request\n" "Content-type: text/html\n" "\n" "\n" " \n" "

Bad Request

\n" "

This server did not understand your request.

\n" " \n" "\n"; static char* not_found_response_template = "HTTP/1.0 404 Not Found\n" "Content-type: text/html\n" "\n" "\n" " \n" "

Not Found

\n" "

The requested URL %s was not found on this server.

\n" " \n" "\n"; static char* bad_method_response_template = "HTTP/1.0 501 Method Not Implemented\n" "Content-type: text/html\n" "\n" "\n" " \n" "

Method Not Implemented

\n" "

The method %s is not implemented by this server.

\n" " \n" "\n"; static char* page = "\n" "

Motion Webinterface

\n" "
  • Show Config\n" "
  • Show Status\n" "
  • Kill\n" "\n"; static char* page_kill = "\n" "

    Motion Kill

    \n" "\n"; static char* page_status = "\n" "

    Motion Status

    \n" "\n"; static char* page_show = "\n" "

    Motion Show

    \n" "\n"; static void signal_handler (int signal_number) { int status; if (signal_number == SIGTERM){ printf("Exiting , Accept one more request and end\n"); exiting = 1; }else{ printf("\nRecieved Signal [%i]\n",signal_number); } } int page_client (client_socket_fd) { int nwrite=0; nwrite = write (client_socket_fd, page, strlen (page)); return 1; } int send_template_client(int client_socket,const char* template) { int nwrite=0; nwrite = write (client_socket, ok_response, strlen (ok_response)); nwrite = write (client_socket,template,strlen(template)); return 1; } int response_client_ok(int client_socket) { int nwrite=0; nwrite = write (client_socket, ok_response, strlen (ok_response)); page_client (client_socket); return 1; } int response_client_no_ok(int client_socket) { int nwrite=0; nwrite = write (client_socket, not_found_response_template , strlen(not_found_response_template)); return 1; } /* Handle GET Request */ int handle_get (int client_socket, const char* url) { if (*url == '/' && strchr (url + 1, '/') == NULL) { if (! (strcmp (url, "/")) ){ response_client_ok(client_socket); }else if(! (strcmp (url, "/show")) ){printf("\nSending page_show\n"); send_template_client(client_socket,page_show);} else if(! (strcmp (url, "/kill")) ){printf("\nSending page_kill\n");send_template_client(client_socket,page_kill);} else if (! (strcmp (url, "/status")) ){printf("\nSending page_status\n");send_template_client(client_socket,page_status);} } else{ printf("\nSending not found\n"); response_client_no_ok(client_socket); } printf("\nhandle GET done\n"); } /* Handle POST Request */ int handle_post (int client_socket, const char* url) { return 1; } int read_client (int client_socket) { int alive=1; char buffer[256]; int length=256; while (alive) { int nread=0; printf("Read\n"); nread = read (client_socket, buffer, length); if (nread == 0){ printf("First Read 0\n"); return -1; } else{ char method[sizeof (buffer)]; char url[sizeof (buffer)]; char protocol[sizeof (buffer)]; buffer[nread] = '\0'; printf("First [%i][%s]",nread,buffer); sscanf (buffer, "%s %s %s", method, url, protocol); while (strstr (buffer, "\r\n\r\n") == NULL){ nread = read (client_socket, buffer, sizeof (buffer)); buffer[nread] = '\0'; printf("[%s]",buffer); } /* Make sure the last read didn't fail. If it did, there's a problem with the connection, so give up. */ if (nread == -1) { return -1; } alive=0; /* Check Protocol */ if (strcmp (protocol, "HTTP/1.0") && strcmp (protocol, "HTTP/1.1")) { /* We don't understand this protocol. Report a bad response. */ write (client_socket, bad_request_response, sizeof (bad_request_response)); return -1; } else if (strcmp (method, "GET")) { /* This server only implements the GET method. The client specified some other method, so report the failure. */ char response[1024]; snprintf (response, sizeof (response),bad_method_response_template, method); write (client_socket, response, strlen (response)); return -1; } else /* A valid request. Process it. */ handle_get (client_socket, url); } } printf("Returning\n"); return 1; } int main (int argc, char *argv[]) { int sd, client_socket_fd, cliLen; int client_sent_quit_message; struct sockaddr_in cliAddr, servAddr; char line[MAX_MSG]; signal (SIGTERM, &signal_handler); /* create socket */ sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) { perror("cannot open socket "); return ERROR; } /* bind server port */ servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(SERVER_PORT); if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) { perror("cannot bind port "); return ERROR; } listen(sd,5); while(!exiting) { printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT); cliLen = sizeof(cliAddr); client_socket_fd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen); if(client_socket_fd<0) { perror("cannot accept connection "); return ERROR; } /* init line */ memset(line,0x0,MAX_MSG); /* Get the Client request */ if ( client_sent_quit_message = read_client (client_socket_fd) != -1) { printf("Success\n"); /* Send the response 200 Ok , 404 Not valid page , 401 Error */ // client_sent_quit_message = response_client (client_socket_fd); /* Send the Page to Client */ // client_sent_quit_message = page_client (client_socket_fd); }else{ printf("Error\n"); } /* Close Connection */ if (client_socket_fd) close(client_socket_fd); } /* while (1) */ close(sd); }