Skip to content

Commit

Permalink
Add all headers from a req or bereq object (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reza Naghibi committed Feb 29, 2016
1 parent 86402b7 commit c52d777
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
20 changes: 20 additions & 0 deletions man/vmod_curl.rst
Expand Up @@ -252,6 +252,26 @@ Example
// alternative using long string syntax
curl.header_add({"X-curl-Request: """});

header_add_all
--------------

Prototype
::

header_add_all()
Return value
VOID
Description
Adds all headers in the req object to the request.
If used from a backend thread, the bereq object is used.
Can be used in combination with header_add() and header_remove().
The rules in header_add() apply to header_add_all().
Example
::

// copy all headers from the req object in context
curl.header_add_all();

header_remove
-------------

Expand Down
43 changes: 43 additions & 0 deletions src/tests/test10.vtc
@@ -0,0 +1,43 @@
varnishtest "Test header_add_all"

server s1 {
rxreq
expect req.http.foo == "bar"
expect req.http.be == "0"
txresp
accept
rxreq
expect req.http.foobe == "barbe"
expect req.http.be == "2"
txresp
} -start

varnish v1 -vcl+backend {
import curl from "${vmod_topbuild}/src/.libs/libvmod_curl.so";
sub vcl_recv {
if (req.http.be == "0") {
curl.header_add_all();
curl.get("http://${s1_addr}:${s1_port}/");
return (synth(200));
}

set req.http.be = "2";
}

sub vcl_backend_fetch {
set bereq.http.foobe = "barbe";
curl.header_add_all();
curl.get("http://${s1_addr}:${s1_port}/");
return (abandon);
}
} -start

client c1 {
txreq -url "/" -hdr "be: 0" -hdr "foo: bar"
rxresp
} -run

client c2 {
txreq -url "/" -hdr "be: 1"
rxresp
} -run
22 changes: 22 additions & 0 deletions src/vmod_curl.c
Expand Up @@ -457,6 +457,28 @@ vmod_header_add(VRT_CTX, VCL_STRING value, struct vmod_priv *priv)
VTAILQ_INSERT_HEAD(&c->req_headers, rh, list);
}

VCL_VOID
vmod_header_add_all(VRT_CTX, struct vmod_priv *priv)
{
struct http *hp;
unsigned u;

CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);

if (VALID_OBJ(ctx->http_bereq, HTTP_MAGIC)) {
hp = ctx->http_bereq;
} else if(VALID_OBJ(ctx->http_req, HTTP_MAGIC)) {
hp = ctx->http_req;
} else {
return;
}

for (u = HTTP_HDR_FIRST; u < hp->nhd; u++) {
Tcheck(hp->hd[u]);
vmod_header_add(ctx, hp->hd[u].b, priv);
}
}

VCL_VOID
vmod_header_remove(VRT_CTX, VCL_STRING header, struct vmod_priv *priv)
{
Expand Down
3 changes: 3 additions & 0 deletions src/vmod_curl.vcc
Expand Up @@ -44,6 +44,9 @@ $Function STRING unescape(STRING)
$Function VOID header_add(STRING, PRIV_TASK)
$Function VOID header_remove(STRING, PRIV_TASK)

# Add all request headers from the req (or bereq) object
$Function VOID header_add_all(PRIV_TASK)

$Function VOID proxy(STRING, PRIV_TASK)

$Function VOID set_proxy(STRING, PRIV_TASK)
Expand Down

0 comments on commit c52d777

Please sign in to comment.