lib552.c 5.0 KB

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