A C-based HTTP client application that interacts with a RESTful API to manage a movie library system with user authentication, admin privileges, and collection management.
This project implements a command-line client for a movie library management system. It supports multiple user roles (regular users and admins), JWT-based authentication, and comprehensive CRUD operations for movies and collections.
- User Login/Logout: Standard user authentication with session management
- Admin Login/Logout: Administrative access with elevated privileges
- User Management: Admin capabilities to add, view, and delete users
- Get Access: Obtain JWT token for authenticated API access
- View Movies: List all available movies or get detailed information about specific movies
- Add Movie: Create new movie entries with title, year, description, and rating
- Update Movie: Modify existing movie information
- Delete Movie: Remove movies from the library
- View Collections: List all collections or get detailed information about specific collections
- Create Collection: Build custom movie collections
- Add Movies to Collection: Populate collections with selected movies
- Remove Movies from Collection: Remove specific movies from collections
- Delete Collection: Remove entire collections
The application is built using standard C libraries and implements HTTP client functionality from scratch:
- Socket-based communication using
sys/socket.h - JSON parsing and serialization with Parson library
- Custom HTTP request builders (GET, POST, PUT, DELETE)
- Session management with cookies and JWT tokens
cookie_find(): Locates cookie data in server responsescookie_extract(): Extracts and stores session cookieserror_message(): Parses error messages from JSON responsesresponse_find(): Extracts HTTP response codes
All operations follow a consistent pattern:
- Read user input and validate data
- Construct JSON payload (when needed)
- Build HTTP request with appropriate headers
- Send request to server
- Parse response and extract status code
- Display success or error message
- Clean up allocated memory
- Server:
63.32.125.183:8081 - Base Path:
/api/v1/tema/ - Content Type:
application/json - Authentication: Cookie-based sessions and JWT tokens
- Integrated Parson library for JSON handling
- Implemented commands incrementally following the specification
- Created custom HTTP request functions (PUT, DELETE) based on existing GET/POST models
- Iteratively tested and refined output formatting
Created a single login() function handling both regular and admin authentication:
- Parameter
login_admin = 0: Regular user login (includesadmin_usernamefield) - Parameter
login_admin = 1: Admin login (username and password only)
Functions like add_movie_to_collection() return status codes:
- Return
1: Operation successful - Return
0: Operation failed
This enables complex operations like add_collection() to validate each step and rollback if needed.
- Username validation: No whitespace allowed
- ID validation: Numeric values only
- Rating validation: Maximum value of 9.9
The application implements comprehensive error handling:
- Input validation before API calls
- HTTP status code checking
- JSON error message extraction
- Memory cleanup on both success and failure paths
Proper memory management throughout:
- Dynamic allocation for all user inputs
- Cleanup after each operation
- Cookie and JWT token management
- Response buffer handling
Initial implementation displayed full server responses. Refined to match exact specification format, including proper use of # prefixes and structured output.
The add_collection() function required careful design:
- Create empty collection first
- Add movies one by one
- On any failure, rollback by deleting the collection
- Only show success if all operations complete
Solution: Implemented return codes in add_movie_to_collection() to enable proper error propagation and rollback logic.
Instead of duplicating code, added optional parameters:
add_movie_to_collection(): Parameter indicates whether to read input or use provided IDsdelete_collection(): Parameter for collection ID to enable programmatic calls
- Standard C libraries (stdio, stdlib, string, etc.)
- POSIX socket libraries
- Parson library for JSON parsing
- Custom helpers, requests, and buffer modules
Compile and run the program, then enter commands interactively:
login
login_admin
add_user
get_users
delete_user
logout
logout_admin
get_access
get_movie
get_movies
add_movie
update_movie
delete_movie
get_collections
get_collection
add_collection
delete_collection
add_movie_to_collection
delete_movie_from_collection
exit├── client.c # Main implementation
├── requests.c/h # HTTP request builders
├── helpers.c/h # Utility functions
├── buffer.c/h # Buffer management
└── parson.c/h # JSON parser library