ngx_int_t ngx_x_rid_header_get_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) {
  u_char            *p         = NULL;
  ngx_table_elt_t   *header;
  ngx_str_t          hv;
  size_t             hlen      = 22;

  header = search_headers_in(r, ngx_x_rid_header_name.data, ngx_x_rid_header_name.len);

  if (header != NULL) {
      hv = header->value;

      if (hv.len >= 20 && hv.len <= 50) {
          // Reuse existing header
          hlen = hv.len;
          p = hv.data;
      }
  }

  if (p == NULL) {
      // Prepare 22 bytes to store the base58 string
      p = ngx_pnalloc(r->pool, 22);
      if (p == NULL) {
          return NGX_ERROR;
      }

#if (NGX_FREEBSD)
#error FreeBSD is not supported yet, sorry.
#elif (NGX_LINUX)
      uuid_t* uuid;

      // return of uuid_s_ok = 0
      if ( uuid_create(&uuid) ) {
        return -1;
      }
      if ( uuid_make(uuid, UUID_MAKE_V4) ) {
        uuid_destroy(uuid);
        return -1;
      }

      // at this point we have 16 bytes in "uuid", ready for conversion
      uuid_fmt22(uuid, p);
      uuid_destroy(uuid);
#elif (NGX_SOLARIS)
#error Solaris is not supported yet, sorry.
#elif (NGX_DARWIN)
      uuid_t uuid;
      uuid_generate(uuid);
      uuid_fmt22(uuid, p);
#endif
  }

  v->len = hlen;
  v->valid = 1;
  v->no_cacheable = 0;
  v->not_found = 0;
  v->data = p;

  return NGX_OK;
}
ngx_int_t ngx_x_rid_header_get_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) {
  u_char *p;

  // Prepare 22 bytes to store the base58 string
  p = ngx_pnalloc(r->pool, 22);
  if (p == NULL) {
      return NGX_ERROR;
  }

#if (NGX_FREEBSD)
#error FreeBSD is not supported yet, sorry.
#elif (NGX_LINUX)
  uuid_t* uuid;

  // return of uuid_s_ok = 0
  if ( uuid_create(&uuid) ) {
    return -1;
  }
  if ( uuid_make(uuid, UUID_MAKE_V4) ) {
    uuid_destroy(uuid);
    return -1;
  }

  // at this point we have 16 bytes in "uuid", ready for conversion
  uuid_fmt22(uuid, p);
  uuid_destroy(uuid);
#elif (NGX_SOLARIS)
#error Solaris is not supported yet, sorry.
#elif (NGX_DARWIN)
  uuid_t uuid;
  uuid_generate(uuid);
  uuid_fmt22(uuid, p);
#endif

  v->len = 22;
  v->valid = 1;
  v->no_cacheable = 0;
  v->not_found = 0;
  v->data = p;

  return NGX_OK;
}