Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Web HTTP server c program help small error This is what i am getting. I only want it to display the 404 File not found.

Web HTTP server c program help small error

This is what i am getting. I only want it to display the 404 File not found. I dont want all the text below that.

To compile: gcc file.c

./a.out (any port)

To get the 404 page

host/port/

Please also add a printf saying that its listening in the given port. But the may problem is shown in the image. I only want the 404 file not found and not I dont want any text below it.

image text in transcribed

#include #include // definitions of a number of data types used in socket.h and netinet/in.h #include // definitions of structures needed for sockets, e.g. sockaddr #include // constants and structures needed for internet domain addresses, e.g. sockaddr_in #include #include #include /* for the waitpid() system call */ #include /* signal name macros, and the kill() prototype */ #include

void error(char *msg) { perror(msg); exit(1); }

// Get filetype extension from filepath char * getExt(char * path) {

char * ext = strrchr(path, '.'); //get extension if (ext[1] == 'j' && ext[3] == 'g') ext[4] = '\0'; else if (ext[3] == 'e') ext[5] = '\0'; else if (ext[3] == 'f') ext[4] = '\0';

if (!ext) ext = "html";

return ext; }

// Open file, format header, send response void get_file(int newsockfd, char * path) { char * OK = "HTTP/1.1 200 OK " "Content-type: %s "; char * MISS = "HTTP/1.1 404 NOT FOUND " "Content-type: text/html " " " "

404 File not found

"; char response[2048]; int filefd = -1; char * header; int i = 0; char c;

memset(response, 0, 2048); //reset memory

filefd = open(++path, O_RDONLY ); //printf("Result of open: %d ", filefd);

//File not found if ( filefd

char * ext = getExt(path);

//printf("Extension: %s ", ext);

// Determine filetype and format appropriate response if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0){ // Do stuff for jpg header = "image/jpeg "; snprintf(response, 2048, OK, header); write(newsockfd, response, strlen(response)); } else if (strcmp(ext, ".gif") == 0 ) { // Do stuff for gif header = "image/gif "; snprintf(response, 2048, OK, header); write(newsockfd, response, strlen(response)); } else { // Do stuff for text/html header = "text/html "; snprintf(response, 2048, OK, header); write(newsockfd, response, strlen(response)); } while( (i = read(filefd, &c, 1))) { if (i

//printf("Extension: %s ", ext);

// Parse client's request, check for proper protocol and method, call get_file() void request_handler(int newsockfd) { int n = -1; char buffer[2048];

memset(buffer, 0, 2048); //reset memory

//read client's message n = read(newsockfd,buffer,2048); if (n

// Ensure accepted protocol if (strcmp(protocol, "HTTP/1.1") || strcmp(protocol, "HTTP/1.0") ){ if (strcmp(method, "GET") == 0){ get_file(newsockfd, path); } } else { printf("protocol: %s, method: %s", protocol, method); error("ERROR, protocol not supported!"); }

}

int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, pid; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr;

if (argc

sockfd = socket(AF_INET, SOCK_STREAM, 0); //create socket

if (sockfd

memset((char *) &serv_addr, 0, sizeof(serv_addr)); //reset memory

//fill in address info portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))

if (pid

request_handler(newsockfd);

if (close(newsockfd)

if (close(newsockfd)

waitpid(-1, NULL, 0); } exit(0); } return 0; }

404 File not found ERROR. file can't be retrieved. pg peg mage peg if mage giftext t ERROR. file can't be readERROR. file can't be sen ERROR reading from socket%s %s %sHTTP 1.1 HTTP 1.0GETprotocol: %s, method %sERROR. protocol not supported!ERROR, no port provided ERROR opening socketERROR on bindingERROR on acceptERROR on forkERROR connection closeERROR connection close parent;IP

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions

Question

Define wiki and wiki log.

Answered: 1 week ago

Question

Explain AAAs diamond rating system.

Answered: 1 week ago

Question

=+5. Why might validity be a concern during an interview process?

Answered: 1 week ago