unsigned url_encode_setHdr(struct sess *sp, char* url,char *head,int *encoded_size){
	char *copy;
	char buf[3075];
	int size = 3 * strlen(url) + 3;
	if(size > 3075){
		///////////////////////////////////////////////
		//use ws
		int u = WS_Reserve(sp->wrk->ws, 0);
		if(u < size){
#ifdef DEBUG_SYSLOG
		syslog(6,"parse:url_encode_setHdr more ws size");
#endif

			return 0;
		}
		copy = (char*)sp->wrk->ws->f;
		///////////////////////////////////////////////
	}else{
		copy = buf;
	}
	__url_encode(url,encoded_size,copy);

	*encoded_size += strlen(copy) + head[0] +1;
	if(size > 3075){
		WS_Release(sp->wrk->ws,strlen(copy)+1);
	}
	//sethdr
    VRT_SetHdr(sp, HDR_REQ, head, copy, vrt_magic_string_end);
	return(1);
}
Exemple #2
0
const char *
vmod_decrypt(struct sess *sp, const char *text, const char *key)
{
	char *p;
	unsigned u, v;
	unsigned char decrypt[TAILLE];

	memset (decrypt, 0, TAILLE );

	u = WS_Reserve(sp->wrk->ws, 0); /* Reserve some work space */
	p = sp->wrk->ws->f;		/* Front of workspace area */
	
        decryption ((char*)key, (unsigned char*) decrypt, (unsigned char*) text);	

	v = snprintf(p, u, "%s", decrypt);
	v++;
	if (v > u) {
		/* No space, reset and leave */
		WS_Release(sp->wrk->ws, 0);
		return (NULL);
	}
	/* Update work space with what we've used */
	WS_Release(sp->wrk->ws, v);
	return (p);
}
Exemple #3
0
void
WRW_Reserve(struct worker *wrk, int *fd, struct vsl_log *vsl, double t0)
{
	struct wrw *wrw;
	unsigned u;

	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
	AZ(wrk->wrw);
	wrw = (void*)WS_Alloc(wrk->aws, sizeof *wrw);
	AN(wrw);
	memset(wrw, 0, sizeof *wrw);
	wrw->magic = WRW_MAGIC;
	u = WS_Reserve(wrk->aws, 0);
	u = PRNDDN(u);
	u /= sizeof(struct iovec);
	if (u > IOV_MAX)
		u = IOV_MAX;
	AN(u);
	wrw->iov = (void*)PRNDUP(wrk->aws->f);
	wrw->siov = u;
	wrw->ciov = u;
	wrw->werr = 0;
	wrw->liov = 0;
	wrw->niov = 0;
	wrw->wfd = fd;
	wrw->t0 = t0;
	wrw->vsl = vsl;
	wrk->wrw = wrw;
}
Exemple #4
0
char *
VRT_String(struct ws *ws, const char *h, const char *p, va_list ap)
{
	char *b, *e;
	unsigned u, x;

	u = WS_Reserve(ws, 0);
	e = b = ws->f;
	e += u;
	if (h != NULL) {
		x = strlen(h);
		if (b + x < e)
			memcpy(b, h, x);
		b += x;
		if (b < e)
			*b = ' ';
		b++;
	}
	b = VRT_StringList(b, e > b ? e - b : 0, p, ap);
	if (b == NULL || b == e) {
		WS_Release(ws, 0);
		return (NULL);
	}
	e = b;
	b = ws->f;
	WS_Release(ws, e - b);
	return (b);
}
static const char *
vmod_updown(const struct vrt_ctx *ctx, int up, const char *s, va_list ap)
{
	unsigned u;
	char *b, *e;
	const char *p;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	u = WS_Reserve(ctx->ws, 0);
	e = b = ctx->ws->f;
	e += u;
	p = s;
	while (p != vrt_magic_string_end && b < e) {
		if (p != NULL) {
			for (; b < e && *p != '\0'; p++)
				if (up)
					*b++ = (char)toupper(*p);
				else
					*b++ = (char)tolower(*p);
		}
		p = va_arg(ap, const char *);
	}
	if (b < e)
		*b = '\0';
	b++;
	if (b > e) {
		WS_Release(ctx->ws, 0);
		return (NULL);
	} else {
		e = b;
		b = ctx->ws->f;
		WS_Release(ctx->ws, e - b);
		return (b);
	}
}
Exemple #6
0
const char *
vmod_encrypt(struct sess *sp, const char *text, const char *key )
{
	char *p;
	unsigned u, v;

	unsigned char crypt64[TAILLE];
	unsigned char ciphertext_buffer[TAILLE];
	unsigned char *ciphertext_string = &ciphertext_buffer[0];
	int ciphertext_len = 0;

	memset (crypt64, 0, TAILLE );
	memset (ciphertext_buffer, 0, TAILLE );

	u = WS_Reserve(sp->wrk->ws, 0); /* Reserve some work space */
	p = sp->wrk->ws->f;		/* Front of workspace area */
	
	encryption ((char*)key,(unsigned char*) text, (unsigned char*) ciphertext_string, (unsigned char*) crypt64, &ciphertext_len);
	
	v = snprintf(p, u, "%s", crypt64);
	v++;
	if (v > u) {
		/* No space, reset and leave */
		WS_Release(sp->wrk->ws, 0);
		return (NULL);
	}
	/* Update work space with what we've used */
	WS_Release(sp->wrk->ws, v);
	return (p);
}
Exemple #7
0
VCL_STRING
vmod_uuid(const struct vrt_ctx *ctx, struct vmod_priv *pv){
   char *p;
   unsigned u, v;

   char *uuid_str = uuid_v1();

   u = WS_Reserve(ctx->ws, 0);     // Reserve some work space 
   if (sizeof(uuid_str) > u) {
      // No space, reset and leave 
      WS_Release(ctx->ws, 0);
      return(NULL);
   }

   p = ctx->ws->f;                 // Front of workspace area 

   strncpy(p, uuid_str, 37);
   // free up the uuid string once it's copied in place
   if(uuid_str){
      free(uuid_str);
   }

   // keep track of how much we actually used
   v+=37;

   // Update work space with what we've used 
   WS_Release(ctx->ws, v);
//   debug("uuid: %s", p);
   return(p);
}
static const char *
absolutize_link(struct ws* ws, const char* path, const char* link)
{
	unsigned available, written;
	char *front, *path_copy;

	if (link[0] == '/') {
		return link;
	}

	path_copy = (char*) WS_Copy(ws, path, -1);

	if (path_copy == NULL) {
		return NULL;
	}

	available = WS_Reserve(ws, 0);
	front = ws->f;

	written = snprintf(front, available, "%s/%s", dirname(path_copy), link);
	written++; // null-terminating char

	if (written > available) {
		WS_Release(ws, 0);
		return NULL;
	}

	WS_Release(ws, written);
	return front;
}
vmod_exec(const struct vrt_ctx *ctx, struct vmod_priv *priv, VCL_STRING sql)
{
	struct vmod_sqlite3_result r;
	struct vmod_sqlite3 *v;
	char *e, *p;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	AN(priv);

	if (!priv->priv) {
		e = (char *)sqlite3_errstr(SQLITE_ERROR);
		p = WS_Copy(ctx->ws, e, -1);
		return (p);
	}

	CAST_OBJ_NOTNULL(v, priv->priv, VMOD_SQLITE3_MAGIC);

	r.u = WS_Reserve(ctx->ws, 0);
	r.p = ctx->ws->f;
	r.d = v->d;
	r.l = 0;
	r.r = 0;

	*(r.p) = '\0';

	if (sqlite3_exec(v->db, sql, vmod_exec_cb, &r, &e) != SQLITE_OK) {
		WS_Release(ctx->ws, 0);
		p = WS_Copy(ctx->ws, e, -1);
		sqlite3_free(e);
		return (p);
	}

	WS_Release(ctx->ws, r.l + 1);
	return (r.p);
}
Exemple #10
0
int
Pool_Task_Arg(struct worker *wrk, task_func_t *func,
    const void *arg, size_t arg_len)
{
	struct pool *pp;
	struct worker *wrk2;
	int retval;

	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
	AN(arg);
	AN(arg_len);
	pp = wrk->pool;
	CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);

	Lck_Lock(&pp->mtx);
	wrk2 = pool_getidleworker(pp);
	if (wrk2 != NULL) {
		VTAILQ_REMOVE(&pp->idle_queue, &wrk2->task, list);
		retval = 1;
	} else {
		wrk2 = wrk;
		retval = 0;
	}
	Lck_Unlock(&pp->mtx);
	AZ(wrk2->task.func);

	assert(arg_len <= WS_Reserve(wrk2->aws, arg_len));
	memcpy(wrk2->aws->f, arg, arg_len);
	wrk2->task.func = func;
	wrk2->task.priv = wrk2->aws->f;
	if (retval)
		AZ(pthread_cond_signal(&wrk2->cond));
	return (retval);
}
Exemple #11
0
void
http_CollectHdr(struct http *hp, const char *hdr)
{
	unsigned u, v, ml, f = 0, x;
	char *b = NULL, *e = NULL;

	for (u = HTTP_HDR_FIRST; u < hp->nhd; u++) {
		while (u < hp->nhd && http_IsHdr(&hp->hd[u], hdr)) {
			Tcheck(hp->hd[u]);
			if (f == 0) {
				/* Found first header, just record the fact */
				f = u;
				break;
			}
			if (b == NULL) {
				/* Found second header, start our collection */
				ml = WS_Reserve(hp->ws, 0);
				b = hp->ws->f;
				e = b + ml;
				x = Tlen(hp->hd[f]);
				if (b + x < e) {
					memcpy(b, hp->hd[f].b, x);
					b += x;
				} else
					b = e;
			}

			AN(b);
			AN(e);

			/* Append the Nth header we found */
			if (b < e)
				*b++ = ',';
			x = Tlen(hp->hd[u]) - *hdr;
			if (b + x < e) {
				memcpy(b, hp->hd[u].b + *hdr, x);
				b += x;
			} else
				b = e;

			/* Shift remaining headers up one slot */
			for (v = u; v < hp->nhd - 1; v++)
				hp->hd[v] = hp->hd[v + 1];
			hp->nhd--;
		}

	}
	if (b == NULL)
		return;
	AN(e);
	if (b >= e) {
		WS_Release(hp->ws, 0);
		return;
	}
	*b = '\0';
	hp->hd[f].b = hp->ws->f;
	hp->hd[f].e = b;
	WS_ReleaseP(hp->ws, b + 1);
}
static const char*
apply_filter(struct filter_context *context)
{
	const char *params = context->params.filter.params;
	unsigned available = WS_Reserve(context->ws, 0);
	char *begin = context->ws->f;
	char *end = &begin[available];
	const char *cursor = context->query_string;

	append_string(&begin, end, context->uri, cursor - context->uri + 1);

	while (*cursor != '\0' && begin < end) {
		const char *param_position = ++cursor;
		const char *equal_position = NULL;

		while (*cursor != '\0' && *cursor != '&') {
			if (equal_position == NULL && *cursor == '=') {
				equal_position = cursor;
			}
			cursor++;
		}

		int param_name_length =
			(equal_position ? equal_position : cursor) - param_position;

		if ( ! context->is_filtered(param_position, param_name_length, context) ) {
			append_string(&begin, end, param_position, cursor - param_position);
			if (*cursor == '&') {
				*begin = '&';
				begin++;
			}
		}
	}

	if (begin < end) {
		begin -= (begin[-1] == '&');
		begin -= (begin[-1] == '?');
		*begin = '\0';
	}

	begin++;

	if (begin > end) {
		WS_Release(context->ws, 0);
		return context->uri;
	}

	end = begin;
	begin = context->ws->f;
	WS_Release(context->ws, end - begin);
	return begin;
}
Exemple #13
0
char *
VRT_IP_string(const struct vrt_ctx *ctx, VCL_IP ip)
{
	char *p;
	unsigned len;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	len = WS_Reserve(ctx->ws, 0);
	p = ctx->ws->f;
	VTCP_name(ip, p, len, NULL, 0);
	WS_Release(ctx->ws, strlen(p) + 1);
	return (p);
}
vmod_syslog(const struct vrt_ctx *ctx, VCL_INT fac, const char *fmt, ...)
{
	char *p;
	unsigned u;
	va_list ap;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	u = WS_Reserve(ctx->ws, 0);
	p = ctx->ws->f;
	va_start(ap, fmt);
	p = VRT_StringList(p, u, fmt, ap);
	va_end(ap);
	if (p != NULL)
		syslog((int)fac, "%s", p);
	WS_Release(ctx->ws, 0);
}
Exemple #15
0
vbf_printRequestBody(struct req *req, void *priv, void *ptr, size_t l)
{
	unsigned u;
	//reserve work-space
	u = WS_Reserve(req->ws, 0);
	if(u <= VMOD_DUMP_KEY_LEN) {
		//no space.
		WS_Release(req->ws, 0);
		return (0);
	}
	work_body(req,req->ws->f, u, ptr, l, req->http0);
	
	//free work-space
	WS_Release(req->ws, 0);
	return(0);
}
void
HTC_Init(struct http_conn *htc, struct ws *ws, int fd, SSL *ssl)
{

	htc->magic = HTTP_CONN_MAGIC;
	htc->ws = ws;
	htc->htc_fd = fd;
	htc->htc_ssl = ssl;
	/* XXX: ->s or ->f ? or param ? */
	(void)WS_Reserve(htc->ws, (htc->ws->e - htc->ws->s) / 2);
	htc->rxbuf.b = ws->f;
	htc->rxbuf.e = ws->f;
	*htc->rxbuf.e = '\0';
	htc->pipeline.b = NULL;
	htc->pipeline.e = NULL;
}
Exemple #17
0
void
h2h_decode_init(const struct h2_sess *h2, struct h2h_decode *d)
{

	CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
	CHECK_OBJ_NOTNULL(h2->new_req, REQ_MAGIC);
	CHECK_OBJ_NOTNULL(h2->new_req->http, HTTP_MAGIC);
	AN(d);
	INIT_OBJ(d, H2H_DECODE_MAGIC);
	VHD_Init(d->vhd);
	d->out_l = WS_Reserve(h2->new_req->http->ws, 0);
	assert(d->out_l > 0);	/* Can't do any work without any buffer
				   space. Require non-zero size. */
	d->out = h2->new_req->http->ws->f;
	d->reset = d->out;
}
Exemple #18
0
void
VRY_Prep(struct req *req)
{
	if (req->hash_objhead == NULL) {
		/* Not a waiting list return */
		AZ(req->vary_b);
		AZ(req->vary_l);
		AZ(req->vary_e);
		(void)WS_Reserve(req->ws, 0);
	} else {
		AN(req->ws->r);
	}
	req->vary_b = (void*)req->ws->f;
	req->vary_e = (void*)req->ws->r;
	if (req->vary_b + 2 < req->vary_e)
		req->vary_b[2] = '\0';
}
Exemple #19
0
static void
ses_reserve_attr(struct sess *sp, enum sess_attr a, void **dst, int sz)
{
	ssize_t o;

	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
	assert(a < SA_LAST);
	assert(sz >= 0);
	AN(dst);
	o = WS_Reserve(sp->ws, sz);
	assert(o >= sz);
	*dst = sp->ws->f;
	o = sp->ws->f - sp->ws->s;
	WS_Release(sp->ws, sz);
	assert(o >= 0 && o <= 0xffff);
	sp->sattr[a] = (uint16_t)o;
}
Exemple #20
0
void
SES_RxInit(struct http_conn *htc, struct ws *ws, unsigned maxbytes,
    unsigned maxhdr)
{

	htc->magic = HTTP_CONN_MAGIC;
	htc->ws = ws;
	htc->maxbytes = maxbytes;
	htc->maxhdr = maxhdr;

	(void)WS_Reserve(htc->ws, htc->maxbytes);
	htc->rxbuf_b = ws->f;
	htc->rxbuf_e = ws->f;
	*htc->rxbuf_e = '\0';
	htc->pipeline_b = NULL;
	htc->pipeline_e = NULL;
}
Exemple #21
0
static VCL_STRING
tlv_string(VRT_CTX, int tlv)
{
	char *dst, *d;
	int len;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);

	if (VPX_tlv(ctx->req, tlv, (void **)&dst, &len))
		return (NULL);
	if (!WS_Reserve(ctx->ws, len+1))
		return (NULL);
	d = ctx->ws->f;
	memcpy(d, dst, len);
	d[len] = '\0';
	WS_Release(ctx->ws, len+1);
	return (d);
}
void
HTC_Init(struct http_conn *htc, struct ws *ws, int fd, unsigned maxbytes,
    unsigned maxhdr)
{

	htc->magic = HTTP_CONN_MAGIC;
	htc->ws = ws;
	htc->fd = fd;
	htc->maxbytes = maxbytes;
	htc->maxhdr = maxhdr;

	(void)WS_Reserve(htc->ws, htc->maxbytes);
	htc->rxbuf.b = ws->f;
	htc->rxbuf.e = ws->f;
	*htc->rxbuf.e = '\0';
	htc->pipeline.b = NULL;
	htc->pipeline.e = NULL;
}
Exemple #23
0
vmod_normalize(VRT_CTX, VCL_STRING s)
{
	char *p;
	utf8proc_ssize_t len;
	unsigned u;
	int options;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	if (!s || !*s) {
		VSLb(ctx->vsl, SLT_Error, "vsf.normalize: No input");
		return (NULL);
	}
	len = strlen(s);
	assert(len > 0);

	u = WS_Reserve(ctx->ws, 0);
	if (u < len * sizeof(utf8proc_int32_t) + 1) {
		VSLb(ctx->vsl, SLT_Error, "vsf.normalize: Out of workspace");
		WS_Release(ctx->ws, 0);
		return (NULL);
	}
	p = ctx->ws->f;

	options = UTF8PROC_STABLE | UTF8PROC_COMPAT | UTF8PROC_COMPOSE |
	    UTF8PROC_IGNORE | UTF8PROC_NLF2LF | UTF8PROC_LUMP |
	    UTF8PROC_STRIPMARK;

	len = utf8proc_decompose((utf8proc_uint8_t *)s, len,
	    (utf8proc_int32_t *)p, len, options);
	if (len < 0) {
		VSLb(ctx->vsl, SLT_Error,
		    "vsf.normalize: utf8proc_decompose: %s",
		    utf8proc_errmsg(len));
		WS_Release(ctx->ws, 0);
		return (NULL);
	}
	assert(len * sizeof(utf8proc_int32_t) + 1 < u);

	len = utf8proc_reencode((utf8proc_int32_t *)p, len, options);
	assert(len > 0);

	WS_Release(ctx->ws, len + 1);
	return (p);
}
void
HTTP1_Init(struct http_conn *htc, struct ws *ws, int fd, struct vsl_log *vsl,
    unsigned maxbytes, unsigned maxhdr)
{

	htc->magic = HTTP_CONN_MAGIC;
	htc->ws = ws;
	htc->fd = fd;
	htc->vsl = vsl;
	htc->maxbytes = maxbytes;
	htc->maxhdr = maxhdr;

	(void)WS_Reserve(htc->ws, htc->maxbytes);
	htc->rxbuf_b = ws->f;
	htc->rxbuf_e = ws->f;
	*htc->rxbuf_e = '\0';
	htc->pipeline_b = NULL;
	htc->pipeline_e = NULL;
}
Exemple #25
0
vmod_syslog(VRT_CTX, VCL_INT fac, const char *fmt, ...)
{
	unsigned u;
	va_list ap;
	txt t;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	va_start(ap, fmt);
	if (ctx->ws != NULL) {
		u = WS_Reserve(ctx->ws, 0);
		t.b = ctx->ws->f;
		t.e = VRT_StringList(ctx->ws->f, u, fmt, ap);
		if (t.e != NULL)
			syslog((int)fac, "%s", t.b);
		WS_Release(ctx->ws, 0);
	} else
		vsyslog((int)fac, fmt, ap);
	va_end(ap);
}
Exemple #26
0
void
SES_RxReInit(struct http_conn *htc)
{
	ssize_t l;

	CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
	(void)WS_Reserve(htc->ws, htc->maxbytes);
	htc->rxbuf_b = htc->ws->f;
	htc->rxbuf_e = htc->ws->f;
	if (htc->pipeline_b != NULL) {
		l = htc->pipeline_e - htc->pipeline_b;
		assert(l > 0);
		memmove(htc->rxbuf_b, htc->pipeline_b, l);
		htc->rxbuf_e += l;
		htc->pipeline_b = NULL;
		htc->pipeline_e = NULL;
	}
	*htc->rxbuf_e = '\0';
}
static const char *
vmod_hmac_sha256(const struct vrt_ctx *ctx,
	const char *key,size_t lkey, const char *msg,size_t lmsg,bool raw)
{
	hashid hash = MHASH_SHA256;
	size_t blocksize = mhash_get_block_size(hash);

	char *p;
	char *ptmp;
	p    = WS_Alloc(ctx->ws, blocksize * 2 + 1);
	ptmp = p;

	
	unsigned char *mac;
	unsigned u;
	u = WS_Reserve(ctx->ws, 0);
	assert(u > blocksize);
	mac = (unsigned char*)ctx->ws->f;
	
	int i;
	MHASH td;

	assert(msg);
	assert(key);

	assert(mhash_get_hash_pblock(hash) > 0);

	td = mhash_hmac_init(hash, (void *) key, lkey,
		mhash_get_hash_pblock(hash));
	mhash(td, msg, lmsg);
	mhash_hmac_deinit(td,mac);
	if(raw){
		WS_Release(ctx->ws, blocksize);
		return (char *)mac;
	}
	WS_Release(ctx->ws, 0);
	
	for (i = 0; i<blocksize;i++) {
		sprintf(ptmp,"%.2x",mac[i]);
		ptmp+=2;
	}
	return p;
}
Exemple #28
0
char *
VRT_IP_string(VRT_CTX, VCL_IP ip)
{
	char *p;
	unsigned len;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	if (ip == NULL)
		return (NULL);
	len = WS_Reserve(ctx->ws, 0);
	if (len == 0) {
		WS_Release(ctx->ws, 0);
		return (NULL);
	}
	p = ctx->ws->f;
	VTCP_name(ip, p, len, NULL, 0);
	WS_Release(ctx->ws, strlen(p) + 1);
	return (p);
}
int
HTC_Reinit(struct http_conn *htc)
{
	unsigned l;

	CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
	(void)WS_Reserve(htc->ws, (htc->ws->e - htc->ws->s) / 2);
	htc->rxbuf.b = htc->ws->f;
	htc->rxbuf.e = htc->ws->f;
	if (htc->pipeline.b != NULL) {
		l = Tlen(htc->pipeline);
		memmove(htc->rxbuf.b, htc->pipeline.b, l);
		htc->rxbuf.e += l;
		htc->pipeline.b = NULL;
		htc->pipeline.e = NULL;
	}
	*htc->rxbuf.e = '\0';
	return (HTC_Complete(htc));
}
vmod_log(const struct vrt_ctx *ctx, const char *fmt, ...)
{
	unsigned u;
	va_list ap;
	txt t;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	u = WS_Reserve(ctx->ws, 0);
	t.b = ctx->ws->f;
	va_start(ap, fmt);
	t.e = VRT_StringList(t.b, u, fmt, ap);
	va_end(ap);
	if (t.e != NULL) {
		assert(t.e > t.b);
		t.e--;
		VSLbt(ctx->vsl, SLT_VCL_Log, t);
	}
	WS_Release(ctx->ws, 0);
}