lib552.c 5.9 KB

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