move metadata handling code

parent 0343fec3
......@@ -41,6 +41,7 @@
#include <vtim.h>
#include <vrnd.h>
#include "tus_b64.h"
#include "tus_file.h"
#include "tus_blob.h"
#include "tus_hdr.h"
......@@ -1033,3 +1034,61 @@ tus_file_sync(struct tus_file_core *fcore)
return;
AZ(fdatasync(fcore->fd));
}
static inline const char *
tus_meta_find(const char *meta, const char *key, size_t l,
const char **vp, size_t *vlp)
{
const char *v, *ve;
while (meta != NULL && *meta != '\0') {
if (strncmp(meta, key, l) == 0) {
v = meta + l;
if (*v == '\0' || *v == ',' ||
(v[0] == ' ' && v[1] == ','))
return (meta);
if (*v == ' ') {
v++;
if (vp != NULL)
*vp = v;
if (vlp != NULL) {
ve = strchr(v, ',');
if (ve != NULL)
*vlp = ve - v;
else
*vlp = strlen(v);
}
return (meta);
}
}
meta = strchr(meta, ',');
if (meta != NULL)
meta++;
}
return (NULL);
}
VCL_BOOL
tus_file_meta(VRT_CTX, const struct tus_file_disk *fdisk,
const char *k, VCL_BLOB *b)
{
const char *meta, *v = NULL;
size_t kl = strlen(k), vl = 0;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(fdisk, VMOD_TUS_FILE_DISK_MAGIC);
meta = fdisk->metadata;
if (b == NULL)
return (tus_meta_find(meta, k, kl, NULL, NULL) != NULL);
meta = tus_meta_find(meta, k, kl, &v, &vl);
if (meta == NULL)
return (0);
*b = tus_b64_decode(ctx, v, vl);
return (1);
}
......@@ -151,8 +151,7 @@ void
tus_file_rename_base(struct tus_file_core *fcore, const char *basename);
#endif
const char *
tus_file_final_urls(VRT_CTX, const struct tus_file_core *fcore,
const char *pfx);
tus_file_final_urls(VRT_CTX, const struct tus_file_core *, const char *);
struct concat_embryo {
unsigned magic;
......@@ -165,14 +164,15 @@ struct concat_embryo {
ssize_t upload_length;
};
struct concat_embryo * tus_file_final_concat(struct VPFX(tus_server) *srv,
struct concat_embryo *embryo, const char *spec);
struct concat_embryo * tus_file_final_concat(struct VPFX(tus_server) *,
struct concat_embryo *, const char *);
VCL_BLOB tus_concat_hash(VRT_CTX, const struct VPFX(tus_server) *,
const struct tus_concat *);
struct tus_file_core *
tus_file_final_birth(struct tus_file_core *fcore, struct concat_embryo *embryo);
void
tus_file_final_abort(struct concat_embryo *embryo);
VCL_BOOL
tus_file_done(struct tus_file_core *, struct tus_file_disk *, const char *);
struct tus_file_core *tus_file_final_birth(struct tus_file_core *,
struct concat_embryo *);
void tus_file_final_abort(struct concat_embryo *);
VCL_BOOL tus_file_done(struct tus_file_core *, struct tus_file_disk *,
const char *);
void tus_file_sync(struct tus_file_core *);
VCL_BOOL tus_file_meta(VRT_CTX, const struct tus_file_disk *, const char *,
VCL_BLOB *);
......@@ -45,6 +45,23 @@
#include "tus_stv.h"
#include "tus_hex.h"
/* ------------------------------------------------------------
* from https://github.com/varnishcache/varnish-cache/pull/3407
*/
#define vct_ishdrval(x) (((x) >= 0x20 && (x) != 0x7f) || (x) == 0x09)
static inline VCL_BOOL
validhdr(const char *p)
{
AN(p);
for(;*p != '\0'; p++)
if (! vct_ishdrval(*p))
return (0);
return (1);
}
/* ------------------------------------------------------------
*/
enum met_e {
......
......@@ -37,7 +37,6 @@
#include "vcc_tus_if.h"
#include "tus_checksums.h"
#include "tus_b64.h"
#include "tus_file.h"
#include "tus_servers.h"
#include "tus_hdr.h"
......@@ -235,49 +234,12 @@ tus_done(VRT_CTX, const struct VPFX(tus_server) *tussrv,
return (tus_file_done(fcore, fdisk, url));
}
static inline const char *
tus_meta_find(const char *meta, const char *key, size_t l,
const char **vp, size_t *vlp)
{
const char *v, *ve;
while (meta != NULL && *meta != '\0') {
if (strncmp(meta, key, l) == 0) {
v = meta + l;
if (*v == '\0' || *v == ',' ||
(v[0] == ' ' && v[1] == ','))
return (meta);
if (*v == ' ') {
v++;
if (vp != NULL)
*vp = v;
if (vlp != NULL) {
ve = strchr(v, ',');
if (ve != NULL)
*vlp = ve - v;
else
*vlp = strlen(v);
}
return (meta);
}
}
meta = strchr(meta, ',');
if (meta != NULL)
meta++;
}
return (NULL);
}
VCL_BOOL
tus_meta(VRT_CTX, const struct tus_response *resp, const char *k, VCL_BLOB *b)
{
const struct tus_file_core *fcore;
const struct tus_file_disk *fdisk;
const char *meta, *v = NULL;
size_t kl = strlen(k), vl = 0;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(resp, VMOD_TUS_RESPONSE_MAGIC);
fcore = resp->fcore;
......@@ -285,18 +247,9 @@ tus_meta(VRT_CTX, const struct tus_response *resp, const char *k, VCL_BLOB *b)
return (0);
CHECK_OBJ_NOTNULL(fcore, VMOD_TUS_FILE_CORE_MAGIC);
fdisk = fcore->disk;
CHECK_OBJ_NOTNULL(fdisk, VMOD_TUS_FILE_DISK_MAGIC);
meta = fdisk->metadata;
if (b == NULL)
return (tus_meta_find(meta, k, kl, NULL, NULL) != NULL);
meta = tus_meta_find(meta, k, kl, &v, &vl);
if (meta == NULL)
if (fdisk == NULL)
return (0);
CHECK_OBJ_NOTNULL(fdisk, VMOD_TUS_FILE_DISK_MAGIC);
*b = tus_b64_decode(ctx, v, vl);
return (1);
return (tus_file_meta(ctx, fdisk, k, b));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment