Add README.md, ensure listening port is printed
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 2s
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 2s
This commit is contained in:
parent
d89117430d
commit
a9864542b3
2 changed files with 41 additions and 2 deletions
26
README.md
Normal file
26
README.md
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
# C Simple HTTP
|
||||||
|
|
||||||
|
A simple HTTP/1.1 server written in C.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
-p <port> | --port <port>
|
||||||
|
--config=<config_file>
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
# Build the project.
|
||||||
|
make c_simple_http
|
||||||
|
|
||||||
|
# Run it with the example config.
|
||||||
|
./c_simple_http --config=example_config/example.config
|
||||||
|
|
||||||
|
# If port is not specified, the server picks a random port.
|
||||||
|
# This program should print which TCP port it is listening on.
|
||||||
|
# Sometimes the program will fail to rebind to the same port due to how TCP
|
||||||
|
# works. Either wait some time or choose a different port.
|
||||||
|
|
||||||
|
# Access the website.
|
||||||
|
# This assumes the server is hosted on port 3000.
|
||||||
|
curl localhost:3000
|
17
src/main.c
17
src/main.c
|
@ -48,8 +48,7 @@ int main(int argc, char **argv) {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("listening on port: %u\n", args.port);
|
printf("Config file is: %s\n", args.config_file);
|
||||||
printf("config file is: %s\n", args.config_file);
|
|
||||||
|
|
||||||
__attribute__((cleanup(c_simple_http_clean_up_parsed_config)))
|
__attribute__((cleanup(c_simple_http_clean_up_parsed_config)))
|
||||||
C_SIMPLE_HTTP_ParsedConfig parsed_config = c_simple_http_parse_config(
|
C_SIMPLE_HTTP_ParsedConfig parsed_config = c_simple_http_parse_config(
|
||||||
|
@ -64,6 +63,20 @@ int main(int argc, char **argv) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
struct sockaddr_in6 ipv6_addr;
|
||||||
|
memset(&ipv6_addr, 0, sizeof(struct sockaddr_in6));
|
||||||
|
socklen_t size = sizeof(ipv6_addr);
|
||||||
|
int ret = getsockname(tcp_socket, (struct sockaddr*)&ipv6_addr, &size);
|
||||||
|
if (ret == 0) {
|
||||||
|
printf("Listening on port: %u\n", u16_be_swap(ipv6_addr.sin6_port));
|
||||||
|
} else {
|
||||||
|
fprintf(
|
||||||
|
stderr,
|
||||||
|
"WARNING Failed to get info on tcp socket to print port number!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct timespec sleep_time;
|
struct timespec sleep_time;
|
||||||
sleep_time.tv_sec = 0;
|
sleep_time.tv_sec = 0;
|
||||||
sleep_time.tv_nsec = C_SIMPLE_HTTP_SLEEP_NANOS;
|
sleep_time.tv_nsec = C_SIMPLE_HTTP_SLEEP_NANOS;
|
||||||
|
|
Loading…
Reference in a new issue