lib552.c 5.8 KB

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