webclient_shard_download_sample.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-06-03 xiangxistu the first version
  9. */
  10. #include <rtthread.h>
  11. #include <webclient.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #define GET_LOCAL_URI "http://www.rt-thread.com/service/rt-thread.txt"
  15. #define CHARACTER_LENGTH 60
  16. /* handle function, you can store data and so on */
  17. static int shard_download_handle(char *buffer, int length)
  18. {
  19. int outindex, inindex = 0;
  20. int boundary;
  21. /* print the receive data */
  22. for (outindex = 0; outindex < length; outindex = outindex + inindex)
  23. {
  24. char print_buffer[CHARACTER_LENGTH + 1] = {0};
  25. char *point = RT_NULL;
  26. point = print_buffer;
  27. if(length - outindex > CHARACTER_LENGTH)
  28. {
  29. boundary = CHARACTER_LENGTH;
  30. }
  31. else
  32. {
  33. boundary = length - outindex;
  34. }
  35. for (inindex = 0; inindex < boundary; inindex++)
  36. {
  37. *point++ = buffer[outindex + inindex];
  38. }
  39. *point = 0;
  40. rt_kprintf("%04d - %04d: %s\n", outindex, outindex + boundary - 1, print_buffer);
  41. }
  42. /* release this buffer if we have handled data */
  43. web_free(buffer);
  44. return RT_EOK;
  45. }
  46. int webclient_shard_download_test(int argc, char **argv)
  47. {
  48. struct webclient_session* session = RT_NULL;
  49. rt_err_t result = RT_EOK;
  50. char *uri = RT_NULL;
  51. int length = 0;
  52. int usage_flag = 0;
  53. int size = 200;
  54. if (argc == 1)
  55. {
  56. uri = web_strdup(GET_LOCAL_URI);
  57. }
  58. else
  59. {
  60. int index;
  61. for(index = 1; index < argc; index = index + 2)
  62. {
  63. if(strstr(argv[index], "-u"))
  64. {
  65. uri = web_strdup(argv[index + 1]);
  66. }
  67. else if(strstr(argv[index], "-l"))
  68. {
  69. size = atoi(argv[index + 1]);
  70. }
  71. else
  72. {
  73. usage_flag = 1;
  74. break;
  75. }
  76. }
  77. }
  78. if(usage_flag)
  79. {
  80. rt_kprintf("web_shard_test -u [URI] - webclient HEAD and GET request test.\n");
  81. rt_kprintf("web_shard_test -l [SIZE] - the length of receive buffer.\n");
  82. return -RT_ERROR;
  83. }
  84. if(uri == RT_NULL)
  85. {
  86. uri = web_strdup(GET_LOCAL_URI);
  87. }
  88. /* sometime, the header bufsz can set more smaller */
  89. session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ / 4);
  90. if (session == RT_NULL)
  91. {
  92. result = -RT_ENOMEM;
  93. goto __exit;
  94. }
  95. /* get the real data length */
  96. webclient_shard_head_function(session, uri, &length);
  97. /* register the handle function, you can handle data in the function */
  98. webclient_register_shard_position_function(session, shard_download_handle);
  99. /* the "memory size" that you can provide in the project and uri */
  100. result = webclient_shard_position_function(session, uri, 0, length, size);
  101. if(result != WEBCLIENT_OK)
  102. {
  103. rt_kprintf("web shard download, test failed!\n");
  104. }
  105. /* clear the handle function */
  106. webclient_register_shard_position_function(session, RT_NULL);
  107. __exit:
  108. if (uri)
  109. {
  110. web_free(uri);
  111. }
  112. if (session)
  113. {
  114. webclient_close(session);
  115. session = RT_NULL;
  116. }
  117. return result;
  118. }
  119. #ifdef FINSH_USING_MSH
  120. #include <finsh.h>
  121. MSH_CMD_EXPORT_ALIAS(webclient_shard_download_test, web_shard_test, webclient head and get request test);
  122. #endif /* FINSH_USING_MSH */