tests.c 8.5 KB

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