lib2302.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. #ifdef USE_WEBSOCKETS
  26. struct ws_data {
  27. CURL *easy;
  28. char buf[1024*1024];
  29. size_t blen;
  30. size_t nwrites;
  31. int has_meta;
  32. int meta_flags;
  33. };
  34. static void flush_data(struct ws_data *wd)
  35. {
  36. size_t i;
  37. if(!wd->nwrites)
  38. return;
  39. for(i = 0; i < wd->blen; ++i)
  40. printf("%02x ", (unsigned char)wd->buf[i]);
  41. printf("\n");
  42. if(wd->has_meta)
  43. printf("RECFLAGS: %x\n", wd->meta_flags);
  44. else
  45. fprintf(stderr, "RECFLAGS: NULL\n");
  46. wd->blen = 0;
  47. wd->nwrites = 0;
  48. }
  49. static size_t add_data(struct ws_data *wd, const char *buf, size_t blen,
  50. const struct curl_ws_frame *meta)
  51. {
  52. if((wd->nwrites == 0) ||
  53. (!!meta != !!wd->has_meta) ||
  54. (meta && meta->flags != wd->meta_flags)) {
  55. if(wd->nwrites > 0)
  56. flush_data(wd);
  57. wd->has_meta = (meta != NULL);
  58. wd->meta_flags = meta? meta->flags : 0;
  59. }
  60. if(wd->blen + blen > sizeof(wd->buf)) {
  61. return 0;
  62. }
  63. memcpy(wd->buf + wd->blen, buf, blen);
  64. wd->blen += blen;
  65. wd->nwrites++;
  66. return blen;
  67. }
  68. static size_t writecb(char *buffer, size_t size, size_t nitems, void *p)
  69. {
  70. struct ws_data *ws_data = p;
  71. size_t incoming = nitems;
  72. const struct curl_ws_frame *meta;
  73. (void)size;
  74. meta = curl_ws_meta(ws_data->easy);
  75. incoming = add_data(ws_data, buffer, incoming, meta);
  76. if(nitems != incoming)
  77. fprintf(stderr, "returns error from callback\n");
  78. return nitems;
  79. }
  80. CURLcode test(char *URL)
  81. {
  82. CURL *curl;
  83. CURLcode res = CURLE_OK;
  84. struct ws_data ws_data;
  85. global_init(CURL_GLOBAL_ALL);
  86. curl = curl_easy_init();
  87. if(curl) {
  88. memset(&ws_data, 0, sizeof(ws_data));
  89. ws_data.easy = curl;
  90. curl_easy_setopt(curl, CURLOPT_URL, URL);
  91. /* use the callback style */
  92. curl_easy_setopt(curl, CURLOPT_USERAGENT, "webbie-sox/3");
  93. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  94. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
  95. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ws_data);
  96. res = curl_easy_perform(curl);
  97. fprintf(stderr, "curl_easy_perform() returned %d\n", res);
  98. /* always cleanup */
  99. curl_easy_cleanup(curl);
  100. flush_data(&ws_data);
  101. }
  102. curl_global_cleanup();
  103. return res;
  104. }
  105. #else
  106. NO_SUPPORT_BUILT_IN
  107. #endif