tests.c 7.9 KB

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