tests.c 8.4 KB

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