1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_DECLARE(net_shell);
9 
10 #include "net_shell_private.h"
11 #include <zephyr/net/http/service.h>
12 #include <zephyr/net/http/server.h>
13 #include <zephyr/net/http/method.h>
14 #include <zephyr/net/http/parser.h>
15 #include <zephyr/sys/util.h>
16 
cmd_net_http(const struct shell * sh,size_t argc,char * argv[])17 static int cmd_net_http(const struct shell *sh, size_t argc, char *argv[])
18 {
19 #if defined(CONFIG_HTTP_SERVER)
20 	int res_count = 0, serv_count = 0;
21 
22 	PR("%-15s\t%-12s\n",
23 	   "Host:Port", "Concurrent/Backlog");
24 	PR("\tResource type\tMethods\t\tEndpoint\n");
25 
26 	HTTP_SERVICE_FOREACH(svc) {
27 		PR("\n");
28 		PR("%s:%d\t%zu/%zu\n",
29 		   svc->host == NULL || svc->host[0] == '\0' ?
30 		   "<any>" : svc->host, svc->port ? *svc->port : 0,
31 		   svc->concurrent, svc->backlog);
32 
33 		HTTP_SERVICE_FOREACH_RESOURCE(svc, res) {
34 			struct http_resource_detail *detail = res->detail;
35 			const char *detail_type = "<unknown>";
36 			int method_count = 0;
37 			bool print_comma;
38 
39 			switch (detail->type) {
40 			case HTTP_RESOURCE_TYPE_STATIC:
41 				detail_type = "static";
42 				break;
43 			case HTTP_RESOURCE_TYPE_STATIC_FS:
44 				detail_type = "static_fs";
45 				break;
46 			case HTTP_RESOURCE_TYPE_DYNAMIC:
47 				detail_type = "dynamic";
48 				break;
49 			case HTTP_RESOURCE_TYPE_WEBSOCKET:
50 				detail_type = "websocket";
51 				break;
52 			}
53 
54 			PR("\t%12s\t", detail_type);
55 
56 			print_comma = false;
57 
58 			for (int i = 0; i < NUM_BITS(uint32_t); i++) {
59 				if (IS_BIT_SET(detail->bitmask_of_supported_http_methods, i)) {
60 					PR("%s%s", print_comma ? "," : "", http_method_str(i));
61 					print_comma = true;
62 					method_count++;
63 				}
64 			}
65 
66 			if (method_count < 2) {
67 				/* make columns line up better */
68 				PR("\t");
69 			}
70 
71 			PR("\t%s\n", res->resource);
72 			res_count++;
73 		}
74 
75 		serv_count++;
76 	}
77 
78 	if (res_count == 0 && serv_count == 0) {
79 		PR("No HTTP services and resources found.\n");
80 	} else {
81 		PR("\n%d service%sand %d resource%sfound.\n",
82 		   serv_count, serv_count > 1 ? "s " : " ",
83 		   res_count, res_count > 1 ? "s " : " ");
84 	}
85 
86 #else /* CONFIG_HTTP_SERVER */
87 	ARG_UNUSED(argc);
88 	ARG_UNUSED(argv);
89 
90 	PR_INFO("Set %s to enable %s support.\n",
91 		"CONFIG_HTTP_SERVER",
92 		"HTTP information");
93 #endif
94 
95 	return 0;
96 }
97 
98 SHELL_SUBCMD_ADD((net), http, NULL,
99 		 "Show HTTP services.",
100 		 cmd_net_http, 1, 0);
101