title: Varnish C - Header setting using VRT_SetHdr method
body: <p>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 <a href="https://stackoverflow.com/a/27717417">https://stackoverflow.com/a/27717417</a>), I got some good insight and then wrote the following routine:</p>
  
  <pre><code>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, '?') ? "&amp;" : "?";
          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);
          }
      }
  </code></pre>
  
  <p>And then I am calling this from <b>sub vcl_error</b>. Here is a portion of that:</p>
  
  <pre><code>if (obj.status == somevalue) {
      C{set_location_with_param(sp, "something", "somethingelse");}C
      set obj.status = 302;
      return(deliver);
  }
  </code></pre>
  
  <p>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?</p>