lib552.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. /* argv1 = URL
  25. * argv2 = proxy with embedded user+password
  26. */
  27. #include "test.h"
  28. #include "warnless.h"
  29. #include "memdebug.h"
  30. struct data {
  31. char trace_ascii; /* 1 or 0 */
  32. };
  33. static
  34. void dump(const char *text,
  35. FILE *stream, unsigned char *ptr, size_t size,
  36. char nohex)
  37. {
  38. size_t i;
  39. size_t c;
  40. unsigned int width = 0x10;
  41. if(nohex)
  42. /* without the hex output, we can fit more on screen */
  43. width = 0x40;
  44. fprintf(stream, "%s, %zu bytes (0x%zx)\n", text, size, size);
  45. for(i = 0; i<size; i += width) {
  46. fprintf(stream, "%04zx: ", i);
  47. if(!nohex) {
  48. /* hex not disabled, show it */
  49. for(c = 0; c < width; c++)
  50. if(i + c < size)
  51. fprintf(stream, "%02x ", ptr[i + c]);
  52. else
  53. fputs(" ", stream);
  54. }
  55. for(c = 0; (c < width) && (i + c < size); c++) {
  56. /* check for 0D0A; if found, skip past and start a new line of output */
  57. if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
  58. ptr[i + c + 1] == 0x0A) {
  59. i += (c + 2 - width);
  60. break;
  61. }
  62. fprintf(stream, "%c",
  63. (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)? ptr[i + c] : '.');
  64. /* check again for 0D0A, to avoid an extra \n if it's at width */
  65. if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
  66. ptr[i + c + 2] == 0x0A) {
  67. i += (c + 3 - width);
  68. break;
  69. }
  70. }
  71. fputc('\n', stream); /* newline */
  72. }
  73. fflush(stream);
  74. }
  75. static
  76. int my_trace(CURL *handle, curl_infotype type,
  77. char *data, size_t size,
  78. void *userp)
  79. {
  80. struct data *config = (struct data *)userp;
  81. const char *text;
  82. (void)handle; /* prevent compiler warning */
  83. switch(type) {
  84. case CURLINFO_TEXT:
  85. fprintf(stderr, "== Info: %s", (char *)data);
  86. /* FALLTHROUGH */
  87. default: /* in case a new one is introduced to shock us */
  88. return 0;
  89. case CURLINFO_HEADER_OUT:
  90. text = "=> Send header";
  91. break;
  92. case CURLINFO_DATA_OUT:
  93. text = "=> Send data";
  94. break;
  95. case CURLINFO_SSL_DATA_OUT:
  96. text = "=> Send SSL data";
  97. break;
  98. case CURLINFO_HEADER_IN:
  99. text = "<= Recv header";
  100. break;
  101. case CURLINFO_DATA_IN:
  102. text = "<= Recv data";
  103. break;
  104. case CURLINFO_SSL_DATA_IN:
  105. text = "<= Recv SSL data";
  106. break;
  107. }
  108. dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
  109. return 0;
  110. }
  111. static size_t current_offset = 0;
  112. static char databuf[70000]; /* MUST be more than 64k OR
  113. MAX_INITIAL_POST_SIZE */
  114. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
  115. {
  116. size_t amount = nmemb * size; /* Total bytes curl wants */
  117. size_t available = sizeof(databuf) - current_offset; /* What we have to
  118. give */
  119. size_t given = amount < available ? amount : available; /* What is given */
  120. (void)stream;
  121. memcpy(ptr, databuf + current_offset, given);
  122. current_offset += given;
  123. return given;
  124. }
  125. static size_t write_callback(void *ptr, size_t size, size_t nmemb,
  126. void *stream)
  127. {
  128. int amount = curlx_uztosi(size * nmemb);
  129. printf("%.*s", amount, (char *)ptr);
  130. (void)stream;
  131. return size * nmemb;
  132. }
  133. static curlioerr ioctl_callback(CURL *handle, int cmd, void *clientp)
  134. {
  135. (void)clientp;
  136. if(cmd == CURLIOCMD_RESTARTREAD) {
  137. printf("APPLICATION received a CURLIOCMD_RESTARTREAD request\n");
  138. printf("APPLICATION ** REWINDING! **\n");
  139. current_offset = 0;
  140. return CURLIOE_OK;
  141. }
  142. (void)handle;
  143. return CURLIOE_UNKNOWNCMD;
  144. }
  145. int test(char *URL)
  146. {
  147. CURL *curl;
  148. CURLcode res = CURLE_OK;
  149. struct data config;
  150. size_t i;
  151. static const char fill[] = "test data";
  152. config.trace_ascii = 1; /* enable ascii tracing */
  153. global_init(CURL_GLOBAL_ALL);
  154. easy_init(curl);
  155. test_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
  156. test_setopt(curl, CURLOPT_DEBUGDATA, &config);
  157. /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
  158. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  159. /* setup repeated data string */
  160. for(i = 0; i < sizeof(databuf); ++i)
  161. databuf[i] = fill[i % sizeof(fill)];
  162. /* Post */
  163. test_setopt(curl, CURLOPT_POST, 1L);
  164. /* Setup read callback */
  165. test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) sizeof(databuf));
  166. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  167. /* Write callback */
  168. test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
  169. /* Ioctl function */
  170. test_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_callback);
  171. test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
  172. test_setopt(curl, CURLOPT_URL, URL);
  173. /* Accept any auth. But for this bug configure proxy with DIGEST, basic
  174. might work too, not NTLM */
  175. test_setopt(curl, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
  176. res = curl_easy_perform(curl);
  177. fprintf(stderr, "curl_easy_perform = %d\n", (int)res);
  178. test_cleanup:
  179. curl_easy_cleanup(curl);
  180. curl_global_cleanup();
  181. return (int)res;
  182. }