added get request check

This commit is contained in:
Win 2024-05-15 20:47:03 +07:00
parent 7d98124fbc
commit d3c75b26fc
2 changed files with 14 additions and 3 deletions

View File

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

View File

@ -1,7 +1,8 @@
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
@ -77,6 +78,16 @@ int main() {
sscanf(buffer, "%s %s %s", method, uri, version);
printf("[%s:%u] %s %s %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), method, version, uri);
// check if request is GET request
if (strncmp("GET", method, 7) != 0) {
const char resp[] = "HTTP/1.0 400 Bad Request\r\n"
"Server: webserver-c\r\n"
"Content-type: text/html\r\n\r\n";
write(newsockfd, resp, strlen(resp));
} else {
// TO DO: IMPLEMENT FILE READING
}
// write to the socket
int valwrite = write(newsockfd, resp, strlen(resp));
if (valwrite < 0) {