xref: /third_party/curl/tests/libtest/lib650.c (revision 13498266)
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#define CURL_DISABLE_DEPRECATION  /* Using and testing the form api */
2513498266Sopenharmony_ci#include "test.h"
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#include "memdebug.h"
2813498266Sopenharmony_ci
2913498266Sopenharmony_cistatic char data[] =
3013498266Sopenharmony_ci  "this is what we post to the silly web server";
3113498266Sopenharmony_ci
3213498266Sopenharmony_cistatic const char name[] = "fieldname";
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci
3513498266Sopenharmony_ci/* This test attempts to use all form API features that are not
3613498266Sopenharmony_ci * used elsewhere.
3713498266Sopenharmony_ci */
3813498266Sopenharmony_ci
3913498266Sopenharmony_ci/* curl_formget callback to count characters. */
4013498266Sopenharmony_cistatic size_t count_chars(void *userp, const char *buf, size_t len)
4113498266Sopenharmony_ci{
4213498266Sopenharmony_ci  size_t *pcounter = (size_t *) userp;
4313498266Sopenharmony_ci
4413498266Sopenharmony_ci  (void) buf;
4513498266Sopenharmony_ci  *pcounter += len;
4613498266Sopenharmony_ci  return len;
4713498266Sopenharmony_ci}
4813498266Sopenharmony_ci
4913498266Sopenharmony_ci
5013498266Sopenharmony_ciint test(char *URL)
5113498266Sopenharmony_ci{
5213498266Sopenharmony_ci  CURL *curl = NULL;
5313498266Sopenharmony_ci  CURLcode res = TEST_ERR_MAJOR_BAD;
5413498266Sopenharmony_ci  CURLFORMcode formrc;
5513498266Sopenharmony_ci  struct curl_slist *headers, *headers2 = NULL;
5613498266Sopenharmony_ci  struct curl_httppost *formpost = NULL;
5713498266Sopenharmony_ci  struct curl_httppost *lastptr = NULL;
5813498266Sopenharmony_ci  struct curl_forms formarray[3];
5913498266Sopenharmony_ci  size_t formlength = 0;
6013498266Sopenharmony_ci  char flbuf[32];
6113498266Sopenharmony_ci  long contentlength = 0;
6213498266Sopenharmony_ci
6313498266Sopenharmony_ci  if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
6413498266Sopenharmony_ci    fprintf(stderr, "curl_global_init() failed\n");
6513498266Sopenharmony_ci    return TEST_ERR_MAJOR_BAD;
6613498266Sopenharmony_ci  }
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci  /* Check proper name and data copying, as well as headers. */
6913498266Sopenharmony_ci  headers = curl_slist_append(NULL, "X-customheader-1: Header 1 data");
7013498266Sopenharmony_ci  if(!headers) {
7113498266Sopenharmony_ci    goto test_cleanup;
7213498266Sopenharmony_ci  }
7313498266Sopenharmony_ci  headers2 = curl_slist_append(headers, "X-customheader-2: Header 2 data");
7413498266Sopenharmony_ci  if(!headers2) {
7513498266Sopenharmony_ci    goto test_cleanup;
7613498266Sopenharmony_ci  }
7713498266Sopenharmony_ci  headers = headers2;
7813498266Sopenharmony_ci  headers2 = curl_slist_append(headers, "Content-Type: text/plain");
7913498266Sopenharmony_ci  if(!headers2) {
8013498266Sopenharmony_ci    goto test_cleanup;
8113498266Sopenharmony_ci  }
8213498266Sopenharmony_ci  headers = headers2;
8313498266Sopenharmony_ci  formrc = curl_formadd(&formpost, &lastptr,
8413498266Sopenharmony_ci                        CURLFORM_COPYNAME, &name,
8513498266Sopenharmony_ci                        CURLFORM_COPYCONTENTS, &data,
8613498266Sopenharmony_ci                        CURLFORM_CONTENTHEADER, headers,
8713498266Sopenharmony_ci                        CURLFORM_END);
8813498266Sopenharmony_ci
8913498266Sopenharmony_ci  if(formrc) {
9013498266Sopenharmony_ci    printf("curl_formadd(1) = %d\n", (int) formrc);
9113498266Sopenharmony_ci    goto test_cleanup;
9213498266Sopenharmony_ci  }
9313498266Sopenharmony_ci
9413498266Sopenharmony_ci  contentlength = (long)(strlen(data) - 1);
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci  /* Use a form array for the non-copy test. */
9713498266Sopenharmony_ci  formarray[0].option = CURLFORM_PTRCONTENTS;
9813498266Sopenharmony_ci  formarray[0].value = data;
9913498266Sopenharmony_ci  formarray[1].option = CURLFORM_CONTENTSLENGTH;
10013498266Sopenharmony_ci  formarray[1].value = (char *)(size_t)contentlength;
10113498266Sopenharmony_ci  formarray[2].option = CURLFORM_END;
10213498266Sopenharmony_ci  formarray[2].value = NULL;
10313498266Sopenharmony_ci  formrc = curl_formadd(&formpost,
10413498266Sopenharmony_ci                        &lastptr,
10513498266Sopenharmony_ci                        CURLFORM_PTRNAME, name,
10613498266Sopenharmony_ci                        CURLFORM_NAMELENGTH, strlen(name) - 1,
10713498266Sopenharmony_ci                        CURLFORM_ARRAY, formarray,
10813498266Sopenharmony_ci                        CURLFORM_FILENAME, "remotefile.txt",
10913498266Sopenharmony_ci                        CURLFORM_END);
11013498266Sopenharmony_ci
11113498266Sopenharmony_ci  if(formrc) {
11213498266Sopenharmony_ci    printf("curl_formadd(2) = %d\n", (int) formrc);
11313498266Sopenharmony_ci    goto test_cleanup;
11413498266Sopenharmony_ci  }
11513498266Sopenharmony_ci
11613498266Sopenharmony_ci  /* Now change in-memory data to affect CURLOPT_PTRCONTENTS value.
11713498266Sopenharmony_ci     Copied values (first field) must not be affected.
11813498266Sopenharmony_ci     CURLOPT_PTRNAME actually copies the name thus we do not test this here. */
11913498266Sopenharmony_ci  data[0]++;
12013498266Sopenharmony_ci
12113498266Sopenharmony_ci  /* Check multi-files and content type propagation. */
12213498266Sopenharmony_ci  formrc = curl_formadd(&formpost,
12313498266Sopenharmony_ci                        &lastptr,
12413498266Sopenharmony_ci                        CURLFORM_COPYNAME, "multifile",
12513498266Sopenharmony_ci                        CURLFORM_FILE, libtest_arg2,    /* Set in first.c. */
12613498266Sopenharmony_ci                        CURLFORM_FILE, libtest_arg2,
12713498266Sopenharmony_ci                        CURLFORM_CONTENTTYPE, "text/whatever",
12813498266Sopenharmony_ci                        CURLFORM_FILE, libtest_arg2,
12913498266Sopenharmony_ci                        CURLFORM_END);
13013498266Sopenharmony_ci
13113498266Sopenharmony_ci  if(formrc) {
13213498266Sopenharmony_ci    printf("curl_formadd(3) = %d\n", (int) formrc);
13313498266Sopenharmony_ci    goto test_cleanup;
13413498266Sopenharmony_ci  }
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci  /* Check data from file content. */
13713498266Sopenharmony_ci  formrc = curl_formadd(&formpost,
13813498266Sopenharmony_ci                        &lastptr,
13913498266Sopenharmony_ci                        CURLFORM_COPYNAME, "filecontents",
14013498266Sopenharmony_ci                        CURLFORM_FILECONTENT, libtest_arg2,
14113498266Sopenharmony_ci                        CURLFORM_END);
14213498266Sopenharmony_ci
14313498266Sopenharmony_ci  if(formrc) {
14413498266Sopenharmony_ci    printf("curl_formadd(4) = %d\n", (int) formrc);
14513498266Sopenharmony_ci    goto test_cleanup;
14613498266Sopenharmony_ci  }
14713498266Sopenharmony_ci
14813498266Sopenharmony_ci  /* Measure the current form length.
14913498266Sopenharmony_ci   * This is done before including stdin data because we want to reuse it
15013498266Sopenharmony_ci   * and stdin cannot be rewound.
15113498266Sopenharmony_ci   */
15213498266Sopenharmony_ci  curl_formget(formpost, (void *) &formlength, count_chars);
15313498266Sopenharmony_ci
15413498266Sopenharmony_ci  /* Include length in data for external check. */
15513498266Sopenharmony_ci  curl_msnprintf(flbuf, sizeof(flbuf), "%lu", (unsigned long) formlength);
15613498266Sopenharmony_ci  formrc = curl_formadd(&formpost,
15713498266Sopenharmony_ci                        &lastptr,
15813498266Sopenharmony_ci                        CURLFORM_COPYNAME, "formlength",
15913498266Sopenharmony_ci                        CURLFORM_COPYCONTENTS, &flbuf,
16013498266Sopenharmony_ci                        CURLFORM_END);
16113498266Sopenharmony_ci  if(formrc) {
16213498266Sopenharmony_ci    printf("curl_formadd(5) = %d\n", (int) formrc);
16313498266Sopenharmony_ci    goto test_cleanup;
16413498266Sopenharmony_ci  }
16513498266Sopenharmony_ci
16613498266Sopenharmony_ci  /* Check stdin (may be problematic on some platforms). */
16713498266Sopenharmony_ci  formrc = curl_formadd(&formpost,
16813498266Sopenharmony_ci                        &lastptr,
16913498266Sopenharmony_ci                        CURLFORM_COPYNAME, "standardinput",
17013498266Sopenharmony_ci                        CURLFORM_FILE, "-",
17113498266Sopenharmony_ci                        CURLFORM_END);
17213498266Sopenharmony_ci  if(formrc) {
17313498266Sopenharmony_ci    printf("curl_formadd(6) = %d\n", (int) formrc);
17413498266Sopenharmony_ci    goto test_cleanup;
17513498266Sopenharmony_ci  }
17613498266Sopenharmony_ci
17713498266Sopenharmony_ci  curl = curl_easy_init();
17813498266Sopenharmony_ci  if(!curl) {
17913498266Sopenharmony_ci    fprintf(stderr, "curl_easy_init() failed\n");
18013498266Sopenharmony_ci    goto test_cleanup;
18113498266Sopenharmony_ci  }
18213498266Sopenharmony_ci
18313498266Sopenharmony_ci  /* First set the URL that is about to receive our POST. */
18413498266Sopenharmony_ci  test_setopt(curl, CURLOPT_URL, URL);
18513498266Sopenharmony_ci
18613498266Sopenharmony_ci  /* send a multi-part formpost */
18713498266Sopenharmony_ci  test_setopt(curl, CURLOPT_HTTPPOST, formpost);
18813498266Sopenharmony_ci
18913498266Sopenharmony_ci  /* get verbose debug output please */
19013498266Sopenharmony_ci  test_setopt(curl, CURLOPT_VERBOSE, 1L);
19113498266Sopenharmony_ci
19213498266Sopenharmony_ci  test_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
19313498266Sopenharmony_ci  test_setopt(curl, CURLOPT_POSTREDIR, (long)CURL_REDIR_POST_301);
19413498266Sopenharmony_ci
19513498266Sopenharmony_ci  /* include headers in the output */
19613498266Sopenharmony_ci  test_setopt(curl, CURLOPT_HEADER, 1L);
19713498266Sopenharmony_ci
19813498266Sopenharmony_ci  /* Perform the request, res will get the return code */
19913498266Sopenharmony_ci  res = curl_easy_perform(curl);
20013498266Sopenharmony_ci
20113498266Sopenharmony_citest_cleanup:
20213498266Sopenharmony_ci
20313498266Sopenharmony_ci  /* always cleanup */
20413498266Sopenharmony_ci  curl_easy_cleanup(curl);
20513498266Sopenharmony_ci
20613498266Sopenharmony_ci  /* now cleanup the formpost chain */
20713498266Sopenharmony_ci  curl_formfree(formpost);
20813498266Sopenharmony_ci  curl_slist_free_all(headers);
20913498266Sopenharmony_ci
21013498266Sopenharmony_ci  curl_global_cleanup();
21113498266Sopenharmony_ci
21213498266Sopenharmony_ci  return res;
21313498266Sopenharmony_ci}
214