added response func

This commit is contained in:
Win 2024-05-22 20:58:28 +07:00
parent 802e974269
commit 76f17729b4
6 changed files with 33 additions and 48 deletions

View File

@ -1,4 +1,4 @@
CC=clang
CC=/usr/bin/clang
all: webserver.c
$(CC) webserver.c -o webserver

BIN
goffy

Binary file not shown.

24
goffy.c
View File

@ -1,24 +0,0 @@
#include <stdio.h>
#include <string.h>
int main() {
const char src[20] = "HA! poopoohead!";
char dest[100];
int eh = sprintf(dest, "%s", "goofy");
memcpy(dest + 6, src, strlen(src) + 1);
printf("Output: %s\n", dest);
printf("src[0] %p\n", &src[0]);
printf("src[0] %p\n\n", src);
printf("src[1] %p\n", &src[1]);
printf("src[1] %p\n", src + 1);
printf("src[50] %p\n", src + 49);
printf("%ld", sizeof(dest));
return 0;
}

BIN
server

Binary file not shown.

View File

@ -10,10 +10,6 @@
#include "server.h"
/*
TODO: CREATE SENDING RESPONSE FUNCTION
int send_response(int fd, char *header, char *content_type, void *body, int content_length) {
const int max_response_size = 65536;
char response[max_response_size];
@ -25,17 +21,26 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont
int response_length = sprintf(response,
"%s\n"
"Date: %s"
"Server: %s\n"
"Date: %s\n"
"Connection: close\n"
"Content-Length: %d\n"
"Content-Type: %s\n"
"\n",
header, asctime(info), content_length, content_type);
header, SERVER_VERSION, asctime(info), content_length, content_type);
memcpy(response + response_length, body, content_length);
}
printf("%s\n", response);
*/
write(fd, response, response_length);
//if (write < 0) {
// perror("webserver (write)");
// return write;
//}
return 0;
}
int main() {
char buffer[BUFFER_SIZE];
@ -126,20 +131,24 @@ int main() {
continue;
}
} else {
char resp[100];
int resp_size = snprintf(resp, 100,
"%s\r\n"
"Server: %s\r\n"
"Content-Type: %s\r\n"
"\r\n",
OK_HEAD, SERVER_VERSION, "text/html");
// char resp[100];
// int resp_size = snprintf(resp, 100,
// "%s\r\n"
// "Server: %s\r\n"
// "Content-Type: %s\r\n"
// "\r\n",
// OK_HEAD, SERVER_VERSION, "text/html");
//
// int valwrite = write(newsockfd, resp, resp_size);
// printf("%s\n", OK_HEAD);
// if (valwrite < 0) {
// perror("webserver (write)");
// continue;
// }
int valwrite = write(newsockfd, resp, resp_size);
printf("%s\n", OK_HEAD);
if (valwrite < 0) {
perror("webserver (write)");
continue;
}
char body[100];
int body_size = snprintf(body, 100, "%s\r\n", "hello");
send_response(newsockfd, "HTTP/1.1 200 OK", "text/html", "deeznuts", sizeof(body_size));
}
// write to the socket

View File

@ -1,7 +1,7 @@
#define PORT 8080
#define BUFFER_SIZE 1024
static const char SERVER_VERSION[] = "winnie-http-server/0.0.1";
static const char SERVER_VERSION[] = "winnie-http-server";
static const char BAD_REQUEST_HEAD[] = "HTTP/1.1 400 Bad Request";
static const char NOT_FOUND_HEAD[] = "HTTP/1.1 404 Not Found";