Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
varnish-cache
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
varnishcache
varnish-cache
Commits
999e7bfe
Commit
999e7bfe
authored
Aug 17, 2020
by
Poul-Henning Kamp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cherry-pick std.blobread out of #3123
parent
20de7660
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
16 deletions
+59
-16
r01145.vtc
bin/varnishtest/tests/r01145.vtc
+4
-2
vmod_std.vcc
lib/libvmod_std/vmod_std.vcc
+14
-3
vmod_std_fileread.c
lib/libvmod_std/vmod_std_fileread.c
+41
-11
No files found.
bin/varnishtest/tests/r01145.vtc
View file @
999e7bfe
...
@@ -19,6 +19,7 @@ varnish v1 -vcl+backend {
...
@@ -19,6 +19,7 @@ varnish v1 -vcl+backend {
sub vcl_deliver {
sub vcl_deliver {
set resp.http.foo = std.fileread("${tmpdir}" + req.url);
set resp.http.foo = std.fileread("${tmpdir}" + req.url);
set resp.http.fooblob = std.blobread("${tmpdir}" + req.url);
}
}
} -start
} -start
...
@@ -27,17 +28,18 @@ client c1 {
...
@@ -27,17 +28,18 @@ client c1 {
txreq -url "/one"
txreq -url "/one"
rxresp
rxresp
expect resp.http.foo == "File One"
expect resp.http.foo == "File One"
expect resp.http.fooblob == :RmlsZSBPbmU=:
txreq -url "/two"
txreq -url "/two"
rxresp
rxresp
expect resp.http.foo == "File Two"
expect resp.http.foo == "File Two"
expect resp.http.fooblob == :RmlsZSBUd28=:
txreq -url "/three"
txreq -url "/three"
rxresp
rxresp
expect resp.http.foo == "File Three"
expect resp.http.foo == "File Three"
expect resp.http.fooblob == :RmlsZSBUaHJlZQ==:
} -run
} -run
client c1 -run
client c1 -run
client c1 -run
client c1 -run
lib/libvmod_std/vmod_std.vcc
View file @
999e7bfe
...
@@ -187,10 +187,13 @@ File(system) functions
...
@@ -187,10 +187,13 @@ File(system) functions
$Function STRING fileread(PRIV_CALL, STRING)
$Function STRING fileread(PRIV_CALL, STRING)
Reads a file and returns a string with the content. The result is
Reads a text file and returns a string with the content.
cached indefinitely per filename.
This function should not be used for reading binary files.
The entire file is cached on the first call, and subsequent calls
will return this cached contents, even if the file has changed in
the meantime.
For binary files, use std.blobread() instead.
Example::
Example::
...
@@ -204,6 +207,14 @@ case, you may need to modify the string, for example with
...
@@ -204,6 +207,14 @@ case, you may need to modify the string, for example with
set beresp.http.served-by = regsub(std.fileread("/etc/hostname"), "\R$", "");
set beresp.http.served-by = regsub(std.fileread("/etc/hostname"), "\R$", "");
$Function BLOB blobread(PRIV_CALL, STRING)
Reads any file and returns a blob with the content.
The entire file is cached on the first call, and subsequent calls
will return this cached contents, even if the file has changed in
the meantime.
$Function BOOL file_exists(STRING path)
$Function BOOL file_exists(STRING path)
Returns ``true`` if path or the file pointed to by path exists,
Returns ``true`` if path or the file pointed to by path exists,
...
...
lib/libvmod_std/vmod_std_fileread.c
View file @
999e7bfe
...
@@ -54,7 +54,8 @@ struct frfile {
...
@@ -54,7 +54,8 @@ struct frfile {
unsigned
magic
;
unsigned
magic
;
#define CACHED_FILE_MAGIC 0xa8e9d87a
#define CACHED_FILE_MAGIC 0xa8e9d87a
char
*
file_name
;
char
*
file_name
;
char
*
contents
;
void
*
contents
;
struct
vrt_blob
blob
[
1
];
int
refcount
;
int
refcount
;
VTAILQ_ENTRY
(
frfile
)
list
;
VTAILQ_ENTRY
(
frfile
)
list
;
};
};
...
@@ -82,14 +83,13 @@ free_frfile(void *ptr)
...
@@ -82,14 +83,13 @@ free_frfile(void *ptr)
}
}
}
}
VCL_STRING
v_matchproto_
(
td_std_fileread
)
static
struct
frfile
*
vmod_fileread
(
VRT_CTX
,
struct
vmod_priv
*
priv
,
find_frfile
(
struct
vmod_priv
*
priv
,
VCL_STRING
file_name
)
VCL_STRING
file_name
)
{
{
struct
frfile
*
frf
=
NULL
;
struct
frfile
*
frf
=
NULL
;
char
*
s
;
char
*
s
;
ssize_t
sz
;
CHECK_OBJ_NOTNULL
(
ctx
,
VRT_CTX_MAGIC
);
AN
(
priv
);
AN
(
priv
);
if
(
file_name
==
NULL
)
if
(
file_name
==
NULL
)
...
@@ -98,7 +98,7 @@ vmod_fileread(VRT_CTX, struct vmod_priv *priv,
...
@@ -98,7 +98,7 @@ vmod_fileread(VRT_CTX, struct vmod_priv *priv,
if
(
priv
->
priv
!=
NULL
)
{
if
(
priv
->
priv
!=
NULL
)
{
CAST_OBJ_NOTNULL
(
frf
,
priv
->
priv
,
CACHED_FILE_MAGIC
);
CAST_OBJ_NOTNULL
(
frf
,
priv
->
priv
,
CACHED_FILE_MAGIC
);
if
(
!
strcmp
(
file_name
,
frf
->
file_name
))
if
(
!
strcmp
(
file_name
,
frf
->
file_name
))
return
(
frf
->
contents
);
return
(
frf
);
}
}
AZ
(
pthread_mutex_lock
(
&
frmtx
));
AZ
(
pthread_mutex_lock
(
&
frmtx
));
...
@@ -114,22 +114,52 @@ vmod_fileread(VRT_CTX, struct vmod_priv *priv,
...
@@ -114,22 +114,52 @@ vmod_fileread(VRT_CTX, struct vmod_priv *priv,
if
(
frf
!=
NULL
)
{
if
(
frf
!=
NULL
)
{
priv
->
free
=
free_frfile
;
priv
->
free
=
free_frfile
;
priv
->
priv
=
frf
;
priv
->
priv
=
frf
;
return
(
frf
->
contents
);
return
(
frf
);
}
}
s
=
VFIL_readfile
(
NULL
,
file_name
,
NULL
);
s
=
VFIL_readfile
(
NULL
,
file_name
,
&
sz
);
if
(
s
!=
NULL
)
{
if
(
s
!=
NULL
)
{
assert
(
sz
>
0
);
ALLOC_OBJ
(
frf
,
CACHED_FILE_MAGIC
);
ALLOC_OBJ
(
frf
,
CACHED_FILE_MAGIC
);
AN
(
frf
);
AN
(
frf
);
frf
->
file_name
=
strdup
(
file_name
);
REPLACE
(
frf
->
file_name
,
file_name
);
AN
(
frf
->
file_name
);
frf
->
refcount
=
1
;
frf
->
refcount
=
1
;
frf
->
contents
=
s
;
frf
->
contents
=
s
;
frf
->
blob
->
blob
=
s
;
frf
->
blob
->
len
=
(
size_t
)
sz
;
priv
->
free
=
free_frfile
;
priv
->
free
=
free_frfile
;
priv
->
priv
=
frf
;
priv
->
priv
=
frf
;
AZ
(
pthread_mutex_lock
(
&
frmtx
));
AZ
(
pthread_mutex_lock
(
&
frmtx
));
VTAILQ_INSERT_HEAD
(
&
frlist
,
frf
,
list
);
VTAILQ_INSERT_HEAD
(
&
frlist
,
frf
,
list
);
AZ
(
pthread_mutex_unlock
(
&
frmtx
));
AZ
(
pthread_mutex_unlock
(
&
frmtx
));
}
}
return
(
s
);
return
(
frf
);
}
VCL_STRING
v_matchproto_
(
td_std_fileread
)
vmod_fileread
(
VRT_CTX
,
struct
vmod_priv
*
priv
,
VCL_STRING
file_name
)
{
struct
frfile
*
frf
;
CHECK_OBJ_NOTNULL
(
ctx
,
VRT_CTX_MAGIC
);
AN
(
priv
);
frf
=
find_frfile
(
priv
,
file_name
);
if
(
frf
==
NULL
)
return
(
NULL
);
return
(
frf
->
contents
);
}
VCL_BLOB
v_matchproto_
(
td_std_blobread
)
vmod_blobread
(
VRT_CTX
,
struct
vmod_priv
*
priv
,
VCL_STRING
file_name
)
{
struct
frfile
*
frf
;
CHECK_OBJ_NOTNULL
(
ctx
,
VRT_CTX_MAGIC
);
AN
(
priv
);
frf
=
find_frfile
(
priv
,
file_name
);
if
(
frf
==
NULL
)
return
(
NULL
);
return
(
frf
->
blob
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment