Skip to content

Commit

Permalink
nits + update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pldubouilh committed Aug 29, 2024
1 parent ad0d08c commit 8f742ed
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 38 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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
*
Expand All @@ -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
```
5 changes: 3 additions & 2 deletions cureq.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down

0 comments on commit 8f742ed

Please sign in to comment.