Skip to content

dianav18/HTTP-Client

Repository files navigation

Overview

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.


JSON Parsing with Parson

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.c and parson.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.


Command Structure

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.

Common Execution Pattern

  1. Input Collection

    The program prompts the user for required information such as usernames, passwords, IDs, or titles.

  2. JSON Body Construction

    If the command requires a payload (e.g., for POST or PUT), a JSON body is generated using helper functions that wrap Parson's API.

  3. HTTP Request Formatting

    Requests are constructed using snprintf, including essential headers:

    • Host
    • Content-Type
    • Content-Length
    • Authentication headers (Cookie or Authorization) if needed
  4. 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.

  5. Response Parsing

    The HTTP status code is extracted using sscanf, and the body is parsed via Parson to handle JSON responses cleanly.

  6. Error Handling and Output

    Based on the status code, the application prints appropriate success or error messages. Known status codes like 200, 401, 403, or 404 are handled explicitly, while other responses fall back to generic error handling with message extraction.

Example Commands

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.


Project Structure

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages