C代写:CMPSC355 Simple Web Server

用C语言实现一个简易的Web Server,能解析HTTP协议。

Assignment Information

This program shall be written in C, compiled with the following options: -Wall -Wextra-std=c99, and display no errors and no warnings. Do not submit executable files (only C source files).

In this assignment, you are to implement a simple Web server using the attached framework. The framework consists of 4 files:

  • simple-server.c - The main function of the server and the functions to start and stop the server and to wait for client requests. You must read this file and understand it. Do not make any changes to this file.
  • simple-server.h - Function prototypes. Do not make any changes to this file.
  • my-server.c - A sample implementation of the server. You must read this file and understand it. You must also re-implementit, as described below.
  • makefile - The makele for the project. You are advised to use it to build the project.

After downloading and unpacking the framework, run make. If the project builds successfully, you will get an executable file testserver. Run the program, then open a web browser and connect to http://localhost:8080 (replace 8080 with another port number if you pass it as the command line argument to testserver). You shall see a valid Web page.

Your Web server shall:

  • Start a new detached thread for each incoming request (the functiondo_server_stuff() shall, therefore, immediately return).
  • Read an HTTP request sent by the client from the client socket.
  • If the request is not in the form GET /some_file_name.html HTTP/…, respond with 405 Method Not Allowed. (See sample response in my-server.c).
  • If the request is in the form GET /some_file_name.html HTTP/…, and some_file_name exists in the directory /tmp of your computer, respond with 200 OK, followed by the content of the file /tmp/some_file_name. (You may want to provide support for some image files, too, for extra credit, but make sure to change the Content-Type header accordingly!)
  • If the request is in the form GET /some_file_name HTTP/…, and some_file_name does not exist in the directory /tmp of your computer or is not an HTML file (unless you opt to support image files), respond with 404 Not Found.
  • If the request is in the form GET /STOP HTTP/…, terminate the server by setting the global variable done to 1.
  • Maintain the total count of serviced requests and the count of currently running threads in the global variables total and current (not provided). Since the variables are shared among all threads, any thread attempting to change any of the variables shall first obtain a mutex.
  • If the request is in the form GET/ STATS HTTP/…, respond with 200 OK, followed by the values of the variables total and current in the form of a human-readable HTML document.

Test the functionality of your server by loading any HTML documents with a plenty of images and local links into your /tmp and browsing them. make sure to test the requests STATS and STOP, too, by pointing your browser at http://localhost:8080/STATS and http://localhost:8080/STOP, respectively. server.zip