This project is a C-based HTTP client designed to communicate via TCP sockets with a RESTful API server. Its goal is to simulate a movie library management system, enabling users to manage authentication, users, movies, and collections through handcrafted HTTP requests.
To handle JSON serialization and parsing, the project uses the Parson library. The decision to use Parson was motivated by several factors:
-
Ease of Integration
Parson is a lightweight, single-source C library that requires minimal setup. It fits perfectly within projects that must compile without external dependency managers.
-
Simple and Clean API
Parson offers intuitive functions for reading JSON responses, accessing nested structures, and building JSON payloads for HTTP requests.
-
Checker Compatibility
Since the project is tested in a clean environment without additional libraries installed, Parson's self-contained nature (only
parson.candparson.h) ensures compatibility without extra configuration. -
Robustness in JSON Handling
Manual string parsing of JSON (using functions like
strstr,strtok, etc.) is fragile and error-prone. Parson provides proper type safety and structured access, significantly reducing potential bugs.
All client commands follow a consistent and modular execution flow, which promotes maintainability and makes the behavior of the program predictable and easy to test.
-
Input Collection
The program prompts the user for required information such as usernames, passwords, IDs, or titles.
-
JSON Body Construction
If the command requires a payload (e.g., for
POSTorPUT), a JSON body is generated using helper functions that wrap Parson's API. -
HTTP Request Formatting
Requests are constructed using
snprintf, including essential headers:HostContent-TypeContent-Length- Authentication headers (
CookieorAuthorization) if needed
-
Sending and Receiving Data
The program sends the request using a dedicated socket helper function and waits for the server's response, buffering it into a fixed-size response string.
-
Response Parsing
The HTTP status code is extracted using
sscanf, and the body is parsed via Parson to handle JSON responses cleanly. -
Error Handling and Output
Based on the status code, the application prints appropriate success or error messages. Known status codes like
200,401,403, or404are handled explicitly, while other responses fall back to generic error handling with message extraction.
All commands (such as login_admin, add_user, get_users, add_movie, get_collections, etc.) follow this pattern. The uniform structure helps isolate bugs and makes command addition straightforward.
The codebase is organized into modular files to separate concerns:
| File | Description |
|---|---|
client.c |
Entry point and command dispatcher |
handlers.c |
Implementation of all supported commands |
helper.c |
Networking and string utility functions |
parson.c |
JSON parsing library (used for API communication) |
Makefile |
Build automation |
README.md |
Project explanation and documentation |