https://github.com/thefcraft/c-http-server
Basic http server in c
https://github.com/thefcraft/c-http-server
c-http-server http-server-library websocket
Last synced: 4 days ago
JSON representation
Basic http server in c
- Host: GitHub
- URL: https://github.com/thefcraft/c-http-server
- Owner: thefcraft
- Created: 2024-04-05T07:31:24.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-27T14:43:11.000Z (5 months ago)
- Last Synced: 2025-03-12T20:40:59.608Z (4 months ago)
- Topics: c-http-server, http-server-library, websocket
- Language: C
- Homepage:
- Size: 11.1 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Basic HTTP Server in C
A lightweight and efficient HTTP server implementation in C that supports basic routing, static file serving, and both GET and POST requests. This server is designed for development and learning purposes.
## Features
- Simple and clean API inspired by modern web frameworks
- Support for both GET and POST requests
- Static file serving capability
- Route patterns with wildcard support (`/path/*`)
- Cross-platform compatibility (Windows and Linux)
- Built-in debugging mode
- Memory-efficient string and list implementations
- Support for serving various content types (HTML, JSON, images, etc.)
- Basic request and response handling## Prerequisites
### Windows
- GCC compiler (MinGW)
- ws2_32 library (Windows Sockets 2)### Linux
- GCC compiler
- Standard C libraries## Building the Library
### Windows
```bash
rm -f libserver.a && gcc -c server.c -o server.o -lws2_32 && gcc -shared -o server.dll server.o -lws2_32
```### Linux
```bash
gcc -c server.c && ar rcs libserver.a server.o
```
## Compilation and Running### Windows
```bash
gcc demo.c -L. -lserver && ./a.exe
```### Linux
```bash
gcc demo.c -L. -lserver && ./a.out
```## Usage Example
Here's a basic example showing how to create a simple web server:
```c
#include
#include
#include "server.h"#define HOST "0.0.0.0"
#define PORT 8080
#define DEBUG 1// Route handler for home page
void home(server *self, str *response, list *headers, str *content_buffer) {
send_file(response, "templates/demo.html");
}// Route handler for API endpoint
void api(server *self, str *response, list *headers, str *content_buffer) {
response->append(response, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n");
response->append(response, "{\"status\": \"success\"}");
}int main() {
server app = http_server();
// Define routes
app.route(&app, "GET", "/", home);
app.route(&app, "POST", "/api", api);
// Start the server
app.run(&app, HOST, PORT, DEBUG);
return 0;
}
```## API Reference
### Server Functions
- `http_server()`: Creates a new server instance
- `server.route(server*, method, path, callback)`: Registers a new route
- `server.run(server*, host, port, debug)`: Starts the server
- `server.stop(server*)`: Stops the server gracefully### Utility Functions
- `send_file(str *response, char *filename)`: Sends a file as response
- `send_file_with_header(str *response, char *filename, char *header)`: Sends a file with custom headers### String and List Operations
The server includes custom implementations for string and list operations:
- `String()`: Creates a new string buffer
- `List()`: Creates a new list for header storage## Route Patterns
The server supports two types of route patterns:
1. Exact matches: `/path`
2. Wildcard patterns: `/path/*`## Debug Mode
When debug mode is enabled (`DEBUG=1`), the server will output:
- Client connection details
- Request information
- Response status codes
- Error messages## Limitations
- Development server only - not recommended for production use
- Basic HTTP/1.1 implementation
- Limited security features
- No SSL/TLS support
- Single-threaded operation## Contributing
Feel free to submit issues, fork the repository, and create pull requests for any improvements.
## Acknowledgments
This server implementation is designed for educational purposes and as a learning tool for understanding HTTP servers and network programming in C.