Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can some help me on finding a way to have the HOME_PAGE read from and HTML file

ID: 3889976 • Letter: C

Question

Can some help me on finding a way to have the HOME_PAGE read from and HTML file instead of being hardcode? THis is the code:

/* webserver.c */
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <cnaiapi.h>
#if defined(LINUX) || defined(SOLARIS) #include <sys/time.h> #endif
#define BUFFSIZE 256 #define SERVER_NAME "CNAI Demo Web Server"
#define ERROR_400 "<html><head></head><body><h1>Error 400</h1><p>Th e server couldn't understand your request.</body></html> "
#define ERROR_404 "<html><head></head><body><h1>Error 404</h1><p>Do cument not found.</body></html> "
#define HOME_PAGE "<html><head></head><body><h1>Welcome to the CNAI Demo Server</h1><p>Why not visit: <ul><li><a href="http://netbook.cs.pu rdue.edu">Netbook Home Page</a><li><a href="http://www.comerbooks.com" >Comer Books Home Page</a></ul></body></html> "
#define TIME_PAGE "<html><head></head><body><h1>The current date is : %s</h1></body></html> "
int recvln(connection, char *, int); void send_head(connection, int, int);
/*----------------------------------------------------------------------* * Program: webserver * Purpose: serve hard-coded webpages to web clients * Usage: webserver <appnum> * *----------------------------------------------------------------------*/ int main(int argc, char *argv[]) {
connection conn; int n;
Example Web Server Code 579
char buff[BUFFSIZE], cmd[16], path[64], vers[16]; char *timestr; #if defined(LINUX) || defined(SOLARIS) struct timeval tv; #elif defined(WIN32) time_t tv; #endif
if (argc != 2) { (void) fprintf(stderr, "usage: %s <appnum> ", argv[0]); exit(1); }
while(1) {
/* wait for contact from a client on specified appnum */
conn = await_contact((appnum) atoi(argv[1])); if (conn < 0) exit(1);
/* read and parse the request line */
n = recvln(conn, buff, BUFFSIZE); sscanf(buff, "%s %s %s", cmd, path, vers);
/* skip all headers - read until we get alone */
while((n = recvln(conn, buff, BUFFSIZE)) > 0) { if (n == 2 && buff[0] == ' ' && buff[1] == ' ') break; }
/* check for unexpected end of file */
if (n < 1) { (void) send_eof(conn); continue; }
/* check for a request that we cannot understand */
if (strcmp(cmd, "GET") || (strcmp(vers, "HTTP/1.0") && strcmp(vers, "HTTP/1.1"))) {
580 A Simplified API Appendix 1
send_head(conn, 400, strlen(ERROR_400)); (void) send(conn, ERROR_400, strlen(ERROR_400),0); (void) send_eof(conn); continue;
}
/* send the requested web page or a "not found" error */
if (strcmp(path, "/") == 0) { send_head(conn, 200, strlen(HOME_PAGE)); (void) send(conn, HOME_PAGE, strlen(HOME_PAGE),0); } else if (strcmp(path, "/time") == 0) { #if defined(LINUX) || defined(SOLARIS) gettimeofday(&tv, NULL); timestr = ctime(&tv.tv_sec); #elif defined(WIN32) time(&tv); timestr = ctime(&tv); #endif (void) sprintf(buff, TIME_PAGE, timestr); send_head(conn, 200, strlen(buff)); (void) send(conn, buff, strlen(buff), 0); } else { /* not found */ send_head(conn, 404, strlen(ERROR_404)); (void) send(conn, ERROR_404, strlen(ERROR_404),0); } (void) send_eof(conn); } }
/*----------------------------------------------------------------------* send_head - send an HTTP 1.0 header with given status and content-len *----------------------------------------------------------------------*/ void send_head(connection conn, int stat, int len) { char *statstr, buff[BUFFSIZE];
/* convert the status code to a string */
switch(stat) { case 200: statstr = "OK";
Example Web Server Code 581
break; case 400: statstr = "Bad Request"; break; case 404: statstr = "Not Found"; break; default: statstr = "Unknown"; break; }
/* * send an HTTP/1.0 response with Server, Content-Length, * and Content-Type headers. */
(void) sprintf(buff, "HTTP/1.0 %d %s ", stat, statstr); (void) send(conn, buff, strlen(buff), 0);
(void) sprintf(buff, "Server: %s ", SERVER_NAME); (void) send(conn, buff, strlen(buff), 0);
(void) sprintf(buff, "Content-Length: %d ", len); (void) send(conn, buff, strlen(buff), 0);
(void) sprintf(buff, "Content-Type: text/html "); (void) send(conn, buff, strlen(buff), 0);
(void) sprintf(buff, " "); (void) send(conn, buff, strlen(buff), 0);
}

Explanation / Answer

For managing the client side states, LocalStorage and SessionState are highly helpful. For swapping the web pages inside and out of the browser, a rich SPA style client side should be built. Even in classic servers, the above mentioned LocalStorage and SessionState objects could be used efficiently to provide caching between the HTML pages. These two application program interfaces survive for long years and are supported by ancient to modern browsers.

SessionState & LocalStorage

Designing API using localStorage and sessionState is really simple. Interfaces are common for both objects that contain a string oriented key-value stores which has setItem, key, getItem,removeItem and clear methods.

The objects, localStorage and sessionState are pseudo-array objects which shall be iterated such like array having length property. You also have array indexer for getting and setting the values.

Coding:

set

var time_of_Last_Access=new Date().getTime();

if(sessionStorage)

sessionStorage.setItem(“ownapptime”,time_of_Last_Access.toString());

//retrieving data on other page or refreshing

var currentTime=null;

if(sessionStorage)

currentTime=sessionStorage.getItem(“ownapptime”);

if(currentTime)

currentTime=new Date(currentTime*1);

else

currentTime=new Date();

The object sessionState can store the browser specific information which contains lifetime of active window session or of browser. Once you close the browser, the storage is lost. localStorage makes use of same application program interface (API) but the only difference is it’s storage is permanent unlike sessionState.

Wrapper methods for code simplification:

function session_Count()

{

var totalCount=0;

if (sessionStorage)

{ var totalCount=sessionStorage.getItem(“vv_count”);

totalCount=!totalCount?0:totalCount*1;

}

function session_Count(totalCount)

{

if(sessionStorage)

sessionStorage.getItem(“vv_count”,count.toString());}

Note: Values passed to methods getItem and setItem must be string for JSON parsing.