Skip to content

Dynamate92/Http_client

Repository files navigation

HTTP Client for Online Library

  1. Project Description

This project implements an HTTP client in C that connects to a REST server to manage users, movies, and collections in an online library system. The client receives text-based commands from the user, constructs HTTP/1.1 requests, sends them through UNIX sockets, and displays the parsed JSON responses received from the server.

  1. Technologies and Libraries Used

The client is written in C and uses the Parson library for JSON serialization and parsing. Communication with the server is handled using the UNIX sockets API, and all messages follow the HTTP/1.1 protocol.

Why Parson? Parson is a lightweight, fast, and dependency-free JSON library compatible with pure C. It allows direct parsing of JSON strings into a navigable structure without external dependencies, making it ideal for this client’s requirements.

  1. Code Organization

client.c – Entry point of the program. Contains the main() function, which reads commands and calls the corresponding handlers.

client.h – Declares prototypes for command handler functions.

helper.c / helper.h – Define utility functions for opening/closing connections, sending and receiving messages, and reading console input (read_line_trim).

requests.c / requests.h – Implement compute_get_request, compute_post_request, compute_put_request, and compute_delete_request, which manually construct HTTP requests.

  1. Functionalities

Each available command performs a clear and distinct operation:

Authentication and Session

login_admin – Authenticates the admin user and stores the session cookie.

logout_admin – Logs out the admin and deletes the stored cookie.

add_user – Adds a normal user (requires admin privileges).

get_users – Lists all existing normal users (admin only).

delete_user – Deletes a user by name (admin only).

User Access and JWT Management

login – Authenticates a normal user under a specific admin.

get_access – Requests library access and retrieves a JWT token.

logout – Logs out the normal user and removes the JWT token.

Movie Management

get_movies – Lists all available movies.

get_movie – Retrieves details of a movie by ID (JWT required).

add_movie – Adds a new movie with the specified details.

update_movie – Updates the fields of an existing movie by ID.

delete_movie – Deletes a movie from the library by ID.

Collections

get_collections – Lists all user movie collections.

add_collection – Creates a new collection (title, number of movies, IDs).

get_collection – Returns full collection details (title, owner, movie list).

delete_collection – Deletes a collection by ID.

add_movie_to_collection – Adds a movie to an existing collection by IDs.

delete_movie_from_collection – Removes a movie from a collection by IDs.

  1. HTTP Request Workflow

To build an HTTP request:

Allocate a fixed-size buffer.

Concatenate the start line, headers, and a blank separator line.

For methods with a body (POST, PUT), append the payload after the Content-Length header.

After receiving the response, locate the CRLFCRLF separator and pass the JSON body to the Parson parser.

  1. Session and JWT Management

After admin authentication, the session cookie is extracted using extract_cookie and stored globally. For access to the movie library, the get_access command retrieves a JWT token, extracted using extract_token and stored globally as jwt_token. This token is then included in the Authorization: Bearer header for all protected operations.

  1. Input Handling

The function read_line_trim uses fgets() to read a line and remove the trailing newline character. Each command handler reads the required fields (e.g., id, title, year, rating) and validates input before constructing the HTTP request.

  1. Usage

To run the client:

make ./client

Then type the desired command followed by its parameters, for example:

login_admin username admin password secret

Use get_access to obtain a JWT token before performing operations on movies or collections.

  1. Makefile

The project includes a Makefile that compiles the entire program with:

make

It generates the client executable and automatically manages dependencies between .c and .h files, ensuring that only modified code is recompiled.

  1. Conclusion

This project demonstrates how to implement an HTTP client in pure C from scratch — including manual HTTP message construction, JSON parsing using Parson, and secure session management with cookies and JWT tokens — all within a command-line interface.

About

This project implements an HTTP client in C that connects to a REST server to manage users, movies, and collections within an online library system. The client receives text-based commands from the user, constructs HTTP/1.1 requests, sends them through sockets, and displays the parsed responses extracted from JSON format.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors