113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci/* argv1 = URL
2513498266Sopenharmony_ci * argv2 = proxy with embedded user+password
2613498266Sopenharmony_ci */
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#include "test.h"
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#include "warnless.h"
3113498266Sopenharmony_ci#include "memdebug.h"
3213498266Sopenharmony_ci
3313498266Sopenharmony_cistruct data {
3413498266Sopenharmony_ci  char trace_ascii; /* 1 or 0 */
3513498266Sopenharmony_ci};
3613498266Sopenharmony_ci
3713498266Sopenharmony_cistatic
3813498266Sopenharmony_civoid dump(const char *text,
3913498266Sopenharmony_ci          FILE *stream, unsigned char *ptr, size_t size,
4013498266Sopenharmony_ci          char nohex)
4113498266Sopenharmony_ci{
4213498266Sopenharmony_ci  size_t i;
4313498266Sopenharmony_ci  size_t c;
4413498266Sopenharmony_ci
4513498266Sopenharmony_ci  unsigned int width = 0x10;
4613498266Sopenharmony_ci
4713498266Sopenharmony_ci  if(nohex)
4813498266Sopenharmony_ci    /* without the hex output, we can fit more on screen */
4913498266Sopenharmony_ci    width = 0x40;
5013498266Sopenharmony_ci
5113498266Sopenharmony_ci  fprintf(stream, "%s, %zu bytes (0x%zx)\n", text, size, size);
5213498266Sopenharmony_ci
5313498266Sopenharmony_ci  for(i = 0; i<size; i += width) {
5413498266Sopenharmony_ci
5513498266Sopenharmony_ci    fprintf(stream, "%04zx: ", i);
5613498266Sopenharmony_ci
5713498266Sopenharmony_ci    if(!nohex) {
5813498266Sopenharmony_ci      /* hex not disabled, show it */
5913498266Sopenharmony_ci      for(c = 0; c < width; c++)
6013498266Sopenharmony_ci        if(i + c < size)
6113498266Sopenharmony_ci          fprintf(stream, "%02x ", ptr[i + c]);
6213498266Sopenharmony_ci        else
6313498266Sopenharmony_ci          fputs("   ", stream);
6413498266Sopenharmony_ci    }
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci    for(c = 0; (c < width) && (i + c < size); c++) {
6713498266Sopenharmony_ci      /* check for 0D0A; if found, skip past and start a new line of output */
6813498266Sopenharmony_ci      if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
6913498266Sopenharmony_ci         ptr[i + c + 1] == 0x0A) {
7013498266Sopenharmony_ci        i += (c + 2 - width);
7113498266Sopenharmony_ci        break;
7213498266Sopenharmony_ci      }
7313498266Sopenharmony_ci      fprintf(stream, "%c",
7413498266Sopenharmony_ci              (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)? ptr[i + c] : '.');
7513498266Sopenharmony_ci      /* check again for 0D0A, to avoid an extra \n if it's at width */
7613498266Sopenharmony_ci      if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
7713498266Sopenharmony_ci         ptr[i + c + 2] == 0x0A) {
7813498266Sopenharmony_ci        i += (c + 3 - width);
7913498266Sopenharmony_ci        break;
8013498266Sopenharmony_ci      }
8113498266Sopenharmony_ci    }
8213498266Sopenharmony_ci    fputc('\n', stream); /* newline */
8313498266Sopenharmony_ci  }
8413498266Sopenharmony_ci  fflush(stream);
8513498266Sopenharmony_ci}
8613498266Sopenharmony_ci
8713498266Sopenharmony_cistatic
8813498266Sopenharmony_ciint my_trace(CURL *handle, curl_infotype type,
8913498266Sopenharmony_ci             char *data, size_t size,
9013498266Sopenharmony_ci             void *userp)
9113498266Sopenharmony_ci{
9213498266Sopenharmony_ci  struct data *config = (struct data *)userp;
9313498266Sopenharmony_ci  const char *text;
9413498266Sopenharmony_ci  (void)handle; /* prevent compiler warning */
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci  switch(type) {
9713498266Sopenharmony_ci  case CURLINFO_TEXT:
9813498266Sopenharmony_ci    fprintf(stderr, "== Info: %s", (char *)data);
9913498266Sopenharmony_ci    return 0;
10013498266Sopenharmony_ci  case CURLINFO_HEADER_OUT:
10113498266Sopenharmony_ci    text = "=> Send header";
10213498266Sopenharmony_ci    break;
10313498266Sopenharmony_ci  case CURLINFO_DATA_OUT:
10413498266Sopenharmony_ci    text = "=> Send data";
10513498266Sopenharmony_ci    break;
10613498266Sopenharmony_ci  case CURLINFO_SSL_DATA_OUT:
10713498266Sopenharmony_ci    text = "=> Send SSL data";
10813498266Sopenharmony_ci    break;
10913498266Sopenharmony_ci  case CURLINFO_HEADER_IN:
11013498266Sopenharmony_ci    text = "<= Recv header";
11113498266Sopenharmony_ci    break;
11213498266Sopenharmony_ci  case CURLINFO_DATA_IN:
11313498266Sopenharmony_ci    text = "<= Recv data";
11413498266Sopenharmony_ci    break;
11513498266Sopenharmony_ci  case CURLINFO_SSL_DATA_IN:
11613498266Sopenharmony_ci    text = "<= Recv SSL data";
11713498266Sopenharmony_ci    break;
11813498266Sopenharmony_ci  default: /* in case a new one is introduced to shock us */
11913498266Sopenharmony_ci    return 0;
12013498266Sopenharmony_ci  }
12113498266Sopenharmony_ci
12213498266Sopenharmony_ci  dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
12313498266Sopenharmony_ci  return 0;
12413498266Sopenharmony_ci}
12513498266Sopenharmony_ci
12613498266Sopenharmony_ci
12713498266Sopenharmony_cistatic size_t current_offset = 0;
12813498266Sopenharmony_cistatic char databuf[70000]; /* MUST be more than 64k OR
12913498266Sopenharmony_ci                               MAX_INITIAL_POST_SIZE */
13013498266Sopenharmony_ci
13113498266Sopenharmony_cistatic size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
13213498266Sopenharmony_ci{
13313498266Sopenharmony_ci  size_t  amount = nmemb * size; /* Total bytes curl wants */
13413498266Sopenharmony_ci  size_t  available = sizeof(databuf) - current_offset; /* What we have to
13513498266Sopenharmony_ci                                                           give */
13613498266Sopenharmony_ci  size_t  given = amount < available ? amount : available; /* What is given */
13713498266Sopenharmony_ci  (void)stream;
13813498266Sopenharmony_ci  memcpy(ptr, databuf + current_offset, given);
13913498266Sopenharmony_ci  current_offset += given;
14013498266Sopenharmony_ci  return given;
14113498266Sopenharmony_ci}
14213498266Sopenharmony_ci
14313498266Sopenharmony_ci
14413498266Sopenharmony_cistatic size_t write_callback(void *ptr, size_t size, size_t nmemb,
14513498266Sopenharmony_ci                             void *stream)
14613498266Sopenharmony_ci{
14713498266Sopenharmony_ci  int amount = curlx_uztosi(size * nmemb);
14813498266Sopenharmony_ci  printf("%.*s", amount, (char *)ptr);
14913498266Sopenharmony_ci  (void)stream;
15013498266Sopenharmony_ci  return size * nmemb;
15113498266Sopenharmony_ci}
15213498266Sopenharmony_ci
15313498266Sopenharmony_ci
15413498266Sopenharmony_cistatic curlioerr ioctl_callback(CURL *handle, int cmd, void *clientp)
15513498266Sopenharmony_ci{
15613498266Sopenharmony_ci  (void)clientp;
15713498266Sopenharmony_ci  if(cmd == CURLIOCMD_RESTARTREAD) {
15813498266Sopenharmony_ci    printf("APPLICATION received a CURLIOCMD_RESTARTREAD request\n");
15913498266Sopenharmony_ci    printf("APPLICATION ** REWINDING! **\n");
16013498266Sopenharmony_ci    current_offset = 0;
16113498266Sopenharmony_ci    return CURLIOE_OK;
16213498266Sopenharmony_ci  }
16313498266Sopenharmony_ci  (void)handle;
16413498266Sopenharmony_ci  return CURLIOE_UNKNOWNCMD;
16513498266Sopenharmony_ci}
16613498266Sopenharmony_ci
16713498266Sopenharmony_ci
16813498266Sopenharmony_ci
16913498266Sopenharmony_ciint test(char *URL)
17013498266Sopenharmony_ci{
17113498266Sopenharmony_ci  CURL *curl;
17213498266Sopenharmony_ci  CURLcode res = CURLE_OK;
17313498266Sopenharmony_ci  struct data config;
17413498266Sopenharmony_ci  size_t i;
17513498266Sopenharmony_ci  static const char fill[] = "test data";
17613498266Sopenharmony_ci
17713498266Sopenharmony_ci  config.trace_ascii = 1; /* enable ascii tracing */
17813498266Sopenharmony_ci
17913498266Sopenharmony_ci  global_init(CURL_GLOBAL_ALL);
18013498266Sopenharmony_ci  easy_init(curl);
18113498266Sopenharmony_ci
18213498266Sopenharmony_ci  test_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
18313498266Sopenharmony_ci  test_setopt(curl, CURLOPT_DEBUGDATA, &config);
18413498266Sopenharmony_ci  /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
18513498266Sopenharmony_ci  test_setopt(curl, CURLOPT_VERBOSE, 1L);
18613498266Sopenharmony_ci
18713498266Sopenharmony_ci  /* setup repeated data string */
18813498266Sopenharmony_ci  for(i = 0; i < sizeof(databuf); ++i)
18913498266Sopenharmony_ci    databuf[i] = fill[i % sizeof(fill)];
19013498266Sopenharmony_ci
19113498266Sopenharmony_ci  /* Post */
19213498266Sopenharmony_ci  test_setopt(curl, CURLOPT_POST, 1L);
19313498266Sopenharmony_ci
19413498266Sopenharmony_ci  /* Setup read callback */
19513498266Sopenharmony_ci  test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) sizeof(databuf));
19613498266Sopenharmony_ci  test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
19713498266Sopenharmony_ci
19813498266Sopenharmony_ci  /* Write callback */
19913498266Sopenharmony_ci  test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
20013498266Sopenharmony_ci
20113498266Sopenharmony_ci  /* Ioctl function */
20213498266Sopenharmony_ci  CURL_IGNORE_DEPRECATION(
20313498266Sopenharmony_ci    test_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_callback);
20413498266Sopenharmony_ci  )
20513498266Sopenharmony_ci
20613498266Sopenharmony_ci  test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
20713498266Sopenharmony_ci
20813498266Sopenharmony_ci  test_setopt(curl, CURLOPT_URL, URL);
20913498266Sopenharmony_ci
21013498266Sopenharmony_ci  /* Accept any auth. But for this bug configure proxy with DIGEST, basic
21113498266Sopenharmony_ci     might work too, not NTLM */
21213498266Sopenharmony_ci  test_setopt(curl, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
21313498266Sopenharmony_ci
21413498266Sopenharmony_ci  res = curl_easy_perform(curl);
21513498266Sopenharmony_ci
21613498266Sopenharmony_citest_cleanup:
21713498266Sopenharmony_ci
21813498266Sopenharmony_ci  curl_easy_cleanup(curl);
21913498266Sopenharmony_ci  curl_global_cleanup();
22013498266Sopenharmony_ci  return (int)res;
22113498266Sopenharmony_ci}
222