Concept explainers
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...
Want to see the full answer?
Check out a sample textbook solutionChapter 12 Solutions
COMPUTER SYSTEMS&MOD MSGT/ET SA AC PKG
- Write a FancyCar class to support basic operations such as drive, add gas, honk horn, and start engine. FancyCar.java is provided with method stubs. Follow each step to gradually complete all methods. Note: This program is designed for incremental development. Complete each step and submit for grading before starting the next step. Only a portion of tests pass after each step but confirm progress. The main() method includes basic method calls. Add statements in main() as methods are completed to support development mode testing. Step 0. Declare private fields for miles driven as shown on the odometer (int), gallons of gas in tank (double), miles per gallon or MPG (double), driving capacity (double), and car model (String). Note the provided final variable indicates the gas tank capacity of 14.0 gallons. Step 1 (2 pts). 1) Complete the default constructor by initializing the odometer to five miles, tank is full of gas, miles per gallon is 24.0, and the model is "Old Clunker". 2)…arrow_forwardFind the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + day_of_week[i] + ': ')arrow_forwardFind the error: daily_sales = [0.0, 0,0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': ')arrow_forward
- Find the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(6): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': '))arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it and normalize it? Give two references with your answer.arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Consider that the database offline is not allowed since people are connected to it and personal data might be bridged and not secured. Provide three refernces with you answer.arrow_forward
- Should software manufacturers should be tolerant of the practice of software piracy in third-world countries to allow these countries an opportunity to move more quickly into the information age? Why or why not?arrow_forwardI would like to know about the features of Advanced Threat Protection (ATP), AMD-V, and domain name space (DNS).arrow_forwardPlease show the code for the Tikz figurearrow_forward
- Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningNp Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:CengageProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning
- New Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage LearningLINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.Computer ScienceISBN:9781337569798Author:ECKERTPublisher:CENGAGE LC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr