tests.c 8.2 KB

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