Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Biblioteca REST en C para Autorización de Transacciones

Cliente REST de alto rendimiento usando libcurl multi y Parson.

Requisitos

  • Linux x86_64 / Windows con compilador C99 y libcurl instalado
    • Parson: copia manual de parson.h y parson.c en src/
  • pthreads

Instalación en Debian/Ubuntu:

sudo apt-get install libcurl4-openssl-dev cmake build-essential

Estructura del proyecto

web-client-C/
├── CMakeLists.txt
├── src/
│   ├── rest_client.h
│   └── rest_client.c
└── examples/
    └── example.c

Compilación y ejemplo

mkdir build && cd build
cmake ..
cmake --build . --config Release
# Ejecutar ejemplo:
./examples/example

Compilar sin CMake en Linux (RHEL 8.1)

Instala dependencias:

sudo dnf install -y gcc gcc-c++ make libcurl-devel openssl-devel

Compilación directa:

gcc -std=c99 -Wall -Wextra -O3 -pthread \
    src/rest_client.c src/parson.c examples/example.c \
    -o example -lcurl -lcrypto

Usando librería estática:

# 1) Construir librería (asegúrate de tener src/parson.c y src/parson.h)
gcc -std=c99 -Wall -Wextra -O3 -pthread -c src/rest_client.c src/parson.c -o rest_client.o
ar rcs librest_client.a rest_client.o parson.o

# 2) Compilar ejemplos y enlazar
gcc examples/example.c -o example -L. -lrest_client -lcurl -lcrypto

Uso en Windows

En PowerShell con Visual Studio y vcpkg:

  1. Clonar y preparar vcpkg:
    git clone https://github.com/microsoft/vcpkg.git
    cd vcpkg
    .\bootstrap-vcpkg.bat
    .\vcpkg.exe install curl
  2. Crear y configurar build:
    mkdir build; cd build
    cmake .. -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/ruta/a/vcpkg/scripts/buildsystems/vcpkg.cmake
  3. Compilar y ejecutar:
    cmake --build . --config Release
    .\Release\examples\example.exe

Uso con MSYS2

Abre la shell MSYS2 MinGW 64-bit e instala dependencias:

pacman -S mingw-w64-x86_64-curl mingw-w64-x86_64-toolchain cmake make pkg-config

Configura pkg-config:

export PKG_CONFIG_PATH=/mingw64/lib/pkgconfig

Crea el directorio de compilación y compila:

mkdir build && cd build
# Generador Unix Makefiles en MSYS2
cmake .. -G "Unix Makefiles"
make

Ejecuta el ejemplo:

./examples/example.exe

API pública

struct rest_client *rest_create(const char *base_url, int max_handles);
void rest_client_set_timeouts(struct rest_client *c, long connect_timeout, long timeout);
rest_response *rest_request(struct rest_client *c,
                            const char *method,
                            const char *path,
                            const char *json_body,
                            const struct rest_header *headers,
                            size_t n_headers);
void rest_response_free(rest_response *r);
void rest_destroy(struct rest_client *c);

Ejemplo de uso

struct rest_client *c = rest_create("https://auth.example.com", 10);
rest_client_set_timeouts(c, 5, 10);
struct rest_header hdrs[] = {{"Content-Type","application/json"}, {"Authorization","Bearer TOKEN"}};
rest_response *r = rest_request(c, "POST", "/v1/txn", "{\"amount\":1000}", hdrs, 2);
printf("Status: %ld\nBody: %s\n", r->status, r->body);
rest_response_free(r);
rest_destroy(c);

Notas de diseño

  • Uso de curl_multi para gestionar múltiples conexiones concurrentes.
  • Reuso de sockets (keep-alive) via CURLMOPT_MAXCONNECTS.
  • API bloqueante pero no reentrante; protegido con mutex para thread-safety.
  • Zero-copy parcial: se expande el buffer solo cuando llegan datos.
  • Serialización JSON con Parson (biblioteca Parson, MIT license).

Benchmark (1000 peticiones concurrentes)

Probado en Intel i7-9700, Linux x86_64, glibc 2.31:

# Con max_handles=1000
time for i in {1..1000}; do ./examples/example & done; wait
# Resultado (real): 820 ms
# Latencia media: ~0.82 ms/petición

Ajuste de parámetros

  • max_handles: número de conexiones en paralelo.
  • connect_timeout y timeout: ajustables vía rest_client_set_timeouts.

¡Listo para producción! Si necesitas más features o mejoras, abre un issue.

About

Biblioteca REST en C enfocada en autorización de transacciones y concurrencia HTTP con libcurl.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages