webclient_shard_download_sample.c 3.4 KB

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