From 8f742ed23646e1e49ecb66ed89dba469d455d143 Mon Sep 17 00:00:00 2001 From: Pierre Dubouilh Date: Thu, 29 Aug 2024 13:18:26 +0200 Subject: [PATCH] nits + update readme --- Makefile | 2 +- README.md | 53 ++++++++++++++++++++++++++++++++++++++--------------- cureq.c | 5 +++-- src/lib.rs | 2 +- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index c6900f6..50e5524 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ build:: rm -rf cureq cureq.h cargo build cbindgen --lang c --output cureq.h - cc cureq.c -L./target/debug -lcureq -o cureq + cc -O3 -Wall cureq.c -L./target/debug -lcureq -o cureq run:: build ./cureq diff --git a/README.md b/README.md index 50aa22b..dc44a37 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,45 @@ # cureq -Small C wrapper for [ureq](https://crates.io/crates/ureq). +Small C wrapper for [ureq](https://crates.io/crates/ureq). Outputs a static lib that can be versy easily linked to a C project. Example usage: ```c -const char *header0[] = {"name0", "value0"}; -const char *const *headers[] = {header0}; -int headers_cnt = 1; - -char buffer[5000]; -int read_bytes = 0; -int status = cureq_call("GET", url, headers_cnt, 1, buffer, sizeof(buffer), &read_bytes, NULL, 0); -if (status == -1) { - fprintf(stderr, "Error fetching content!\n"); -} +#include +#include +#include +#include "./cureq.h" + +int main() { + unsigned char buffer[5000]; + char *url = "https://httpbin.org/anything"; + char *method = "POST"; + char *payload = "some payload"; + int payload_len = strlen(payload); + + const char *header0[] = {"name0", "value0"}; + const char *const *headers[] = {header0}; + int headers_count = 1; + + fprintf(stderr, "Making a %s request to %s\n", method, url); + fprintf(stderr, "====================\n"); -fprintf(stderr, "=> Status code: %d\n", status); -fprintf(stderr, "=> Response: %s\n", buffer); + int read_bytes = 0; + int status = cureq_call(method, url, headers, headers_count, buffer, sizeof(buffer), &read_bytes, (unsigned char *) payload, payload_len); + if (status == -1) { + fprintf(stderr, "Error fetching content!\n"); + return 1; + } + + fprintf(stderr, "=> Status code: %d\n", status); + fprintf(stderr, "=> Response: %s\n", buffer); + return 0; +} ``` Prototype: ```c /** - * Perform a HTTP request + * Perform a request to the given URL * * @fn ureq_get * @@ -41,11 +58,17 @@ Prototype: */ int cureq_call(const char *method, const char *url, - char ***headers, + const char *const *const *headers, int headers_count, unsigned char *ret_buffer, int max_ret_buffer, int *ret_buffer_read, unsigned char *payload, int payload_len); +``` + +Build: +```sh +# requires make, rust, cargo, cbindgen, and a C compiler +; make ``` \ No newline at end of file diff --git a/cureq.c b/cureq.c index edf825a..30dc353 100644 --- a/cureq.c +++ b/cureq.c @@ -4,7 +4,7 @@ #include "./cureq.h" int main() { - char buffer[5000]; + unsigned char buffer[5000]; char *url = "https://httpbin.org/anything"; char *method = "POST"; char *payload = "some payload"; @@ -13,13 +13,14 @@ int main() { const char *header0[] = {"name0", "value0"}; const char *header1[] = {"name1", "value1"}; const char *const *headers[] = {header0, header1}; + int headers_count = 2; fprintf(stderr, "Making a %s request to %s\n", method, url); fprintf(stderr, "Set headers: %s: %s, %s: %s\n", header0[0], header0[1], header1[0], header1[1]); fprintf(stderr, "====================\n"); int read_bytes = 0; - int status = cureq_call(method, url, headers, 2, buffer, sizeof(buffer), &read_bytes, payload, payload_len); + int status = cureq_call(method, url, headers, headers_count, buffer, sizeof(buffer), &read_bytes, (unsigned char *) payload, payload_len); if (status == -1) { fprintf(stderr, "Error fetching content!\n"); return 1; diff --git a/src/lib.rs b/src/lib.rs index 276459c..71d2577 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ use std::ffi::{c_int, c_uchar, c_char, CStr}; use std::io::Read; #[no_mangle] -/// Make a GET request to the given URL +/// Perform a request to the given URL /// /// @fn ureq_get ///