tests.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include <stdlib.h>
  2. #include <stdbool.h>
  3. #include <esp_log.h>
  4. #include <esp_system.h>
  5. #include <http_server.h>
  6. #include "tests.h"
  7. static const char *TAG="TESTS";
  8. int pre_start_mem, post_stop_mem, post_stop_min_mem;
  9. bool basic_sanity = true;
  10. /********************* Basic Handlers Start *******************/
  11. esp_err_t hello_get_handler(httpd_req_t *req)
  12. {
  13. #define STR "Hello World!"
  14. ESP_LOGI(TAG, "Free Stack for server task: '%d'", uxTaskGetStackHighWaterMark(NULL));
  15. httpd_resp_send(req, STR, strlen(STR));
  16. return ESP_OK;
  17. #undef STR
  18. }
  19. esp_err_t hello_type_get_handler(httpd_req_t *req)
  20. {
  21. #define STR "Hello World!"
  22. httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
  23. httpd_resp_send(req, STR, strlen(STR));
  24. return ESP_OK;
  25. #undef STR
  26. }
  27. esp_err_t hello_status_get_handler(httpd_req_t *req)
  28. {
  29. #define STR "Hello World!"
  30. httpd_resp_set_status(req, HTTPD_500);
  31. httpd_resp_send(req, STR, strlen(STR));
  32. return ESP_OK;
  33. #undef STR
  34. }
  35. esp_err_t echo_post_handler(httpd_req_t *req)
  36. {
  37. ESP_LOGI(TAG, "/echo handler read content length %d", req->content_len);
  38. char* buf = malloc(req->content_len + 1);
  39. size_t off = 0;
  40. int ret;
  41. if (!buf) {
  42. return ESP_FAIL;
  43. }
  44. while (off < req->content_len) {
  45. /* Read data received in the request */
  46. ret = httpd_req_recv(req, buf + off, req->content_len - off);
  47. if (ret < 0) {
  48. free (buf);
  49. return ESP_FAIL;
  50. }
  51. off += ret;
  52. ESP_LOGI(TAG, "/echo handler recv length %d", ret);
  53. }
  54. buf[off] = '\0';
  55. if (req->content_len < 128) {
  56. ESP_LOGI(TAG, "/echo handler read %s", buf);
  57. }
  58. /* Search for Custom header field */
  59. char* req_hdr = 0;
  60. size_t hdr_len = httpd_req_get_hdr_value_len(req, "Custom");
  61. if (hdr_len) {
  62. /* Read Custom header value */
  63. req_hdr = malloc(hdr_len + 1);
  64. if (req_hdr) {
  65. httpd_req_get_hdr_value_str(req, "Custom", req_hdr, hdr_len + 1);
  66. /* Set as additional header for response packet */
  67. httpd_resp_set_hdr(req, "Custom", req_hdr);
  68. }
  69. }
  70. httpd_resp_send(req, buf, req->content_len);
  71. free (req_hdr);
  72. free (buf);
  73. return ESP_OK;
  74. }
  75. void adder_free_func(void *ctx)
  76. {
  77. ESP_LOGI(TAG, "Custom Free Context function called");
  78. free(ctx);
  79. }
  80. /* Create a context, keep incrementing value in the context, by whatever was
  81. * received. Return the result
  82. */
  83. esp_err_t adder_post_handler(httpd_req_t *req)
  84. {
  85. char buf[10];
  86. char outbuf[50];
  87. int ret;
  88. /* Read data received in the request */
  89. ret = httpd_req_recv(req, buf, sizeof(buf));
  90. if (ret < 0) {
  91. return ESP_FAIL;
  92. }
  93. buf[ret] = '\0';
  94. int val = atoi(buf);
  95. ESP_LOGI(TAG, "/adder handler read %d", val);
  96. if (! req->sess_ctx) {
  97. ESP_LOGI(TAG, "/adder allocating new session");
  98. req->sess_ctx = malloc(sizeof(int));
  99. req->free_ctx = adder_free_func;
  100. *(int *)req->sess_ctx = 0;
  101. }
  102. int *adder = (int *)req->sess_ctx;
  103. *adder += val;
  104. snprintf(outbuf, sizeof(outbuf),"%d", *adder);
  105. httpd_resp_send(req, outbuf, strlen(outbuf));
  106. return ESP_OK;
  107. }
  108. esp_err_t leftover_data_post_handler(httpd_req_t *req)
  109. {
  110. /* Only echo the first 10 bytes of the request, leaving the rest of the
  111. * request data as is.
  112. */
  113. char buf[11];
  114. int ret;
  115. /* Read data received in the request */
  116. ret = httpd_req_recv(req, buf, sizeof(buf) - 1);
  117. if (ret < 0) {
  118. return ESP_FAIL;
  119. }
  120. buf[ret] = '\0';
  121. ESP_LOGI(TAG, "leftover data handler read %s", buf);
  122. httpd_resp_send(req, buf, strlen(buf));
  123. return ESP_OK;
  124. }
  125. int httpd_default_send(int sockfd, const char *buf, unsigned buf_len, int flags);
  126. void generate_async_resp(void *arg)
  127. {
  128. char buf[250];
  129. int fd = (int )arg;
  130. #define HTTPD_HDR_STR "HTTP/1.1 200 OK\r\n" \
  131. "Content-Type: text/html\r\n" \
  132. "Content-Length: %d\r\n"
  133. #define STR "Hello Double World!"
  134. ESP_LOGI(TAG, "Executing queued work fd : %d", fd);
  135. snprintf(buf, sizeof(buf), HTTPD_HDR_STR,
  136. strlen(STR));
  137. httpd_default_send(fd, buf, strlen(buf), 0);
  138. /* Space for sending additional headers based on set_header */
  139. httpd_default_send(fd, "\r\n", strlen("\r\n"), 0);
  140. httpd_default_send(fd, STR, strlen(STR), 0);
  141. #undef STR
  142. }
  143. esp_err_t async_get_handler(httpd_req_t *req)
  144. {
  145. #define STR "Hello World!"
  146. httpd_resp_send(req, STR, strlen(STR));
  147. /* Also register a HTTPD Work which sends the same data on the same
  148. * socket again
  149. */
  150. int fd = httpd_req_to_sockfd(req);
  151. if (fd < 0) {
  152. return ESP_FAIL;
  153. }
  154. ESP_LOGI(TAG, "Queuing work fd : %d", fd);
  155. httpd_queue_work(req->handle, generate_async_resp, (void *)fd);
  156. return ESP_OK;
  157. #undef STR
  158. }
  159. httpd_uri_t basic_handlers[] = {
  160. { .uri = "/hello/type_html",
  161. .method = HTTP_GET,
  162. .handler = hello_type_get_handler,
  163. .user_ctx = NULL,
  164. },
  165. { .uri = "/hello",
  166. .method = HTTP_GET,
  167. .handler = hello_get_handler,
  168. .user_ctx = NULL,
  169. },
  170. { .uri = "/hello/status_500",
  171. .method = HTTP_GET,
  172. .handler = hello_status_get_handler,
  173. .user_ctx = NULL,
  174. },
  175. { .uri = "/echo",
  176. .method = HTTP_POST,
  177. .handler = echo_post_handler,
  178. .user_ctx = NULL,
  179. },
  180. { .uri = "/echo",
  181. .method = HTTP_PUT,
  182. .handler = echo_post_handler,
  183. .user_ctx = NULL,
  184. },
  185. { .uri = "/leftover_data",
  186. .method = HTTP_POST,
  187. .handler = leftover_data_post_handler,
  188. .user_ctx = NULL,
  189. },
  190. { .uri = "/adder",
  191. .method = HTTP_POST,
  192. .handler = adder_post_handler,
  193. .user_ctx = NULL,
  194. },
  195. { .uri = "/async_data",
  196. .method = HTTP_GET,
  197. .handler = async_get_handler,
  198. .user_ctx = NULL,
  199. }
  200. };
  201. int basic_handlers_no = sizeof(basic_handlers)/sizeof(httpd_uri_t);
  202. void register_basic_handlers(httpd_handle_t hd)
  203. {
  204. int i;
  205. ESP_LOGI(TAG, "Registering basic handlers");
  206. ESP_LOGI(TAG, "No of handlers = %d", basic_handlers_no);
  207. for (i = 0; i < basic_handlers_no; i++) {
  208. if (httpd_register_uri_handler(hd, &basic_handlers[i]) != ESP_OK) {
  209. ESP_LOGW(TAG, "register uri failed for %d", i);
  210. return;
  211. }
  212. }
  213. ESP_LOGI(TAG, "Success");
  214. }
  215. httpd_handle_t test_httpd_start()
  216. {
  217. pre_start_mem = esp_get_free_heap_size();
  218. httpd_handle_t hd;
  219. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  220. config.server_port = 1234;
  221. /* This check should be a part of http_server */
  222. config.max_open_sockets = (CONFIG_LWIP_MAX_SOCKETS - 3);
  223. if (httpd_start(&hd, &config) == ESP_OK) {
  224. ESP_LOGI(TAG, "Started HTTP server on port: '%d'", config.server_port);
  225. ESP_LOGI(TAG, "Max URI handlers: '%d'", config.max_uri_handlers);
  226. ESP_LOGI(TAG, "Max Open Sessions: '%d'", config.max_open_sockets);
  227. ESP_LOGI(TAG, "Max Header Length: '%d'", HTTPD_MAX_REQ_HDR_LEN);
  228. ESP_LOGI(TAG, "Max URI Length: '%d'", HTTPD_MAX_URI_LEN);
  229. ESP_LOGI(TAG, "Max Stack Size: '%d'", config.stack_size);
  230. return hd;
  231. }
  232. return NULL;
  233. }
  234. void test_httpd_stop(httpd_handle_t hd)
  235. {
  236. httpd_stop(hd);
  237. post_stop_mem = esp_get_free_heap_size();
  238. ESP_LOGI(TAG, "HTTPD Stop: Current free memory: %d", post_stop_mem);
  239. }
  240. httpd_handle_t start_tests()
  241. {
  242. httpd_handle_t hd = test_httpd_start();
  243. if (hd) {
  244. register_basic_handlers(hd);
  245. }
  246. return hd;
  247. }
  248. void stop_tests(httpd_handle_t hd)
  249. {
  250. ESP_LOGI(TAG, "Stopping httpd");
  251. test_httpd_stop(hd);
  252. }