wn_sample.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * File : wn_sample.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This software is dual-licensed: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation. For the terms of this
  9. * license, see <http://www.gnu.org/licenses/>.
  10. *
  11. * You are free to use this software under the terms of the GNU General
  12. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  13. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * Alternatively for commercial application, you can contact us
  17. * by email <business@rt-thread.com> for commercial license.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2018-10-26 ChenYong First version
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <webnet.h>
  27. #include <wn_module.h>
  28. static void asp_var_version(struct webnet_session* session)
  29. {
  30. RT_ASSERT(session != RT_NULL);
  31. static const char *version = "<html><body><font size=\"+2\">RT-Thread %d.%d.%d</font><br><br>"
  32. "<a href=\"javascript:history.go(-1);\">Go back to root</a></html></body>";
  33. webnet_session_printf(session, version, RT_VERSION, RT_SUBVERSION, RT_REVISION);
  34. }
  35. static void cgi_calc_handler(struct webnet_session* session)
  36. {
  37. int a, b;
  38. const char* mimetype;
  39. struct webnet_request* request;
  40. static const char* header = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; "
  41. "charset=gb2312\" /><title> calc </title></head>";
  42. static const char* body = "<body><form method=\"post\" action=\"/cgi-bin/calc\"><input type=\"text\" name=\"a\" value=\"%d\"> "
  43. "+ <input type=\"text\" name=\"b\" value=\"%d\"> = %d<br><input type=\"submit\" value=\"\xBC\xC6\xCB\xE3\"></form>"
  44. "<br><a href=\"/index.html\">Go back to root</a></body></html>\r\n";
  45. RT_ASSERT(session != RT_NULL);
  46. request = session->request;
  47. RT_ASSERT(request != RT_NULL);
  48. /* get mimetype */
  49. mimetype = mime_get_type(".html");
  50. a = 1;
  51. b = 1;
  52. /* set http header */
  53. session->request->result_code = 200;
  54. webnet_session_set_header(session, mimetype, 200, "Ok", -1);
  55. webnet_session_write(session, (const rt_uint8_t*)header, rt_strlen(header));
  56. if (request->query_counter)
  57. {
  58. const char *a_value, *b_value;
  59. a_value = webnet_request_get_query(request, "a");
  60. b_value = webnet_request_get_query(request, "b");
  61. a = atoi(a_value);
  62. b = atoi(b_value);
  63. }
  64. webnet_session_printf(session, body, a, b, a + b);
  65. }
  66. static void cgi_hello_handler(struct webnet_session* session)
  67. {
  68. const char* mimetype;
  69. static const char* status = "<html><head><title> hello </title>"
  70. "</head><body><font size=\"+2\">hello world</font><br><br>"
  71. "<a href=\"javascript:history.go(-1);\">Go back to root</a></body></html>\r\n";
  72. RT_ASSERT(session != RT_NULL);
  73. /* get mimetype */
  74. mimetype = mime_get_type(".html");
  75. /* set http header */
  76. session->request->result_code = 200;
  77. webnet_session_set_header(session, mimetype, 200, "Ok", strlen(status));
  78. webnet_session_write(session, (const rt_uint8_t*)status, rt_strlen(status));
  79. }
  80. void webnet_test(void)
  81. {
  82. #ifdef WEBNET_USING_CGI
  83. webnet_cgi_register("hello", cgi_hello_handler);
  84. webnet_cgi_register("calc", cgi_calc_handler);
  85. #endif
  86. #ifdef WEBNET_USING_ASP
  87. webnet_asp_add_var("version", asp_var_version);
  88. #endif
  89. #ifdef WEBNET_USING_ALIAS
  90. webnet_alias_set("/test", "/admin");
  91. #endif
  92. #ifdef WEBNET_USING_AUTH
  93. webnet_auth_set("/admin", "admin:admin");
  94. #endif
  95. #ifdef WEBNET_USING_UPLOAD
  96. extern const struct webnet_module_upload_entry upload_entry_upload;
  97. webnet_upload_add(&upload_entry_upload);
  98. #endif
  99. webnet_init();
  100. }
  101. #ifdef FINSH_USING_MSH
  102. MSH_CMD_EXPORT(webnet_test, wenbet test);
  103. #endif