如何使用 Apache httpd API 和 request_rec 获取完整的 HTTP 请求 URL?

How to get the full HTTP request URL using Apache httpd API and request_rec?

我正在用 C 语言编写一个 Apache 模块,我希望针对当前请求(通过 request_rec)在整个 HTTP URL 中应用正则表达式。我找不到包含此信息的 request_rec 的任何成员。我试过了

...仅举几例。

如何将整个 URL 作为单个 char * 获取?

我知道这个问题与下面链接的问题类似,但这是针对 C 语言的(他们使用的是 C++),他们的解决方案(我还是试过了)对我不起作用。

static int on_request(request_rec *r) {

  // Substraction for request uri (r->uri is broken when use mod_rewrite)
  char *request_uri = calloc(strlen(r->the_request), 1);
  long unsigned int i;
  for(i = strlen(r->method) + 1; i <= (strlen(r->the_request) - strlen(r->protocol) - 1); i++) {
      request_uri[i - strlen(r->method) - 1] = r->the_request[i];
  }

  // Use it

  free(request_uri);

  // ...

对于 http://example.com/abc/def.html?ghi=jklrequest_uri/abc/def.html?ghi=jkl

我无法在 httpd API 中找到此信息(正如 Cheatah 所建议的),因此我使用以下函数自行构建信息。

免责声明:当我针对 url sub.localhost/a/b/c?query=param 进行测试时,request_rec 下的端口值未被填充,所以我此时不尝试处理不同的端口这对我的用例并不重要。

const char *buildUrl(request_rec *r)
{
    char *url = apr_pcalloc(r->pool, urlMaxLength);

    // <scheme>://<host><:port(IFF!=80)><unparsed-uri>
    snprintf(url, urlMaxLength, "%s://%s%s%s",
        ap_http_scheme(r),
        r->hostname,
        "", // could not get port # from request_rec... let's just assume that won't be needed...
        r->unparsed_uri
    );

    return url;
}