Skip to content

Commit

Permalink
feat: add custom_headers (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm authored Jul 1, 2023
1 parent bd4dbf5 commit 5a04ea3
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/_instruction_download.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import vibe.curl

fn (req Request) download_file_(url string, file_path string) !Response {
h := curl.easy_init() or { return http_error(.easy_init, none) }
header := set_header(req.headers, h)
header := set_header(h, common: req.headers, custom: req.custom_headers)
defer {
curl.easy_cleanup(h)
curl.slist_free_all(header)
Expand Down Expand Up @@ -35,7 +35,7 @@ fn (req Request) download_file_(url string, file_path string) !Response {

fn (req Request) download_file_with_progress_(url string, file_path string, mut dl Download) !Response {
h := curl.easy_init() or { return http_error(.easy_init, none) }
header := set_header(req.headers, h)
header := set_header(h, common: req.headers, custom: req.custom_headers)
defer {
curl.easy_cleanup(h)
curl.slist_free_all(header)
Expand Down
4 changes: 2 additions & 2 deletions src/_instructions_get.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import vibe.curl
fn (req Request) get_(url string) !Response {
// Curl handle
h := curl.easy_init() or { return http_error(.easy_init, none) }
header := set_header(req.headers, h)
header := set_header(h, common: req.headers, custom: req.custom_headers)
defer {
curl.easy_cleanup(h)
curl.slist_free_all(header)
Expand All @@ -31,7 +31,7 @@ fn (req Request) get_(url string) !Response {

fn (req Request) get_slice_(url string, start usize, max_size_ ?usize) !Response {
h := curl.easy_init() or { return http_error(.easy_init, none) }
header := set_header(req.headers, h)
header := set_header(h, common: req.headers, custom: req.custom_headers)
defer {
curl.easy_cleanup(h)
curl.slist_free_all(header)
Expand Down
2 changes: 1 addition & 1 deletion src/_instructions_head.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import vibe.curl

fn (req Request) head_(url string) !Response {
h := curl.easy_init() or { return http_error(.easy_init, none) }
header := set_header(req.headers, h)
header := set_header(h, common: req.headers, custom: req.custom_headers)
defer {
curl.easy_cleanup(h)
curl.slist_free_all(header)
Expand Down
17 changes: 8 additions & 9 deletions src/_instructions_header.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ module vibe

import vibe.curl

fn set_header(header_map map[HttpHeader]string, handle &C.CURL) &HeaderList {
fn set_header(handle &C.CURL, headers HttpHeaders) &HeaderList {
mut list := &HeaderList(unsafe { nil })

// Default header
if header_map.len == 0 {
// Set default header and return if no headers were specified
if headers.common.len == 0 && headers.custom.len == 0 {
curl.easy_setopt(handle, .useragent, '${manifest.name}/${manifest.version}')
return list
}

mut arr := []string{}
mut has_user_agent := false
for k, v in header_map {
for k, v in headers.common {
if k == .user_agent {
has_user_agent = true
}
arr << '${k.str()}: ${v}'
list = curl.slist_append(list, '${k.str()}: ${v}')
}

for h in arr {
list = curl.slist_append(list, h)
for k, v in headers.custom {
list = curl.slist_append(list, '${k}: ${v}')
}

curl.easy_setopt(handle, .httpheader, list)

// Set default user agent if none was specified
Expand Down
2 changes: 1 addition & 1 deletion src/_instructions_post.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import vibe.curl

fn (req Request) post_(url string, data string) !Response {
h := curl.easy_init() or { return http_error(.easy_init, none) }
header := set_header(req.headers, h)
header := set_header(h, common: req.headers, custom: req.custom_headers)
defer {
curl.easy_cleanup(h)
curl.slist_free_all(header)
Expand Down
2 changes: 1 addition & 1 deletion src/_state_common.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import time
pub struct Request {
pub mut:
headers map[HttpHeader]string
custom_headers map[string]string // TODO:
custom_headers map[string]string
cookie_jar string
cookie_file string
timeout time.Duration
Expand Down
6 changes: 6 additions & 0 deletions src/_state_header.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ type HeaderList = C.curl_slist

type Status = int

[params]
struct HttpHeaders {
common map[HttpHeader]string
custom map[string]string
}

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
[generated]
pub enum HttpHeader {
Expand Down
12 changes: 10 additions & 2 deletions src/_tests_post_test.v
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
module vibe

import time
import x.json2 as json

fn test_post() {
req := Request{
headers: {
.user_agent: 'YourCustomUserAgent/v0.0.1'
.content_type: 'application/json; charset=utf-8'
}
custom_headers: {
'My-Custom-Header': 'FooBar'
}
timeout: time.second * 10
}
mut resp := req.post('https://httpbin.org/post', '{"msg":"hello from vibe"}')!
Expand All @@ -17,6 +21,10 @@ fn test_post() {
}

assert resp.status == 200
assert resp.body.contains('"User-Agent": "YourCustomUserAgent/v0.0.1"')
assert resp.body.contains('"msg": "hello from vibe"')
raw_json_resp := json.raw_decode(resp.body)!.as_map()
headers := raw_json_resp['headers']!.as_map()
assert headers['My-Custom-Header']!.str() == 'FooBar'
assert headers['User-Agent']!.str() == 'YourCustomUserAgent/v0.0.1'
json_data := raw_json_resp['json']!.as_map()
assert json_data['msg']!.str() == 'hello from vibe'
}

0 comments on commit 5a04ea3

Please sign in to comment.