1

For a specific use case, I needed to write a C routine inside vcl script. There I needed to set the Location header dynamically. After going through varnish tutorial and few stack overflow (I would love to mention this https://stackoverflow.com/a/27717417), I got some good insight and then wrote the following routine:

char* set_location_with_param(void *sp, const char *key, const char *value){
        const char *key_value_separator = "=";
        const char *req_url = VRT_r_req_url(sp);
        const char *req_host = VRT_GetHdr(sp, HDR_REQ, "\005Host:");
        const char *param_separator = strchr(req_url, '?') ? "&" : "?";
        char *query_string = malloc(strlen(param_separator) + strlen(key) + strlen(key_value_separator) + strlen(value) + 1);
        if(query_string != NULL){
            strcpy(query_string, param_separator);
            strcat(query_string, key);
            strcat(query_string, key_value_separator);
            strcat(query_string, value);
            VRT_SetHdr(sp, HDR_OBJ, "\011Location:", "https://", req_host, req_url, query_string, vrt_magic_string_end);
            free(query_string);
        }
    }

And then I am calling this from sub vcl_error. Here is a portion of that:

if (obj.status == somevalue) {
    C{set_location_with_param(sp, "something", "somethingelse");}C
    set obj.status = 302;
    return(deliver);
}

Need expert opinion on writing C function and setting header using VRT_SetHdr method. Is there any potential risk if I go with the above solution?

Sajol
  • 51
  • 4

0 Answers0