Coverage Report

Created: 2025-08-26 07:08

/src/PROJ/curl/lib/cf-haproxy.c
Line
Count
Source (jump to first uncovered line)
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
25
#include "curl_setup.h"
26
27
#ifndef CURL_DISABLE_PROXY
28
29
#include <curl/curl.h>
30
#include "urldata.h"
31
#include "cfilters.h"
32
#include "cf-haproxy.h"
33
#include "curl_trc.h"
34
#include "multiif.h"
35
#include "select.h"
36
37
/* The last 3 #include files should be in this order */
38
#include "curl_printf.h"
39
#include "curl_memory.h"
40
#include "memdebug.h"
41
42
43
typedef enum {
44
    HAPROXY_INIT,     /* init/default/no tunnel state */
45
    HAPROXY_SEND,     /* data_out being sent */
46
    HAPROXY_DONE      /* all work done */
47
} haproxy_state;
48
49
struct cf_haproxy_ctx {
50
  int state;
51
  struct dynbuf data_out;
52
};
53
54
static void cf_haproxy_ctx_reset(struct cf_haproxy_ctx *ctx)
55
0
{
56
0
  DEBUGASSERT(ctx);
57
0
  ctx->state = HAPROXY_INIT;
58
0
  curlx_dyn_reset(&ctx->data_out);
59
0
}
60
61
static void cf_haproxy_ctx_free(struct cf_haproxy_ctx *ctx)
62
0
{
63
0
  if(ctx) {
64
0
    curlx_dyn_free(&ctx->data_out);
65
0
    free(ctx);
66
0
  }
67
0
}
68
69
static CURLcode cf_haproxy_date_out_set(struct Curl_cfilter*cf,
70
                                        struct Curl_easy *data)
71
0
{
72
0
  struct cf_haproxy_ctx *ctx = cf->ctx;
73
0
  CURLcode result;
74
0
  const char *client_ip;
75
0
  struct ip_quadruple ipquad;
76
0
  bool is_ipv6;
77
78
0
  DEBUGASSERT(ctx);
79
0
  DEBUGASSERT(ctx->state == HAPROXY_INIT);
80
0
#ifdef USE_UNIX_SOCKETS
81
0
  if(cf->conn->unix_domain_socket)
82
    /* the buffer is large enough to hold this! */
83
0
    result = curlx_dyn_addn(&ctx->data_out, STRCONST("PROXY UNKNOWN\r\n"));
84
0
  else {
85
0
#endif /* USE_UNIX_SOCKETS */
86
0
  result = Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad);
87
0
  if(result)
88
0
    return result;
89
90
  /* Emit the correct prefix for IPv6 */
91
0
  if(data->set.str[STRING_HAPROXY_CLIENT_IP])
92
0
    client_ip = data->set.str[STRING_HAPROXY_CLIENT_IP];
93
0
  else
94
0
    client_ip = ipquad.local_ip;
95
96
0
  result = curlx_dyn_addf(&ctx->data_out, "PROXY %s %s %s %i %i\r\n",
97
0
                          is_ipv6 ? "TCP6" : "TCP4",
98
0
                          client_ip, ipquad.remote_ip,
99
0
                          ipquad.local_port, ipquad.remote_port);
100
101
0
#ifdef USE_UNIX_SOCKETS
102
0
  }
103
0
#endif /* USE_UNIX_SOCKETS */
104
0
  return result;
105
0
}
106
107
static CURLcode cf_haproxy_connect(struct Curl_cfilter *cf,
108
                                   struct Curl_easy *data,
109
                                   bool *done)
110
0
{
111
0
  struct cf_haproxy_ctx *ctx = cf->ctx;
112
0
  CURLcode result;
113
0
  size_t len;
114
115
0
  DEBUGASSERT(ctx);
116
0
  if(cf->connected) {
117
0
    *done = TRUE;
118
0
    return CURLE_OK;
119
0
  }
120
121
0
  result = cf->next->cft->do_connect(cf->next, data, done);
122
0
  if(result || !*done)
123
0
    return result;
124
125
0
  switch(ctx->state) {
126
0
  case HAPROXY_INIT:
127
0
    result = cf_haproxy_date_out_set(cf, data);
128
0
    if(result)
129
0
      goto out;
130
0
    ctx->state = HAPROXY_SEND;
131
0
    FALLTHROUGH();
132
0
  case HAPROXY_SEND:
133
0
    len = curlx_dyn_len(&ctx->data_out);
134
0
    if(len > 0) {
135
0
      size_t nwritten;
136
0
      result = Curl_conn_cf_send(cf->next, data,
137
0
                                 curlx_dyn_ptr(&ctx->data_out), len, FALSE,
138
0
                                 &nwritten);
139
0
      if(result) {
140
0
        if(result != CURLE_AGAIN)
141
0
          goto out;
142
0
        result = CURLE_OK;
143
0
        nwritten = 0;
144
0
      }
145
0
      curlx_dyn_tail(&ctx->data_out, len - nwritten);
146
0
      if(curlx_dyn_len(&ctx->data_out) > 0) {
147
0
        result = CURLE_OK;
148
0
        goto out;
149
0
      }
150
0
    }
151
0
    ctx->state = HAPROXY_DONE;
152
0
    FALLTHROUGH();
153
0
  default:
154
0
    curlx_dyn_free(&ctx->data_out);
155
0
    break;
156
0
  }
157
158
0
out:
159
0
  *done = (!result) && (ctx->state == HAPROXY_DONE);
160
0
  cf->connected = *done;
161
0
  return result;
162
0
}
163
164
static void cf_haproxy_destroy(struct Curl_cfilter *cf,
165
                               struct Curl_easy *data)
166
0
{
167
0
  (void)data;
168
0
  CURL_TRC_CF(data, cf, "destroy");
169
0
  cf_haproxy_ctx_free(cf->ctx);
170
0
}
171
172
static void cf_haproxy_close(struct Curl_cfilter *cf,
173
                             struct Curl_easy *data)
174
0
{
175
0
  CURL_TRC_CF(data, cf, "close");
176
0
  cf->connected = FALSE;
177
0
  cf_haproxy_ctx_reset(cf->ctx);
178
0
  if(cf->next)
179
0
    cf->next->cft->do_close(cf->next, data);
180
0
}
181
182
static CURLcode cf_haproxy_adjust_pollset(struct Curl_cfilter *cf,
183
                                          struct Curl_easy *data,
184
                                          struct easy_pollset *ps)
185
0
{
186
0
  if(cf->next->connected && !cf->connected) {
187
    /* If we are not connected, but the filter "below" is
188
     * and not waiting on something, we are sending. */
189
0
    return Curl_pollset_set_out_only(
190
0
      data, ps, Curl_conn_cf_get_socket(cf, data));
191
0
  }
192
0
  return CURLE_OK;
193
0
}
194
195
struct Curl_cftype Curl_cft_haproxy = {
196
  "HAPROXY",
197
  CF_TYPE_PROXY,
198
  0,
199
  cf_haproxy_destroy,
200
  cf_haproxy_connect,
201
  cf_haproxy_close,
202
  Curl_cf_def_shutdown,
203
  cf_haproxy_adjust_pollset,
204
  Curl_cf_def_data_pending,
205
  Curl_cf_def_send,
206
  Curl_cf_def_recv,
207
  Curl_cf_def_cntrl,
208
  Curl_cf_def_conn_is_alive,
209
  Curl_cf_def_conn_keep_alive,
210
  Curl_cf_def_query,
211
};
212
213
static CURLcode cf_haproxy_create(struct Curl_cfilter **pcf,
214
                                  struct Curl_easy *data)
215
0
{
216
0
  struct Curl_cfilter *cf = NULL;
217
0
  struct cf_haproxy_ctx *ctx;
218
0
  CURLcode result;
219
220
0
  (void)data;
221
0
  ctx = calloc(1, sizeof(*ctx));
222
0
  if(!ctx) {
223
0
    result = CURLE_OUT_OF_MEMORY;
224
0
    goto out;
225
0
  }
226
0
  ctx->state = HAPROXY_INIT;
227
0
  curlx_dyn_init(&ctx->data_out, DYN_HAXPROXY);
228
229
0
  result = Curl_cf_create(&cf, &Curl_cft_haproxy, ctx);
230
0
  if(result)
231
0
    goto out;
232
0
  ctx = NULL;
233
234
0
out:
235
0
  cf_haproxy_ctx_free(ctx);
236
0
  *pcf = result ? NULL : cf;
237
0
  return result;
238
0
}
239
240
CURLcode Curl_cf_haproxy_insert_after(struct Curl_cfilter *cf_at,
241
                                      struct Curl_easy *data)
242
0
{
243
0
  struct Curl_cfilter *cf;
244
0
  CURLcode result;
245
246
0
  result = cf_haproxy_create(&cf, data);
247
0
  if(result)
248
0
    goto out;
249
0
  Curl_conn_cf_insert_after(cf_at, cf);
250
251
0
out:
252
0
  return result;
253
0
}
254
255
#endif /* !CURL_DISABLE_PROXY */