Cliente REST de alto rendimiento usando libcurl multi y Parson.
- Linux x86_64 / Windows con compilador C99 y libcurl instalado
- Parson: copia manual de
parson.hyparson.censrc/
- Parson: copia manual de
- pthreads
Instalación en Debian/Ubuntu:
sudo apt-get install libcurl4-openssl-dev cmake build-essentialweb-client-C/
├── CMakeLists.txt
├── src/
│ ├── rest_client.h
│ └── rest_client.c
└── examples/
└── example.c
mkdir build && cd build
cmake ..
cmake --build . --config Release
# Ejecutar ejemplo:
./examples/exampleInstala dependencias:
sudo dnf install -y gcc gcc-c++ make libcurl-devel openssl-develCompilación directa:
gcc -std=c99 -Wall -Wextra -O3 -pthread \
src/rest_client.c src/parson.c examples/example.c \
-o example -lcurl -lcryptoUsando 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 -lcryptoEn PowerShell con Visual Studio y vcpkg:
- Clonar y preparar vcpkg:
git clone https://github.com/microsoft/vcpkg.git cd vcpkg .\bootstrap-vcpkg.bat .\vcpkg.exe install curl
- Crear y configurar build:
mkdir build; cd build cmake .. -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/ruta/a/vcpkg/scripts/buildsystems/vcpkg.cmake
- Compilar y ejecutar:
cmake --build . --config Release .\Release\examples\example.exe
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-configConfigura pkg-config:
export PKG_CONFIG_PATH=/mingw64/lib/pkgconfigCrea el directorio de compilación y compila:
mkdir build && cd build
# Generador Unix Makefiles en MSYS2
cmake .. -G "Unix Makefiles"
makeEjecuta el ejemplo:
./examples/example.exestruct 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);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);- Uso de
curl_multipara 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).
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
max_handles: número de conexiones en paralelo.connect_timeoutytimeout: ajustables víarest_client_set_timeouts.
¡Listo para producción! Si necesitas más features o mejoras, abre un issue.