tests.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. /* This handler is intended to check what happens in case of empty values of headers.
  24. * Here `Header2` is an empty header and `Header1` and `Header3` will have `Value1`
  25. * and `Value3` in them. */
  26. static esp_err_t test_header_get_handler(httpd_req_t *req)
  27. {
  28. httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
  29. int buf_len;
  30. char *buf;
  31. buf_len = httpd_req_get_hdr_value_len(req, "Header1");
  32. if (buf_len > 0) {
  33. buf = malloc(++buf_len);
  34. if (!buf) {
  35. ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", buf_len);
  36. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Memory allocation failed");
  37. return ESP_ERR_NO_MEM;
  38. }
  39. /* Copy null terminated value string into buffer */
  40. if (httpd_req_get_hdr_value_str(req, "Header1", buf, buf_len) == ESP_OK) {
  41. ESP_LOGI(TAG, "Header1 content: %s", buf);
  42. if (strcmp("Value1", buf) != 0) {
  43. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Wrong value of Header1 received");
  44. free(buf);
  45. return ESP_ERR_INVALID_ARG;
  46. } else {
  47. ESP_LOGI(TAG, "Expected value and received value matched for Header1");
  48. }
  49. } else {
  50. ESP_LOGE(TAG, "Error in getting value of Header1");
  51. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Error in getting value of Header1");
  52. free(buf);
  53. return ESP_FAIL;
  54. }
  55. free(buf);
  56. } else {
  57. ESP_LOGE(TAG, "Header1 not found");
  58. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Header1 not found");
  59. return ESP_ERR_NOT_FOUND;
  60. }
  61. buf_len = httpd_req_get_hdr_value_len(req, "Header3");
  62. if (buf_len > 0) {
  63. buf = malloc(++buf_len);
  64. if (!buf) {
  65. ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", buf_len);
  66. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Memory allocation failed");
  67. return ESP_ERR_NO_MEM;
  68. }
  69. /* Copy null terminated value string into buffer */
  70. if (httpd_req_get_hdr_value_str(req, "Header3", buf, buf_len) == ESP_OK) {
  71. ESP_LOGI(TAG, "Header3 content: %s", buf);
  72. if (strcmp("Value3", buf) != 0) {
  73. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Wrong value of Header3 received");
  74. free(buf);
  75. return ESP_ERR_INVALID_ARG;
  76. } else {
  77. ESP_LOGI(TAG, "Expected value and received value matched for Header3");
  78. }
  79. } else {
  80. ESP_LOGE(TAG, "Error in getting value of Header3");
  81. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Error in getting value of Header3");
  82. free(buf);
  83. return ESP_FAIL;
  84. }
  85. free(buf);
  86. } else {
  87. ESP_LOGE(TAG, "Header3 not found");
  88. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Header3 not found");
  89. return ESP_ERR_NOT_FOUND;
  90. }
  91. buf_len = httpd_req_get_hdr_value_len(req, "Header2");
  92. buf = malloc(++buf_len);
  93. if (!buf) {
  94. ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", buf_len);
  95. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Memory allocation failed");
  96. return ESP_ERR_NO_MEM;
  97. }
  98. if (httpd_req_get_hdr_value_str(req, "Header2", buf, buf_len) == ESP_OK) {
  99. ESP_LOGI(TAG, "Header2 content: %s", buf);
  100. httpd_resp_send(req, buf, strlen(buf));
  101. } else {
  102. ESP_LOGE(TAG, "Header2 not found");
  103. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Header2 not found");
  104. return ESP_FAIL;
  105. }
  106. return ESP_OK;
  107. }
  108. static esp_err_t hello_type_get_handler(httpd_req_t *req)
  109. {
  110. #define STR "Hello World!"
  111. httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
  112. httpd_resp_send(req, STR, strlen(STR));
  113. return ESP_OK;
  114. #undef STR
  115. }
  116. esp_err_t hello_status_get_handler(httpd_req_t *req)
  117. {
  118. #define STR "Hello World!"
  119. httpd_resp_set_status(req, HTTPD_500);
  120. httpd_resp_send(req, STR, strlen(STR));
  121. return ESP_OK;
  122. #undef STR
  123. }
  124. esp_err_t echo_post_handler(httpd_req_t *req)
  125. {
  126. ESP_LOGI(TAG, "/echo handler read content length %d", req->content_len);
  127. char* buf = malloc(req->content_len + 1);
  128. size_t off = 0;
  129. int ret;
  130. if (!buf) {
  131. ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", req->content_len + 1);
  132. httpd_resp_send_500(req);
  133. return ESP_FAIL;
  134. }
  135. while (off < req->content_len) {
  136. /* Read data received in the request */
  137. ret = httpd_req_recv(req, buf + off, req->content_len - off);
  138. if (ret <= 0) {
  139. if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
  140. httpd_resp_send_408(req);
  141. }
  142. free (buf);
  143. return ESP_FAIL;
  144. }
  145. off += ret;
  146. ESP_LOGI(TAG, "/echo handler recv length %d", ret);
  147. }
  148. buf[off] = '\0';
  149. if (req->content_len < 128) {
  150. ESP_LOGI(TAG, "/echo handler read %s", buf);
  151. }
  152. /* Search for Custom header field */
  153. char* req_hdr = 0;
  154. size_t hdr_len = httpd_req_get_hdr_value_len(req, "Custom");
  155. if (hdr_len) {
  156. /* Read Custom header value */
  157. req_hdr = malloc(hdr_len + 1);
  158. if (!req_hdr) {
  159. ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", hdr_len + 1);
  160. httpd_resp_send_500(req);
  161. return ESP_FAIL;
  162. }
  163. httpd_req_get_hdr_value_str(req, "Custom", req_hdr, hdr_len + 1);
  164. /* Set as additional header for response packet */
  165. httpd_resp_set_hdr(req, "Custom", req_hdr);
  166. }
  167. httpd_resp_send(req, buf, req->content_len);
  168. free (req_hdr);
  169. free (buf);
  170. return ESP_OK;
  171. }
  172. void adder_free_func(void *ctx)
  173. {
  174. ESP_LOGI(TAG, "Custom Free Context function called");
  175. free(ctx);
  176. }
  177. /* Create a context, keep incrementing value in the context, by whatever was
  178. * received. Return the result
  179. */
  180. esp_err_t adder_post_handler(httpd_req_t *req)
  181. {
  182. char buf[10];
  183. char outbuf[50];
  184. int ret;
  185. /* Read data received in the request */
  186. ret = httpd_req_recv(req, buf, sizeof(buf));
  187. if (ret <= 0) {
  188. if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
  189. httpd_resp_send_408(req);
  190. }
  191. return ESP_FAIL;
  192. }
  193. buf[ret] = '\0';
  194. int val = atoi(buf);
  195. ESP_LOGI(TAG, "/adder handler read %d", val);
  196. if (! req->sess_ctx) {
  197. ESP_LOGI(TAG, "/adder allocating new session");
  198. req->sess_ctx = malloc(sizeof(int));
  199. req->free_ctx = adder_free_func;
  200. *(int *)req->sess_ctx = 0;
  201. }
  202. int *adder = (int *)req->sess_ctx;
  203. *adder += val;
  204. snprintf(outbuf, sizeof(outbuf),"%d", *adder);
  205. httpd_resp_send(req, outbuf, strlen(outbuf));
  206. return ESP_OK;
  207. }
  208. esp_err_t leftover_data_post_handler(httpd_req_t *req)
  209. {
  210. /* Only echo the first 10 bytes of the request, leaving the rest of the
  211. * request data as is.
  212. */
  213. char buf[11];
  214. int ret;
  215. /* Read data received in the request */
  216. ret = httpd_req_recv(req, buf, sizeof(buf) - 1);
  217. if (ret <= 0) {
  218. if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
  219. httpd_resp_send_408(req);
  220. }
  221. return ESP_FAIL;
  222. }
  223. buf[ret] = '\0';
  224. ESP_LOGI(TAG, "leftover data handler read %s", buf);
  225. httpd_resp_send(req, buf, strlen(buf));
  226. return ESP_OK;
  227. }
  228. int httpd_default_send(httpd_handle_t hd, int sockfd, const char *buf, unsigned buf_len, int flags);
  229. void generate_async_resp(void *arg)
  230. {
  231. char buf[250];
  232. struct async_resp_arg *resp_arg = (struct async_resp_arg *)arg;
  233. httpd_handle_t hd = resp_arg->hd;
  234. int fd = resp_arg->fd;
  235. #define HTTPD_HDR_STR "HTTP/1.1 200 OK\r\n" \
  236. "Content-Type: text/html\r\n" \
  237. "Content-Length: %d\r\n"
  238. #define STR "Hello Double World!"
  239. ESP_LOGI(TAG, "Executing queued work fd : %d", fd);
  240. snprintf(buf, sizeof(buf), HTTPD_HDR_STR,
  241. strlen(STR));
  242. httpd_default_send(hd, fd, buf, strlen(buf), 0);
  243. /* Space for sending additional headers based on set_header */
  244. httpd_default_send(hd, fd, "\r\n", strlen("\r\n"), 0);
  245. httpd_default_send(hd, fd, STR, strlen(STR), 0);
  246. #undef STR
  247. free(arg);
  248. }
  249. esp_err_t async_get_handler(httpd_req_t *req)
  250. {
  251. #define STR "Hello World!"
  252. httpd_resp_send(req, STR, strlen(STR));
  253. /* Also register a HTTPD Work which sends the same data on the same
  254. * socket again
  255. */
  256. struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg));
  257. resp_arg->hd = req->handle;
  258. resp_arg->fd = httpd_req_to_sockfd(req);
  259. if (resp_arg->fd < 0) {
  260. return ESP_FAIL;
  261. }
  262. ESP_LOGI(TAG, "Queuing work fd : %d", resp_arg->fd);
  263. httpd_queue_work(req->handle, generate_async_resp, resp_arg);
  264. return ESP_OK;
  265. #undef STR
  266. }
  267. httpd_uri_t basic_handlers[] = {
  268. { .uri = "/hello/type_html",
  269. .method = HTTP_GET,
  270. .handler = hello_type_get_handler,
  271. .user_ctx = NULL,
  272. },
  273. { .uri = "/test_header",
  274. .method = HTTP_GET,
  275. .handler = test_header_get_handler,
  276. .user_ctx = NULL,
  277. },
  278. { .uri = "/hello",
  279. .method = HTTP_GET,
  280. .handler = hello_get_handler,
  281. .user_ctx = NULL,
  282. },
  283. { .uri = "/hello/status_500",
  284. .method = HTTP_GET,
  285. .handler = hello_status_get_handler,
  286. .user_ctx = NULL,
  287. },
  288. { .uri = "/echo",
  289. .method = HTTP_POST,
  290. .handler = echo_post_handler,
  291. .user_ctx = NULL,
  292. },
  293. { .uri = "/echo",
  294. .method = HTTP_PUT,
  295. .handler = echo_post_handler,
  296. .user_ctx = NULL,
  297. },
  298. { .uri = "/leftover_data",
  299. .method = HTTP_POST,
  300. .handler = leftover_data_post_handler,
  301. .user_ctx = NULL,
  302. },
  303. { .uri = "/adder",
  304. .method = HTTP_POST,
  305. .handler = adder_post_handler,
  306. .user_ctx = NULL,
  307. },
  308. { .uri = "/async_data",
  309. .method = HTTP_GET,
  310. .handler = async_get_handler,
  311. .user_ctx = NULL,
  312. }
  313. };
  314. int basic_handlers_no = sizeof(basic_handlers)/sizeof(httpd_uri_t);
  315. void register_basic_handlers(httpd_handle_t hd)
  316. {
  317. int i;
  318. ESP_LOGI(TAG, "Registering basic handlers");
  319. ESP_LOGI(TAG, "No of handlers = %d", basic_handlers_no);
  320. for (i = 0; i < basic_handlers_no; i++) {
  321. if (httpd_register_uri_handler(hd, &basic_handlers[i]) != ESP_OK) {
  322. ESP_LOGW(TAG, "register uri failed for %d", i);
  323. return;
  324. }
  325. }
  326. ESP_LOGI(TAG, "Success");
  327. }
  328. httpd_handle_t test_httpd_start()
  329. {
  330. pre_start_mem = esp_get_free_heap_size();
  331. httpd_handle_t hd;
  332. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  333. /* Modify this setting to match the number of test URI handlers */
  334. config.max_uri_handlers = 9;
  335. config.server_port = 1234;
  336. /* This check should be a part of http_server */
  337. config.max_open_sockets = (CONFIG_LWIP_MAX_SOCKETS - 3);
  338. if (httpd_start(&hd, &config) == ESP_OK) {
  339. ESP_LOGI(TAG, "Started HTTP server on port: '%d'", config.server_port);
  340. ESP_LOGI(TAG, "Max URI handlers: '%d'", config.max_uri_handlers);
  341. ESP_LOGI(TAG, "Max Open Sessions: '%d'", config.max_open_sockets);
  342. ESP_LOGI(TAG, "Max Header Length: '%d'", HTTPD_MAX_REQ_HDR_LEN);
  343. ESP_LOGI(TAG, "Max URI Length: '%d'", HTTPD_MAX_URI_LEN);
  344. ESP_LOGI(TAG, "Max Stack Size: '%d'", config.stack_size);
  345. return hd;
  346. }
  347. return NULL;
  348. }
  349. void test_httpd_stop(httpd_handle_t hd)
  350. {
  351. httpd_stop(hd);
  352. post_stop_mem = esp_get_free_heap_size();
  353. ESP_LOGI(TAG, "HTTPD Stop: Current free memory: %d", post_stop_mem);
  354. }
  355. httpd_handle_t start_tests()
  356. {
  357. httpd_handle_t hd = test_httpd_start();
  358. if (hd) {
  359. register_basic_handlers(hd);
  360. }
  361. return hd;
  362. }
  363. void stop_tests(httpd_handle_t hd)
  364. {
  365. ESP_LOGI(TAG, "Stopping httpd");
  366. test_httpd_stop(hd);
  367. }