Coverage Report

Created: 2026-09-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_free(struct cf_haproxy_ctx *ctx)
49
42
{
50
42
  if(ctx) {
51
21
    curlx_dyn_free(&ctx->data_out);
52
21
    curlx_free(ctx);
53
21
  }
54
42
}
55
56
static CURLcode cf_haproxy_date_out_set(struct Curl_cfilter *cf,
57
                                        struct Curl_easy *data)
58
21
{
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
21
  const char *client_source_ip;
66
21
  const char *client_dest_ip;
67
21
  struct cf_haproxy_ctx *ctx = cf->ctx;
68
21
  CURLcode result;
69
21
  struct ip_quadruple ipquad;
70
21
  bool is_ipv6;
71
72
21
  DEBUGASSERT(ctx);
73
21
  DEBUGASSERT(ctx->state == HAPROXY_INIT);
74
21
#ifdef USE_UNIX_SOCKETS
75
21
  if(Curl_conn_get_first_peer(cf->conn, cf->sockindex)->unix_socket)
76
    /* the buffer is large enough to hold this! */
77
1
    result = curlx_dyn_addn(&ctx->data_out, STRCONST("PROXY UNKNOWN\r\n"));
78
20
  else {
79
20
#endif /* USE_UNIX_SOCKETS */
80
20
  result = Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad);
81
20
  if(result)
82
0
    return result;
83
84
20
  client_source_ip = CURL_EASY_STR(data, STRING_HAPROXY_CLIENT_IP);
85
20
  if(client_source_ip) {
86
19
    client_dest_ip = client_source_ip;
87
19
    is_ipv6 = !Curl_is_ipv4addr(client_source_ip);
88
19
  }
89
1
  else {
90
1
    client_source_ip = ipquad.local_ip;
91
1
    client_dest_ip = ipquad.remote_ip;
92
1
  }
93
94
20
  result = curlx_dyn_addf(&ctx->data_out, "PROXY %s %s %s %d %d\r\n",
95
20
                          is_ipv6 ? "TCP6" : "TCP4",
96
20
                          client_source_ip, client_dest_ip,
97
20
                          ipquad.local_port, ipquad.remote_port);
98
99
20
#ifdef USE_UNIX_SOCKETS
100
20
  }
101
21
#endif /* USE_UNIX_SOCKETS */
102
21
  return result;
103
21
}
104
105
static CURLcode cf_haproxy_connect(struct Curl_cfilter *cf,
106
                                   struct Curl_easy *data,
107
                                   bool *done)
108
21
{
109
21
  struct cf_haproxy_ctx *ctx = cf->ctx;
110
21
  CURLcode result;
111
21
  size_t len;
112
113
21
  DEBUGASSERT(ctx);
114
21
  if(cf->connected) {
115
0
    *done = TRUE;
116
0
    return CURLE_OK;
117
0
  }
118
119
21
  result = cf->next->cft->do_connect(cf->next, data, done);
120
21
  if(result || !*done)
121
0
    return result;
122
123
21
  switch(ctx->state) {
124
21
  case HAPROXY_INIT:
125
21
    result = cf_haproxy_date_out_set(cf, data);
126
21
    if(result)
127
2
      goto out;
128
19
    ctx->state = HAPROXY_SEND;
129
19
    FALLTHROUGH();
130
19
  case HAPROXY_SEND:
131
19
    len = curlx_dyn_len(&ctx->data_out);
132
19
    if(len > 0) {
133
19
      size_t nwritten;
134
19
      result = Curl_conn_cf_send(cf->next, data,
135
19
                                 curlx_dyn_uptr(&ctx->data_out), len, FALSE,
136
19
                                 &nwritten);
137
19
      if(result) {
138
0
        if(result != CURLE_AGAIN)
139
0
          goto out;
140
0
        result = CURLE_OK;
141
0
        nwritten = 0;
142
0
      }
143
19
      curlx_dyn_tail(&ctx->data_out, len - nwritten);
144
19
      if(curlx_dyn_len(&ctx->data_out) > 0) {
145
0
        result = CURLE_OK;
146
0
        goto out;
147
0
      }
148
19
    }
149
19
    ctx->state = HAPROXY_DONE;
150
19
    FALLTHROUGH();
151
19
  default:
152
19
    curlx_dyn_free(&ctx->data_out);
153
19
    break;
154
21
  }
155
156
21
out:
157
21
  *done = (!result) && (ctx->state == HAPROXY_DONE);
158
21
  cf->connected = *done;
159
21
  return result;
160
21
}
161
162
static void cf_haproxy_destroy(struct Curl_cfilter *cf,
163
                               struct Curl_easy *data)
164
21
{
165
21
  CURL_TRC_CF(data, cf, "destroy");
166
21
  cf_haproxy_ctx_free(cf->ctx);
167
21
}
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
21
{
202
21
  struct Curl_cfilter *cf = NULL;
203
21
  struct cf_haproxy_ctx *ctx;
204
21
  CURLcode result;
205
206
21
  (void)data;
207
21
  ctx = curlx_calloc(1, sizeof(*ctx));
208
21
  if(!ctx) {
209
0
    result = CURLE_OUT_OF_MEMORY;
210
0
    goto out;
211
0
  }
212
21
  ctx->state = HAPROXY_INIT;
213
21
  curlx_dyn_init(&ctx->data_out, DYN_HAXPROXY);
214
215
21
  result = Curl_cf_create(&cf, &Curl_cft_haproxy, ctx);
216
21
  if(result)
217
0
    goto out;
218
21
  ctx = NULL;
219
220
21
out:
221
21
  cf_haproxy_ctx_free(ctx);
222
21
  *pcf = result ? NULL : cf;
223
21
  return result;
224
21
}
225
226
CURLcode Curl_cf_haproxy_insert_after(struct Curl_cfilter *cf_at,
227
                                      struct Curl_easy *data)
228
21
{
229
21
  struct Curl_cfilter *cf;
230
21
  CURLcode result;
231
232
21
  result = cf_haproxy_create(&cf, data);
233
21
  if(result)
234
0
    goto out;
235
21
  Curl_conn_cf_insert_after(cf_at, cf);
236
237
21
out:
238
21
  return result;
239
21
}
240
241
#endif /* !CURL_DISABLE_PROXY */