bpaul-http

http server that will eventually be used for bpaul.xyz
Log | Files | Refs | README

commit a54629ade1ef58d44e9bfe0ab3b3e02115e81ff0
parent 47195b3f4f5820c46b1de1742e6b6e9cb65294fe
Author: benjamin paul <bpaul@bpaul.xyz>
Date:   Sun,  1 Aug 2021 16:15:41 +1000

html response semiautomated

Diffstat:
Mhttp.c | 5+++--
Mhttp.h | 2+-
Mmain.c | 4++--
Mtcp.c | 16+---------------
4 files changed, 7 insertions(+), 20 deletions(-)

diff --git a/http.c b/http.c @@ -1,3 +1,4 @@ +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -12,8 +13,8 @@ respond(char *payload) { "\n" "%s"; - - response = malloc(4096); + length = snprintf(NULL, 0, format, strlen(payload), payload); + response = malloc(length); sprintf(response, format, strlen(payload), payload); diff --git a/http.h b/http.h @@ -1,3 +1,3 @@ #pragma once -char *respond(char *request, char *payload); +char *respond(char *payload); diff --git a/main.c b/main.c @@ -3,7 +3,7 @@ #include <string.h> #include <unistd.h> -#include "html.h" +#include "http.h" #include "tcp.h" int @@ -26,7 +26,7 @@ main() { char *request = read_socket(asock); printf("%s", request); - char *text = respond(request, buf); + char *text = respond(buf); write(asock, text, strlen(text)); } } diff --git a/tcp.c b/tcp.c @@ -24,7 +24,7 @@ create_socket() { exit(1); } - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &(bool){true}, sizeof(bool)); + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)); addr.sin_family = AF_INET; addr.sin_port = htons(PORT); @@ -39,20 +39,6 @@ create_socket() { exit(1); } - /* - public_addr.sin_family = AF_INET; - public_addr.sin_port = htons(PORT); - if (!inet_aton("192.168.50.22", &public_addr.sin_addr)) { - perror("inet_aton public_addr"); - exit(1); - } - - if (connect(sock, (struct sockaddr *)&public_addr, sizeof(public_addr)) == -1) { - perror("connect"); - exit(1); - } - */ - /* Listen for connections on the socket */ if (listen(sock, BACKLOG) == -1) { perror("listen");