xref: /third_party/curl/tests/libtest/lib554.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\n";
3113498266Sopenharmony_ci
3213498266Sopenharmony_cistruct WriteThis {
3313498266Sopenharmony_ci  char *readptr;
3413498266Sopenharmony_ci  size_t sizeleft;
3513498266Sopenharmony_ci};
3613498266Sopenharmony_ci
3713498266Sopenharmony_cistatic size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
3813498266Sopenharmony_ci{
3913498266Sopenharmony_ci#ifdef LIB587
4013498266Sopenharmony_ci  (void)ptr;
4113498266Sopenharmony_ci  (void)size;
4213498266Sopenharmony_ci  (void)nmemb;
4313498266Sopenharmony_ci  (void)userp;
4413498266Sopenharmony_ci  return CURL_READFUNC_ABORT;
4513498266Sopenharmony_ci#else
4613498266Sopenharmony_ci
4713498266Sopenharmony_ci  struct WriteThis *pooh = (struct WriteThis *)userp;
4813498266Sopenharmony_ci
4913498266Sopenharmony_ci  if(size*nmemb < 1)
5013498266Sopenharmony_ci    return 0;
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci  if(pooh->sizeleft) {
5313498266Sopenharmony_ci    *ptr = pooh->readptr[0]; /* copy one single byte */
5413498266Sopenharmony_ci    pooh->readptr++;                 /* advance pointer */
5513498266Sopenharmony_ci    pooh->sizeleft--;                /* less data left */
5613498266Sopenharmony_ci    return 1;                        /* we return 1 byte at a time! */
5713498266Sopenharmony_ci  }
5813498266Sopenharmony_ci
5913498266Sopenharmony_ci  return 0;                         /* no more data left to deliver */
6013498266Sopenharmony_ci#endif
6113498266Sopenharmony_ci}
6213498266Sopenharmony_ci
6313498266Sopenharmony_cistatic int once(char *URL, bool oldstyle)
6413498266Sopenharmony_ci{
6513498266Sopenharmony_ci  CURL *curl;
6613498266Sopenharmony_ci  CURLcode res = CURLE_OK;
6713498266Sopenharmony_ci  CURLFORMcode formrc;
6813498266Sopenharmony_ci
6913498266Sopenharmony_ci  struct curl_httppost *formpost = NULL;
7013498266Sopenharmony_ci  struct curl_httppost *lastptr = NULL;
7113498266Sopenharmony_ci  struct WriteThis pooh;
7213498266Sopenharmony_ci  struct WriteThis pooh2;
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci  pooh.readptr = data;
7513498266Sopenharmony_ci  pooh.sizeleft = strlen(data);
7613498266Sopenharmony_ci
7713498266Sopenharmony_ci  /* Fill in the file upload field */
7813498266Sopenharmony_ci  if(oldstyle) {
7913498266Sopenharmony_ci    formrc = curl_formadd(&formpost,
8013498266Sopenharmony_ci                          &lastptr,
8113498266Sopenharmony_ci                          CURLFORM_COPYNAME, "sendfile",
8213498266Sopenharmony_ci                          CURLFORM_STREAM, &pooh,
8313498266Sopenharmony_ci                          CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
8413498266Sopenharmony_ci                          CURLFORM_FILENAME, "postit2.c",
8513498266Sopenharmony_ci                          CURLFORM_END);
8613498266Sopenharmony_ci  }
8713498266Sopenharmony_ci  else {
8813498266Sopenharmony_ci    /* new style */
8913498266Sopenharmony_ci    formrc = curl_formadd(&formpost,
9013498266Sopenharmony_ci                          &lastptr,
9113498266Sopenharmony_ci                          CURLFORM_COPYNAME, "sendfile alternative",
9213498266Sopenharmony_ci                          CURLFORM_STREAM, &pooh,
9313498266Sopenharmony_ci                          CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
9413498266Sopenharmony_ci                          CURLFORM_FILENAME, "file name 2",
9513498266Sopenharmony_ci                          CURLFORM_END);
9613498266Sopenharmony_ci  }
9713498266Sopenharmony_ci
9813498266Sopenharmony_ci  if(formrc)
9913498266Sopenharmony_ci    printf("curl_formadd(1) = %d\n", (int)formrc);
10013498266Sopenharmony_ci
10113498266Sopenharmony_ci  /* Now add the same data with another name and make it not look like
10213498266Sopenharmony_ci     a file upload but still using the callback */
10313498266Sopenharmony_ci
10413498266Sopenharmony_ci  pooh2.readptr = data;
10513498266Sopenharmony_ci  pooh2.sizeleft = strlen(data);
10613498266Sopenharmony_ci
10713498266Sopenharmony_ci  /* Fill in the file upload field */
10813498266Sopenharmony_ci  formrc = curl_formadd(&formpost,
10913498266Sopenharmony_ci                        &lastptr,
11013498266Sopenharmony_ci                        CURLFORM_COPYNAME, "callbackdata",
11113498266Sopenharmony_ci                        CURLFORM_STREAM, &pooh2,
11213498266Sopenharmony_ci                        CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
11313498266Sopenharmony_ci                        CURLFORM_END);
11413498266Sopenharmony_ci
11513498266Sopenharmony_ci  if(formrc)
11613498266Sopenharmony_ci    printf("curl_formadd(2) = %d\n", (int)formrc);
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci  /* Fill in the filename field */
11913498266Sopenharmony_ci  formrc = curl_formadd(&formpost,
12013498266Sopenharmony_ci                        &lastptr,
12113498266Sopenharmony_ci                        CURLFORM_COPYNAME, "filename",
12213498266Sopenharmony_ci                        CURLFORM_COPYCONTENTS, "postit2.c",
12313498266Sopenharmony_ci                        CURLFORM_END);
12413498266Sopenharmony_ci
12513498266Sopenharmony_ci  if(formrc)
12613498266Sopenharmony_ci    printf("curl_formadd(3) = %d\n", (int)formrc);
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci  /* Fill in a submit field too */
12913498266Sopenharmony_ci  formrc = curl_formadd(&formpost,
13013498266Sopenharmony_ci                        &lastptr,
13113498266Sopenharmony_ci                        CURLFORM_COPYNAME, "submit",
13213498266Sopenharmony_ci                        CURLFORM_COPYCONTENTS, "send",
13313498266Sopenharmony_ci                        CURLFORM_CONTENTTYPE, "text/plain",
13413498266Sopenharmony_ci                        CURLFORM_END);
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci  if(formrc)
13713498266Sopenharmony_ci    printf("curl_formadd(4) = %d\n", (int)formrc);
13813498266Sopenharmony_ci
13913498266Sopenharmony_ci  formrc = curl_formadd(&formpost, &lastptr,
14013498266Sopenharmony_ci                        CURLFORM_COPYNAME, "somename",
14113498266Sopenharmony_ci                        CURLFORM_BUFFER, "somefile.txt",
14213498266Sopenharmony_ci                        CURLFORM_BUFFERPTR, "blah blah",
14313498266Sopenharmony_ci                        CURLFORM_BUFFERLENGTH, (long)9,
14413498266Sopenharmony_ci                        CURLFORM_END);
14513498266Sopenharmony_ci
14613498266Sopenharmony_ci  if(formrc)
14713498266Sopenharmony_ci    printf("curl_formadd(5) = %d\n", (int)formrc);
14813498266Sopenharmony_ci
14913498266Sopenharmony_ci  curl = curl_easy_init();
15013498266Sopenharmony_ci  if(!curl) {
15113498266Sopenharmony_ci    fprintf(stderr, "curl_easy_init() failed\n");
15213498266Sopenharmony_ci    curl_formfree(formpost);
15313498266Sopenharmony_ci    curl_global_cleanup();
15413498266Sopenharmony_ci    return TEST_ERR_MAJOR_BAD;
15513498266Sopenharmony_ci  }
15613498266Sopenharmony_ci
15713498266Sopenharmony_ci  /* First set the URL that is about to receive our POST. */
15813498266Sopenharmony_ci  test_setopt(curl, CURLOPT_URL, URL);
15913498266Sopenharmony_ci
16013498266Sopenharmony_ci  /* Now specify we want to POST data */
16113498266Sopenharmony_ci  test_setopt(curl, CURLOPT_POST, 1L);
16213498266Sopenharmony_ci
16313498266Sopenharmony_ci  /* Set the expected POST size */
16413498266Sopenharmony_ci  test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
16513498266Sopenharmony_ci
16613498266Sopenharmony_ci  /* we want to use our own read function */
16713498266Sopenharmony_ci  test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
16813498266Sopenharmony_ci
16913498266Sopenharmony_ci  /* send a multi-part formpost */
17013498266Sopenharmony_ci  test_setopt(curl, CURLOPT_HTTPPOST, formpost);
17113498266Sopenharmony_ci
17213498266Sopenharmony_ci  /* get verbose debug output please */
17313498266Sopenharmony_ci  test_setopt(curl, CURLOPT_VERBOSE, 1L);
17413498266Sopenharmony_ci
17513498266Sopenharmony_ci  /* include headers in the output */
17613498266Sopenharmony_ci  test_setopt(curl, CURLOPT_HEADER, 1L);
17713498266Sopenharmony_ci
17813498266Sopenharmony_ci  /* Perform the request, res will get the return code */
17913498266Sopenharmony_ci  res = curl_easy_perform(curl);
18013498266Sopenharmony_ci
18113498266Sopenharmony_citest_cleanup:
18213498266Sopenharmony_ci
18313498266Sopenharmony_ci  /* always cleanup */
18413498266Sopenharmony_ci  curl_easy_cleanup(curl);
18513498266Sopenharmony_ci
18613498266Sopenharmony_ci  /* now cleanup the formpost chain */
18713498266Sopenharmony_ci  curl_formfree(formpost);
18813498266Sopenharmony_ci
18913498266Sopenharmony_ci  return res;
19013498266Sopenharmony_ci}
19113498266Sopenharmony_ci
19213498266Sopenharmony_ciint test(char *URL)
19313498266Sopenharmony_ci{
19413498266Sopenharmony_ci  int res;
19513498266Sopenharmony_ci
19613498266Sopenharmony_ci  if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
19713498266Sopenharmony_ci    fprintf(stderr, "curl_global_init() failed\n");
19813498266Sopenharmony_ci    return TEST_ERR_MAJOR_BAD;
19913498266Sopenharmony_ci  }
20013498266Sopenharmony_ci
20113498266Sopenharmony_ci  res = once(URL, TRUE); /* old */
20213498266Sopenharmony_ci  if(!res)
20313498266Sopenharmony_ci    res = once(URL, FALSE); /* new */
20413498266Sopenharmony_ci
20513498266Sopenharmony_ci  curl_global_cleanup();
20613498266Sopenharmony_ci
20713498266Sopenharmony_ci  return res;
20813498266Sopenharmony_ci}
209