miconda/ruxc

RUst eXports to C

C/C++ library exporting useful functions written in Rust, such as simple HTTP/S

RUXC

``

client functions for doing GET or POST requests.

Build

Build With Cargo

The library can be build directly using cargo tool that comes with rust:

git clone https://github.com/miconda/ruxc
cd ruxc
cargo build --release

After build, the libruxc static (.a) and dynamic (.so or .dylib) library files are in the folder target/release/.

The C structures and functions are available in include/ruxc.h file.

Build With Make

For convenience, a Makefile is available to simplify the build and install tasks:

git clone https://github.com/miconda/ruxc
cd ruxc
make lib
make install

C API Functions

HTTP Client Functions

The library offers HTTP client functions to perform blocking GET or POST requests, with option to reuse connections per process. The functions are built on ureq Rust library (https://github.com/algesten/ureq/), which has also support for HTTPS based on rustls (https://github.com/ctz/rustls).

They are a simple alternative to the versatile cURL library, not depending on libssl or gnutls either. The functions are useful when willing to build a simple HTTP client in C, specially for multi-process applications when one wants to re-use the HTTP/S connection per process and do not care about multi-threading constraints of libssl or libcurl.

It supports setting custom headers for GET and POST requests as well as body data for POST requests. There is no user/password authentication support. An option can be set to accept only trusted TLS certificates for HTTPS connections or any certificate.

For usage example, see also examples/httpcli.c or the ruxc module of Kamailio project (https://github.com/kamailio/kamailio/tree/master/src/modules/ruxc).

Example Of Usage

Basic C HTTP GET client:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ruxc.h>

int main(int argc, char *argv[])
{
	RuxcHTTPRequest v_http_request = {0};
	RuxcHTTPResponse v_http_response = {0};

	v_http_request.timeout = 5000;
	v_http_request.timeout_connect = 5000;
	v_http_request.timeout_read = 5000;
	v_http_request.timeout_write = 5000;
	v_http_request.debug = 1;
	v_http_request.reuse = 1;

	v_http_request.url = "https://myserver.com/get";
	v_http_request.url_len = strlen(v_http_request.url);

	v_http_request.headers = "X-My-Key: xyzw\r\nX-My-Hdr: abcd\r\n";
	v_http_request.headers_len = strlen(v_http_request.headers);

	ruxc_http_get(&v_http_request, &v_http_response);

	if(v_http_response.retcode < 0) {
		printf("failed to perform http get - retcode: %d\n", v_http_response.retcode);
	} else {
		if(v_http_response.resdata != NULL &&  v_http_response.resdata_len>0) {
			printf("http response code: %d - data len: %d - data: [%.*s]\n",
					v_http_response.rescode, v_http_response.resdata_len,
					v_http_response.resdata_len, v_http_response.resdata);
		} else {
			printf("http response code: %d\n", v_http_response.rescode);
		}
	}
	ruxc_http_response_release(&v_http_response);

	return 0;
}

Running examples/httpcli on MacOS:

git clone https://github.com/miconda/ruxc
cd ruxc
cargo build --release
gcc -o examples/httpcli -I include/ examples/httpcli.c target/release/libruxc.a -framework Security
./examples/httpcli

License

MIT

Copyright: Daniel-Constantin Mierla <[email protected]>

Credits

This library is built using:

  • rust - https://www.rust-lang.org/
  • ureq - https://github.com/algesten/ureq/
  • rustls - https://github.com/ctz/rustls
Issues

Collection of the latest Issues

nakchak

nakchak

Comment Icon5

Carrying on discussion from https://github.com/kamailio/kamailio/issues/2820

Unfortunately my rust is non existent so i don't think i will be able to help much other than discuss ideas...

A retry mechanism would be really handy in case of transient network and server issues.

I have modified the ruxc module to have a basic retry mechanism as follows:

	ruxc_http_post(&v_http_request, &v_http_response);
	
	//Retry logic
	//If attempts < configured retries and return code is < 0 but > -20 [libruxc null url return code] or has a response code between 500-699
	int attempt;
	attempt = 0;
	while(attempt < _ruxc_http_retry && 
	  ((v_http_response.retcode < 0 && v_http_response.retcode > -20) || 
	  (v_http_response.rescode > 499 && v_http_response.rescode < 699)){
		attempt++;
		LM_DBG("Retrying Request Attempt: %d - Response Code: %d - Return Code: %d - URI: %s\n",
		  attempt, v_http_response.retcode, v_http_response.rescode, v_http_request.url);
		ruxc_http_post(&v_http_request, &v_http_response);
	}

Which seems to work nicely and protects against the issues openssl seems to be having on ubuntu 18.04

In order to make this logic as unopinionated as possible there should be an optout mechanism so you could specify that a request shouldnt retry if retries are enabled. I would propose that the least intrusive way of doing this would be to use a "X-" http header to control participation. The header name could be a configurable value with a sensible default of X-RUXC-NORETRY, the presence of the configured header name in a request would disable the retry logic, the value would be irrelevant.

There is also no reason why the configured retry attempts couldnt also be overriden with a http header included in the request.

i.e. if 3 retries are configured globally, then for a given unreliable destination you could override the number of attempts by including an X-RUXC-RETRYATTEMPTS in the specific request. This would allow simple setting of defaults through mod params if using kamailio, and per request overrides if you add the headers in kamailio config, or any other client to libruxc

Information - Updated Oct 03, 2021

Stars: 3
Forks: 1
Issues: 1

Repositories & Extras

Rust library for Self Organising Maps (SOM)

Add rusticsom as a dependency in Cargo

Rust library for Self Organising Maps (SOM)

Rust library for parsing configuration files

The 'option' can be any string with no whitespace

Rust library for parsing configuration files

Rust library for the Pimoroni Four Letter pHAT

This library aims to port ht16k33 (or rather a fork, as of right now) so credit goes to ht16k33-diet

Rust library for the Pimoroni Four Letter pHAT

Rust library for emulating 32-bit RISC-V

This library can execute instructions against any memory and register file that implements

Rust library for emulating 32-bit RISC-V

Rust library for connecting to the IPFS HTTP API using Hyper/Actix

You can use actix-web as a backend instead of hyper

Rust library for connecting to the IPFS HTTP API using Hyper/Actix

Rust library to manipulate file system access control lists (ACL) on macOS, Linux, and FreeBSD

This module provides two high level functions, getfacl and setfacl

Rust library to manipulate file system access control lists (ACL) on macOS, Linux, and FreeBSD

Rust library translation (rust-src/rust-std/stdlib/rustlib translation)

This is the place to translate Having a documentation in your native language is essential if you don't speak English, and still enjoyable even if...

Rust library translation (rust-src/rust-std/stdlib/rustlib translation)

Rust library for using Infrared hardware decoders (For example a Vishay TSOP* decoder),

enabling remote control support for embedded project

Rust library for using Infrared hardware decoders (For example a Vishay TSOP* decoder),

Rust library for interaction with the OriginTrail Decentralized Knowledge Graph

open up an issue on this repository and let us know

Rust library for interaction with the OriginTrail Decentralized Knowledge Graph

Rust library for parsing COLLADA files

Notice: This library is built around files exported from Blender 2

Rust library for parsing COLLADA files

Rust library for low-level abstraction of MIPS32 processors

This project is licensed under the terms of the MIT license

Rust library for low-level abstraction of MIPS32 processors
Facebook Instagram Twitter GitHub Dribbble
Privacy