Coverage Report

Created: 2026-07-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/lib/cf-haproxy.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifndef CURL_DISABLE_PROXY
27
28
#include "urldata.h"
29
#include "cfilters.h"
30
#include "cf-haproxy.h"
31
#include "connect.h"
32
#include "curl_addrinfo.h"
33
#include "curl_trc.h"
34
#include "select.h"
35
36
37
typedef enum {
38
  HAPROXY_INIT,     /* init/default/no tunnel state */
39
  HAPROXY_SEND,     /* data_out being sent */
40
  HAPROXY_DONE      /* all work done */
41
} haproxy_state;
42
43
struct cf_haproxy_ctx {
44
  int state;
45
  struct dynbuf data_out;
46
};
47
48
static void cf_haproxy_ctx_free(struct cf_haproxy_ctx *ctx)
49
0
{
50
0
  if(ctx) {
51
0
    curlx_dyn_free(&ctx->data_out);
52
0
    curlx_free(ctx);
53
0
  }
54
0
}
55
56
static CURLcode cf_haproxy_date_out_set(struct Curl_cfilter *cf,
57
                                        struct Curl_easy *data)
58
0
{
59
  /* We fake a client connection report to the upstream server
60
   * with the HAProxy protocol, reporting the client's source
61
   * and destination IP addresses and ports.
62
   * addresses: either the ones used to talk to the upstream
63
   *            OR the value supplied by the user
64
   * ports: the ports used in the upstream connection */
65
0
  const char *client_source_ip;
66
0
  const char *client_dest_ip;
67
0
  struct cf_haproxy_ctx *ctx = cf->ctx;
68
0
  CURLcode result;
69
0
  struct ip_quadruple ipquad;
70
0
  bool is_ipv6;
71
72
0
  DEBUGASSERT(ctx);
73
0
  DEBUGASSERT(ctx->state == HAPROXY_INIT);
74
#ifdef USE_UNIX_SOCKETS
75
  if(Curl_conn_get_first_peer(cf->conn, cf->sockindex)->unix_socket)
76
    /* the buffer is large enough to hold this! */
77
    result = curlx_dyn_addn(&ctx->data_out, STRCONST("PROXY UNKNOWN\r\n"));
78
  else {
79
#endif /* USE_UNIX_SOCKETS */
80
0
  result = Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad);
81
0
  if(result)
82
0
    return result;
83
84
0
  if(data->set.str[STRING_HAPROXY_CLIENT_IP]) {
85
0
    client_source_ip = data->set.str[STRING_HAPROXY_CLIENT_IP];
86
0
    client_dest_ip = client_source_ip;
87
0
    is_ipv6 = !Curl_is_ipv4addr(client_source_ip);
88
0
  }
89
0
  else {
90
0
    client_source_ip = ipquad.local_ip;
91
0
    client_dest_ip = ipquad.remote_ip;
92
0
  }
93
94
0
  result = curlx_dyn_addf(&ctx->data_out, "PROXY %s %s %s %d %d\r\n",
95
0
                          is_ipv6 ? "TCP6" : "TCP4",
96
0
                          client_source_ip, client_dest_ip,
97
0
                          ipquad.local_port, ipquad.remote_port);
98
99
#ifdef USE_UNIX_SOCKETS
100
  }
101
#endif /* USE_UNIX_SOCKETS */
102
0
  return result;
103
0
}
104
105
static CURLcode cf_haproxy_connect(struct Curl_cfilter *cf,
106
                                   struct Curl_easy *data,
107
                                   bool *done)
108
0
{
109
0
  struct cf_haproxy_ctx *ctx = cf->ctx;
110
0
  CURLcode result;
111
0
  size_t len;
112
113
0
  DEBUGASSERT(ctx);
114
0
  if(cf->connected) {
115
0
    *done = TRUE;
116
0
    return CURLE_OK;
117
0
  }
118
119
0
  result = cf->next->cft->do_connect(cf->next, data, done);
120
0
  if(result || !*done)
121
0
    return result;
122
123
0
  switch(ctx->state) {
124
0
  case HAPROXY_INIT:
125
0
    result = cf_haproxy_date_out_set(cf, data);
126
0
    if(result)
127
0
      goto out;
128
0
    ctx->state = HAPROXY_SEND;
129
0
    FALLTHROUGH();
130
0
  case HAPROXY_SEND:
131
0
    len = curlx_dyn_len(&ctx->data_out);
132
0
    if(len > 0) {
133
0
      size_t nwritten;
134
0
      result = Curl_conn_cf_send(cf->next, data,
135
0
                                 curlx_dyn_uptr(&ctx->data_out), len, FALSE,
136
0
                                 &nwritten);
137
0
      if(result) {
138
0
        if(result != CURLE_AGAIN)
139
0
          goto out;
140
0
        result = CURLE_OK;
141
0
        nwritten = 0;
142
0
      }
143
0
      curlx_dyn_tail(&ctx->data_out, len - nwritten);
144
0
      if(curlx_dyn_len(&ctx->data_out) > 0) {
145
0
        result = CURLE_OK;
146
0
        goto out;
147
0
      }
148
0
    }
149
0
    ctx->state = HAPROXY_DONE;
150
0
    FALLTHROUGH();
151
0
  default:
152
0
    curlx_dyn_free(&ctx->data_out);
153
0
    break;
154
0
  }
155
156
0
out:
157
0
  *done = (!result) && (ctx->state == HAPROXY_DONE);
158
0
  cf->connected = *done;
159
0
  return result;
160
0
}
161
162
static void cf_haproxy_destroy(struct Curl_cfilter *cf,
163
                               struct Curl_easy *data)
164
0
{
165
0
  CURL_TRC_CF(data, cf, "destroy");
166
0
  cf_haproxy_ctx_free(cf->ctx);
167
0
}
168
169
static CURLcode cf_haproxy_adjust_pollset(struct Curl_cfilter *cf,
170
                                          struct Curl_easy *data,
171
                                          struct easy_pollset *ps)
172
0
{
173
0
  if(cf->next->connected && !cf->connected) {
174
    /* If we are not connected, but the filter "below" is
175
     * and not waiting on something, we are sending. */
176
0
    return Curl_pollset_set_out_only(
177
0
      data, ps, Curl_conn_cf_get_socket(cf, data));
178
0
  }
179
0
  return CURLE_OK;
180
0
}
181
182
struct Curl_cftype Curl_cft_haproxy = {
183
  "HAPROXY",
184
  CF_TYPE_PROXY | CF_TYPE_SETUP,
185
  0,
186
  cf_haproxy_destroy,
187
  cf_haproxy_connect,
188
  Curl_cf_def_shutdown,
189
  cf_haproxy_adjust_pollset,
190
  Curl_cf_def_data_pending,
191
  Curl_cf_def_send,
192
  Curl_cf_def_recv,
193
  Curl_cf_def_cntrl,
194
  Curl_cf_def_conn_is_alive,
195
  Curl_cf_def_conn_keep_alive,
196
  Curl_cf_def_query,
197
};
198
199
static CURLcode cf_haproxy_create(struct Curl_cfilter **pcf,
200
                                  struct Curl_easy *data)
201
0
{
202
0
  struct Curl_cfilter *cf = NULL;
203
0
  struct cf_haproxy_ctx *ctx;
204
0
  CURLcode result;
205
206
0
  (void)data;
207
0
  ctx = curlx_calloc(1, sizeof(*ctx));
208
0
  if(!ctx) {
209
0
    result = CURLE_OUT_OF_MEMORY;
210
0
    goto out;
211
0
  }
212
0
  ctx->state = HAPROXY_INIT;
213
0
  curlx_dyn_init(&ctx->data_out, DYN_HAXPROXY);
214
215
0
  result = Curl_cf_create(&cf, &Curl_cft_haproxy, ctx);
216
0
  if(result)
217
0
    goto out;
218
0
  ctx = NULL;
219
220
0
out:
221
0
  cf_haproxy_ctx_free(ctx);
222
0
  *pcf = result ? NULL : cf;
223
0
  return result;
224
0
}
225
226
CURLcode Curl_cf_haproxy_insert_after(struct Curl_cfilter *cf_at,
227
                                      struct Curl_easy *data)
228
0
{
229
0
  struct Curl_cfilter *cf;
230
0
  CURLcode result;
231
232
0
  result = cf_haproxy_create(&cf, data);
233
0
  if(result)
234
0
    goto out;
235
0
  Curl_conn_cf_insert_after(cf_at, cf);
236
237
0
out:
238
0
  return result;
239
0
}
240
241
#endif /* !CURL_DISABLE_PROXY */