lib556.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include "test.h"
  23. #include "memdebug.h"
  24. /* For Windows, mainly (may be moved in a config file?) */
  25. #ifndef STDIN_FILENO
  26. #define STDIN_FILENO 0
  27. #endif
  28. #ifndef STDOUT_FILENO
  29. #define STDOUT_FILENO 1
  30. #endif
  31. #ifndef STDERR_FILENO
  32. #define STDERR_FILENO 2
  33. #endif
  34. int test(char *URL)
  35. {
  36. CURLcode res;
  37. CURL *curl;
  38. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  39. fprintf(stderr, "curl_global_init() failed\n");
  40. return TEST_ERR_MAJOR_BAD;
  41. }
  42. if ((curl = curl_easy_init()) == NULL) {
  43. fprintf(stderr, "curl_easy_init() failed\n");
  44. curl_global_cleanup();
  45. return TEST_ERR_MAJOR_BAD;
  46. }
  47. test_setopt(curl, CURLOPT_URL, URL);
  48. test_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  49. res = curl_easy_perform(curl);
  50. if(!res) {
  51. /* we are connected, now get a HTTP document the raw way */
  52. const char *request =
  53. #ifdef CURL_DOES_CONVERSIONS
  54. /* ASCII representation with escape sequences for non-ASCII platforms */
  55. "\x47\x45\x54\x20\x2f\x35\x35\x36\x20\x48\x54\x54\x50\x2f\x31\x2e"
  56. "\x32\x0d\x0a\x48\x6f\x73\x74\x3a\x20\x6e\x69\x6e\x6a\x61\x0d\x0a"
  57. "\x0d\x0a";
  58. #else
  59. "GET /556 HTTP/1.2\r\n"
  60. "Host: ninja\r\n\r\n";
  61. #endif
  62. size_t iolen;
  63. char buf[1024];
  64. res = curl_easy_send(curl, request, strlen(request), &iolen);
  65. if(!res) {
  66. /* we assume that sending always work */
  67. size_t total=0;
  68. do {
  69. /* busy-read like crazy */
  70. res = curl_easy_recv(curl, buf, 1024, &iolen);
  71. #ifdef TPF
  72. sleep(1); /* avoid ctl-10 dump */
  73. #endif
  74. if(iolen) {
  75. /* send received stuff to stdout */
  76. if(!write(STDOUT_FILENO, buf, iolen))
  77. break;
  78. }
  79. total += iolen;
  80. } while(((res == CURLE_OK) || (res == CURLE_AGAIN)) && (total < 129));
  81. }
  82. }
  83. test_cleanup:
  84. curl_easy_cleanup(curl);
  85. curl_global_cleanup();
  86. return (int)res;
  87. }