Coverage Report

Created: 2026-06-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/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_reset(struct cf_haproxy_ctx *ctx)
49
59
{
50
59
  DEBUGASSERT(ctx);
51
59
  ctx->state = HAPROXY_INIT;
52
59
  curlx_dyn_reset(&ctx->data_out);
53
59
}
54
55
static void cf_haproxy_ctx_free(struct cf_haproxy_ctx *ctx)
56
1.39k
{
57
1.39k
  if(ctx) {
58
699
    curlx_dyn_free(&ctx->data_out);
59
699
    curlx_free(ctx);
60
699
  }
61
1.39k
}
62
63
static CURLcode cf_haproxy_date_out_set(struct Curl_cfilter *cf,
64
                                        struct Curl_easy *data)
65
699
{
66
  /* We fake a client connection report to the upstream server
67
   * with the HAProxy protocol, reporting the client's source
68
   * and destination IP addresses and ports.
69
   * addresses: either the ones used to talk to the upstream
70
   *            OR the value supplied by the user
71
   * ports: the ports used in the upstream connection */
72
699
  const char *client_source_ip;
73
699
  const char *client_dest_ip;
74
699
  struct cf_haproxy_ctx *ctx = cf->ctx;
75
699
  CURLcode result;
76
699
  struct ip_quadruple ipquad;
77
699
  bool is_ipv6;
78
79
699
  DEBUGASSERT(ctx);
80
699
  DEBUGASSERT(ctx->state == HAPROXY_INIT);
81
699
#ifdef USE_UNIX_SOCKETS
82
699
  if(Curl_conn_get_first_peer(cf->conn, cf->sockindex)->unix_socket)
83
    /* the buffer is large enough to hold this! */
84
200
    result = curlx_dyn_addn(&ctx->data_out, STRCONST("PROXY UNKNOWN\r\n"));
85
499
  else {
86
499
#endif /* USE_UNIX_SOCKETS */
87
499
  result = Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad);
88
499
  if(result)
89
0
    return result;
90
91
499
  if(data->set.str[STRING_HAPROXY_CLIENT_IP]) {
92
411
    client_source_ip = data->set.str[STRING_HAPROXY_CLIENT_IP];
93
411
    client_dest_ip = client_source_ip;
94
411
    is_ipv6 = !Curl_is_ipv4addr(client_source_ip);
95
411
  }
96
88
  else {
97
88
    client_source_ip = ipquad.local_ip;
98
88
    client_dest_ip = ipquad.remote_ip;
99
88
  }
100
101
499
  result = curlx_dyn_addf(&ctx->data_out, "PROXY %s %s %s %i %i\r\n",
102
499
                          is_ipv6 ? "TCP6" : "TCP4",
103
499
                          client_source_ip, client_dest_ip,
104
499
                          ipquad.local_port, ipquad.remote_port);
105
106
499
#ifdef USE_UNIX_SOCKETS
107
499
  }
108
699
#endif /* USE_UNIX_SOCKETS */
109
699
  return result;
110
699
}
111
112
static CURLcode cf_haproxy_connect(struct Curl_cfilter *cf,
113
                                   struct Curl_easy *data,
114
                                   bool *done)
115
699
{
116
699
  struct cf_haproxy_ctx *ctx = cf->ctx;
117
699
  CURLcode result;
118
699
  size_t len;
119
120
699
  DEBUGASSERT(ctx);
121
699
  if(cf->connected) {
122
0
    *done = TRUE;
123
0
    return CURLE_OK;
124
0
  }
125
126
699
  result = cf->next->cft->do_connect(cf->next, data, done);
127
699
  if(result || !*done)
128
0
    return result;
129
130
699
  switch(ctx->state) {
131
699
  case HAPROXY_INIT:
132
699
    result = cf_haproxy_date_out_set(cf, data);
133
699
    if(result)
134
19
      goto out;
135
680
    ctx->state = HAPROXY_SEND;
136
680
    FALLTHROUGH();
137
680
  case HAPROXY_SEND:
138
680
    len = curlx_dyn_len(&ctx->data_out);
139
680
    if(len > 0) {
140
680
      size_t nwritten;
141
680
      result = Curl_conn_cf_send(cf->next, data,
142
680
                                 curlx_dyn_uptr(&ctx->data_out), len, FALSE,
143
680
                                 &nwritten);
144
680
      if(result) {
145
0
        if(result != CURLE_AGAIN)
146
0
          goto out;
147
0
        result = CURLE_OK;
148
0
        nwritten = 0;
149
0
      }
150
680
      curlx_dyn_tail(&ctx->data_out, len - nwritten);
151
680
      if(curlx_dyn_len(&ctx->data_out) > 0) {
152
0
        result = CURLE_OK;
153
0
        goto out;
154
0
      }
155
680
    }
156
680
    ctx->state = HAPROXY_DONE;
157
680
    FALLTHROUGH();
158
680
  default:
159
680
    curlx_dyn_free(&ctx->data_out);
160
680
    break;
161
699
  }
162
163
699
out:
164
699
  *done = (!result) && (ctx->state == HAPROXY_DONE);
165
699
  cf->connected = *done;
166
699
  return result;
167
699
}
168
169
static void cf_haproxy_destroy(struct Curl_cfilter *cf,
170
                               struct Curl_easy *data)
171
699
{
172
699
  CURL_TRC_CF(data, cf, "destroy");
173
699
  cf_haproxy_ctx_free(cf->ctx);
174
699
}
175
176
static void cf_haproxy_close(struct Curl_cfilter *cf,
177
                             struct Curl_easy *data)
178
59
{
179
59
  CURL_TRC_CF(data, cf, "close");
180
59
  cf->connected = FALSE;
181
59
  cf_haproxy_ctx_reset(cf->ctx);
182
59
  if(cf->next)
183
59
    cf->next->cft->do_close(cf->next, data);
184
59
}
185
186
static CURLcode cf_haproxy_adjust_pollset(struct Curl_cfilter *cf,
187
                                          struct Curl_easy *data,
188
                                          struct easy_pollset *ps)
189
9
{
190
9
  if(cf->next->connected && !cf->connected) {
191
    /* If we are not connected, but the filter "below" is
192
     * and not waiting on something, we are sending. */
193
0
    return Curl_pollset_set_out_only(
194
0
      data, ps, Curl_conn_cf_get_socket(cf, data));
195
0
  }
196
9
  return CURLE_OK;
197
9
}
198
199
struct Curl_cftype Curl_cft_haproxy = {
200
  "HAPROXY",
201
  CF_TYPE_PROXY | CF_TYPE_SETUP,
202
  0,
203
  cf_haproxy_destroy,
204
  cf_haproxy_connect,
205
  cf_haproxy_close,
206
  Curl_cf_def_shutdown,
207
  cf_haproxy_adjust_pollset,
208
  Curl_cf_def_data_pending,
209
  Curl_cf_def_send,
210
  Curl_cf_def_recv,
211
  Curl_cf_def_cntrl,
212
  Curl_cf_def_conn_is_alive,
213
  Curl_cf_def_conn_keep_alive,
214
  Curl_cf_def_query,
215
};
216
217
static CURLcode cf_haproxy_create(struct Curl_cfilter **pcf,
218
                                  struct Curl_easy *data)
219
699
{
220
699
  struct Curl_cfilter *cf = NULL;
221
699
  struct cf_haproxy_ctx *ctx;
222
699
  CURLcode result;
223
224
699
  (void)data;
225
699
  ctx = curlx_calloc(1, sizeof(*ctx));
226
699
  if(!ctx) {
227
0
    result = CURLE_OUT_OF_MEMORY;
228
0
    goto out;
229
0
  }
230
699
  ctx->state = HAPROXY_INIT;
231
699
  curlx_dyn_init(&ctx->data_out, DYN_HAXPROXY);
232
233
699
  result = Curl_cf_create(&cf, &Curl_cft_haproxy, ctx);
234
699
  if(result)
235
0
    goto out;
236
699
  ctx = NULL;
237
238
699
out:
239
699
  cf_haproxy_ctx_free(ctx);
240
699
  *pcf = result ? NULL : cf;
241
699
  return result;
242
699
}
243
244
CURLcode Curl_cf_haproxy_insert_after(struct Curl_cfilter *cf_at,
245
                                      struct Curl_easy *data)
246
699
{
247
699
  struct Curl_cfilter *cf;
248
699
  CURLcode result;
249
250
699
  result = cf_haproxy_create(&cf, data);
251
699
  if(result)
252
0
    goto out;
253
699
  Curl_conn_cf_insert_after(cf_at, cf);
254
255
699
out:
256
699
  return result;
257
699
}
258
259
#endif /* !CURL_DISABLE_PROXY */