Skip to content

Commit d0f0fe8

Browse files
authored
Simplify workspace copies (#9)
1 parent 2633ff9 commit d0f0fe8

File tree

1 file changed

+20
-44
lines changed

1 file changed

+20
-44
lines changed

src/vmod_querymodifier.c

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -225,52 +225,33 @@ vmod_modifyparams(VRT_CTX, VCL_STRING uri, VCL_STRING params_in,
225225
return NULL;
226226
}
227227

228-
// Check for existing query string
229-
char *query_str = strchr(uri, '?');
230-
if (query_str == NULL) {
231-
char *ws_uri = WS_Copy(ctx->ws, uri, strlen(uri) + 1);
232-
if (ws_uri == NULL) {
233-
VRT_fail(ctx,
234-
"WS_Copy: out of workspace when returning unmodified URI");
235-
return NULL;
236-
}
237-
return ws_uri;
228+
char *uri_buf = WS_Copy(ctx->ws, uri, strlen(uri) + 1);
229+
if (!uri_buf) {
230+
VRT_fail(ctx, "WS_Copy: uri_buf: out of workspace");
231+
return NULL;
238232
}
239233

240-
size_t base_uri_len = query_str - uri;
241-
char base_uri[base_uri_len + 1];
242-
memcpy(base_uri, uri, base_uri_len);
243-
base_uri[base_uri_len] = '\0';
234+
char *query_str = strchr(uri_buf, '?');
235+
// No query string present so return the base URI
236+
if (query_str == NULL) {
237+
return uri_buf;
238+
}
244239

245240
// Move past the '?'
246-
query_str = query_str + 1;
241+
*query_str = '\0';
242+
query_str++;
247243

248-
// If no query params, return base_uri
244+
// If no query params present, return just the base URI
249245
if (*query_str == '\0') {
250-
char *ws_uri = WS_Copy(ctx->ws, base_uri, base_uri_len + 1);
251-
if (ws_uri == NULL) {
252-
VRT_fail(ctx, "WS_Copy: out of workspace");
253-
return NULL;
254-
}
255-
return ws_uri;
246+
return uri_buf;
256247
}
257248

249+
// If params_in is NULL or empty, remove all query params and just return
250+
// base URI
258251
if (params_in == NULL || *params_in == '\0') {
259-
char *ws_uri = WS_Copy(ctx->ws, base_uri, base_uri_len + 1);
260-
if (!ws_uri) {
261-
VRT_fail(ctx, "WS_Copy: out of workspace");
262-
return NULL;
263-
}
264-
return ws_uri;
252+
return uri_buf;
265253
}
266254

267-
char *query_str_copy = WS_Copy(ctx->ws, query_str, strlen(query_str) + 1);
268-
if (!query_str_copy) {
269-
VRT_fail(ctx, "WS_Copy: query_str_copy: out of workspace");
270-
return NULL;
271-
}
272-
273-
// Parse filter parameters
274255
char *filter_params[MAX_FILTER_PARAMS];
275256
size_t num_filter_params = 0;
276257
if (parse_filter_params(ctx, params_in, filter_params, &num_filter_params) <
@@ -279,22 +260,17 @@ vmod_modifyparams(VRT_CTX, VCL_STRING uri, VCL_STRING params_in,
279260
}
280261

281262
query_param_t *head;
282-
int no_param = tokenize_querystring(ctx, &head, query_str_copy);
263+
int no_param = tokenize_querystring(ctx, &head, query_str);
283264
if (no_param < 0) {
284-
VRT_fail(ctx, "tokenize_querystring failed");
285265
return NULL;
286266
}
287267

268+
// No parameters after tokenization means just return the base URI
288269
if (no_param == 0) {
289-
char *ws_uri = WS_Copy(ctx->ws, base_uri, base_uri_len + 1);
290-
if (!ws_uri) {
291-
VRT_fail(ctx, "WS_Copy: out of workspace");
292-
return NULL;
293-
}
294-
return ws_uri;
270+
return uri_buf;
295271
}
296272

297-
return rebuild_query_string(ctx, base_uri, head, (size_t)no_param,
273+
return rebuild_query_string(ctx, uri_buf, head, (size_t)no_param,
298274
filter_params, num_filter_params,
299275
exclude_params);
300276
}

0 commit comments

Comments
 (0)