lib552.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://curl.haxx.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, %d bytes (0x%x)\n", text, (int)size, (int)size);
  43. for(i=0; i<size; i+= width) {
  44. fprintf(stream, "%04x: ", (int)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 && ptr[i+c+1]==0x0A) {
  56. i+=(c+2-width);
  57. break;
  58. }
  59. fprintf(stream, "%c",
  60. (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
  61. /* check again for 0D0A, to avoid an extra \n if it's at width */
  62. if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
  63. i+=(c+3-width);
  64. break;
  65. }
  66. }
  67. fputc('\n', stream); /* newline */
  68. }
  69. fflush(stream);
  70. }
  71. static
  72. int my_trace(CURL *handle, curl_infotype type,
  73. char *data, size_t size,
  74. void *userp)
  75. {
  76. struct data *config = (struct data *)userp;
  77. const char *text;
  78. (void)handle; /* prevent compiler warning */
  79. switch (type) {
  80. case CURLINFO_TEXT:
  81. fprintf(stderr, "== Info: %s", (char *)data);
  82. default: /* in case a new one is introduced to shock us */
  83. return 0;
  84. case CURLINFO_HEADER_OUT:
  85. text = "=> Send header";
  86. break;
  87. case CURLINFO_DATA_OUT:
  88. text = "=> Send data";
  89. break;
  90. case CURLINFO_SSL_DATA_OUT:
  91. text = "=> Send SSL data";
  92. break;
  93. case CURLINFO_HEADER_IN:
  94. text = "<= Recv header";
  95. break;
  96. case CURLINFO_DATA_IN:
  97. text = "<= Recv data";
  98. break;
  99. case CURLINFO_SSL_DATA_IN:
  100. text = "<= Recv SSL data";
  101. break;
  102. }
  103. dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
  104. return 0;
  105. }
  106. static size_t current_offset = 0;
  107. static char databuf[70000]; /* MUST be more than 64k OR MAX_INITIAL_POST_SIZE */
  108. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
  109. {
  110. size_t amount = nmemb * size; /* Total bytes curl wants */
  111. size_t available = sizeof(databuf) - current_offset; /* What we have to give */
  112. size_t given = amount < available ? amount : available; /* What is given */
  113. (void)stream;
  114. memcpy(ptr, databuf + current_offset, given);
  115. current_offset += given;
  116. return given;
  117. }
  118. static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
  119. {
  120. int amount = curlx_uztosi(size * nmemb);
  121. printf("%.*s", amount, (char *)ptr);
  122. (void)stream;
  123. return size * nmemb;
  124. }
  125. static curlioerr ioctl_callback(CURL * handle, int cmd, void *clientp)
  126. {
  127. (void)clientp;
  128. if (cmd == CURLIOCMD_RESTARTREAD ) {
  129. printf("APPLICATION: recieved a CURLIOCMD_RESTARTREAD request\n");
  130. printf("APPLICATION: ** REWINDING! **\n");
  131. current_offset = 0;
  132. return CURLIOE_OK;
  133. }
  134. (void)handle;
  135. return CURLIOE_UNKNOWNCMD;
  136. }
  137. int test(char *URL)
  138. {
  139. CURL *curl;
  140. CURLcode res = CURLE_OUT_OF_MEMORY;
  141. struct data config;
  142. size_t i;
  143. static const char fill[] = "test data";
  144. config.trace_ascii = 1; /* enable ascii tracing */
  145. if((curl = curl_easy_init()) == NULL) {
  146. fprintf(stderr, "curl_easy_init() failed\n");
  147. curl_global_cleanup();
  148. return TEST_ERR_MAJOR_BAD;
  149. }
  150. test_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
  151. test_setopt(curl, CURLOPT_DEBUGDATA, &config);
  152. /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
  153. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  154. /* setup repeated data string */
  155. for (i=0; i < sizeof(databuf); ++i)
  156. databuf[i] = fill[i % sizeof fill];
  157. /* Post */
  158. test_setopt(curl, CURLOPT_POST, 1L);
  159. #ifdef CURL_DOES_CONVERSIONS
  160. /* Convert the POST data to ASCII */
  161. test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
  162. #endif
  163. /* Setup read callback */
  164. test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) sizeof(databuf));
  165. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  166. /* Write callback */
  167. test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
  168. /* Ioctl function */
  169. test_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_callback);
  170. test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
  171. test_setopt(curl, CURLOPT_URL, URL);
  172. /* Accept any auth. But for this bug configure proxy with DIGEST, basic 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. }