Build a Simple HTTP/1.0 Web Server with CGI and Database Support in C
This guide walks through creating a basic HTTP/1.0 web server in C that handles GET/POST requests, serves static HTML, performs error handling, runs simple CGI scripts, and interacts with a MySQL database, complete with project structure and common troubleshooting tips.
Project Goals
The aim is to implement a minimal HTTP/1.0 web server that can:
Accept GET and POST requests from clients.
Return requested resources as HTML pages and handle errors such as missing files with a 404 page.
Execute simple CGI scripts and return their output.
Perform basic database operations (create, read, update, delete) through web pages.
Basic Framework of the HTTP Server
HTTP Protocol – An application‑layer protocol built on TCP that follows the sequence: connect → request → response → close.
URL – Uniform Resource Locator; includes protocol, host, optional port, and path. Two formats are used in the project: URLs with query parameters for GET and URLs without parameters for POST.
Request & Response Format – The response header contains a status code (e.g., 404 NotFound) indicating the result of the request.
Implementation Approach
1. Because HTTP runs over TCP, the first step is to establish TCP communication between client and server processes. 2. After receiving a request, the server parses the request method (GET or POST) to decide how to handle CGI. 3. The server extracts the URL; for GET the parameters are in the URL, for POST they are in the request body. 4. It checks whether the requested resource exists and determines if it is a directory, a regular file, or an executable program.
Non‑CGI mode – When the request is a GET without parameters, the server calls echo_www() to return the resource as an HTML page.
CGI mode – The server forks a child process to run the CGI program, passes parameters via environment variables, and redirects the child’s I/O so that its output is sent back to the browser.
The responsibilities of the parent and child processes are summarized as follows:
Error Handling
The echo_www() function is reused, but the response header is modified to send the appropriate status code and description. For example, when a resource is missing, a custom 404 page is returned.
Project Files
Directory layout:
cgi – CGI implementation code.
conf – Configuration files (IP and port).
log – Shell and HTTP error logs.
lib – MySQL libraries.
sql_client – MySQL API and CGI code.
wwwroot – Web root containing static pages, 404 page, and executable CGI programs.
Key source files: configure.sh – Shell script that generates the Makefile. http_ctl.sh – Script to start, stop, and restart the server. httpd.pid – Stores the server PID for daemon management. httpd.h – Declarations. httpd.c – Implementations. main.c – Main server logic.
Implementation Results
When the requested resource exists, the server returns the page correctly:
After submitting data to a CGI script, the processed result is displayed:
Source Code
Repository: https://github.com/lybb/Linux/tree/master/httpd
Common Issues
Local loopback works, but external browsers cannot connect without bridge mode; remember to disable the firewall.
If the server sends raw code instead of HTML, check the length passed to send() in echo_www().
Images not displayed because the server only handled directories and executables; add handling for regular files.
Partial image display caused by an insufficient line buffer size.
Incorrect Content-Type header ("text" vs. "text/html") leads browsers to prompt a download.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
