Coverage Report

Created: 2026-07-30 07:26

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