Computer Systems: A Programmer's Perspective (3rd Edition)
Computer Systems: A Programmer's Perspective (3rd Edition)
3rd Edition
ISBN: 9780134092669
Author: Bryant, Randal E. Bryant, David R. O'Hallaron, David R., Randal E.; O'Hallaron, Bryant/O'hallaron
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 12, Problem 12.37HW

Explanation of Solution

Implementation of a concurrent version of the TINY web server on I/O threads:

Modified “main.c” code:

#include "csapp.h"

//Function declaration

void doit(int fd);

void read_requesthdrs(rio_t *rp);

int parse_uri(char *uri, char *filename, char *cgiargs);

void serve_static(int fd, char *filename, int filesize);

void get_filetype(char *filename, char *filetype);

void serve_dynamic(int fd, char *filename, char *cgiargs);

void clienterror(int fd, char *cause, char *errnum,char *shortmsg, char *longmsg);

void *thread(void *vargp);

//Main function

int main(int argc, char **argv)

{

  //Declare variable

  int listenfd, connfd;

  int *connfdp;

  pthread_t tid;

  char hostname[MAXLINE], port[MAXLINE];

  socklen_t clientlen;

  struct sockaddr_storage clientaddr;

/* Check the command line argument. if the argument does not satify, then  */

  if (argc != 2)

  {

    //Display the below error message

    fprintf(stderr, "usage: %s <port>\n", argv[0]);

    fprintf(stderr, "use default port 5000\n");

    listenfd = Open_listenfd("5000");

  }

  //Otherwise,

  else

  {

    //Call the Open_listenfd method

    listenfd = Open_listenfd(argv[1]);

  }

  //Check condition

  while (1)

  {

    //Get the length the client address

    clientlen = sizeof(clientaddr);

    //For connection accept

    connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

    //Call the method

Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE,

        port, MAXLINE, 0);

    //Display the accepted connection

printf("Accepted connection from (%s, %s)\n", hostname, port);

    connfdp = (int*)Malloc(sizeof(int));

    *connfdp = connfd;

    //Call Pthread_create method

    Pthread_create(&tid, NULL, thread, connfdp);

  }

}

//Function definition for thread method

void *thread(void *vargp)

{

  //Compute the connection file descriptor number

  int connfd = *(int*)vargp;

  //Call pthread detach method

  Pthread_detach(Pthread_self());

  //Free the given file

  Free(vargp);

  //Call doit method

  doit(connfd);

  //Close the connection

  Close(connfd);

  return NULL;

}

/* Function definition for doit method that is for handle HTTP request or response transfer */

void doit(int fd)

{

  //Declare variable

  int is_static;

  struct stat sbuf;

char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];

  char filename[MAXLINE], cgiargs[MAXLINE];

  //Create object for rio function

  rio_t rio;

  /* Read request line and headers */

  Rio_readinitb(&rio, fd);

  //Check read request

  if (!Rio_readlineb(&rio, buf, MAXLINE)) 

    return;

  //Display the buffer

  printf("%s", buf);

  //Display the parse request

  sscanf(buf, "%s %s %s", method, uri, version); 

  //Check condition  

  if (strcasecmp(method, "GET"))

  {              

    //Call client error method

clienterror(fd, method, "501", "Not Implemented","Tiny does not implement this method");

    return;

  }        

  //Call read_requestdrs method

  read_requesthdrs(&rio);

  /* Parse URI from GET request */

  is_static = parse_uri(uri, filename, cgiargs);     

  //Check the static connection

  if (stat(filename, &sbuf) < 0)

  {            

    //Call client error method     

    clienterror(fd, filename, "404", "Not found",

        "Tiny couldn't find this file");

    return;

  }

  /* Check serve static content */

  if (is_static)

  {

if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode))

    {     

      //Call client error method

clienterror(fd, filename, "403", "Forbidden","Tiny couldn't read the file");

      return;

    }

    //Call serve_static method

    serve_static(fd, filename, sbuf.st_size);

  }

  //Otherwise check the serve dynamic content

  else

  {

if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode))

    {

      //Call client error method

clienterror(fd, filename, "403", "Forbidden","Tiny couldn't run the CGI program");

      return;

    }

    //Call serve dynamic method

    serve_dynamic(fd, filename, cgiargs);

  }

}

/* Function definition for read_requestdrs that is for read HTTP request headers */

void read_requesthdrs(rio_t *rp)

{

  //Declare variable

  char buf[MAXLINE];

  Rio_readlineb(rp, buf, MAXLINE);

  printf("%s", buf);

  //Check condition

  while(strcmp(buf, "\r\n"))

  {       

    //Call rio readlineb function

    Rio_readlineb(rp, buf, MAXLINE);

    printf("%s", buf);

  }

  return;

}

/* Function definition for parse uri into file nane and cgi argument */

int parse_uri(char *uri, char *filename, char *cgiargs)

{

  //Declare char variable

  char *ptr;

  //Check content type...

Blurred answer
Students have asked these similar questions
Please answer the JAVA OOP Programming Assignment scenario below: Patriot Ships is a new cruise line company which has a fleet of 10 cruise ships, each with a capacity of 300 passengers. To manage its operations efficiently, the company is looking for a program that can help track its fleet, manage bookings, and calculate revenue for each cruise. Each cruise is tracked by a Cruise Identifier (must be 5 characters long), cruise route (e.g. Miami to Nassau), and ticket price. The program should also track how many tickets have been sold for each cruise. Create an object-oriented solution with a menu that allows a user to select one of the following options: 1. Create Cruise – This option allows a user to create a new cruise by entering all necessary details (Cruise ID, route, ticket price). If the maximum number of cruises has already been created, display an error message. 2. Search Cruise – This option allows to search a cruise by the user provided cruise ID. 3. Remove Cruise – This op…
I need to know about the use and configuration of files and folders, and their attributes in Windows Server 2019.
Southern Airline has 15 daily flights from Miami to New York.  Each flight requires two pilots.  Flights that do not have two pilots are canceled (passengers are transferred to other airlines).  The average profit per flight is $6000.  Because pilots get sick from time to time, the airline is considering a policy of keeping four *reserve pilots on standby to replace sick pilots.  Such pilots would introduce an additional cost of $1800 per reserve pilot (whether they fly or not). The pilots on each flight are distinct and the likelihood of any pilot getting sick is independent of the likelihood of any other pilot getting sick.  Southern believes that the probability of any given pilot getting sick is 0.15.  A) Run a simulation of this situation with at least 1000 iterations and report the following for the present policy (no reserve pilots) and the proposed policy (four reserve pilots): The average daily utilization of the aircraft (percentage of total flights that fly) The…
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
Computer Science
ISBN:9781337569798
Author:ECKERT
Publisher:CENGAGE L
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr